1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2020 Facebook
4 * Copyright 2020 Google LLC.
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>
22 DEFINE_BPF_STORAGE_CACHE(task_cache);
24 static DEFINE_PER_CPU(int, bpf_task_storage_busy);
26 static void bpf_task_storage_lock(void)
29 this_cpu_inc(bpf_task_storage_busy);
32 static void bpf_task_storage_unlock(void)
34 this_cpu_dec(bpf_task_storage_busy);
38 static bool bpf_task_storage_trylock(void)
41 if (unlikely(this_cpu_inc_return(bpf_task_storage_busy) != 1)) {
42 this_cpu_dec(bpf_task_storage_busy);
49 static struct bpf_local_storage __rcu **task_storage_ptr(void *owner)
51 struct task_struct *task = owner;
53 return &task->bpf_storage;
56 static struct bpf_local_storage_data *
57 task_storage_lookup(struct task_struct *task, struct bpf_map *map,
60 struct bpf_local_storage *task_storage;
61 struct bpf_local_storage_map *smap;
64 rcu_dereference_check(task->bpf_storage, bpf_rcu_lock_held());
68 smap = (struct bpf_local_storage_map *)map;
69 return bpf_local_storage_lookup(task_storage, smap, cacheit_lockit);
72 void bpf_task_storage_free(struct task_struct *task)
74 struct bpf_local_storage *local_storage;
78 local_storage = rcu_dereference(task->bpf_storage);
84 bpf_task_storage_lock();
85 bpf_local_storage_destroy(local_storage);
86 bpf_task_storage_unlock();
90 static void *bpf_pid_task_storage_lookup_elem(struct bpf_map *map, void *key)
92 struct bpf_local_storage_data *sdata;
93 struct task_struct *task;
99 pid = pidfd_get_pid(fd, &f_flags);
101 return ERR_CAST(pid);
103 /* We should be in an RCU read side critical section, it should be safe
106 WARN_ON_ONCE(!rcu_read_lock_held());
107 task = pid_task(pid, PIDTYPE_PID);
113 bpf_task_storage_lock();
114 sdata = task_storage_lookup(task, map, true);
115 bpf_task_storage_unlock();
117 return sdata ? sdata->data : NULL;
123 static long bpf_pid_task_storage_update_elem(struct bpf_map *map, void *key,
124 void *value, u64 map_flags)
126 struct bpf_local_storage_data *sdata;
127 struct task_struct *task;
128 unsigned int f_flags;
133 pid = pidfd_get_pid(fd, &f_flags);
137 /* We should be in an RCU read side critical section, it should be safe
140 WARN_ON_ONCE(!rcu_read_lock_held());
141 task = pid_task(pid, PIDTYPE_PID);
147 bpf_task_storage_lock();
148 sdata = bpf_local_storage_update(
149 task, (struct bpf_local_storage_map *)map, value, map_flags,
151 bpf_task_storage_unlock();
153 err = PTR_ERR_OR_ZERO(sdata);
159 static int task_storage_delete(struct task_struct *task, struct bpf_map *map,
162 struct bpf_local_storage_data *sdata;
164 sdata = task_storage_lookup(task, map, false);
171 bpf_selem_unlink(SELEM(sdata), false);
176 static long bpf_pid_task_storage_delete_elem(struct bpf_map *map, void *key)
178 struct task_struct *task;
179 unsigned int f_flags;
184 pid = pidfd_get_pid(fd, &f_flags);
188 /* We should be in an RCU read side critical section, it should be safe
191 WARN_ON_ONCE(!rcu_read_lock_held());
192 task = pid_task(pid, PIDTYPE_PID);
198 bpf_task_storage_lock();
199 err = task_storage_delete(task, map, true);
200 bpf_task_storage_unlock();
206 /* Called by bpf_task_storage_get*() helpers */
207 static void *__bpf_task_storage_get(struct bpf_map *map,
208 struct task_struct *task, void *value,
209 u64 flags, gfp_t gfp_flags, bool nobusy)
211 struct bpf_local_storage_data *sdata;
213 sdata = task_storage_lookup(task, map, nobusy);
217 /* only allocate new storage, when the task is refcounted */
218 if (refcount_read(&task->usage) &&
219 (flags & BPF_LOCAL_STORAGE_GET_F_CREATE) && nobusy) {
220 sdata = bpf_local_storage_update(
221 task, (struct bpf_local_storage_map *)map, value,
222 BPF_NOEXIST, gfp_flags);
223 return IS_ERR(sdata) ? NULL : sdata->data;
229 /* *gfp_flags* is a hidden argument provided by the verifier */
230 BPF_CALL_5(bpf_task_storage_get_recur, struct bpf_map *, map, struct task_struct *,
231 task, void *, value, u64, flags, gfp_t, gfp_flags)
236 WARN_ON_ONCE(!bpf_rcu_lock_held());
237 if (flags & ~BPF_LOCAL_STORAGE_GET_F_CREATE || !task)
238 return (unsigned long)NULL;
240 nobusy = bpf_task_storage_trylock();
241 data = __bpf_task_storage_get(map, task, value, flags,
244 bpf_task_storage_unlock();
245 return (unsigned long)data;
248 /* *gfp_flags* is a hidden argument provided by the verifier */
249 BPF_CALL_5(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *,
250 task, void *, value, u64, flags, gfp_t, gfp_flags)
254 WARN_ON_ONCE(!bpf_rcu_lock_held());
255 if (flags & ~BPF_LOCAL_STORAGE_GET_F_CREATE || !task)
256 return (unsigned long)NULL;
258 bpf_task_storage_lock();
259 data = __bpf_task_storage_get(map, task, value, flags,
261 bpf_task_storage_unlock();
262 return (unsigned long)data;
265 BPF_CALL_2(bpf_task_storage_delete_recur, struct bpf_map *, map, struct task_struct *,
271 WARN_ON_ONCE(!bpf_rcu_lock_held());
275 nobusy = bpf_task_storage_trylock();
276 /* This helper must only be called from places where the lifetime of the task
277 * is guaranteed. Either by being refcounted or by being protected
278 * by an RCU read-side critical section.
280 ret = task_storage_delete(task, map, nobusy);
282 bpf_task_storage_unlock();
286 BPF_CALL_2(bpf_task_storage_delete, struct bpf_map *, map, struct task_struct *,
291 WARN_ON_ONCE(!bpf_rcu_lock_held());
295 bpf_task_storage_lock();
296 /* This helper must only be called from places where the lifetime of the task
297 * is guaranteed. Either by being refcounted or by being protected
298 * by an RCU read-side critical section.
300 ret = task_storage_delete(task, map, true);
301 bpf_task_storage_unlock();
305 static int notsupp_get_next_key(struct bpf_map *map, void *key, void *next_key)
310 static struct bpf_map *task_storage_map_alloc(union bpf_attr *attr)
312 return bpf_local_storage_map_alloc(attr, &task_cache, true);
315 static void task_storage_map_free(struct bpf_map *map)
317 bpf_local_storage_map_free(map, &task_cache, &bpf_task_storage_busy);
320 BTF_ID_LIST_GLOBAL_SINGLE(bpf_local_storage_map_btf_id, struct, bpf_local_storage_map)
321 const struct bpf_map_ops task_storage_map_ops = {
322 .map_meta_equal = bpf_map_meta_equal,
323 .map_alloc_check = bpf_local_storage_map_alloc_check,
324 .map_alloc = task_storage_map_alloc,
325 .map_free = task_storage_map_free,
326 .map_get_next_key = notsupp_get_next_key,
327 .map_lookup_elem = bpf_pid_task_storage_lookup_elem,
328 .map_update_elem = bpf_pid_task_storage_update_elem,
329 .map_delete_elem = bpf_pid_task_storage_delete_elem,
330 .map_check_btf = bpf_local_storage_map_check_btf,
331 .map_mem_usage = bpf_local_storage_map_mem_usage,
332 .map_btf_id = &bpf_local_storage_map_btf_id[0],
333 .map_owner_storage_ptr = task_storage_ptr,
336 const struct bpf_func_proto bpf_task_storage_get_recur_proto = {
337 .func = bpf_task_storage_get_recur,
339 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
340 .arg1_type = ARG_CONST_MAP_PTR,
341 .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
342 .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
343 .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
344 .arg4_type = ARG_ANYTHING,
347 const struct bpf_func_proto bpf_task_storage_get_proto = {
348 .func = bpf_task_storage_get,
350 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
351 .arg1_type = ARG_CONST_MAP_PTR,
352 .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
353 .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
354 .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
355 .arg4_type = ARG_ANYTHING,
358 const struct bpf_func_proto bpf_task_storage_delete_recur_proto = {
359 .func = bpf_task_storage_delete_recur,
361 .ret_type = RET_INTEGER,
362 .arg1_type = ARG_CONST_MAP_PTR,
363 .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
364 .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
367 const struct bpf_func_proto bpf_task_storage_delete_proto = {
368 .func = bpf_task_storage_delete,
370 .ret_type = RET_INTEGER,
371 .arg1_type = ARG_CONST_MAP_PTR,
372 .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
373 .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],