pgo: Add Clang's Profile Guided Optimization infrastructure
authorSami Tolvanen <samitolvanen@google.com>
Wed, 7 Apr 2021 21:17:04 +0000 (14:17 -0700)
committerKees Cook <keescook@chromium.org>
Tue, 29 Jun 2021 20:25:39 +0000 (13:25 -0700)
commit3bc68891829b776b9a5dd9174de05e69138af7b6
tree46e24001ee7b3e2fe2335edc95990b2fb33c2efc
parentfca41af18e10318e4de090db47d9fa7169e1bf2f
pgo: Add Clang's Profile Guided Optimization infrastructure

Enable the use of clang's Profile-Guided Optimization[1]. To generate a
profile, the kernel is instrumented with PGO counters, a representative
workload is run, and the raw profile data is collected from
/sys/kernel/debug/pgo/profraw.

The raw profile data must be processed by clang's "llvm-profdata" tool
before it can be used during recompilation:

  $ cp /sys/kernel/debug/pgo/profraw vmlinux.profraw
  $ llvm-profdata merge --output=vmlinux.profdata vmlinux.profraw

Multiple raw profiles may be merged during this step.

The data can now be used by the compiler:

  $ make LLVM=1 KCFLAGS=-fprofile-use=vmlinux.profdata ...

This initial submission is restricted to x86, as that's the platform we
know works. This restriction can be lifted once other platforms have
been verified to work with PGO.

Note that this method of profiling the kernel is clang-native, unlike
the clang support in kernel/gcov.

[1] https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization

Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Co-developed-by: Bill Wendling <morbo@google.com>
Signed-off-by: Bill Wendling <morbo@google.com>
Tested-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Fangrui Song <maskray@google.com>
Reviewed-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20210407211704.367039-1-morbo@google.com
25 files changed:
Documentation/dev-tools/index.rst
Documentation/dev-tools/pgo.rst [new file with mode: 0644]
MAINTAINERS
Makefile
arch/Kconfig
arch/x86/Kconfig
arch/x86/boot/Makefile
arch/x86/boot/compressed/Makefile
arch/x86/crypto/Makefile
arch/x86/entry/vdso/Makefile
arch/x86/kernel/Makefile
arch/x86/kernel/vmlinux.lds.S
arch/x86/platform/efi/Makefile
arch/x86/purgatory/Makefile
arch/x86/realmode/rm/Makefile
arch/x86/um/vdso/Makefile
drivers/firmware/efi/libstub/Makefile
include/asm-generic/vmlinux.lds.h
kernel/Makefile
kernel/pgo/Kconfig [new file with mode: 0644]
kernel/pgo/Makefile [new file with mode: 0644]
kernel/pgo/fs.c [new file with mode: 0644]
kernel/pgo/instrument.c [new file with mode: 0644]
kernel/pgo/pgo.h [new file with mode: 0644]
scripts/Makefile.lib