BPF List
 help / color / mirror / Atom feed
From: Siddharth Nayyar <sidnayyar@google.com>
To: 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>,
	 Quentin Monnet <qmo@kernel.org>, Shuah Khan <shuah@kernel.org>
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org,
	"Anton Protopopov" <a.s.protopopov@gmail.com>,
	"Giuliano Procida" <gprocida@google.com>,
	"Neill Kapron" <nkapron@google.com>,
	"Thiébaud Weksteen" <tweek@google.com>,
	"Maxime Ripard" <mripard@kernel.org>,
	"Siddharth Nayyar" <sidnayyar@google.com>,
	"Alexei Starovoitov" <ast@kernel.org>
Subject: [PATCH bpf-next v2 0/4] bpf: Support struct_ops in light skeletons
Date: Mon, 30 Nov 2026 21:44:45 +0000	[thread overview]
Message-ID: <20261130-lskel-struct-ops-v2-0-8b76e65073ab@google.com> (raw)

BPF light skeletons (bpftool gen skeleton -L) compile BPF object loading
into a self-contained BPF_PROG_TYPE_SYSCALL loader program (gen_loader)
embedded in a C header, removing any runtime dependency on libbpf or
userspace BTF parsing. Currently, light skeletons do not support
BPF_MAP_TYPE_STRUCT_OPS maps or BPF_PROG_TYPE_STRUCT_OPS programs
because preparing a struct_ops map normally relies on libbpf parsing the
target vmlinux or kernel module BTF in userspace to resolve type IDs,
struct sizes, member byte offsets, and member indices.

Since the kernel already has access to both the BPF object's BTF (loaded
by the light skeleton via btf_fd) and vmlinux/module BTFs, the loader
can pass static object-BTF metadata known at skeleton generation time
and let the kernel match the target struct_ops type and members by name.
This avoids adding a new BPF helper or kfunc, eliminates runtime BTF
queries and module BTF FD management from the loader program, and avoids
pre-allocating a fixed-size kernel value buffer in the loader blob.

This series adds BPF_MAP_TYPE_STRUCT_OPS support for link-based
(SEC(".struct_ops.link")) maps in light skeletons across both vmlinux
and kernel module BTF targets:

- Patch 1 teaches the kernel to resolve BPF_MAP_TYPE_STRUCT_OPS maps and
  BPF_PROG_TYPE_STRUCT_OPS attach targets from the BPF object's BTF by
  name (stripping any CO-RE ___flavor suffix). BPF_MAP_CREATE accepts a
  local struct btf_value_type_id and value_size, BPF_PROG_LOAD accepts
  the object BTF FD in attach_btf_obj_fd with the local struct ID and
  member index, and BPF_MAP_UPDATE_ELEM validates and copies the local
  struct layout into the kernel struct_ops value buffer.
- Patch 2 updates libbpf's gen_loader to record local struct_ops type
  IDs and member indices, retain btf_value_type_id on BPF_MAP_CREATE,
  pass the object btf_fd in attach_btf_obj_fd on BPF_PROG_LOAD, and emit
  bpf_gen__struct_ops_update_elem() to populate callback FDs and issue
  BPF_MAP_UPDATE_ELEM.
- Patch 3 teaches bpftool to generate per-map skel_link_create() attach
  functions and skel__attach() / skel__detach() plumbing for BPF_F_LINK
  struct_ops maps in light skeletons, skips gen_st_ops_shadow() so the
  light skeleton ctx layout is preserved, and rejects non-link
  struct_ops maps.
- Patch 4 adds light skeleton struct_ops selftests covering both vmlinux
  (tcp_congestion_ops) and bpf_testmod (bpf_testmod_ops, including a
  reordered subset struct bpf_testmod_ops___v2 with a zeroed member
  absent from the kernel, shared callbacks, scalar fields, detach, and
  per-map re-attachment).

Non-link struct_ops support
===========================
Non-link struct_ops maps (SEC(".struct_ops") without BPF_F_LINK) remain
rejected in light skeletons for now. Because BPF_MAP_UPDATE_ELEM runs
inside the loader program during skel__load(), a non-link struct_ops map
would register and activate callbacks before skel__attach() is called,
prevent re-attaching after detach, and leak an active registration if a
later loader step fails. Supporting non-link struct_ops can be added as
follow-up work by deferring BPF_MAP_UPDATE_ELEM to userspace
skel__attach().

