Skip to contentSkip to conversation

selftests/bpf: Add BPF struct_ops + livepatch integration test

PATCH In review Song Liu 3 participants 10 messages Apr 8 – Apr 16

Subsystems (5)
lore
Share & tools
lore

Song Liu

author maintainer
#0 lore raw

PATCH 1/1 selftests/bpf: Add BPF struct_ops + livepatch integration test

Add a selftest that demonstrates BPF struct_ops controlling kernel behavior through livepatch. A kernel module (test_klp_bpf) livepatches cmdline_proc_show() and delegates output to a BPF struct_ops callback. The BPF program writes a custom string to /proc/cmdline via the bpf_klp_seq_write kfunc.

Components:
- test_kmods/test_klp_bpf.c: kernel module that registers a klp_bpf_cmdline_ops struct_ops type, livepatches cmdline_proc_show, and provides bpf_klp_seq_write kfunc for seq_file writes
- progs/test_klp_bpf.c: BPF struct_ops program implementing set_cmdline callback
- prog_tests/test_klp_bpf.c: userspace test verifying /proc/cmdline output with and without BPF struct_ops attached

Assisted-by Claude:claude-opus-4-6

---

This shows an alternative solution to the idea proposed in [1]. This approach doesn't need any kernel change.

[1] https://lore.kernel.org/bpf/[email protected]/

---
 tools/testing/selftests/bpf/Makefile          |   2 +-
 .../selftests/bpf/prog_tests/test_klp_bpf.c   | 109 ++++++++++++
 .../selftests/bpf/progs/test_klp_bpf.c        |  26 +++
 .../testing/selftests/bpf/test_kmods/Makefile |   3 +-
 .../selftests/bpf/test_kmods/test_klp_bpf.c   | 166 ++++++++++++++++++
 .../selftests/bpf/test_kmods/test_klp_bpf.h   |  12 ++
 6 files changed, 316 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_klp_bpf.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_klp_bpf.c
 create mode 100644 tools/testing/selftests/bpf/test_kmods/test_klp_bpf.c
 create mode 100644 tools/testing/selftests/bpf/test_kmods/test_klp_bpf.h
tools/testing/selftests/bpf/Makefile +1 -1
index f75c4f52c028..22ef1f76196d 100644
@@ -121,7 +121,7 @@ TEST_PROGS_EXTENDED := \
 	ima_setup.sh verify_sig_setup.sh
 
 TEST_KMODS := bpf_testmod.ko bpf_test_no_cfi.ko bpf_test_modorder_x.ko \
-	bpf_test_modorder_y.ko bpf_test_rqspinlock.ko
+	bpf_test_modorder_y.ko bpf_test_rqspinlock.ko test_klp_bpf.ko
 TEST_KMOD_TARGETS = $(addprefix $(OUTPUT)/,$(TEST_KMODS))
 
 # Compile but not part of 'make run_tests'
tools/testing/selftests/bpf/prog_tests/test_klp_bpf.c new file +109 -0

109 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_klp_bpf.c new file +26 -0
new file mode 100644
index 000000000000..f3b68232231a
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "../test_kmods/test_klp_bpf.h"
+
+char _license[] SEC("license") = "GPL";
+
+extern void bpf_klp_seq_write(struct seq_file *m,
+			      const char *data, u32 data__sz) __ksym;
+
+SEC("struct_ops/set_cmdline")
+int BPF_PROG(set_cmdline, struct seq_file *m)
+{
+	char custom[] = "klp_bpf: custom cmdline\n";
+
+	bpf_klp_seq_write(m, custom, sizeof(custom) - 1);
+	return 0;
+}
+
+SEC(".struct_ops.link")
+struct klp_bpf_cmdline_ops cmdline_ops = {
+	.set_cmdline = (void *)set_cmdline,
+};
tools/testing/selftests/bpf/test_kmods/Makefile +2 -1
index 63c4d3f6a12f..bb182d668277 100644
@@ -8,11 +8,12 @@ Q = @
 endif
 
 MODULES = bpf_testmod.ko bpf_test_no_cfi.ko bpf_test_modorder_x.ko \
-	bpf_test_modorder_y.ko bpf_test_rqspinlock.ko
+	bpf_test_modorder_y.ko bpf_test_rqspinlock.ko test_klp_bpf.ko
 
 $(foreach m,$(MODULES),$(eval obj-m += $(m:.ko=.o)))
 
 CFLAGS_bpf_testmod.o = -I$(src)
