All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Alan Maguire" <almagui@kernel.org>, <daniel@iogearbox.net>,
	<andrii@kernel.org>
Cc: <eddyz87@gmail.com>, <memxor@gmail.com>, <martin.lau@linux.dev>,
	<song@kernel.org>, <yonghong.song@linux.dev>, <jolsa@kernel.org>,
	<emil@etsalapatis.com>, <ihor.solodrai@linux.dev>,
	<john.fastabend@gmail.com>, <bpf@vger.kernel.org>
Subject: Re: [PATCH bpf-next 0/7] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering
Date: Tue, 24 Nov 2026 00:50:27 +0000	[thread overview]
Message-ID: <DN311OXVTVFO.AKQ8VYM3W5EL@gmail.com> (raw)
In-Reply-To: <20261123223227.745345-1-almagui@kernel.org>

On Mon, Nov 23, 2026 at 02:32 PM Alan Maguire <almagui@kernel.org> wrote:
> By instead creating a structure with named members, we can
> bring a Compile Once, Run Everywhere experience to parameter
> specifications via a new BPF_PROG_CORE() macro.  This will
> better handle cases like the following:

I like the idea. It sounds similar to what Ihor propopsed in the other
thread.
But implementation is kinda heavy.

Here is an alternative
and it cover tp_btf, fentry, fexit, fmod_ret, fsession and lsm progs.

Let bpf prog describe the context as

  struct bpf_func_args {
          int dfd;
          struct filename *name;
          int __ret;
  };

in a split BTF on top of the BTF of the target.
It is the only candidate for 'struct bpf_func_args___xyz' of the prog.
The rest is ordinary CO-RE.
No changes in relo_core.[ch] and in bpf_tracing.h.
The kernel does the same for lskel.

The prog:

  struct bpf_func_args___unlink {
          struct filename *name;
          int dfd;
          int __ret;
  } __attribute__((preserve_access_index));

  SEC("fexit/filename_unlinkat")
  int handle(struct bpf_func_args___unlink *ctx)
  {
          if (bpf_core_field_exists(ctx->dfd))
                  bpf_printk("dfd %d", ctx->dfd);
          bpf_printk("%s %d", ctx->name->name, ctx->__ret);
          return 0;
  }

What goes away:

  the series                            with the real struct
  ------------------------------------  -------------------------------
  FUNC as a candidate, 3 cand flags     a struct candidate
  2 decl tags, marker member for GCC    the name of the struct
  ___flavor on members                  ___flavor on the struct
  compat rules for pointers             bpf_core_type_matches()
  TYPE_EXISTS special case              bpf_core_type_exists()
  scan of all types for every relo      strncmp()
  BPF_PROG_CORE() and friends           ctx->arg

bpf_core_type_matches() compares the names of the pointed-to structs.
It covers vma vs vma___new:

  struct bpf_func_args___new {
          struct mm_area *vma;
  } __attribute__((preserve_access_index));

  if (bpf_core_type_matches(struct bpf_func_args___new))

BPF_PROG_CORE() reads a missing arg as zero.
An old libbpf finds no candidate. Every arg is zero and the prog loads.
Patches 5 and 6 exist because of that.
ctx->arg without a guard is poisoned there and the load fails.

Big endian: a small arg is in the low bytes of its slot.
btf_ctx_access() rejects such offset.
I think the 'off % 8' check can go for scalars.
Then there is no need to rewrite loads in libbpf, and int args work.

Not in the diff: that verifier change and the selftest.
tp_btf on kernels older than 5.10 fails to load.

