xref: /linux/kernel/bpf/bpf_cgrp_storage.c (revision e2683c8868d03382da7e1ce8453b543a043066d1)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
4  */
5 
6 #include <linux/types.h>
7 #include <linux/bpf.h>
8 #include <linux/bpf_local_storage.h>
9 #include <uapi/linux/btf.h>
10 #include <linux/btf_ids.h>
11 
12 DEFINE_BPF_STORAGE_CACHE(cgroup_cache);
13 
14 static struct bpf_local_storage __rcu **cgroup_storage_ptr(void *owner)
15 {
16 	struct cgroup *cg = owner;
17 
18 	return &cg->bpf_cgrp_storage;
19 }
20 
21 void bpf_cgrp_storage_free(struct cgroup *cgroup)
22 {
23 	struct bpf_local_storage *local_storage;
24 
25 	rcu_read_lock();
26 	local_storage = rcu_dereference(cgroup->bpf_cgrp_storage);
27 	if (!local_storage)
28 		goto out;
29 
30 	bpf_local_storage_destroy(local_storage);
31 out:
32 	rcu_read_unlock();
33 }
34 
35 static struct bpf_local_storage_data *
36 cgroup_storage_lookup(struct cgroup *cgroup, struct bpf_map *map, bool cacheit_lockit)
37 {
38 	struct bpf_local_storage *cgroup_storage;
39 	struct bpf_local_storage_map *smap;
40 
41 	cgroup_storage = rcu_dereference_check(cgroup->bpf_cgrp_storage,
42 					       bpf_rcu_lock_held());
43 	if (!cgroup_storage)
44 		return NULL;
45 
46 	smap = (struct bpf_local_storage_map *)map;
47 	return bpf_local_storage_lookup(cgroup_storage, smap, cacheit_lockit);
48 }
49 
50 static void *bpf_cgrp_storage_lookup_elem(struct bpf_map *map, void *key)
51 {
52 	struct bpf_local_storage_data *sdata;
53 	struct cgroup *cgroup;
54 	int fd;
55 
56 	fd = *(int *)key;
57 	cgroup = cgroup_v1v2_get_from_fd(fd);
58 	if (IS_ERR(cgroup))
59 		return ERR_CAST(cgroup);
60 
61 	sdata = cgroup_storage_lookup(cgroup, map, true);
62 	cgroup_put(cgroup);
63 	return sdata ? sdata->data : NULL;
64 }
65 
66 static long bpf_cgrp_storage_update_elem(struct bpf_map *map, void *key,
67 					 void *value, u64 map_flags)
68 {
69 	struct bpf_local_storage_data *sdata;
70 	struct cgroup *cgroup;
71 	int fd;
72 
73 	fd = *(int *)key;
74 	cgroup = cgroup_v1v2_get_from_fd(fd);
75 	if (IS_ERR(cgroup))
76 		return PTR_ERR(cgroup);
77 
78 	sdata = bpf_local_storage_update(cgroup, (struct bpf_local_storage_map *)map,
79 					 value, map_flags, false);
80 	cgroup_put(cgroup);
81 	return PTR_ERR_OR_ZERO(sdata);
82 }
83 
84 static int cgroup_storage_delete(struct cgroup *cgroup, struct bpf_map *map)
85 {
86 	struct bpf_local_storage_data *sdata;
87 
88 	sdata = cgroup_storage_lookup(cgroup, map, false);
89 	if (!sdata)
90 		return -ENOENT;
91 
92 	return bpf_selem_unlink(SELEM(sdata));
93 }
94 
95 static long bpf_cgrp_storage_delete_elem(struct bpf_map *map, void *key)
96 {
97 	struct cgroup *cgroup;
98 	int err, fd;
99 
100 	fd = *(int *)key;
101 	cgroup = cgroup_v1v2_get_from_fd(fd);
102 	if (IS_ERR(cgroup))
103 		return PTR_ERR(cgroup);
104 
105 	err = cgroup_storage_delete(cgroup, map);
106 	cgroup_put(cgroup);
107 	return err;
108 }
109 
110 static int notsupp_get_next_key(struct bpf_map *map, void *key, void *next_key)
111 {
112 	return -ENOTSUPP;
113 }
114 
115 static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
116 {
117 	return bpf_local_storage_map_alloc(attr, &cgroup_cache);
118 }
119 
120 static void cgroup_storage_map_free(struct bpf_map *map)
121 {
122 	bpf_local_storage_map_free(map, &cgroup_cache);
123 }
124 
125 BPF_CALL_4(bpf_cgrp_storage_get, struct bpf_map *, map, struct cgroup *, cgroup,
126 	   void *, value, u64, flags)
127 {
128 	struct bpf_local_storage_data *sdata;
129 
130 	WARN_ON_ONCE(!bpf_rcu_lock_held());
131 	if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE))
132 		return (unsigned long)NULL;
133 
134 	if (!cgroup)
135 		return (unsigned long)NULL;
136 
137 	sdata = cgroup_storage_lookup(cgroup, map, true);
138 	if (sdata)
139 		goto out;
140 
141 	/* only allocate new storage, when the cgroup is refcounted */
142 	if (!percpu_ref_is_dying(&cgroup->self.refcnt) &&
143 	    (flags & BPF_LOCAL_STORAGE_GET_F_CREATE))
144 		sdata = bpf_local_storage_update(cgroup, (struct bpf_local_storage_map *)map,
145 						 value, BPF_NOEXIST, false);
146 
147 out:
148 	return IS_ERR_OR_NULL(sdata) ? (unsigned long)NULL : (unsigned long)sdata->data;
149 }
150 
151 BPF_CALL_2(bpf_cgrp_storage_delete, struct bpf_map *, map, struct cgroup *, cgroup)
152 {
153 	WARN_ON_ONCE(!bpf_rcu_lock_held());
154 	if (!cgroup)
155 		return -EINVAL;
156 
157 	return cgroup_storage_delete(cgroup, map);
158 }
159 
160 const struct bpf_map_ops cgrp_storage_map_ops = {
161 	.map_meta_equal = bpf_map_meta_equal,
162 	.map_alloc_check = bpf_local_storage_map_alloc_check,
163 	.map_alloc = cgroup_storage_map_alloc,
164 	.map_free = cgroup_storage_map_free,
165 	.map_get_next_key = notsupp_get_next_key,
166 	.map_lookup_elem = bpf_cgrp_storage_lookup_elem,
167 	.map_update_elem = bpf_cgrp_storage_update_elem,
168 	.map_delete_elem = bpf_cgrp_storage_delete_elem,
169 	.map_check_btf = bpf_local_storage_map_check_btf,
170 	.map_mem_usage = bpf_local_storage_map_mem_usage,
171 	.map_btf_id = &bpf_local_storage_map_btf_id[0],
172 	.map_owner_storage_ptr = cgroup_storage_ptr,
173 };
174 
175 const struct bpf_func_proto bpf_cgrp_storage_get_proto = {
176 	.func		= bpf_cgrp_storage_get,
177 	.gpl_only	= false,
178 	.ret_type	= RET_PTR_TO_MAP_VALUE_OR_NULL,
179 	.arg1_type	= ARG_CONST_MAP_PTR,
180 	.arg2_type	= ARG_PTR_TO_BTF_ID_OR_NULL,
181 	.arg2_btf_id	= &bpf_cgroup_btf_id[0],
182 	.arg3_type	= ARG_PTR_TO_MAP_VALUE_OR_NULL,
183 	.arg4_type	= ARG_ANYTHING,
184 };
185 
186 const struct bpf_func_proto bpf_cgrp_storage_delete_proto = {
187 	.func		= bpf_cgrp_storage_delete,
188 	.gpl_only	= false,
189 	.ret_type	= RET_INTEGER,
190 	.arg1_type	= ARG_CONST_MAP_PTR,
191 	.arg2_type	= ARG_PTR_TO_BTF_ID_OR_NULL,
192 	.arg2_btf_id	= &bpf_cgroup_btf_id[0],
193 };
194