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/rcupdate_trace.h>
21 DEFINE_BPF_STORAGE_CACHE(task_cache);
23 static DEFINE_PER_CPU(int, bpf_task_storage_busy);
25 static void bpf_task_storage_lock(void)
28 this_cpu_inc(bpf_task_storage_busy);
31 static void bpf_task_storage_unlock(void)
33 this_cpu_dec(bpf_task_storage_busy);
37 static bool bpf_task_storage_trylock(void)
40 if (unlikely(this_cpu_inc_return(bpf_task_storage_busy) != 1)) {
41 this_cpu_dec(bpf_task_storage_busy);
48 static struct bpf_local_storage __rcu **task_storage_ptr(void *owner)
50 struct task_struct *task = owner;
52 return &task->bpf_storage;
55 static struct bpf_local_storage_data *
56 task_storage_lookup(struct task_struct *task, struct bpf_map *map,
59 struct bpf_local_storage *task_storage;
60 struct bpf_local_storage_map *smap;
63 rcu_dereference_check(task->bpf_storage, bpf_rcu_lock_held());
67 smap = (struct bpf_local_storage_map *)map;
68 return bpf_local_storage_lookup(task_storage, smap, cacheit_lockit);
71 void bpf_task_storage_free(struct task_struct *task)
73 struct bpf_local_storage *local_storage;
77 local_storage = rcu_dereference(task->bpf_storage);
83 bpf_task_storage_lock();
84 bpf_local_storage_destroy(local_storage);
85 bpf_task_storage_unlock();
89 static void *bpf_pid_task_storage_lookup_elem(struct bpf_map *map, void *key)
91 struct bpf_local_storage_data *sdata;
92 struct task_struct *task;
98 pid = pidfd_get_pid(fd, &f_flags);
100 return ERR_CAST(pid);
102 /* We should be in an RCU read side critical section, it should be safe
105 WARN_ON_ONCE(!rcu_read_lock_held());
106 task = pid_task(pid, PIDTYPE_PID);
112 bpf_task_storage_lock();
113 sdata = task_storage_lookup(task, map, true);
114 bpf_task_storage_unlock();
116 return sdata ? sdata->data : NULL;
122 static long bpf_pid_task_storage_update_elem(struct bpf_map *map, void *key,
123 void *value, u64 map_flags)
125 struct bpf_local_storage_data *sdata;
126 struct task_struct *task;
127 unsigned int f_flags;
131 if ((map_flags & BPF_F_LOCK) && btf_record_has_field(map->record, BPF_UPTR))
135 pid = pidfd_get_pid(fd, &f_flags);
139 /* We should be in an RCU read side critical section, it should be safe
142 WARN_ON_ONCE(!rcu_read_lock_held());
143 task = pid_task(pid, PIDTYPE_PID);
149 bpf_task_storage_lock();
150 sdata = bpf_local_storage_update(
151 task, (struct bpf_local_storage_map *)map, value, map_flags,
153 bpf_task_storage_unlock();
155 err = PTR_ERR_OR_ZERO(sdata);
161 static int task_storage_delete(struct task_struct *task, struct bpf_map *map,
164 struct bpf_local_storage_data *sdata;
166 sdata = task_storage_lookup(task, map, false);
173 bpf_selem_unlink(SELEM(sdata), false);
178 static long bpf_pid_task_storage_delete_elem(struct bpf_map *map, void *key)
180 struct task_struct *task;
181 unsigned int f_flags;
186 pid = pidfd_get_pid(fd, &f_flags);
190 /* We should be in an RCU read side critical section, it should be safe
193 WARN_ON_ONCE(!rcu_read_lock_held());
194 task = pid_task(pid, PIDTYPE_PID);
200 bpf_task_storage_lock();
201 err = task_storage_delete(task, map, true);
202 bpf_task_storage_unlock();
208 /* Called by bpf_task_storage_get*() helpers */
209 static void *__bpf_task_storage_get(struct bpf_map *map,
210 struct task_struct *task, void *value,
211 u64 flags, gfp_t gfp_flags, bool nobusy)
213 struct bpf_local_storage_data *sdata;
215 sdata = task_storage_lookup(task, map, nobusy);
219 /* only allocate new storage, when the task is refcounted */
220 if (refcount_read(&task->usage) &&
221 (flags & BPF_LOCAL_STORAGE_GET_F_CREATE) && nobusy) {
222 sdata = bpf_local_storage_update(
223 task, (struct bpf_local_storage_map *)map, value,
224 BPF_NOEXIST, false, gfp_flags);
225 return IS_ERR(sdata) ? NULL : sdata->data;
231 /* *gfp_flags* is a hidden argument provided by the verifier */
232 BPF_CALL_5(bpf_task_storage_get_recur, struct bpf_map *, map, struct task_struct *,
233 task, void *, value, u64, flags, gfp_t, gfp_flags)
238 WARN_ON_ONCE(!bpf_rcu_lock_held());
239 if (flags & ~BPF_LOCAL_STORAGE_GET_F_CREATE || !task)
240 return (unsigned long)NULL;
242 nobusy = bpf_task_storage_trylock();
243 data = __bpf_task_storage_get(map, task, value, flags,
246 bpf_task_storage_unlock();
247 return (unsigned long)data;
250 /* *gfp_flags* is a hidden argument provided by the verifier */
251 BPF_CALL_5(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *,
252 task, void *, value, u64, flags, gfp_t, gfp_flags)
256 WARN_ON_ONCE(!bpf_rcu_lock_held());
257 if (flags & ~BPF_LOCAL_STORAGE_GET_F_CREATE || !task)
258 return (unsigned long)NULL;
260 bpf_task_storage_lock();
261 data = __bpf_task_storage_get(map, task, value, flags,
263 bpf_task_storage_unlock();
264 return (unsigned long)data;
267 BPF_CALL_2(bpf_task_storage_delete_recur, struct bpf_map *, map, struct task_struct *,
273 WARN_ON_ONCE(!bpf_rcu_lock_held());
277 nobusy = bpf_task_storage_trylock();
278 /* This helper must only be called from places where the lifetime of the task
279 * is guaranteed. Either by being refcounted or by being protected
280 * by an RCU read-side critical section.
282 ret = task_storage_delete(task, map, nobusy);
284 bpf_task_storage_unlock();
288 BPF_CALL_2(bpf_task_storage_delete, struct bpf_map *, map, struct task_struct *,
293 WARN_ON_ONCE(!bpf_rcu_lock_held());
297 bpf_task_storage_lock();
298 /* This helper must only be called from places where the lifetime of the task
299 * is guaranteed. Either by being refcounted or by being protected
300 * by an RCU read-side critical section.
302 ret = task_storage_delete(task, map, true);
303 bpf_task_storage_unlock();
307 static int notsupp_get_next_key(struct bpf_map *map, void *key, void *next_key)
312 static struct bpf_map *task_storage_map_alloc(union bpf_attr *attr)
314 return bpf_local_storage_map_alloc(attr, &task_cache, true);
317 static void task_storage_map_free(struct bpf_map *map)
319 bpf_local_storage_map_free(map, &task_cache, &bpf_task_storage_busy);
322 BTF_ID_LIST_GLOBAL_SINGLE(bpf_local_storage_map_btf_id, struct, bpf_local_storage_map)
323 const struct bpf_map_ops task_storage_map_ops = {
324 .map_meta_equal = bpf_map_meta_equal,
325 .map_alloc_check = bpf_local_storage_map_alloc_check,
326 .map_alloc = task_storage_map_alloc,
327 .map_free = task_storage_map_free,
328 .map_get_next_key = notsupp_get_next_key,
329 .map_lookup_elem = bpf_pid_task_storage_lookup_elem,
330 .map_update_elem = bpf_pid_task_storage_update_elem,
331 .map_delete_elem = bpf_pid_task_storage_delete_elem,
332 .map_check_btf = bpf_local_storage_map_check_btf,
333 .map_mem_usage = bpf_local_storage_map_mem_usage,
334 .map_btf_id = &bpf_local_storage_map_btf_id[0],
335 .map_owner_storage_ptr = task_storage_ptr,
338 const struct bpf_func_proto bpf_task_storage_get_recur_proto = {
339 .func = bpf_task_storage_get_recur,
341 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
342 .arg1_type = ARG_CONST_MAP_PTR,
343 .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
344 .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
345 .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
346 .arg4_type = ARG_ANYTHING,
349 const struct bpf_func_proto bpf_task_storage_get_proto = {
350 .func = bpf_task_storage_get,
352 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
353 .arg1_type = ARG_CONST_MAP_PTR,
354 .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
355 .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
356 .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
357 .arg4_type = ARG_ANYTHING,
360 const struct bpf_func_proto bpf_task_storage_delete_recur_proto = {
361 .func = bpf_task_storage_delete_recur,
363 .ret_type = RET_INTEGER,
364 .arg1_type = ARG_CONST_MAP_PTR,
365 .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
366 .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
369 const struct bpf_func_proto bpf_task_storage_delete_proto = {
370 .func = bpf_task_storage_delete,
372 .ret_type = RET_INTEGER,
373 .arg1_type = ARG_CONST_MAP_PTR,
374 .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
375 .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],