All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	Tejun Heo <tj@kernel.org>,
	kkd@meta.com, kernel-team@meta.com
Subject: [PATCH bpf-next v1 00/10] Fix some arena paper cuts
Date: Mon, 12 Oct 2026 15:36:57 +0200	[thread overview]
Message-ID: <20261012133711.4030594-1-memxor@gmail.com> (raw)

Kernel code that shares arena memory with BPF programs runs into two
paper cuts, both visible in sched_ext's cid-form schedulers today.

A kernel struct cannot publish an arena pointer: a pointer member reaches
the program typed as a kernel pointer, so sched_ext hands out a plain u64
holding a pre-translated arena address and every program casts it before
use. __arena and __nullable become BTF type tags like __user and __rcu.
A load through an __arena member of a kernel struct yields PTR_TO_ARENA:
the member holds the kernel address and the fixup pass appends the
conversion to the arena pointer form that the struct_ops trampoline
applies to __arena arguments, with a NULL test for __nullable members.
A __nullable member on its own keeps PTR_MAYBE_NULL on trusted and RCU
pointers, where NULL would otherwise be assumed away. libbpf renders the
arena tag with its address space, so the member arrives in a program
built against vmlinux.h ready to use, without a cast.

A program only has an arena when one of its insns refers to the arena
map, yet the kernel hands programs arena pointers through struct_ops
arguments, tagged members and kfunc return values, and a struct_ops
callback that touches nothing but its arguments fails verification until
it refers to the arena artificially. libbpf binds the arena of an object
to every program loaded from it, through a continuous fd_array. So that
programs which never touch the arena pay nothing for it, the verifier
records which functions have insns that work with the arena base and the
JITs set it up only for those.

Tested with sched_ext for-next as well: a typed cmask member next to the
existing cmask_arena_addr in struct scx_enable_args passes the
enable_cmask selftest read straight from the member, without a cast and
without the asm volatile arena reference.

See commit logs for details.

Kumar Kartikeya Dwivedi (10):
  compiler_types: Add __arena and __nullable BTF type tag qualifiers
  bpf: Recognize __nullable on pointer members of kernel structs
  bpf: Turn loads of __arena pointer members into arena pointers
  selftests/bpf: Test __nullable pointer members
  selftests/bpf: Test __arena pointer members
  libbpf: Bind the arena of an object to every program in it
  selftests/bpf: Test arena binding by libbpf
  bpf: Set up the arena base only in functions that use it
  libbpf: Render the arena type tag with its address space
  selftests/bpf: Test the address space rendering of the arena type tag

 arch/arm64/net/bpf_jit_comp.c                 |   6 +-
 arch/loongarch/net/bpf_jit.c                  |   6 +-
 arch/powerpc/net/bpf_jit_comp.c               |   6 +-
 arch/riscv/net/bpf_jit_core.c                 |   6 +-
 arch/s390/net/bpf_jit_comp.c                  |   7 +-
 arch/x86/net/bpf_jit_comp.c                   |  14 ++-
 include/linux/bpf.h                           |   2 +
 include/linux/bpf_verifier.h                  |  10 ++
 include/linux/compiler_types.h                |  10 ++
 include/linux/filter.h                        |  11 ++
 kernel/bpf/btf.c                              |  48 +++++++-
 kernel/bpf/fixups.c                           |  51 +++++++-
 kernel/bpf/verifier.c                         |  67 +++++++++++
 tools/lib/bpf/btf_dump.c                      |   3 +
 tools/lib/bpf/features.c                      |  40 +++++++
 tools/lib/bpf/libbpf.c                        |  37 ++++++
 tools/lib/bpf/libbpf_internal.h               |   2 +
 .../selftests/bpf/prog_tests/btf_dump.c       |  16 ++-
 .../bpf/prog_tests/test_struct_ops_arena.c    | 111 ++++++++++++++++++
 .../bpf/prog_tests/test_struct_ops_nullable.c |  43 +++++++
 .../selftests/bpf/prog_tests/verifier.c       |   2 +
 .../testing/selftests/bpf/progs/arena_kfunc.c |  10 --
 .../selftests/bpf/progs/arena_noarena.c       |  39 ++++++
 .../bpf/progs/struct_ops_arena_autobind.c     |  64 ++++++++++
 .../bpf/progs/struct_ops_arena_member.c       |  92 +++++++++++++++
 .../struct_ops_arena_member_fail__load_acq.c  |  38 ++++++
 .../struct_ops_arena_member_fail__mismatch.c  |  45 +++++++
 .../struct_ops_arena_member_fail__no_arena.c  |  23 ++++
 .../struct_ops_arena_member_fail__union.c     |  34 ++++++
 .../struct_ops_arena_member_fail__write.c     |  32 +++++
 .../progs/struct_ops_arena_member_render.c    |  82 +++++++++++++
 .../selftests/bpf/progs/struct_ops_nullable.c |  45 +++++++
 .../bpf/progs/struct_ops_nullable_fail.c      |  28 +++++
 .../bpf/progs/verifier_arena_scalar.c         |  13 --
 .../selftests/bpf/test_kmods/bpf_testmod.c    |  53 +++++++++
 .../selftests/bpf/test_kmods/bpf_testmod.h    |  46 ++++++++
 .../bpf/test_kmods/bpf_testmod_kfunc.h        |   2 +
 37 files changed, 1095 insertions(+), 49 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_nullable.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_noarena.c
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_arena_autobind.c
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_arena_member.c
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_arena_member_fail__load_acq.c
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_arena_member_fail__mismatch.c
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_arena_member_fail__no_arena.c
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_arena_member_fail__union.c
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_arena_member_fail__write.c
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_arena_member_render.c
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_nullable.c
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_nullable_fail.c


