Skip to content

Commit 36f7248

Browse files
author
Alexei Starovoitov
committed
Merge branch 'shared-cgroup-storage'
YiFei Zhu says: ==================== To access the storage in a CGROUP_STORAGE map, one uses bpf_get_local_storage helper, which is extremely fast due to its use of per-CPU variables. However, its whole code is built on the assumption that one map can only be used by one program at any time, and this prohibits any sharing of data between multiple programs using these maps, eliminating a lot of use cases, such as some per-cgroup configuration storage, written to by a setsockopt program and read by a cg_sock_addr program. Why not use other map types? The great part of CGROUP_STORAGE map is that it is isolated by different cgroups its attached to. When one program uses bpf_get_local_storage, even on the same map, it gets different storages if it were run as a result of attaching to different cgroups. The kernel manages the storages, simplifying BPF program or userspace. In theory, one could probably use other maps like array or hash to do the same thing, but it would be a major overhead / complexity. Userspace needs to know when a cgroup is being freed in order to free up a space in the replacement map. This patch set introduces a significant change to the semantics of CGROUP_STORAGE map type. Instead of each storage being tied to one single attachment, it is shared across different attachments to the same cgroup, and persists until either the map or the cgroup attached to is being freed. User may use u64 as the key to the map, and the result would be that the attach type become ignored during key comparison, and programs of different attach types will share the same storage if the cgroups they are attached to are the same. How could this break existing users? * Users that uses detach & reattach / program replacement as a shortcut to zeroing the storage. Since we need sharing between programs, we cannot zero the storage. Users that expect this behavior should either attach a program with a new map, or explicitly zero the map with a syscall. This case is dependent on undocumented implementation details, so the impact should be very minimal. Patch 1 introduces a test on the old expected behavior of the map type. Patch 2 introduces a test showing how two programs cannot share one such map. Patch 3 implements the change of semantics to the map. Patch 4 amends the new test such that it yields the behavior we expect from the change. Patch 5 documents the map type. Changes since RFC: * Clarify commit message in patch 3 such that it says the lifetime of the storage is ended at the freeing of the cgroup_bpf, rather than the cgroup itself. * Restored an -ENOMEM check in __cgroup_bpf_attach. * Update selftests for recent change in network_helpers API. Changes since v1: * s/CHECK_FAIL/CHECK/ * s/bpf_prog_attach/bpf_program__attach_cgroup/ * Moved test__start_subtest to test_cg_storage_multi. * Removed some redundant CHECK_FAIL where they are already CHECK-ed. Changes since v2: * Lock cgroup_mutex during map_free. * Publish new storages only if attach is successful, by tracking exactly which storages are reused in an array of bools. * Mention bpftool map dump showing a value of zero for attach_type in patch 3 commit message. Changes since v3: * Use a much simpler lookup and allocate-if-not-exist from the fact that cgroup_mutex is locked during attach. * Removed an unnecessary spinlock hold. Changes since v4: * Changed semantics so that if the key type is struct bpf_cgroup_storage_key the map retains isolation between different attach types. Sharing between different attach types only occur when key type is u64. * Adapted tests and docs for the above change. Changes since v5: * Removed redundant NULL check before bpf_link__destroy. * Free BPF object explicitly, after asserting that object failed to load, in the event that the object did not fail to load. * Rename variable in bpf_cgroup_storage_key_cmp for clarity. * Added a lot of information to Documentation, more or less copied from what Martin KaFai Lau wrote. ==================== Acked-by: Martin KaFai Lau <kafai@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents 90065c0 + 4e15f46 commit 36f7248

11 files changed

Lines changed: 905 additions & 143 deletions

File tree

‎Documentation/bpf/index.rst‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ Program types
4848
bpf_lsm
4949

5050

