BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Michael S. Tsirkin" <mst@redhat.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH] bpf: ringbuf: add wakeup coalescing
Date: Tue, 01 Dec 2026 14:47:26 +0000	[thread overview]
Message-ID: <sashiko-outbox-243520@kernel.org> (raw)
In-Reply-To: <5663e1035f52f0b358710721c1c77e5c73552a2e.1796135892.git.mst@redhat.com>

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

      reply	other threads:[~2026-12-01 14:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 14:39 [PATCH] bpf: ringbuf: add wakeup coalescing Michael S. Tsirkin
2026-12-01 14:47 ` sashiko-bot [this message]

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=sashiko-outbox-243520@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=sashiko-reviews@lists.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