arraymap.c 14 KB

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