local_storage.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/bpf-cgroup.h>
  3. #include <linux/bpf.h>
  4. #include <linux/bpf_local_storage.h>
  5. #include <linux/btf.h>
  6. #include <linux/bug.h>
  7. #include <linux/filter.h>
  8. #include <linux/mm.h>
  9. #include <linux/rbtree.h>
  10. #include <linux/slab.h>
  11. #include <uapi/linux/btf.h>
  12. #include <linux/btf_ids.h>
  13. #ifdef CONFIG_CGROUP_BPF
  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 = bpf_map_kmalloc_node(map, struct_size(new, data, map->value_size),
  136. __GFP_ZERO | GFP_NOWAIT | __GFP_NOWARN,
  137. map->numa_node);
  138. if (!new)
  139. return -ENOMEM;
  140. memcpy(&new->data[0], value, map->value_size);
  141. check_and_init_map_value(map, new->data);
  142. new = xchg(&storage->buf, new);
  143. kfree_rcu(new, rcu);
  144. return 0;
  145. }
  146. int bpf_percpu_cgroup_storage_copy(struct bpf_map *_map, void *key,
  147. void *value)
  148. {
  149. struct bpf_cgroup_storage_map *map = map_to_storage(_map);
  150. struct bpf_cgroup_storage *storage;
  151. int cpu, off = 0;
  152. u32 size;
  153. rcu_read_lock();
  154. storage = cgroup_storage_lookup(map, key, false);
  155. if (!storage) {
  156. rcu_read_unlock();
  157. return -ENOENT;
  158. }
  159. /* per_cpu areas are zero-filled and bpf programs can only
  160. * access 'value_size' of them, so copying rounded areas
  161. * will not leak any kernel data
  162. */
  163. size = round_up(_map->value_size, 8);
  164. for_each_possible_cpu(cpu) {
  165. bpf_long_memcpy(value + off,
  166. per_cpu_ptr(storage->percpu_buf, cpu), size);
  167. off += size;
  168. }
  169. rcu_read_unlock();
  170. return 0;
  171. }
  172. int bpf_percpu_cgroup_storage_update(struct bpf_map *_map, void *key,
  173. void *value, u64 map_flags)
  174. {
  175. struct bpf_cgroup_storage_map *map = map_to_storage(_map);
  176. struct bpf_cgroup_storage *storage;
  177. int cpu, off = 0;
  178. u32 size;
  179. if (map_flags != BPF_ANY && map_flags != BPF_EXIST)
  180. return -EINVAL;
  181. rcu_read_lock();
  182. storage = cgroup_storage_lookup(map, key, false);
  183. if (!storage) {
  184. rcu_read_unlock();
  185. return -ENOENT;
  186. }
  187. /* the user space will provide round_up(value_size, 8) bytes that
  188. * will be copied into per-cpu area. bpf programs can only access
  189. * value_size of it. During lookup the same extra bytes will be
  190. * returned or zeros which were zero-filled by percpu_alloc,
  191. * so no kernel data leaks possible
  192. */
  193. size = round_up(_map->value_size, 8);
  194. for_each_possible_cpu(cpu) {
  195. bpf_long_memcpy(per_cpu_ptr(storage->percpu_buf, cpu),
  196. value + off, size);
  197. off += size;
  198. }
  199. rcu_read_unlock();
  200. return 0;
  201. }
  202. static int cgroup_storage_get_next_key(struct bpf_map *_map, void *key,
  203. void *_next_key)
  204. {
  205. struct bpf_cgroup_storage_map *map = map_to_storage(_map);
  206. struct bpf_cgroup_storage *storage;
  207. spin_lock_bh(&map->lock);
  208. if (list_empty(&map->list))
  209. goto enoent;
  210. if (key) {
  211. storage = cgroup_storage_lookup(map, key, true);
  212. if (!storage)
  213. goto enoent;
  214. storage = list_next_entry(storage, list_map);
  215. if (!storage)
  216. goto enoent;
  217. } else {
  218. storage = list_first_entry(&map->list,
  219. struct bpf_cgroup_storage, list_map);
  220. }
  221. spin_unlock_bh(&map->lock);
  222. if (attach_type_isolated(&map->map)) {
  223. struct bpf_cgroup_storage_key *next = _next_key;
  224. *next = storage->key;
  225. } else {
  226. __u64 *next = _next_key;
  227. *next = storage->key.cgroup_inode_id;
  228. }
  229. return 0;
  230. enoent:
  231. spin_unlock_bh(&map->lock);
  232. return -ENOENT;
  233. }
  234. static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
  235. {
  236. __u32 max_value_size = BPF_LOCAL_STORAGE_MAX_VALUE_SIZE;
  237. int numa_node = bpf_map_attr_numa_node(attr);
  238. struct bpf_cgroup_storage_map *map;
  239. /* percpu is bound by PCPU_MIN_UNIT_SIZE, non-percu
  240. * is the same as other local storages.
  241. */
  242. if (attr->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
  243. max_value_size = min_t(__u32, max_value_size,
  244. PCPU_MIN_UNIT_SIZE);
  245. if (attr->key_size != sizeof(struct bpf_cgroup_storage_key) &&
  246. attr->key_size != sizeof(__u64))
  247. return ERR_PTR(-EINVAL);
  248. if (attr->value_size == 0)
  249. return ERR_PTR(-EINVAL);
  250. if (attr->value_size > max_value_size)
  251. return ERR_PTR(-E2BIG);
  252. if (attr->map_flags & ~LOCAL_STORAGE_CREATE_FLAG_MASK ||
  253. !bpf_map_flags_access_ok(attr->map_flags))
  254. return ERR_PTR(-EINVAL);
  255. if (attr->max_entries)
  256. /* max_entries is not used and enforced to be 0 */
  257. return ERR_PTR(-EINVAL);
  258. map = bpf_map_area_alloc(sizeof(struct bpf_cgroup_storage_map), numa_node);
  259. if (!map)
  260. return ERR_PTR(-ENOMEM);
  261. /* copy mandatory map attributes */
  262. bpf_map_init_from_attr(&map->map, attr);
  263. spin_lock_init(&map->lock);
  264. map->root = RB_ROOT;
  265. INIT_LIST_HEAD(&map->list);
  266. return &map->map;
  267. }
  268. static void cgroup_storage_map_free(struct bpf_map *_map)
  269. {
  270. struct bpf_cgroup_storage_map *map = map_to_storage(_map);
  271. struct list_head *storages = &map->list;
  272. struct bpf_cgroup_storage *storage, *stmp;
  273. cgroup_lock();
  274. list_for_each_entry_safe(storage, stmp, storages, list_map) {
  275. bpf_cgroup_storage_unlink(storage);
  276. bpf_cgroup_storage_free(storage);
  277. }
  278. cgroup_unlock();
  279. WARN_ON(!RB_EMPTY_ROOT(&map->root));
  280. WARN_ON(!list_empty(&map->list));
  281. bpf_map_area_free(map);
  282. }
  283. static int cgroup_storage_delete_elem(struct bpf_map *map, void *key)
  284. {
  285. return -EINVAL;
  286. }
  287. static int cgroup_storage_check_btf(const struct bpf_map *map,
  288. const struct btf *btf,
  289. const struct btf_type *key_type,
  290. const struct btf_type *value_type)
  291. {
  292. if (attach_type_isolated(map)) {
  293. struct btf_member *m;
  294. u32 offset, size;
  295. /* Key is expected to be of struct bpf_cgroup_storage_key type,
  296. * which is:
  297. * struct bpf_cgroup_storage_key {
  298. * __u64 cgroup_inode_id;
  299. * __u32 attach_type;
  300. * };
  301. */
  302. /*
  303. * Key_type must be a structure with two fields.
  304. */
  305. if (BTF_INFO_KIND(key_type->info) != BTF_KIND_STRUCT ||
  306. BTF_INFO_VLEN(key_type->info) != 2)
  307. return -EINVAL;
  308. /*
  309. * The first field must be a 64 bit integer at 0 offset.
  310. */
  311. m = (struct btf_member *)(key_type + 1);
  312. size = sizeof_field(struct bpf_cgroup_storage_key, cgroup_inode_id);
  313. if (!btf_member_is_reg_int(btf, key_type, m, 0, size))
  314. return -EINVAL;
  315. /*
  316. * The second field must be a 32 bit integer at 64 bit offset.
  317. */
  318. m++;
  319. offset = offsetof(struct bpf_cgroup_storage_key, attach_type);
  320. size = sizeof_field(struct bpf_cgroup_storage_key, attach_type);
  321. if (!btf_member_is_reg_int(btf, key_type, m, offset, size))
  322. return -EINVAL;
  323. } else {
  324. u32 int_data;
  325. /*
  326. * Key is expected to be u64, which stores the cgroup_inode_id
  327. */
  328. if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
  329. return -EINVAL;
  330. int_data = *(u32 *)(key_type + 1);
  331. if (BTF_INT_BITS(int_data) != 64 || BTF_INT_OFFSET(int_data))
  332. return -EINVAL;
  333. }
  334. return 0;
  335. }
  336. static void cgroup_storage_seq_show_elem(struct bpf_map *map, void *key,
  337. struct seq_file *m)
  338. {
  339. enum bpf_cgroup_storage_type stype;
  340. struct bpf_cgroup_storage *storage;
  341. int cpu;
  342. rcu_read_lock();
  343. storage = cgroup_storage_lookup(map_to_storage(map), key, false);
  344. if (!storage) {
  345. rcu_read_unlock();
  346. return;
  347. }
  348. btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
  349. stype = cgroup_storage_type(map);
  350. if (stype == BPF_CGROUP_STORAGE_SHARED) {
  351. seq_puts(m, ": ");
  352. btf_type_seq_show(map->btf, map->btf_value_type_id,
  353. &READ_ONCE(storage->buf)->data[0], m);
  354. seq_puts(m, "\n");
  355. } else {
  356. seq_puts(m, ": {\n");
  357. for_each_possible_cpu(cpu) {
  358. seq_printf(m, "\tcpu%d: ", cpu);
  359. btf_type_seq_show(map->btf, map->btf_value_type_id,
  360. per_cpu_ptr(storage->percpu_buf, cpu),
  361. m);
  362. seq_puts(m, "\n");
  363. }
  364. seq_puts(m, "}\n");
  365. }
  366. rcu_read_unlock();
  367. }
  368. BTF_ID_LIST_SINGLE(cgroup_storage_map_btf_ids, struct,
  369. bpf_cgroup_storage_map)
  370. const struct bpf_map_ops cgroup_storage_map_ops = {
  371. .map_alloc = cgroup_storage_map_alloc,
  372. .map_free = cgroup_storage_map_free,
  373. .map_get_next_key = cgroup_storage_get_next_key,
  374. .map_lookup_elem = cgroup_storage_lookup_elem,
  375. .map_update_elem = cgroup_storage_update_elem,
  376. .map_delete_elem = cgroup_storage_delete_elem,
  377. .map_check_btf = cgroup_storage_check_btf,
  378. .map_seq_show_elem = cgroup_storage_seq_show_elem,
  379. .map_btf_id = &cgroup_storage_map_btf_ids[0],
  380. };
  381. int bpf_cgroup_storage_assign(struct bpf_prog_aux *aux, struct bpf_map *_map)
  382. {
  383. enum bpf_cgroup_storage_type stype = cgroup_storage_type(_map);
  384. if (aux->cgroup_storage[stype] &&
  385. aux->cgroup_storage[stype] != _map)
  386. return -EBUSY;
  387. aux->cgroup_storage[stype] = _map;
  388. return 0;
  389. }
  390. static size_t bpf_cgroup_storage_calculate_size(struct bpf_map *map, u32 *pages)
  391. {
  392. size_t size;
  393. if (cgroup_storage_type(map) == BPF_CGROUP_STORAGE_SHARED) {
  394. size = sizeof(struct bpf_storage_buffer) + map->value_size;
  395. *pages = round_up(sizeof(struct bpf_cgroup_storage) + size,
  396. PAGE_SIZE) >> PAGE_SHIFT;
  397. } else {
  398. size = map->value_size;
  399. *pages = round_up(round_up(size, 8) * num_possible_cpus(),
  400. PAGE_SIZE) >> PAGE_SHIFT;
  401. }
  402. return size;
  403. }
  404. struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(struct bpf_prog *prog,
  405. enum bpf_cgroup_storage_type stype)
  406. {
  407. const gfp_t gfp = __GFP_ZERO | GFP_USER;
  408. struct bpf_cgroup_storage *storage;
  409. struct bpf_map *map;
  410. size_t size;
  411. u32 pages;
  412. map = prog->aux->cgroup_storage[stype];
  413. if (!map)
  414. return NULL;
  415. size = bpf_cgroup_storage_calculate_size(map, &pages);
  416. storage = bpf_map_kmalloc_node(map, sizeof(struct bpf_cgroup_storage),
  417. gfp, map->numa_node);
  418. if (!storage)
  419. goto enomem;
  420. if (stype == BPF_CGROUP_STORAGE_SHARED) {
  421. storage->buf = bpf_map_kmalloc_node(map, size, gfp,
  422. map->numa_node);
  423. if (!storage->buf)
  424. goto enomem;
  425. check_and_init_map_value(map, storage->buf->data);
  426. } else {
  427. storage->percpu_buf = bpf_map_alloc_percpu(map, size, 8, gfp);
  428. if (!storage->percpu_buf)
  429. goto enomem;
  430. }
  431. storage->map = (struct bpf_cgroup_storage_map *)map;
  432. return storage;
  433. enomem:
  434. kfree(storage);
  435. return ERR_PTR(-ENOMEM);
  436. }
  437. static void free_shared_cgroup_storage_rcu(struct rcu_head *rcu)
  438. {
  439. struct bpf_cgroup_storage *storage =
  440. container_of(rcu, struct bpf_cgroup_storage, rcu);
  441. kfree(storage->buf);
  442. kfree(storage);
  443. }
  444. static void free_percpu_cgroup_storage_rcu(struct rcu_head *rcu)
  445. {
  446. struct bpf_cgroup_storage *storage =
  447. container_of(rcu, struct bpf_cgroup_storage, rcu);
  448. free_percpu(storage->percpu_buf);
  449. kfree(storage);
  450. }
  451. void bpf_cgroup_storage_free(struct bpf_cgroup_storage *storage)
  452. {
  453. enum bpf_cgroup_storage_type stype;
  454. struct bpf_map *map;
  455. if (!storage)
  456. return;
  457. map = &storage->map->map;
  458. stype = cgroup_storage_type(map);
  459. if (stype == BPF_CGROUP_STORAGE_SHARED)
  460. call_rcu(&storage->rcu, free_shared_cgroup_storage_rcu);
  461. else
  462. call_rcu(&storage->rcu, free_percpu_cgroup_storage_rcu);
  463. }
  464. void bpf_cgroup_storage_link(struct bpf_cgroup_storage *storage,
  465. struct cgroup *cgroup,
  466. enum bpf_attach_type type)
  467. {
  468. struct bpf_cgroup_storage_map *map;
  469. if (!storage)
  470. return;
  471. storage->key.attach_type = type;
  472. storage->key.cgroup_inode_id = cgroup_id(cgroup);
  473. map = storage->map;
  474. spin_lock_bh(&map->lock);
  475. WARN_ON(cgroup_storage_insert(map, storage));
  476. list_add(&storage->list_map, &map->list);
  477. list_add(&storage->list_cg, &cgroup->bpf.storages);
  478. spin_unlock_bh(&map->lock);
  479. }
  480. void bpf_cgroup_storage_unlink(struct bpf_cgroup_storage *storage)
  481. {
  482. struct bpf_cgroup_storage_map *map;
  483. struct rb_root *root;
  484. if (!storage)
  485. return;
  486. map = storage->map;
  487. spin_lock_bh(&map->lock);
  488. root = &map->root;
  489. rb_erase(&storage->node, root);
  490. list_del(&storage->list_map);
  491. list_del(&storage->list_cg);
  492. spin_unlock_bh(&map->lock);
  493. }
  494. #endif