+CFLAGS_test_klp_bpf.o = -I$(src)
 
 all:
 	$(Q)$(MAKE) -C $(KDIR) M=$(TEST_KMOD_DIR) modules
tools/testing/selftests/bpf/test_kmods/test_klp_bpf.c new file +166 -0

166 lines not on this page · raw message

tools/testing/selftests/bpf/test_kmods/test_klp_bpf.h new file +12 -0
new file mode 100644
index 000000000000..0ead3681fb89
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#ifndef _TEST_KLP_BPF_H
+#define _TEST_KLP_BPF_H
+
+struct seq_file;
+
+struct klp_bpf_cmdline_ops {
+	int (*set_cmdline)(struct seq_file *m);
+};
+
+#endif /* _TEST_KLP_BPF_H */
Signature · 1 line
2.52.0
#1 lore raw
Quoted 7 lines · Song Liu

Add a selftest that demonstrates BPF struct_ops controlling kernel behavior through livepatch. A kernel module (test_klp_bpf) livepatches cmdline_proc_show() and delegates output to a BPF struct_ops callback. The BPF program writes a custom string to /proc/cmdline via the bpf_klp_seq_write kfunc.

Quoted 8 lines

Components:
- test_kmods/test_klp_bpf.c: kernel module that registers a klp_bpf_cmdline_ops struct_ops type, livepatches cmdline_proc_show, and provides bpf_klp_seq_write kfunc for seq_file writes
- progs/test_klp_bpf.c: BPF struct_ops program implementing set_cmdline callback
- prog_tests/test_klp_bpf.c: userspace test verifying /proc/cmdline output with and without BPF struct_ops attached

Cool idea! I just checked it out, and it works perfectly.

[...]

Quoted 143 lines
tools/testing/selftests/bpf/test_kmods/test_klp_bpf.c new file +140 -0
@@ -0,0 +1,166 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/btf_ids.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/livepatch.h>
+#include <linux/seq_file.h>
+#include <linux/bpf_verifier.h>
+#include "test_klp_bpf.h"
+
+static struct klp_bpf_cmdline_ops *active_ops;
+
+/* --- kfunc: allow BPF struct_ops programs to write to seq_file --- */
+
+__bpf_kfunc_start_defs();
+
+__bpf_kfunc void bpf_klp_seq_write(struct seq_file *m,
+                                   const char *data, u32 data__sz)
+{
+       seq_write(m, data, data__sz);
+}
+
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(klp_bpf_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_klp_seq_write)
+BTF_KFUNCS_END(klp_bpf_kfunc_ids)
+
+static const struct btf_kfunc_id_set klp_bpf_kfunc_set = {
+       .owner = THIS_MODULE,
+       .set   = &klp_bpf_kfunc_ids,
+};
+
+/* --- Livepatch replacement for cmdline_proc_show --- */
+
+static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
+{
+       struct klp_bpf_cmdline_ops *ops = READ_ONCE(active_ops);
+
+       if (ops && ops->set_cmdline)
+               return ops->set_cmdline(m);
+
+       seq_printf(m, "%s: no struct_ops attached\n", THIS_MODULE->name);
+       return 0;
+}
+
+static struct klp_func funcs[] = {
+       {
+               .old_name = "cmdline_proc_show",
+               .new_func = livepatch_cmdline_proc_show,
+       }, { }
+};
+
+static struct klp_object objs[] = {
+       {
+               /* name being NULL means vmlinux */
+               .funcs = funcs,
+       }, { }
+};
+
+static struct klp_patch patch = {
+       .mod = THIS_MODULE,
+       .objs = objs,
+};
+
+/* --- struct_ops registration --- */
+
+static int klp_bpf_cmdline_reg(void *kdata, struct bpf_link *link)
+{
+       struct klp_bpf_cmdline_ops *ops = kdata;
+
+       if (cmpxchg(&active_ops, NULL, ops))
+               return -EBUSY;
+
+       return 0;
+}
+
+static void klp_bpf_cmdline_unreg(void *kdata, struct bpf_link *link)
+{
+       WRITE_ONCE(active_ops, NULL);
+}
+
+static int klp_bpf_cmdline_init(struct btf *btf)
+{
+       return 0;
+}
+
+static int klp_bpf_cmdline_init_member(const struct btf_type *t,
+                                      const struct btf_member *member,
+                                      void *kdata, const void *udata)
+{
+       return 0;
+}
+
+static bool klp_bpf_cmdline_is_valid_access(int off, int size,
+                                           enum bpf_access_type type,
+                                           const struct bpf_prog *prog,
+                                           struct bpf_insn_access_aux *info)
+{
+       return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
+}
+
+static int klp_bpf_cmdline_btf_struct_access(struct bpf_verifier_log *log,
+                                            const struct bpf_reg_state *reg,
+                                            int off, int size)
+{
+       return -EACCES;
+}
+
+static const struct bpf_verifier_ops klp_bpf_cmdline_verifier_ops = {
+       .is_valid_access = klp_bpf_cmdline_is_valid_access,
+       .btf_struct_access = klp_bpf_cmdline_btf_struct_access,
+};
+
+/* CFI stubs */
+static int klp_bpf_cmdline__set_cmdline(struct seq_file *m)
+{
+       return 0;
+}
+
+static struct klp_bpf_cmdline_ops __bpf_klp_bpf_cmdline_ops = {
+       .set_cmdline = klp_bpf_cmdline__set_cmdline,
+};
+
+static struct bpf_struct_ops bpf_klp_bpf_cmdline_ops = {
+       .verifier_ops = &klp_bpf_cmdline_verifier_ops,
+       .init = klp_bpf_cmdline_init,
+       .init_member = klp_bpf_cmdline_init_member,
+       .reg = klp_bpf_cmdline_reg,
+       .unreg = klp_bpf_cmdline_unreg,
+       .cfi_stubs = &__bpf_klp_bpf_cmdline_ops,
+       .name = "klp_bpf_cmdline_ops",
+       .owner = THIS_MODULE,
+};
+

