ringbuf_map_poll_kern() reports the ring as readable when ringbuf_avail_data_sz() is not zero, which in overwrite mode is over_pos = smp_load_acquire(&rb->overwrite_pos); prod_pos = smp_load_acquire(&rb->producer_pos); return min(prod_pos - cons_pos, prod_pos - over_pos); __bpf_ringbuf_reserve() stores overwrite_pos before it publishes producer_pos. When the new record overwrites every record in the ring, it moves overwrite_pos up to the old producer_pos. A poll between the two stores computes prod_pos - over_pos == 0 and finds the ring empty, although the consumer is behind the producer. A consumer that polls at that moment goes back to sleep on unread records, and the commit of the new record does not wake it up, because bpf_ringbuf_commit() only wakes up the consumer for a record that starts at the offset of the consumer position in the ring. With a ring of 4096 bytes and records that take 2048: consumer producers consumed up to 0, epoll_wait() reserve and commit R1 at 0; consumer_pos is 0: wake up reserve R2: overwrite_pos = 2048 woken up, poll looks again: over_pos 2048, prod_pos 2048, nothing to read, sleep producer_pos = 4096 commit R2, at 2048; consumer_pos is 0: no wakeup There is no wakeup for R2. The consumer sleeps until a record starts at that offset again, which is the next one here. There is something to read whenever the consumer position is behind the producer position, in either mode. Compare those two and leave overwrite_pos out of it. bpf_ringbuf_query(BPF_RB_AVAIL_DATA) is unchanged. The missed wakeup follows from the code and was not reproduced, but the empty poll was. A user space loop that polls an overwritable ring, while a program overwrites it with records of more than half its size, found the ring empty in 25 to 59 of 1.1 million polls in three runs of half a second on a 4-CPU VM, and in none with this change. Fixes: feeaf1346f80 ("bpf: Add overwrite mode for BPF ring buffer") Acked-by: Andrii Nakryiko Signed-off-by: Kumar Kartikeya Dwivedi --- kernel/bpf/ringbuf.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c index 5806a5eb5d16..1334cfd90f2a 100644 --- a/kernel/bpf/ringbuf.c +++ b/kernel/bpf/ringbuf.c @@ -336,12 +336,18 @@ static u32 ringbuf_total_data_sz(const struct bpf_ringbuf *rb) static __poll_t ringbuf_map_poll_kern(struct bpf_map *map, struct file *filp, struct poll_table_struct *pts) { - struct bpf_ringbuf_map *rb_map; + struct bpf_ringbuf *rb; - rb_map = container_of(map, struct bpf_ringbuf_map, map); - poll_wait(filp, &rb_map->rb->waitq, pts); + rb = container_of(map, struct bpf_ringbuf_map, map)->rb; + poll_wait(filp, &rb->waitq, pts); - if (ringbuf_avail_data_sz(rb_map->rb)) + /* + * In overwrite mode ringbuf_avail_data_sz() can return 0 for a ring + * with unread records: a reservation can move overwrite_pos up to + * producer_pos before it updates producer_pos. Check if the consumer + * is behind the producer instead. + */ + if (smp_load_acquire(&rb->producer_pos) != smp_load_acquire(&rb->consumer_pos)) return EPOLLIN | EPOLLRDNORM; return 0; } -- 2.53.1-Meta