local_storage.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. //SPDX-License-Identifier: GPL-2.0
  2. #include <linux/bpf-cgroup.h>
  3. #include <linux/bpf.h>
  4. #include <linux/bug.h>
  5. #include <linux/filter.h>
  6. #include <linux/mm.h>
  7. #include <linux/rbtree.h>
  8. #include <linux/slab.h>
  9. DEFINE_PER_CPU(struct bpf_cgroup_storage*,
  10. bpf_cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]);
  11. #ifdef CONFIG_CGROUP_BPF
  12. #define LOCAL_STORAGE_CREATE_FLAG_MASK \
  13. (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
  14. struct bpf_cgroup_storage_map {
  15. struct bpf_map map;
  16. spinlock_t lock;
  17. struct bpf_prog *prog;
  18. struct rb_root root;
  19. struct list_head list;
  20. };
  21. static struct bpf_cgroup_storage_map *map_to_storage(struct bpf_map *map)
  22. {
  23. return container_of(map, struct bpf_cgroup_storage_map, map);
  24. }
  25. static int bpf_cgroup_storage_key_cmp(
  26. const struct bpf_cgroup_storage_key *key1,
  27. const struct bpf_cgroup_storage_key *key2)
  28. {
  29. if (key1->cgroup_inode_id < key2->cgroup_inode_id)
  30. return -1;
  31. else if (key1->cgroup_inode_id > key2->cgroup_inode_id)
  32. return 1;
  33. else if (key1->attach_type < key2->attach_type)
  34. return -1;
  35. else if (key1->attach_type > key2->attach_type)
  36. return 1;
  37. return 0;
  38. }
  39. static struct bpf_cgroup_storage *cgroup_storage_lookup(
  40. struct bpf_cgroup_storage_map *map, struct bpf_cgroup_storage_key *key,
  41. bool locked)
  42. {
  43. struct rb_root *root = &map->root;
  44. struct rb_node *node;
  45. if (!locked)
  46. spin_lock_bh(&map->lock);
  47. node = root->rb_node;
  48. while (node) {
  49. struct bpf_cgroup_storage *storage;
  50. storage = container_of(node, struct bpf_cgroup_storage, node);
  51. switch (bpf_cgroup_storage_key_cmp(key, &storage->key)) {
  52. case -1:
  53. node = node->rb_left;
  54. break;
  55. case 1:
  56. node = node->rb_right;
  57. break;
  58. default:
  59. if (!locked)
  60. spin_unlock_bh(&map->lock);
  61. return storage;
  62. }
  63. }
  64. if (!locked)
  65. spin_unlock_bh(&map->lock);
  66. return NULL;
  67. }
  68. static int cgroup_storage_insert(struct bpf_cgroup_storage_map *map,
  69. struct bpf_cgroup_storage *storage)
  70. {
  71. struct rb_root *root = &map->root;
  72. struct rb_node **new = &(root->rb_node), *parent = NULL;
  73. while (*new) {
  74. struct bpf_cgroup_storage *this;
  75. this = container_of(*new, struct bpf_cgroup_storage, node);
  76. parent = *new;
  77. switch (bpf_cgroup_storage_key_cmp(&storage->key, &this->key)) {
  78. case -1:
  79. new = &((*new)->rb_left);
  80. break;
  81. case 1:
  82. new = &((*new)->rb_right);
  83. break;
  84. default:
  85. return -EEXIST;
  86. }
  87. }
  88. rb_link_node(&storage->node, parent, new);
  89. rb_insert_color(&storage->node, root);
  90. return 0;
  91. }
  92. static void *cgroup_storage_lookup_elem(struct bpf_map *_map, void *_key)
  93. {
  94. struct bpf_cgroup_storage_map *map = map_to_storage(_map);
  95. struct bpf_cgroup_storage_key *key = _key;
  96. struct bpf_cgroup_storage *storage;
  97. storage = cgroup_storage_lookup(map, key, false);
  98. if (!storage)
  99. return NULL;
  100. return &READ_ONCE(storage->buf)->data[0];
  101. }
  102. static int cgroup_storage_update_elem(struct bpf_map *map, void *_key,
  103. void *value, u64 flags)
  104. {
  105. struct bpf_cgroup_storage_key *key = _key;
  106. struct bpf_cgroup_storage *storage;
  107. struct bpf_storage_buffer *new;
  108. if (flags != BPF_ANY && flags != BPF_EXIST)
  109. return -EINVAL;
  110. storage = cgroup_storage_lookup((struct bpf_cgroup_storage_map *)map,
  111. key, false);
  112. if (!storage)
  113. return -ENOENT;
  114. new = kmalloc_node(sizeof(struct bpf_storage_buffer) +
  115. map->value_size,
  116. __GFP_ZERO | GFP_ATOMIC | __GFP_NOWARN,
  117. map->numa_node);
  118. if (!new)
  119. return -ENOMEM;
  120. memcpy(&new->data[0], value, map->value_size);
  121. new = xchg(&storage->buf, new);
  122. kfree_rcu(new, rcu);
  123. return 0;
  124. }
  125. int bpf_percpu_cgroup_storage_copy(struct bpf_map *_map, void *_key,
  126. void *value)
  127. {
  128. struct bpf_cgroup_storage_map *map = map_to_storage(_map);
  129. struct bpf_cgroup_storage_key *key = _key;
  130. struct bpf_cgroup_storage *storage;
  131. int cpu, off = 0;
  132. u32 size;
  133. rcu_read_lock();
  134. storage = cgroup_storage_lookup(map, key, false);
  135. if (!storage) {
  136. rcu_read_unlock();
  137. return -ENOENT;
  138. }
  139. /* per_cpu areas are zero-filled and bpf programs can only
  140. * access 'value_size' of them, so copying rounded areas
  141. * will not leak any kernel data
  142. */
  143. size = round_up(_map->value_size, 8);
  144. for_each_possible_cpu(cpu) {
  145. bpf_long_memcpy(value + off,
  146. per_cpu_ptr(storage->percpu_buf, cpu), size);
  147. off += size;
  148. }
  149. rcu_read_unlock();
  150. return 0;
  151. }
  152. int bpf_percpu_cgroup_storage_update(struct bpf_map *_map, void *_key,
  153. void *value, u64 map_flags)
  154. {
  155. struct bpf_cgroup_storage_map *map = map_to_storage(_map);
  156. struct bpf_cgroup_storage_key *key = _key;
  157. struct bpf_cgroup_storage *storage;
  158. int cpu, off = 0;
  159. u32 size;
  160. if (map_flags != BPF_ANY && map_flags != BPF_EXIST)
  161. return -EINVAL;
  162. rcu_read_lock();
  163. storage = cgroup_storage_lookup(map, key, false);
  164. if (!storage) {
  165. rcu_read_unlock();
  166. return -ENOENT;
  167. }
  168. /* the user space will provide round_up(value_size, 8) bytes that
  169. * will be copied into per-cpu area. bpf programs can only access
  170. * value_size of it. During lookup the same extra bytes will be
  171. * returned or zeros which were zero-filled by percpu_alloc,
  172. * so no kernel data leaks possible
  173. */
  174. size = round_up(_map->value_size, 8);
  175. for_each_possible_cpu(cpu) {
  176. bpf_long_memcpy(per_cpu_ptr(storage->percpu_buf, cpu),
  177. value + off, size);
  178. off += size;
  179. }
  180. rcu_read_unlock();
  181. return 0;
  182. }
  183. static int cgroup_storage_get_next_key(struct bpf_map *_map, void *_key,
  184. void *_next_key)
  185. {
  186. struct bpf_cgroup_storage_map *map = map_to_storage(_map);
  187. struct bpf_cgroup_storage_key *key = _key;
  188. struct bpf_cgroup_storage_key *next = _next_key;
  189. struct bpf_cgroup_storage *storage;
  190. spin_lock_bh(&map->lock);
  191. if (list_empty(&map->list))
  192. goto enoent;
  193. if (key) {
  194. storage = cgroup_storage_lookup(map, key, true);
  195. if (!storage)
  196. goto enoent;
  197. storage = list_next_entry(storage, list);
  198. if (!storage)
  199. goto enoent;
  200. } else {
  201. storage = list_first_entry(&map->list,
  202. struct bpf_cgroup_storage, list);
  203. }
  204. spin_unlock_bh(&map->lock);
  205. next->attach_type = storage->key.attach_type;
  206. next->cgroup_inode_id = storage->key.cgroup_inode_id;
  207. return 0;
  208. enoent:
  209. spin_unlock_bh(&map->lock);
  210. return -ENOENT;
  211. }
  212. static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
  213. {
  214. int numa_node = bpf_map_attr_numa_node(attr);
  215. struct bpf_cgroup_storage_map *map;
  216. if (attr->key_size != sizeof(struct bpf_cgroup_storage_key))
  217. return ERR_PTR(-EINVAL);
  218. if (attr->value_size == 0)
  219. return ERR_PTR(-EINVAL);
  220. if (attr->value_size > PAGE_SIZE)
  221. return ERR_PTR(-E2BIG);
  222. if (attr->map_flags & ~LOCAL_STORAGE_CREATE_FLAG_MASK)
  223. /* reserved bits should not be used */
  224. return ERR_PTR(-EINVAL);
  225. if (attr->max_entries)
  226. /* max_entries is not used and enforced to be 0 */
  227. return ERR_PTR(-EINVAL);
  228. map = kmalloc_node(sizeof(struct bpf_cgroup_storage_map),
  229. __GFP_ZERO | GFP_USER, numa_node);
  230. if (!map)
  231. return ERR_PTR(-ENOMEM);
  232. map->map.pages = round_up(sizeof(struct bpf_cgroup_storage_map),
  233. PAGE_SIZE) >> PAGE_SHIFT;
  234. /* copy mandatory map attributes */
  235. bpf_map_init_from_attr(&map->map, attr);
  236. spin_lock_init(&map->lock);
  237. map->root = RB_ROOT;
  238. INIT_LIST_HEAD(&map->list);
  239. return &map->map;
  240. }
  241. static void cgroup_storage_map_free(struct bpf_map *_map)
  242. {
  243. struct bpf_cgroup_storage_map *map = map_to_storage(_map);
  244. WARN_ON(!RB_EMPTY_ROOT(&map->root));
  245. WARN_ON(!list_empty(&map->list));
  246. kfree(map);
  247. }
  248. static int cgroup_storage_delete_elem(struct bpf_map *map, void *key)
  249. {
  250. return -EINVAL;
  251. }
  252. const struct bpf_map_ops cgroup_storage_map_ops = {
  253. .map_alloc = cgroup_storage_map_alloc,
  254. .map_free = cgroup_storage_map_free,
  255. .map_get_next_key = cgroup_storage_get_next_key,
  256. .map_lookup_elem = cgroup_storage_lookup_elem,
  257. .map_update_elem = cgroup_storage_update_elem,
  258. .map_delete_elem = cgroup_storage_delete_elem,
  259. .map_check_btf = map_check_no_btf,
  260. };
  261. int bpf_cgroup_storage_assign(struct bpf_prog *prog, struct bpf_map *_map)
  262. {
  263. enum bpf_cgroup_storage_type stype = cgroup_storage_type(_map);
  264. struct bpf_cgroup_storage_map *map = map_to_storage(_map);
  265. int ret = -EBUSY;
  266. spin_lock_bh(&map->lock);
  267. if (map->prog && map->prog != prog)
  268. goto unlock;
  269. if (prog->aux->cgroup_storage[stype] &&
  270. prog->aux->cgroup_storage[stype] != _map)
  271. goto unlock;
  272. map->prog = prog;
  273. prog->aux->cgroup_storage[stype] = _map;
  274. ret = 0;
  275. unlock:
  276. spin_unlock_bh(&map->lock);
  277. return ret;
  278. }
  279. void bpf_cgroup_storage_release(struct bpf_prog *prog, struct bpf_map *_map)
  280. {
  281. enum bpf_cgroup_storage_type stype = cgroup_storage_type(_map);
  282. struct bpf_cgroup_storage_map *map = map_to_storage(_map);
  283. spin_lock_bh(&map->lock);
  284. if (map->prog == prog) {
  285. WARN_ON(prog->aux->cgroup_storage[stype] != _map);
  286. map->prog = NULL;
  287. prog->aux->cgroup_storage[stype] = NULL;
  288. }
  289. spin_unlock_bh(&map->lock);
  290. }
  291. static size_t bpf_cgroup_storage_calculate_size(struct bpf_map *map, u32 *pages)
  292. {
  293. size_t size;
  294. if (cgroup_storage_type(map) == BPF_CGROUP_STORAGE_SHARED) {
  295. size = sizeof(struct bpf_storage_buffer) + map->value_size;
  296. *pages = round_up(sizeof(struct bpf_cgroup_storage) + size,
  297. PAGE_SIZE) >> PAGE_SHIFT;
  298. } else {
  299. size = map->value_size;
  300. *pages = round_up(round_up(size, 8) * num_possible_cpus(),
  301. PAGE_SIZE) >> PAGE_SHIFT;
  302. }
  303. return size;
  304. }
  305. struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(struct bpf_prog *prog,
  306. enum bpf_cgroup_storage_type stype)
  307. {
  308. struct bpf_cgroup_storage *storage;
  309. struct bpf_map *map;
  310. gfp_t flags;
  311. size_t size;
  312. u32 pages;
  313. map = prog->aux->cgroup_storage[stype];
  314. if (!map)
  315. return NULL;
  316. size = bpf_cgroup_storage_calculate_size(map, &pages);
  317. if (bpf_map_charge_memlock(map, pages))
  318. return ERR_PTR(-EPERM);
  319. storage = kmalloc_node(sizeof(struct bpf_cgroup_storage),
  320. __GFP_ZERO | GFP_USER, map->numa_node);
  321. if (!storage)
  322. goto enomem;
  323. flags = __GFP_ZERO | GFP_USER;
  324. if (stype == BPF_CGROUP_STORAGE_SHARED) {
  325. storage->buf = kmalloc_node(size, flags, map->numa_node);
  326. if (!storage->buf)
  327. goto enomem;
  328. } else {
  329. storage->percpu_buf = __alloc_percpu_gfp(size, 8, flags);
  330. if (!storage->percpu_buf)
  331. goto enomem;
  332. }
  333. storage->map = (struct bpf_cgroup_storage_map *)map;
  334. return storage;
  335. enomem:
  336. bpf_map_uncharge_memlock(map, pages);
  337. kfree(storage);
  338. return ERR_PTR(-ENOMEM);
  339. }
  340. static void free_shared_cgroup_storage_rcu(struct rcu_head *rcu)
  341. {
  342. struct bpf_cgroup_storage *storage =
  343. container_of(rcu, struct bpf_cgroup_storage, rcu);
  344. kfree(storage->buf);
  345. kfree(storage);
  346. }
  347. static void free_percpu_cgroup_storage_rcu(struct rcu_head *rcu)
  348. {
  349. struct bpf_cgroup_storage *storage =
  350. container_of(rcu, struct bpf_cgroup_storage, rcu);
  351. free_percpu(storage->percpu_buf);
  352. kfree(storage);
  353. }
  354. void bpf_cgroup_storage_free(struct bpf_cgroup_storage *storage)
  355. {
  356. enum bpf_cgroup_storage_type stype;
  357. struct bpf_map *map;
  358. u32 pages;
  359. if (!storage)
  360. return;
  361. map = &storage->map->map;
  362. bpf_cgroup_storage_calculate_size(map, &pages);
  363. bpf_map_uncharge_memlock(map, pages);
  364. stype = cgroup_storage_type(map);
  365. if (stype == BPF_CGROUP_STORAGE_SHARED)
  366. call_rcu(&storage->rcu, free_shared_cgroup_storage_rcu);
  367. else
  368. call_rcu(&storage->rcu, free_percpu_cgroup_storage_rcu);
  369. }
  370. void bpf_cgroup_storage_link(struct bpf_cgroup_storage *storage,
  371. struct cgroup *cgroup,
  372. enum bpf_attach_type type)
  373. {
  374. struct bpf_cgroup_storage_map *map;
  375. if (!storage)
  376. return;
  377. storage->key.attach_type = type;
  378. storage->key.cgroup_inode_id = cgroup->kn->id.id;
  379. map = storage->map;
  380. spin_lock_bh(&map->lock);
  381. WARN_ON(cgroup_storage_insert(map, storage));
  382. list_add(&storage->list, &map->list);
  383. spin_unlock_bh(&map->lock);
  384. }
  385. void bpf_cgroup_storage_unlink(struct bpf_cgroup_storage *storage)
  386. {
  387. struct bpf_cgroup_storage_map *map;
  388. struct rb_root *root;
  389. if (!storage)
  390. return;
  391. map = storage->map;
  392. spin_lock_bh(&map->lock);
  393. root = &map->root;
  394. rb_erase(&storage->node, root);
  395. list_del(&storage->list);
  396. spin_unlock_bh(&map->lock);
  397. }
  398. #endif