tools/testing/selftests/bpf/test_kmods/test_klp_bpf.c line 150

Quoted 10 lines
tools/testing/selftests/bpf/test_kmods/test_klp_bpf.c new file +10 -0
+/* --- Module init/exit --- */
+
+static int __init test_klp_bpf_init(void)
+{
+       int ret;
+
+       ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
+                                       &klp_bpf_kfunc_set);
+       ret = ret ?: register_bpf_struct_ops(&bpf_klp_bpf_cmdline_ops,
+                                            klp_bpf_cmdline_ops);

Is it possible to integrate this BTF registration directly into klp-build? It appears we might need to extend scripts/livepatch/init.c to provide the necessary support.

Signature · 2 lines
Regards
Yafang
author maintainer
#2 lore raw

On Sun, Apr 12, 2026 at 1:44 AM Yafang Shao <[email protected]> wrote:
[...]

Quoted 7 lines
Quoted 7 lines
(quoted diff) +7 -0
+
+/* --- Module init/exit --- */
+
+static int __init test_klp_bpf_init(void)
+{
+       int ret;
+
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+       ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
+                                       &klp_bpf_kfunc_set);
+       ret = ret ?: register_bpf_struct_ops(&bpf_klp_bpf_cmdline_ops,
+                                            klp_bpf_cmdline_ops);

Is it possible to integrate this BTF registration directly into klp-build? It appears we might need to extend scripts/livepatch/init.c to provide the necessary support.

Can you try the attached patch and see whether it works?

Thanks,
Song

scripts/livepatch/klp-build +47 -2
index 7b82c7503c2b..6201f99e4d05 100755
@@ -566,6 +566,11 @@ copy_orig_objects() {
 	done
 	xtrace_restore
 
+	# Save vmlinux for BTF base reference when generating module BTF
+	if [[ -v CONFIG_DEBUG_INFO_BTF_MODULES ]] && [[ -f "$OBJ/vmlinux" ]]; then
+		cp -f "$OBJ/vmlinux" "$ORIG_DIR/vmlinux"
+	fi
+
 	mv -f "$TMP_DIR/build.log" "$ORIG_DIR"
 	touch "$TIMESTAMP"
 }
@@ -715,6 +720,36 @@ diff_checksums() {
 	done
 }
 
