xref: /linux-6.15/kernel/bpf/bpf_task_storage.c (revision 8a0cfd8a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2020 Facebook
4  * Copyright 2020 Google LLC.
5  */
6 
7 #include <linux/pid.h>
8 #include <linux/sched.h>
9 #include <linux/rculist.h>
10 #include <linux/list.h>
11 #include <linux/hash.h>
12 #include <linux/types.h>
13 #include <linux/spinlock.h>
14 #include <linux/bpf.h>
15 #include <linux/bpf_local_storage.h>
16 #include <linux/filter.h>
17 #include <uapi/linux/btf.h>
18 #include <linux/btf_ids.h>
19 #include <linux/fdtable.h>
20 #include <linux/rcupdate_trace.h>
21 
22 DEFINE_BPF_STORAGE_CACHE(task_cache);
23 
24 static DEFINE_PER_CPU(int, bpf_task_storage_busy);
25 
26 static void bpf_task_storage_lock(void)
27 {
28 	migrate_disable();
29 	this_cpu_inc(bpf_task_storage_busy);
30 }
31 
32 static void bpf_task_storage_unlock(void)
33 {
34 	this_cpu_dec(bpf_task_storage_busy);
35 	migrate_enable();
36 }
37 
38 static bool bpf_task_storage_trylock(void)
39 {
40 	migrate_disable();
41 	if (unlikely(this_cpu_inc_return(bpf_task_storage_busy) != 1)) {
42 		this_cpu_dec(bpf_task_storage_busy);
43 		migrate_enable();
44 		return false;
45 	}
46 	return true;
47 }
48 
49 static struct bpf_local_storage __rcu **task_storage_ptr(void *owner)
50 {
51 	struct task_struct *task = owner;
52 
53 	return &task->bpf_storage;
54 }
55 
56 static struct bpf_local_storage_data *
57 task_storage_lookup(struct task_struct *task, struct bpf_map *map,
58 		    bool cacheit_lockit)
59 {
60 	struct bpf_local_storage *task_storage;
61 	struct bpf_local_storage_map *smap;
62 
63 	task_storage =
64 		rcu_dereference_check(task->bpf_storage, bpf_rcu_lock_held());
65 	if (!task_storage)
66 		return NULL;
67 
68 	smap = (struct bpf_local_storage_map *)map;
69 	return bpf_local_storage_lookup(task_storage, smap, cacheit_lockit);
70 }
71 
72 void bpf_task_storage_free(struct task_struct *task)
73 {
74 	struct bpf_local_storage *local_storage;
75 
76 	rcu_read_lock();
77 
78 	local_storage = rcu_dereference(task->bpf_storage);
79 	if (!local_storage) {
80 		rcu_read_unlock();
81 		return;
82 	}
83 
84 	bpf_task_storage_lock();
85 	bpf_local_storage_destroy(local_storage);
86 	bpf_task_storage_unlock();
87 	rcu_read_unlock();
88 }
89 
90 static void *bpf_pid_task_storage_lookup_elem(struct bpf_map *map, void *key)
91 {
92 	struct bpf_local_storage_data *sdata;
93 	struct task_struct *task;
94 	unsigned int f_flags;
95 	struct pid *pid;
96 	int fd, err;
97 
98 	fd = *(int *)key;
99 	pid = pidfd_get_pid(fd, &f_flags);
100 	if (IS_ERR(pid))
101 		return ERR_CAST(pid);
102 
103 	/* We should be in an RCU read side critical section, it should be safe
104 	 * to call pid_task.
105 	 */
106 	WARN_ON_ONCE(!rcu_read_lock_held());
107 	task = pid_task(pid, PIDTYPE_PID);
108 	if (!task) {
109 		err = -ENOENT;
110 		goto out;
111 	}
112 
113 	bpf_task_storage_lock();
114 	sdata = task_storage_lookup(task, map, true);
115 	bpf_task_storage_unlock();
116 	put_pid(pid);
117 	return sdata ? sdata->data : NULL;
118 out:
119 	put_pid(pid);
120 	return ERR_PTR(err);
121 }
122 
123 static long bpf_pid_task_storage_update_elem(struct bpf_map *map, void *key,
124 					     void *value, u64 map_flags)
125 {
126 	struct bpf_local_storage_data *sdata;
127 	struct task_struct *task;
128 	unsigned int f_flags;
129 	struct pid *pid;
130 	int fd, err;
131 
132 	if ((map_flags & BPF_F_LOCK) && btf_record_has_field(map->record, BPF_UPTR))
133 		return -EOPNOTSUPP;
134 
135 	fd = *(int *)key;
136 	pid = pidfd_get_pid(fd, &f_flags);
137 	if (IS_ERR(pid))
138 		return PTR_ERR(pid);
139 
140 	/* We should be in an RCU read side critical section, it should be safe
141 	 * to call pid_task.
142 	 */
143 	WARN_ON_ONCE(!rcu_read_lock_held());
144 	task = pid_task(pid, PIDTYPE_PID);
145 	if (!task) {
146 		err = -ENOENT;
147 		goto out;
148 	}
149 
150 	bpf_task_storage_lock();
151 	sdata = bpf_local_storage_update(
152 		task, (struct bpf_local_storage_map *)map, value, map_flags,
153 		true, GFP_ATOMIC);
154 	bpf_task_storage_unlock();
155 
156 	err = PTR_ERR_OR_ZERO(sdata);
157 out:
158 	put_pid(pid);
159 	return err;
160 }
161 
162 static int task_storage_delete(struct task_struct *task, struct bpf_map *map,
163 			       bool nobusy)
164 {
165 	struct bpf_local_storage_data *sdata;
166 
167 	sdata = task_storage_lookup(task, map, false);
168 	if (!sdata)
169 		return -ENOENT;
170 
171 	if (!nobusy)
172 		return -EBUSY;
173 
174 	bpf_selem_unlink(SELEM(sdata), false);
175 
176 	return 0;
177 }
178 
179 static long bpf_pid_task_storage_delete_elem(struct bpf_map *map, void *key)
180 {
181 	struct task_struct *task;
182 	unsigned int f_flags;
183 	struct pid *pid;
184 	int fd, err;
185 
186 	fd = *(int *)key;
187 	pid = pidfd_get_pid(fd, &f_flags);
188 	if (IS_ERR(pid))
189 		return PTR_ERR(pid);
190 
191 	/* We should be in an RCU read side critical section, it should be safe
192 	 * to call pid_task.
193 	 */
194 	WARN_ON_ONCE(!rcu_read_lock_held());
195 	task = pid_task(pid, PIDTYPE_PID);
196 	if (!task) {
197 		err = -ENOENT;
198 		goto out;
199 	}
200 
201 	bpf_task_storage_lock();
202 	err = task_storage_delete(task, map, true);
203 	bpf_task_storage_unlock();
204 out:
205 	put_pid(pid);
206 	return err;
207 }
208 
209 /* Called by bpf_task_storage_get*() helpers */
210 static void *__bpf_task_storage_get(struct bpf_map *map,
211 				    struct task_struct *task, void *value,
212 				    u64 flags, gfp_t gfp_flags, bool nobusy)
213 {
214 	struct bpf_local_storage_data *sdata;
215 
216 	sdata = task_storage_lookup(task, map, nobusy);
217 	if (sdata)
218 		return sdata->data;
219 
220 	/* only allocate new storage, when the task is refcounted */
221 	if (refcount_read(&task->usage) &&
222 	    (flags & BPF_LOCAL_STORAGE_GET_F_CREATE) && nobusy) {
223 		sdata = bpf_local_storage_update(
224 			task, (struct bpf_local_storage_map *)map, value,
225 			BPF_NOEXIST, false, gfp_flags);
226 		return IS_ERR(sdata) ? NULL : sdata->data;
227 	}
228 
229 	return NULL;
230 }
231 
232 /* *gfp_flags* is a hidden argument provided by the verifier */
233 BPF_CALL_5(bpf_task_storage_get_recur, struct bpf_map *, map, struct task_struct *,
234 	   task, void *, value, u64, flags, gfp_t, gfp_flags)
235 {
236 	bool nobusy;
237 	void *data;
238 
239 	WARN_ON_ONCE(!bpf_rcu_lock_held());
240 	if (flags & ~BPF_LOCAL_STORAGE_GET_F_CREATE || !task)
241 		return (unsigned long)NULL;
242 
243 	nobusy = bpf_task_storage_trylock();
244 	data = __bpf_task_storage_get(map, task, value, flags,
245 				      gfp_flags, nobusy);
246 	if (nobusy)
247 		bpf_task_storage_unlock();
248 	return (unsigned long)data;
249 }
250 
251 /* *gfp_flags* is a hidden argument provided by the verifier */
252 BPF_CALL_5(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *,
253 	   task, void *, value, u64, flags, gfp_t, gfp_flags)
254 {
255 	void *data;
256 
257 	WARN_ON_ONCE(!bpf_rcu_lock_held());
258 	if (flags & ~BPF_LOCAL_STORAGE_GET_F_CREATE || !task)
259 		return (unsigned long)NULL;
260 
261 	bpf_task_storage_lock();
262 	data = __bpf_task_storage_get(map, task, value, flags,
263 				      gfp_flags, true);
264 	bpf_task_storage_unlock();
265 	return (unsigned long)data;
266 }
267 
268 BPF_CALL_2(bpf_task_storage_delete_recur, struct bpf_map *, map, struct task_struct *,
269 	   task)
270 {
271 	bool nobusy;
272 	int ret;
273 
274 	WARN_ON_ONCE(!bpf_rcu_lock_held());
275 	if (!task)
276 		return -EINVAL;
277 
278 	nobusy = bpf_task_storage_trylock();
279 	/* This helper must only be called from places where the lifetime of the task
280 	 * is guaranteed. Either by being refcounted or by being protected
281 	 * by an RCU read-side critical section.
282 	 */
283 	ret = task_storage_delete(task, map, nobusy);
284 	if (nobusy)
285 		bpf_task_storage_unlock();
286 	return ret;
287 }
288 
289 BPF_CALL_2(bpf_task_storage_delete, struct bpf_map *, map, struct task_struct *,
290 	   task)
291 {
292 	int ret;
293 
294 	WARN_ON_ONCE(!bpf_rcu_lock_held());
295 	if (!task)
296 		return -EINVAL;
297 
298 	bpf_task_storage_lock();
299 	/* This helper must only be called from places where the lifetime of the task
300 	 * is guaranteed. Either by being refcounted or by being protected
301 	 * by an RCU read-side critical section.
302 	 */
303 	ret = task_storage_delete(task, map, true);
304 	bpf_task_storage_unlock();
305 	return ret;
306 }
307 
308 static int notsupp_get_next_key(struct bpf_map *map, void *key, void *next_key)
309 {
310 	return -ENOTSUPP;
311 }
312 
313 static struct bpf_map *task_storage_map_alloc(union bpf_attr *attr)
314 {
315 	return bpf_local_storage_map_alloc(attr, &task_cache, true);
316 }
317 
318 static void task_storage_map_free(struct bpf_map *map)
319 {
320 	bpf_local_storage_map_free(map, &task_cache, &bpf_task_storage_busy);
321 }
322 
323 BTF_ID_LIST_GLOBAL_SINGLE(bpf_local_storage_map_btf_id, struct, bpf_local_storage_map)
324 const struct bpf_map_ops task_storage_map_ops = {
325 	.map_meta_equal = bpf_map_meta_equal,
326 	.map_alloc_check = bpf_local_storage_map_alloc_check,
327 	.map_alloc = task_storage_map_alloc,
328 	.map_free = task_storage_map_free,
329 	.map_get_next_key = notsupp_get_next_key,
330 	.map_lookup_elem = bpf_pid_task_storage_lookup_elem,
331 	.map_update_elem = bpf_pid_task_storage_update_elem,
332 	.map_delete_elem = bpf_pid_task_storage_delete_elem,
333 	.map_check_btf = bpf_local_storage_map_check_btf,
334 	.map_mem_usage = bpf_local_storage_map_mem_usage,
335 	.map_btf_id = &bpf_local_storage_map_btf_id[0],
336 	.map_owner_storage_ptr = task_storage_ptr,
337 };
338 
339 const struct bpf_func_proto bpf_task_storage_get_recur_proto = {
340 	.func = bpf_task_storage_get_recur,
341 	.gpl_only = false,
342 	.ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
343 	.arg1_type = ARG_CONST_MAP_PTR,
344 	.arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
345 	.arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
346 	.arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
347 	.arg4_type = ARG_ANYTHING,
348 };
349 
350 const struct bpf_func_proto bpf_task_storage_get_proto = {
351 	.func = bpf_task_storage_get,
352 	.gpl_only = false,
353 	.ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
354 	.arg1_type = ARG_CONST_MAP_PTR,
355 	.arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
356 	.arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
357 	.arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
358 	.arg4_type = ARG_ANYTHING,
359 };
360 
361 const struct bpf_func_proto bpf_task_storage_delete_recur_proto = {
362 	.func = bpf_task_storage_delete_recur,
363 	.gpl_only = false,
364 	.ret_type = RET_INTEGER,
365 	.arg1_type = ARG_CONST_MAP_PTR,
366 	.arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
367 	.arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
368 };
369 
370 const struct bpf_func_proto bpf_task_storage_delete_proto = {
371 	.func = bpf_task_storage_delete,
372 	.gpl_only = false,
373 	.ret_type = RET_INTEGER,
374 	.arg1_type = ARG_CONST_MAP_PTR,
375 	.arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
376 	.arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
377 };
378