1 .. SPDX-License-Identifier: GPL-2.0-only
2 .. Copyright (C) 2022 Meta Platforms, Inc. and affiliates.
4 =========================
5 BPF_MAP_TYPE_CGRP_STORAGE
6 =========================
8 The ``BPF_MAP_TYPE_CGRP_STORAGE`` map type represents a local fix-sized
9 storage for cgroups. It is only available with ``CONFIG_CGROUPS``.
10 The programs are made available by the same Kconfig. The
11 data for a particular cgroup can be retrieved by looking up the map
14 This document describes the usage and semantics of the
15 ``BPF_MAP_TYPE_CGRP_STORAGE`` map type.
20 The map key must be ``sizeof(int)`` representing a cgroup fd.
21 To access the storage in a program, use ``bpf_cgrp_storage_get``::
23 void *bpf_cgrp_storage_get(struct bpf_map *map, struct cgroup *cgroup, void *value, u64 flags)
25 ``flags`` could be 0 or ``BPF_LOCAL_STORAGE_GET_F_CREATE`` which indicates that
26 a new local storage will be created if one does not exist.
28 The local storage can be removed with ``bpf_cgrp_storage_delete``::
30 long bpf_cgrp_storage_delete(struct bpf_map *map, struct cgroup *cgroup)
32 The map is available to all program types.
37 A BPF program example with BPF_MAP_TYPE_CGRP_STORAGE::
40 #include <bpf/bpf_helpers.h>
41 #include <bpf/bpf_tracing.h>
44 __uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
45 __uint(map_flags, BPF_F_NO_PREALLOC);
48 } cgrp_storage SEC(".maps");
50 SEC("tp_btf/sys_enter")
51 int BPF_PROG(on_enter, struct pt_regs *regs, long id)
53 struct task_struct *task = bpf_get_current_task_btf();
56 ptr = bpf_cgrp_storage_get(&cgrp_storage, task->cgroups->dfl_cgrp, 0,
57 BPF_LOCAL_STORAGE_GET_F_CREATE);
59 __sync_fetch_and_add(ptr, 1);
64 Userspace accessing map declared above::
66 #include <linux/bpf.h>
67 #include <linux/libbpf.h>
69 __u32 map_lookup(struct bpf_map *map, int cgrp_fd)
72 value = bpf_map_lookup_elem(bpf_map__fd(map), &cgrp_fd);
78 Difference Between BPF_MAP_TYPE_CGRP_STORAGE and BPF_MAP_TYPE_CGROUP_STORAGE
79 ============================================================================
81 The old cgroup storage map ``BPF_MAP_TYPE_CGROUP_STORAGE`` has been marked as
82 deprecated (renamed to ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED``). The new
83 ``BPF_MAP_TYPE_CGRP_STORAGE`` map should be used instead. The following
84 illusates the main difference between ``BPF_MAP_TYPE_CGRP_STORAGE`` and
85 ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED``.
87 (1). ``BPF_MAP_TYPE_CGRP_STORAGE`` can be used by all program types while
88 ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED`` is available only to cgroup program types
89 like BPF_CGROUP_INET_INGRESS or BPF_CGROUP_SOCK_OPS, etc.
91 (2). ``BPF_MAP_TYPE_CGRP_STORAGE`` supports local storage for more than one
92 cgroup while ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED`` only supports one cgroup
93 which is attached by a BPF program.
95 (3). ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED`` allocates local storage at attach time so
96 ``bpf_get_local_storage()`` always returns non-NULL local storage.
97 ``BPF_MAP_TYPE_CGRP_STORAGE`` allocates local storage at runtime so
98 it is possible that ``bpf_cgrp_storage_get()`` may return null local storage.
99 To avoid such null local storage issue, user space can do
100 ``bpf_map_update_elem()`` to pre-allocate local storage before a BPF program
103 (4). ``BPF_MAP_TYPE_CGRP_STORAGE`` supports deleting local storage by a BPF program
104 while ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED`` only deletes storage during
107 So overall, ``BPF_MAP_TYPE_CGRP_STORAGE`` supports all ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED``
108 functionality and beyond. It is recommended to use ``BPF_MAP_TYPE_CGRP_STORAGE``
109 instead of ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED``.