AI assistance and testing
=========================
This series (code, selftests, commit messages, and cover letter) was
developed interactively with an AI coding assistant (Google Gemini),
with me guiding the design step by step across each patch and reviewing
all changes.

The series was built with clang/LLVM and tested in a QEMU x86_64 VM
using test_progs (covering the new struct_ops_lskel selftests for both
vmlinux tcp_congestion_ops and bpf_testmod_ops, as well as existing
struct_ops and light skeleton tests).

Signed-off-by: Siddharth Nayyar <sidnayyar@google.com>
---
Changes in v2:
- Replaced the BPF helper approach from v1 with kernel-side object-BTF
  name matching as suggested by Alexei Starovoitov, replacing patches
  1-5 with two patches (kernel and libbpf).
- Added kernel validation for local member BTF kind/func-ptr matching,
  non-zero padding holes, and non-kernel btf_fd, and synced
  st_ops->progs from st_ops->data in libbpf.
- Updated bpftool light skeleton skel__detach() to zero detached
  struct_ops link FDs.
- Updated struct_ops_lskel selftests to assert detached link FDs are
  zeroed and to test skipping a zeroed field absent from the kernel.
- Dropped the RFC prefix.
- Link to v1: https://lore.kernel.org/r/20261106-lskel-struct-ops-v1-0-a4486ffc79cb@google.com

---
Siddharth Nayyar (4):
      bpf: Support struct_ops with object BTF
      libbpf: Support struct_ops in light skeleton loader
      bpftool: Generate attach and detach for struct_ops in light skeletons
      selftests/bpf: Add light skeleton struct_ops tests

 include/linux/bpf.h                                |  14 ++
 include/uapi/linux/bpf.h                           |  17 +-
 kernel/bpf/bpf_struct_ops.c                        | 236 ++++++++++++++++++++-
 kernel/bpf/syscall.c                               |  14 +-
 tools/bpf/bpftool/gen.c                            |  72 ++++++-
 tools/include/uapi/linux/bpf.h                     |  17 +-
 tools/lib/bpf/bpf_gen_internal.h                   |   3 +
 tools/lib/bpf/gen_loader.c                         |  35 +++
 tools/lib/bpf/libbpf.c                             | 110 +++++++++-
 tools/testing/selftests/bpf/Makefile.skel          |   3 +-
 .../selftests/bpf/prog_tests/struct_ops_lskel.c    | 122 +++++++++++
 tools/testing/selftests/bpf/progs/struct_ops_mod.c |  52 +++++
 .../selftests/bpf/progs/struct_ops_vmlinux.c       |  42 ++++
 13 files changed, 715 insertions(+), 22 deletions(-)
---
base-commit: b7a509bd447085118d03dd962d2cca308e283b57
change-id: 20261102-lskel-struct-ops-03090424cfa2

Best regards,
-- 
Siddharth Nayyar <sidnayyar@google.com>


             reply	other threads:[~2026-11-30 21:45 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 21:44 Siddharth Nayyar [this message]
2026-11-30 21:44 ` [PATCH bpf-next v2 1/4] bpf: Support struct_ops with object BTF Siddharth Nayyar
2026-11-30 21:44 ` [PATCH bpf-next v2 2/4] libbpf: Support struct_ops in light skeleton loader Siddharth Nayyar
2026-11-30 21:44 ` [PATCH bpf-next v2 3/4] bpftool: Generate attach and detach for struct_ops in light skeletons Siddharth Nayyar
2026-11-30 21:44 ` [PATCH bpf-next v2 4/4] selftests/bpf: Add light skeleton struct_ops tests Siddharth Nayyar

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=20261130-lskel-struct-ops-v2-0-8b76e65073ab@google.com \
    --to=sidnayyar@google.com \
    --cc=a.s.protopopov@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=gprocida@google.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=mripard@kernel.org \
    --cc=nkapron@google.com \
    --cc=qmo@kernel.org \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=tweek@google.com \
    --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