arraymap.c 8.3 KB

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