+# Regenerate BTF for the livepatch module
+gen_module_btf() {
+	local kmod_file="$1"
+
+	# Use the original (pre-patch) vmlinux as BTF base, since the module
+	# will be loaded on a kernel running that vmlinux.  The patched vmlinux
+	# (currently at $OBJ/vmlinux) has different BTF string offsets.
+	local btf_base="$ORIG_DIR/vmlinux"
+
+	[[ ! -f "$btf_base" ]] && { warn "original vmlinux not found, skipping BTF"; return; }
+
+	# gen-btf.sh is a subprocess and needs these variables exported.
+	# Query tool paths and flags from kbuild rather than hardcoding
+	# them, since they vary with LLVM= and CROSS_COMPILE=.
+	local pahole objcopy pahole_flags
+	pahole="$(cd "$SRC" && make -s --no-print-directory \
+		--eval='print-var: ; @echo $(PAHOLE)' print-var 2>/dev/null)"
+	objcopy="$(cd "$SRC" && make -s --no-print-directory \
+		--eval='print-var: ; @echo $(OBJCOPY)' print-var 2>/dev/null)"
+	pahole_flags="$(cd "$SRC" && make -s --no-print-directory \
+		--eval='print-var: ; @echo $(PAHOLE_FLAGS)' print-var 2>/dev/null)"
+
+	PAHOLE="$pahole" PAHOLE_FLAGS="$pahole_flags" OBJCOPY="$objcopy"	\
+	RESOLVE_BTFIDS="$OBJ/tools/bpf/resolve_btfids/resolve_btfids"		\
+	RESOLVE_BTFIDS_FLAGS="--distill_base"					\
+	objtree="$OBJ" KBUILD_VERBOSE=""					\
+		"$SRC/scripts/gen-btf.sh" --btf_base "$btf_base" "$kmod_file"	\
+		|| die "BTF generation failed"
+}
+
 # Build and post-process livepatch module in $KMOD_DIR
 build_patch_module() {
 	local makefile="$KMOD_DIR/Kbuild"
@@ -781,12 +816,22 @@ build_patch_module() {
 	# Save off the intermediate binary for debugging
 	cp -f "$kmod_file" "$kmod_file.orig"
 
-	# Work around issue where slight .config change makes corrupt BTF
-	objcopy --remove-section=.BTF "$kmod_file"
+	# Remove BTF sections from the kbuild M= build.  They were generated
+	# against the patched vmlinux and will be regenerated below against
+	# the original vmlinux.
+	objcopy --remove-section=.BTF		\
+		--remove-section=.BTF.base	\
+		--remove-section=.BTF_ids	\
+		"$kmod_file"
 
 	# Fix (and work around) linker wreckage for klp syms / relocs
 	"$SRC/tools/objtool/objtool" klp post-link "$kmod_file" || die "objtool klp post-link failed"
 
+	# Regenerate BTF after post-link
+	if [[ -v CONFIG_DEBUG_INFO_BTF_MODULES ]]; then
+		gen_module_btf "$kmod_file"
+	fi
+
 	cp -f "$kmod_file" "$OUTFILE"
 }
 
#3 lore raw
Quoted 19 lines · Song Liu

On Sun, Apr 12, 2026 at 1:44 AM Yafang Shao <[email protected]> wrote:
[...]

Quoted 7 lines
Quoted 7 lines
(quoted diff) +7 -0
+
+/* --- Module init/exit --- */
+
+static int __init test_klp_bpf_init(void)
+{
+       int ret;
+
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+       ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
+                                       &klp_bpf_kfunc_set);
+       ret = ret ?: register_bpf_struct_ops(&bpf_klp_bpf_cmdline_ops,
+                                            klp_bpf_cmdline_ops);

Is it possible to integrate this BTF registration directly into klp-build? It appears we might need to extend scripts/livepatch/init.c to provide the necessary support.

Quoted 1 line

Can you try the attached patch and see whether it works?

While your klp-build.patch generates the necessary BTF for the livepatch, it doesn't actually handle the registration of specific struct_ops like klp_bpf_cmdline_ops, does it?

Consequently, this struct_ops approach seems to require a hand-written kernel module rather than a patch-generated one, as we can't trigger register_bpf_struct_ops() from a .patch file without modifying scripts/livepatch/init.c.

The primary motivation for staying with the .patch workflow is the
scope: a standard kernel module is restricted to exported symbols, whereas a patch-generated module grants us access to non-exported functions. Am I missing any alternative workarounds?

Signature · 2 lines
Regards
Yafang
author maintainer
#4 lore raw
Quoted 32 lines · Yafang Shao
Quoted 19 lines · Song Liu

On Sun, Apr 12, 2026 at 1:44 AM Yafang Shao <[email protected]> wrote:
[...]

Quoted 7 lines
Quoted 7 lines
(quoted diff) +7 -0
+
+/* --- Module init/exit --- */
+
+static int __init test_klp_bpf_init(void)
+{
+       int ret;
+
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+       ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
+                                       &klp_bpf_kfunc_set);
+       ret = ret ?: register_bpf_struct_ops(&bpf_klp_bpf_cmdline_ops,
+                                            klp_bpf_cmdline_ops);

Is it possible to integrate this BTF registration directly into klp-build? It appears we might need to extend scripts/livepatch/init.c to provide the necessary support.

Quoted 1 line

Can you try the attached patch and see whether it works?

While your klp-build.patch generates the necessary BTF for the livepatch, it doesn't actually handle the registration of specific struct_ops like klp_bpf_cmdline_ops, does it?

Consequently, this struct_ops approach seems to require a hand-written kernel module rather than a patch-generated one, as we can't trigger register_bpf_struct_ops() from a .patch file without modifying scripts/livepatch/init.c.

Quoted 4 lines

The primary motivation for staying with the .patch workflow is the
scope: a standard kernel module is restricted to exported symbols, whereas a patch-generated module grants us access to non-exported functions. Am I missing any alternative workarounds?

There are other ways to access non-exported symbols. :) But I agree that binary diff based solutions (klp-build, kpatch-build) have their advantages.