base-commit: 0d5e3fc78a6f2522e6339743b8e5ecc02351a9dd
-- 
2.53.0-Meta


             reply	other threads:[~2026-10-12 13:37 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-12 13:36 Kumar Kartikeya Dwivedi [this message]
2026-10-12 13:36 ` [PATCH bpf-next v1 01/10] compiler_types: Add __arena and __nullable BTF type tag qualifiers Kumar Kartikeya Dwivedi
2026-10-12 13:36 ` [PATCH bpf-next v1 02/10] bpf: Recognize __nullable on pointer members of kernel structs Kumar Kartikeya Dwivedi
2026-10-12 13:54   ` sashiko-bot
2026-10-12 13:37 ` [PATCH bpf-next v1 03/10] bpf: Turn loads of __arena pointer members into arena pointers Kumar Kartikeya Dwivedi
2026-10-12 14:00   ` sashiko-bot
2026-10-12 14:27   ` bot+bpf-ci
2026-10-12 13:37 ` [PATCH bpf-next v1 04/10] selftests/bpf: Test __nullable pointer members Kumar Kartikeya Dwivedi
2026-10-12 14:27   ` bot+bpf-ci
2026-10-12 13:37 ` [PATCH bpf-next v1 05/10] selftests/bpf: Test __arena " Kumar Kartikeya Dwivedi
2026-10-12 14:27   ` bot+bpf-ci
2026-10-12 13:37 ` [PATCH bpf-next v1 06/10] libbpf: Bind the arena of an object to every program in it Kumar Kartikeya Dwivedi
2026-10-12 14:27   ` bot+bpf-ci
2026-10-12 13:37 ` [PATCH bpf-next v1 07/10] selftests/bpf: Test arena binding by libbpf Kumar Kartikeya Dwivedi
2026-10-12 13:44   ` sashiko-bot
2026-10-12 14:27   ` bot+bpf-ci
2026-10-12 13:37 ` [PATCH bpf-next v1 08/10] bpf: Set up the arena base only in functions that use it Kumar Kartikeya Dwivedi
2026-10-12 14:27   ` bot+bpf-ci
2026-10-12 13:37 ` [PATCH bpf-next v1 09/10] libbpf: Render the arena type tag with its address space Kumar Kartikeya Dwivedi
2026-10-12 13:37 ` [PATCH bpf-next v1 10/10] selftests/bpf: Test the address space rendering of the arena type tag Kumar Kartikeya Dwivedi

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=20261012133711.4030594-1-memxor@gmail.com \
    --to=memxor@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=kernel-team@meta.com \
    --cc=kkd@meta.com \
    --cc=tj@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.