queue_stack_maps.c 6.9 KB

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