queue_stack_maps.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * queue_stack_maps.c: BPF queue and stack maps
  4. *
  5. * Copyright (c) 2018 Politecnico di Torino
  6. */
  7. #include <linux/bpf.h>
  8. #include <linux/list.h>
  9. #include <linux/slab.h>
  10. #include <linux/btf_ids.h>
  11. #include "percpu_freelist.h"
  12. #define QUEUE_STACK_CREATE_FLAG_MASK \
  13. (BPF_F_NUMA_NODE | BPF_F_ACCESS_MASK)
  14. struct bpf_queue_stack {
  15. struct bpf_map map;
  16. raw_spinlock_t lock;
  17. u32 head, tail;
  18. u32 size; /* max_entries + 1 */
  19. char elements[] __aligned(8);
  20. };
  21. static struct bpf_queue_stack *bpf_queue_stack(struct bpf_map *map)
  22. {
  23. return container_of(map, struct bpf_queue_stack, map);
  24. }
  25. static bool queue_stack_map_is_empty(struct bpf_queue_stack *qs)
  26. {
  27. return qs->head == qs->tail;
  28. }
  29. static bool queue_stack_map_is_full(struct bpf_queue_stack *qs)
  30. {
  31. u32 head = qs->head + 1;
  32. if (unlikely(head >= qs->size))
  33. head = 0;
  34. return head == qs->tail;
  35. }
  36. /* Called from syscall */
  37. static int queue_stack_map_alloc_check(union bpf_attr *attr)
  38. {
  39. /* check sanity of attributes */
  40. if (attr->max_entries == 0 || attr->key_size != 0 ||
  41. attr->value_size == 0 ||
  42. attr->map_flags & ~QUEUE_STACK_CREATE_FLAG_MASK ||
  43. !bpf_map_flags_access_ok(attr->map_flags))
  44. return -EINVAL;
  45. if (attr->value_size > KMALLOC_MAX_SIZE)
  46. /* if value_size is bigger, the user space won't be able to
  47. * access the elements.
  48. */
  49. return -E2BIG;
  50. return 0;
  51. }
  52. static struct bpf_map *queue_stack_map_alloc(union bpf_attr *attr)
  53. {
  54. int numa_node = bpf_map_attr_numa_node(attr);
  55. struct bpf_queue_stack *qs;
  56. u64 size, queue_size;
  57. size = (u64) attr->max_entries + 1;
  58. queue_size = sizeof(*qs) + size * attr->value_size;
  59. qs = bpf_map_area_alloc(queue_size, numa_node);
  60. if (!qs)
  61. return ERR_PTR(-ENOMEM);
  62. bpf_map_init_from_attr(&qs->map, attr);
  63. qs->size = size;
  64. raw_spin_lock_init(&qs->lock);
  65. return &qs->map;
  66. }
  67. /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  68. static void queue_stack_map_free(struct bpf_map *map)
  69. {
  70. struct bpf_queue_stack *qs = bpf_queue_stack(map);
  71. bpf_map_area_free(qs);
  72. }
  73. static long __queue_map_get(struct bpf_map *map, void *value, bool delete)
  74. {
  75. struct bpf_queue_stack *qs = bpf_queue_stack(map);
  76. unsigned long flags;
  77. int err = 0;
  78. void *ptr;
  79. if (in_nmi()) {
  80. if (!raw_spin_trylock_irqsave(&qs->lock, flags))
  81. return -EBUSY;
  82. } else {
  83. raw_spin_lock_irqsave(&qs->lock, flags);
  84. }
  85. if (queue_stack_map_is_empty(qs)) {
  86. memset(value, 0, qs->map.value_size);
  87. err = -ENOENT;
  88. goto out;
  89. }
  90. ptr = &qs->elements[qs->tail * qs->map.value_size];
  91. memcpy(value, ptr, qs->map.value_size);
  92. if (delete) {
  93. if (unlikely(++qs->tail >= qs->size))
  94. qs->tail = 0;
  95. }
  96. out:
  97. raw_spin_unlock_irqrestore(&qs->lock, flags);
  98. return err;
  99. }
  100. static long __stack_map_get(struct bpf_map *map, void *value, bool delete)
  101. {
  102. struct bpf_queue_stack *qs = bpf_queue_stack(map);
  103. unsigned long flags;
  104. int err = 0;
  105. void *ptr;
  106. u32 index;
  107. if (in_nmi()) {
  108. if (!raw_spin_trylock_irqsave(&qs->lock, flags))
  109. return -EBUSY;
  110. } else {
  111. raw_spin_lock_irqsave(&qs->lock, flags);
  112. }
  113. if (queue_stack_map_is_empty(qs)) {
  114. memset(value, 0, qs->map.value_size);
  115. err = -ENOENT;
  116. goto out;
  117. }
  118. index = qs->head - 1;
  119. if (unlikely(index >= qs->size))
  120. index = qs->size - 1;
  121. ptr = &qs->elements[index * qs->map.value_size];
  122. memcpy(value, ptr, qs->map.value_size);
  123. if (delete)
  124. qs->head = index;
  125. out:
  126. raw_spin_unlock_irqrestore(&qs->lock, flags);
  127. return err;
  128. }
  129. /* Called from syscall or from eBPF program */
  130. static long queue_map_peek_elem(struct bpf_map *map, void *value)
  131. {
  132. return __queue_map_get(map, value, false);
  133. }
  134. /* Called from syscall or from eBPF program */
  135. static long stack_map_peek_elem(struct bpf_map *map, void *value)
  136. {
  137. return __stack_map_get(map, value, false);
  138. }
  139. /* Called from syscall or from eBPF program */
  140. static long queue_map_pop_elem(struct bpf_map *map, void *value)
  141. {
  142. return __queue_map_get(map, value, true);
  143. }
  144. /* Called from syscall or from eBPF program */
  145. static long stack_map_pop_elem(struct bpf_map *map, void *value)
  146. {
  147. return __stack_map_get(map, value, true);
  148. }
  149. /* Called from syscall or from eBPF program */
  150. static long queue_stack_map_push_elem(struct bpf_map *map, void *value,
  151. u64 flags)
  152. {
  153. struct bpf_queue_stack *qs = bpf_queue_stack(map);
  154. unsigned long irq_flags;
  155. int err = 0;
  156. void *dst;
  157. /* BPF_EXIST is used to force making room for a new element in case the
  158. * map is full
  159. */
  160. bool replace = (flags & BPF_EXIST);
  161. /* Check supported flags for queue and stack maps */
  162. if (flags & BPF_NOEXIST || flags > BPF_EXIST)
  163. return -EINVAL;
  164. if (in_nmi()) {
  165. if (!raw_spin_trylock_irqsave(&qs->lock, irq_flags))
  166. return -EBUSY;
  167. } else {
  168. raw_spin_lock_irqsave(&qs->lock, irq_flags);
  169. }
  170. if (queue_stack_map_is_full(qs)) {
  171. if (!replace) {
  172. err = -E2BIG;
  173. goto out;
  174. }
  175. /* advance tail pointer to overwrite oldest element */
  176. if (unlikely(++qs->tail >= qs->size))
  177. qs->tail = 0;
  178. }
  179. dst = &qs->elements[qs->head * qs->map.value_size];
  180. memcpy(dst, value, qs->map.value_size);
  181. if (unlikely(++qs->head >= qs->size))
  182. qs->head = 0;
  183. out:
  184. raw_spin_unlock_irqrestore(&qs->lock, irq_flags);
  185. return err;
  186. }
  187. /* Called from syscall or from eBPF program */
  188. static void *queue_stack_map_lookup_elem(struct bpf_map *map, void *key)
  189. {
  190. return NULL;
  191. }
  192. /* Called from syscall or from eBPF program */
  193. static long queue_stack_map_update_elem(struct bpf_map *map, void *key,
  194. void *value, u64 flags)
  195. {
  196. return -EINVAL;
  197. }
  198. /* Called from syscall or from eBPF program */
  199. static long queue_stack_map_delete_elem(struct bpf_map *map, void *key)
  200. {
  201. return -EINVAL;
  202. }
  203. /* Called from syscall */
  204. static int queue_stack_map_get_next_key(struct bpf_map *map, void *key,
  205. void *next_key)
  206. {
  207. return -EINVAL;
  208. }
  209. static u64 queue_stack_map_mem_usage(const struct bpf_map *map)
  210. {
  211. u64 usage = sizeof(struct bpf_queue_stack);
  212. usage += ((u64)map->max_entries + 1) * map->value_size;
  213. return usage;
  214. }
  215. BTF_ID_LIST_SINGLE(queue_map_btf_ids, struct, bpf_queue_stack)
  216. const struct bpf_map_ops queue_map_ops = {
  217. .map_meta_equal = bpf_map_meta_equal,
  218. .map_alloc_check = queue_stack_map_alloc_check,
  219. .map_alloc = queue_stack_map_alloc,
  220. .map_free = queue_stack_map_free,
  221. .map_lookup_elem = queue_stack_map_lookup_elem,
  222. .map_update_elem = queue_stack_map_update_elem,
  223. .map_delete_elem = queue_stack_map_delete_elem,
  224. .map_push_elem = queue_stack_map_push_elem,
  225. .map_pop_elem = queue_map_pop_elem,
  226. .map_peek_elem = queue_map_peek_elem,
  227. .map_get_next_key = queue_stack_map_get_next_key,
  228. .map_mem_usage = queue_stack_map_mem_usage,
  229. .map_btf_id = &queue_map_btf_ids[0],
  230. };
  231. const struct bpf_map_ops stack_map_ops = {
  232. .map_meta_equal = bpf_map_meta_equal,
  233. .map_alloc_check = queue_stack_map_alloc_check,
  234. .map_alloc = queue_stack_map_alloc,
  235. .map_free = queue_stack_map_free,
  236. .map_lookup_elem = queue_stack_map_lookup_elem,
  237. .map_update_elem = queue_stack_map_update_elem,
  238. .map_delete_elem = queue_stack_map_delete_elem,
  239. .map_push_elem = queue_stack_map_push_elem,
  240. .map_pop_elem = stack_map_pop_elem,
  241. .map_peek_elem = stack_map_peek_elem,
  242. .map_get_next_key = queue_stack_map_get_next_key,
  243. .map_mem_usage = queue_stack_map_mem_usage,
  244. .map_btf_id = &queue_map_btf_ids[0],
  245. };