Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpftool: Add support for custom BTF path in prog load/loadall

This patch exposes the btf_custom_path feature to bpftool, allowing users
to specify a custom BTF file when loading BPF programs using prog load or
prog loadall commands.

The argument 'btf_custom_path' in libbpf is used for those kernels that
don't have CONFIG_DEBUG_INFO_BTF enabled but still want to perform CO-RE
relocations.

Suggested-by: Quentin Monnet <qmo@kernel.org>
Reviewed-by: Quentin Monnet <qmo@kernel.org>
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
Link: https://lore.kernel.org/r/20250516144708.298652-1-jiayuan.chen@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Jiayuan Chen and committed by
Alexei Starovoitov
1ae7a84e 92de53d2

+21 -5
+8 -2
tools/bpf/bpftool/Documentation/bpftool-prog.rst
··· 31 31 | **bpftool** **prog dump xlated** *PROG* [{ **file** *FILE* | [**opcodes**] [**linum**] [**visual**] }] 32 32 | **bpftool** **prog dump jited** *PROG* [{ **file** *FILE* | [**opcodes**] [**linum**] }] 33 33 | **bpftool** **prog pin** *PROG* *FILE* 34 - | **bpftool** **prog** { **load** | **loadall** } *OBJ* *PATH* [**type** *TYPE*] [**map** { **idx** *IDX* | **name** *NAME* } *MAP*] [{ **offload_dev** | **xdpmeta_dev** } *NAME*] [**pinmaps** *MAP_DIR*] [**autoattach**] 34 + | **bpftool** **prog** { **load** | **loadall** } *OBJ* *PATH* [**type** *TYPE*] [**map** { **idx** *IDX* | **name** *NAME* } *MAP*] [{ **offload_dev** | **xdpmeta_dev** } *NAME*] [**pinmaps** *MAP_DIR*] [**autoattach**] [**kernel_btf** *BTF_FILE*] 35 35 | **bpftool** **prog attach** *PROG* *ATTACH_TYPE* [*MAP*] 36 36 | **bpftool** **prog detach** *PROG* *ATTACH_TYPE* [*MAP*] 37 37 | **bpftool** **prog tracelog** ··· 127 127 Note: *FILE* must be located in *bpffs* mount. It must not contain a dot 128 128 character ('.'), which is reserved for future extensions of *bpffs*. 129 129 130 - bpftool prog { load | loadall } *OBJ* *PATH* [type *TYPE*] [map { idx *IDX* | name *NAME* } *MAP*] [{ offload_dev | xdpmeta_dev } *NAME*] [pinmaps *MAP_DIR*] [autoattach] 130 + bpftool prog { load | loadall } *OBJ* *PATH* [type *TYPE*] [map { idx *IDX* | name *NAME* } *MAP*] [{ offload_dev | xdpmeta_dev } *NAME*] [pinmaps *MAP_DIR*] [autoattach] [kernel_btf *BTF_FILE*] 131 131 Load bpf program(s) from binary *OBJ* and pin as *PATH*. **bpftool prog 132 132 load** pins only the first program from the *OBJ* as *PATH*. **bpftool prog 133 133 loadall** pins all programs from the *OBJ* under *PATH* directory. **type** ··· 152 152 object file, in particular, it's not supported for all program types. If a 153 153 program does not support autoattach, bpftool falls back to regular pinning 154 154 for that program instead. 155 + 156 + The **kernel_btf** option allows specifying an external BTF file to replace 157 + the system's own vmlinux BTF file for CO-RE relocations. Note that any 158 + other feature relying on BTF (such as fentry/fexit programs, struct_ops) 159 + requires the BTF file for the actual kernel running on the host, often 160 + exposed at /sys/kernel/btf/vmlinux. 155 161 156 162 Note: *PATH* must be located in *bpffs* mount. It must not contain a dot 157 163 character ('.'), which is reserved for future extensions of *bpffs*.
+2 -2
tools/bpf/bpftool/bash-completion/bpftool
··· 505 505 _bpftool_get_map_names 506 506 return 0 507 507 ;; 508 - pinned|pinmaps) 508 + pinned|pinmaps|kernel_btf) 509 509 _filedir 510 510 return 0 511 511 ;; 512 512 *) 513 513 COMPREPLY=( $( compgen -W "map" -- "$cur" ) ) 514 - _bpftool_once_attr 'type pinmaps autoattach' 514 + _bpftool_once_attr 'type pinmaps autoattach kernel_btf' 515 515 _bpftool_one_of_list 'offload_dev xdpmeta_dev' 516 516 return 0 517 517 ;;
+11 -1
tools/bpf/bpftool/prog.c
··· 1681 1681 } else if (is_prefix(*argv, "autoattach")) { 1682 1682 auto_attach = true; 1683 1683 NEXT_ARG(); 1684 + } else if (is_prefix(*argv, "kernel_btf")) { 1685 + NEXT_ARG(); 1686 + 1687 + if (!REQ_ARGS(1)) 1688 + goto err_free_reuse_maps; 1689 + 1690 + open_opts.btf_custom_path = GET_ARG(); 1684 1691 } else { 1685 - p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?", 1692 + p_err("expected no more arguments, " 1693 + "'type', 'map', 'offload_dev', 'xdpmeta_dev', 'pinmaps', " 1694 + "'autoattach', or 'kernel_btf', got: '%s'?", 1686 1695 *argv); 1687 1696 goto err_free_reuse_maps; 1688 1697 } ··· 2483 2474 " [map { idx IDX | name NAME } MAP]\\\n" 2484 2475 " [pinmaps MAP_DIR]\n" 2485 2476 " [autoattach]\n" 2477 + " [kernel_btf BTF_FILE]\n" 2486 2478 " %1$s %2$s attach PROG ATTACH_TYPE [MAP]\n" 2487 2479 " %1$s %2$s detach PROG ATTACH_TYPE [MAP]\n" 2488 2480 " %1$s %2$s run PROG \\\n"