51+
Map types
52+
=========
53+
54+
.. toctree::
55+
:maxdepth: 1
56+
57+
map_cgroup_storage
58+
59+
5160
Testing and debugging BPF
5261
=========================
5362

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
.. SPDX-License-Identifier: GPL-2.0-only
2+
.. Copyright (C) 2020 Google LLC.
3+
4+
===========================
5+
BPF_MAP_TYPE_CGROUP_STORAGE
6+
===========================
7+
8+
The ``BPF_MAP_TYPE_CGROUP_STORAGE`` map type represents a local fix-sized
9+
storage. It is only available with ``CONFIG_CGROUP_BPF``, and to programs that
10+
attach to cgroups; the programs are made available by the same Kconfig. The
11+
storage is identified by the cgroup the program is attached to.
12+
13+
The map provide a local storage at the cgroup that the BPF program is attached
14+
to. It provides a faster and simpler access than the general purpose hash
15+
table, which performs a hash table lookups, and requires user to track live
16+
cgroups on their own.
17+
18+
This document describes the usage and semantics of the
19+
``BPF_MAP_TYPE_CGROUP_STORAGE`` map type. Some of its behaviors was changed in
20+
Linux 5.9 and this document will describe the differences.
21+
22+
Usage
23+
=====
24+
25+
The map uses key of type of either ``__u64 cgroup_inode_id`` or
26+
``struct bpf_cgroup_storage_key``, declared in ``linux/bpf.h``::
27+
28+
struct bpf_cgroup_storage_key {
29+
__u64 cgroup_inode_id;
30+
__u32 attach_type;
31+
};
32+
33+
``cgroup_inode_id`` is the inode id of the cgroup directory.
34+
``attach_type`` is the the program's attach type.
35+
36+
Linux 5.9 added support for type ``__u64 cgroup_inode_id`` as the key type.
37+
When this key type is used, then all attach types of the particular cgroup and
38+
map will share the same storage. Otherwise, if the type is
39+
``struct bpf_cgroup_storage_key``, then programs of different attach types
40+
be isolated and see different storages.
41+
42+
To access the storage in a program, use ``bpf_get_local_storage``::
43+
44+
void *bpf_get_local_storage(void *map, u64 flags)
45+
46+
``flags`` is reserved for future use and must be 0.
47+
48+
There is no implicit synchronization. Storages of ``BPF_MAP_TYPE_CGROUP_STORAGE``
49+
can be accessed by multiple programs across different CPUs, and user should
50+
take care of synchronization by themselves. The bpf infrastructure provides
51+
``struct bpf_spin_lock`` to synchronize the storage. See
52+
``tools/testing/selftests/bpf/progs/test_spin_lock.c``.
53+
54+
Examples
55+
========
56+
57+
Usage with key type as ``struct bpf_cgroup_storage_key``::
58+
59+
#include <bpf/bpf.h>
60+
61+
struct {
62+
__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
63+
__type(key, struct bpf_cgroup_storage_key);
64+
__type(value, __u32);
65+
} cgroup_storage SEC(".maps");
66+
67+
int program(struct __sk_buff *skb)
68+
{
69+
__u32 *ptr = bpf_get_local_storage(&cgroup_storage, 0);
70+
__sync_fetch_and_add(ptr, 1);
71+
72+
return 0;
73+
}
74+
75+
Userspace accessing map declared above::
76+
77+
#include <linux/bpf.h>
78+
#include <linux/libbpf.h>
79+
80+
__u32 map_lookup(struct bpf_map *map, __u64 cgrp, enum bpf_attach_type type)
81+
{
82+
struct bpf_cgroup_storage_key = {
83+
.cgroup_inode_id = cgrp,
84+
.attach_type = type,
85+
};
86+
__u32 value;
87+
bpf_map_lookup_elem(bpf_map__fd(map), &key, &value);
88+
// error checking omitted
89+
return value;
90+
}
91+
92+
Alternatively, using just ``__u64 cgroup_inode_id`` as key type::
93+
94+
#include <bpf/bpf.h>
95+
96+
struct {
97+
__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
98+
__type(key, __u64);
99+
__type(value, __u32);
100+
} cgroup_storage SEC(".maps");
101+
102+
int program(struct __sk_buff *skb)
103+
{
104+
__u32 *ptr = bpf_get_local_storage(&cgroup_storage, 0);
105+
__sync_fetch_and_add(ptr, 1);
106+
107+
return 0;
108+
}
109+
110+
And userspace::
111+
112+
#include <linux/bpf.h>
113+
#include <linux/libbpf.h>
114+
115+
__u32 map_lookup(struct bpf_map *map, __u64 cgrp, enum bpf_attach_type type)
116+
{
117+
__u32 value;
118+
bpf_map_lookup_elem(bpf_map__fd(map), &cgrp, &value);
119+
// error checking omitted
120+
return value;
121+
}
122+
123+
Semantics
124+
=========
125+
126+
``BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE`` is a variant of this map type. This
127+
per-CPU variant will have different memory regions for each CPU for each
128+
storage. The non-per-CPU will have the same memory region for each storage.
129+
130+
Prior to Linux 5.9, the lifetime of a storage is precisely per-attachment, and
131+
for a single ``CGROUP_STORAGE`` map, there can be at most one program loaded
132+
that uses the map. A program may be attached to multiple cgroups or have
133+
multiple attach types, and each attach creates a fresh zeroed storage. The
134+
storage is freed upon detach.
135+
136+
There is a one-to-one association between the map of each type (per-CPU and
137+
non-per-CPU) and the BPF program during load verification time. As a result,
138+
each map can only be used by one BPF program and each BPF program can only use
139+
one storage map of each type. Because of map can only be used by one BPF
140+
program, sharing of this cgroup's storage with other BPF programs were
141+
impossible.
142+
143+
Since Linux 5.9, storage can be shared by multiple programs. When a program is
144+
attached to a cgroup, the kernel would create a new storage only if the map
145+
does not already contain an entry for the cgroup and attach type pair, or else
146+
the old storage is reused for the new attachment. If the map is attach type
147+
shared, then attach type is simply ignored during comparison. Storage is freed
148+
only when either the map or the cgroup attached to is being freed. Detaching
149+
will not directly free the storage, but it may cause the reference to the map
150+
to reach zero and indirectly freeing all storage in the map.
151+
152+
The map is not associated with any BPF program, thus making sharing possible.
153+
However, the BPF program can still only associate with one map of each type
154+
(per-CPU and non-per-CPU). A BPF program cannot use more than one
155+
``BPF_MAP_TYPE_CGROUP_STORAGE`` or more than one
156+
``BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE``.
157+
158+
In all versions, userspace may use the the attach parameters of cgroup and
159+
attach type pair in ``struct bpf_cgroup_storage_key`` as the key to the BPF map
160+
APIs to read or update the storage for a given attachment. For Linux 5.9
161+
attach type shared storages, only the first value in the struct, cgroup inode
162+
id, is used during comparison, so userspace may just specify a ``__u64``
163+
directly.
164+
165+
The storage is bound at attach time. Even if the program is attached to parent
166+
and triggers in child, the storage still belongs to the parent.
167+
168+
Userspace cannot create a new entry in the map or delete an existing entry.
169+
Program test runs always use a temporary storage.

