Skip to contentSkip to conversation

bpf: Implement bpf_get_kern_btf_id() kfunc

RFC PATCH Superseded Yonghong Song 5 participants 13 messages Nov 14, 2022 – Nov 17, 2022

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover bpf: Implement bpf_get_kern_btf_id() kfunc

Currenty, a non-tracing bpf program typically has a single 'context' argument with predefined uapi struct type. Following these uapi struct, user is able to access other fields defined in uapi header. Inside the kernel, the user-seen 'context' argument is replaced with 'kernel context' (or 'kcontext' in short) which can access more information than what uapi header provides. To access other info not in uapi header, people typically do two things:
(1). extend uapi to access more fields rooted from 'context'. (2). use bpf_probe_read_kernl() helper to read particular field based on
kcontext.
Using (1) needs uapi change and using (2) makes code more complex since direct memory access is not allowed.

There are already a few instances trying to access more information from kcontext:
(1). trying to access some fields from perf_event kcontext. (2). trying to access some fields from xdp kcontext.

This patch set tried to allow direct memory access for kcontext fields by introducing bpf_get_kern_btf_id() kfunc.

Martin mentioned a use case like type casting below:
#define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
basically a 'unsigned char *" casted to 'struct skb_shared_info *'. This patch set tries to support such a use case as well with bpf_get_kern_btf_id().

For the patch series, Patch 1 is a preparation patch. Patch 2 did some but incomplete implementation. Please see details in Patch 2 for what is missing. The goal of this RFC patch is to seek comments about whether such a kfunc is helpful and what scope (e.g., type casting like skb_shinfo(SKB) can be implemented with this helper) it should cover. Patch 3 has two simple tests.

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

Yonghong Song (3):
bpf: Add support for kfunc set with generic btf_ids
bpf: Implement bpf_get_kern_btf_id() kfunc
bpf: Add bpf_get_kern_btf_id() tests

 include/linux/bpf.h                           |  2 +
 kernel/bpf/btf.c                              | 75 ++++++++++++++++-
 kernel/bpf/helpers.c                          | 18 ++++-
 kernel/bpf/verifier.c                         |  8 +-
 .../bpf/prog_tests/get_kern_btf_id.c          | 81 +++++++++++++++++++
 .../selftests/bpf/progs/get_kern_btf_id.c     | 44 ++++++++++
 6 files changed, 225 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/get_kern_btf_id.c
 create mode 100644 tools/testing/selftests/bpf/progs/get_kern_btf_id.c
Signature · 1 line
2.30.2
#1 lore raw

RFC PATCH 1/3 bpf: Add support for kfunc set with generic btf_ids

Later on, we will introduce a kfunc bpf_get_kern_btf_id() which applies to all program types. Currently kfunc set only supports individual prog types. This patch added support for a kfunc applying to all program types. The implementation is identical to [1].

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

---
 kernel/bpf/btf.c     |  8 ++++++++
 kernel/bpf/helpers.c | 13 ++++++++++++-
 2 files changed, 20 insertions(+), 1 deletion(-)
