From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 50CA8606A325 for ; Tue, 1 Dec 2026 02:53:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=PG3Qekuh; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=YMHhN4Yg DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=SJqtR3AnCNlZn8uSwnowsuGmoXoDGblAxeqUoGnKkJo=; b=PG3QekuhGqcUIS f7UsmVJtlHCokXzyNNgqss0FT6lZXYc+E9qnrVwDNVaylfLUo96I56pBJTR1WGiiiJWNbQmbgizg0 TcG3rbXHNFtq0bH9VVYS+B74s3W98NX3kWW7AW3OOnm0j5jng4mxgpswlW/Co5jai4XCg9ZmNxLE4 XVHBpmIg6TcFWdlvHZFAYw+o5iILUVCBshjGG+Spetmm6yv19Z6KgiCphuL0jo2tBMoF7Htl+iY54 n2SDjmjX0NXoCnpCQIUXnBQAp9Xu1O0gkVnI3CkM1ccaDFQv4G7nbCt4hKiE1ahJ1KxCin8UBGIpr fJD5FjEGCuDPUYodQnzw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYD4A-00000006E8L-0ox5; Tue, 01 Dec 2026 01:53:58 +0000 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYD3H-00000006DNM-3ARa for linux-arm-kernel@lists.infradead.org; Tue, 01 Dec 2026 01:53:05 +0000 Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-4bb6ba3b244so115011805e9.3 for ; Mon, 30 Nov 2026 17:53:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796089981; x=1796694781; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=SJqtR3AnCNlZn8uSwnowsuGmoXoDGblAxeqUoGnKkJo=; b=YMHhN4Yg0LIz2oki0k+BFGQATywfUUXgeGvq+k4xXgcLVkF3fLoynZwynPUQSCzmu1 QWSL5sWySlapQhZ3+RzImioiF0vN/KPEgVKhtXCq/nvpjdp/uTahrlwm/MLAUWAKQarF PYatwOz1AH7he+zxq//r++0yRH/QRCaiODwPqFtic1jl4SxVj7eVA5sfd+D+iprOE4Et vna2PsX6T/7eOwpMJOVoQcf7t43FMcZNRGDsIownS0pq5fC5CWpn6sRmLP7ufdD62nHo hw8ECmQ35MWpYE3f8uV8sSz18/pF5B8aVJ1a/Tm0R5/M1GfUrilZxHMoiNA2gTIT187v 0/6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796089981; x=1796694781; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=SJqtR3AnCNlZn8uSwnowsuGmoXoDGblAxeqUo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MwL9MACc8JE289eET/u6cEmMszKNYm2XB SOFueq8BXaPIxKZelsmFOHBL99qqOvjmqTdvVA== X-Received: by 2002:a05:600c:a08e:b0:4bb:647d:dabe with SMTP id 5b1f17b1804b1-4bb887c3afamr750953175e9.3.1796089980734; Mon, 30 Nov 2026 17:53:00 -0800 (PST) Received: from NULL.Home.net ([2001:8a0:7280:4000:12d:c3b9:cecc:8966]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb884911e3sm1199037055e9.6.2026.11.30.17.52.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 17:53:00 -0800 (PST) From: Eric Curtin To: linux-arm-kernel@lists.infradead.org Cc: Ryan Roberts , Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , 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 Message-ID: <20261201015240.1017259-10-ericcurtin17@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261201015240.1017259-1-ericcurtin17@gmail.com> References: <20261201015240.1017259-1-ericcurtin17@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261130_175303_878065_340D0568 X-CRM114-Status: GOOD ( 24.26 ) X-Spam-Score: -1.9 (-) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Ryan Roberts 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 Signed-off-by: Eric Curtin (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,