btf.c compiles with this. I didn't compile the libbpf half and didn't
run anything:

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 4e9a4d1..fb29ebf 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -3446,6 +3446,7 @@ int bpf_get_kfunc_addr(const struct bpf_prog *prog, u32 func_id,
 struct bpf_core_ctx {
 	struct bpf_verifier_log *log;
 	const struct btf *btf;
+	const struct bpf_prog *prog;
 };

 bool btf_nested_type_is_trusted(struct bpf_verifier_log *log,
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index c4386a9..c86ad9a 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -10709,11 +10709,124 @@ check_modules:
 	return populate_cand_cache(cands, module_cand_cache, MODULE_CAND_CACHE_SIZE);
 }

+#define BPF_FUNC_ARGS "bpf_func_args"
+
+/*
+ * The context of tp_btf, fentry, fexit, fmod_ret, fsession and lsm progs is
+ * an array of u64 slots: the arguments of the attach target, then its return
+ * value, see btf_ctx_arg_idx(). Describe it as
+ *
+ *	struct bpf_func_args {
+ *		<type of the parameter> <name of the parameter>;
+ *		...
+ *		<return type> __ret;
+ *	};
+ *
+ * in a split BTF on top of the BTF of the target. Then the relocations
+ * against 'struct bpf_func_args___xyz' of the prog are ordinary CO-RE.
+ */
+struct func_args_btf {
+	struct btf btf;
+	struct btf_type *types[1];
+	struct bpf_core_cand cand;
+	struct btf_type t;
+	struct btf_member m[MAX_BPF_FUNC_ARGS + 1];
+};
+
+static const char func_args_strs[] = BPF_FUNC_ARGS "\0__ret";
+
+static bool bpf_core_is_func_args(const struct btf *btf, const struct btf_type *t)
+{
+	const char *name = btf_name_by_offset(btf, t->name_off);
+	size_t len = sizeof(BPF_FUNC_ARGS) - 1;
+
+	return __btf_type_is_struct(t) && name &&
+	       bpf_core_essential_name_len(name) == len &&
+	       !strncmp(name, BPF_FUNC_ARGS, len);
+}
+
+static int func_args_add(struct func_args_btf *a, u32 name_off, u32 type_id)
+{
+	const struct btf_type *t = btf_type_skip_modifiers(&a->btf, type_id, NULL);
+	u32 sz = btf_type_is_ptr(t) ? 8 : t->size;
+	u32 n = btf_type_vlen(&a->t);
+
+	if (n == ARRAY_SIZE(a->m))
+		return -E2BIG;
+
+	a->m[n].name_off = name_off;
+	a->m[n].type = type_id;
+	a->m[n].offset = a->t.size * 8;
+	/* a small argument is in the low bytes of its slot */
+	if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) && sz < 8)
+		a->m[n].offset += (8 - sz) * 8;
+	a->t.size += roundup(sz, 8);
+	a->t.info = BTF_KIND_STRUCT << 24 | (n + 1);
+	return 0;
+}
+
+static struct func_args_btf *bpf_core_func_args_btf(const struct bpf_prog *prog)
+{
+	struct btf *targ_btf = prog->aux->attach_btf;
+	const struct btf_type *t = NULL;
+	const struct btf_param *p;
+	struct func_args_btf *a;
+	int i, first = 0, err = 0;
+	const char *tname;
+	char name[128];
+	s32 id;
+
+	if (targ_btf)
+		t = btf_type_by_id(targ_btf, prog->aux->attach_btf_id);
+	if (t && btf_type_is_typedef(t)) {
+		/* btf_trace_xyz has no names of parameters, __probestub_xyz() has */
+		tname = btf_name_by_offset(targ_btf, t->name_off);
+		if (strncmp(tname, "btf_trace_", 10))
+			return ERR_PTR(-EINVAL);
+		snprintf(name, sizeof(name), "__probestub_%s", tname + 10);
+		id = btf_find_by_name_kind(targ_btf, name, BTF_KIND_FUNC);
+		t = id > 0 ? btf_type_by_id(targ_btf, id) : NULL;
+		first = 1; /* void *__data is not in the context */
+	}
+	if (!t || !btf_type_is_func(t))
+		return ERR_PTR(-EINVAL);
+	t = btf_type_by_id(targ_btf, t->type);
+
+	a = kzalloc_obj(*a, GFP_KERNEL_ACCOUNT);
+	if (!a)
+		return ERR_PTR(-ENOMEM);
+
+	a->btf.base_btf = targ_btf;
+	a->btf.start_id = btf_nr_types(targ_btf);
+	a->btf.start_str_off = targ_btf->start_str_off + targ_btf->hdr.str_len;
+	a->btf.strings = func_args_strs;
+	a->btf.hdr.str_len = sizeof(func_args_strs);
+	a->btf.types = a->types;
+	a->btf.nr_types = 1;
+	a->types[0] = &a->t;
+	a->t.name_off = a->btf.start_str_off;
+	a->t.info = BTF_KIND_STRUCT << 24;
+	a->cand.btf = &a->btf;
+	a->cand.id = a->btf.start_id;
+
+	p = btf_params(t);
+	for (i = first; !err && i < btf_type_vlen(t); i++)
+		err = func_args_add(a, p[i].name_off, p[i].type);
+	if (!err && !first && t->type)
+		err = func_args_add(a, a->t.name_off + sizeof(BPF_FUNC_ARGS), t->type);
+	if (err) {
+		kfree(a);
+		return ERR_PTR(err);
+	}
+	return a;
+}
+
 int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
 		   int relo_idx, void *insn)
 {
 	bool need_cands = relo->kind != BPF_CORE_TYPE_ID_LOCAL;
 	struct bpf_core_cand_list cands = {};
+	struct func_args_btf *args = NULL;
 	struct bpf_core_relo_res targ_res;
 	struct bpf_core_spec *specs;
 	const struct btf_type *type;
@@ -10734,6 +10847,19 @@ int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
 		return -EINVAL;
 	}