‎include/linux/bpf-cgroup.h‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ struct bpf_cgroup_storage {
4646
};
4747
struct bpf_cgroup_storage_map *map;
4848
struct bpf_cgroup_storage_key key;
49-
struct list_head list;
49+
struct list_head list_map;
50+
struct list_head list_cg;
5051
struct rb_node node;
5152
struct rcu_head rcu;
5253
};
@@ -78,6 +79,9 @@ struct cgroup_bpf {
7879
struct list_head progs[MAX_BPF_ATTACH_TYPE];
7980
u32 flags[MAX_BPF_ATTACH_TYPE];
8081

82+
/* list of cgroup shared storages */
83+
struct list_head storages;
84+
8185
/* temp storage for effective prog array used by prog_attach/detach */
8286
struct bpf_prog_array *inactive;
8387

@@ -161,6 +165,9 @@ static inline void bpf_cgroup_storage_set(struct bpf_cgroup_storage
161165
this_cpu_write(bpf_cgroup_storage[stype], storage[stype]);
162166
}
163167

168+
struct bpf_cgroup_storage *
169+
cgroup_storage_lookup(struct bpf_cgroup_storage_map *map,
170+
void *key, bool locked);
164171
struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(struct bpf_prog *prog,
165172
enum bpf_cgroup_storage_type stype);
166173
void bpf_cgroup_storage_free(struct bpf_cgroup_storage *storage);
@@ -169,7 +176,6 @@ void bpf_cgroup_storage_link(struct bpf_cgroup_storage *storage,
169176
enum bpf_attach_type type);
170177
void bpf_cgroup_storage_unlink(struct bpf_cgroup_storage *storage);
171178
int bpf_cgroup_storage_assign(struct bpf_prog_aux *aux, struct bpf_map *map);
172-
void bpf_cgroup_storage_release(struct bpf_prog_aux *aux, struct bpf_map *map);
173179

174180
int bpf_percpu_cgroup_storage_copy(struct bpf_map *map, void *key, void *value);
175181
int bpf_percpu_cgroup_storage_update(struct bpf_map *map, void *key,
@@ -383,8 +389,6 @@ static inline void bpf_cgroup_storage_set(
383389
struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE]) {}
384390
static inline int bpf_cgroup_storage_assign(struct bpf_prog_aux *aux,
385391
struct bpf_map *map) { return 0; }
386-
static inline void bpf_cgroup_storage_release(struct bpf_prog_aux *aux,
387-
struct bpf_map *map) {}
388392
static inline struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(
389393
struct bpf_prog *prog, enum bpf_cgroup_storage_type stype) { return NULL; }
390394
static inline void bpf_cgroup_storage_free(

0 commit comments

Comments
 (0)