From: "Michael S. Tsirkin" <mst@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: Andrii Nakryiko <andrii@kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
John Fastabend <john.fastabend@gmail.com>,
bpf@vger.kernel.org
Subject: [PATCH] bpf: ringbuf: add wakeup coalescing
Date: Tue, 1 Dec 2026 09:39:00 -0500 [thread overview]
Message-ID: <5663e1035f52f0b358710721c1c77e5c73552a2e.1796135892.git.mst@redhat.com> (raw)
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
next reply other threads:[~2026-12-01 14:39 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 14:39 Michael S. Tsirkin [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5663e1035f52f0b358710721c1c77e5c73552a2e.1796135892.git.mst@redhat.com \
--to=mst@redhat.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®