1158115Sume// SPDX-License-Identifier: GPL-2.0 2158115Sume/* 3158115Sume * Copyright (c) 2020 Facebook 4158115Sume * Copyright 2020 Google LLC. 5158115Sume */ 6158115Sume 7158115Sume#include <linux/pid.h> 8158115Sume#include <linux/sched.h> 9158115Sume#include <linux/rculist.h> 10158115Sume#include <linux/list.h> 11158115Sume#include <linux/hash.h> 12158115Sume#include <linux/types.h> 13158115Sume#include <linux/spinlock.h> 14158115Sume#include <linux/bpf.h> 15158115Sume#include <linux/bpf_local_storage.h> 16158115Sume#include <linux/filter.h> 17158115Sume#include <uapi/linux/btf.h> 18158115Sume#include <linux/btf_ids.h> 19158115Sume#include <linux/fdtable.h> 20158115Sume#include <linux/rcupdate_trace.h> 21158115Sume 22158115SumeDEFINE_BPF_STORAGE_CACHE(task_cache); 23158115Sume 24158115Sumestatic DEFINE_PER_CPU(int, bpf_task_storage_busy); 25158115Sume 26158115Sumestatic void bpf_task_storage_lock(void) 27158115Sume{ 28158115Sume migrate_disable(); 29158115Sume this_cpu_inc(bpf_task_storage_busy); 30158115Sume} 31158115Sume 32158115Sumestatic void bpf_task_storage_unlock(void) 33158115Sume{ 34158115Sume this_cpu_dec(bpf_task_storage_busy); 35158115Sume migrate_enable(); 36158115Sume} 37158115Sume 38158115Sumestatic bool bpf_task_storage_trylock(void) 39158115Sume{ 40158115Sume migrate_disable(); 41158115Sume if (unlikely(this_cpu_inc_return(bpf_task_storage_busy) != 1)) { 42158115Sume this_cpu_dec(bpf_task_storage_busy); 43158115Sume migrate_enable(); 44158115Sume return false; 45158115Sume } 46158115Sume return true; 47158115Sume} 48158115Sume 49158115Sumestatic struct bpf_local_storage __rcu **task_storage_ptr(void *owner) 50158115Sume{ 51158115Sume struct task_struct *task = owner; 52158115Sume 53158115Sume return &task->bpf_storage; 54158115Sume} 55158115Sume 56158115Sumestatic struct bpf_local_storage_data * 57158115Sumetask_storage_lookup(struct task_struct *task, struct bpf_map *map, 58158115Sume bool cacheit_lockit) 59158115Sume{ 60158115Sume struct bpf_local_storage *task_storage; 61158115Sume struct bpf_local_storage_map *smap; 62158115Sume 63158115Sume task_storage = 64158115Sume rcu_dereference_check(task->bpf_storage, bpf_rcu_lock_held()); 65158115Sume if (!task_storage) 66158115Sume return NULL; 67158115Sume 68158115Sume smap = (struct bpf_local_storage_map *)map; 69158115Sume return bpf_local_storage_lookup(task_storage, smap, cacheit_lockit); 70158115Sume} 71158115Sume 72158115Sumevoid bpf_task_storage_free(struct task_struct *task) 73158115Sume{ 74158115Sume struct bpf_local_storage *local_storage; 75158115Sume 76158115Sume rcu_read_lock(); 77158115Sume 78158115Sume local_storage = rcu_dereference(task->bpf_storage); 79158115Sume if (!local_storage) { 80158115Sume rcu_read_unlock(); 81158115Sume return; 82158115Sume } 83158115Sume 84158115Sume bpf_task_storage_lock(); 85158115Sume bpf_local_storage_destroy(local_storage); 86158115Sume bpf_task_storage_unlock(); 87158115Sume rcu_read_unlock(); 88158115Sume} 89158115Sume 90158115Sumestatic void *bpf_pid_task_storage_lookup_elem(struct bpf_map *map, void *key) 91158115Sume{ 92158115Sume struct bpf_local_storage_data *sdata; 93158115Sume struct task_struct *task; 94158115Sume unsigned int f_flags; 95158115Sume struct pid *pid; 96158115Sume int fd, err; 97158115Sume 98158115Sume fd = *(int *)key; 99158115Sume pid = pidfd_get_pid(fd, &f_flags); 100158115Sume if (IS_ERR(pid)) 101158115Sume return ERR_CAST(pid); 102158115Sume 103158115Sume /* We should be in an RCU read side critical section, it should be safe 104158115Sume * to call pid_task. 105158115Sume */ 106158115Sume WARN_ON_ONCE(!rcu_read_lock_held()); 107158115Sume task = pid_task(pid, PIDTYPE_PID); 108158115Sume if (!task) { 109158115Sume err = -ENOENT; 110158115Sume goto out; 111158115Sume } 112158115Sume 113158115Sume bpf_task_storage_lock(); 114158115Sume sdata = task_storage_lookup(task, map, true); 115158115Sume bpf_task_storage_unlock(); 116158115Sume put_pid(pid); 117158115Sume return sdata ? sdata->data : NULL; 118158115Sumeout: 119158115Sume put_pid(pid); 120158115Sume return ERR_PTR(err); 121158115Sume} 122158115Sume 123158115Sumestatic long bpf_pid_task_storage_update_elem(struct bpf_map *map, void *key, 124158115Sume void *value, u64 map_flags) 125158115Sume{ 126158115Sume struct bpf_local_storage_data *sdata; 127158115Sume struct task_struct *task; 128158115Sume unsigned int f_flags; 129158115Sume struct pid *pid; 130158115Sume int fd, err; 131158115Sume 132158115Sume fd = *(int *)key; 133158115Sume pid = pidfd_get_pid(fd, &f_flags); 134158115Sume if (IS_ERR(pid)) 135158115Sume return PTR_ERR(pid); 136158115Sume 137158115Sume /* We should be in an RCU read side critical section, it should be safe 138158115Sume * to call pid_task. 139158115Sume */ 140158115Sume WARN_ON_ONCE(!rcu_read_lock_held()); 141158115Sume task = pid_task(pid, PIDTYPE_PID); 142158115Sume if (!task) { 143158115Sume err = -ENOENT; 144158115Sume goto out; 145158115Sume } 146158115Sume 147158115Sume bpf_task_storage_lock(); 148158115Sume sdata = bpf_local_storage_update( 149158115Sume task, (struct bpf_local_storage_map *)map, value, map_flags, 150158115Sume GFP_ATOMIC); 151158115Sume bpf_task_storage_unlock(); 152158115Sume 153158115Sume err = PTR_ERR_OR_ZERO(sdata); 154158115Sumeout: 155158115Sume put_pid(pid); 156158115Sume return err; 157158115Sume} 158158115Sume 159158115Sumestatic int task_storage_delete(struct task_struct *task, struct bpf_map *map, 160158115Sume bool nobusy) 161158115Sume{ 162158115Sume struct bpf_local_storage_data *sdata; 163158115Sume 164158115Sume sdata = task_storage_lookup(task, map, false); 165158115Sume if (!sdata) 166158115Sume return -ENOENT; 167158115Sume 168158115Sume if (!nobusy) 169158115Sume return -EBUSY; 170158115Sume 171158115Sume bpf_selem_unlink(SELEM(sdata), false); 172158115Sume 173158115Sume return 0; 174158115Sume} 175158115Sume 176158115Sumestatic long bpf_pid_task_storage_delete_elem(struct bpf_map *map, void *key) 177158115Sume{ 178158115Sume struct task_struct *task; 179158115Sume unsigned int f_flags; 180158115Sume struct pid *pid; 181158115Sume int fd, err; 182158115Sume 183158115Sume fd = *(int *)key; 184158115Sume pid = pidfd_get_pid(fd, &f_flags); 185158115Sume if (IS_ERR(pid)) 186158115Sume return PTR_ERR(pid); 187158115Sume 188158115Sume /* We should be in an RCU read side critical section, it should be safe 189158115Sume * to call pid_task. 190158115Sume */ 191158115Sume WARN_ON_ONCE(!rcu_read_lock_held()); 192158115Sume task = pid_task(pid, PIDTYPE_PID); 193158115Sume if (!task) { 194158115Sume err = -ENOENT; 195158115Sume goto out; 196158115Sume } 197158115Sume 198158115Sume bpf_task_storage_lock(); 199158115Sume err = task_storage_delete(task, map, true); 200158115Sume bpf_task_storage_unlock(); 201158115Sumeout: 202158115Sume put_pid(pid); 203158115Sume return err; 204158115Sume} 205158115Sume 206158115Sume/* Called by bpf_task_storage_get*() helpers */ 207158115Sumestatic void *__bpf_task_storage_get(struct bpf_map *map, 208158115Sume struct task_struct *task, void *value, 209158115Sume u64 flags, gfp_t gfp_flags, bool nobusy) 210158115Sume{ 211158115Sume struct bpf_local_storage_data *sdata; 212158115Sume 213158115Sume sdata = task_storage_lookup(task, map, nobusy); 214158115Sume if (sdata) 215158115Sume return sdata->data; 216158115Sume 217158115Sume /* only allocate new storage, when the task is refcounted */ 218158115Sume 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; 224 } 225 226 return NULL; 227} 228 229/* *gfp_flags* is a hidden argument provided by the verifier */ 230BPF_CALL_5(bpf_task_storage_get_recur, struct bpf_map *, map, struct task_struct *, 231 task, void *, value, u64, flags, gfp_t, gfp_flags) 232{ 233 bool nobusy; 234 void *data; 235 236 WARN_ON_ONCE(!bpf_rcu_lock_held()); 237 if (flags & ~BPF_LOCAL_STORAGE_GET_F_CREATE || !task) 238 return (unsigned long)NULL; 239 240 nobusy = bpf_task_storage_trylock(); 241 data = __bpf_task_storage_get(map, task, value, flags, 242 gfp_flags, nobusy); 243 if (nobusy) 244 bpf_task_storage_unlock(); 245 return (unsigned long)data; 246} 247 248/* *gfp_flags* is a hidden argument provided by the verifier */ 249BPF_CALL_5(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *, 250 task, void *, value, u64, flags, gfp_t, gfp_flags) 251{ 252 void *data; 253 254 WARN_ON_ONCE(!bpf_rcu_lock_held()); 255 if (flags & ~BPF_LOCAL_STORAGE_GET_F_CREATE || !task) 256 return (unsigned long)NULL; 257 258 bpf_task_storage_lock(); 259 data = __bpf_task_storage_get(map, task, value, flags, 260 gfp_flags, true); 261 bpf_task_storage_unlock(); 262 return (unsigned long)data; 263} 264 265BPF_CALL_2(bpf_task_storage_delete_recur, struct bpf_map *, map, struct task_struct *, 266 task) 267{ 268 bool nobusy; 269 int ret; 270 271 WARN_ON_ONCE(!bpf_rcu_lock_held()); 272 if (!task) 273 return -EINVAL; 274 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. 279 */ 280 ret = task_storage_delete(task, map, nobusy); 281 if (nobusy) 282 bpf_task_storage_unlock(); 283 return ret; 284} 285 286BPF_CALL_2(bpf_task_storage_delete, struct bpf_map *, map, struct task_struct *, 287 task) 288{ 289 int ret; 290 291 WARN_ON_ONCE(!bpf_rcu_lock_held()); 292 if (!task) 293 return -EINVAL; 294 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. 299 */ 300 ret = task_storage_delete(task, map, true); 301 bpf_task_storage_unlock(); 302 return ret; 303} 304 305static int notsupp_get_next_key(struct bpf_map *map, void *key, void *next_key) 306{ 307 return -ENOTSUPP; 308} 309 310static struct bpf_map *task_storage_map_alloc(union bpf_attr *attr) 311{ 312 return bpf_local_storage_map_alloc(attr, &task_cache, true); 313} 314 315static void task_storage_map_free(struct bpf_map *map) 316{ 317 bpf_local_storage_map_free(map, &task_cache, &bpf_task_storage_busy); 318} 319 320BTF_ID_LIST_GLOBAL_SINGLE(bpf_local_storage_map_btf_id, struct, bpf_local_storage_map) 321const 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, 334}; 335 336const struct bpf_func_proto bpf_task_storage_get_recur_proto = { 337 .func = bpf_task_storage_get_recur, 338 .gpl_only = false, 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, 345}; 346 347const struct bpf_func_proto bpf_task_storage_get_proto = { 348 .func = bpf_task_storage_get, 349 .gpl_only = false, 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, 356}; 357 358const struct bpf_func_proto bpf_task_storage_delete_recur_proto = { 359 .func = bpf_task_storage_delete_recur, 360 .gpl_only = false, 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], 365}; 366 367const struct bpf_func_proto bpf_task_storage_delete_proto = { 368 .func = bpf_task_storage_delete, 369 .gpl_only = false, 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], 374}; 375