BPF List
 help / color / mirror / Atom feed
* [PATCH] bpf: ringbuf: add wakeup coalescing
@ 2026-12-01 14:39 Michael S. Tsirkin
  2026-12-01 14:47 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Michael S. Tsirkin @ 2026-12-01 14:39 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrii Nakryiko, Alexei Starovoitov, Daniel Borkmann,
	Martin KaFai Lau, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Song Liu, Yonghong Song, Jiri Olsa, John Fastabend, bpf

Currently, bpf_ringbuf_output triggers irq_work_queue on every record
when a poll is waiting. Under bursty traffic with a VFS monitoring
script, I observe this driving hundreds of epoll wakeups/s per core via
irq_work and wake_up.

Add per-ringbuf pending count and wakeup threshold to coalesce wakeups:
first record after idle wakes immediately, next ones are batched. This
reduces context-switch churn substantially under bursty traffic while
keeping latency low.

The threshold is tunable at runtime via
kernel.bpf_ringbuf_wakeup_threshold, nice for tuning on different
workloads.

With threshold 8, I observe the monitoring overhead going from 14.2%
down ~1.8% CPU time and system wakeups from 1,100/s to ~140/s.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
 kernel/bpf/ringbuf.c | 28 ++++++++++++++++++++++++----
 kernel/bpf/syscall.c |  9 +++++++++
 2 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c
index 35ae64ade36b8..4d71826997c66 100644
--- a/kernel/bpf/ringbuf.c
+++ b/kernel/bpf/ringbuf.c
@@ -14,6 +14,8 @@
 #include <linux/btf_ids.h>
 #include <asm/rqspinlock.h>
 
+extern int sysctl_bpf_ringbuf_wakeup_threshold;
+
 #define RINGBUF_CREATE_FLAG_MASK (BPF_F_NUMA_NODE | BPF_F_RB_OVERWRITE)
 
 /* non-mmap()'able part of bpf_ringbuf (everything up to consumer page) */
@@ -29,6 +31,8 @@ struct bpf_ringbuf {
 	wait_queue_head_t waitq;
 	struct irq_work work;
 	u64 mask;
+	atomic_t pending_cnt;
+	u32 wakeup_threshold;
 	struct page **pages;
 	int nr_pages;
 	bool overwrite_mode;
@@ -179,6 +183,7 @@ static struct bpf_ringbuf *bpf_ringbuf_alloc(size_t data_sz, int numa_node, bool
 
 	raw_res_spin_lock_init(&rb->spinlock);
 	atomic_set(&rb->busy, 0);
+	atomic_set(&rb->pending_cnt, 0);
 	init_waitqueue_head(&rb->waitq);
 	init_irq_work(&rb->work, bpf_ringbuf_notify);
 
@@ -187,6 +192,7 @@ static struct bpf_ringbuf *bpf_ringbuf_alloc(size_t data_sz, int numa_node, bool
 	rb->producer_pos = 0;
 	rb->pending_pos = 0;
 	rb->overwrite_mode = overwrite_mode;
+	rb->wakeup_threshold = sysctl_bpf_ringbuf_wakeup_threshold;
 
 	return rb;
 }
@@ -573,15 +579,29 @@ static void bpf_ringbuf_commit(void *sample, u64 flags, bool discard)
 	xchg(&hdr->len, new_len);
 
 	/* if consumer caught up and is waiting for our record, notify about
-	 * new data availability
+	 * new data availability. With coalescing, we batch wakeups when
+	 * consumer is idle to reduce bpfrb context switches under bursty
+	 * VFS traffic (Falcon).
 	 */
 	rec_pos = (void *)hdr - (void *)rb->data;
 	cons_pos = smp_load_acquire(&rb->consumer_pos) & rb->mask;
 
-	if (flags & BPF_RB_FORCE_WAKEUP)
-		irq_work_queue(&rb->work);
-	else if (cons_pos == rec_pos && !(flags & BPF_RB_NO_WAKEUP))
+	if (flags & BPF_RB_FORCE_WAKEUP) {
+		atomic_set(&rb->pending_cnt, 0);
 		irq_work_queue(&rb->work);
+	} else if (!(flags & BPF_RB_NO_WAKEUP)) {
+		if (cons_pos == rec_pos) {
+			int pending = atomic_inc_return(&rb->pending_cnt);
+			if (pending == 1) {
+				irq_work_queue(&rb->work);
+			} else if (pending >= rb->wakeup_threshold) {
+				atomic_set(&rb->pending_cnt, 0);
+				irq_work_queue(&rb->work);
+			}
+		} else {
+			atomic_set(&rb->pending_cnt, 0);
+		}
+	}
 }
 
 BPF_CALL_2(bpf_ringbuf_submit, void *, sample, u64, flags)
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 630d530782fe8..a297aef77fd70 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -68,6 +68,8 @@ static DEFINE_SPINLOCK(link_idr_lock);
 int sysctl_unprivileged_bpf_disabled __read_mostly =
 	IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;
 
+int sysctl_bpf_ringbuf_wakeup_threshold __read_mostly = 1;
+
 static const struct bpf_map_ops * const bpf_map_types[] = {
 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
 #define BPF_MAP_TYPE(_id, _ops) \
@@ -6629,6 +6631,13 @@ static const struct ctl_table bpf_syscall_table[] = {
 		.mode		= 0644,
 		.proc_handler	= bpf_stats_handler,
 	},
+	{
+		.procname	= "bpf_ringbuf_wakeup_threshold",
+		.data		= &sysctl_bpf_ringbuf_wakeup_threshold,
+		.maxlen		= sizeof(sysctl_bpf_ringbuf_wakeup_threshold),
+		.mode		= 0644,
+		.proc_handler	= proc_dointvec,
+	},
 };
 
 static int __init bpf_syscall_sysctl_init(void)
-- 
MST

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] bpf: ringbuf: add wakeup coalescing
  2026-12-01 14:39 [PATCH] bpf: ringbuf: add wakeup coalescing Michael S. Tsirkin