0001-objtool-klp-Build-proper-BTF-for-the-livepatch-modul.patch (attached) seems to work for this. It is not very clean though.

I tested it with 0001-test-klp-build-btf.patch (also attached) as:

./scripts/livepatch/klp-build -e scripts/livepatch/test_klp_bpf_ops.c 0001-test-klp-build-btf.patch

Does this work for your use cases?

Thanks,
Song

From bad2de891014de41cc11646501519d01fff20074 Mon Sep 17 00:00:00 2001
From: Song Liu <[email protected]>
Date: Mon, 13 Apr 2026 14:53:56 -0700
Subject: [PATCH] objtool/klp: Build proper BTF for the livepatch module

---
 scripts/livepatch/init.c    | 16 +++++++
 scripts/livepatch/klp-build | 87 +++++++++++++++++++++++++++++++++----
 2 files changed, 95 insertions(+), 8 deletions(-)
scripts/livepatch/init.c +16 -0
index f14d8c8fb35f..2fb07fc86dd4 100644
@@ -6,9 +6,18 @@
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/kernel.h>
+#include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/livepatch.h>
 
+/*
+ * Optional module init hook for extra source files (--extra-mod).
+ * Override this to register kfuncs, struct_ops, etc.  The 'mod' argument
+ * is the livepatch module (THIS_MODULE from init.c).
+ */
+extern int __klp_mod_init(struct module *mod);
+int __weak __klp_mod_init(struct module *mod) { return 0; }
+
 static struct klp_patch *patch;
 
 static int __init livepatch_mod_init(void)
@@ -78,8 +87,15 @@ static int __init livepatch_mod_init(void)
 	patch->replace = true;
 #endif
 
+	ret = __klp_mod_init(THIS_MODULE);
+	if (ret)
+		goto err_free_funcs;
+
 	return klp_enable_patch(patch);
 
+err_free_funcs:
+	klp_for_each_object_static(patch, objs)
+		kfree(objs->funcs);
 err_free_objs:
 	kfree(objs);
 err_free_patch:
scripts/livepatch/klp-build +79 -8

166 lines not on this page · raw message

Signature · 1 line
2.52.0

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Song Liu <[email protected]>
Date: Mon, 14 Apr 2026 00:00:00 -0700
Subject: [PATCH] test-klp-bpf

Livepatch cmdline_proc_show to delegate to BPF struct_ops. Use with: klp-build -e scripts/livepatch/test_klp_bpf_ops.c
---

fs/proc/cmdline.c +12 -2
index a6f76121955f..c672ead00ff4 100644
@@ -5,10 +5,20 @@
 #include <linux/seq_file.h>
 #include "internal.h"
 
+struct klp_bpf_cmdline_ops {
+	int (*set_cmdline)(struct seq_file *m);
+};
+
+struct klp_bpf_cmdline_ops *active_ops;
+
 static int cmdline_proc_show(struct seq_file *m, void *v)
 {
-	seq_puts(m, saved_command_line);
-	seq_putc(m, '\n');
+	struct klp_bpf_cmdline_ops *ops = READ_ONCE(active_ops);
+
+	if (ops && ops->set_cmdline)
+		return ops->set_cmdline(m);
+
+	seq_puts(m, "klp_bpf: no struct_ops attached\n");
 	return 0;
 }
 
