local_storage.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. //SPDX-License-Identifier: GPL-2.0
  2. #include <linux/bpf-cgroup.h>
  3. #include <linux/bpf.h>
  4. #include <linux/btf.h>
  5. #include <linux/bug.h>
  6. #include <linux/filter.h>
  7. #include <linux/mm.h>
  8. #include <linux/rbtree.h>
  9. #include <linux/slab.h>
  10. #include <uapi/linux/btf.h>
  11. #ifdef CONFIG_CGROUP_BPF
  12. DEFINE_PER_CPU(struct bpf_cgroup_storage_info,
  13. bpf_cgroup_storage_info[BPF_CGROUP_STORAGE_NEST_MAX]);
  14. #include "../cgroup/cgroup-internal.h"
  15. #define LOCAL_STORAGE_CREATE_FLAG_MASK \
  16. (BPF_F_NUMA_NODE | BPF_F_ACCESS_MASK)
  17. struct bpf_cgroup_storage_map {
  18. struct bpf_map map;
  19. spinlock_t lock;
  20. struct rb_root root;
  21. struct list_head list;
  22. };
  23. static struct bpf_cgroup_storage_map *map_to_storage(struct bpf_map *map)
  24. {
  25. return container_of(map, struct bpf_cgroup_storage_map, map);
  26. }
  27. static bool attach_type_isolated(const struct bpf_map *map)
  28. {
  29. return map->key_size == sizeof(struct bpf_cgroup_storage_key);
  30. }
  31. static int bpf_cgroup_storage_key_cmp(const struct bpf_cgroup_storage_map *map,
  32. const void *_key1, const void *_key2)
  33. {
  34. if (attach_type_isolated(&map->map)) {
  35. const struct bpf_cgroup_storage_key *key1 = _key1;
  36. const struct bpf_cgroup_storage_key *key2 = _key2;
  37. if (key1->cgroup_inode_id < key2->cgroup_inode_id)
  38. return -1;
  39. else if (key1->cgroup_inode_id > key2->cgroup_inode_id)
  40. return 1;
  41. else if (key1->attach_type < key2->attach_type)
  42. return -1;
  43. else if (key1->attach_type > key2->attach_type)
  44. return 1;
  45. } else {
  46. const __u64 *cgroup_inode_id1 = _key1;
  47. const __u64 *cgroup_inode_id2 = _key2;
  48. if (*cgroup_inode_id1 < *cgroup_inode_id2)
  49. return -1;
  50. else if (*cgroup_inode_id1 > *cgroup_inode_id2)
  51. return 1;
  52. }
  53. return 0;
  54. }
  55. struct bpf_cgroup_storage *
  56. cgroup_storage_lookup(struct bpf_cgroup_storage_map *map,
  57. void *key, bool locked)
  58. {
  59. struct rb_root *root = &map->root;
  60. struct rb_node *node;
  61. if (!locked)
  62. spin_lock_bh(&map->lock);
  63. node = root->rb_node;
  64. while (node) {
  65. struct bpf_cgroup_storage *storage;
  66. storage = container_of(node, struct bpf_cgroup_storage, node);
  67. switch (bpf_cgroup_storage_key_cmp(map, key, &storage->key)) {
  68. case -1:
  69. node = node->rb_left;
  70. break;
  71. case 1:
  72. node = node->rb_right;
  73. break;
  74. default:
  75. if (!locked)
  76. spin_unlock_bh(&map->lock);
  77. return storage;
  78. }
  79. }
  80. if (!locked)
  81. spin_unlock_bh(&map->lock);
  82. return NULL;
  83. }
  84. static int cgroup_storage_insert(struct bpf_cgroup_storage_map *map,
  85. struct bpf_cgroup_storage *storage)
  86. {
  87. struct rb_root *root = &map->root;
  88. struct rb_node **new = &(root->rb_node), *parent = NULL;
  89. while (*new) {
  90. struct bpf_cgroup_storage *this;
  91. this = container_of(*new, struct bpf_cgroup_storage, node);
  92. parent = *new;
  93. switch (bpf_cgroup_storage_key_cmp(map, &storage->key, &this->key)) {
  94. case -1:
  95. new = &((*new)->rb_left);
  96. break;
  97. case 1:
  98. new = &((*new)->rb_right);
  99. break;
  100. default:
  101. return -EEXIST;
  102. }
  103. }
  104. rb_link_node(&storage->node, parent, new);
  105. rb_insert_color(&storage->node, root);
  106. return 0;
  107. }
  108. static void *cgroup_storage_lookup_elem(struct bpf_map *_map, void *key)
  109. {
  110. struct bpf_cgroup_storage_map *map = map_to_storage(_map);
  111. struct bpf_cgroup_storage *storage;
  112. storage = cgroup_storage_lookup(map, key, false);
  113. if (!storage)
  114. return NULL;
  115. return &READ_ONCE(storage->buf)->data[0];
  116. }
  117. static int cgroup_storage_update_elem(struct bpf_map *map, void *key,
  118. void *value, u64 flags)
  119. {
  120. struct bpf_cgroup_storage *storage;
  121. struct bpf_storage_buffer *new;
  122. if (unlikely(flags & ~(BPF_F_LOCK | BPF_EXIST)))
  123. return -EINVAL;
  124. if (unlikely((flags & BPF_F_LOCK) &&
  125. !map_value_has_spin_lock(map)))
  126. return -EINVAL;
  127. storage = cgroup_storage_lookup((struct bpf_cgroup_storage_map *)map,
  128. key, false);
  129. if (!storage)
  130. return -ENOENT;
  131. if (flags & BPF_F_LOCK) {
  132. copy_map_value_locked(map, storage->buf->data, value, false);
  133. return 0;
  134. }
  135. new = kmalloc_node(sizeof(struct bpf_storage_buffer) +
  136. map->value_size,
  137. __GFP_ZERO | GFP_ATOMIC | __GFP_NOWARN,
  138. map->numa_node);
  139. if (!new)
  140. return -ENOMEM;
  141. memcpy(&new->data[0], value, map->value_size);
  142. check_and_init_map_lock(map, new->data);
  143. new = xchg(&storage->buf, new);
  144. kfree_rcu(new, rcu);
  145. return 0;
  146. }
  147. int bpf_percpu_cgroup_storage_copy(struct bpf_map *_map, void *key,
  148. void *value)
  149. {
  150. struct bpf_cgroup_storage_map *map = map_to_storage(_map);
  151. struct bpf_cgroup_storage *storage;
  152. int cpu, off = 0;
  153. u32 size;
  154. rcu_read_lock();
  155. storage = cgroup_storage_lookup(map, key, false);
  156. if (!storage) {
  157. rcu_read_unlock();
  158. return -ENOENT;
  159. }
  160. /* per_cpu areas are zero-filled and bpf programs can only
  161. * access 'value_size' of them, so copying rounded areas
  162. * will not leak any kernel data
  163. */
  164. size = round_up(_map->value_size, 8);
  165. for_each_possible_cpu(cpu) {
  166. bpf_long_memcpy(value + off,
  167. per_cpu_ptr(storage->percpu_buf, cpu), size);
  168. off += size;
  169. }
  170. rcu_read_unlock();
  171. return 0;
  172. }
  173. int bpf_percpu_cgroup_storage_update(struct bpf_map *_map, void *key,
  174. void *value, u64 map_flags)
  175. {
  176. struct bpf_cgroup_storage_map *map = map_to_storage(_map);
  177. struct bpf_cgroup_storage *storage;
  178. int cpu, off = 0;
  179. u32 size;
  180. if (map_flags != BPF_ANY && map_flags != BPF_EXIST)
  181. return -EINVAL;
  182. rcu_read_lock();
  183. storage = cgroup_storage_lookup(map, key, false);
  184. if (!storage) {
  185. rcu_read_unlock();
  186. return -ENOENT;
  187. }
  188. /* the user space will provide round_up(value_size, 8) bytes that
  189. * will be copied into per-cpu area. bpf programs can only access
  190. * value_size of it. During lookup the same extra bytes will be
  191. * returned or zeros which were zero-filled by percpu_alloc,
  192. * so no kernel data leaks possible
  193. */
  194. size = round_up(_map->value_size, 8);
  195. for_each_possible_cpu(cpu) {
  196. bpf_long_memcpy(per_cpu_ptr(storage->percpu_buf, cpu),
  197. value + off, size);
  198. off += size;
  199. }
  200. rcu_read_unlock();
  201. return 0;
  202. }
  203. static int cgroup_storage_get_next_key(struct bpf_map *_map, void *key,
  204. void *_next_key)
  205. {
  206. struct bpf_cgroup_storage_map *map = map_to_storage(_map);
  207. struct bpf_cgroup_storage *storage;
  208. spin_lock_bh(&map->lock);
  209. if (list_empty(&map->list))
  210. goto enoent;
  211. if (key) {
  212. storage = cgroup_storage_lookup(map, key, true);
  213. if (!storage)
  214. goto enoent;
  215. storage = list_next_entry(storage, list_map);
  216. if (!storage)
  217. goto enoent;
  218. } else {
  219. storage = list_first_entry(&map->list,
  220. struct bpf_cgroup_storage, list_map);
  221. }
  222. spin_unlock_bh(&map->lock);
  223. if (attach_type_isolated(&map->map)) {
  224. struct bpf_cgroup_storage_key *next = _next_key;
  225. *next = storage->key;
  226. } else {
  227. __u64 *next = _next_key;
  228. *next = storage->key.cgroup_inode_id;
  229. }
  230. return 0;
  231. enoent:
  232. spin_unlock_bh(&map->lock);
  233. return -ENOENT;
  234. }
  235. static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
  236. {
  237. int numa_node = bpf_map_attr_numa_node(attr);
  238. struct bpf_cgroup_storage_map *map;
  239. struct bpf_map_memory mem;
  240. int ret;
  241. if (attr->key_size != sizeof(struct bpf_cgroup_storage_key) &&
  242. attr->key_size != sizeof(__u64))
  243. return ERR_PTR(-EINVAL);
  244. if (attr->value_size == 0)
  245. return ERR_PTR(-EINVAL);
  246. if (attr->value_size > PAGE_SIZE)
  247. return ERR_PTR(-E2BIG);
  248. if (attr->map_flags & ~LOCAL_STORAGE_CREATE_FLAG_MASK ||
  249. !bpf_map_flags_access_ok(attr->map_flags))
  250. return ERR_PTR(-EINVAL);
  251. if (attr->max_entries)
  252. /* max_entries is not used and enforced to be 0 */
  253. return ERR_PTR(-EINVAL);
  254. ret = bpf_map_charge_init(&mem, sizeof(struct bpf_cgroup_storage_map));
  255. if (ret < 0)
  256. return ERR_PTR(ret);
  257. map = kmalloc_node(sizeof(struct bpf_cgroup_storage_map),
  258. __GFP_ZERO | GFP_USER, numa_node);
  259. if (!map) {
  260. bpf_map_charge_finish(&mem);
  261. return ERR_PTR(-ENOMEM);
  262. }
  263. bpf_map_charge_move(&map->map.memory, &mem);
  264. /* copy mandatory map attributes */
  265. bpf_map_init_from_attr(&map->map, attr);
  266. spin_lock_init(&map->lock);
  267. map->root = RB_ROOT;
  268. INIT_LIST_HEAD(&map->list);
  269. return &map->map;
  270. }
  271. static void cgroup_storage_map_free(struct bpf_map *_map)
  272. {
  273. struct bpf_cgroup_storage_map *map = map_to_storage(_map);
  274. struct list_head *storages = &map->list;
  275. struct bpf_cgroup_storage *storage, *stmp;
  276. mutex_lock(&cgroup_mutex);
  277. list_for_each_entry_safe(storage, stmp, storages, list_map) {
  278. bpf_cgroup_storage_unlink(storage);
  279. bpf_cgroup_storage_free(storage);
  280. }
  281. mutex_unlock(&cgroup_mutex);
  282. WARN_ON(!RB_EMPTY_ROOT(&map->root));
  283. WARN_ON(!list_empty(&map->list));
  284. kfree(map);
  285. }
  286. static int cgroup_storage_delete_elem(struct bpf_map *map, void *key)
  287. {
  288. return -EINVAL;
  289. }
  290. static int cgroup_storage_check_btf(const struct bpf_map *map,
  291. const struct btf *btf,
  292. const struct btf_type *key_type,
  293. const struct btf_type *value_type)
  294. {
  295. if (attach_type_isolated(map)) {
  296. struct btf_member *m;
  297. u32 offset, size;
  298. /* Key is expected to be of struct bpf_cgroup_storage_key type,
  299. * which is:
  300. * struct bpf_cgroup_storage_key {
  301. * __u64 cgroup_inode_id;
  302. * __u32 attach_type;
  303. * };
  304. */
  305. /*
  306. * Key_type must be a structure with two fields.
  307. */
  308. if (BTF_INFO_KIND(key_type->info) != BTF_KIND_STRUCT ||
  309. BTF_INFO_VLEN(key_type->info) != 2)
  310. return -EINVAL;
  311. /*
  312. * The first field must be a 64 bit integer at 0 offset.
  313. */
  314. m = (struct btf_member *)(key_type + 1);
  315. size = sizeof_field(struct bpf_cgroup_storage_key, cgroup_inode_id);
  316. if (!btf_member_is_reg_int(btf, key_type, m, 0, size))
  317. return -EINVAL;
  318. /*
  319. * The second field must be a 32 bit integer at 64 bit offset.
  320. */
  321. m++;
  322. offset = offsetof(struct bpf_cgroup_storage_key, attach_type);
  323. size = sizeof_field(struct bpf_cgroup_storage_key, attach_type);
  324. if (!btf_member_is_reg_int(btf, key_type, m, offset, size))
  325. return -EINVAL;
  326. } else {
  327. u32 int_data;
  328. /*
  329. * Key is expected to be u64, which stores the cgroup_inode_id
  330. */
  331. if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
  332. return -EINVAL;
  333. int_data = *(u32 *)(key_type + 1);
  334. if (BTF_INT_BITS(int_data) != 64 || BTF_INT_OFFSET(int_data))
  335. return -EINVAL;
  336. }
  337. return 0;
  338. }
  339. static void cgroup_storage_seq_show_elem(struct bpf_map *map, void *key,
  340. struct seq_file *m)
  341. {
  342. enum bpf_cgroup_storage_type stype = cgroup_storage_type(map);
  343. struct bpf_cgroup_storage *storage;
  344. int cpu;
  345. rcu_read_lock();
  346. storage = cgroup_storage_lookup(map_to_storage(map), key, false);
  347. if (!storage) {
  348. rcu_read_unlock();
  349. return;
  350. }
  351. btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
  352. stype = cgroup_storage_type(map);
  353. if (stype == BPF_CGROUP_STORAGE_SHARED) {
  354. seq_puts(m, ": ");
  355. btf_type_seq_show(map->btf, map->btf_value_type_id,
  356. &READ_ONCE(storage->buf)->data[0], m);
  357. seq_puts(m, "\n");
  358. } else {
  359. seq_puts(m, ": {\n");
  360. for_each_possible_cpu(cpu) {
  361. seq_printf(m, "\tcpu%d: ", cpu);
  362. btf_type_seq_show(map->btf, map->btf_value_type_id,
  363. per_cpu_ptr(storage->percpu_buf, cpu),
  364. m);
  365. seq_puts(m, "\n");
  366. }
  367. seq_puts(m, "}\n");
  368. }
  369. rcu_read_unlock();
  370. }
  371. static int cgroup_storage_map_btf_id;
  372. const struct bpf_map_ops cgroup_storage_map_ops = {
  373. .map_alloc = cgroup_storage_map_alloc,
  374. .map_free = cgroup_storage_map_free,
  375. .map_get_next_key = cgroup_storage_get_next_key,
  376. .map_lookup_elem = cgroup_storage_lookup_elem,
  377. .map_update_elem = cgroup_storage_update_elem,
  378. .map_delete_elem = cgroup_storage_delete_elem,
  379. .map_check_btf = cgroup_storage_check_btf,
  380. .map_seq_show_elem = cgroup_storage_seq_show_elem,
  381. .map_btf_name = "bpf_cgroup_storage_map",
  382. .map_btf_id = &cgroup_storage_map_btf_id,
  383. };
  384. int bpf_cgroup_storage_assign(struct bpf_prog_aux *aux, struct bpf_map *_map)
  385. {
  386. enum bpf_cgroup_storage_type stype = cgroup_storage_type(_map);
  387. if (aux->cgroup_storage[stype] &&
  388. aux->cgroup_storage[stype] != _map)
  389. return -EBUSY;
  390. aux->cgroup_storage[stype] = _map;
  391. return 0;
  392. }
  393. static size_t bpf_cgroup_storage_calculate_size(struct bpf_map *map, u32 *pages)
  394. {
  395. size_t size;
  396. if (cgroup_storage_type(map) == BPF_CGROUP_STORAGE_SHARED) {
  397. size = sizeof(struct bpf_storage_buffer) + map->value_size;
  398. *pages = round_up(sizeof(struct bpf_cgroup_storage) + size,
  399. PAGE_SIZE) >> PAGE_SHIFT;
  400. } else {
  401. size = map->value_size;
  402. *pages = round_up(round_up(size, 8) * num_possible_cpus(),
  403. PAGE_SIZE) >> PAGE_SHIFT;
  404. }
  405. return size;
  406. }
  407. struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(struct bpf_prog *prog,
  408. enum bpf_cgroup_storage_type stype)
  409. {
  410. struct bpf_cgroup_storage *storage;
  411. struct bpf_map *map;
  412. gfp_t flags;
  413. size_t size;
  414. u32 pages;
  415. map = prog->aux->cgroup_storage[stype];
  416. if (!map)
  417. return NULL;
  418. size = bpf_cgroup_storage_calculate_size(map, &pages);
  419. if (bpf_map_charge_memlock(map, pages))
  420. return ERR_PTR(-EPERM);
  421. storage = kmalloc_node(sizeof(struct bpf_cgroup_storage),
  422. __GFP_ZERO | GFP_USER, map->numa_node);
  423. if (!storage)
  424. goto enomem;
  425. flags = __GFP_ZERO | GFP_USER;
  426. if (stype == BPF_CGROUP_STORAGE_SHARED) {
  427. storage->buf = kmalloc_node(size, flags, map->numa_node);
  428. if (!storage->buf)
  429. goto enomem;
  430. check_and_init_map_lock(map, storage->buf->data);
  431. } else {
  432. storage->percpu_buf = __alloc_percpu_gfp(size, 8, flags);
  433. if (!storage->percpu_buf)
  434. goto enomem;
  435. }
  436. storage->map = (struct bpf_cgroup_storage_map *)map;
  437. return storage;
  438. enomem:
  439. bpf_map_uncharge_memlock(map, pages);
  440. kfree(storage);
  441. return ERR_PTR(-ENOMEM);
  442. }
  443. static void free_shared_cgroup_storage_rcu(struct rcu_head *rcu)
  444. {
  445. struct bpf_cgroup_storage *storage =
  446. container_of(rcu, struct bpf_cgroup_storage, rcu);
  447. kfree(storage->buf);
  448. kfree(storage);
  449. }
  450. static void free_percpu_cgroup_storage_rcu(struct rcu_head *rcu)
  451. {
  452. struct bpf_cgroup_storage *storage =
  453. container_of(rcu, struct bpf_cgroup_storage, rcu);
  454. free_percpu(storage->percpu_buf);
  455. kfree(storage);
  456. }
  457. void bpf_cgroup_storage_free(struct bpf_cgroup_storage *storage)
  458. {
  459. enum bpf_cgroup_storage_type stype;
  460. struct bpf_map *map;
  461. u32 pages;
  462. if (!storage)
  463. return;
  464. map = &storage->map->map;
  465. bpf_cgroup_storage_calculate_size(map, &pages);
  466. bpf_map_uncharge_memlock(map, pages);
  467. stype = cgroup_storage_type(map);
  468. if (stype == BPF_CGROUP_STORAGE_SHARED)
  469. call_rcu(&storage->rcu, free_shared_cgroup_storage_rcu);
  470. else
  471. call_rcu(&storage->rcu, free_percpu_cgroup_storage_rcu);
  472. }
  473. void bpf_cgroup_storage_link(struct bpf_cgroup_storage *storage,
  474. struct cgroup *cgroup,
  475. enum bpf_attach_type type)
  476. {
  477. struct bpf_cgroup_storage_map *map;
  478. if (!storage)
  479. return;
  480. storage->key.attach_type = type;
  481. storage->key.cgroup_inode_id = cgroup_id(cgroup);
  482. map = storage->map;
  483. spin_lock_bh(&map->lock);
  484. WARN_ON(cgroup_storage_insert(map, storage));
  485. list_add(&storage->list_map, &map->list);
  486. list_add(&storage->list_cg, &cgroup->bpf.storages);
  487. spin_unlock_bh(&map->lock);
  488. }
  489. void bpf_cgroup_storage_unlink(struct bpf_cgroup_storage *storage)
  490. {
  491. struct bpf_cgroup_storage_map *map;
  492. struct rb_root *root;
  493. if (!storage)
  494. return;
  495. map = storage->map;
  496. spin_lock_bh(&map->lock);
  497. root = &map->root;
  498. rb_erase(&storage->node, root);
  499. list_del(&storage->list_map);
  500. list_del(&storage->list_cg);
  501. spin_unlock_bh(&map->lock);
  502. }
  503. #endif