arraymap.c 8.3 KB

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