arraymap.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 *prog_array_map_alloc(union bpf_attr *attr)
  125. {
  126. /* only bpf_prog file descriptors can be stored in prog_array map */
  127. if (attr->value_size != sizeof(u32))
  128. return ERR_PTR(-EINVAL);
  129. return array_map_alloc(attr);
  130. }
  131. static void prog_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->prog[i] != NULL);
  139. kvfree(array);
  140. }
  141. static void *prog_array_map_lookup_elem(struct bpf_map *map, void *key)
  142. {
  143. return NULL;
  144. }
  145. /* only called from syscall */
  146. static int prog_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. struct bpf_prog *prog, *old_prog;
  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. prog = bpf_prog_get(ufd);
  158. if (IS_ERR(prog))
  159. return PTR_ERR(prog);
  160. if (!bpf_prog_array_compatible(array, prog)) {
  161. bpf_prog_put(prog);
  162. return -EINVAL;
  163. }
  164. old_prog = xchg(array->prog + index, prog);
  165. if (old_prog)
  166. bpf_prog_put_rcu(old_prog);
  167. return 0;
  168. }
  169. static int prog_array_map_delete_elem(struct bpf_map *map, void *key)
  170. {
  171. struct bpf_array *array = container_of(map, struct bpf_array, map);
  172. struct bpf_prog *old_prog;
  173. u32 index = *(u32 *)key;
  174. if (index >= array->map.max_entries)
  175. return -E2BIG;
  176. old_prog = xchg(array->prog + index, NULL);
  177. if (old_prog) {
  178. bpf_prog_put_rcu(old_prog);
  179. return 0;
  180. } else {
  181. return -ENOENT;
  182. }
  183. }
  184. /* decrement refcnt of all bpf_progs that are stored in this map */
  185. void bpf_prog_array_map_clear(struct bpf_map *map)
  186. {
  187. struct bpf_array *array = container_of(map, struct bpf_array, map);
  188. int i;
  189. for (i = 0; i < array->map.max_entries; i++)
  190. prog_array_map_delete_elem(map, &i);
  191. }
  192. static const struct bpf_map_ops prog_array_ops = {
  193. .map_alloc = prog_array_map_alloc,
  194. .map_free = prog_array_map_free,
  195. .map_get_next_key = array_map_get_next_key,
  196. .map_lookup_elem = prog_array_map_lookup_elem,
  197. .map_update_elem = prog_array_map_update_elem,
  198. .map_delete_elem = prog_array_map_delete_elem,
  199. };
  200. static struct bpf_map_type_list prog_array_type __read_mostly = {
  201. .ops = &prog_array_ops,
  202. .type = BPF_MAP_TYPE_PROG_ARRAY,
  203. };
  204. static int __init register_prog_array_map(void)
  205. {
  206. bpf_register_map_type(&prog_array_type);
  207. return 0;
  208. }
  209. late_initcall(register_prog_array_map);