From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796135948; b=LMqK4t5ae46pNQ7j92SXr4CIh+n4ryeENoxR3Jz1qQGkWt3ycWVKJmxc9B3/Y0RsSfvf 3t5i3CnV6Sjv7HWzNDpYOWw7Dejj42hmKgLAqFmcjmOaNwGpQCxWVqM38TXXqrt33A9Vt s9p/8vvJGs/dsmLMb6BjXI/arWWfJxODjdQ5tZFijySIrnqBYtg55vywHnDso0A9dVcu9 dz2OXHtkMDhC7cGrE+mUES+6UedAaOJlpbTpNY3bwWOlNRTLOW9CQmraxaC+bgcJ/ywe6 GMRZlFekEsE8eFpX5K0hjNIwBjq+bCehkIdCi3SgoCESbRsQ8EsD9tYYvWDqU9AsPsA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796135948; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:X-Received:Received:Date:From: To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition:X-Mailer:X-Mutt-Fcc; bh=uW1wcj1K3TMZ7wzATVt4sbvPnyvwWbFdNm1ZKlXessM=; b=34Av2C18qAuG5iVjTL+HapY28MJQqLUjKM6bBgT7smV0EycJ42FIP7KmCAw1Meckm0gg TysOXWdorSDmQvL1wNlsu8s/MtI7huwIIF71/xJPuASOPhD9UuA/ckmFL58T1H6T1XVSB WaClcD5HHGf6UuespIBHkStLbHxxCxyOWRs/8iAzPQq4LB1+GEJfvSzmrOt/ENsCu8vC+ 2D6cjq1apanToGiBphwzcjF7iKzhpGveBIN/4KVkQlGNhHkRoXRcmYsQ28KSOFpIvfZFa lGHoX+kJ7gjVzn5xuIwJypkqgjlwj7C7og4xCrPzgYKEDr5GKS3RST+gWIEbDwq8WKg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=hMfWNN4j; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=f978ri1y; arc=none smtp.remote-ip=170.10.129.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 98A2C606A31A for ; Tue, 1 Dec 2026 14:39:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 98A2C606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=hMfWNN4j; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=f978ri1y DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796135946; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type; bh=uW1wcj1K3TMZ7wzATVt4sbvPnyvwWbFdNm1ZKlXessM=; b=hMfWNN4j/De5xB4xT9at7drHUSSjGexl57CSOZe+vIoi+sEErm+kQhitMP9D2vEvElnSC8 KsZh4b2aRlKvPd2DWjbJy2C1eyJnF83lVVJjwisN+Cmi4ztzCpKMVGfrLNgBGLOJIXVscM ZeIvkAp3fgQ70V1hN40MJ/G+e2LM7dE= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-251-ed9568JXNguGuiX1npIRZw-1; Tue, 1 Dec 2026 14:39:04 +0000 X-MC-Unique: ed9568JXNguGuiX1npIRZw-1 X-Mimecast-MFC-AGG-ID: ed9568JXNguGuiX1npIRZw_1796135943 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4a80bd45e1cso65782115e9.3 for ; Tue, 01 Dec 2026 06:39:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1796135943; x=1796740743; darn=vger.kernel.org; h=content-disposition:content-type:mime-version:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=uW1wcj1K3TMZ7wzATVt4sbvPnyvwWbFdNm1ZKlXessM=; b=f978ri1ygE3bOe46v9yTzSIVxwRsgkJOezqGSIpJtkXMGFnEyQ5PNsw1ZUVfuMVAm1 R4Hx6VHrt4rm7HgUaBmDD7lE9KVBBaSI5E2Dc22GXLoj0FeXlSUqZ/eihYLIfrq9jtmO PWAO0m0S8nKkEy9YN5CWEh5CVH9W8lJCupWo6Fu6Db5SSIR5TvdlumGOU92kFeKGdyBm VAqnCxmECY0YXJtBlp4lg1Eiz/18kQtSN6UlcfSMBTZ0f7c9517A6F4hPEbywTLThD8L DmVy2nnIp1Sr2PLX7HOayE/tFxYwi/BiHxY2UTbfjceDlwG+8vPZu8hnxNO3Pj+iqH3a JPfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796135943; x=1796740743; h=content-disposition:content-type:mime-version:message-id:subject:cc :to:from:date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=uW1wc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QiEXmnbx1DCZBYm/3uyg== X-Received: by 2002:a05:600c:4ec9:b0:4bb:6a5a:782d with SMTP id 5b1f17b1804b1-4bb88925fcdmr758624815e9.17.1796135943249; Tue, 01 Dec 2026 06:39:03 -0800 (PST) X-Received: by 2002:a05:600c:4ec9:b0:4bb:6a5a:782d with SMTP id 5b1f17b1804b1-4bb88925fcdmr758624065e9.17.1796135942631; Tue, 01 Dec 2026 06:39:02 -0800 (PST) Received: from redhat.com ([2a0d:6fc0:1529:2e00:86ec:bef7:c309:ba1f]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb906635c7sm689321865e9.2.2026.12.01.06.39.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 06:39:02 -0800 (PST) Date: Tue, 1 Dec 2026 09:39:00 -0500 From: "Michael S. Tsirkin" To: linux-kernel@vger.kernel.org Cc: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Eduard Zingerman , Kumar Kartikeya Dwivedi , Song Liu , Yonghong Song , Jiri Olsa , John Fastabend , bpf@vger.kernel.org Subject: [PATCH] bpf: ringbuf: add wakeup coalescing Message-ID: <5663e1035f52f0b358710721c1c77e5c73552a2e.1796135892.git.mst@redhat.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Mailer: git-send-email 2.51.2.2891.g4157995a80.dirty X-Mutt-Fcc: =sent 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 --- 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 #include +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