EBPF Talk: Ringbuf Experience Sharing

Beck Moulton
2 min read1 day ago

I have been using Ringbuf recentlybpf_ringbuf_reserve()I stepped on a hole and recorded it.

Introduction to Ringbuf

answer: have

Directly flip throughbpf_ringbuf_reserve()Source code:

// https://github.com/torvalds/linux/blob/5be63fc19fcaa4c236b307420483578a56986a37/kernel/bpf/ringbuf.c#L408static void *__bpf_ringbuf_reserve(struct bpf_ringbuf *rb, u64 size)
{
// ... cons_pos = smp_load_acquire(&rb->consumer_pos); if (in_nmi()) {
if (!spin_trylock_irqsave(&rb->spinlock, flags))
return NULL;
} else {
spin_lock_irqsave(&rb->spinlock, flags);
} pend_pos = rb->pending_pos;
prod_pos = rb->producer_pos;
new_prod_pos = prod_pos + len; // ... /* pairs with consumer's smp_load_acquire() */
smp_store_release(&rb->producer_pos, new_prod_pos); spin_unlock_irqrestore(&rb->spinlock, flags); return (void *)hdr + BPF_RINGBUF_HDR_SZ;
}BPF_CALL_3(bpf_ringbuf_reserve, struct bpf_map *, map, u64, size, u64, flags)
{
struct bpf_ringbuf_map *rb_map; if (unlikely(flags))
return 0; rb_map = container_of(map, struct bpf_ringbuf_map, map);
return (unsigned long)__bpf_ringbuf_reserve(rb_map->rb, size);
}

When writing BPF code, I did not pay attention to the impact of this lock on performance, resulting in poor performance. Example:

static __always_inline void
record_event(struct xdp_md *xdp)
{
struct event_t *event; event =

--

--

Beck Moulton

Focus on the back-end field, do actual combat technology sharing Buy me a Coffee if You Appreciate My Hard Work https://www.buymeacoffee.com/BeckMoulton