arraymap.c 9.4 KB

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