ringbuf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. #include <linux/bpf.h>
  2. #include <linux/btf.h>
  3. #include <linux/err.h>
  4. #include <linux/irq_work.h>
  5. #include <linux/slab.h>
  6. #include <linux/filter.h>
  7. #include <linux/mm.h>
  8. #include <linux/vmalloc.h>
  9. #include <linux/wait.h>
  10. #include <linux/poll.h>
  11. #include <linux/kmemleak.h>
  12. #include <uapi/linux/btf.h>
  13. #include <linux/btf_ids.h>
  14. #define RINGBUF_CREATE_FLAG_MASK (BPF_F_NUMA_NODE)
  15. /* non-mmap()'able part of bpf_ringbuf (everything up to consumer page) */
  16. #define RINGBUF_PGOFF \
  17. (offsetof(struct bpf_ringbuf, consumer_pos) >> PAGE_SHIFT)
  18. /* consumer page and producer page */
  19. #define RINGBUF_POS_PAGES 2
  20. #define RINGBUF_NR_META_PAGES (RINGBUF_PGOFF + RINGBUF_POS_PAGES)
  21. #define RINGBUF_MAX_RECORD_SZ (UINT_MAX/4)
  22. struct bpf_ringbuf {
  23. wait_queue_head_t waitq;
  24. struct irq_work work;
  25. u64 mask;
  26. struct page **pages;
  27. int nr_pages;
  28. raw_spinlock_t spinlock ____cacheline_aligned_in_smp;
  29. /* For user-space producer ring buffers, an atomic_t busy bit is used
  30. * to synchronize access to the ring buffers in the kernel, rather than
  31. * the spinlock that is used for kernel-producer ring buffers. This is
  32. * done because the ring buffer must hold a lock across a BPF program's
  33. * callback:
  34. *
  35. * __bpf_user_ringbuf_peek() // lock acquired
  36. * -> program callback_fn()
  37. * -> __bpf_user_ringbuf_sample_release() // lock released
  38. *
  39. * It is unsafe and incorrect to hold an IRQ spinlock across what could
  40. * be a long execution window, so we instead simply disallow concurrent
  41. * access to the ring buffer by kernel consumers, and return -EBUSY from
  42. * __bpf_user_ringbuf_peek() if the busy bit is held by another task.
  43. */
  44. atomic_t busy ____cacheline_aligned_in_smp;
  45. /* Consumer and producer counters are put into separate pages to
  46. * allow each position to be mapped with different permissions.
  47. * This prevents a user-space application from modifying the
  48. * position and ruining in-kernel tracking. The permissions of the
  49. * pages depend on who is producing samples: user-space or the
  50. * kernel. Note that the pending counter is placed in the same
  51. * page as the producer, so that it shares the same cache line.
  52. *
  53. * Kernel-producer
  54. * ---------------
  55. * The producer position and data pages are mapped as r/o in
  56. * userspace. For this approach, bits in the header of samples are
  57. * used to signal to user-space, and to other producers, whether a
  58. * sample is currently being written.
  59. *
  60. * User-space producer
  61. * -------------------
  62. * Only the page containing the consumer position is mapped r/o in
  63. * user-space. User-space producers also use bits of the header to
  64. * communicate to the kernel, but the kernel must carefully check and
  65. * validate each sample to ensure that they're correctly formatted, and
  66. * fully contained within the ring buffer.
  67. */
  68. unsigned long consumer_pos __aligned(PAGE_SIZE);
  69. unsigned long producer_pos __aligned(PAGE_SIZE);
  70. unsigned long pending_pos;
  71. char data[] __aligned(PAGE_SIZE);
  72. };
  73. struct bpf_ringbuf_map {
  74. struct bpf_map map;
  75. struct bpf_ringbuf *rb;
  76. };
  77. /* 8-byte ring buffer record header structure */
  78. struct bpf_ringbuf_hdr {
  79. u32 len;
  80. u32 pg_off;
  81. };
  82. static struct bpf_ringbuf *bpf_ringbuf_area_alloc(size_t data_sz, int numa_node)
  83. {
  84. const gfp_t flags = GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL |
  85. __GFP_NOWARN | __GFP_ZERO;
  86. int nr_meta_pages = RINGBUF_NR_META_PAGES;
  87. int nr_data_pages = data_sz >> PAGE_SHIFT;
  88. int nr_pages = nr_meta_pages + nr_data_pages;
  89. struct page **pages, *page;
  90. struct bpf_ringbuf *rb;
  91. size_t array_size;
  92. int i;
  93. /* Each data page is mapped twice to allow "virtual"
  94. * continuous read of samples wrapping around the end of ring
  95. * buffer area:
  96. * ------------------------------------------------------
  97. * | meta pages | real data pages | same data pages |
  98. * ------------------------------------------------------
  99. * | | 1 2 3 4 5 6 7 8 9 | 1 2 3 4 5 6 7 8 9 |
  100. * ------------------------------------------------------
  101. * | | TA DA | TA DA |
  102. * ------------------------------------------------------
  103. * ^^^^^^^
  104. * |
  105. * Here, no need to worry about special handling of wrapped-around
  106. * data due to double-mapped data pages. This works both in kernel and
  107. * when mmap()'ed in user-space, simplifying both kernel and
  108. * user-space implementations significantly.
  109. */
  110. array_size = (nr_meta_pages + 2 * nr_data_pages) * sizeof(*pages);
  111. pages = bpf_map_area_alloc(array_size, numa_node);
  112. if (!pages)
  113. return NULL;
  114. for (i = 0; i < nr_pages; i++) {
  115. page = alloc_pages_node(numa_node, flags, 0);
  116. if (!page) {
  117. nr_pages = i;
  118. goto err_free_pages;
  119. }
  120. pages[i] = page;
  121. if (i >= nr_meta_pages)
  122. pages[nr_data_pages + i] = page;
  123. }
  124. rb = vmap(pages, nr_meta_pages + 2 * nr_data_pages,
  125. VM_MAP | VM_USERMAP, PAGE_KERNEL);
  126. if (rb) {
  127. kmemleak_not_leak(pages);
  128. rb->pages = pages;
  129. rb->nr_pages = nr_pages;
  130. return rb;
  131. }
  132. err_free_pages:
  133. for (i = 0; i < nr_pages; i++)
  134. __free_page(pages[i]);
  135. bpf_map_area_free(pages);
  136. return NULL;
  137. }
  138. static void bpf_ringbuf_notify(struct irq_work *work)
  139. {
  140. struct bpf_ringbuf *rb = container_of(work, struct bpf_ringbuf, work);
  141. wake_up_all(&rb->waitq);
  142. }
  143. /* Maximum size of ring buffer area is limited by 32-bit page offset within
  144. * record header, counted in pages. Reserve 8 bits for extensibility, and
  145. * take into account few extra pages for consumer/producer pages and
  146. * non-mmap()'able parts, the current maximum size would be:
  147. *
  148. * (((1ULL << 24) - RINGBUF_POS_PAGES - RINGBUF_PGOFF) * PAGE_SIZE)
  149. *
  150. * This gives 64GB limit, which seems plenty for single ring buffer. Now
  151. * considering that the maximum value of data_sz is (4GB - 1), there
  152. * will be no overflow, so just note the size limit in the comments.
  153. */
  154. static struct bpf_ringbuf *bpf_ringbuf_alloc(size_t data_sz, int numa_node)
  155. {
  156. struct bpf_ringbuf *rb;
  157. rb = bpf_ringbuf_area_alloc(data_sz, numa_node);
  158. if (!rb)
  159. return NULL;
  160. raw_spin_lock_init(&rb->spinlock);
  161. atomic_set(&rb->busy, 0);
  162. init_waitqueue_head(&rb->waitq);
  163. init_irq_work(&rb->work, bpf_ringbuf_notify);
  164. rb->mask = data_sz - 1;
  165. rb->consumer_pos = 0;
  166. rb->producer_pos = 0;
  167. rb->pending_pos = 0;
  168. return rb;
  169. }
  170. static struct bpf_map *ringbuf_map_alloc(union bpf_attr *attr)
  171. {
  172. struct bpf_ringbuf_map *rb_map;
  173. if (attr->map_flags & ~RINGBUF_CREATE_FLAG_MASK)
  174. return ERR_PTR(-EINVAL);
  175. if (attr->key_size || attr->value_size ||
  176. !is_power_of_2(attr->max_entries) ||
  177. !PAGE_ALIGNED(attr->max_entries))
  178. return ERR_PTR(-EINVAL);
  179. rb_map = bpf_map_area_alloc(sizeof(*rb_map), NUMA_NO_NODE);
  180. if (!rb_map)
  181. return ERR_PTR(-ENOMEM);
  182. bpf_map_init_from_attr(&rb_map->map, attr);
  183. rb_map->rb = bpf_ringbuf_alloc(attr->max_entries, rb_map->map.numa_node);
  184. if (!rb_map->rb) {
  185. bpf_map_area_free(rb_map);
  186. return ERR_PTR(-ENOMEM);
  187. }
  188. return &rb_map->map;
  189. }
  190. static void bpf_ringbuf_free(struct bpf_ringbuf *rb)
  191. {
  192. /* copy pages pointer and nr_pages to local variable, as we are going
  193. * to unmap rb itself with vunmap() below
  194. */
  195. struct page **pages = rb->pages;
  196. int i, nr_pages = rb->nr_pages;
  197. vunmap(rb);
  198. for (i = 0; i < nr_pages; i++)
  199. __free_page(pages[i]);
  200. bpf_map_area_free(pages);
  201. }
  202. static void ringbuf_map_free(struct bpf_map *map)
  203. {
  204. struct bpf_ringbuf_map *rb_map;
  205. rb_map = container_of(map, struct bpf_ringbuf_map, map);
  206. bpf_ringbuf_free(rb_map->rb);
  207. bpf_map_area_free(rb_map);
  208. }
  209. static void *ringbuf_map_lookup_elem(struct bpf_map *map, void *key)
  210. {
  211. return ERR_PTR(-ENOTSUPP);
  212. }
  213. static long ringbuf_map_update_elem(struct bpf_map *map, void *key, void *value,
  214. u64 flags)
  215. {
  216. return -ENOTSUPP;
  217. }
  218. static long ringbuf_map_delete_elem(struct bpf_map *map, void *key)
  219. {
  220. return -ENOTSUPP;
  221. }
  222. static int ringbuf_map_get_next_key(struct bpf_map *map, void *key,
  223. void *next_key)
  224. {
  225. return -ENOTSUPP;
  226. }
  227. static int ringbuf_map_mmap_kern(struct bpf_map *map, struct vm_area_struct *vma)
  228. {
  229. struct bpf_ringbuf_map *rb_map;
  230. rb_map = container_of(map, struct bpf_ringbuf_map, map);
  231. if (vma->vm_flags & VM_WRITE) {
  232. /* allow writable mapping for the consumer_pos only */
  233. if (vma->vm_pgoff != 0 || vma->vm_end - vma->vm_start != PAGE_SIZE)
  234. return -EPERM;
  235. }
  236. /* remap_vmalloc_range() checks size and offset constraints */
  237. return remap_vmalloc_range(vma, rb_map->rb,
  238. vma->vm_pgoff + RINGBUF_PGOFF);
  239. }
  240. static int ringbuf_map_mmap_user(struct bpf_map *map, struct vm_area_struct *vma)
  241. {
  242. struct bpf_ringbuf_map *rb_map;
  243. rb_map = container_of(map, struct bpf_ringbuf_map, map);
  244. if (vma->vm_flags & VM_WRITE) {
  245. if (vma->vm_pgoff == 0)
  246. /* Disallow writable mappings to the consumer pointer,
  247. * and allow writable mappings to both the producer
  248. * position, and the ring buffer data itself.
  249. */
  250. return -EPERM;
  251. }
  252. /* remap_vmalloc_range() checks size and offset constraints */
  253. return remap_vmalloc_range(vma, rb_map->rb, vma->vm_pgoff + RINGBUF_PGOFF);
  254. }
  255. static unsigned long ringbuf_avail_data_sz(struct bpf_ringbuf *rb)
  256. {
  257. unsigned long cons_pos, prod_pos;
  258. cons_pos = smp_load_acquire(&rb->consumer_pos);
  259. prod_pos = smp_load_acquire(&rb->producer_pos);
  260. return prod_pos - cons_pos;
  261. }
  262. static u32 ringbuf_total_data_sz(const struct bpf_ringbuf *rb)
  263. {
  264. return rb->mask + 1;
  265. }
  266. static __poll_t ringbuf_map_poll_kern(struct bpf_map *map, struct file *filp,
  267. struct poll_table_struct *pts)
  268. {
  269. struct bpf_ringbuf_map *rb_map;
  270. rb_map = container_of(map, struct bpf_ringbuf_map, map);
  271. poll_wait(filp, &rb_map->rb->waitq, pts);
  272. if (ringbuf_avail_data_sz(rb_map->rb))
  273. return EPOLLIN | EPOLLRDNORM;
  274. return 0;
  275. }
  276. static __poll_t ringbuf_map_poll_user(struct bpf_map *map, struct file *filp,
  277. struct poll_table_struct *pts)
  278. {
  279. struct bpf_ringbuf_map *rb_map;
  280. rb_map = container_of(map, struct bpf_ringbuf_map, map);
  281. poll_wait(filp, &rb_map->rb->waitq, pts);
  282. if (ringbuf_avail_data_sz(rb_map->rb) < ringbuf_total_data_sz(rb_map->rb))
  283. return EPOLLOUT | EPOLLWRNORM;
  284. return 0;
  285. }
  286. static u64 ringbuf_map_mem_usage(const struct bpf_map *map)
  287. {
  288. struct bpf_ringbuf *rb;
  289. int nr_data_pages;
  290. int nr_meta_pages;
  291. u64 usage = sizeof(struct bpf_ringbuf_map);
  292. rb = container_of(map, struct bpf_ringbuf_map, map)->rb;
  293. usage += (u64)rb->nr_pages << PAGE_SHIFT;
  294. nr_meta_pages = RINGBUF_NR_META_PAGES;
  295. nr_data_pages = map->max_entries >> PAGE_SHIFT;
  296. usage += (nr_meta_pages + 2 * nr_data_pages) * sizeof(struct page *);
  297. return usage;
  298. }
  299. BTF_ID_LIST_SINGLE(ringbuf_map_btf_ids, struct, bpf_ringbuf_map)
  300. const struct bpf_map_ops ringbuf_map_ops = {
  301. .map_meta_equal = bpf_map_meta_equal,
  302. .map_alloc = ringbuf_map_alloc,
  303. .map_free = ringbuf_map_free,
  304. .map_mmap = ringbuf_map_mmap_kern,
  305. .map_poll = ringbuf_map_poll_kern,
  306. .map_lookup_elem = ringbuf_map_lookup_elem,
  307. .map_update_elem = ringbuf_map_update_elem,
  308. .map_delete_elem = ringbuf_map_delete_elem,
  309. .map_get_next_key = ringbuf_map_get_next_key,
  310. .map_mem_usage = ringbuf_map_mem_usage,
  311. .map_btf_id = &ringbuf_map_btf_ids[0],
  312. };
  313. BTF_ID_LIST_SINGLE(user_ringbuf_map_btf_ids, struct, bpf_ringbuf_map)
  314. const struct bpf_map_ops user_ringbuf_map_ops = {
  315. .map_meta_equal = bpf_map_meta_equal,
  316. .map_alloc = ringbuf_map_alloc,
  317. .map_free = ringbuf_map_free,
  318. .map_mmap = ringbuf_map_mmap_user,
  319. .map_poll = ringbuf_map_poll_user,
  320. .map_lookup_elem = ringbuf_map_lookup_elem,
  321. .map_update_elem = ringbuf_map_update_elem,
  322. .map_delete_elem = ringbuf_map_delete_elem,
  323. .map_get_next_key = ringbuf_map_get_next_key,
  324. .map_mem_usage = ringbuf_map_mem_usage,
  325. .map_btf_id = &user_ringbuf_map_btf_ids[0],
  326. };
  327. /* Given pointer to ring buffer record metadata and struct bpf_ringbuf itself,
  328. * calculate offset from record metadata to ring buffer in pages, rounded
  329. * down. This page offset is stored as part of record metadata and allows to
  330. * restore struct bpf_ringbuf * from record pointer. This page offset is
  331. * stored at offset 4 of record metadata header.
  332. */
  333. static size_t bpf_ringbuf_rec_pg_off(struct bpf_ringbuf *rb,
  334. struct bpf_ringbuf_hdr *hdr)
  335. {
  336. return ((void *)hdr - (void *)rb) >> PAGE_SHIFT;
  337. }
  338. /* Given pointer to ring buffer record header, restore pointer to struct
  339. * bpf_ringbuf itself by using page offset stored at offset 4
  340. */
  341. static struct bpf_ringbuf *
  342. bpf_ringbuf_restore_from_rec(struct bpf_ringbuf_hdr *hdr)
  343. {
  344. unsigned long addr = (unsigned long)(void *)hdr;
  345. unsigned long off = (unsigned long)hdr->pg_off << PAGE_SHIFT;
  346. return (void*)((addr & PAGE_MASK) - off);
  347. }
  348. static void *__bpf_ringbuf_reserve(struct bpf_ringbuf *rb, u64 size)
  349. {
  350. unsigned long cons_pos, prod_pos, new_prod_pos, pend_pos, flags;
  351. struct bpf_ringbuf_hdr *hdr;
  352. u32 len, pg_off, tmp_size, hdr_len;
  353. if (unlikely(size > RINGBUF_MAX_RECORD_SZ))
  354. return NULL;
  355. len = round_up(size + BPF_RINGBUF_HDR_SZ, 8);
  356. if (len > ringbuf_total_data_sz(rb))
  357. return NULL;
  358. cons_pos = smp_load_acquire(&rb->consumer_pos);
  359. if (in_nmi()) {
  360. if (!raw_spin_trylock_irqsave(&rb->spinlock, flags))
  361. return NULL;
  362. } else {
  363. raw_spin_lock_irqsave(&rb->spinlock, flags);
  364. }
  365. pend_pos = rb->pending_pos;
  366. prod_pos = rb->producer_pos;
  367. new_prod_pos = prod_pos + len;
  368. while (pend_pos < prod_pos) {
  369. hdr = (void *)rb->data + (pend_pos & rb->mask);
  370. hdr_len = READ_ONCE(hdr->len);
  371. if (hdr_len & BPF_RINGBUF_BUSY_BIT)
  372. break;
  373. tmp_size = hdr_len & ~BPF_RINGBUF_DISCARD_BIT;
  374. tmp_size = round_up(tmp_size + BPF_RINGBUF_HDR_SZ, 8);
  375. pend_pos += tmp_size;
  376. }
  377. rb->pending_pos = pend_pos;
  378. /* check for out of ringbuf space:
  379. * - by ensuring producer position doesn't advance more than
  380. * (ringbuf_size - 1) ahead
  381. * - by ensuring oldest not yet committed record until newest
  382. * record does not span more than (ringbuf_size - 1)
  383. */
  384. if (new_prod_pos - cons_pos > rb->mask ||
  385. new_prod_pos - pend_pos > rb->mask) {
  386. raw_spin_unlock_irqrestore(&rb->spinlock, flags);
  387. return NULL;
  388. }
  389. hdr = (void *)rb->data + (prod_pos & rb->mask);
  390. pg_off = bpf_ringbuf_rec_pg_off(rb, hdr);
  391. hdr->len = size | BPF_RINGBUF_BUSY_BIT;
  392. hdr->pg_off = pg_off;
  393. /* pairs with consumer's smp_load_acquire() */
  394. smp_store_release(&rb->producer_pos, new_prod_pos);
  395. raw_spin_unlock_irqrestore(&rb->spinlock, flags);
  396. return (void *)hdr + BPF_RINGBUF_HDR_SZ;
  397. }
  398. BPF_CALL_3(bpf_ringbuf_reserve, struct bpf_map *, map, u64, size, u64, flags)
  399. {
  400. struct bpf_ringbuf_map *rb_map;
  401. if (unlikely(flags))
  402. return 0;
  403. rb_map = container_of(map, struct bpf_ringbuf_map, map);
  404. return (unsigned long)__bpf_ringbuf_reserve(rb_map->rb, size);
  405. }
  406. const struct bpf_func_proto bpf_ringbuf_reserve_proto = {
  407. .func = bpf_ringbuf_reserve,
  408. .ret_type = RET_PTR_TO_RINGBUF_MEM_OR_NULL,
  409. .arg1_type = ARG_CONST_MAP_PTR,
  410. .arg2_type = ARG_CONST_ALLOC_SIZE_OR_ZERO,
  411. .arg3_type = ARG_ANYTHING,
  412. };
  413. static void bpf_ringbuf_commit(void *sample, u64 flags, bool discard)
  414. {
  415. unsigned long rec_pos, cons_pos;
  416. struct bpf_ringbuf_hdr *hdr;
  417. struct bpf_ringbuf *rb;
  418. u32 new_len;
  419. hdr = sample - BPF_RINGBUF_HDR_SZ;
  420. rb = bpf_ringbuf_restore_from_rec(hdr);
  421. new_len = hdr->len ^ BPF_RINGBUF_BUSY_BIT;
  422. if (discard)
  423. new_len |= BPF_RINGBUF_DISCARD_BIT;
  424. /* update record header with correct final size prefix */
  425. xchg(&hdr->len, new_len);
  426. /* if consumer caught up and is waiting for our record, notify about
  427. * new data availability
  428. */
  429. rec_pos = (void *)hdr - (void *)rb->data;
  430. cons_pos = smp_load_acquire(&rb->consumer_pos) & rb->mask;
  431. if (flags & BPF_RB_FORCE_WAKEUP)
  432. irq_work_queue(&rb->work);
  433. else if (cons_pos == rec_pos && !(flags & BPF_RB_NO_WAKEUP))
  434. irq_work_queue(&rb->work);
  435. }
  436. BPF_CALL_2(bpf_ringbuf_submit, void *, sample, u64, flags)
  437. {
  438. bpf_ringbuf_commit(sample, flags, false /* discard */);
  439. return 0;
  440. }
  441. const struct bpf_func_proto bpf_ringbuf_submit_proto = {
  442. .func = bpf_ringbuf_submit,
  443. .ret_type = RET_VOID,
  444. .arg1_type = ARG_PTR_TO_RINGBUF_MEM | OBJ_RELEASE,
  445. .arg2_type = ARG_ANYTHING,
  446. };
  447. BPF_CALL_2(bpf_ringbuf_discard, void *, sample, u64, flags)
  448. {
  449. bpf_ringbuf_commit(sample, flags, true /* discard */);
  450. return 0;
  451. }
  452. const struct bpf_func_proto bpf_ringbuf_discard_proto = {
  453. .func = bpf_ringbuf_discard,
  454. .ret_type = RET_VOID,
  455. .arg1_type = ARG_PTR_TO_RINGBUF_MEM | OBJ_RELEASE,
  456. .arg2_type = ARG_ANYTHING,
  457. };
  458. BPF_CALL_4(bpf_ringbuf_output, struct bpf_map *, map, void *, data, u64, size,
  459. u64, flags)
  460. {
  461. struct bpf_ringbuf_map *rb_map;
  462. void *rec;
  463. if (unlikely(flags & ~(BPF_RB_NO_WAKEUP | BPF_RB_FORCE_WAKEUP)))
  464. return -EINVAL;
  465. rb_map = container_of(map, struct bpf_ringbuf_map, map);
  466. rec = __bpf_ringbuf_reserve(rb_map->rb, size);
  467. if (!rec)
  468. return -EAGAIN;
  469. memcpy(rec, data, size);
  470. bpf_ringbuf_commit(rec, flags, false /* discard */);
  471. return 0;
  472. }
  473. const struct bpf_func_proto bpf_ringbuf_output_proto = {
  474. .func = bpf_ringbuf_output,
  475. .ret_type = RET_INTEGER,
  476. .arg1_type = ARG_CONST_MAP_PTR,
  477. .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  478. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  479. .arg4_type = ARG_ANYTHING,
  480. };
  481. BPF_CALL_2(bpf_ringbuf_query, struct bpf_map *, map, u64, flags)
  482. {
  483. struct bpf_ringbuf *rb;
  484. rb = container_of(map, struct bpf_ringbuf_map, map)->rb;
  485. switch (flags) {
  486. case BPF_RB_AVAIL_DATA:
  487. return ringbuf_avail_data_sz(rb);
  488. case BPF_RB_RING_SIZE:
  489. return ringbuf_total_data_sz(rb);
  490. case BPF_RB_CONS_POS:
  491. return smp_load_acquire(&rb->consumer_pos);
  492. case BPF_RB_PROD_POS:
  493. return smp_load_acquire(&rb->producer_pos);
  494. default:
  495. return 0;
  496. }
  497. }
  498. const struct bpf_func_proto bpf_ringbuf_query_proto = {
  499. .func = bpf_ringbuf_query,
  500. .ret_type = RET_INTEGER,
  501. .arg1_type = ARG_CONST_MAP_PTR,
  502. .arg2_type = ARG_ANYTHING,
  503. };
  504. BPF_CALL_4(bpf_ringbuf_reserve_dynptr, struct bpf_map *, map, u32, size, u64, flags,
  505. struct bpf_dynptr_kern *, ptr)
  506. {
  507. struct bpf_ringbuf_map *rb_map;
  508. void *sample;
  509. int err;
  510. if (unlikely(flags)) {
  511. bpf_dynptr_set_null(ptr);
  512. return -EINVAL;
  513. }
  514. err = bpf_dynptr_check_size(size);
  515. if (err) {
  516. bpf_dynptr_set_null(ptr);
  517. return err;
  518. }
  519. rb_map = container_of(map, struct bpf_ringbuf_map, map);
  520. sample = __bpf_ringbuf_reserve(rb_map->rb, size);
  521. if (!sample) {
  522. bpf_dynptr_set_null(ptr);
  523. return -EINVAL;
  524. }
  525. bpf_dynptr_init(ptr, sample, BPF_DYNPTR_TYPE_RINGBUF, 0, size);
  526. return 0;
  527. }
  528. const struct bpf_func_proto bpf_ringbuf_reserve_dynptr_proto = {
  529. .func = bpf_ringbuf_reserve_dynptr,
  530. .ret_type = RET_INTEGER,
  531. .arg1_type = ARG_CONST_MAP_PTR,
  532. .arg2_type = ARG_ANYTHING,
  533. .arg3_type = ARG_ANYTHING,
  534. .arg4_type = ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_RINGBUF | MEM_UNINIT | MEM_WRITE,
  535. };
  536. BPF_CALL_2(bpf_ringbuf_submit_dynptr, struct bpf_dynptr_kern *, ptr, u64, flags)
  537. {
  538. if (!ptr->data)
  539. return 0;
  540. bpf_ringbuf_commit(ptr->data, flags, false /* discard */);
  541. bpf_dynptr_set_null(ptr);
  542. return 0;
  543. }
  544. const struct bpf_func_proto bpf_ringbuf_submit_dynptr_proto = {
  545. .func = bpf_ringbuf_submit_dynptr,
  546. .ret_type = RET_VOID,
  547. .arg1_type = ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_RINGBUF | OBJ_RELEASE,
  548. .arg2_type = ARG_ANYTHING,
  549. };
  550. BPF_CALL_2(bpf_ringbuf_discard_dynptr, struct bpf_dynptr_kern *, ptr, u64, flags)
  551. {
  552. if (!ptr->data)
  553. return 0;
  554. bpf_ringbuf_commit(ptr->data, flags, true /* discard */);
  555. bpf_dynptr_set_null(ptr);
  556. return 0;
  557. }
  558. const struct bpf_func_proto bpf_ringbuf_discard_dynptr_proto = {
  559. .func = bpf_ringbuf_discard_dynptr,
  560. .ret_type = RET_VOID,
  561. .arg1_type = ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_RINGBUF | OBJ_RELEASE,
  562. .arg2_type = ARG_ANYTHING,
  563. };
  564. static int __bpf_user_ringbuf_peek(struct bpf_ringbuf *rb, void **sample, u32 *size)
  565. {
  566. int err;
  567. u32 hdr_len, sample_len, total_len, flags, *hdr;
  568. u64 cons_pos, prod_pos;
  569. /* Synchronizes with smp_store_release() in user-space producer. */
  570. prod_pos = smp_load_acquire(&rb->producer_pos);
  571. if (prod_pos % 8)
  572. return -EINVAL;
  573. /* Synchronizes with smp_store_release() in __bpf_user_ringbuf_sample_release() */
  574. cons_pos = smp_load_acquire(&rb->consumer_pos);
  575. if (cons_pos >= prod_pos)
  576. return -ENODATA;
  577. hdr = (u32 *)((uintptr_t)rb->data + (uintptr_t)(cons_pos & rb->mask));
  578. /* Synchronizes with smp_store_release() in user-space producer. */
  579. hdr_len = smp_load_acquire(hdr);
  580. flags = hdr_len & (BPF_RINGBUF_BUSY_BIT | BPF_RINGBUF_DISCARD_BIT);
  581. sample_len = hdr_len & ~flags;
  582. total_len = round_up(sample_len + BPF_RINGBUF_HDR_SZ, 8);
  583. /* The sample must fit within the region advertised by the producer position. */
  584. if (total_len > prod_pos - cons_pos)
  585. return -EINVAL;
  586. /* The sample must fit within the data region of the ring buffer. */
  587. if (total_len > ringbuf_total_data_sz(rb))
  588. return -E2BIG;
  589. /* The sample must fit into a struct bpf_dynptr. */
  590. err = bpf_dynptr_check_size(sample_len);
  591. if (err)
  592. return -E2BIG;
  593. if (flags & BPF_RINGBUF_DISCARD_BIT) {
  594. /* If the discard bit is set, the sample should be skipped.
  595. *
  596. * Update the consumer pos, and return -EAGAIN so the caller
  597. * knows to skip this sample and try to read the next one.
  598. */
  599. smp_store_release(&rb->consumer_pos, cons_pos + total_len);
  600. return -EAGAIN;
  601. }
  602. if (flags & BPF_RINGBUF_BUSY_BIT)
  603. return -ENODATA;
  604. *sample = (void *)((uintptr_t)rb->data +
  605. (uintptr_t)((cons_pos + BPF_RINGBUF_HDR_SZ) & rb->mask));
  606. *size = sample_len;
  607. return 0;
  608. }
  609. static void __bpf_user_ringbuf_sample_release(struct bpf_ringbuf *rb, size_t size, u64 flags)
  610. {
  611. u64 consumer_pos;
  612. u32 rounded_size = round_up(size + BPF_RINGBUF_HDR_SZ, 8);
  613. /* Using smp_load_acquire() is unnecessary here, as the busy-bit
  614. * prevents another task from writing to consumer_pos after it was read
  615. * by this task with smp_load_acquire() in __bpf_user_ringbuf_peek().
  616. */
  617. consumer_pos = rb->consumer_pos;
  618. /* Synchronizes with smp_load_acquire() in user-space producer. */
  619. smp_store_release(&rb->consumer_pos, consumer_pos + rounded_size);
  620. }
  621. BPF_CALL_4(bpf_user_ringbuf_drain, struct bpf_map *, map,
  622. void *, callback_fn, void *, callback_ctx, u64, flags)
  623. {
  624. struct bpf_ringbuf *rb;
  625. long samples, discarded_samples = 0, ret = 0;
  626. bpf_callback_t callback = (bpf_callback_t)callback_fn;
  627. u64 wakeup_flags = BPF_RB_NO_WAKEUP | BPF_RB_FORCE_WAKEUP;
  628. int busy = 0;
  629. if (unlikely(flags & ~wakeup_flags))
  630. return -EINVAL;
  631. rb = container_of(map, struct bpf_ringbuf_map, map)->rb;
  632. /* If another consumer is already consuming a sample, wait for them to finish. */
  633. if (!atomic_try_cmpxchg(&rb->busy, &busy, 1))
  634. return -EBUSY;
  635. for (samples = 0; samples < BPF_MAX_USER_RINGBUF_SAMPLES && ret == 0; samples++) {
  636. int err;
  637. u32 size;
  638. void *sample;
  639. struct bpf_dynptr_kern dynptr;
  640. err = __bpf_user_ringbuf_peek(rb, &sample, &size);
  641. if (err) {
  642. if (err == -ENODATA) {
  643. break;
  644. } else if (err == -EAGAIN) {
  645. discarded_samples++;
  646. continue;
  647. } else {
  648. ret = err;
  649. goto schedule_work_return;
  650. }
  651. }
  652. bpf_dynptr_init(&dynptr, sample, BPF_DYNPTR_TYPE_LOCAL, 0, size);
  653. ret = callback((uintptr_t)&dynptr, (uintptr_t)callback_ctx, 0, 0, 0);
  654. __bpf_user_ringbuf_sample_release(rb, size, flags);
  655. }
  656. ret = samples - discarded_samples;
  657. schedule_work_return:
  658. /* Prevent the clearing of the busy-bit from being reordered before the
  659. * storing of any rb consumer or producer positions.
  660. */
  661. atomic_set_release(&rb->busy, 0);
  662. if (flags & BPF_RB_FORCE_WAKEUP)
  663. irq_work_queue(&rb->work);
  664. else if (!(flags & BPF_RB_NO_WAKEUP) && samples > 0)
  665. irq_work_queue(&rb->work);
  666. return ret;
  667. }
  668. const struct bpf_func_proto bpf_user_ringbuf_drain_proto = {
  669. .func = bpf_user_ringbuf_drain,
  670. .ret_type = RET_INTEGER,
  671. .arg1_type = ARG_CONST_MAP_PTR,
  672. .arg2_type = ARG_PTR_TO_FUNC,
  673. .arg3_type = ARG_PTR_TO_STACK_OR_NULL,
  674. .arg4_type = ARG_ANYTHING,
  675. };