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;
75 bool free_task_storage = false;
80 local_storage = rcu_dereference(task->bpf_storage);
86 bpf_task_storage_lock();
87 raw_spin_lock_irqsave(&local_storage->lock, flags);
88 free_task_storage = bpf_local_storage_unlink_nolock(local_storage);
89 raw_spin_unlock_irqrestore(&local_storage->lock, flags);
90 bpf_task_storage_unlock();
93 if (free_task_storage)
94 kfree_rcu(local_storage, rcu);
97 static void *bpf_pid_task_storage_lookup_elem(struct bpf_map *map, void *key)
99 struct bpf_local_storage_data *sdata;
100 struct task_struct *task;
101 unsigned int f_flags;
106 pid = pidfd_get_pid(fd, &f_flags);
108 return ERR_CAST(pid);
110 /* We should be in an RCU read side critical section, it should be safe
113 WARN_ON_ONCE(!rcu_read_lock_held());
114 task = pid_task(pid, PIDTYPE_PID);
120 bpf_task_storage_lock();
121 sdata = task_storage_lookup(task, map, true);
122 bpf_task_storage_unlock();
124 return sdata ? sdata->data : NULL;
130 static int bpf_pid_task_storage_update_elem(struct bpf_map *map, void *key,
131 void *value, u64 map_flags)
133 struct bpf_local_storage_data *sdata;
134 struct task_struct *task;
135 unsigned int f_flags;
140 pid = pidfd_get_pid(fd, &f_flags);
144 /* We should be in an RCU read side critical section, it should be safe
147 WARN_ON_ONCE(!rcu_read_lock_held());
148 task = pid_task(pid, PIDTYPE_PID);
154 bpf_task_storage_lock();
155 sdata = bpf_local_storage_update(
156 task, (struct bpf_local_storage_map *)map, value, map_flags,
158 bpf_task_storage_unlock();
160 err = PTR_ERR_OR_ZERO(sdata);
166 static int task_storage_delete(struct task_struct *task, struct bpf_map *map,
169 struct bpf_local_storage_data *sdata;
171 sdata = task_storage_lookup(task, map, false);
178 bpf_selem_unlink(SELEM(sdata), true);
183 static int bpf_pid_task_storage_delete_elem(struct bpf_map *map, void *key)
185 struct task_struct *task;
186 unsigned int f_flags;
191 pid = pidfd_get_pid(fd, &f_flags);
195 /* We should be in an RCU read side critical section, it should be safe
198 WARN_ON_ONCE(!rcu_read_lock_held());
199 task = pid_task(pid, PIDTYPE_PID);
205 bpf_task_storage_lock();
206 err = task_storage_delete(task, map, true);
207 bpf_task_storage_unlock();
213 /* Called by bpf_task_storage_get*() helpers */
214 static void *__bpf_task_storage_get(struct bpf_map *map,
215 struct task_struct *task, void *value,
216 u64 flags, gfp_t gfp_flags, bool nobusy)
218 struct bpf_local_storage_data *sdata;
220 sdata = task_storage_lookup(task, map, nobusy);
224 /* only allocate new storage, when the task is refcounted */
225 if (refcount_read(&task->usage) &&
226 (flags & BPF_LOCAL_STORAGE_GET_F_CREATE) && nobusy) {
227 sdata = bpf_local_storage_update(
228 task, (struct bpf_local_storage_map *)map, value,
229 BPF_NOEXIST, gfp_flags);
230 return IS_ERR(sdata) ? NULL : sdata->data;
236 /* *gfp_flags* is a hidden argument provided by the verifier */
237 BPF_CALL_5(bpf_task_storage_get_recur, struct bpf_map *, map, struct task_struct *,
238 task, void *, value, u64, flags, gfp_t, gfp_flags)
243 WARN_ON_ONCE(!bpf_rcu_lock_held());
244 if (flags & ~BPF_LOCAL_STORAGE_GET_F_CREATE || !task)
245 return (unsigned long)NULL;
247 nobusy = bpf_task_storage_trylock();
248 data = __bpf_task_storage_get(map, task, value, flags,
251 bpf_task_storage_unlock();
252 return (unsigned long)data;
255 /* *gfp_flags* is a hidden argument provided by the verifier */
256 BPF_CALL_5(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *,
257 task, void *, value, u64, flags, gfp_t, gfp_flags)
261 WARN_ON_ONCE(!bpf_rcu_lock_held());
262 if (flags & ~BPF_LOCAL_STORAGE_GET_F_CREATE || !task)
263 return (unsigned long)NULL;
265 bpf_task_storage_lock();
266 data = __bpf_task_storage_get(map, task, value, flags,
268 bpf_task_storage_unlock();
269 return (unsigned long)data;
272 BPF_CALL_2(bpf_task_storage_delete_recur, struct bpf_map *, map, struct task_struct *,
278 WARN_ON_ONCE(!bpf_rcu_lock_held());
282 nobusy = bpf_task_storage_trylock();
283 /* This helper must only be called from places where the lifetime of the task
284 * is guaranteed. Either by being refcounted or by being protected
285 * by an RCU read-side critical section.
287 ret = task_storage_delete(task, map, nobusy);
289 bpf_task_storage_unlock();
293 BPF_CALL_2(bpf_task_storage_delete, struct bpf_map *, map, struct task_struct *,
298 WARN_ON_ONCE(!bpf_rcu_lock_held());
302 bpf_task_storage_lock();
303 /* This helper must only be called from places where the lifetime of the task
304 * is guaranteed. Either by being refcounted or by being protected
305 * by an RCU read-side critical section.
307 ret = task_storage_delete(task, map, true);
308 bpf_task_storage_unlock();
312 static int notsupp_get_next_key(struct bpf_map *map, void *key, void *next_key)
317 static struct bpf_map *task_storage_map_alloc(union bpf_attr *attr)
319 return bpf_local_storage_map_alloc(attr, &task_cache);
322 static void task_storage_map_free(struct bpf_map *map)
324 bpf_local_storage_map_free(map, &task_cache, &bpf_task_storage_busy);
327 BTF_ID_LIST_GLOBAL_SINGLE(bpf_local_storage_map_btf_id, struct, bpf_local_storage_map)
328 const struct bpf_map_ops task_storage_map_ops = {
329 .map_meta_equal = bpf_map_meta_equal,
330 .map_alloc_check = bpf_local_storage_map_alloc_check,
331 .map_alloc = task_storage_map_alloc,
332 .map_free = task_storage_map_free,
333 .map_get_next_key = notsupp_get_next_key,
334 .map_lookup_elem = bpf_pid_task_storage_lookup_elem,
335 .map_update_elem = bpf_pid_task_storage_update_elem,
336 .map_delete_elem = bpf_pid_task_storage_delete_elem,
337 .map_check_btf = bpf_local_storage_map_check_btf,
338 .map_btf_id = &bpf_local_storage_map_btf_id[0],
339 .map_owner_storage_ptr = task_storage_ptr,
342 const struct bpf_func_proto bpf_task_storage_get_recur_proto = {
343 .func = bpf_task_storage_get_recur,
345 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
346 .arg1_type = ARG_CONST_MAP_PTR,
347 .arg2_type = ARG_PTR_TO_BTF_ID,
348 .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
349 .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
350 .arg4_type = ARG_ANYTHING,
353 const struct bpf_func_proto bpf_task_storage_get_proto = {
354 .func = bpf_task_storage_get,
356 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
357 .arg1_type = ARG_CONST_MAP_PTR,
358 .arg2_type = ARG_PTR_TO_BTF_ID,
359 .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
360 .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
361 .arg4_type = ARG_ANYTHING,
364 const struct bpf_func_proto bpf_task_storage_delete_recur_proto = {
365 .func = bpf_task_storage_delete_recur,
367 .ret_type = RET_INTEGER,
368 .arg1_type = ARG_CONST_MAP_PTR,
369 .arg2_type = ARG_PTR_TO_BTF_ID,
370 .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
373 const struct bpf_func_proto bpf_task_storage_delete_proto = {
374 .func = bpf_task_storage_delete,
376 .ret_type = RET_INTEGER,
377 .arg1_type = ARG_CONST_MAP_PTR,
378 .arg2_type = ARG_PTR_TO_BTF_ID,
379 .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],