scripts/livepatch/test_klp_bpf_ops.c new file +129 -0

129 lines not on this page · raw message

Signature · 1 line
2.52.0
#5 lore raw
Quoted 43 lines · Song Liu
Quoted 32 lines · Yafang Shao
Quoted 19 lines · Song Liu

On Sun, Apr 12, 2026 at 1:44 AM Yafang Shao <[email protected]> wrote:
[...]

Quoted 7 lines
Quoted 7 lines
scripts/livepatch/test_klp_bpf_ops.c new file +7 -0
+
+/* --- Module init/exit --- */
+
+static int __init test_klp_bpf_init(void)
+{
+       int ret;
+
Quoted 8 lines

scripts/livepatch/test_klp_bpf_ops.c line 127

Quoted 4 lines
scripts/livepatch/test_klp_bpf_ops.c new file +4 -0
+       ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
+                                       &klp_bpf_kfunc_set);
+       ret = ret ?: register_bpf_struct_ops(&bpf_klp_bpf_cmdline_ops,
+                                            klp_bpf_cmdline_ops);

Is it possible to integrate this BTF registration directly into klp-build? It appears we might need to extend scripts/livepatch/init.c to provide the necessary support.

Quoted 1 line

Can you try the attached patch and see whether it works?

While your klp-build.patch generates the necessary BTF for the livepatch, it doesn't actually handle the registration of specific struct_ops like klp_bpf_cmdline_ops, does it?

Consequently, this struct_ops approach seems to require a hand-written kernel module rather than a patch-generated one, as we can't trigger register_bpf_struct_ops() from a .patch file without modifying scripts/livepatch/init.c.

Quoted 4 lines

The primary motivation for staying with the .patch workflow is the
scope: a standard kernel module is restricted to exported symbols, whereas a patch-generated module grants us access to non-exported functions. Am I missing any alternative workarounds?

There are other ways to access non-exported symbols. :) But I agree that binary diff based solutions (klp-build, kpatch-build) have their advantages.

Quoted 7 lines

0001-objtool-klp-Build-proper-BTF-for-the-livepatch-modul.patch (attached) seems to work for this. It is not very clean though.

I tested it with 0001-test-klp-build-btf.patch (also attached) as:

./scripts/livepatch/klp-build -e scripts/livepatch/test_klp_bpf_ops.c 0001-test-klp-build-btf.patch

I tested the case you provided, and it performed as expected.

Quoted 2 lines

Does this work for your use cases?

I haven't tested this with my specific use case yet, but I expect it will work as intended. I’ll set aside some time to verify it shortly.

Could you please submit an official patch?

--
Regards
Yafang

#6 lore raw
In reply to Song Liu
Quoted 25 lines · Song Liu

Add a selftest that demonstrates BPF struct_ops controlling kernel behavior through livepatch. A kernel module (test_klp_bpf) livepatches cmdline_proc_show() and delegates output to a BPF struct_ops callback. The BPF program writes a custom string to /proc/cmdline via the bpf_klp_seq_write kfunc.

Components:
- test_kmods/test_klp_bpf.c: kernel module that registers a klp_bpf_cmdline_ops struct_ops type, livepatches cmdline_proc_show, and provides bpf_klp_seq_write kfunc for seq_file writes
- progs/test_klp_bpf.c: BPF struct_ops program implementing set_cmdline callback
- prog_tests/test_klp_bpf.c: userspace test verifying /proc/cmdline output with and without BPF struct_ops attached

Assisted-by Claude:claude-opus-4-6

---

This shows an alternative solution to the idea proposed in [1]. This approach doesn't need any kernel change.

[1] https://lore.kernel.org/bpf/[email protected]/

Quoted 8 lines
---
 tools/testing/selftests/bpf/Makefile          |   2 +-
 .../selftests/bpf/prog_tests/test_klp_bpf.c   | 109 ++++++++++++
 .../selftests/bpf/progs/test_klp_bpf.c        |  26 +++
 .../testing/selftests/bpf/test_kmods/Makefile |   3 +-
 .../selftests/bpf/test_kmods/test_klp_bpf.c   | 166 ++++++++++++++++++
 .../selftests/bpf/test_kmods/test_klp_bpf.h   |  12 ++
 6 files changed, 316 insertions(+), 2 deletions(-)