kernel/bpf/btf.c +8 -0
index 5579ff3a5b54..84f09235857c 100644
@@ -199,6 +199,7 @@ DEFINE_IDR(btf_idr);
 DEFINE_SPINLOCK(btf_idr_lock);
 
 enum btf_kfunc_hook {
+	BTF_KFUNC_HOOK_GENERIC,
 	BTF_KFUNC_HOOK_XDP,
 	BTF_KFUNC_HOOK_TC,
 	BTF_KFUNC_HOOK_STRUCT_OPS,
@@ -7499,6 +7500,8 @@ static u32 *__btf_kfunc_id_set_contains(const struct btf *btf,
 static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)
 {
 	switch (prog_type) {
+	case BPF_PROG_TYPE_UNSPEC:
+		return BTF_KFUNC_HOOK_GENERIC;
 	case BPF_PROG_TYPE_XDP:
 		return BTF_KFUNC_HOOK_XDP;
 	case BPF_PROG_TYPE_SCHED_CLS:
@@ -7527,6 +7530,11 @@ u32 *btf_kfunc_id_set_contains(const struct btf *btf,
 			       u32 kfunc_btf_id)
 {
 	enum btf_kfunc_hook hook;
+	u32 *kfunc_flags;
+
+	kfunc_flags = __btf_kfunc_id_set_contains(btf, BTF_KFUNC_HOOK_GENERIC, kfunc_btf_id);
+	if (kfunc_flags)
+		return kfunc_flags;
 
 	hook = bpf_prog_type_to_kfunc_hook(prog_type);
 	return __btf_kfunc_id_set_contains(btf, hook, kfunc_btf_id);
kernel/bpf/helpers.c +12 -1
index 283f55bbeb70..2f11e22eefba 100644
@@ -1717,9 +1717,20 @@ static const struct btf_kfunc_id_set tracing_kfunc_set = {
 	.set   = &tracing_btf_ids,
 };
 
+BTF_SET8_START(generic_btf_ids)
+BTF_SET8_END(generic_btf_ids)
+
+static const struct btf_kfunc_id_set generic_kfunc_set = {
+	.owner = THIS_MODULE,
+	.set   = &generic_btf_ids,
+};
+
 static int __init kfunc_init(void)
 {
-	return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &tracing_kfunc_set);
+	int ret;
+
+	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &tracing_kfunc_set);
+	return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, &generic_kfunc_set);
 }
 
 late_initcall(kfunc_init);
Signature · 1 line
2.30.2
#2 lore raw

RFC PATCH 2/3 bpf: Implement bpf_get_kern_btf_id() kfunc

In reply to Yonghong Song

The signature of bpf_get_kern_btf_id() function looks like
void *bpf_get_kern_btf_id(obj, expected_btf_id)
The obj has a pointer type. The expected_btf_id is 0 or
a btf id to be returned by the kfunc. The function
currently supports two kinds of obj:
- obj: ptr_to_ctx, expected_btf_id: 0
return the expected kernel ctx btf id
- obj: ptr to char/unsigned char, expected_btf_id: a struct btf id return expected_btf_id
The second case looks like a type casting, e.g., in kernel we have
#define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
bpf program can get a skb_shared_info btf id ptr with bpf_get_kern_btf_id() kfunc.

The current prototype is incomplete in the following areas:
- ptr to char/unsigned char is not supported. In btf_struct_walk(), for a pointer not pointing to a struct, a WALK_SCALAR is returned. this needs to be improved to recognize walking ptr to non-struct.
- the current implementation didn't validate non-zero expected_btf_id parameter has to be a struct. This can be added easily later.
- The forced type casting case may not be reliable, so the resulted ptr and later walked ptr cannot be passed to helper/kfunc's. We need to resolve this some how. David Vernet has some work ([1]) in this area and it is not finalized yet.

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

---
 include/linux/bpf.h   |  2 ++
 kernel/bpf/btf.c      | 67 ++++++++++++++++++++++++++++++++++++++++++-
 kernel/bpf/helpers.c  |  5 ++++
 kernel/bpf/verifier.c |  8 +++++-
 4 files changed, 80 insertions(+), 2 deletions(-)
include/linux/bpf.h +2 -0
index 798aec816970..d6a1c463b87e 100644
@@ -2087,6 +2087,7 @@ struct bpf_kfunc_arg_meta {
 	bool r0_rdonly;
 	int ref_obj_id;
 	u32 flags;
+	u32 expected_ret_btf_id;
 };
 
 struct bpf_reg_state;
@@ -2113,6 +2114,7 @@ bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog);
 const struct btf_func_model *
 bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
 			 const struct bpf_insn *insn);
