bpf: Implement two type cast kfuncs
Status from this message: This series was applied to bpf/bpf-next.git (master)
Get series with b4
b4 am [email protected]Versions newest first · linked by author and title
- v4 Latest4 patches 12 repliesApplied 4/4
- v34 patches 1 replySuperseded
- v24 patches 5 repliesSuperseded
Earlier versions · 1
| Version | Posted | Patches | Replies | Status |
|---|---|---|---|---|
| v1 RFC | 3 patches | 9 replies | Superseded |
Changes in v4 from the cover letter
- remove unnecessary bpf_ctx_convert.t error checking
- add and use meta.ret_btf_id instead of meta.arg_constant.value for bpf_cast_to_kern_ctx().
- add PTR_TRUSTED to the return PTR_TO_BTF_ID type for bpf_cast_to_kern_ctx().
Patches in v4 All 4 applied
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/4 | bpf: Add support for kfunc set with common btf_idskernel/ kernel/ | +19 | 0 replies | Applied |
| 2/4 | bpf: Add a kfunc to type cast from bpf uapi ctx to kernel ctxinclude/ kernel/ kernel/ +1 more | +53 | 0 replies | Applied |
| 3/4 | bpf: Add a kfunc for generic type castkernel/ kernel/ | +30 | 5 replies | Applied |
| 4/4 | bpf: Add type cast unit teststools/ tools/ tools/ | +198 | 0 replies | Applied |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v4 yet.
Cover letter [PATCH bpf-next v4 0/4] bpf: Implement two type cast kfuncs
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 'kctx' 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
kctx.
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 kctx:
. trying to access some fields from perf_event kctx ([1]). . trying to access some fields from xdp kctx ([2]).
This patch set tried to allow direct memory access for kctx fields by introducing bpf_cast_to_kern_ctx() 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_rdonly_cast().
For the patch series, Patch 1 added support for a kfunc available to all prog types. Patch 2 added bpf_cast_to_kern_ctx() kfunc. Patch 3 added bpf_rdonly_cast() kfunc. Patch 4 added a few positive and negative tests.
[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/
Changelog:
v3 -> v4:
- remove unnecessary bpf_ctx_convert.t error checking
- add and use meta.ret_btf_id instead of meta.arg_constant.value for bpf_cast_to_kern_ctx().
- add PTR_TRUSTED to the return PTR_TO_BTF_ID type for bpf_cast_to_kern_ctx().
v2 -> v3:
- rebase on top of bpf-next (for merging conflicts)
- add the selftest to s390x deny list
rfcv1 -> v2:
- break original one kfunc into two.
- add missing error checks and error logs.
- adapt to the new conventions in
https://lore.kernel.org/all/[email protected]/
for example, with __ign and __k suffix.
- added support in fixup_kfunc_call() to replace kfunc calls with a single mov.
Yonghong Song (4):
bpf: Add support for kfunc set with common btf_ids
bpf: Add a kfunc to type cast from bpf uapi ctx to kernel ctx
bpf: Add a kfunc for generic type cast
bpf: Add type cast unit tests
include/linux/btf.h | 5 + kernel/bpf/btf.c | 28 +++++ kernel/bpf/helpers.c | 24 +++- kernel/bpf/verifier.c | 46 ++++++- tools/testing/selftests/bpf/DENYLIST.s390x | 1 + .../selftests/bpf/prog_tests/type_cast.c | 114 ++++++++++++++++++ tools/testing/selftests/bpf/progs/type_cast.c | 83 +++++++++++++ 7 files changed, 299 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/type_cast.c create mode 100644 tools/testing/selftests/bpf/progs/type_cast.c
Signature · 1 line
2.30.2