Skip to content

Commit de9cbba

Browse files
rgushchinborkmann
authored andcommitted
bpf: introduce cgroup storage maps
This commit introduces BPF_MAP_TYPE_CGROUP_STORAGE maps: a special type of maps which are implementing the cgroup storage. >From the userspace point of view it's almost a generic hash map with the (cgroup inode id, attachment type) pair used as a key. The only difference is that some operations are restricted: 1) a user can't create new entries, 2) a user can't remove existing entries. The lookup from userspace is o(log(n)). Signed-off-by: Roman Gushchin <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Daniel Borkmann <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 0a4c58f commit de9cbba

File tree

8 files changed

+440
-0
lines changed

8 files changed

+440
-0
lines changed

include/linux/bpf-cgroup.h

+38
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,39 @@
44

55
#include <linux/errno.h>
66
#include <linux/jump_label.h>
7+
#include <linux/rbtree.h>
78
#include <uapi/linux/bpf.h>
89

910
struct sock;
1011
struct sockaddr;
1112
struct cgroup;
1213
struct sk_buff;
14+
struct bpf_map;
15+
struct bpf_prog;
1316
struct bpf_sock_ops_kern;
17+
struct bpf_cgroup_storage;
1418

1519
#ifdef CONFIG_CGROUP_BPF
1620

1721
extern struct static_key_false cgroup_bpf_enabled_key;
1822
#define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key)
1923

24+
struct bpf_cgroup_storage_map;
25+
26+
struct bpf_storage_buffer {
27+
struct rcu_head rcu;
28+
char data[0];
29+
};
30+
31+
struct bpf_cgroup_storage {
32+
struct bpf_storage_buffer *buf;
33+
struct bpf_cgroup_storage_map *map;
34+
struct bpf_cgroup_storage_key key;
35+
struct list_head list;
36+
struct rb_node node;
37+
struct rcu_head rcu;
38+
};
39+
2040
struct bpf_prog_list {
2141
struct list_head node;
2242
struct bpf_prog *prog;
@@ -77,6 +97,15 @@ int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
7797
int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
7898
short access, enum bpf_attach_type type);
7999

100+
struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(struct bpf_prog *prog);
101+
void bpf_cgroup_storage_free(struct bpf_cgroup_storage *storage);
102+
void bpf_cgroup_storage_link(struct bpf_cgroup_storage *storage,
103+
struct cgroup *cgroup,
104+
enum bpf_attach_type type);
105+
void bpf_cgroup_storage_unlink(struct bpf_cgroup_storage *storage);
106+
int bpf_cgroup_storage_assign(struct bpf_prog *prog, struct bpf_map *map);
107+
void bpf_cgroup_storage_release(struct bpf_prog *prog, struct bpf_map *map);
108+
80109
/* Wrappers for __cgroup_bpf_run_filter_skb() guarded by cgroup_bpf_enabled. */
81110
#define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb) \
82111
({ \
@@ -221,6 +250,15 @@ static inline int cgroup_bpf_prog_query(const union bpf_attr *attr,
221250
return -EINVAL;
222251
}
223252

253+
static inline int bpf_cgroup_storage_assign(struct bpf_prog *prog,
254+
struct bpf_map *map) { return 0; }
255+
static inline void bpf_cgroup_storage_release(struct bpf_prog *prog,
256+
struct bpf_map *map) {}
257+
static inline struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(
258+
struct bpf_prog *prog) { return 0; }
259+
static inline void bpf_cgroup_storage_free(
260+
struct bpf_cgroup_storage *storage) {}
261+
224262
#define cgroup_bpf_enabled (0)
225263
#define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (0)
226264
#define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) ({ 0; })

include/linux/bpf.h

+1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ struct bpf_prog_aux {
282282
struct bpf_prog *prog;
283283
struct user_struct *user;
284284
u64 load_time; /* ns since boottime */
285+
struct bpf_map *cgroup_storage;
285286
char name[BPF_OBJ_NAME_LEN];
286287
#ifdef CONFIG_SECURITY
287288
void *security;

include/linux/bpf_types.h

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_PERF_EVENT_ARRAY, perf_event_array_map_ops)
3737
#ifdef CONFIG_CGROUPS
3838
BPF_MAP_TYPE(BPF_MAP_TYPE_CGROUP_ARRAY, cgroup_array_map_ops)
3939
#endif
40+
#ifdef CONFIG_CGROUP_BPF
41+
BPF_MAP_TYPE(BPF_MAP_TYPE_CGROUP_STORAGE, cgroup_storage_map_ops)
42+
#endif
4043
BPF_MAP_TYPE(BPF_MAP_TYPE_HASH, htab_map_ops)
4144
BPF_MAP_TYPE(BPF_MAP_TYPE_PERCPU_HASH, htab_percpu_map_ops)
4245
BPF_MAP_TYPE(BPF_MAP_TYPE_LRU_HASH, htab_lru_map_ops)

include/uapi/linux/bpf.h

+6
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ struct bpf_lpm_trie_key {
7575
__u8 data[0]; /* Arbitrary size */
7676
};
7777

78+
struct bpf_cgroup_storage_key {
79+
__u64 cgroup_inode_id; /* cgroup inode id */
80+
__u32 attach_type; /* program attach type */
81+
};
82+
7883
/* BPF syscall commands, see bpf(2) man-page for details. */
7984
enum bpf_cmd {
8085
BPF_MAP_CREATE,
@@ -120,6 +125,7 @@ enum bpf_map_type {
120125
BPF_MAP_TYPE_CPUMAP,
121126
BPF_MAP_TYPE_XSKMAP,
122127
BPF_MAP_TYPE_SOCKHASH,
128+
BPF_MAP_TYPE_CGROUP_STORAGE,
123129
};
124130

125131
enum bpf_prog_type {

kernel/bpf/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ obj-y := core.o
33

44
obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o
55
obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o
6+
obj-$(CONFIG_BPF_SYSCALL) += local_storage.o
67
obj-$(CONFIG_BPF_SYSCALL) += disasm.o
78
obj-$(CONFIG_BPF_SYSCALL) += btf.o
89
ifeq ($(CONFIG_NET),y)

0 commit comments

Comments
 (0)