+void *bpf_get_kern_btf_id(void *, u32 expected_btf_id);
 struct bpf_core_ctx {
 	struct bpf_verifier_log *log;
 	const struct btf *btf;
kernel/bpf/btf.c +66 -1

91 lines not on this page · raw message

kernel/bpf/helpers.c +5 -0
index 2f11e22eefba..f8995947a790 100644
@@ -1717,7 +1717,12 @@ static const struct btf_kfunc_id_set tracing_kfunc_set = {
 	.set   = &tracing_btf_ids,
 };
 
+void *bpf_get_kern_btf_id(void *obj, u32 expected_btf_id) {
+	return obj;
+}
+
 BTF_SET8_START(generic_btf_ids)
+BTF_ID_FLAGS(func, bpf_get_kern_btf_id)
 BTF_SET8_END(generic_btf_ids)
 
 static const struct btf_kfunc_id_set generic_kfunc_set = {
kernel/bpf/verifier.c +7 -1
index 07c0259dfc1a..cf284af382a3 100644
@@ -7868,7 +7868,13 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 	} else if (btf_type_is_ptr(t)) {
 		ptr_type = btf_type_skip_modifiers(desc_btf, t->type,
 						   &ptr_type_id);
-		if (!btf_type_is_struct(ptr_type)) {
+		if (meta.expected_ret_btf_id) {
+			mark_reg_known_zero(env, regs, BPF_REG_0);
+			regs[BPF_REG_0].btf = desc_btf;
+			regs[BPF_REG_0].type = PTR_TO_BTF_ID;
+			regs[BPF_REG_0].btf_id = meta.expected_ret_btf_id;
+
+		} else if (!btf_type_is_struct(ptr_type)) {
 			if (!meta.r0_size) {
 				ptr_type_name = btf_name_by_offset(desc_btf,
 								   ptr_type->name_off);
Signature · 1 line
2.30.2
#3 lore raw
Quoted 5 lines · Yonghong Song

The signature of bpf_get_kern_btf_id() function looks like
void *bpf_get_kern_btf_id(obj, expected_btf_id)
The obj has a pointer type. The expected_btf_id is 0 or a btf id to be returned by the kfunc. The function currently supports two kinds of obj:

Quoted 8 lines

- obj: ptr_to_ctx, expected_btf_id: 0
return the expected kernel ctx btf id
- obj: ptr to char/unsigned char, expected_btf_id: a struct btf id return expected_btf_id
The second case looks like a type casting, e.g., in kernel we have
#define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
bpf program can get a skb_shared_info btf id ptr with bpf_get_kern_btf_id() kfunc.

Kumar has proposed
bpf_rdonly_cast(any_64bit_value, btf_id) -> PTR_TO_BTF_ID | PTR_UNTRUSTED. The idea of bpf_get_kern_btf_id(ctx) looks complementary.
The bpf_get_kern_btf_id name is too specific imo.
How about two kfuncs:

bpf_cast_to_kern_ctx(ctx) -> ptr_to_btf_id | ptr_trusted bpf_rdonly_cast(any_scalar, btf_id) -> ptr_to_btf_id | ptr_untrusted

ptr_trusted flag will have semantics as discsused with David and Kumar in:
https://lore.kernel.org/bpf/CAADnVQ+KZcFZdC=W_qZ3kam9yAjORtpN-9+Ptg_Whj-gRxCZNQ@mail.gmail.com/

The verifier knows how to cast safe pointer 'ctx' to kernel 'mirror' structure. No need for additional btf_id argument.
We can express it as ptr_to_btf_id | ptr_trusted and safely pass to kfuncs. bpf_rdonly_cast() can accept any 64-bit value.
There is no need to limit it to 'char *' arg. Since it's ptr_to_btf_id | ptr_untrusted it cannot be passed to kfuncs and only rdonly acccess is allowed.
Both kfuncs need to be cap_perfmon gated, of course.
Thoughts?

#4 lore raw
Quoted 27 lines · Alexei Starovoitov
Quoted 5 lines · Yonghong Song

The signature of bpf_get_kern_btf_id() function looks like
void *bpf_get_kern_btf_id(obj, expected_btf_id)
The obj has a pointer type. The expected_btf_id is 0 or a btf id to be returned by the kfunc. The function currently supports two kinds of obj:

Quoted 8 lines

- obj: ptr_to_ctx, expected_btf_id: 0
return the expected kernel ctx btf id
- obj: ptr to char/unsigned char, expected_btf_id: a struct btf id return expected_btf_id
The second case looks like a type casting, e.g., in kernel we have
#define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
bpf program can get a skb_shared_info btf id ptr with bpf_get_kern_btf_id() kfunc.

Kumar has proposed
bpf_rdonly_cast(any_64bit_value, btf_id) -> PTR_TO_BTF_ID | PTR_UNTRUSTED. The idea of bpf_get_kern_btf_id(ctx) looks complementary.
The bpf_get_kern_btf_id name is too specific imo.
How about two kfuncs:

bpf_cast_to_kern_ctx(ctx) -> ptr_to_btf_id | ptr_trusted bpf_rdonly_cast(any_scalar, btf_id) -> ptr_to_btf_id | ptr_untrusted

ptr_trusted flag will have semantics as discsused with David and Kumar in:
https://lore.kernel.org/bpf/CAADnVQ+KZcFZdC=W_qZ3kam9yAjORtpN-9+Ptg_Whj-gRxCZNQ@mail.gmail.com/

Quoted 8 lines

The verifier knows how to cast safe pointer 'ctx' to kernel 'mirror' structure. No need for additional btf_id argument.
We can express it as ptr_to_btf_id | ptr_trusted and safely pass to kfuncs. bpf_rdonly_cast() can accept any 64-bit value.
There is no need to limit it to 'char *' arg. Since it's ptr_to_btf_id | ptr_untrusted it cannot be passed to kfuncs and only rdonly acccess is allowed.
Both kfuncs need to be cap_perfmon gated, of course.
Thoughts?

Here is the PoC I wrote when we discussed this:
It still uses bpf_unsafe_cast naming, but that was before Alexei suggested the bpf_rdonly_cast name.
https://github.com/kkdwivedi/linux/commits/unsafe-cast (see the 2 latest commits) The selftest showcases how it will be useful.

#5 lore raw
Quoted 16 lines · Kumar Kartikeya Dwivedi
Quoted 15 lines · Alexei Starovoitov
Quoted 13 lines · Yonghong Song

The signature of bpf_get_kern_btf_id() function looks like
void *bpf_get_kern_btf_id(obj, expected_btf_id)
The obj has a pointer type. The expected_btf_id is 0 or
a btf id to be returned by the kfunc. The function
currently supports two kinds of obj:
- obj: ptr_to_ctx, expected_btf_id: 0
return the expected kernel ctx btf id
- obj: ptr to char/unsigned char, expected_btf_id: a struct btf id return expected_btf_id
The second case looks like a type casting, e.g., in kernel we have
#define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
bpf program can get a skb_shared_info btf id ptr with bpf_get_kern_btf_id() kfunc.

Quoted 8 lines
Quoted 8 lines

Kumar has proposed
bpf_rdonly_cast(any_64bit_value, btf_id) -> PTR_TO_BTF_ID | PTR_UNTRUSTED. The idea of bpf_get_kern_btf_id(ctx) looks complementary.
The bpf_get_kern_btf_id name is too specific imo.
How about two kfuncs:

bpf_cast_to_kern_ctx(ctx) -> ptr_to_btf_id | ptr_trusted bpf_rdonly_cast(any_scalar, btf_id) -> ptr_to_btf_id | ptr_untrusted

Sounds good. Two helpers can make sense as it is indeed true for bpf_cast_to_kern_ctx(ctx), the btf_id is not needed.

Quoted 4 lines
Quoted 4 lines

ptr_trusted flag will have semantics as discsused with David and Kumar in:
https://lore.kernel.org/bpf/CAADnVQ+KZcFZdC=W_qZ3kam9yAjORtpN-9+Ptg_Whj-gRxCZNQ@mail.gmail.com/

Quoted 8 lines
Quoted 8 lines

The verifier knows how to cast safe pointer 'ctx' to kernel 'mirror' structure. No need for additional btf_id argument.
We can express it as ptr_to_btf_id | ptr_trusted and safely pass to kfuncs. bpf_rdonly_cast() can accept any 64-bit value.
There is no need to limit it to 'char *' arg. Since it's ptr_to_btf_id | ptr_untrusted it cannot be passed to kfuncs and only rdonly acccess is allowed.
Both kfuncs need to be cap_perfmon gated, of course.
Thoughts?

Currently, we only have SCALAR_VALUE to represent 'void *', 'char *', 'unsigned char *'. yes, some pointer might be long and cast to 'struct foo *', so the generalization of bpf_rdonly_cast() to all scalar value should be fine. Although it is possible the it might be abused and incuring some exception handling, but guarding it with cap_perfmon
should be okay.

Quoted 6 lines

Here is the PoC I wrote when we discussed this:
It still uses bpf_unsafe_cast naming, but that was before Alexei suggested the bpf_rdonly_cast name.
https://github.com/kkdwivedi/linux/commits/unsafe-cast (see the 2 latest commits) The selftest showcases how it will be useful.

Sounds good. I can redo may patch for bpf_cast_to_kern_ctx(), which should cover some of existing cases. Kumar, since you are working on bpf_rdonly_cast(), you could work on that later. If you want me to do it, just let me know I can incorporate it in my patch set.

#6 lore raw
Quoted 58 lines · Yonghong Song
Quoted 16 lines · Kumar Kartikeya Dwivedi
Quoted 15 lines · Alexei Starovoitov
Quoted 13 lines · Yonghong Song

The signature of bpf_get_kern_btf_id() function looks like
void *bpf_get_kern_btf_id(obj, expected_btf_id)
The obj has a pointer type. The expected_btf_id is 0 or
a btf id to be returned by the kfunc. The function
currently supports two kinds of obj:
- obj: ptr_to_ctx, expected_btf_id: 0
return the expected kernel ctx btf id
- obj: ptr to char/unsigned char, expected_btf_id: a struct btf id return expected_btf_id
The second case looks like a type casting, e.g., in kernel we have
#define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
bpf program can get a skb_shared_info btf id ptr with bpf_get_kern_btf_id() kfunc.

Quoted 8 lines
Quoted 8 lines

Kumar has proposed
bpf_rdonly_cast(any_64bit_value, btf_id) -> PTR_TO_BTF_ID | PTR_UNTRUSTED. The idea of bpf_get_kern_btf_id(ctx) looks complementary.
The bpf_get_kern_btf_id name is too specific imo.
How about two kfuncs:

bpf_cast_to_kern_ctx(ctx) -> ptr_to_btf_id | ptr_trusted bpf_rdonly_cast(any_scalar, btf_id) -> ptr_to_btf_id | ptr_untrusted

Sounds good. Two helpers can make sense as it is indeed true for bpf_cast_to_kern_ctx(ctx), the btf_id is not needed.

Quoted 4 lines
Quoted 4 lines

ptr_trusted flag will have semantics as discsused with David and Kumar in:
https://lore.kernel.org/bpf/CAADnVQ+KZcFZdC=W_qZ3kam9yAjORtpN-9+Ptg_Whj-gRxCZNQ@mail.gmail.com/

Quoted 8 lines
Quoted 8 lines

The verifier knows how to cast safe pointer 'ctx' to kernel 'mirror' structure. No need for additional btf_id argument.
We can express it as ptr_to_btf_id | ptr_trusted and safely pass to kfuncs. bpf_rdonly_cast() can accept any 64-bit value.
There is no need to limit it to 'char *' arg. Since it's ptr_to_btf_id | ptr_untrusted it cannot be passed to kfuncs and only rdonly acccess is allowed.
Both kfuncs need to be cap_perfmon gated, of course.
Thoughts?

Currently, we only have SCALAR_VALUE to represent 'void *', 'char *', 'unsigned char *'. yes, some pointer might be long and cast to 'struct foo *', so the generalization of bpf_rdonly_cast() to all scalar value should be fine. Although it is possible the it might be abused and incuring some exception handling, but guarding it with cap_perfmon should be okay.

Quoted 6 lines

Here is the PoC I wrote when we discussed this:
It still uses bpf_unsafe_cast naming, but that was before Alexei suggested the bpf_rdonly_cast name.
https://github.com/kkdwivedi/linux/commits/unsafe-cast (see the 2 latest commits) The selftest showcases how it will be useful.

Quoted 4 lines

Sounds good. I can redo may patch for bpf_cast_to_kern_ctx(), which should cover some of existing cases. Kumar, since you are working on bpf_rdonly_cast(), you could work on that later. If you want me to do it, just let me know I can incorporate it in my patch set.

I think the patch itself is pretty trivial. What's needed is a bit of refactoring, since I would also want to make this work for module BTF types.

In that case, we need to take a type in prog BTF, look it up in the kernel, and mark the reg using looked up BTF and BTF ID. However this raises module BTF reference, and it needs to be kept until verifier is done (as it gets set to reg->btf).

This is why the helper takes local type ID instead of bpf_core_type_id_kernel, since that doesn't work for module types (IIUC).

Instead of the current used_btfs array logic, Alexei suggested guarding module BTF free path with a rwsem, which the verifier can hold in bpf_check, so that we don't have to worry about keeping module BTF references around during verification. Modules are loaded/unloaded infrequently so it should be fine.

Then it also became clear we currently stash BTFs in some places unecessarily and we could simply drop those after prog is verified. So it would make sense to drop those cases too (kfunc_btf_tab, used_btfs btf_mod_pair, etc.). After verification the prog only needs to pin the module references, not mod BTF references.

Maybe all of this does not have to be done together.

So let me know if you want to take it, I have no problems with that, otherwise I can get to it once I am done with the linked list and dynptr stuff.

#7 lore raw
Quoted 59 lines · Kumar Kartikeya Dwivedi
Quoted 58 lines · Yonghong Song
Quoted 16 lines · Kumar Kartikeya Dwivedi
Quoted 15 lines · Alexei Starovoitov
Quoted 13 lines · Yonghong Song

The signature of bpf_get_kern_btf_id() function looks like
void *bpf_get_kern_btf_id(obj, expected_btf_id)
The obj has a pointer type. The expected_btf_id is 0 or
a btf id to be returned by the kfunc. The function currently supports two kinds of obj:
- obj: ptr_to_ctx, expected_btf_id: 0
return the expected kernel ctx btf id
- obj: ptr to char/unsigned char, expected_btf_id: a struct btf id return expected_btf_id
The second case looks like a type casting, e.g., in kernel we have

    #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))

bpf program can get a skb_shared_info btf id ptr with bpf_get_kern_btf_id() kfunc.

Quoted 8 lines
Quoted 8 lines

Kumar has proposed
bpf_rdonly_cast(any_64bit_value, btf_id) -> PTR_TO_BTF_ID | PTR_UNTRUSTED. The idea of bpf_get_kern_btf_id(ctx) looks complementary.
The bpf_get_kern_btf_id name is too specific imo.
How about two kfuncs:

bpf_cast_to_kern_ctx(ctx) -> ptr_to_btf_id | ptr_trusted bpf_rdonly_cast(any_scalar, btf_id) -> ptr_to_btf_id | ptr_untrusted

Sounds good. Two helpers can make sense as it is indeed true for bpf_cast_to_kern_ctx(ctx), the btf_id is not needed.

Quoted 4 lines
Quoted 4 lines

ptr_trusted flag will have semantics as discsused with David and Kumar in:
https://lore.kernel.org/bpf/CAADnVQ+KZcFZdC=W_qZ3kam9yAjORtpN-9+Ptg_Whj-gRxCZNQ@mail.gmail.com/

Quoted 8 lines
Quoted 8 lines

The verifier knows how to cast safe pointer 'ctx' to kernel 'mirror' structure. No need for additional btf_id argument.
We can express it as ptr_to_btf_id | ptr_trusted and safely pass to kfuncs. bpf_rdonly_cast() can accept any 64-bit value.
There is no need to limit it to 'char *' arg. Since it's ptr_to_btf_id | ptr_untrusted it cannot be passed to kfuncs and only rdonly acccess is allowed.
Both kfuncs need to be cap_perfmon gated, of course.
Thoughts?

Currently, we only have SCALAR_VALUE to represent 'void *', 'char *', 'unsigned char *'. yes, some pointer might be long and cast to 'struct foo *', so the generalization of bpf_rdonly_cast() to all scalar value should be fine. Although it is possible the it might be abused and incuring some exception handling, but guarding it with cap_perfmon should be okay.

Quoted 6 lines

Here is the PoC I wrote when we discussed this:
It still uses bpf_unsafe_cast naming, but that was before Alexei suggested the bpf_rdonly_cast name.
https://github.com/kkdwivedi/linux/commits/unsafe-cast (see the 2 latest commits) The selftest showcases how it will be useful.

Quoted 4 lines
Quoted 4 lines

Sounds good. I can redo may patch for bpf_cast_to_kern_ctx(), which should cover some of existing cases. Kumar, since you are working on bpf_rdonly_cast(), you could work on that later. If you want me to do it, just let me know I can incorporate it in my patch set.

I just prototyped a little bit with Alexei's suggested interface. It has some differences. I will explain in my next revision.

My prototype already added bpf_rdonly_cast(). As you suggested, it is not too hard. I have not done with module btf yet. Will add it as you suggested below.

Quoted 17 lines

I think the patch itself is pretty trivial. What's needed is a bit of refactoring, since I would also want to make this work for module BTF types.

In that case, we need to take a type in prog BTF, look it up in the kernel, and mark the reg using looked up BTF and BTF ID. However this raises module BTF reference, and it needs to be kept until verifier is done (as it gets set to reg->btf).

This is why the helper takes local type ID instead of bpf_core_type_id_kernel, since that doesn't work for module types (IIUC).

Instead of the current used_btfs array logic, Alexei suggested guarding module BTF free path with a rwsem, which the verifier can hold in bpf_check, so that we don't have to worry about keeping module BTF references around during verification. Modules are loaded/unloaded infrequently so it should be fine.

Quoted 7 lines

Then it also became clear we currently stash BTFs in some places unecessarily and we could simply drop those after prog is verified. So it would make sense to drop those cases too (kfunc_btf_tab, used_btfs btf_mod_pair, etc.). After verification the prog only needs to pin the module references, not mod BTF references.

Maybe all of this does not have to be done together.

I will focus on implementing bpf_cast_to_kern_ctx() and bpf_rdonly_cast(). Others can be delayed for later patches.

Quoted 3 lines

So let me know if you want to take it, I have no problems with that, otherwise I can get to it once I am done with the linked list and dynptr stuff.

#8 lore raw
Quoted 67 lines · Yonghong Song
Quoted 59 lines · Kumar Kartikeya Dwivedi
Quoted 58 lines · Yonghong Song
Quoted 16 lines · Kumar Kartikeya Dwivedi
Quoted 15 lines · Alexei Starovoitov
Quoted 13 lines · Yonghong Song

The signature of bpf_get_kern_btf_id() function looks like
void *bpf_get_kern_btf_id(obj, expected_btf_id)
The obj has a pointer type. The expected_btf_id is 0 or
a btf id to be returned by the kfunc. The function currently supports two kinds of obj:
- obj: ptr_to_ctx, expected_btf_id: 0
return the expected kernel ctx btf id
- obj: ptr to char/unsigned char, expected_btf_id: a struct btf id return expected_btf_id
The second case looks like a type casting, e.g., in kernel we have

    #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))

bpf program can get a skb_shared_info btf id ptr with bpf_get_kern_btf_id() kfunc.

Quoted 8 lines
Quoted 8 lines

Kumar has proposed
bpf_rdonly_cast(any_64bit_value, btf_id) -> PTR_TO_BTF_ID | PTR_UNTRUSTED. The idea of bpf_get_kern_btf_id(ctx) looks complementary.
The bpf_get_kern_btf_id name is too specific imo.
How about two kfuncs:

bpf_cast_to_kern_ctx(ctx) -> ptr_to_btf_id | ptr_trusted bpf_rdonly_cast(any_scalar, btf_id) -> ptr_to_btf_id | ptr_untrusted

Sounds good. Two helpers can make sense as it is indeed true for bpf_cast_to_kern_ctx(ctx), the btf_id is not needed.

Quoted 4 lines
Quoted 4 lines

ptr_trusted flag will have semantics as discsused with David and Kumar in:
https://lore.kernel.org/bpf/CAADnVQ+KZcFZdC=W_qZ3kam9yAjORtpN-9+Ptg_Whj-gRxCZNQ@mail.gmail.com/

Quoted 8 lines
Quoted 8 lines

The verifier knows how to cast safe pointer 'ctx' to kernel 'mirror' structure. No need for additional btf_id argument.
We can express it as ptr_to_btf_id | ptr_trusted and safely pass to kfuncs. bpf_rdonly_cast() can accept any 64-bit value.
There is no need to limit it to 'char *' arg. Since it's ptr_to_btf_id | ptr_untrusted it cannot be passed to kfuncs and only rdonly acccess is allowed.
Both kfuncs need to be cap_perfmon gated, of course.
Thoughts?

Currently, we only have SCALAR_VALUE to represent 'void *', 'char *', 'unsigned char *'. yes, some pointer might be long and cast to 'struct foo *', so the generalization of bpf_rdonly_cast() to all scalar value should be fine. Although it is possible the it might be abused and incuring some exception handling, but guarding it with cap_perfmon should be okay.

Quoted 6 lines

Here is the PoC I wrote when we discussed this:
It still uses bpf_unsafe_cast naming, but that was before Alexei suggested the bpf_rdonly_cast name.
https://github.com/kkdwivedi/linux/commits/unsafe-cast (see the 2 latest commits) The selftest showcases how it will be useful.

Quoted 4 lines
Quoted 4 lines

Sounds good. I can redo may patch for bpf_cast_to_kern_ctx(), which should cover some of existing cases. Kumar, since you are working on bpf_rdonly_cast(), you could work on that later. If you want me to do it, just let me know I can incorporate it in my patch set.

Quoted 7 lines

I just prototyped a little bit with Alexei's suggested interface. It has some differences. I will explain in my next revision.

My prototype already added bpf_rdonly_cast(). As you suggested, it is not too hard. I have not done with module btf yet. Will add it as you suggested below.

It's fine to also leave out types in module BTFs for now, atleast as long you return a reasonable error message from the verifier. Just relying on btf_vmlinux is enough for the FIXMEs in selftests.

#9 lore raw
Quoted 71 lines · Kumar Kartikeya Dwivedi
Quoted 70 lines · Yonghong Song
Quoted 59 lines · Kumar Kartikeya Dwivedi
Quoted 58 lines · Yonghong Song
Quoted 16 lines · Kumar Kartikeya Dwivedi
Quoted 15 lines · Alexei Starovoitov
Quoted 13 lines · Yonghong Song

The signature of bpf_get_kern_btf_id() function looks like
void *bpf_get_kern_btf_id(obj, expected_btf_id)
The obj has a pointer type. The expected_btf_id is 0 or
a btf id to be returned by the kfunc. The function
currently supports two kinds of obj:
- obj: ptr_to_ctx, expected_btf_id: 0
return the expected kernel ctx btf id
- obj: ptr to char/unsigned char, expected_btf_id: a struct btf id return expected_btf_id
The second case looks like a type casting, e.g., in kernel we have

     #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))

bpf program can get a skb_shared_info btf id ptr with bpf_get_kern_btf_id() kfunc.

Quoted 8 lines
Quoted 8 lines

Kumar has proposed
bpf_rdonly_cast(any_64bit_value, btf_id) -> PTR_TO_BTF_ID | PTR_UNTRUSTED. The idea of bpf_get_kern_btf_id(ctx) looks complementary.
The bpf_get_kern_btf_id name is too specific imo.
How about two kfuncs:

bpf_cast_to_kern_ctx(ctx) -> ptr_to_btf_id | ptr_trusted bpf_rdonly_cast(any_scalar, btf_id) -> ptr_to_btf_id | ptr_untrusted

Sounds good. Two helpers can make sense as it is indeed true for bpf_cast_to_kern_ctx(ctx), the btf_id is not needed.

Quoted 4 lines
Quoted 4 lines

ptr_trusted flag will have semantics as discsused with David and Kumar in:
https://lore.kernel.org/bpf/CAADnVQ+KZcFZdC=W_qZ3kam9yAjORtpN-9+Ptg_Whj-gRxCZNQ@mail.gmail.com/

Quoted 8 lines
Quoted 8 lines

The verifier knows how to cast safe pointer 'ctx' to kernel 'mirror' structure. No need for additional btf_id argument.
We can express it as ptr_to_btf_id | ptr_trusted and safely pass to kfuncs. bpf_rdonly_cast() can accept any 64-bit value.
There is no need to limit it to 'char *' arg. Since it's ptr_to_btf_id | ptr_untrusted it cannot be passed to kfuncs and only rdonly acccess is allowed.
Both kfuncs need to be cap_perfmon gated, of course.
Thoughts?

Currently, we only have SCALAR_VALUE to represent 'void *', 'char *', 'unsigned char *'. yes, some pointer might be long and cast to 'struct foo *', so the generalization of bpf_rdonly_cast() to all scalar value should be fine. Although it is possible the it might be abused and incuring some exception handling, but guarding it with cap_perfmon should be okay.

Quoted 6 lines

Here is the PoC I wrote when we discussed this:
It still uses bpf_unsafe_cast naming, but that was before Alexei suggested the bpf_rdonly_cast name.
https://github.com/kkdwivedi/linux/commits/unsafe-cast (see the 2 latest commits) The selftest showcases how it will be useful.

Quoted 4 lines
Quoted 4 lines

Sounds good. I can redo may patch for bpf_cast_to_kern_ctx(), which should cover some of existing cases. Kumar, since you are working on bpf_rdonly_cast(), you could work on that later. If you want me to do it, just let me know I can incorporate it in my patch set.

I just prototyped a little bit with Alexei's suggested interface. It has some differences. I will explain in my next revision.

Quoted 8 lines
Quoted 4 lines

My prototype already added bpf_rdonly_cast(). As you suggested, it is not too hard. I have not done with module btf yet. Will add it as you suggested below.

It's fine to also leave out types in module BTFs for now, atleast as long you return a reasonable error message from the verifier. Just relying on btf_vmlinux is enough for the FIXMEs in selftests.

Okay, will leave module support at this point then.

#10 lore raw

RFC PATCH 3/3 bpf: Add bpf_get_kern_btf_id() tests

In reply to Yonghong Song

Two tests are added. One is from John Fastabend ({1]) which tests tracing style access for xdp program from the kernel ctx. Another is a tc test to test both kernel ctx tracing style access and explicit non-ctx type cast.

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

---
 .../bpf/prog_tests/get_kern_btf_id.c          | 81 +++++++++++++++++++
 .../selftests/bpf/progs/get_kern_btf_id.c     | 44 ++++++++++
 2 files changed, 125 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/get_kern_btf_id.c
 create mode 100644 tools/testing/selftests/bpf/progs/get_kern_btf_id.c
tools/testing/selftests/bpf/prog_tests/get_kern_btf_id.c new file +81 -0
new file mode 100644
index 000000000000..205631916564
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <network_helpers.h>
+#include "get_kern_btf_id.skel.h"
+
+static void test_xdp(void)
+{
+	struct get_kern_btf_id *skel;
+	int err, prog_fd;
+	char buf[128];
+
+	LIBBPF_OPTS(bpf_test_run_opts, topts,
+		.data_in = &pkt_v4,
+		.data_size_in = sizeof(pkt_v4),
+		.data_out = buf,
+		.data_size_out = sizeof(buf),
+		.repeat = 1,
+	);
+
+	skel = get_kern_btf_id__open();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	bpf_program__set_autoload(skel->progs.md_xdp, true);
+	err = get_kern_btf_id__load(skel);
+	if (!ASSERT_OK(err, "skel_load"))
+		goto out;
+
+	prog_fd = bpf_program__fd(skel->progs.md_xdp);
+	err = bpf_prog_test_run_opts(prog_fd, &topts);
+	ASSERT_OK(err, "test_run");
+	ASSERT_EQ(topts.retval, XDP_PASS, "xdp test_run retval");
+
+	ASSERT_EQ(skel->bss->ifindex, 1, "xdp_md ifindex");
+	ASSERT_EQ(skel->bss->ifindex, skel->bss->ingress_ifindex, "xdp_md ingress_ifindex");
+	ASSERT_STREQ(skel->bss->name, "lo", "xdp_md name");
+	ASSERT_NEQ(skel->bss->inum, 0, "xdp_md inum");
+
+out:
+	get_kern_btf_id__destroy(skel);
+}
+
+static void test_tc(void)
+{
+	struct get_kern_btf_id *skel;
+	int err, prog_fd;
+
+	LIBBPF_OPTS(bpf_test_run_opts, topts,
+		.data_in = &pkt_v4,
+		.data_size_in = sizeof(pkt_v4),
+		.repeat = 1,
+	);
+
+	skel = get_kern_btf_id__open();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	bpf_program__set_autoload(skel->progs.md_skb, true);
+	err = get_kern_btf_id__load(skel);
+	if (!ASSERT_OK(err, "skel_load"))
+		goto out;
+
+	prog_fd = bpf_program__fd(skel->progs.md_skb);
+	err = bpf_prog_test_run_opts(prog_fd, &topts);
+	ASSERT_OK(err, "test_run");
+	ASSERT_EQ(topts.retval, 0, "tc test_run retval");
+
+	ASSERT_EQ(skel->bss->meta_len, 0, "skb meta_len");
+	ASSERT_EQ(skel->bss->frag0_len, 0, "skb frag0_len");
+
+out:
+	get_kern_btf_id__destroy(skel);
+}
+
+void test_get_kern_btf_id(void)
+{
+	if (test__start_subtest("xdp"))
+		test_xdp();
+	if (test__start_subtest("tc"))
+		test_tc();
+}
tools/testing/selftests/bpf/progs/get_kern_btf_id.c new file +44 -0

44 lines not on this page · raw message

Signature · 1 line
2.30.2
#11 lore raw
In reply to Yonghong Song
Quoted 12 lines · Yonghong Song

Currenty, a non-tracing bpf program typically has a single 'context' argument with predefined uapi struct type. Following these uapi struct, user is able to access other fields defined in uapi header. Inside the kernel, the user-seen 'context' argument is replaced with 'kernel context' (or 'kcontext' in short) which can access more information than what uapi header provides. To access other info not in uapi header, people typically do two things:
(1). extend uapi to access more fields rooted from 'context'. (2). use bpf_probe_read_kernl() helper to read particular field based on
kcontext.
Using (1) needs uapi change and using (2) makes code more complex since direct memory access is not allowed.

Quoted 7 lines

There are already a few instances trying to access more information from kcontext:
(1). trying to access some fields from perf_event kcontext. (2). trying to access some fields from xdp kcontext.

This patch set tried to allow direct memory access for kcontext fields by introducing bpf_get_kern_btf_id() kfunc.

I think the general idea is neat. However, I'm a bit concerned that with this we're allowing networking BPF programs (like XDP) to walk more or less arbitrary kernel structures, which has thus far been something only tracing programs have had access to. So we probably require programs to have CAP_PERFMON to use this kfunc, no?

-Toke

#12 lore raw
Quoted 19 lines · Toke Høiland-Jørgensen
Quoted 17 lines · Yonghong Song

Currenty, a non-tracing bpf program typically has a single 'context' argument with predefined uapi struct type. Following these uapi struct, user is able to access other fields defined in uapi header. Inside the kernel, the user-seen 'context' argument is replaced with 'kernel context' (or 'kcontext' in short) which can access more information than what uapi header provides. To access other info not in uapi header, people typically do two things:
(1). extend uapi to access more fields rooted from 'context'. (2). use bpf_probe_read_kernl() helper to read particular field based on
kcontext.
Using (1) needs uapi change and using (2) makes code more complex since direct memory access is not allowed.

There are already a few instances trying to access more information from kcontext:
(1). trying to access some fields from perf_event kcontext. (2). trying to access some fields from xdp kcontext.

Quoted 8 lines
Quoted 2 lines

This patch set tried to allow direct memory access for kcontext fields by introducing bpf_get_kern_btf_id() kfunc.

I think the general idea is neat. However, I'm a bit concerned that with this we're allowing networking BPF programs (like XDP) to walk more or less arbitrary kernel structures, which has thus far been something only tracing programs have had access to. So we probably require programs to have CAP_PERFMON to use this kfunc, no?

Make sense, since the kfunc is to enable tracing, yes CAP_PERFMON is a good idea. I will make the change in the next revision.

Quoted 2 lines

-Toke