bpf_cgrp_storage.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
  4. */
  5. #include <linux/types.h>
  6. #include <linux/bpf.h>
  7. #include <linux/bpf_local_storage.h>
  8. #include <uapi/linux/btf.h>
  9. #include <linux/btf_ids.h>
  10. DEFINE_BPF_STORAGE_CACHE(cgroup_cache);
  11. static DEFINE_PER_CPU(int, bpf_cgrp_storage_busy);
  12. static void bpf_cgrp_storage_lock(void)
  13. {
  14. migrate_disable();
  15. this_cpu_inc(bpf_cgrp_storage_busy);
  16. }
  17. static void bpf_cgrp_storage_unlock(void)
  18. {
  19. this_cpu_dec(bpf_cgrp_storage_busy);
  20. migrate_enable();
  21. }
  22. static bool bpf_cgrp_storage_trylock(void)
  23. {
  24. migrate_disable();
  25. if (unlikely(this_cpu_inc_return(bpf_cgrp_storage_busy) != 1)) {
  26. this_cpu_dec(bpf_cgrp_storage_busy);
  27. migrate_enable();
  28. return false;
  29. }
  30. return true;
  31. }
  32. static struct bpf_local_storage __rcu **cgroup_storage_ptr(void *owner)
  33. {
  34. struct cgroup *cg = owner;
  35. return &cg->bpf_cgrp_storage;
  36. }
  37. void bpf_cgrp_storage_free(struct cgroup *cgroup)
  38. {
  39. struct bpf_local_storage *local_storage;
  40. rcu_read_lock();
  41. local_storage = rcu_dereference(cgroup->bpf_cgrp_storage);
  42. if (!local_storage) {
  43. rcu_read_unlock();
  44. return;
  45. }
  46. bpf_cgrp_storage_lock();
  47. bpf_local_storage_destroy(local_storage);
  48. bpf_cgrp_storage_unlock();
  49. rcu_read_unlock();
  50. }
  51. static struct bpf_local_storage_data *
  52. cgroup_storage_lookup(struct cgroup *cgroup, struct bpf_map *map, bool cacheit_lockit)
  53. {
  54. struct bpf_local_storage *cgroup_storage;
  55. struct bpf_local_storage_map *smap;
  56. cgroup_storage = rcu_dereference_check(cgroup->bpf_cgrp_storage,
  57. bpf_rcu_lock_held());
  58. if (!cgroup_storage)
  59. return NULL;
  60. smap = (struct bpf_local_storage_map *)map;
  61. return bpf_local_storage_lookup(cgroup_storage, smap, cacheit_lockit);
  62. }
  63. static void *bpf_cgrp_storage_lookup_elem(struct bpf_map *map, void *key)
  64. {
  65. struct bpf_local_storage_data *sdata;
  66. struct cgroup *cgroup;
  67. int fd;
  68. fd = *(int *)key;
  69. cgroup = cgroup_v1v2_get_from_fd(fd);
  70. if (IS_ERR(cgroup))
  71. return ERR_CAST(cgroup);
  72. bpf_cgrp_storage_lock();
  73. sdata = cgroup_storage_lookup(cgroup, map, true);
  74. bpf_cgrp_storage_unlock();
  75. cgroup_put(cgroup);
  76. return sdata ? sdata->data : NULL;
  77. }
  78. static long bpf_cgrp_storage_update_elem(struct bpf_map *map, void *key,
  79. void *value, u64 map_flags)
  80. {
  81. struct bpf_local_storage_data *sdata;
  82. struct cgroup *cgroup;
  83. int fd;
  84. fd = *(int *)key;
  85. cgroup = cgroup_v1v2_get_from_fd(fd);
  86. if (IS_ERR(cgroup))
  87. return PTR_ERR(cgroup);
  88. bpf_cgrp_storage_lock();
  89. sdata = bpf_local_storage_update(cgroup, (struct bpf_local_storage_map *)map,
  90. value, map_flags, GFP_ATOMIC);
  91. bpf_cgrp_storage_unlock();
  92. cgroup_put(cgroup);
  93. return PTR_ERR_OR_ZERO(sdata);
  94. }
  95. static int cgroup_storage_delete(struct cgroup *cgroup, struct bpf_map *map)
  96. {
  97. struct bpf_local_storage_data *sdata;
  98. sdata = cgroup_storage_lookup(cgroup, map, false);
  99. if (!sdata)
  100. return -ENOENT;
  101. bpf_selem_unlink(SELEM(sdata), false);
  102. return 0;
  103. }
  104. static long bpf_cgrp_storage_delete_elem(struct bpf_map *map, void *key)
  105. {
  106. struct cgroup *cgroup;
  107. int err, fd;
  108. fd = *(int *)key;
  109. cgroup = cgroup_v1v2_get_from_fd(fd);
  110. if (IS_ERR(cgroup))
  111. return PTR_ERR(cgroup);
  112. bpf_cgrp_storage_lock();
  113. err = cgroup_storage_delete(cgroup, map);
  114. bpf_cgrp_storage_unlock();
  115. cgroup_put(cgroup);
  116. return err;
  117. }
  118. static int notsupp_get_next_key(struct bpf_map *map, void *key, void *next_key)
  119. {
  120. return -ENOTSUPP;
  121. }
  122. static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
  123. {
  124. return bpf_local_storage_map_alloc(attr, &cgroup_cache, true);
  125. }
  126. static void cgroup_storage_map_free(struct bpf_map *map)
  127. {
  128. bpf_local_storage_map_free(map, &cgroup_cache, &bpf_cgrp_storage_busy);
  129. }
  130. /* *gfp_flags* is a hidden argument provided by the verifier */
  131. BPF_CALL_5(bpf_cgrp_storage_get, struct bpf_map *, map, struct cgroup *, cgroup,
  132. void *, value, u64, flags, gfp_t, gfp_flags)
  133. {
  134. struct bpf_local_storage_data *sdata;
  135. bool nobusy;
  136. WARN_ON_ONCE(!bpf_rcu_lock_held());
  137. if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE))
  138. return (unsigned long)NULL;
  139. if (!cgroup)
  140. return (unsigned long)NULL;
  141. nobusy = bpf_cgrp_storage_trylock();
  142. sdata = cgroup_storage_lookup(cgroup, map, nobusy);
  143. if (sdata)
  144. goto unlock;
  145. /* only allocate new storage, when the cgroup is refcounted */
  146. if (!percpu_ref_is_dying(&cgroup->self.refcnt) &&
  147. (flags & BPF_LOCAL_STORAGE_GET_F_CREATE) && nobusy)
  148. sdata = bpf_local_storage_update(cgroup, (struct bpf_local_storage_map *)map,
  149. value, BPF_NOEXIST, gfp_flags);
  150. unlock:
  151. if (nobusy)
  152. bpf_cgrp_storage_unlock();
  153. return IS_ERR_OR_NULL(sdata) ? (unsigned long)NULL : (unsigned long)sdata->data;
  154. }
  155. BPF_CALL_2(bpf_cgrp_storage_delete, struct bpf_map *, map, struct cgroup *, cgroup)
  156. {
  157. int ret;
  158. WARN_ON_ONCE(!bpf_rcu_lock_held());
  159. if (!cgroup)
  160. return -EINVAL;
  161. if (!bpf_cgrp_storage_trylock())
  162. return -EBUSY;
  163. ret = cgroup_storage_delete(cgroup, map);
  164. bpf_cgrp_storage_unlock();
  165. return ret;
  166. }
  167. const struct bpf_map_ops cgrp_storage_map_ops = {
  168. .map_meta_equal = bpf_map_meta_equal,
  169. .map_alloc_check = bpf_local_storage_map_alloc_check,
  170. .map_alloc = cgroup_storage_map_alloc,
  171. .map_free = cgroup_storage_map_free,
  172. .map_get_next_key = notsupp_get_next_key,
  173. .map_lookup_elem = bpf_cgrp_storage_lookup_elem,
  174. .map_update_elem = bpf_cgrp_storage_update_elem,
  175. .map_delete_elem = bpf_cgrp_storage_delete_elem,
  176. .map_check_btf = bpf_local_storage_map_check_btf,
  177. .map_mem_usage = bpf_local_storage_map_mem_usage,
  178. .map_btf_id = &bpf_local_storage_map_btf_id[0],
  179. .map_owner_storage_ptr = cgroup_storage_ptr,
  180. };
  181. const struct bpf_func_proto bpf_cgrp_storage_get_proto = {
  182. .func = bpf_cgrp_storage_get,
  183. .gpl_only = false,
  184. .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
  185. .arg1_type = ARG_CONST_MAP_PTR,
  186. .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
  187. .arg2_btf_id = &bpf_cgroup_btf_id[0],
  188. .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
  189. .arg4_type = ARG_ANYTHING,
  190. };
  191. const struct bpf_func_proto bpf_cgrp_storage_delete_proto = {
  192. .func = bpf_cgrp_storage_delete,
  193. .gpl_only = false,
  194. .ret_type = RET_INTEGER,
  195. .arg1_type = ARG_CONST_MAP_PTR,
  196. .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
  197. .arg2_btf_id = &bpf_cgroup_btf_id[0],
  198. };