@ 2026-12-01 14:47 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-01 14:47 UTC (permalink / raw)
  To: Michael S. Tsirkin; +Cc: bpf

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] bpf: consumer wakeup stall in bpf_ringbuf_commit() when
  threshold > 1
- [Medium] bpf: missing bounds check and runtime update for ringbuf
  wakeup sysctl
- [Medium] bpf: false sharing on pending_cnt in bpf_ringbuf_commit()
- [Low] bpf: extern declaration of sysctl_bpf_ringbuf_wakeup_threshold
  in .c file
- [Low] bpf: non-standard multi-line comment format in
  bpf_ringbuf_commit()
--

commit 1b7b76ef2815dac5ca7b8f1973374e0f676f7ce2
Author: Michael S. Tsirkin <mst@redhat.com>

bpf: ringbuf: add wakeup coalescing

This commit adds per-ringbuf pending record counting and a
kernel.bpf_ringbuf_wakeup_threshold sysctl to coalesce irq_work consumer
wakeups in bpf_ringbuf_commit().

> diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c
> index 447863ece26a3..afd968d06e7c7 100644
> --- a/kernel/bpf/ringbuf.c
> +++ b/kernel/bpf/ringbuf.c
> @@ -14,6 +14,8 @@
>  #include <linux/btf_ids.h>
>  #include <asm/rqspinlock.h>
>  
> +extern int sysctl_bpf_ringbuf_wakeup_threshold;

[Severity: Low]
Should sysctl_bpf_ringbuf_wakeup_threshold be declared in a shared header
such as include/linux/bpf.h rather than with extern in kernel/bpf/ringbuf.c?
Defining it in kernel/bpf/syscall.c without a visible header declaration
prevents the compiler from type-checking the definition against its
declaration and triggers a sparse -Wdecl warning.

