arraymap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  2. * Copyright (c) 2016,2017 Facebook
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #include <linux/bpf.h>
  14. #include <linux/err.h>
  15. #include <linux/slab.h>
  16. #include <linux/mm.h>
  17. #include <linux/filter.h>
  18. #include <linux/perf_event.h>
  19. #include "map_in_map.h"
  20. static void bpf_array_free_percpu(struct bpf_array *array)
  21. {
  22. int i;
  23. for (i = 0; i < array->map.max_entries; i++)
  24. free_percpu(array->pptrs[i]);
  25. }
  26. static int bpf_array_alloc_percpu(struct bpf_array *array)
  27. {
  28. void __percpu *ptr;
  29. int i;
  30. for (i = 0; i < array->map.max_entries; i++) {
  31. ptr = __alloc_percpu_gfp(array->elem_size, 8,
  32. GFP_USER | __GFP_NOWARN);
  33. if (!ptr) {
  34. bpf_array_free_percpu(array);
  35. return -ENOMEM;
  36. }
  37. array->pptrs[i] = ptr;
  38. }
  39. return 0;
  40. }
  41. /* Called from syscall */
  42. static struct bpf_map *array_map_alloc(union bpf_attr *attr)
  43. {
  44. bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
  45. struct bpf_array *array;
  46. u64 array_size;
  47. u32 elem_size;
  48. /* check sanity of attributes */
  49. if (attr->max_entries == 0 || attr->key_size != 4 ||
  50. attr->value_size == 0 || attr->map_flags)
  51. return ERR_PTR(-EINVAL);
  52. if (attr->value_size > KMALLOC_MAX_SIZE)
  53. /* if value_size is bigger, the user space won't be able to
  54. * access the elements.
  55. */
  56. return ERR_PTR(-E2BIG);
  57. elem_size = round_up(attr->value_size, 8);
  58. array_size = sizeof(*array);
  59. if (percpu)
  60. array_size += (u64) attr->max_entries * sizeof(void *);
  61. else
  62. array_size += (u64) attr->max_entries * elem_size;
  63. /* make sure there is no u32 overflow later in round_up() */
  64. if (array_size >= U32_MAX - PAGE_SIZE)
  65. return ERR_PTR(-ENOMEM);
  66. /* allocate all map elements and zero-initialize them */
  67. array = bpf_map_area_alloc(array_size);
  68. if (!array)
  69. return ERR_PTR(-ENOMEM);
  70. /* copy mandatory map attributes */
  71. array->map.map_type = attr->map_type;
  72. array->map.key_size = attr->key_size;
  73. array->map.value_size = attr->value_size;
  74. array->map.max_entries = attr->max_entries;
  75. array->elem_size = elem_size;
  76. if (!percpu)
  77. goto out;
  78. array_size += (u64) attr->max_entries * elem_size * num_possible_cpus();
  79. if (array_size >= U32_MAX - PAGE_SIZE ||
  80. elem_size > PCPU_MIN_UNIT_SIZE || bpf_array_alloc_percpu(array)) {
  81. bpf_map_area_free(array);
  82. return ERR_PTR(-ENOMEM);
  83. }
  84. out:
  85. array->map.pages = round_up(array_size, PAGE_SIZE) >> PAGE_SHIFT;
  86. return &array->map;
  87. }
  88. /* Called from syscall or from eBPF program */
  89. static void *array_map_lookup_elem(struct bpf_map *map, void *key)
  90. {
  91. struct bpf_array *array = container_of(map, struct bpf_array, map);
  92. u32 index = *(u32 *)key;
  93. if (unlikely(index >= array->map.max_entries))
  94. return NULL;
  95. return array->value + array->elem_size * index;
  96. }
  97. /* emit BPF instructions equivalent to C code of array_map_lookup_elem() */
  98. static u32 array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
  99. {
  100. struct bpf_insn *insn = insn_buf;
  101. u32 elem_size = round_up(map->value_size, 8);
  102. const int ret = BPF_REG_0;
  103. const int map_ptr = BPF_REG_1;
  104. const int index = BPF_REG_2;
  105. *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
  106. *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
  107. *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 3);
  108. if (is_power_of_2(elem_size)) {
  109. *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
  110. } else {
  111. *insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
  112. }
  113. *insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
  114. *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
  115. *insn++ = BPF_MOV64_IMM(ret, 0);
  116. return insn - insn_buf;
  117. }
  118. /* Called from eBPF program */
  119. static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
  120. {
  121. struct bpf_array *array = container_of(map, struct bpf_array, map);
  122. u32 index = *(u32 *)key;
  123. if (unlikely(index >= array->map.max_entries))
  124. return NULL;
  125. return this_cpu_ptr(array->pptrs[index]);
  126. }
  127. int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
  128. {
  129. struct bpf_array *array = container_of(map, struct bpf_array, map);
  130. u32 index = *(u32 *)key;
  131. void __percpu *pptr;
  132. int cpu, off = 0;
  133. u32 size;
  134. if (unlikely(index >= array->map.max_entries))
  135. return -ENOENT;
  136. /* per_cpu areas are zero-filled and bpf programs can only
  137. * access 'value_size' of them, so copying rounded areas
  138. * will not leak any kernel data
  139. */
  140. size = round_up(map->value_size, 8);
  141. rcu_read_lock();
  142. pptr = array->pptrs[index];
  143. for_each_possible_cpu(cpu) {
  144. bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size);
  145. off += size;
  146. }
  147. rcu_read_unlock();
  148. return 0;
  149. }
  150. /* Called from syscall */
  151. static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  152. {
  153. struct bpf_array *array = container_of(map, struct bpf_array, map);
  154. u32 index = key ? *(u32 *)key : U32_MAX;
  155. u32 *next = (u32 *)next_key;
  156. if (index >= array->map.max_entries) {
  157. *next = 0;
  158. return 0;
  159. }
  160. if (index == array->map.max_entries - 1)
  161. return -ENOENT;
  162. *next = index + 1;
  163. return 0;
  164. }
  165. /* Called from syscall or from eBPF program */
  166. static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
  167. u64 map_flags)
  168. {
  169. struct bpf_array *array = container_of(map, struct bpf_array, map);
  170. u32 index = *(u32 *)key;
  171. if (unlikely(map_flags > BPF_EXIST))
  172. /* unknown flags */
  173. return -EINVAL;
  174. if (unlikely(index >= array->map.max_entries))
  175. /* all elements were pre-allocated, cannot insert a new one */
  176. return -E2BIG;
  177. if (unlikely(map_flags == BPF_NOEXIST))
  178. /* all elements already exist */
  179. return -EEXIST;
  180. if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  181. memcpy(this_cpu_ptr(array->pptrs[index]),
  182. value, map->value_size);
  183. else
  184. memcpy(array->value + array->elem_size * index,
  185. value, map->value_size);
  186. return 0;
  187. }
  188. int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
  189. u64 map_flags)
  190. {
  191. struct bpf_array *array = container_of(map, struct bpf_array, map);
  192. u32 index = *(u32 *)key;
  193. void __percpu *pptr;
  194. int cpu, off = 0;
  195. u32 size;
  196. if (unlikely(map_flags > BPF_EXIST))
  197. /* unknown flags */
  198. return -EINVAL;
  199. if (unlikely(index >= array->map.max_entries))
  200. /* all elements were pre-allocated, cannot insert a new one */
  201. return -E2BIG;
  202. if (unlikely(map_flags == BPF_NOEXIST))
  203. /* all elements already exist */
  204. return -EEXIST;
  205. /* the user space will provide round_up(value_size, 8) bytes that
  206. * will be copied into per-cpu area. bpf programs can only access
  207. * value_size of it. During lookup the same extra bytes will be
  208. * returned or zeros which were zero-filled by percpu_alloc,
  209. * so no kernel data leaks possible
  210. */
  211. size = round_up(map->value_size, 8);
  212. rcu_read_lock();
  213. pptr = array->pptrs[index];
  214. for_each_possible_cpu(cpu) {
  215. bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size);
  216. off += size;
  217. }
  218. rcu_read_unlock();
  219. return 0;
  220. }
  221. /* Called from syscall or from eBPF program */
  222. static int array_map_delete_elem(struct bpf_map *map, void *key)
  223. {
  224. return -EINVAL;
  225. }
  226. /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  227. static void array_map_free(struct bpf_map *map)
  228. {
  229. struct bpf_array *array = container_of(map, struct bpf_array, map);
  230. /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
  231. * so the programs (can be more than one that used this map) were
  232. * disconnected from events. Wait for outstanding programs to complete
  233. * and free the array
  234. */
  235. synchronize_rcu();
  236. if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  237. bpf_array_free_percpu(array);
  238. bpf_map_area_free(array);
  239. }
  240. const struct bpf_map_ops array_map_ops = {
  241. .map_alloc = array_map_alloc,
  242. .map_free = array_map_free,
  243. .map_get_next_key = array_map_get_next_key,
  244. .map_lookup_elem = array_map_lookup_elem,
  245. .map_update_elem = array_map_update_elem,
  246. .map_delete_elem = array_map_delete_elem,
  247. .map_gen_lookup = array_map_gen_lookup,
  248. };
  249. const struct bpf_map_ops percpu_array_map_ops = {
  250. .map_alloc = array_map_alloc,
  251. .map_free = array_map_free,
  252. .map_get_next_key = array_map_get_next_key,
  253. .map_lookup_elem = percpu_array_map_lookup_elem,
  254. .map_update_elem = array_map_update_elem,
  255. .map_delete_elem = array_map_delete_elem,
  256. };
  257. static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr)
  258. {
  259. /* only file descriptors can be stored in this type of map */
  260. if (attr->value_size != sizeof(u32))
  261. return ERR_PTR(-EINVAL);
  262. return array_map_alloc(attr);
  263. }
  264. static void fd_array_map_free(struct bpf_map *map)
  265. {
  266. struct bpf_array *array = container_of(map, struct bpf_array, map);
  267. int i;
  268. synchronize_rcu();
  269. /* make sure it's empty */
  270. for (i = 0; i < array->map.max_entries; i++)
  271. BUG_ON(array->ptrs[i] != NULL);
  272. bpf_map_area_free(array);
  273. }
  274. static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
  275. {
  276. return NULL;
  277. }
  278. /* only called from syscall */
  279. int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
  280. void *key, void *value, u64 map_flags)
  281. {
  282. struct bpf_array *array = container_of(map, struct bpf_array, map);
  283. void *new_ptr, *old_ptr;
  284. u32 index = *(u32 *)key, ufd;
  285. if (map_flags != BPF_ANY)
  286. return -EINVAL;
  287. if (index >= array->map.max_entries)
  288. return -E2BIG;
  289. ufd = *(u32 *)value;
  290. new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
  291. if (IS_ERR(new_ptr))
  292. return PTR_ERR(new_ptr);
  293. old_ptr = xchg(array->ptrs + index, new_ptr);
  294. if (old_ptr)
  295. map->ops->map_fd_put_ptr(old_ptr);
  296. return 0;
  297. }
  298. static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
  299. {
  300. struct bpf_array *array = container_of(map, struct bpf_array, map);
  301. void *old_ptr;
  302. u32 index = *(u32 *)key;
  303. if (index >= array->map.max_entries)
  304. return -E2BIG;
  305. old_ptr = xchg(array->ptrs + index, NULL);
  306. if (old_ptr) {
  307. map->ops->map_fd_put_ptr(old_ptr);
  308. return 0;
  309. } else {
  310. return -ENOENT;
  311. }
  312. }
  313. static void *prog_fd_array_get_ptr(struct bpf_map *map,
  314. struct file *map_file, int fd)
  315. {
  316. struct bpf_array *array = container_of(map, struct bpf_array, map);
  317. struct bpf_prog *prog = bpf_prog_get(fd);
  318. if (IS_ERR(prog))
  319. return prog;
  320. if (!bpf_prog_array_compatible(array, prog)) {
  321. bpf_prog_put(prog);
  322. return ERR_PTR(-EINVAL);
  323. }
  324. return prog;
  325. }
  326. static void prog_fd_array_put_ptr(void *ptr)
  327. {
  328. bpf_prog_put(ptr);
  329. }
  330. /* decrement refcnt of all bpf_progs that are stored in this map */
  331. void bpf_fd_array_map_clear(struct bpf_map *map)
  332. {
  333. struct bpf_array *array = container_of(map, struct bpf_array, map);
  334. int i;
  335. for (i = 0; i < array->map.max_entries; i++)
  336. fd_array_map_delete_elem(map, &i);
  337. }
  338. const struct bpf_map_ops prog_array_map_ops = {
  339. .map_alloc = fd_array_map_alloc,
  340. .map_free = fd_array_map_free,
  341. .map_get_next_key = array_map_get_next_key,
  342. .map_lookup_elem = fd_array_map_lookup_elem,
  343. .map_delete_elem = fd_array_map_delete_elem,
  344. .map_fd_get_ptr = prog_fd_array_get_ptr,
  345. .map_fd_put_ptr = prog_fd_array_put_ptr,
  346. };
  347. static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
  348. struct file *map_file)
  349. {
  350. struct bpf_event_entry *ee;
  351. ee = kzalloc(sizeof(*ee), GFP_ATOMIC);
  352. if (ee) {
  353. ee->event = perf_file->private_data;
  354. ee->perf_file = perf_file;
  355. ee->map_file = map_file;
  356. }
  357. return ee;
  358. }
  359. static void __bpf_event_entry_free(struct rcu_head *rcu)
  360. {
  361. struct bpf_event_entry *ee;
  362. ee = container_of(rcu, struct bpf_event_entry, rcu);
  363. fput(ee->perf_file);
  364. kfree(ee);
  365. }
  366. static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
  367. {
  368. call_rcu(&ee->rcu, __bpf_event_entry_free);
  369. }
  370. static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
  371. struct file *map_file, int fd)
  372. {
  373. const struct perf_event_attr *attr;
  374. struct bpf_event_entry *ee;
  375. struct perf_event *event;
  376. struct file *perf_file;
  377. perf_file = perf_event_get(fd);
  378. if (IS_ERR(perf_file))
  379. return perf_file;
  380. event = perf_file->private_data;
  381. ee = ERR_PTR(-EINVAL);
  382. attr = perf_event_attrs(event);
  383. if (IS_ERR(attr) || attr->inherit)
  384. goto err_out;
  385. switch (attr->type) {
  386. case PERF_TYPE_SOFTWARE:
  387. if (attr->config != PERF_COUNT_SW_BPF_OUTPUT)
  388. goto err_out;
  389. /* fall-through */
  390. case PERF_TYPE_RAW:
  391. case PERF_TYPE_HARDWARE:
  392. ee = bpf_event_entry_gen(perf_file, map_file);
  393. if (ee)
  394. return ee;
  395. ee = ERR_PTR(-ENOMEM);
  396. /* fall-through */
  397. default:
  398. break;
  399. }
  400. err_out:
  401. fput(perf_file);
  402. return ee;
  403. }
  404. static void perf_event_fd_array_put_ptr(void *ptr)
  405. {
  406. bpf_event_entry_free_rcu(ptr);
  407. }
  408. static void perf_event_fd_array_release(struct bpf_map *map,
  409. struct file *map_file)
  410. {
  411. struct bpf_array *array = container_of(map, struct bpf_array, map);
  412. struct bpf_event_entry *ee;
  413. int i;
  414. rcu_read_lock();
  415. for (i = 0; i < array->map.max_entries; i++) {
  416. ee = READ_ONCE(array->ptrs[i]);
  417. if (ee && ee->map_file == map_file)
  418. fd_array_map_delete_elem(map, &i);
  419. }
  420. rcu_read_unlock();
  421. }
  422. const struct bpf_map_ops perf_event_array_map_ops = {
  423. .map_alloc = fd_array_map_alloc,
  424. .map_free = fd_array_map_free,
  425. .map_get_next_key = array_map_get_next_key,
  426. .map_lookup_elem = fd_array_map_lookup_elem,
  427. .map_delete_elem = fd_array_map_delete_elem,
  428. .map_fd_get_ptr = perf_event_fd_array_get_ptr,
  429. .map_fd_put_ptr = perf_event_fd_array_put_ptr,
  430. .map_release = perf_event_fd_array_release,
  431. };
  432. #ifdef CONFIG_CGROUPS
  433. static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
  434. struct file *map_file /* not used */,
  435. int fd)
  436. {
  437. return cgroup_get_from_fd(fd);
  438. }
  439. static void cgroup_fd_array_put_ptr(void *ptr)
  440. {
  441. /* cgroup_put free cgrp after a rcu grace period */
  442. cgroup_put(ptr);
  443. }
  444. static void cgroup_fd_array_free(struct bpf_map *map)
  445. {
  446. bpf_fd_array_map_clear(map);
  447. fd_array_map_free(map);
  448. }
  449. const struct bpf_map_ops cgroup_array_map_ops = {
  450. .map_alloc = fd_array_map_alloc,
  451. .map_free = cgroup_fd_array_free,
  452. .map_get_next_key = array_map_get_next_key,
  453. .map_lookup_elem = fd_array_map_lookup_elem,
  454. .map_delete_elem = fd_array_map_delete_elem,
  455. .map_fd_get_ptr = cgroup_fd_array_get_ptr,
  456. .map_fd_put_ptr = cgroup_fd_array_put_ptr,
  457. };
  458. #endif
  459. static struct bpf_map *array_of_map_alloc(union bpf_attr *attr)
  460. {
  461. struct bpf_map *map, *inner_map_meta;
  462. inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
  463. if (IS_ERR(inner_map_meta))
  464. return inner_map_meta;
  465. map = fd_array_map_alloc(attr);
  466. if (IS_ERR(map)) {
  467. bpf_map_meta_free(inner_map_meta);
  468. return map;
  469. }
  470. map->inner_map_meta = inner_map_meta;
  471. return map;
  472. }
  473. static void array_of_map_free(struct bpf_map *map)
  474. {
  475. /* map->inner_map_meta is only accessed by syscall which
  476. * is protected by fdget/fdput.
  477. */
  478. bpf_map_meta_free(map->inner_map_meta);
  479. bpf_fd_array_map_clear(map);
  480. fd_array_map_free(map);
  481. }
  482. static void *array_of_map_lookup_elem(struct bpf_map *map, void *key)
  483. {
  484. struct bpf_map **inner_map = array_map_lookup_elem(map, key);
  485. if (!inner_map)
  486. return NULL;
  487. return READ_ONCE(*inner_map);
  488. }
  489. const struct bpf_map_ops array_of_maps_map_ops = {
  490. .map_alloc = array_of_map_alloc,
  491. .map_free = array_of_map_free,
  492. .map_get_next_key = array_map_get_next_key,
  493. .map_lookup_elem = array_of_map_lookup_elem,
  494. .map_delete_elem = fd_array_map_delete_elem,
  495. .map_fd_get_ptr = bpf_map_fd_get_ptr,
  496. .map_fd_put_ptr = bpf_map_fd_put_ptr,
  497. };