It looks good, but takes 1.6 seconds on a non-debug kernel. This example can stay out of the tree.

author maintainer
#7 lore raw

On Sun, Apr 12, 2026 at 2:02 PM Alexei Starovoitov <[email protected]> wrote:
[...]

Quoted 14 lines
Quoted 5 lines

This shows an alternative solution to the idea proposed in [1]. This approach doesn't need any kernel change.

[1] https://lore.kernel.org/bpf/[email protected]/

Quoted 8 lines
---
 tools/testing/selftests/bpf/Makefile          |   2 +-
 .../selftests/bpf/prog_tests/test_klp_bpf.c   | 109 ++++++++++++
 .../selftests/bpf/progs/test_klp_bpf.c        |  26 +++
 .../testing/selftests/bpf/test_kmods/Makefile |   3 +-
 .../selftests/bpf/test_kmods/test_klp_bpf.c   | 166 ++++++++++++++++++
 .../selftests/bpf/test_kmods/test_klp_bpf.h   |  12 ++
 6 files changed, 316 insertions(+), 2 deletions(-)
Quoted 2 lines

It looks good, but takes 1.6 seconds on a non-debug kernel. This example can stay out of the tree.

Sure. This is not a typical self test to begin with.

Thanks,
Song

#8 lore raw
Quoted 18 lines · Song Liu

On Sun, Apr 12, 2026 at 2:02 PM Alexei Starovoitov <[email protected]> wrote:
[...]

Quoted 14 lines
Quoted 13 lines

This shows an alternative solution to the idea proposed in [1]. This approach doesn't need any kernel change.

[1] https://lore.kernel.org/bpf/[email protected]/

---
 tools/testing/selftests/bpf/Makefile          |   2 +-
 .../selftests/bpf/prog_tests/test_klp_bpf.c   | 109 ++++++++++++
 .../selftests/bpf/progs/test_klp_bpf.c        |  26 +++
 .../testing/selftests/bpf/test_kmods/Makefile |   3 +-
 .../selftests/bpf/test_kmods/test_klp_bpf.c   | 166 ++++++++++++++++++
 .../selftests/bpf/test_kmods/test_klp_bpf.h   |  12 ++
 6 files changed, 316 insertions(+), 2 deletions(-)
Quoted 4 lines
Quoted 2 lines

It looks good, but takes 1.6 seconds on a non-debug kernel. This example can stay out of the tree.

Sure. This is not a typical self test to begin with.

How about adding this test case to tools/testing/selftests/livepatch/test_modules/? It would serve as an excellent demonstration of how livepatching can be leveraged for more creative kernel extensions.

Signature · 2 lines
Regards
Yafang
author maintainer
#9 lore raw
Quoted 23 lines · Yafang Shao
Quoted 21 lines · Song Liu

On Sun, Apr 12, 2026 at 2:02 PM Alexei Starovoitov <[email protected]> wrote:
[...]

Quoted 16 lines
Quoted 5 lines

This shows an alternative solution to the idea proposed in [1]. This approach doesn't need any kernel change.

[1] https://lore.kernel.org/bpf/[email protected]/

Quoted 8 lines
---
 tools/testing/selftests/bpf/Makefile          |   2 +-
 .../selftests/bpf/prog_tests/test_klp_bpf.c   | 109 ++++++++++++
 .../selftests/bpf/progs/test_klp_bpf.c        |  26 +++
 .../testing/selftests/bpf/test_kmods/Makefile |   3 +-
 .../selftests/bpf/test_kmods/test_klp_bpf.c   | 166 ++++++++++++++++++
 .../selftests/bpf/test_kmods/test_klp_bpf.h   |  12 ++
 6 files changed, 316 insertions(+), 2 deletions(-)

It looks good, but takes 1.6 seconds on a non-debug kernel. This example can stay out of the tree.

Quoted 6 lines
Quoted 1 line

Sure. This is not a typical self test to begin with.

How about adding this test case to tools/testing/selftests/livepatch/test_modules/? It would serve as an excellent demonstration of how livepatching can be leveraged for more creative kernel extensions.

I don't know whether we want BPF heavy code in selftests/livepatch/. Maybe we can just add the livepatch module to samples/livepatch/.

Thanks,
Song