bpf_task_storage.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2020 Facebook
  4. * Copyright 2020 Google LLC.
  5. */
  6. #include <linux/pid.h>
  7. #include <linux/sched.h>
  8. #include <linux/rculist.h>
  9. #include <linux/list.h>
  10. #include <linux/hash.h>
  11. #include <linux/types.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/bpf.h>
  14. #include <linux/bpf_local_storage.h>
  15. #include <linux/filter.h>
  16. #include <uapi/linux/btf.h>
  17. #include <linux/btf_ids.h>
  18. #include <linux/fdtable.h>
  19. #include <linux/rcupdate_trace.h>
  20. DEFINE_BPF_STORAGE_CACHE(task_cache);
  21. static DEFINE_PER_CPU(int, bpf_task_storage_busy);
  22. static void bpf_task_storage_lock(void)
  23. {
  24. migrate_disable();
  25. this_cpu_inc(bpf_task_storage_busy);
  26. }
  27. static void bpf_task_storage_unlock(void)
  28. {
  29. this_cpu_dec(bpf_task_storage_busy);
  30. migrate_enable();
  31. }
  32. static bool bpf_task_storage_trylock(void)
  33. {
  34. migrate_disable();
  35. if (unlikely(this_cpu_inc_return(bpf_task_storage_busy) != 1)) {
  36. this_cpu_dec(bpf_task_storage_busy);
  37. migrate_enable();
  38. return false;
  39. }
  40. return true;
  41. }
  42. static struct bpf_local_storage __rcu **task_storage_ptr(void *owner)
  43. {
  44. struct task_struct *task = owner;
  45. return &task->bpf_storage;
  46. }
  47. static struct bpf_local_storage_data *
  48. task_storage_lookup(struct task_struct *task, struct bpf_map *map,
  49. bool cacheit_lockit)
  50. {
  51. struct bpf_local_storage *task_storage;
  52. struct bpf_local_storage_map *smap;
  53. task_storage =
  54. rcu_dereference_check(task->bpf_storage, bpf_rcu_lock_held());
  55. if (!task_storage)
  56. return NULL;
  57. smap = (struct bpf_local_storage_map *)map;
  58. return bpf_local_storage_lookup(task_storage, smap, cacheit_lockit);
  59. }
  60. void bpf_task_storage_free(struct task_struct *task)
  61. {
  62. struct bpf_local_storage_elem *selem;
  63. struct bpf_local_storage *local_storage;
  64. bool free_task_storage = false;
  65. struct hlist_node *n;
  66. unsigned long flags;
  67. rcu_read_lock();
  68. local_storage = rcu_dereference(task->bpf_storage);
  69. if (!local_storage) {
  70. rcu_read_unlock();
  71. return;
  72. }
  73. /* Neither the bpf_prog nor the bpf-map's syscall
  74. * could be modifying the local_storage->list now.
  75. * Thus, no elem can be added-to or deleted-from the
  76. * local_storage->list by the bpf_prog or by the bpf-map's syscall.
  77. *
  78. * It is racing with bpf_local_storage_map_free() alone
  79. * when unlinking elem from the local_storage->list and
  80. * the map's bucket->list.
  81. */
  82. bpf_task_storage_lock();
  83. raw_spin_lock_irqsave(&local_storage->lock, flags);
  84. hlist_for_each_entry_safe(selem, n, &local_storage->list, snode) {
  85. /* Always unlink from map before unlinking from
  86. * local_storage.
  87. */
  88. bpf_selem_unlink_map(selem);
  89. free_task_storage = bpf_selem_unlink_storage_nolock(
  90. local_storage, selem, false, false);
  91. }
  92. raw_spin_unlock_irqrestore(&local_storage->lock, flags);
  93. bpf_task_storage_unlock();
  94. rcu_read_unlock();
  95. /* free_task_storage should always be true as long as
  96. * local_storage->list was non-empty.
  97. */
  98. if (free_task_storage)
  99. kfree_rcu(local_storage, rcu);
  100. }
  101. static void *bpf_pid_task_storage_lookup_elem(struct bpf_map *map, void *key)
  102. {
  103. struct bpf_local_storage_data *sdata;
  104. struct task_struct *task;
  105. unsigned int f_flags;
  106. struct pid *pid;
  107. int fd, err;
  108. fd = *(int *)key;
  109. pid = pidfd_get_pid(fd, &f_flags);
  110. if (IS_ERR(pid))
  111. return ERR_CAST(pid);
  112. /* We should be in an RCU read side critical section, it should be safe
  113. * to call pid_task.
  114. */
  115. WARN_ON_ONCE(!rcu_read_lock_held());
  116. task = pid_task(pid, PIDTYPE_PID);
  117. if (!task) {
  118. err = -ENOENT;
  119. goto out;
  120. }
  121. bpf_task_storage_lock();
  122. sdata = task_storage_lookup(task, map, true);
  123. bpf_task_storage_unlock();
  124. put_pid(pid);
  125. return sdata ? sdata->data : NULL;
  126. out:
  127. put_pid(pid);
  128. return ERR_PTR(err);
  129. }
  130. static int bpf_pid_task_storage_update_elem(struct bpf_map *map, void *key,
  131. void *value, u64 map_flags)
  132. {
  133. struct bpf_local_storage_data *sdata;
  134. struct task_struct *task;
  135. unsigned int f_flags;
  136. struct pid *pid;
  137. int fd, err;
  138. fd = *(int *)key;
  139. pid = pidfd_get_pid(fd, &f_flags);
  140. if (IS_ERR(pid))
  141. return PTR_ERR(pid);
  142. /* We should be in an RCU read side critical section, it should be safe
  143. * to call pid_task.
  144. */
  145. WARN_ON_ONCE(!rcu_read_lock_held());
  146. task = pid_task(pid, PIDTYPE_PID);
  147. if (!task) {
  148. err = -ENOENT;
  149. goto out;
  150. }
  151. bpf_task_storage_lock();
  152. sdata = bpf_local_storage_update(
  153. task, (struct bpf_local_storage_map *)map, value, map_flags,
  154. GFP_ATOMIC);
  155. bpf_task_storage_unlock();
  156. err = PTR_ERR_OR_ZERO(sdata);
  157. out:
  158. put_pid(pid);
  159. return err;
  160. }
  161. static int task_storage_delete(struct task_struct *task, struct bpf_map *map)
  162. {
  163. struct bpf_local_storage_data *sdata;
  164. sdata = task_storage_lookup(task, map, false);
  165. if (!sdata)
  166. return -ENOENT;
  167. bpf_selem_unlink(SELEM(sdata), true);
  168. return 0;
  169. }
  170. static int bpf_pid_task_storage_delete_elem(struct bpf_map *map, void *key)
  171. {
  172. struct task_struct *task;
  173. unsigned int f_flags;
  174. struct pid *pid;
  175. int fd, err;
  176. fd = *(int *)key;
  177. pid = pidfd_get_pid(fd, &f_flags);
  178. if (IS_ERR(pid))
  179. return PTR_ERR(pid);
  180. /* We should be in an RCU read side critical section, it should be safe
  181. * to call pid_task.
  182. */
  183. WARN_ON_ONCE(!rcu_read_lock_held());
  184. task = pid_task(pid, PIDTYPE_PID);
  185. if (!task) {
  186. err = -ENOENT;
  187. goto out;
  188. }
  189. bpf_task_storage_lock();
  190. err = task_storage_delete(task, map);
  191. bpf_task_storage_unlock();
  192. out:
  193. put_pid(pid);
  194. return err;
  195. }
  196. /* *gfp_flags* is a hidden argument provided by the verifier */
  197. BPF_CALL_5(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *,
  198. task, void *, value, u64, flags, gfp_t, gfp_flags)
  199. {
  200. struct bpf_local_storage_data *sdata;
  201. WARN_ON_ONCE(!bpf_rcu_lock_held());
  202. if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE))
  203. return (unsigned long)NULL;
  204. if (!task)
  205. return (unsigned long)NULL;
  206. if (!bpf_task_storage_trylock())
  207. return (unsigned long)NULL;
  208. sdata = task_storage_lookup(task, map, true);
  209. if (sdata)
  210. goto unlock;
  211. /* only allocate new storage, when the task is refcounted */
  212. if (refcount_read(&task->usage) &&
  213. (flags & BPF_LOCAL_STORAGE_GET_F_CREATE))
  214. sdata = bpf_local_storage_update(
  215. task, (struct bpf_local_storage_map *)map, value,
  216. BPF_NOEXIST, gfp_flags);
  217. unlock:
  218. bpf_task_storage_unlock();
  219. return IS_ERR_OR_NULL(sdata) ? (unsigned long)NULL :
  220. (unsigned long)sdata->data;
  221. }
  222. BPF_CALL_2(bpf_task_storage_delete, struct bpf_map *, map, struct task_struct *,
  223. task)
  224. {
  225. int ret;
  226. WARN_ON_ONCE(!bpf_rcu_lock_held());
  227. if (!task)
  228. return -EINVAL;
  229. if (!bpf_task_storage_trylock())
  230. return -EBUSY;
  231. /* This helper must only be called from places where the lifetime of the task
  232. * is guaranteed. Either by being refcounted or by being protected
  233. * by an RCU read-side critical section.
  234. */
  235. ret = task_storage_delete(task, map);
  236. bpf_task_storage_unlock();
  237. return ret;
  238. }
  239. static int notsupp_get_next_key(struct bpf_map *map, void *key, void *next_key)
  240. {
  241. return -ENOTSUPP;
  242. }
  243. static struct bpf_map *task_storage_map_alloc(union bpf_attr *attr)
  244. {
  245. struct bpf_local_storage_map *smap;
  246. smap = bpf_local_storage_map_alloc(attr);
  247. if (IS_ERR(smap))
  248. return ERR_CAST(smap);
  249. smap->cache_idx = bpf_local_storage_cache_idx_get(&task_cache);
  250. return &smap->map;
  251. }
  252. static void task_storage_map_free(struct bpf_map *map)
  253. {
  254. struct bpf_local_storage_map *smap;
  255. smap = (struct bpf_local_storage_map *)map;
  256. bpf_local_storage_cache_idx_free(&task_cache, smap->cache_idx);
  257. bpf_local_storage_map_free(smap, &bpf_task_storage_busy);
  258. }
  259. BTF_ID_LIST_SINGLE(task_storage_map_btf_ids, struct, bpf_local_storage_map)
  260. const struct bpf_map_ops task_storage_map_ops = {
  261. .map_meta_equal = bpf_map_meta_equal,
  262. .map_alloc_check = bpf_local_storage_map_alloc_check,
  263. .map_alloc = task_storage_map_alloc,
  264. .map_free = task_storage_map_free,
  265. .map_get_next_key = notsupp_get_next_key,
  266. .map_lookup_elem = bpf_pid_task_storage_lookup_elem,
  267. .map_update_elem = bpf_pid_task_storage_update_elem,
  268. .map_delete_elem = bpf_pid_task_storage_delete_elem,
  269. .map_check_btf = bpf_local_storage_map_check_btf,
  270. .map_btf_id = &task_storage_map_btf_ids[0],
  271. .map_owner_storage_ptr = task_storage_ptr,
  272. };
  273. const struct bpf_func_proto bpf_task_storage_get_proto = {
  274. .func = bpf_task_storage_get,
  275. .gpl_only = false,
  276. .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
  277. .arg1_type = ARG_CONST_MAP_PTR,
  278. .arg2_type = ARG_PTR_TO_BTF_ID,
  279. .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
  280. .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
  281. .arg4_type = ARG_ANYTHING,
  282. };
  283. const struct bpf_func_proto bpf_task_storage_delete_proto = {
  284. .func = bpf_task_storage_delete,
  285. .gpl_only = false,
  286. .ret_type = RET_INTEGER,
  287. .arg1_type = ARG_CONST_MAP_PTR,
  288. .arg2_type = ARG_PTR_TO_BTF_ID,
  289. .arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
  290. };