From: Eric Curtin <ericcurtin17@gmail.com>
To: linux-arm-kernel@lists.infradead.org
Cc: Ryan Roberts <ryan.roberts@arm.com>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
John Fastabend <john.fastabend@gmail.com>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
bpf@vger.kernel.org
Subject: [RFC PATCH v2 09/43] bpf: Do not embed page-aligned members in struct bpf_ringbuf
Date: Tue, 1 Dec 2026 01:51:41 +0000 [thread overview]
Message-ID: <20261201015240.1017259-10-ericcurtin17@gmail.com> (raw)
In-Reply-To: <20261201015240.1017259-1-ericcurtin17@gmail.com>
From: Ryan Roberts <ryan.roberts@arm.com>
struct bpf_ringbuf places consumer_pos, producer_pos and data at offsets
aligned with __aligned(PAGE_SIZE). With boot-time page size selection
the page size is not known at compile time, so this layout cannot be
expressed.
Make consumer_pos, producer_pos, pending_pos, overwrite_pos and data
pointers instead, and keep only the non-mmap()able metadata in the
struct. bpf_ringbuf_area_alloc() points them into the pages that follow
the struct when the area is vmap()ed, and all users dereference the
pointers. RINGBUF_PGOFF becomes the page-aligned size of the struct.
In kernel/bpf/core.c, always expose __PAGE_SIZE_MIN and __PAGE_SIZE_MAX
to BTF, but expose __PAGE_SIZE only when PAGE_SIZE_MIN equals
PAGE_SIZE_MAX, since otherwise it is not a constant.
bpf_prog_alloc_no_stats() now rounds up with PAGE_SIZE directly.
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit dacca9a55f8e2b32aeccf6bf90497e5890f46de0)
---
kernel/bpf/core.c | 11 ++++++--
kernel/bpf/ringbuf.c | 67 ++++++++++++++++++++++++--------------------
2 files changed, 46 insertions(+), 32 deletions(-)
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 6742f5be8658..dc3d62671a9d 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -97,9 +97,16 @@ void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, uns
return NULL;
}
-/* tell bpf programs that include vmlinux.h kernel's PAGE_SIZE */
+/*
+ * tell bpf programs that include vmlinux.h kernel's PAGE_SIZE. __PAGE_SIZE is
+ * only known for compile-time PAGE_SIZE builds, the limits are always known.
+ */
enum page_size_enum {
+ __PAGE_SIZE_MIN = PAGE_SIZE_MIN,
+ __PAGE_SIZE_MAX = PAGE_SIZE_MAX,
+#if PAGE_SIZE_MIN == PAGE_SIZE_MAX
__PAGE_SIZE = PAGE_SIZE
+#endif
};
struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags)
@@ -108,7 +115,7 @@ struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flag
struct bpf_prog_aux *aux;
struct bpf_prog *fp;
- size = round_up(size, __PAGE_SIZE);
+ size = round_up(size, PAGE_SIZE);
fp = __vmalloc(size, gfp_flags);
if (fp == NULL)
return NULL;
diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c
index 447863ece26a..9f9fab60e0c9 100644
--- a/kernel/bpf/ringbuf.c
+++ b/kernel/bpf/ringbuf.c
@@ -16,9 +16,9 @@
#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) */
+/* non-mmap()'able part of bpf_ringbuf (everything defined in struct) */
#define RINGBUF_PGOFF \
- (offsetof(struct bpf_ringbuf, consumer_pos) >> PAGE_SHIFT)
+ (PAGE_ALIGN(sizeof(struct bpf_ringbuf)) >> PAGE_SHIFT)
/* consumer page and producer page */
#define RINGBUF_POS_PAGES 2
#define RINGBUF_NR_META_PAGES (RINGBUF_PGOFF + RINGBUF_POS_PAGES)
@@ -72,11 +72,11 @@ struct bpf_ringbuf {
* validate each sample to ensure that they're correctly formatted, and
* fully contained within the ring buffer.
*/
- unsigned long consumer_pos __aligned(PAGE_SIZE);
- unsigned long producer_pos __aligned(PAGE_SIZE);
- unsigned long pending_pos;
- unsigned long overwrite_pos; /* position after the last overwritten record */
- char data[] __aligned(PAGE_SIZE);
+ unsigned long *consumer_pos;
+ unsigned long *producer_pos;
+ unsigned long *pending_pos;
+ unsigned long *overwrite_pos; /* position after the last overwritten record */
+ char *data;
};
struct bpf_ringbuf_map {
@@ -138,9 +138,16 @@ static struct bpf_ringbuf *bpf_ringbuf_area_alloc(size_t data_sz, int numa_node)
rb = vmap(pages, nr_meta_pages + 2 * nr_data_pages,
VM_MAP | VM_USERMAP, PAGE_KERNEL);
if (rb) {
+ void *base = rb;
+
kmemleak_not_leak(pages);
rb->pages = pages;
rb->nr_pages = nr_pages;
+ rb->consumer_pos = (unsigned long *)(base + PAGE_SIZE * RINGBUF_PGOFF);
+ rb->producer_pos = (unsigned long *)(base + PAGE_SIZE * (RINGBUF_PGOFF + 1));
+ rb->pending_pos = rb->producer_pos + 1;
+ rb->overwrite_pos = rb->producer_pos + 2;
+ rb->data = base + PAGE_SIZE * nr_meta_pages;
return rb;
}
@@ -183,9 +190,9 @@ static struct bpf_ringbuf *bpf_ringbuf_alloc(size_t data_sz, int numa_node, bool
init_irq_work(&rb->work, bpf_ringbuf_notify);
rb->mask = data_sz - 1;
- rb->consumer_pos = 0;
- rb->producer_pos = 0;
- rb->pending_pos = 0;
+ *rb->consumer_pos = 0;
+ *rb->producer_pos = 0;
+ *rb->pending_pos = 0;
rb->overwrite_mode = overwrite_mode;
return rb;
@@ -316,14 +323,14 @@ static unsigned long ringbuf_avail_data_sz(struct bpf_ringbuf *rb)
{
unsigned long cons_pos, prod_pos, over_pos;
- cons_pos = smp_load_acquire(&rb->consumer_pos);
+ cons_pos = smp_load_acquire(rb->consumer_pos);
if (unlikely(rb->overwrite_mode)) {
- over_pos = smp_load_acquire(&rb->overwrite_pos);
- prod_pos = smp_load_acquire(&rb->producer_pos);
+ 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);
} else {
- prod_pos = smp_load_acquire(&rb->producer_pos);
+ prod_pos = smp_load_acquire(rb->producer_pos);
return prod_pos - cons_pos;
}
}
@@ -473,13 +480,13 @@ static void *__bpf_ringbuf_reserve(struct bpf_ringbuf *rb, u64 size)
if (len > ringbuf_total_data_sz(rb))
return NULL;
- cons_pos = smp_load_acquire(&rb->consumer_pos);
+ cons_pos = smp_load_acquire(rb->consumer_pos);
if (raw_res_spin_lock_irqsave(&rb->spinlock, flags))
return NULL;
- pend_pos = rb->pending_pos;
- prod_pos = rb->producer_pos;
+ pend_pos = *rb->pending_pos;
+ prod_pos = *rb->producer_pos;
new_prod_pos = prod_pos + len;
while (prod_pos - pend_pos > 0) {
@@ -489,7 +496,7 @@ static void *__bpf_ringbuf_reserve(struct bpf_ringbuf *rb, u64 size)
break;
pend_pos += bpf_ringbuf_round_up_hdr_len(hdr_len);
}
- rb->pending_pos = pend_pos;
+ *rb->pending_pos = pend_pos;
if (!bpf_ringbuf_has_space(rb, new_prod_pos, cons_pos, pend_pos)) {
raw_res_spin_unlock_irqrestore(&rb->spinlock, flags);
@@ -502,7 +509,7 @@ static void *__bpf_ringbuf_reserve(struct bpf_ringbuf *rb, u64 size)
* to fit the new one.
*/
if (unlikely(rb->overwrite_mode)) {
- over_pos = rb->overwrite_pos;
+ over_pos = *rb->overwrite_pos;
while (new_prod_pos - over_pos > rb->mask) {
hdr = (void *)rb->data + (over_pos & rb->mask);
hdr_len = READ_ONCE(hdr->len);
@@ -521,7 +528,7 @@ static void *__bpf_ringbuf_reserve(struct bpf_ringbuf *rb, u64 size)
* after smp_load_acquire(r->producer_pos), the overwrite_pos
* will always be valid.
*/
- WRITE_ONCE(rb->overwrite_pos, over_pos);
+ WRITE_ONCE(*rb->overwrite_pos, over_pos);
}
hdr = (void *)rb->data + (prod_pos & rb->mask);
@@ -530,7 +537,7 @@ static void *__bpf_ringbuf_reserve(struct bpf_ringbuf *rb, u64 size)
hdr->pg_off = pg_off;
/* pairs with consumer's smp_load_acquire() */
- smp_store_release(&rb->producer_pos, new_prod_pos);
+ smp_store_release(rb->producer_pos, new_prod_pos);
raw_res_spin_unlock_irqrestore(&rb->spinlock, flags);
@@ -576,7 +583,7 @@ static void bpf_ringbuf_commit(void *sample, u64 flags, bool discard)
* new data availability
*/
rec_pos = (void *)hdr - (void *)rb->data;
- cons_pos = smp_load_acquire(&rb->consumer_pos) & rb->mask;
+ cons_pos = smp_load_acquire(rb->consumer_pos) & rb->mask;
if (flags & BPF_RB_FORCE_WAKEUP)
irq_work_queue(&rb->work);
@@ -650,11 +657,11 @@ BPF_CALL_2(bpf_ringbuf_query, struct bpf_map *, map, u64, flags)
case BPF_RB_RING_SIZE:
return ringbuf_total_data_sz(rb);
case BPF_RB_CONS_POS:
- return smp_load_acquire(&rb->consumer_pos);
+ return smp_load_acquire(rb->consumer_pos);
case BPF_RB_PROD_POS:
- return smp_load_acquire(&rb->producer_pos);
+ return smp_load_acquire(rb->producer_pos);
case BPF_RB_OVERWRITE_POS:
- return smp_load_acquire(&rb->overwrite_pos);
+ return smp_load_acquire(rb->overwrite_pos);
default:
return 0;
}
@@ -752,12 +759,12 @@ static int __bpf_user_ringbuf_peek(struct bpf_ringbuf *rb, void **sample, u32 *s
u64 cons_pos, prod_pos;
/* Synchronizes with smp_store_release() in user-space producer. */
- prod_pos = smp_load_acquire(&rb->producer_pos);
+ prod_pos = smp_load_acquire(rb->producer_pos);
if (prod_pos % 8)
return -EINVAL;
/* Synchronizes with smp_store_release() in __bpf_user_ringbuf_sample_release() */
- cons_pos = smp_load_acquire(&rb->consumer_pos);
+ cons_pos = smp_load_acquire(rb->consumer_pos);
if (cons_pos >= prod_pos)
return -ENODATA;
@@ -787,7 +794,7 @@ static int __bpf_user_ringbuf_peek(struct bpf_ringbuf *rb, void **sample, u32 *s
* Update the consumer pos, and return -EAGAIN so the caller
* knows to skip this sample and try to read the next one.
*/
- smp_store_release(&rb->consumer_pos, cons_pos + total_len);
+ smp_store_release(rb->consumer_pos, cons_pos + total_len);
return -EAGAIN;
}
@@ -809,9 +816,9 @@ static void __bpf_user_ringbuf_sample_release(struct bpf_ringbuf *rb, size_t siz
* prevents another task from writing to consumer_pos after it was read
* by this task with smp_load_acquire() in __bpf_user_ringbuf_peek().
*/
- consumer_pos = rb->consumer_pos;
+ consumer_pos = *rb->consumer_pos;
/* Synchronizes with smp_load_acquire() in user-space producer. */
- smp_store_release(&rb->consumer_pos, consumer_pos + rounded_size);
+ smp_store_release(rb->consumer_pos, consumer_pos + rounded_size);
}
BPF_CALL_4(bpf_user_ringbuf_drain, struct bpf_map *, map,
next prev parent reply other threads:[~2026-12-01 1:53 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
2026-12-01 1:51 ` Eric Curtin [this message]
2026-12-01 10:41 ` Lorenzo Stoakes (ARM)
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=20261201015240.1017259-10-ericcurtin17@gmail.com \
--to=ericcurtin17@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=ryan.roberts@arm.com \
--cc=song@kernel.org \
--cc=yonghong.song@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