In an overwritable ring buffer (BPF_F_RB_OVERWRITE) the kernel doesn't wait for the consumer and overwrites the oldest records when the ring buffer is full. A record returned by ring__next() can be overwritten while the caller is reading it, which is why such ring buffers can't be consumed through callbacks. Add ring__invalidated(), which returns true if any of the records returned by ring__next() since the last ring__release() or ring__rewind() may have been overwritten. The caller has to copy record data first, and use the copy only if ring__invalidated() returns false afterwards: do { while ((rec = ring__next(r))) { memcpy(copy, rec->data, rec->len); if (ring__invalidated(r)) break; handle(copy, rec->len); } } while (ring__release(r) > 0); It can also be called once for a number of records, e.g. to dump the ring buffer without consuming anything: n = 0; while ((rec = ring__next(r))) { memcpy(copies[n], rec->data, rec->len); lens[n++] = rec->len; } if (!ring__invalidated(r)) dump(copies, lens, n); ring__rewind(r); Records are overwritten in order, so ring__invalidated() only has to check that the producer is less than a ring buffer size ahead of the first record returned since the last release or rewind. For regular ring buffers it always returns false. Overwritable ring buffers get their own implementation of ring__next(), ringbuf_overwrite_next(), so that the regular one stays as it is. ringbuf_overwrite_next() does the same check for every record header before using it, so pos and len of a returned record are always valid and only record data needs ring__invalidated(). If the check fails, it continues from the kernel's overwrite position. There is no limit on how often that can repeat: a producer that keeps overwriting records before they can be read is not expected in practice. Overwritable ring buffers are not supported by 32-bit libbpf, where ring__next() fails with EOPNOTSUPP. It doesn't know where the overwrite position is, and 32-bit positions wrap after 4 GiB of records, which the check can't notice. Signed-off-by: Kumar Kartikeya Dwivedi --- tools/lib/bpf/libbpf.h | 32 ++++++++++++++ tools/lib/bpf/libbpf.map | 1 + tools/lib/bpf/ringbuf.c | 94 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 123 insertions(+), 4 deletions(-) diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 91d25bed5a04..24a70bf3f782 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -1691,6 +1691,10 @@ struct ring_rec { * Each call invalidates and reuses the previous struct ring_rec, so copy it if * it is needed later. * + * In an overwritable ring buffer (BPF_F_RB_OVERWRITE) record data can be + * overwritten at any time, see **ring__invalidated()**. Such ring buffers are + * not supported by 32-bit libbpf: **ring__next()** fails with EOPNOTSUPP. + * * @param r A ringbuffer object. * @return The next record, or NULL if there is nothing to consume. errno is * set on any error. @@ -1719,6 +1723,34 @@ LIBBPF_API int ring__release(struct ring *r); */ LIBBPF_API void ring__rewind(struct ring *r); +/** + * @brief **ring__invalidated()** checks if any of the records returned by + * **ring__next()** since the last **ring__release()** or **ring__rewind()** + * may have been overwritten. + * + * The kernel can overwrite records of an overwritable ring buffer + * (BPF_F_RB_OVERWRITE) while they are being read. Copy record data first, and + * use the copy only if this function returns false afterwards: + * + * do { + * while ((rec = ring__next(r))) { + * memcpy(copy, rec->data, rec->len); + * if (ring__invalidated(r)) + * break; + * handle(copy, rec->len); + * } + * } while (ring__release(r) > 0); + * + * Once it returns true, it keeps returning true until **ring__release()** or + * **ring__rewind()** is called. *pos* and *len* of a returned record are + * always valid and don't need this check. + * + * @param r A ringbuffer object. + * @return true if records may have been overwritten, false if not. Always + * false for a ring buffer that is not overwritable. + */ +LIBBPF_API bool ring__invalidated(const struct ring *r); + struct user_ring_buffer_opts { size_t sz; /* size of this struct, for forward/backward compatibility */ }; diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 943f6f3f0639..6a953f123138 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -479,6 +479,7 @@ LIBBPF_1.9.0 { bpf_program__load; bpf_program__load_strategy; bpf_program__set_load_strategy; + ring__invalidated; ring__next; ring__release; ring__rewind; diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c index 12d586afed1d..b4e9e27951eb 100644 --- a/tools/lib/bpf/ringbuf.c +++ b/tools/lib/bpf/ringbuf.c @@ -36,6 +36,9 @@ struct ring { struct ring_rec rec; /* always zero, in case struct ring_rec needs new fields */ unsigned long rec_reserved[2]; + /* overwrite mode: first record returned since the last release or rewind */ + unsigned long batch_pos; + bool has_batch; int map_fd; bool overwrite; }; @@ -497,11 +500,72 @@ static int ringbuf_validate_rec_api(const struct ring *r) { if (unlikely(r->sample_cb)) return -EINVAL; - if (unlikely(r->overwrite)) + /* 32-bit: no overwrite_pos, and positions wrap after 4 GiB of records */ + if (unlikely(r->overwrite && !r->overwrite_pos)) return -EOPNOTSUPP; return 0; } +/* + * Overwrite mode: check that whatever was read at pos, and at later positions, + * was not overwritten while we were reading it. The producer can overwrite pos + * once it is a ring size ahead of pos. + */ +static bool ringbuf_pos_valid(const struct ring *r, unsigned long pos, unsigned long *prod_pos) +{ + /* pairs with smp_wmb() after the producer_pos update in the kernel */ + smp_rmb(); + *prod_pos = smp_load_acquire(r->producer_pos); + return *prod_pos - pos <= r->mask; +} + +/* ring__next() for overwritable ring buffers */ +static const struct ring_rec *ringbuf_overwrite_next(struct ring *r) +{ + unsigned long pos = r->next_pos, prod_pos; + __u32 *len_ptr, len; + + prod_pos = smp_load_acquire(r->producer_pos); + for (;;) { + if (prod_pos - pos > r->mask) { + /* + * The producer lapped us, continue from the oldest record. + * overwrite_pos is never ahead of a producer_pos that is + * loaded after it. + */ + pos = smp_load_acquire(r->overwrite_pos); + prod_pos = smp_load_acquire(r->producer_pos); + continue; + } + r->next_pos = pos; + if (pos == prod_pos) + return NULL; + + len_ptr = r->data + (pos & r->mask); + len = smp_load_acquire(len_ptr); + /* if the producer lapped us, this is not a record header */ + if (!ringbuf_pos_valid(r, pos, &prod_pos)) + continue; + /* record is not committed yet */ + if (len & BPF_RINGBUF_BUSY_BIT) + return NULL; + + r->next_pos = pos + roundup_len(len); + if (len & BPF_RINGBUF_DISCARD_BIT) { + pos = r->next_pos; + continue; + } + if (!r->has_batch) { + r->batch_pos = pos; + r->has_batch = true; + } + r->rec.data = (void *)len_ptr + BPF_RINGBUF_HDR_SZ; + r->rec.pos = pos; + r->rec.len = len; + return &r->rec; + } +} + const struct ring_rec *ring__next(struct ring *r) { unsigned long pos, prod_pos; @@ -512,6 +576,9 @@ const struct ring_rec *ring__next(struct ring *r) if (err) return libbpf_err_ptr(err); + if (r->overwrite) + return ringbuf_overwrite_next(r); + pos = r->next_pos; prod_pos = r->prod_pos; for (;;) { @@ -540,9 +607,20 @@ const struct ring_rec *ring__next(struct ring *r) } } +bool ring__invalidated(const struct ring *r) +{ + unsigned long prod_pos; + + if (!r->has_batch) + return false; + + /* records are overwritten in order, so it is enough to check the first one */ + return !ringbuf_pos_valid(r, r->batch_pos, &prod_pos); +} + int ring__release(struct ring *r) { - unsigned long pos = r->next_pos; + unsigned long pos = r->next_pos, prod_pos; __u32 len; int err; @@ -550,6 +628,7 @@ int ring__release(struct ring *r) if (err) return libbpf_err(err); + r->has_batch = false; smp_store_release(r->consumer_pos, pos); /* * bpf_ringbuf_commit() clears the busy bit of a record and then checks @@ -563,8 +642,14 @@ int ring__release(struct ring *r) if (pos == r->prod_pos) return 0; len = smp_load_acquire((__u32 *)(r->data + (pos & r->mask))); - /* we get a wakeup when a busy record is committed */ - return !(len & BPF_RINGBUF_BUSY_BIT); + if (!(len & BPF_RINGBUF_BUSY_BIT)) + return 1; + /* + * A busy record wakes us up when it is committed, so return 0. In + * overwrite mode return 1 if the producer lapped us, as len was not + * read from a record header then. + */ + return r->overwrite && !ringbuf_pos_valid(r, pos, &prod_pos); } void ring__rewind(struct ring *r) @@ -573,6 +658,7 @@ void ring__rewind(struct ring *r) return; r->next_pos = smp_load_acquire(r->consumer_pos); + r->has_batch = false; } static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb) -- 2.53.1-Meta