arraymap.c 16 KB

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