> +
>  #define RINGBUF_CREATE_FLAG_MASK (BPF_F_NUMA_NODE | BPF_F_RB_OVERWRITE)
>  
>  /* non-mmap()'able part of bpf_ringbuf (everything up to consumer page) */
> @@ -29,6 +31,8 @@ struct bpf_ringbuf {
>  	wait_queue_head_t waitq;
>  	struct irq_work work;
>  	u64 mask;
> +	atomic_t pending_cnt;
> +	u32 wakeup_threshold;
>  	struct page **pages;
>  	int nr_pages;
>  	bool overwrite_mode;

[Severity: Medium]
Does placing pending_cnt in the first cacheline of struct bpf_ringbuf
cause false sharing with read-mostly fields like mask and overwrite_mode?
Because bpf_ringbuf_commit() writes to rb->pending_cnt on every commit
without BPF_RB_NO_WAKEUP (including an unconditional atomic_set() to 0
when cons_pos != rec_pos, even when pending_cnt is already 0 or
wakeup_threshold is 1), concurrent producers will repeatedly invalidate
the cacheline read on every reserve and commit.

[ ... ]

> @@ -573,15 +579,29 @@ static void bpf_ringbuf_commit(void *sample, u64 flags, bool discard)
>  	xchg(&hdr->len, new_len);
>  
>  	/* if consumer caught up and is waiting for our record, notify about
> -	 * new data availability
> +	 * new data availability. With coalescing, we batch wakeups when
> +	 * consumer is idle to reduce bpfrb context switches under bursty
> +	 * VFS traffic (Falcon).
>  	 */

[Severity: Low]
This isn't a bug, but BPF multi-line comment style puts the opening /* on
its own line with the text starting on the next line, and the reference to
"(Falcon)" lacks context in the kernel tree.

>  	rec_pos = (void *)hdr - (void *)rb->data;
>  	cons_pos = smp_load_acquire(&rb->consumer_pos) & rb->mask;
>  
> -	if (flags & BPF_RB_FORCE_WAKEUP)
> -		irq_work_queue(&rb->work);
> -	else if (cons_pos == rec_pos && !(flags & BPF_RB_NO_WAKEUP))
> +	if (flags & BPF_RB_FORCE_WAKEUP) {
> +		atomic_set(&rb->pending_cnt, 0);
>  		irq_work_queue(&rb->work);
> +	} else if (!(flags & BPF_RB_NO_WAKEUP)) {
> +		if (cons_pos == rec_pos) {
> +			int pending = atomic_inc_return(&rb->pending_cnt);
> +			if (pending == 1) {
> +				irq_work_queue(&rb->work);
> +			} else if (pending >= rb->wakeup_threshold) {
> +				atomic_set(&rb->pending_cnt, 0);
> +				irq_work_queue(&rb->work);
> +			}
> +		} else {
> +			atomic_set(&rb->pending_cnt, 0);
> +		}
> +	}
>  }

[Severity: High]
Can this permanently stall consumer wakeups when rb->wakeup_threshold > 1?

cons_pos == rec_pos is only true when the record being committed is at
rb->consumer_pos (the consumer had caught up to all prior records).  When
record 1 is committed into an empty ring buffer, pending becomes 1 and
irq_work_queue() wakes the consumer, leaving rb->pending_cnt at 1.  After
the consumer drains record 1, advances rb->consumer_pos, and goes back to
sleep, record 2 arrives with cons_pos == rec_pos again and increments
rb->pending_cnt to 2.  For any wakeup_threshold > 2, record 2 does not
wake the sleeping consumer (and for wakeup_threshold == 2, record 2 stays
unconsumed if no third record arrives).

Because the consumer remains asleep, rb->consumer_pos stops advancing at
record 2.  Every subsequent record 3, 4, ... has rec_pos != cons_pos, so
it takes the else branch, resets rb->pending_cnt to 0, and never calls
irq_work_queue().  In non-overwrite mode, bpf_ringbuf_has_space() prevents
producer_pos from wrapping around to cons_pos, so the ring buffer fills to
capacity and drops all future records while the consumer hangs in poll().

> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> index 0bdb4288beb29..78d50d28bd14f 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
[ ... ]
> @@ -6917,6 +6919,13 @@ static const struct ctl_table bpf_syscall_table[] = {
>  		.mode		= 0644,
>  		.proc_handler	= bpf_stats_handler,
>  	},
> +	{
> +		.procname	= "bpf_ringbuf_wakeup_threshold",
> +		.data		= &sysctl_bpf_ringbuf_wakeup_threshold,
> +		.maxlen		= sizeof(sysctl_bpf_ringbuf_wakeup_threshold),
> +		.mode		= 0644,
> +		.proc_handler	= proc_dointvec,
> +	},
>  };

[Severity: Medium]
Should this sysctl use proc_dointvec_minmax with .extra1 = SYSCTL_ONE?
With proc_dointvec, writing a negative value converts to a huge unsigned
u32 in rb->wakeup_threshold, which signed int pending in
bpf_ringbuf_commit() is then compared against using unsigned comparison.
Also, because bpf_ringbuf_alloc() latches sysctl_bpf_ringbuf_wakeup_threshold
only at map creation time, changing the sysctl at runtime has no effect on
existing ring buffers.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/5663e1035f52f0b358710721c1c77e5c73552a2e.1796135892.git.mst@redhat.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01 14:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 14:39 [PATCH] bpf: ringbuf: add wakeup coalescing Michael S. Tsirkin
2026-12-01 14:47 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox