arraymap.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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/vmalloc.h>
  15. #include <linux/slab.h>
  16. #include <linux/mm.h>
  17. #include <linux/filter.h>
  18. #include <linux/perf_event.h>
  19. /* Called from syscall */
  20. static struct bpf_map *array_map_alloc(union bpf_attr *attr)
  21. {
  22. struct bpf_array *array;
  23. u32 elem_size, array_size;
  24. /* check sanity of attributes */
  25. if (attr->max_entries == 0 || attr->key_size != 4 ||
  26. attr->value_size == 0)
  27. return ERR_PTR(-EINVAL);
  28. elem_size = round_up(attr->value_size, 8);
  29. /* check round_up into zero and u32 overflow */
  30. if (elem_size == 0 ||
  31. attr->max_entries > (U32_MAX - sizeof(*array)) / elem_size)
  32. return ERR_PTR(-ENOMEM);
  33. array_size = sizeof(*array) + attr->max_entries * elem_size;
  34. /* allocate all map elements and zero-initialize them */
  35. array = kzalloc(array_size, GFP_USER | __GFP_NOWARN);
  36. if (!array) {
  37. array = vzalloc(array_size);
  38. if (!array)
  39. return ERR_PTR(-ENOMEM);
  40. }
  41. /* copy mandatory map attributes */
  42. array->map.key_size = attr->key_size;
  43. array->map.value_size = attr->value_size;
  44. array->map.max_entries = attr->max_entries;
  45. array->map.pages = round_up(array_size, PAGE_SIZE) >> PAGE_SHIFT;
  46. array->elem_size = elem_size;
  47. return &array->map;
  48. }
  49. /* Called from syscall or from eBPF program */
  50. static void *array_map_lookup_elem(struct bpf_map *map, void *key)
  51. {
  52. struct bpf_array *array = container_of(map, struct bpf_array, map);
  53. u32 index = *(u32 *)key;
  54. if (index >= array->map.max_entries)
  55. return NULL;
  56. return array->value + array->elem_size * index;
  57. }
  58. /* Called from syscall */
  59. static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  60. {
  61. struct bpf_array *array = container_of(map, struct bpf_array, map);
  62. u32 index = *(u32 *)key;
  63. u32 *next = (u32 *)next_key;
  64. if (index >= array->map.max_entries) {
  65. *next = 0;
  66. return 0;
  67. }
  68. if (index == array->map.max_entries - 1)
  69. return -ENOENT;
  70. *next = index + 1;
  71. return 0;
  72. }
  73. /* Called from syscall or from eBPF program */
  74. static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
  75. u64 map_flags)
  76. {
  77. struct bpf_array *array = container_of(map, struct bpf_array, map);
  78. u32 index = *(u32 *)key;
  79. if (map_flags > BPF_EXIST)
  80. /* unknown flags */
  81. return -EINVAL;
  82. if (index >= array->map.max_entries)
  83. /* all elements were pre-allocated, cannot insert a new one */
  84. return -E2BIG;
  85. if (map_flags == BPF_NOEXIST)
  86. /* all elements already exist */
  87. return -EEXIST;
  88. memcpy(array->value + array->elem_size * index, value, array->elem_size);
  89. return 0;
  90. }
  91. /* Called from syscall or from eBPF program */
  92. static int array_map_delete_elem(struct bpf_map *map, void *key)
  93. {
  94. return -EINVAL;
  95. }
  96. /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  97. static void array_map_free(struct bpf_map *map)
  98. {
  99. struct bpf_array *array = container_of(map, struct bpf_array, map);
  100. /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
  101. * so the programs (can be more than one that used this map) were
  102. * disconnected from events. Wait for outstanding programs to complete
  103. * and free the array
  104. */
  105. synchronize_rcu();
  106. kvfree(array);
  107. }
  108. static const struct bpf_map_ops array_ops = {
  109. .map_alloc = array_map_alloc,
  110. .map_free = array_map_free,
  111. .map_get_next_key = array_map_get_next_key,
  112. .map_lookup_elem = array_map_lookup_elem,
  113. .map_update_elem = array_map_update_elem,
  114. .map_delete_elem = array_map_delete_elem,
  115. };
  116. static struct bpf_map_type_list array_type __read_mostly = {
  117. .ops = &array_ops,
  118. .type = BPF_MAP_TYPE_ARRAY,
  119. };
  120. static int __init register_array_map(void)
  121. {
  122. bpf_register_map_type(&array_type);
  123. return 0;
  124. }
  125. late_initcall(register_array_map);
  126. static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr)
  127. {
  128. /* only file descriptors can be stored in this type of map */
  129. if (attr->value_size != sizeof(u32))
  130. return ERR_PTR(-EINVAL);
  131. return array_map_alloc(attr);
  132. }
  133. static void fd_array_map_free(struct bpf_map *map)
  134. {
  135. struct bpf_array *array = container_of(map, struct bpf_array, map);
  136. int i;
  137. synchronize_rcu();
  138. /* make sure it's empty */
  139. for (i = 0; i < array->map.max_entries; i++)
  140. BUG_ON(array->ptrs[i] != NULL);
  141. kvfree(array);
  142. }
  143. static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
  144. {
  145. return NULL;
  146. }
  147. /* only called from syscall */
  148. static int fd_array_map_update_elem(struct bpf_map *map, void *key,
  149. void *value, u64 map_flags)
  150. {
  151. struct bpf_array *array = container_of(map, struct bpf_array, map);
  152. void *new_ptr, *old_ptr;
  153. u32 index = *(u32 *)key, ufd;
  154. if (map_flags != BPF_ANY)
  155. return -EINVAL;
  156. if (index >= array->map.max_entries)
  157. return -E2BIG;
  158. ufd = *(u32 *)value;
  159. new_ptr = map->ops->map_fd_get_ptr(map, ufd);
  160. if (IS_ERR(new_ptr))
  161. return PTR_ERR(new_ptr);
  162. old_ptr = xchg(array->ptrs + index, new_ptr);
  163. if (old_ptr)
  164. map->ops->map_fd_put_ptr(old_ptr);
  165. return 0;
  166. }
  167. static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
  168. {
  169. struct bpf_array *array = container_of(map, struct bpf_array, map);
  170. void *old_ptr;
  171. u32 index = *(u32 *)key;
  172. if (index >= array->map.max_entries)
  173. return -E2BIG;
  174. old_ptr = xchg(array->ptrs + index, NULL);
  175. if (old_ptr) {
  176. map->ops->map_fd_put_ptr(old_ptr);
  177. return 0;
  178. } else {
  179. return -ENOENT;
  180. }
  181. }
  182. static void *prog_fd_array_get_ptr(struct bpf_map *map, int fd)
  183. {
  184. struct bpf_array *array = container_of(map, struct bpf_array, map);
  185. struct bpf_prog *prog = bpf_prog_get(fd);
  186. if (IS_ERR(prog))
  187. return prog;
  188. if (!bpf_prog_array_compatible(array, prog)) {
  189. bpf_prog_put(prog);
  190. return ERR_PTR(-EINVAL);
  191. }
  192. return prog;
  193. }
  194. static void prog_fd_array_put_ptr(void *ptr)
  195. {
  196. struct bpf_prog *prog = ptr;
  197. bpf_prog_put_rcu(prog);
  198. }
  199. /* decrement refcnt of all bpf_progs that are stored in this map */
  200. void bpf_fd_array_map_clear(struct bpf_map *map)
  201. {
  202. struct bpf_array *array = container_of(map, struct bpf_array, map);
  203. int i;
  204. for (i = 0; i < array->map.max_entries; i++)
  205. fd_array_map_delete_elem(map, &i);
  206. }
  207. static const struct bpf_map_ops prog_array_ops = {
  208. .map_alloc = fd_array_map_alloc,
  209. .map_free = fd_array_map_free,
  210. .map_get_next_key = array_map_get_next_key,
  211. .map_lookup_elem = fd_array_map_lookup_elem,
  212. .map_update_elem = fd_array_map_update_elem,
  213. .map_delete_elem = fd_array_map_delete_elem,
  214. .map_fd_get_ptr = prog_fd_array_get_ptr,
  215. .map_fd_put_ptr = prog_fd_array_put_ptr,
  216. };
  217. static struct bpf_map_type_list prog_array_type __read_mostly = {
  218. .ops = &prog_array_ops,
  219. .type = BPF_MAP_TYPE_PROG_ARRAY,
  220. };
  221. static int __init register_prog_array_map(void)
  222. {
  223. bpf_register_map_type(&prog_array_type);
  224. return 0;
  225. }
  226. late_initcall(register_prog_array_map);
  227. static void perf_event_array_map_free(struct bpf_map *map)
  228. {
  229. bpf_fd_array_map_clear(map);
  230. fd_array_map_free(map);
  231. }
  232. static void *perf_event_fd_array_get_ptr(struct bpf_map *map, int fd)
  233. {
  234. struct perf_event *event;
  235. const struct perf_event_attr *attr;
  236. event = perf_event_get(fd);
  237. if (IS_ERR(event))
  238. return event;
  239. attr = perf_event_attrs(event);
  240. if (IS_ERR(attr))
  241. return (void *)attr;
  242. if (attr->type != PERF_TYPE_RAW &&
  243. !(attr->type == PERF_TYPE_SOFTWARE &&
  244. attr->config == PERF_COUNT_SW_BPF_OUTPUT) &&
  245. attr->type != PERF_TYPE_HARDWARE) {
  246. perf_event_release_kernel(event);
  247. return ERR_PTR(-EINVAL);
  248. }
  249. return event;
  250. }
  251. static void perf_event_fd_array_put_ptr(void *ptr)
  252. {
  253. struct perf_event *event = ptr;
  254. perf_event_release_kernel(event);
  255. }
  256. static const struct bpf_map_ops perf_event_array_ops = {
  257. .map_alloc = fd_array_map_alloc,
  258. .map_free = perf_event_array_map_free,
  259. .map_get_next_key = array_map_get_next_key,
  260. .map_lookup_elem = fd_array_map_lookup_elem,
  261. .map_update_elem = fd_array_map_update_elem,
  262. .map_delete_elem = fd_array_map_delete_elem,
  263. .map_fd_get_ptr = perf_event_fd_array_get_ptr,
  264. .map_fd_put_ptr = perf_event_fd_array_put_ptr,
  265. };
  266. static struct bpf_map_type_list perf_event_array_type __read_mostly = {
  267. .ops = &perf_event_array_ops,
  268. .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
  269. };
  270. static int __init register_perf_event_array_map(void)
  271. {
  272. bpf_register_map_type(&perf_event_array_type);
  273. return 0;
  274. }
  275. late_initcall(register_perf_event_array_map);