kbuild: add ability to generate BTF type info for vmlinux
authorAndrii Nakryiko <andriin@fb.com>
Tue, 2 Apr 2019 16:49:50 +0000 (09:49 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Tue, 2 Apr 2019 22:53:07 +0000 (00:53 +0200)
This patch adds new config option to trigger generation of BTF type
information from DWARF debuginfo for vmlinux and kernel modules through
pahole, which in turn relies on libbpf for btf_dedup() algorithm.

The intent is to record compact type information of all types used
inside kernel, including all the structs/unions/typedefs/etc. This
enables BPF's compile-once-run-everywhere ([0]) approach, in which
tracing programs that are inspecting kernel's internal data (e.g.,
struct task_struct) can be compiled on a system running some kernel
version, but would be possible to run on other kernel versions (and
configurations) without recompilation, even if the layout of structs
changed and/or some of the fields were added, removed, or renamed.

This is only possible if BPF loader can get kernel type info to adjust
all the offsets correctly. This patch is a first time in this direction,
making sure that BTF type info is part of Linux kernel image in
non-loadable ELF section.

BTF deduplication ([1]) algorithm typically provides 100x savings
compared to DWARF data, so resulting .BTF section is not big as is
typically about 2MB in size.

[0] http://vger.kernel.org/lpc-bpf2018.html#session-2
[1] https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html

Cc: Masahiro Yamada <yamada.masahiro@socionext.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Alexei Starovoitov <ast@fb.com>
Cc: Yonghong Song <yhs@fb.com>
Cc: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: David S. Miller <davem@davemloft.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Makefile
lib/Kconfig.debug
scripts/link-vmlinux.sh

index c0a3406..73c0133 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -399,6 +399,7 @@ NM          = $(CROSS_COMPILE)nm
 STRIP          = $(CROSS_COMPILE)strip
 OBJCOPY                = $(CROSS_COMPILE)objcopy
 OBJDUMP                = $(CROSS_COMPILE)objdump
+PAHOLE         = pahole
 LEX            = flex
 YACC           = bison
 AWK            = awk
@@ -453,7 +454,7 @@ KBUILD_LDFLAGS :=
 GCC_PLUGINS_CFLAGS :=
 
 export ARCH SRCARCH CONFIG_SHELL HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE AS LD CC
-export CPP AR NM STRIP OBJCOPY OBJDUMP KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS
+export CPP AR NM STRIP OBJCOPY OBJDUMP PAHOLE KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS
 export MAKE LEX YACC AWK INSTALLKERNEL PERL PYTHON PYTHON2 PYTHON3 UTS_MACHINE
 export HOSTCXX KBUILD_HOSTCXXFLAGS LDFLAGS_MODULE CHECK CHECKFLAGS
 
index 0d9e817..188fc17 100644 (file)
@@ -219,6 +219,14 @@ config DEBUG_INFO_DWARF4
          But it significantly improves the success of resolving
          variables in gdb on optimized code.
 
+config DEBUG_INFO_BTF
+       bool "Generate BTF typeinfo"
+       depends on DEBUG_INFO
+       help
+         Generate deduplicated BTF type information from DWARF debug info.
+         Turning this on expects presence of pahole tool, which will convert
+         DWARF type info into equivalent deduplicated BTF type info.
+
 config GDB_SCRIPTS
        bool "Provide GDB scripts for kernel debugging"
        depends on DEBUG_INFO
index dc0e8c5..dd2b31c 100755 (executable)
@@ -35,7 +35,7 @@ set -e
 info()
 {
        if [ "${quiet}" != "silent_" ]; then
-               printf "  %-7s %s\n" ${1} ${2}
+               printf "  %-7s %s\n" "${1}" "${2}"
        fi
 }
 
@@ -91,6 +91,20 @@ vmlinux_link()
        fi
 }
 
+# generate .BTF typeinfo from DWARF debuginfo
+gen_btf()
+{
+       local pahole_ver;
+
+       pahole_ver=$(${PAHOLE} --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/')
+       if [ "${pahole_ver}" -lt "113" ]; then
+               info "BTF" "${1}: pahole version $(${PAHOLE} --version) is too old, need at least v1.13"
+               exit 0
+       fi
+
+       info "BTF" ${1}
+       LLVM_OBJCOPY=${OBJCOPY} ${PAHOLE} -J ${1}
+}
 
 # Create ${2} .o file with all symbols from the ${1} object file
 kallsyms()
@@ -248,6 +262,10 @@ fi
 info LD vmlinux
 vmlinux_link "${kallsymso}" vmlinux
 
+if [ -n "${CONFIG_DEBUG_INFO_BTF}" ]; then
+       gen_btf vmlinux
+fi
+
 if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
        info SORTEX vmlinux
        sortextable vmlinux