* [PATCH] bpf: ringbuf: add wakeup coalescing
@ 2026-12-01 14:39 Michael S. Tsirkin
2026-12-02 0:09 ` Alexei Starovoitov
0 siblings, 1 reply; 6+ 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 [flat|nested] 6+ 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-02 0:09 ` Alexei Starovoitov
2026-12-02 0:29 ` Michael S. Tsirkin
0 siblings, 1 reply; 6+ messages in thread
From: Alexei Starovoitov @ 2026-12-02 0:09 UTC (permalink / raw)
To: Michael S. Tsirkin, linux-kernel
Cc: Andrii Nakryiko, Daniel Borkmann, Martin KaFai Lau,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Song Liu,
Yonghong Song, Jiri Olsa, John Fastabend, bpf
On Tue, Dec 01, 2026 at 06:39 AM Michael S. Tsirkin <mst@redhat.com> wrote:
> 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.
Sounds like your tracing tool is simply inefficient.
No need for kernel changes. The prog can do it today.
BPF_RB_NO_WAKEUP, BPF_RB_FORCE_WAKEUP and bpf_ringbuf_query() exist for
that.
avail = bpf_ringbuf_query(&rb, BPF_RB_AVAIL_DATA);
bpf_ringbuf_submit(rec, avail >= wakeup_data_size ?
BPF_RB_FORCE_WAKEUP : BPF_RB_NO_WAKEUP);
pw-bot: cr
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] bpf: ringbuf: add wakeup coalescing
2026-12-02 0:09 ` Alexei Starovoitov
@ 2026-12-02 0:29 ` Michael S. Tsirkin
2026-12-02 0:54 ` Alexei Starovoitov
0 siblings, 1 reply; 6+ messages in thread
From: Michael S. Tsirkin @ 2026-12-02 0:29 UTC (permalink / raw)
To: Alexei Starovoitov
Cc: linux-kernel, Andrii Nakryiko, Daniel Borkmann, Martin KaFai Lau,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Song Liu,
Yonghong Song, Jiri Olsa, John Fastabend, bpf
On Wed, Dec 02, 2026 at 12:09:54AM +0000, Alexei Starovoitov wrote:
> On Tue, Dec 01, 2026 at 06:39 AM Michael S. Tsirkin <mst@redhat.com> wrote:
> > 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.
>
> Sounds like your tracing tool is simply inefficient.
Oh yes. Yet I don't have control over it.
> No need for kernel changes. The prog can do it today.
> BPF_RB_NO_WAKEUP, BPF_RB_FORCE_WAKEUP and bpf_ringbuf_query() exist for
> that.
>
> avail = bpf_ringbuf_query(&rb, BPF_RB_AVAIL_DATA);
> bpf_ringbuf_submit(rec, avail >= wakeup_data_size ?
> BPF_RB_FORCE_WAKEUP : BPF_RB_NO_WAKEUP);
This assumes that every bpf program is tuned for every hardware box it
runs on. It's not gonnu happen.
>
> pw-bot: cr
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] bpf: ringbuf: add wakeup coalescing
2026-12-02 0:29 ` Michael S. Tsirkin
@ 2026-12-02 0:54 ` Alexei Starovoitov
2026-12-02 1:00 ` Michael S. Tsirkin
0 siblings, 1 reply; 6+ messages in thread
From: Alexei Starovoitov @ 2026-12-02 0:54 UTC (permalink / raw)
To: Michael S. Tsirkin
Cc: linux-kernel, Andrii Nakryiko, Daniel Borkmann, Martin KaFai Lau,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Song Liu,
Yonghong Song, Jiri Olsa, John Fastabend, bpf
On Tue, Dec 01, 2026 at 04:29 PM Michael S. Tsirkin <mst@redhat.com> wrote:
>> Sounds like your tracing tool is simply inefficient.
>
> Oh yes. Yet I don't have control over it.
Then pls report it to the people who do. It's two lines in their prog.
The kernel shouldn't work around an inefficient tool.
>> avail = bpf_ringbuf_query(&rb, BPF_RB_AVAIL_DATA);
>> bpf_ringbuf_submit(rec, avail >= wakeup_data_size ?
>> BPF_RB_FORCE_WAKEUP : BPF_RB_NO_WAKEUP);
>
>
> This assumes that every bpf program is tuned for every hardware box it
> runs on. It's not gonnu happen.
The sysctl assumes the same. Its default is 1, which is what we have today.
Somebody has to find that 8 for every box and every workload.
Then that one number applies to every ringbuf on the box.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] bpf: ringbuf: add wakeup coalescing
2026-12-02 0:54 ` Alexei Starovoitov
@ 2026-12-02 1:00 ` Michael S. Tsirkin
2026-12-02 1:19 ` Alexei Starovoitov
0 siblings, 1 reply; 6+ messages in thread
From: Michael S. Tsirkin @ 2026-12-02 1:00 UTC (permalink / raw)
To: Alexei Starovoitov
Cc: linux-kernel, Andrii Nakryiko, Daniel Borkmann, Martin KaFai Lau,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Song Liu,
Yonghong Song, Jiri Olsa, John Fastabend, bpf
On Wed, Dec 02, 2026 at 12:54:17AM +0000, Alexei Starovoitov wrote:
> On Tue, Dec 01, 2026 at 04:29 PM Michael S. Tsirkin <mst@redhat.com> wrote:
> >> Sounds like your tracing tool is simply inefficient.
> >
> > Oh yes. Yet I don't have control over it.
>
> Then pls report it to the people who do. It's two lines in their prog.
> The kernel shouldn't work around an inefficient tool.
Shrug. Most software in the world is inefficient.
If I can improve my laptop's battery life, I'm be happier.
> >> avail = bpf_ringbuf_query(&rb, BPF_RB_AVAIL_DATA);
> >> bpf_ringbuf_submit(rec, avail >= wakeup_data_size ?
> >> BPF_RB_FORCE_WAKEUP : BPF_RB_NO_WAKEUP);
> >
> >
> > This assumes that every bpf program is tuned for every hardware box it
> > runs on. It's not gonnu happen.
>
> The sysctl assumes the same. Its default is 1, which is what we have today.
If you feel changing that to 8 is okay, lemme know.
> Somebody has to find that 8 for every box and every workload.
> Then that one number applies to every ringbuf on the box.
I mean, I can do it on my box. That's the point. Currently
bpf can only have the universal threshold, I want to add a
per-box one, if some box is different than others.
--
MST
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] bpf: ringbuf: add wakeup coalescing
2026-12-02 1:00 ` Michael S. Tsirkin
@ 2026-12-02 1:19 ` Alexei Starovoitov
0 siblings, 0 replies; 6+ messages in thread
From: Alexei Starovoitov @ 2026-12-02 1:19 UTC (permalink / raw)
To: Michael S. Tsirkin
Cc: linux-kernel, Andrii Nakryiko, Daniel Borkmann, Martin KaFai Lau,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Song Liu,
Yonghong Song, Jiri Olsa, John Fastabend, bpf
On Tue, Dec 01, 2026 at 05:00 PM Michael S. Tsirkin <mst@redhat.com> wrote:
>> The sysctl assumes the same. Its default is 1, which is what we have today.
>
> If you feel changing that to 8 is okay, lemme know.
No. Not every ringbuf is a stream of samples.
> I mean, I can do it on my box. That's the point. Currently
> bpf can only have the universal threshold, I want to add a
> per-box one, if some box is different than others.
No. It's not universal. It was always up to bpf program
and explicitly documented Documentation/bpf/ringbuf.rst
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-02 1:20 UTC | newest]
Thread overview: 6+ 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-02 0:09 ` Alexei Starovoitov
2026-12-02 0:29 ` Michael S. Tsirkin
2026-12-02 0:54 ` Alexei Starovoitov
2026-12-02 1:00 ` Michael S. Tsirkin
2026-12-02 1:19 ` Alexei Starovoitov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®