+	if (need_cands && bpf_core_is_func_args(ctx->btf, type)) {
+		/* the prog holds attach_btf, no need for cand_cache_mutex */
+		need_cands = false;
+		args = bpf_core_func_args_btf(ctx->prog);
+		if (IS_ERR(args)) {
+			err = PTR_ERR(args);
+			args = NULL;
+			goto out;
+		}
+		cands.cands = &args->cand;
+		cands.len = 1;
+	}
+
 	if (need_cands) {
 		struct bpf_cand_cache *cc;
 		int i;
@@ -10779,6 +10905,7 @@ int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,

 out:
 	kfree(specs);
+	kfree(args);
 	if (need_cands) {
 		kfree(cands.cands);
 		mutex_unlock(&cand_cache_mutex);
diff --git a/kernel/bpf/check_btf.c b/kernel/bpf/check_btf.c
index 79e079e..e302fee 100644
--- a/kernel/bpf/check_btf.c
+++ b/kernel/bpf/check_btf.c
@@ -352,6 +352,7 @@ int bpf_check_core_relo(struct bpf_verifier_env *env,
 	struct bpf_core_ctx ctx = {
 		.log = &env->log,
 		.btf = btf,
+		.prog = prog,
 	};
 	bpfptr_t u_core_relo;
 	int err;
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 1efc0dd..82a532a 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -6256,6 +6256,18 @@ static const struct bpf_core_relo *find_relo_core(struct bpf_program *prog, int
 	return NULL;
 }

+#define BPF_FUNC_ARGS "bpf_func_args"
+
+static struct btf *bpf_program_func_args_btf(struct bpf_program *prog);
+
+static bool bpf_core_is_func_args(const struct btf_type *t, const char *name)
+{
+	size_t len = sizeof(BPF_FUNC_ARGS) - 1;
+
+	return btf_is_struct(t) && bpf_core_essential_name_len(name) == len &&
+	       strncmp(name, BPF_FUNC_ARGS, len) == 0;
+}
+
 static int bpf_core_resolve_relo(struct bpf_program *prog,
 				 const struct bpf_core_relo *relo,
 				 int relo_idx,
@@ -6279,6 +6291,25 @@ static int bpf_core_resolve_relo(struct bpf_program *prog,
 	if (!local_name)
 		return -EINVAL;

+	if (relo->kind != BPF_CORE_TYPE_ID_LOCAL &&
+	    bpf_core_is_func_args(local_type, local_name)) {
+		/* the candidate depends on the prog, keep it out of cand_cache */
+		struct bpf_core_cand_list args_cands = { .len = 1 };
+		struct bpf_core_cand args_cand;
+		struct btf *args_btf;
+
+		args_btf = bpf_program_func_args_btf(prog);
+		if (IS_ERR(args_btf))
+			return PTR_ERR(args_btf);
+		args_cand.btf = args_btf;
+		args_cand.id = btf__type_cnt(args_btf) - 1;
+		args_cands.cands = &args_cand;
+		err = bpf_core_calc_relo_insn(prog_name, relo, relo_idx, local_btf, &args_cands,
+					      specs_scratch, targ_res);
+		btf__free(args_btf);
+		return err;
+	}
+
 	if (relo->kind != BPF_CORE_TYPE_ID_LOCAL &&
 	    !hashmap__find(cand_cache, local_id, &cands)) {
 		cands = bpf_core_find_cands(prog->obj, local_btf, local_id);
@@ -11641,6 +11672,110 @@ static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attac
 	return 0;
 }

+static int func_args_add(struct btf *btf, const char *name, int type_id, __u32 *off)
+{
+	__s64 sz = btf__resolve_size(btf, type_id);
+	__u32 pad = 0;
+	int err;
+
+	if (sz < 0)
+		return sz;
+	/* a small argument is in the low bytes of its slot */
+	if (sz < 8 && btf__endianness(btf) == BTF_BIG_ENDIAN)
+		pad = 8 - sz;
+	err = btf__add_field(btf, name, type_id, (*off + pad) * 8, 0);
+	*off += roundup(sz, 8);
+	return err;
+}
+
+/*
+ * The context of tp_btf, fentry, fexit, fmod_ret, fsession and lsm progs is
+ * an array of u64 slots: the arguments of the attach target, then its return
+ * value, see btf_ctx_arg_idx() in the kernel. Describe it as
+ *
+ *	struct bpf_func_args {
+ *		<type of the parameter> <name of the parameter>;
+ *		...
+ *		<return type> __ret;
+ *	};
+ *
+ * in a split BTF on top of the BTF of the target. Then the relocations
+ * against 'struct bpf_func_args___xyz' of the prog are ordinary CO-RE.
+ */
+static struct btf *bpf_program_func_args_btf(struct bpf_program *prog)
+{
+	int btf_obj_fd = 0, id = 0, i, first = 0, err;
+	struct bpf_object *obj = prog->obj;
+	struct btf *targ_btf = obj->btf_vmlinux, *btf;
+	const struct btf_param *p;
+	const struct btf_type *t;
+	const char *name;
+	__u32 off = 0;
+
+	/* subprogs are shared and have no type */
+	if ((prog->type != BPF_PROG_TYPE_TRACING && prog->type != BPF_PROG_TYPE_LSM) ||
+	    prog->attach_prog_fd) {
+		pr_warn("prog '%s': struct %s needs an attach target in kernel BTF\n",
+			prog->name, BPF_FUNC_ARGS);
+		return ERR_PTR(-EOPNOTSUPP);
+	}
+
+	if (!prog->attach_btf_id) {
+		name = strchr(prog->sec_name, '/');
+		if (!name)
+			return ERR_PTR(-EINVAL);
+		err = libbpf_find_attach_btf_id(prog, name + 1, &btf_obj_fd, &id);
+		if (err)
+			return ERR_PTR(err);
+		/* libbpf_prepare_prog_load() doesn't search again */
+		prog->attach_btf_obj_fd = btf_obj_fd;
+		prog->attach_btf_id = id;
+	}
+
+	for (i = 0; prog->attach_btf_obj_fd && i < obj->btf_module_cnt; i++) {
+		if (obj->btf_modules[i].fd == prog->attach_btf_obj_fd)
+			targ_btf = obj->btf_modules[i].btf;
+	}
+
+	t = btf__type_by_id(targ_btf, prog->attach_btf_id);
+	if (t && btf_is_typedef(t)) {
+		/*
+		 * btf_trace_xyz has no names of parameters. __probestub_xyz()
+		 * has (6.5+), and __traceiter_xyz() has (5.10+).
+		 */
+		name = btf__name_by_offset(targ_btf, t->name_off) + sizeof(BTF_TRACE_PREFIX) - 1;
+		id = find_btf_by_prefix_kind(targ_btf, "__probestub_", name, BTF_KIND_FUNC);
+		if (id == -ENOENT)
+			id = find_btf_by_prefix_kind(targ_btf, "__traceiter_", name, BTF_KIND_FUNC);
+		if (id < 0)
+			return ERR_PTR(id);
+		t = btf__type_by_id(targ_btf, id);
+		first = 1; /* void *__data is not in the context */
+	}
+	if (!t || !btf_is_func(t))
+		return ERR_PTR(-EINVAL);
+	t = btf__type_by_id(targ_btf, t->type);
+
+	btf = btf__new_empty_split(targ_btf);
+	if (!btf)
+		return ERR_PTR(-errno);
+
+	id = btf__add_struct(btf, BPF_FUNC_ARGS, 0);
+	err = id < 0 ? id : 0;
+	p = btf_params(t);
+	for (i = first; !err && i < btf_vlen(t); i++)
+		err = func_args_add(btf, btf__name_by_offset(targ_btf, p[i].name_off),
+				    p[i].type, &off);
+	if (!err && !first && t->type)
+		err = func_args_add(btf, "__ret", t->type, &off);
+	if (err) {
+		btf__free(btf);
+		return ERR_PTR(err);
+	}
+	btf_type_by_id(btf, id)->size = off;
+	return btf;
+}
+
 int libbpf_attach_type_by_name(const char *name,
 			       enum bpf_attach_type *attach_type)
 {

>  .../selftests/bpf/prog_tests/func_args_core.c | 325 +++++++++++++
>  .../selftests/bpf/progs/func_args_core.c      | 241 +++++++++
>  .../selftests/bpf/progs/func_args_core_lsm.c  | 156 ++++++
>  .../bpf/progs/func_args_core_lsm_cgroup.c     |  40 ++

Also 762 lines of selftests are too much.
Pls make all of it much shorter.

pw-bot: cr

      parent reply	other threads:[~2026-11-24  0:50 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 22:32 [PATCH bpf-next 0/7] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering Alan Maguire
2026-11-23 22:32 ` [PATCH bpf-next 5/7] libbpf: Report function argument relocation support via type existence Alan Maguire
2026-11-23 22:38   ` sashiko-bot
2026-11-23 22:32 ` [PATCH bpf-next 6/7] selftests/bpf: Test detection of function argument relocation support Alan Maguire
2026-11-23 22:36   ` sashiko-bot
2026-11-23 22:32 ` [PATCH bpf-next 7/7] Documentation/bpf: Describe CO-RE relocation of function arguments Alan Maguire
     [not found] ` <20261123223227.745345-5-almagui@kernel.org>
2026-11-23 22:41   ` [PATCH bpf-next 4/7] selftests/bpf: Test CO-RE relocation of BPF LSM program arguments sashiko-bot
     [not found] ` <20261123223227.745345-4-almagui@kernel.org>
2026-11-23 22:43   ` [PATCH bpf-next 3/7] selftests/bpf: Test CO-RE relocation of tracing " sashiko-bot
     [not found] ` <20261123223227.745345-3-almagui@kernel.org>
2026-11-23 22:52   ` [PATCH bpf-next 2/7] bpf: Support " sashiko-bot
     [not found] ` <20261123223227.745345-2-almagui@kernel.org>
2026-11-23 22:53   ` [PATCH bpf-next 1/7] libbpf: " sashiko-bot
2026-11-23 23:23   ` bot+bpf-ci
2026-11-24 20:17     ` Andrey Grodzovsky
2026-11-24  0:50 ` Alexei Starovoitov [this message]

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=DN311OXVTVFO.AKQ8VYM3W5EL@gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=almagui@kernel.org \
    --cc=andrii@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=martin.lau@linux.dev \
    --cc=memxor@gmail.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 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.