pgo: add clang's Profile Guided Optimization infrastructure
Subsystems (24)
PATCH 1/1 pgo: add clang's Profile Guided Optimization infrastructure
From: Sami Tolvanen <[email protected]>
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 be used either by the compiler if LTO isn't enabled:
... -fprofile-use=vmlinux.profdata ...
or by LLD if LTO is enabled:
... -lto-cs-profile-file=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 and isn't compatible with clang's gcov support in kernel/gcov.
[1] https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization
--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/pgo.rst | 127 +++++++++ MAINTAINERS | 9 + Makefile | 3 + arch/Kconfig | 1 + arch/arm/boot/bootp/Makefile | 1 + arch/arm/boot/compressed/Makefile | 1 + arch/arm/vdso/Makefile | 3 +- arch/arm64/kernel/vdso/Makefile | 3 +- arch/arm64/kvm/hyp/nvhe/Makefile | 1 + arch/mips/boot/compressed/Makefile | 1 + arch/mips/vdso/Makefile | 1 + arch/nds32/kernel/vdso/Makefile | 4 +- arch/parisc/boot/compressed/Makefile | 1 + arch/powerpc/kernel/Makefile | 6 +- arch/powerpc/kernel/trace/Makefile | 3 +- arch/powerpc/kernel/vdso32/Makefile | 1 + arch/powerpc/kernel/vdso64/Makefile | 1 + arch/powerpc/kexec/Makefile | 3 +- arch/powerpc/xmon/Makefile | 1 + arch/riscv/kernel/vdso/Makefile | 3 +- arch/s390/boot/Makefile | 1 + arch/s390/boot/compressed/Makefile | 1 + arch/s390/kernel/Makefile | 1 + arch/s390/kernel/vdso64/Makefile | 3 +- arch/s390/purgatory/Makefile | 1 + arch/sh/boot/compressed/Makefile | 1 + arch/sh/mm/Makefile | 1 + arch/sparc/vdso/Makefile | 1 + arch/x86/Kconfig | 1 + arch/x86/boot/Makefile | 1 + arch/x86/boot/compressed/Makefile | 1 + arch/x86/entry/vdso/Makefile | 1 + arch/x86/kernel/vmlinux.lds.S | 2 + arch/x86/platform/efi/Makefile | 1 + arch/x86/purgatory/Makefile | 1 + arch/x86/realmode/rm/Makefile | 1 + arch/x86/um/vdso/Makefile | 1 + drivers/firmware/efi/libstub/Makefile | 1 + drivers/s390/char/Makefile | 1 + include/asm-generic/vmlinux.lds.h | 44 +++ kernel/Makefile | 1 + kernel/pgo/Kconfig | 34 +++ kernel/pgo/Makefile | 5 + kernel/pgo/fs.c | 382 ++++++++++++++++++++++++++ kernel/pgo/instrument.c | 147 ++++++++++ kernel/pgo/pgo.h | 206 ++++++++++++++ scripts/Makefile.lib | 10 + 48 files changed, 1017 insertions(+), 9 deletions(-) create mode 100644 Documentation/dev-tools/pgo.rst create mode 100644 kernel/pgo/Kconfig create mode 100644 kernel/pgo/Makefile create mode 100644 kernel/pgo/fs.c create mode 100644 kernel/pgo/instrument.c create mode 100644 kernel/pgo/pgo.h
Documentation/dev-tools/index.rst +1 -0
@@ -26,6 +26,7 @@ whole; patches welcome! kgdb kselftest kunit/index + pgo .. only:: subproject and html
Documentation/dev-tools/pgo.rst new file +127 -0
127 lines not on this page · raw message
MAINTAINERS +9 -0
@@ -13955,6 +13955,15 @@ S: Maintained F: include/linux/personality.h F: include/uapi/linux/personality.h +PGO BASED KERNEL PROFILING +M: Sami Tolvanen <[email protected]> +M: Bill Wendling <[email protected]> +R: Nathan Chancellor <[email protected]> +R: Nick Desaulniers <[email protected]> +S: Supported +F: Documentation/dev-tools/pgo.rst +F: kernel/pgo + PHOENIX RC FLIGHT CONTROLLER ADAPTER M: Marcus Folkesson <[email protected]> L: [email protected]
Makefile +3 -0
@@ -659,6 +659,9 @@ endif # KBUILD_EXTMOD # Defaults to vmlinux, but the arch makefile usually adds further targets all: vmlinux +CFLAGS_PGO_CLANG := -fprofile-generate +export CFLAGS_PGO_CLANG + CFLAGS_GCOV := -fprofile-arcs -ftest-coverage \ $(call cc-option,-fno-tree-loop-im) \ $(call cc-disable-warning,maybe-uninitialized,)
arch/Kconfig +1 -0
@@ -1106,6 +1106,7 @@ config ARCH_SUPPORTS_DEBUG_PAGEALLOC bool source "kernel/gcov/Kconfig" +source "kernel/pgo/Kconfig" source "scripts/gcc-plugins/Kconfig"
arch/arm/boot/bootp/Makefile +1 -0
@@ -7,6 +7,7 @@ # GCOV_PROFILE := n +PGO_PROFILE := n LDFLAGS_bootp := --no-undefined -X \ --defsym initrd_phys=$(INITRD_PHYS) \
arch/arm/boot/compressed/Makefile +1 -0
@@ -24,6 +24,7 @@ OBJS += hyp-stub.o endif GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.
arch/arm/vdso/Makefile +2 -1
@@ -36,8 +36,9 @@ else CFLAGS_vgettimeofday.o = -O2 -include $(c-gettimeofday-y) endif -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in. KCOV_INSTRUMENT := n
arch/arm64/kernel/vdso/Makefile +2 -1
@@ -41,8 +41,9 @@ ifneq ($(c-gettimeofday-y),) CFLAGS_vgettimeofday.o += -include $(c-gettimeofday-y) endif -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n obj-y += vdso.o targets += vdso.lds
arch/arm64/kvm/hyp/nvhe/Makefile +1 -0
@@ -60,6 +60,7 @@ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS), $(KBUILD_CFLAG # compiler instrumentation that inserts callbacks or checks into the code may # cause crashes. Just disable it. GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n UBSAN_SANITIZE := n KCOV_INSTRUMENT := n
arch/mips/boot/compressed/Makefile +1 -0
@@ -37,6 +37,7 @@ KBUILD_AFLAGS := $(KBUILD_AFLAGS) -D__ASSEMBLY__ \ # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in. KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n # decompressor objects (linked with vmlinuz) vmlinuzobjs-y := $(obj)/head.o $(obj)/decompress.o $(obj)/string.o
arch/mips/vdso/Makefile +1 -0
@@ -66,6 +66,7 @@ ldflags-y := -Bsymbolic --no-undefined -soname=linux-vdso.so.1 \ CFLAGS_REMOVE_vdso.o = -pg GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KCOV_INSTRUMENT := n
arch/nds32/kernel/vdso/Makefile +2 -2
@@ -15,9 +15,9 @@ obj-vdso := $(addprefix $(obj)/, $(obj-vdso)) ccflags-y := -shared -fno-common -fno-builtin -nostdlib -fPIC -Wl,-shared -g \ -Wl,-soname=linux-vdso.so.1 -Wl,--hash-style=sysv -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n - +PGO_PROFILE := n obj-y += vdso.o targets += vdso.lds
arch/parisc/boot/compressed/Makefile +1 -0
@@ -7,6 +7,7 @@ KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n targets := vmlinux.lds vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2
arch/powerpc/kernel/Makefile +5 -1
@@ -153,17 +153,21 @@ endif obj-$(CONFIG_PPC_SECURE_BOOT) += secure_boot.o ima_arch.o secvar-ops.o obj-$(CONFIG_PPC_SECVAR_SYSFS) += secvar-sysfs.o -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive code GCOV_PROFILE_prom_init.o := n +PGO_PROFILE_prom_init.o := n KCOV_INSTRUMENT_prom_init.o := n UBSAN_SANITIZE_prom_init.o := n GCOV_PROFILE_kprobes.o := n +PGO_PROFILE_kprobes.o := n KCOV_INSTRUMENT_kprobes.o := n UBSAN_SANITIZE_kprobes.o := n GCOV_PROFILE_kprobes-ftrace.o := n +PGO_PROFILE_kprobes-ftrace.o := n KCOV_INSTRUMENT_kprobes-ftrace.o := n UBSAN_SANITIZE_kprobes-ftrace.o := n GCOV_PROFILE_syscall_64.o := n +PGO_PROFILE_syscall_64.o := n KCOV_INSTRUMENT_syscall_64.o := n UBSAN_SANITIZE_syscall_64.o := n UBSAN_SANITIZE_vdso.o := n
arch/powerpc/kernel/trace/Makefile +2 -1
@@ -23,7 +23,8 @@ obj-$(CONFIG_TRACING) += trace_clock.o obj-$(CONFIG_PPC64) += $(obj64-y) obj-$(CONFIG_PPC32) += $(obj32-y) -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive code GCOV_PROFILE_ftrace.o := n +PGO_PROFILE_ftrace.o := n KCOV_INSTRUMENT_ftrace.o := n UBSAN_SANITIZE_ftrace.o := n
arch/powerpc/kernel/vdso32/Makefile +1 -0
@@ -34,6 +34,7 @@ targets := $(obj-vdso32) vdso32.so.dbg obj-vdso32 := $(addprefix $(obj)/, $(obj-vdso32)) GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/powerpc/kernel/vdso64/Makefile +1 -0
@@ -21,6 +21,7 @@ targets := $(obj-vdso64) vdso64.so.dbg obj-vdso64 := $(addprefix $(obj)/, $(obj-vdso64)) GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/powerpc/kexec/Makefile +2 -1
@@ -16,7 +16,8 @@ endif endif -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive code GCOV_PROFILE_core_$(BITS).o := n +PGO_PROFILE_core_$(BITS).o := n KCOV_INSTRUMENT_core_$(BITS).o := n UBSAN_SANITIZE_core_$(BITS).o := n
arch/powerpc/xmon/Makefile +1 -0
@@ -2,6 +2,7 @@ # Makefile for xmon GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/riscv/kernel/vdso/Makefile +2 -1
@@ -32,8 +32,9 @@ CPPFLAGS_vdso.lds += -P -C -U$(ARCH) # Disable -pg to prevent insert call site CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n # Force dependency
arch/s390/boot/Makefile +1 -0
@@ -5,6 +5,7 @@ KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/s390/boot/compressed/Makefile +1 -0
@@ -7,6 +7,7 @@ KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/s390/kernel/Makefile +1 -0
@@ -14,6 +14,7 @@ CFLAGS_REMOVE_early.o = $(CC_FLAGS_FTRACE) endif GCOV_PROFILE_early.o := n +PGO_PROFILE_early.o := n KCOV_INSTRUMENT_early.o := n UBSAN_SANITIZE_early.o := n KASAN_SANITIZE_ipl.o := n
arch/s390/kernel/vdso64/Makefile +2 -1
@@ -35,8 +35,9 @@ obj-y += vdso64_wrapper.o targets += vdso64.lds CPPFLAGS_vdso64.lds += -P -C -U$(ARCH) -# Disable gcov profiling, ubsan and kasan for VDSO code +# Disable gcov and PGO profiling, ubsan and kasan for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/s390/purgatory/Makefile +1 -0
@@ -17,6 +17,7 @@ $(obj)/mem.o: $(srctree)/arch/s390/lib/mem.S FORCE KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/sh/boot/compressed/Makefile +1 -0
@@ -13,6 +13,7 @@ targets := vmlinux vmlinux.bin vmlinux.bin.gz \ OBJECTS = $(obj)/head_32.o $(obj)/misc.o $(obj)/cache.o GCOV_PROFILE := n +PGO_PROFILE := n # # IMAGE_OFFSET is the load offset of the compression loader
arch/sh/mm/Makefile +1 -0
@@ -43,3 +43,4 @@ obj-$(CONFIG_UNCACHED_MAPPING) += uncached.o obj-$(CONFIG_HAVE_SRAM_POOL) += sram.o GCOV_PROFILE_pmb.o := n +PGO_PROFILE_pmb.o := n
arch/sparc/vdso/Makefile +1 -0
@@ -115,6 +115,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 -Bsymbolic GCOV_PROFILE := n +PGO_PROFILE := n # # Install the unstripped copies of vdso*.so. If our toolchain supports
arch/x86/Kconfig +1 -0
@@ -95,6 +95,7 @@ config X86 select ARCH_SUPPORTS_DEBUG_PAGEALLOC select ARCH_SUPPORTS_NUMA_BALANCING if X86_64 select ARCH_SUPPORTS_KMAP_LOCAL_FORCE_MAP if NR_CPUS <= 4096 + select ARCH_SUPPORTS_PGO_CLANG if X86_64 select ARCH_USE_BUILTIN_BSWAP select ARCH_USE_QUEUED_RWLOCKS select ARCH_USE_QUEUED_SPINLOCKS
arch/x86/boot/Makefile +1 -0
@@ -71,6 +71,7 @@ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=) KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n $(obj)/bzImage: asflags-y := $(SVGA_MODE)
arch/x86/boot/compressed/Makefile +1 -0
@@ -54,6 +54,7 @@ CFLAGS_sev-es.o += -I$(objtree)/arch/x86/lib/ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE :=n KBUILD_LDFLAGS := -m elf_$(UTS_MACHINE)
arch/x86/entry/vdso/Makefile +1 -0
@@ -179,6 +179,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 \ $(call ld-option, --eh-frame-hdr) -Bsymbolic GCOV_PROFILE := n +PGO_PROFILE := n quiet_cmd_vdso_and_check = VDSO $@ cmd_vdso_and_check = $(cmd_vdso); $(cmd_vdso_check)
arch/x86/kernel/vmlinux.lds.S +2 -0
@@ -184,6 +184,8 @@ SECTIONS BUG_TABLE + PGO_CLANG_DATA + ORC_UNWIND_TABLE . = ALIGN(PAGE_SIZE);
arch/x86/platform/efi/Makefile +1 -0
@@ -2,6 +2,7 @@ OBJECT_FILES_NON_STANDARD_efi_thunk_$(BITS).o := y KASAN_SANITIZE := n GCOV_PROFILE := n +PGO_PROFILE := n obj-$(CONFIG_EFI) += quirks.o efi.o efi_$(BITS).o efi_stub_$(BITS).o obj-$(CONFIG_EFI_MIXED) += efi_thunk_$(BITS).o
arch/x86/purgatory/Makefile +1 -0
@@ -23,6 +23,7 @@ targets += purgatory.ro purgatory.chk # Sanitizer, etc. runtimes are unavailable and cannot be linked here. GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n UBSAN_SANITIZE := n KCSAN_SANITIZE := n
arch/x86/realmode/rm/Makefile +1 -0
@@ -76,4 +76,5 @@ KBUILD_CFLAGS := $(REALMODE_CFLAGS) -D_SETUP -D_WAKEUP \ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n
arch/x86/um/vdso/Makefile +1 -0
@@ -64,6 +64,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -fPIC -shared -Wl,--hash-style=sysv GCOV_PROFILE := n +PGO_PROFILE := n # # Install the unstripped copy of vdso*.so listed in $(vdso-install-y).
drivers/firmware/efi/libstub/Makefile +1 -0
@@ -40,6 +40,7 @@ KBUILD_CFLAGS := $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS)) GCOV_PROFILE := n +PGO_PROFILE := n # Sanitizer runtimes are unavailable and cannot be linked here. KASAN_SANITIZE := n KCSAN_SANITIZE := n
drivers/s390/char/Makefile +1 -0
@@ -9,6 +9,7 @@ CFLAGS_REMOVE_sclp_early_core.o = $(CC_FLAGS_FTRACE) endif GCOV_PROFILE_sclp_early_core.o := n +PGO_PROFILE_sclp_early_core.o := n KCOV_INSTRUMENT_sclp_early_core.o := n UBSAN_SANITIZE_sclp_early_core.o := n KASAN_SANITIZE_sclp_early_core.o := n
include/asm-generic/vmlinux.lds.h +44 -0
56 lines not on this page · raw message
kernel/Makefile +1 -0
@@ -111,6 +111,7 @@ obj-$(CONFIG_BPF) += bpf/ obj-$(CONFIG_KCSAN) += kcsan/ obj-$(CONFIG_SHADOW_CALL_STACK) += scs.o obj-$(CONFIG_HAVE_STATIC_CALL_INLINE) += static_call.o +obj-$(CONFIG_PGO_CLANG) += pgo/ obj-$(CONFIG_PERF_EVENTS) += events/
kernel/pgo/Kconfig new file +34 -0
@@ -0,0 +1,34 @@ +# SPDX-License-Identifier: GPL-2.0-only +menu "Profile Guided Optimization (PGO) (EXPERIMENTAL)" + +config ARCH_SUPPORTS_PGO_CLANG + bool + +config PGO_CLANG + bool "Enable clang's PGO-based kernel profiling" + depends on DEBUG_FS + depends on ARCH_SUPPORTS_PGO_CLANG + help + This option enables clang's PGO (Profile Guided Optimization) based + code profiling to better optimize the kernel. + + If unsure, say N. + + Run a representative workload for your application on a kernel + compiled with this option and download the raw profile file from + /sys/kernel/debug/pgo/profraw. This file needs to be processed with + llvm-profdata. It may be merged with other collected raw profiles. + + Copy the resulting profile file into vmlinux.profdata, and enable + KCFLAGS=-fprofile-use=vmlinux.profdata to produce an optimized + kernel. + + Note that a kernel compiled with profiling flags will be + significatnly larger and run slower. Also be sure to exclude files + from profiling which are not linked to the kernel image to prevent + linker errors. + + Note that the debugfs filesystem has to be mounted to access + profiling data. + +endmenu
kernel/pgo/Makefile new file +5 -0
@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +GCOV_PROFILE := n +PGO_PROFILE := n + +obj-y += fs.o instrument.o
kernel/pgo/fs.c new file +382 -0
382 lines not on this page · raw message
kernel/pgo/instrument.c new file +147 -0
147 lines not on this page · raw message
kernel/pgo/pgo.h new file +206 -0
206 lines not on this page · raw message
scripts/Makefile.lib +10 -0
@@ -143,6 +143,16 @@ _c_flags += $(if $(patsubst n%,, \ $(CFLAGS_GCOV)) endif +# +# Enable clang's PGO profiling flags for a file or directory depending on +# variables PGO_PROFILE_obj.o and PGO_PROFILE. +# +ifeq ($(CONFIG_PGO_CLANG),y) +_c_flags += $(if $(patsubst n%,, \ + $(PGO_PROFILE_$(basetarget).o)$(PGO_PROFILE)y), \ + $(CFLAGS_PGO_CLANG)) +endif + # # Enable address sanitizer flags for kernel except some files or directories # we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE)
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
Quoted 24 lines · Bill Wendling' via Clang Built Linux
From: Sami Tolvanen <[email protected]>
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.profrawMultiple raw profiles may be merged during this step.
The data can be used either by the compiler if LTO isn't enabled:
... -fprofile-use=vmlinux.profdata ...or by LLD if LTO is enabled:
... -lto-cs-profile-file=vmlinux.profdata ...
Quoted 9 lines
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 and isn't compatible with clang's gcov support in kernel/gcov.
[1] https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization
Hi Bill and Sami,
I have seen the pull-request in the CBL issue tracker and had some questions in mind.
Good you send this.
First of all, I like to fetch any development stuff easily from a Git repository.
Can you offer this, please?
What is the base for your work?
I hope this is (fresh released) Linux v5.11-rc3.
I myself had some experiences with a PGO + ThinLTO optimized LLVM toolchain built with the help of tc-build.
Here it takes very long to build it.
This means I have some profile-data archived.
Can I use it?
Is an own PGO + ThinLTO optimized LLVM toolchain pre-requirement for this or not?
That is one of my important questions.
Thanks for your work.
Regards,
- Sedat -
Quoted 1,608 lines
Signed-off-by Sami Tolvanen <[email protected]>Co-developed-by Bill Wendling <[email protected]>Signed-off-by Bill Wendling <[email protected]>--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/pgo.rst | 127 +++++++++ MAINTAINERS | 9 + Makefile | 3 + arch/Kconfig | 1 + arch/arm/boot/bootp/Makefile | 1 + arch/arm/boot/compressed/Makefile | 1 + arch/arm/vdso/Makefile | 3 +- arch/arm64/kernel/vdso/Makefile | 3 +- arch/arm64/kvm/hyp/nvhe/Makefile | 1 + arch/mips/boot/compressed/Makefile | 1 + arch/mips/vdso/Makefile | 1 + arch/nds32/kernel/vdso/Makefile | 4 +- arch/parisc/boot/compressed/Makefile | 1 + arch/powerpc/kernel/Makefile | 6 +- arch/powerpc/kernel/trace/Makefile | 3 +- arch/powerpc/kernel/vdso32/Makefile | 1 + arch/powerpc/kernel/vdso64/Makefile | 1 + arch/powerpc/kexec/Makefile | 3 +- arch/powerpc/xmon/Makefile | 1 + arch/riscv/kernel/vdso/Makefile | 3 +- arch/s390/boot/Makefile | 1 + arch/s390/boot/compressed/Makefile | 1 + arch/s390/kernel/Makefile | 1 + arch/s390/kernel/vdso64/Makefile | 3 +- arch/s390/purgatory/Makefile | 1 + arch/sh/boot/compressed/Makefile | 1 + arch/sh/mm/Makefile | 1 + arch/sparc/vdso/Makefile | 1 + arch/x86/Kconfig | 1 + arch/x86/boot/Makefile | 1 + arch/x86/boot/compressed/Makefile | 1 + arch/x86/entry/vdso/Makefile | 1 + arch/x86/kernel/vmlinux.lds.S | 2 + arch/x86/platform/efi/Makefile | 1 + arch/x86/purgatory/Makefile | 1 + arch/x86/realmode/rm/Makefile | 1 + arch/x86/um/vdso/Makefile | 1 + drivers/firmware/efi/libstub/Makefile | 1 + drivers/s390/char/Makefile | 1 + include/asm-generic/vmlinux.lds.h | 44 +++ kernel/Makefile | 1 + kernel/pgo/Kconfig | 34 +++ kernel/pgo/Makefile | 5 + kernel/pgo/fs.c | 382 ++++++++++++++++++++++++++ kernel/pgo/instrument.c | 147 ++++++++++ kernel/pgo/pgo.h | 206 ++++++++++++++ scripts/Makefile.lib | 10 + 48 files changed, 1017 insertions(+), 9 deletions(-) create mode 100644 Documentation/dev-tools/pgo.rst create mode 100644 kernel/pgo/Kconfig create mode 100644 kernel/pgo/Makefile create mode 100644 kernel/pgo/fs.c create mode 100644 kernel/pgo/instrument.c create mode 100644 kernel/pgo/pgo.hDocumentation/dev-tools/index.rst +1
-0@@ -26,6 +26,7 @@ whole; patches welcome! kgdb kselftest kunit/index + pgo .. only:: subproject and htmlDocumentation/dev-tools/pgo.rst new file +127
-0@@ -0,0 +1,127 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=============================== +Using PGO with the Linux kernel +=============================== + +Clang's profiling kernel support (PGO_) enables profiling of the Linux kernel +when building with Clang. The profiling data is exported via the ``pgo`` +debugfs directory. + +.. _PGO: https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization + + +Preparation +=========== + +Configure the kernel with: + +.. code-block:: make + + CONFIG_DEBUG_FS=y + CONFIG_PGO_CLANG=y + +Note that kernels compiled with profiling flags will be significantly larger +and run slower. + +Profiling data will only become accessible once debugfs has been mounted: + +.. code-block:: sh + + mount -t debugfs none /sys/kernel/debug + + +Customization +============= + +You can enable or disable profiling for individual file and directories by +adding a line similar to the following to the respective kernel Makefile: + +- For a single file (e.g. main.o) + + .. code-block:: make + + PGO_PROFILE_main.o := y + +- For all files in one directory + + .. code-block:: make + + PGO_PROFILE := y + +To exclude files from being profiled use + + .. code-block:: make + + PGO_PROFILE_main.o := n + +and + + .. code-block:: make + + PGO_PROFILE := n + +Only files which are linked to the main kernel image or are compiled as kernel +modules are supported by this mechanism. + + +Files +===== + +The PGO kernel support creates the following files in debugfs: + +``/sys/kernel/debug/pgo`` + Parent directory for all PGO-related files. + +``/sys/kernel/debug/pgo/reset`` + Global reset file: resets all coverage data to zero when written to. + +``/sys/kernel/debug/profraw`` + The raw PGO data that must be processed with ``llvm_profdata``. + + +Workflow +======== + +The PGO kernel can be run on the host or test machines. The data though should +be analyzed with Clang's tools from the same Clang version as the kernel was +compiled. Clang's tolerant of version skew, but it's easier to use the same +Clang version. + +The profiling data is useful for optimizing the kernel, analyzing coverage, +etc. Clang offers tools to perform these tasks. + +Here is an example workflow for profiling an instrumented kernel with PGO and +using the result to optimize the kernel: + +1) Install the kernel on the TEST machine. + +2) Reset the data counters right before running the load tests + + .. code-block:: sh + + echo 1 > /sys/kernel/debug/pgo/reset + +3) Run the load tests. + +4) Collect the raw profile data + + .. code-block:: sh + + cp -a /sys/kernel/debug/pgo/profraw /tmp/vmlinux.profraw + +5) (Optional) Download the raw profile data to the HOST machine. + +6) Process the raw profile data + + .. code-block:: sh + + llvm-profdata merge --output=vmlinux.profdata vmlinux.profraw + + Note that multiple raw profile data files can be merged during this step. + +7) Rebuild the kernel using the profile data (PGO disabled) + + .. code-block:: sh + + make ... KCLAGS=-fprofile-use=vmlinux.profdataMAINTAINERS +9
-0@@ -13955,6 +13955,15 @@ S: Maintained F: include/linux/personality.h F: include/uapi/linux/personality.h +PGO BASED KERNEL PROFILING +M: Sami Tolvanen <[email protected]> +M: Bill Wendling <[email protected]> +R: Nathan Chancellor <[email protected]> +R: Nick Desaulniers <[email protected]> +S: Supported +F: Documentation/dev-tools/pgo.rst +F: kernel/pgo + PHOENIX RC FLIGHT CONTROLLER ADAPTER M: Marcus Folkesson <[email protected]> L: [email protected]Makefile +3
-0@@ -659,6 +659,9 @@ endif # KBUILD_EXTMOD # Defaults to vmlinux, but the arch makefile usually adds further targets all: vmlinux +CFLAGS_PGO_CLANG := -fprofile-generate +export CFLAGS_PGO_CLANG + CFLAGS_GCOV := -fprofile-arcs -ftest-coverage \ $(call cc-option,-fno-tree-loop-im) \ $(call cc-disable-warning,maybe-uninitialized,)arch/Kconfig +1
-0@@ -1106,6 +1106,7 @@ config ARCH_SUPPORTS_DEBUG_PAGEALLOC bool source "kernel/gcov/Kconfig" +source "kernel/pgo/Kconfig" source "scripts/gcc-plugins/Kconfig"arch/arm/boot/bootp/Makefile +1
-0@@ -7,6 +7,7 @@ # GCOV_PROFILE := n +PGO_PROFILE := n LDFLAGS_bootp := --no-undefined -X \ --defsym initrd_phys=$(INITRD_PHYS) \arch/arm/boot/compressed/Makefile +1
-0@@ -24,6 +24,7 @@ OBJS += hyp-stub.o endif GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.arch/arm/vdso/Makefile +2
-1@@ -36,8 +36,9 @@ else CFLAGS_vgettimeofday.o = -O2 -include $(c-gettimeofday-y) endif -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in. KCOV_INSTRUMENT := narch/arm64/kernel/vdso/Makefile +2
-1@@ -41,8 +41,9 @@ ifneq ($(c-gettimeofday-y),) CFLAGS_vgettimeofday.o += -include $(c-gettimeofday-y) endif -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n obj-y += vdso.o targets += vdso.ldsarch/arm64/kvm/hyp/nvhe/Makefile +1
-0@@ -60,6 +60,7 @@ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS), $(KBUILD_CFLAG # compiler instrumentation that inserts callbacks or checks into the code may # cause crashes. Just disable it. GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n UBSAN_SANITIZE := n KCOV_INSTRUMENT := narch/mips/boot/compressed/Makefile +1
-0@@ -37,6 +37,7 @@ KBUILD_AFLAGS := $(KBUILD_AFLAGS) -D__ASSEMBLY__ \ # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in. KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n # decompressor objects (linked with vmlinuz) vmlinuzobjs-y := $(obj)/head.o $(obj)/decompress.o $(obj)/string.oarch/mips/vdso/Makefile +1
-0@@ -66,6 +66,7 @@ ldflags-y := -Bsymbolic --no-undefined -soname=linux-vdso.so.1 \ CFLAGS_REMOVE_vdso.o = -pg GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KCOV_INSTRUMENT := narch/nds32/kernel/vdso/Makefile +2
-2@@ -15,9 +15,9 @@ obj-vdso := $(addprefix $(obj)/, $(obj-vdso)) ccflags-y := -shared -fno-common -fno-builtin -nostdlib -fPIC -Wl,-shared -g \ -Wl,-soname=linux-vdso.so.1 -Wl,--hash-style=sysv -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n - +PGO_PROFILE := n obj-y += vdso.o targets += vdso.ldsarch/parisc/boot/compressed/Makefile +1
-0@@ -7,6 +7,7 @@ KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n targets := vmlinux.lds vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2arch/powerpc/kernel/Makefile +5
-1@@ -153,17 +153,21 @@ endif obj-$(CONFIG_PPC_SECURE_BOOT) += secure_boot.o ima_arch.o secvar-ops.o obj-$(CONFIG_PPC_SECVAR_SYSFS) += secvar-sysfs.o -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive code GCOV_PROFILE_prom_init.o := n +PGO_PROFILE_prom_init.o := n KCOV_INSTRUMENT_prom_init.o := n UBSAN_SANITIZE_prom_init.o := n GCOV_PROFILE_kprobes.o := n +PGO_PROFILE_kprobes.o := n KCOV_INSTRUMENT_kprobes.o := n UBSAN_SANITIZE_kprobes.o := n GCOV_PROFILE_kprobes-ftrace.o := n +PGO_PROFILE_kprobes-ftrace.o := n KCOV_INSTRUMENT_kprobes-ftrace.o := n UBSAN_SANITIZE_kprobes-ftrace.o := n GCOV_PROFILE_syscall_64.o := n +PGO_PROFILE_syscall_64.o := n KCOV_INSTRUMENT_syscall_64.o := n UBSAN_SANITIZE_syscall_64.o := n UBSAN_SANITIZE_vdso.o := narch/powerpc/kernel/trace/Makefile +2
-1@@ -23,7 +23,8 @@ obj-$(CONFIG_TRACING) += trace_clock.o obj-$(CONFIG_PPC64) += $(obj64-y) obj-$(CONFIG_PPC32) += $(obj32-y) -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive code GCOV_PROFILE_ftrace.o := n +PGO_PROFILE_ftrace.o := n KCOV_INSTRUMENT_ftrace.o := n UBSAN_SANITIZE_ftrace.o := narch/powerpc/kernel/vdso32/Makefile +1
-0@@ -34,6 +34,7 @@ targets := $(obj-vdso32) vdso32.so.dbg obj-vdso32 := $(addprefix $(obj)/, $(obj-vdso32)) GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/powerpc/kernel/vdso64/Makefile +1
-0@@ -21,6 +21,7 @@ targets := $(obj-vdso64) vdso64.so.dbg obj-vdso64 := $(addprefix $(obj)/, $(obj-vdso64)) GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/powerpc/kexec/Makefile +2
-1@@ -16,7 +16,8 @@ endif endif -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive code GCOV_PROFILE_core_$(BITS).o := n +PGO_PROFILE_core_$(BITS).o := n KCOV_INSTRUMENT_core_$(BITS).o := n UBSAN_SANITIZE_core_$(BITS).o := narch/powerpc/xmon/Makefile +1
-0@@ -2,6 +2,7 @@ # Makefile for xmon GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/riscv/kernel/vdso/Makefile +2
-1@@ -32,8 +32,9 @@ CPPFLAGS_vdso.lds += -P -C -U$(ARCH) # Disable -pg to prevent insert call site CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n # Force dependencyarch/s390/boot/Makefile +1
-0@@ -5,6 +5,7 @@ KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/s390/boot/compressed/Makefile +1
-0@@ -7,6 +7,7 @@ KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/s390/kernel/Makefile +1
-0@@ -14,6 +14,7 @@ CFLAGS_REMOVE_early.o = $(CC_FLAGS_FTRACE) endif GCOV_PROFILE_early.o := n +PGO_PROFILE_early.o := n KCOV_INSTRUMENT_early.o := n UBSAN_SANITIZE_early.o := n KASAN_SANITIZE_ipl.o := narch/s390/kernel/vdso64/Makefile +2
-1@@ -35,8 +35,9 @@ obj-y += vdso64_wrapper.o targets += vdso64.lds CPPFLAGS_vdso64.lds += -P -C -U$(ARCH) -# Disable gcov profiling, ubsan and kasan for VDSO code +# Disable gcov and PGO profiling, ubsan and kasan for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/s390/purgatory/Makefile +1
-0@@ -17,6 +17,7 @@ $(obj)/mem.o: $(srctree)/arch/s390/lib/mem.S FORCE KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/sh/boot/compressed/Makefile +1
-0@@ -13,6 +13,7 @@ targets := vmlinux vmlinux.bin vmlinux.bin.gz \ OBJECTS = $(obj)/head_32.o $(obj)/misc.o $(obj)/cache.o GCOV_PROFILE := n +PGO_PROFILE := n # # IMAGE_OFFSET is the load offset of the compression loaderarch/sh/mm/Makefile +1
-0@@ -43,3 +43,4 @@ obj-$(CONFIG_UNCACHED_MAPPING) += uncached.o obj-$(CONFIG_HAVE_SRAM_POOL) += sram.o GCOV_PROFILE_pmb.o := n +PGO_PROFILE_pmb.o := narch/sparc/vdso/Makefile +1
-0@@ -115,6 +115,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 -Bsymbolic GCOV_PROFILE := n +PGO_PROFILE := n # # Install the unstripped copies of vdso*.so. If our toolchain supportsarch/x86/Kconfig +1
-0@@ -95,6 +95,7 @@ config X86 select ARCH_SUPPORTS_DEBUG_PAGEALLOC select ARCH_SUPPORTS_NUMA_BALANCING if X86_64 select ARCH_SUPPORTS_KMAP_LOCAL_FORCE_MAP if NR_CPUS <= 4096 + select ARCH_SUPPORTS_PGO_CLANG if X86_64 select ARCH_USE_BUILTIN_BSWAP select ARCH_USE_QUEUED_RWLOCKS select ARCH_USE_QUEUED_SPINLOCKSarch/x86/boot/Makefile +1
-0@@ -71,6 +71,7 @@ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=) KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n $(obj)/bzImage: asflags-y := $(SVGA_MODE)arch/x86/boot/compressed/Makefile +1
-0@@ -54,6 +54,7 @@ CFLAGS_sev-es.o += -I$(objtree)/arch/x86/lib/ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE :=n KBUILD_LDFLAGS := -m elf_$(UTS_MACHINE)arch/x86/entry/vdso/Makefile +1
-0@@ -179,6 +179,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 \ $(call ld-option, --eh-frame-hdr) -Bsymbolic GCOV_PROFILE := n +PGO_PROFILE := n quiet_cmd_vdso_and_check = VDSO $@ cmd_vdso_and_check = $(cmd_vdso); $(cmd_vdso_check)arch/x86/kernel/vmlinux.lds.S +2
-0@@ -184,6 +184,8 @@ SECTIONS BUG_TABLE + PGO_CLANG_DATA + ORC_UNWIND_TABLE . = ALIGN(PAGE_SIZE);arch/x86/platform/efi/Makefile +1
-0@@ -2,6 +2,7 @@ OBJECT_FILES_NON_STANDARD_efi_thunk_$(BITS).o := y KASAN_SANITIZE := n GCOV_PROFILE := n +PGO_PROFILE := n obj-$(CONFIG_EFI) += quirks.o efi.o efi_$(BITS).o efi_stub_$(BITS).o obj-$(CONFIG_EFI_MIXED) += efi_thunk_$(BITS).oarch/x86/purgatory/Makefile +1
-0@@ -23,6 +23,7 @@ targets += purgatory.ro purgatory.chk # Sanitizer, etc. runtimes are unavailable and cannot be linked here. GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n UBSAN_SANITIZE := n KCSAN_SANITIZE := narch/x86/realmode/rm/Makefile +1
-0@@ -76,4 +76,5 @@ KBUILD_CFLAGS := $(REALMODE_CFLAGS) -D_SETUP -D_WAKEUP \ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := narch/x86/um/vdso/Makefile +1
-0@@ -64,6 +64,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -fPIC -shared -Wl,--hash-style=sysv GCOV_PROFILE := n +PGO_PROFILE := n # # Install the unstripped copy of vdso*.so listed in $(vdso-install-y).drivers/firmware/efi/libstub/Makefile +1
-0@@ -40,6 +40,7 @@ KBUILD_CFLAGS := $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS)) GCOV_PROFILE := n +PGO_PROFILE := n # Sanitizer runtimes are unavailable and cannot be linked here. KASAN_SANITIZE := n KCSAN_SANITIZE := ndrivers/s390/char/Makefile +1
-0@@ -9,6 +9,7 @@ CFLAGS_REMOVE_sclp_early_core.o = $(CC_FLAGS_FTRACE) endif GCOV_PROFILE_sclp_early_core.o := n +PGO_PROFILE_sclp_early_core.o := n KCOV_INSTRUMENT_sclp_early_core.o := n UBSAN_SANITIZE_sclp_early_core.o := n KASAN_SANITIZE_sclp_early_core.o := ninclude/asm-generic/vmlinux.lds.h +44
-0@@ -316,6 +316,49 @@#define THERMAL_TABLE (name)#endif +#ifdef CONFIG_PGO_CLANG +#define PGO_CLANG_DATA \ + __llvm_prf_data : AT(ADDR(__llvm_prf_data) - LOAD_OFFSET) { \ + . = ALIGN(8); \ + __llvm_prf_start = .; \ + __llvm_prf_data_start = .; \ + KEEP(*(__llvm_prf_data)) \ + . = ALIGN(8); \ + __llvm_prf_data_end = .; \ + } \ + __llvm_prf_cnts : AT(ADDR(__llvm_prf_cnts) - LOAD_OFFSET) { \ + . = ALIGN(8); \ + __llvm_prf_cnts_start = .; \ + KEEP(*(__llvm_prf_cnts)) \ + . = ALIGN(8); \ + __llvm_prf_cnts_end = .; \ + } \ + __llvm_prf_names : AT(ADDR(__llvm_prf_names) - LOAD_OFFSET) { \ + . = ALIGN(8); \ + __llvm_prf_names_start = .; \ + KEEP(*(__llvm_prf_names)) \ + . = ALIGN(8); \ + __llvm_prf_names_end = .; \ + . = ALIGN(8); \ + } \ + __llvm_prf_vals : AT(ADDR(__llvm_prf_vals) - LOAD_OFFSET) { \ + __llvm_prf_vals_start = .; \ + KEEP(*(__llvm_prf_vals)) \ + . = ALIGN(8); \ + __llvm_prf_vals_end = .; \ + . = ALIGN(8); \ + } \ + __llvm_prf_vnds : AT(ADDR(__llvm_prf_vnds) - LOAD_OFFSET) { \ + __llvm_prf_vnds_start = .; \ + KEEP(*(__llvm_prf_vnds)) \ + . = ALIGN(8); \ + __llvm_prf_vnds_end = .; \ + __llvm_prf_end = .; \ + } +#else +#define PGO_CLANG_DATA +#endif +#define KERNEL_DTB () \ STRUCT_ALIGN(); \ __dtb_start = .; \@@ -1125,6 +1168,7 @@ CONSTRUCTORS \ } \BUG_TABLE \ + PGO_CLANG_DATA#define INIT_TEXT_SECTION (inittext_align) \ . = ALIGN(inittext_align); \kernel/Makefile +1
-0@@ -111,6 +111,7 @@ obj-$(CONFIG_BPF) += bpf/ obj-$(CONFIG_KCSAN) += kcsan/ obj-$(CONFIG_SHADOW_CALL_STACK) += scs.o obj-$(CONFIG_HAVE_STATIC_CALL_INLINE) += static_call.o +obj-$(CONFIG_PGO_CLANG) += pgo/ obj-$(CONFIG_PERF_EVENTS) += events/kernel/pgo/Kconfig new file +34
-0@@ -0,0 +1,34 @@ +# SPDX-License-Identifier: GPL-2.0-only +menu "Profile Guided Optimization (PGO) (EXPERIMENTAL)" + +config ARCH_SUPPORTS_PGO_CLANG + bool + +config PGO_CLANG + bool "Enable clang's PGO-based kernel profiling" + depends on DEBUG_FS + depends on ARCH_SUPPORTS_PGO_CLANG + help + This option enables clang's PGO (Profile Guided Optimization) based + code profiling to better optimize the kernel. + + If unsure, say N. + + Run a representative workload for your application on a kernel + compiled with this option and download the raw profile file from + /sys/kernel/debug/pgo/profraw. This file needs to be processed with + llvm-profdata. It may be merged with other collected raw profiles. + + Copy the resulting profile file into vmlinux.profdata, and enable + KCFLAGS=-fprofile-use=vmlinux.profdata to produce an optimized + kernel. + + Note that a kernel compiled with profiling flags will be + significatnly larger and run slower. Also be sure to exclude files + from profiling which are not linked to the kernel image to prevent + linker errors. + + Note that the debugfs filesystem has to be mounted to access + profiling data. + +endmenukernel/pgo/Makefile new file +5
-0@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +GCOV_PROFILE := n +PGO_PROFILE := n + +obj-y += fs.o instrument.okernel/pgo/fs.c new file +382
-0@@ -0,0 +1,382 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2019 Google, Inc. +* +* Author: +* Sami Tolvanen <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#define pr_fmt (fmt) "pgo: " fmt + +#include <linux/kernel.h> +#include <linux/debugfs.h> +#include <linux/fs.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/vmalloc.h> +#include "pgo.h" + +static struct dentry *directory; + +struct prf_private_data { +void *buffer; +unsigned long size; +}; + +/* +* Raw profile data format: +* +* - llvm_prf_header +* - __llvm_prf_data +* - __llvm_prf_cnts +* - __llvm_prf_names +* - zero padding to 8 bytes +* - for each llvm_prf_data in __llvm_prf_data: +* - llvm_prf_value_data +* - llvm_prf_value_record + site count array +* - llvm_prf_value_node_data +* ... +* ... +* ... +*/ + +static void prf_fill_header (void **buffer) +{ +struct llvm_prf_header *header = *(struct llvm_prf_header **)buffer; + + header->magic =LLVM_PRF_MAGIC ; + header->version =LLVM_PRF_VARIANT_MASK_IR |LLVM_PRF_VERSION ; + header->data_size =prf_data_count (); + header->padding_bytes_before_counters =0 ; + header->counters_size =prf_cnts_count (); + header->padding_bytes_after_counters =0 ; + header->names_size =prf_names_count (); + header->counters_delta = (u64 )__llvm_prf_cnts_start; + header->names_delta = (u64 )__llvm_prf_names_start; + header->value_kind_last =LLVM_PRF_IPVK_LAST ; + + *buffer +=sizeof (*header); +} + +/* +* Copy the source into the buffer, incrementing the pointer into buffer in the +* process. +*/ +static void prf_copy_to_buffer (void **buffer,void *src,unsigned long size) +{ +memcpy (*buffer, src, size); + *buffer += size; +} + +static u32 __prf_get_value_size (struct llvm_prf_data *p,u32 *value_kinds) +{ +struct llvm_prf_value_node **nodes = + (struct llvm_prf_value_node **)p->values; +u32 kinds =0 ; +u32 size =0 ; +unsigned int kind; +unsigned int n; +unsigned int s =0 ; + +for (kind =0 ; kind <ARRAY_SIZE (p->num_value_sites); kind++) { +unsigned int sites = p->num_value_sites[kind]; + +if (!sites) +continue ; + +/* Record + site count array */ + size +=prf_get_value_record_size (sites); + kinds++; + +if (!nodes) +continue ; + +for (n =0 ; n < sites; n++) { +u32 count =0 ; +struct llvm_prf_value_node *site = nodes[s + n]; + +while (site && ++count <=U8_MAX ) + site = site->next; + + size += count * +sizeof (struct llvm_prf_value_node_data ); + } + + s += sites; + } + +if (size) + size +=sizeof (struct llvm_prf_value_data ); + +if (value_kinds) + *value_kinds = kinds; + +return size; +} + +static u32 prf_get_value_size (void ) +{ +u32 size =0 ; +struct llvm_prf_data *p; + +for (p = __llvm_prf_data_start; p < __llvm_prf_data_end; p++) + size +=__prf_get_value_size (p,NULL ); + +return size; +} + +/* Serialize the profiling's value. */ +static void prf_serialize_value (struct llvm_prf_data *p,void **buffer) +{ +struct llvm_prf_value_data header; +struct llvm_prf_value_node **nodes = + (struct llvm_prf_value_node **)p->values; +unsigned int kind; +unsigned int n; +unsigned int s =0 ; + + header.total_size =__prf_get_value_size (p, &header.num_value_kinds); + +if (!header.num_value_kinds) +/* Nothing to write. */ +return ; + +prf_copy_to_buffer (buffer, &header,sizeof (header)); + +for (kind =0 ; kind <ARRAY_SIZE (p->num_value_sites); kind++) { +struct llvm_prf_value_record *record; +u8 *counts; +unsigned int sites = p->num_value_sites[kind]; + +if (!sites) +continue ; + +/* Profiling value record. */ + record = *(struct llvm_prf_value_record **)buffer; + *buffer +=prf_get_value_record_header_size (); + + record->kind = kind; + record->num_value_sites = sites; + +/* Site count array. */ + counts = *(u8 **)buffer; + *buffer +=prf_get_value_record_site_count_size (sites); + +/* +* If we don't have nodes, we can skip updating the site count +* array, because the buffer is zero filled. +*/ +if (!nodes) +continue ; + +for (n =0 ; n < sites; n++) { +u32 count =0 ; +struct llvm_prf_value_node *site = nodes[s + n]; + +while (site && ++count <=U8_MAX ) { +prf_copy_to_buffer (buffer, site, +sizeof (struct llvm_prf_value_node_data )); + site = site->next; + } + + counts[n] = (u8 )count; + } + + s += sites; + } +} + +static void prf_serialize_values (void **buffer) +{ +struct llvm_prf_data *p; + +for (p = __llvm_prf_data_start; p < __llvm_prf_data_end; p++) +prf_serialize_value (p, buffer); +} + +static inline unsigned long prf_get_padding (unsigned long size) +{ +return 8 - (size %8 ); +} + +static unsigned long prf_buffer_size (void ) +{ +return sizeof (struct llvm_prf_header ) + +prf_data_size () + +prf_cnts_size () + +prf_names_size () + +prf_get_padding (prf_names_size ()) + +prf_get_value_size (); +} + +/* Serialize the profling data into a format LLVM's tools can understand. */ +static int prf_serialize (struct prf_private_data *p) +{ +int err =0 ; +void *buffer; + + p->size =prf_buffer_size (); + p->buffer =vzalloc (p->size); + +if (!p->buffer) { + err = -ENOMEM ; + goto out; + } + + buffer = p->buffer; + +prf_fill_header (&buffer); +prf_copy_to_buffer (&buffer, __llvm_prf_data_start,prf_data_size ()); +prf_copy_to_buffer (&buffer, __llvm_prf_cnts_start,prf_cnts_size ()); +prf_copy_to_buffer (&buffer, __llvm_prf_names_start,prf_names_size ()); + buffer +=prf_get_padding (prf_names_size ()); + +prf_serialize_values (&buffer); + +out: +return err; +} + +/* open() implementation for PGO. Creates a copy of the profiling data set. */ +static int prf_open (struct inode *inode,struct file *file) +{ +struct prf_private_data *data; +unsigned long flags; +int err; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) { + err = -ENOMEM ; + goto out; + } + + flags =prf_lock (); + + err =prf_serialize (data); +if (err) { +kfree (data); + goto out_unlock; + } + + file->private_data = data; + +out_unlock: +prf_unlock (flags); +out: +return err; +} + +/* read() implementation for PGO. */ +static ssize_t prf_read (struct file *file,char __user *buf,size_t count, +loff_t *ppos) +{ +struct prf_private_data *data = file->private_data; + +BUG_ON (!data); + +return simple_read_from_buffer (buf, count, ppos, data->buffer, + data->size); +} + +/* release() implementation for PGO. Release resources allocated by open(). */ +static int prf_release (struct inode *inode,struct file *file) +{ +struct prf_private_data *data = file->private_data; + +if (data) { +vfree (data->buffer); +kfree (data); + } + +return 0 ; +} + +static const struct file_operations prf_fops = { + .owner =THIS_MODULE , + .open = prf_open, + .read = prf_read, + .llseek = default_llseek, + .release = prf_release +}; + +/* write() implementation for resetting PGO's profile data. */ +static ssize_t reset_write (struct file *file,const char __user *addr, +size_t len,loff_t *pos) +{ +struct llvm_prf_data *data; + +memset (__llvm_prf_cnts_start,0 ,prf_cnts_size ()); + +for (data = __llvm_prf_data_start; data < __llvm_prf_data_end; ++data) { +struct llvm_prf_value_node **vnodes; +u64 current_vsite_count; +u32 i; + +if (!data->values) +continue ; + + current_vsite_count =0 ; + vnodes = (struct llvm_prf_value_node **)data->values; + +for (i =LLVM_PRF_IPVK_FIRST ; i <=LLVM_PRF_IPVK_LAST ; ++i) + current_vsite_count += data->num_value_sites[i]; + +for (i =0 ; i < current_vsite_count; ++i) { +struct llvm_prf_value_node *current_vnode = vnodes[i]; + +while (current_vnode) { + current_vnode->count =0 ; + current_vnode = current_vnode->next; + } + } + } + +return len; +} + +static const struct file_operations prf_reset_fops = { + .owner =THIS_MODULE , + .write = reset_write, + .llseek = noop_llseek, +}; + +/* Create debugfs entries. */ +static int __init pgo_init (void ) +{ + directory =debugfs_create_dir ("pgo" ,NULL ); +if (!directory) + goto err_remove; + +if (!debugfs_create_file ("profraw" ,0600 , directory,NULL , + &prf_fops)) + goto err_remove; + +if (!debugfs_create_file ("reset" ,0200 , directory,NULL , + &prf_reset_fops)) + goto err_remove; + +return 0 ; + +err_remove: +pr_err ("initialization failed\n" ); +return -EIO ; +} + +/* Remove debufs entries. */ +static void __exit pgo_exit(void) +{ +debugfs_remove_recursive (directory); +} + +module_init (pgo_init); +module_exit (pgo_exit);kernel/pgo/instrument.c new file +147
-0@@ -0,0 +1,147 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2019 Google, Inc. +* +* Author: +* Sami Tolvanen <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#define pr_fmt (fmt) "pgo: " fmt + +#include <linux/kernel.h> +#include <linux/export.h> +#include <linux/spinlock.h> +#include <linux/types.h> +#include "pgo.h" + +/* Lock guarding value node access and serialization. */ +static DEFINE_SPINLOCK (pgo_lock); +static int current_node; + +unsigned long prf_lock (void ) +{ +unsigned long flags; + +spin_lock_irqsave (&pgo_lock, flags); + +return flags; +} + +void prf_unlock (unsigned long flags) +{ +spin_unlock_irqrestore (&pgo_lock, flags); +} + +/* +* Return a newly allocated profiling value node which contains the tracked +* value by the value profiler. +* Note: caller *must* hold pgo_lock. +*/ +static struct llvm_prf_value_node *allocate_node (struct llvm_prf_data *p, +u32 index,u64 value) +{ +if (&__llvm_prf_vnds_start[current_node +1 ] >= __llvm_prf_vnds_end) +return NULL ;/* Out of nodes */ + + current_node++; + +/* Make sure the node is entirely within the section */ +if (&__llvm_prf_vnds_start[current_node] >= __llvm_prf_vnds_end || + &__llvm_prf_vnds_start[current_node +1 ] > __llvm_prf_vnds_end) +return NULL ; + +return &__llvm_prf_vnds_start[current_node]; +} + +/* +* Counts the number of times a target value is seen. +* +* Records the target value for the CounterIndex if not seen before. Otherwise, +* increments the counter associated w/ the target value. +*/ +void __llvm_profile_instrument_target (u64 target_value,void *data,u32 index) +{ +struct llvm_prf_data *p = (struct llvm_prf_data *)data; +struct llvm_prf_value_node **counters; +struct llvm_prf_value_node *curr; +struct llvm_prf_value_node *min =NULL ; +struct llvm_prf_value_node *prev =NULL ; +u64 min_count =U64_MAX ; +u8 values =0 ; +unsigned long flags; + +if (!p || !p->values) +return ; + + counters = (struct llvm_prf_value_node **)p->values; + curr = counters[index]; + +while (curr) { +if (target_value == curr->value) { + curr->count++; +return ; + } + +if (curr->count < min_count) { + min_count = curr->count; + min = curr; + } + + prev = curr; + curr = curr->next; + values++; + } + +if (values >=LLVM_PRF_MAX_NUM_VALS_PER_SITE ) { +if (!min->count || !(--min->count)) { + curr = min; + curr->value = target_value; + curr->count++; + } +return ; + } + +/* Lock when updating the value node structure. */ + flags =prf_lock (); + + curr =allocate_node (p, index, target_value); +if (!curr) + goto out; + + curr->value = target_value; + curr->count++; + +if (!counters[index]) + counters[index] = curr; +else if (prev && !prev->next) + prev->next = curr; + +out: +prf_unlock (flags); +} +EXPORT_SYMBOL (__llvm_profile_instrument_target); + +/* Counts the number of times a range of targets values are seen. */ +void __llvm_profile_instrument_range (u64 target_value,void *data, +u32 index,s64 precise_start, +s64 precise_last,s64 large_value) +{ +if (large_value !=S64_MIN && (s64 )target_value >= large_value) + target_value = large_value; +else if ((s64 )target_value < precise_start || + (s64 )target_value > precise_last) + target_value = precise_last +1 ; + +__llvm_profile_instrument_target (target_value, data, index); +} +EXPORT_SYMBOL (__llvm_profile_instrument_range);kernel/pgo/pgo.h new file +206
-0@@ -0,0 +1,206 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2019 Google, Inc. +* +* Author: +* Sami Tolvanen <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef _PGO_H +#define _PGO_H + +/* +* Note: These internal LLVM definitions must match the compiler version. +* See llvm/include/llvm/ProfileData/InstrProfData.inc in LLVM's source code. +*/ + +#ifdef CONFIG_64BIT +#define LLVM_PRF_MAGIC \ + ((u64)255 << 56 | \ + (u64)'l' << 48 | \ + (u64)'p' << 40 | \ + (u64)'r' << 32 | \ + (u64)'o' << 24 | \ + (u64)'f' << 16 | \ + (u64)'r' << 8 | \ + (u64)129) +#else +#define LLVM_PRF_MAGIC \ + ((u64)255 << 56 | \ + (u64)'l' << 48 | \ + (u64)'p' << 40 | \ + (u64)'r' << 32 | \ + (u64)'o' << 24 | \ + (u64)'f' << 16 | \ + (u64)'R' << 8 | \ + (u64)129) +#endif + +#define LLVM_PRF_VERSION 5 +#define LLVM_PRF_DATA_ALIGN 8 +#define LLVM_PRF_IPVK_FIRST 0 +#define LLVM_PRF_IPVK_LAST 1 +#define LLVM_PRF_MAX_NUM_VALS_PER_SITE 16 + +#define LLVM_PRF_VARIANT_MASK_IR (0x1ull << 56) +#define LLVM_PRF_VARIANT_MASK_CSIR (0x1ull << 57) + +/** +* struct llvm_prf_header - represents the raw profile header data structure. +* @magic: the magic token for the file format. +* @version: the version of the file format. +* @data_size: the number of entries in the profile data section. +* @padding_bytes_before_counters: the number of padding bytes before the +* counters. +* @counters_size: the size in bytes of the LLVM profile section containing the +* counters. +* @padding_bytes_after_counters: the number of padding bytes after the +* counters. +* @names_size: the size in bytes of the LLVM profile section containing the +* counters' names. +* @counters_delta: the beginning of the LLMV profile counters section. +* @names_delta: the beginning of the LLMV profile names section. +* @value_kind_last: the last profile value kind. +*/ +struct llvm_prf_header { +u64 magic; +u64 version; +u64 data_size; +u64 padding_bytes_before_counters; +u64 counters_size; +u64 padding_bytes_after_counters; +u64 names_size; +u64 counters_delta; +u64 names_delta; +u64 value_kind_last; +}; + +/** +* struct llvm_prf_data - represents the per-function control structure. +* @name_ref: the reference to the function's name. +* @func_hash: the hash value of the function. +* @counter_ptr: a pointer to the profile counter. +* @function_ptr: a pointer to the function. +* @values: the profiling values associated with this function. +* @num_counters: the number of counters in the function. +* @num_value_sites: the number of value profile sites. +*/ +struct llvm_prf_data { +const u64 name_ref; +const u64 func_hash; +const void *counter_ptr; +const void *function_ptr; +void *values; +const u32 num_counters; +const u16 num_value_sites[LLVM_PRF_IPVK_LAST +1 ]; +}__aligned(LLVM_PRF_DATA_ALIGN) ; + +/** +* structure llvm_prf_value_node_data - represents the data part of the struct +* llvm_prf_value_node data structure. +* @value: the value counters. +* @count: the counters' count. +*/ +struct llvm_prf_value_node_data { +u64 value; +u64 count; +}; + +/** +* struct llvm_prf_value_node - represents an internal data structure used by +* the value profiler. +* @value: the value counters. +* @count: the counters' count. +* @next: the next value node. +*/ +struct llvm_prf_value_node { +u64 value; +u64 count; +struct llvm_prf_value_node *next; +}; + +/** +* struct llvm_prf_value_data - represents the value profiling data in indexed +* format. +* @total_size: the total size in bytes including this field. +* @num_value_kinds: the number of value profile kinds that has value profile +* data. +*/ +struct llvm_prf_value_data { +u32 total_size; +u32 num_value_kinds; +}; + +/** +* struct llvm_prf_value_record - represents the on-disk layout of the value +* profile data of a particular kind for one function. +* @kind: the kind of the value profile record. +* @num_value_sites: the number of value profile sites. +* @site_count_array: the first element of the array that stores the number +* of profiled values for each value site. +*/ +struct llvm_prf_value_record { +u32 kind; +u32 num_value_sites; +u8 site_count_array[]; +}; + +#define prf_get_value_record_header_size () \ + offsetof(struct llvm_prf_value_record, site_count_array) +#define prf_get_value_record_site_count_size (sites) \ + roundup((sites), 8) +#define prf_get_value_record_size (sites) \ + (prf_get_value_record_header_size() + \ + prf_get_value_record_site_count_size((sites))) + +/* Data sections */ +extern struct llvm_prf_data __llvm_prf_data_start[]; +extern struct llvm_prf_data __llvm_prf_data_end[]; + +extern u64 __llvm_prf_cnts_start[]; +extern u64 __llvm_prf_cnts_end[]; + +extern char __llvm_prf_names_start[]; +extern char __llvm_prf_names_end[]; + +extern struct llvm_prf_value_node __llvm_prf_vnds_start[]; +extern struct llvm_prf_value_node __llvm_prf_vnds_end[]; + +/* Locking for vnodes */ +extern unsigned long prf_lock (void ); +extern void prf_unlock (unsigned long flags); + +#define __DEFINE_PRF_SIZE (s) \ + static inline unsigned long prf_ ## s ## _size(void) \ + { \ + unsigned long start = \ + (unsigned long)__llvm_prf_ ## s ## _start; \ + unsigned long end = \ + (unsigned long)__llvm_prf_ ## s ## _end; \ + return roundup(end - start, \ + sizeof(__llvm_prf_ ## s ## _start[0])); \ + } \ + static inline unsigned long prf_ ## s ## _count(void) \ + { \ + return prf_ ## s ## _size() / \ + sizeof(__llvm_prf_ ## s ## _start[0]); \ + } + +__DEFINE_PRF_SIZE (data); +__DEFINE_PRF_SIZE (cnts); +__DEFINE_PRF_SIZE (names); +__DEFINE_PRF_SIZE (vnds); + +#undef __DEFINE_PRF_SIZE + +#endif /* _PGO_H */ scripts/Makefile.lib +10
-1@@ -143,6 +143,16 @@ _c_flags += $(if $(patsubst n%,, \ $(CFLAGS_GCOV)) endif +# +# Enable clang's PGO profiling flags for a file or directory depending on +# variables PGO_PROFILE_obj.o and PGO_PROFILE. +# +ifeq ($(CONFIG_PGO_CLANG),y) +_c_flags += $(if $(patsubst n%,, \ + $(PGO_PROFILE_$(basetarget).o)$(PGO_PROFILE)y), \ + $(CFLAGS_PGO_CLANG)) +endif + # # Enable address sanitizer flags for kernel except some files or directories # we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE) --2.30.0.284.gd98b1dd5eaa7-goog
--
You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/20210111081821.3041587-1-morbo%40google.com.
Quoted 54 lines · Sedat Dilek
Quoted 24 lines · Bill Wendling' via Clang Built Linux
From: Sami Tolvanen <[email protected]>
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.profrawMultiple raw profiles may be merged during this step.
The data can be used either by the compiler if LTO isn't enabled:
... -fprofile-use=vmlinux.profdata ...or by LLD if LTO is enabled:
... -lto-cs-profile-file=vmlinux.profdata ...Quoted 9 lines
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 and isn't compatible with clang's gcov support in kernel/gcov.
[1] https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization
Hi Bill and Sami,
I have seen the pull-request in the CBL issue tracker and had some questions in mind.
Good you send this.
First of all, I like to fetch any development stuff easily from a Git repository.
Can you offer this, please?
What is the base for your work?
I hope this is (fresh released) Linux v5.11-rc3.I myself had some experiences with a PGO + ThinLTO optimized LLVM toolchain built with the help of tc-build.
Here it takes very long to build it.
Quoted 5 lines
This means I have some profile-data archived.
Can I use it?Is an own PGO + ThinLTO optimized LLVM toolchain pre-requirement for this or not?
When I recall correctly such an optimized LLVM toolchain saved here 40% of build-time.
So, I am highly interested in reducing my build-time.
- Sedat -
Quoted 1,615 lines
That is one of my important questions.
Thanks for your work.
Regards,
- Sedat -Quoted 1,608 lines
Signed-off-by Sami Tolvanen <[email protected]>Co-developed-by Bill Wendling <[email protected]>Signed-off-by Bill Wendling <[email protected]>--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/pgo.rst | 127 +++++++++ MAINTAINERS | 9 + Makefile | 3 + arch/Kconfig | 1 + arch/arm/boot/bootp/Makefile | 1 + arch/arm/boot/compressed/Makefile | 1 + arch/arm/vdso/Makefile | 3 +- arch/arm64/kernel/vdso/Makefile | 3 +- arch/arm64/kvm/hyp/nvhe/Makefile | 1 + arch/mips/boot/compressed/Makefile | 1 + arch/mips/vdso/Makefile | 1 + arch/nds32/kernel/vdso/Makefile | 4 +- arch/parisc/boot/compressed/Makefile | 1 + arch/powerpc/kernel/Makefile | 6 +- arch/powerpc/kernel/trace/Makefile | 3 +- arch/powerpc/kernel/vdso32/Makefile | 1 + arch/powerpc/kernel/vdso64/Makefile | 1 + arch/powerpc/kexec/Makefile | 3 +- arch/powerpc/xmon/Makefile | 1 + arch/riscv/kernel/vdso/Makefile | 3 +- arch/s390/boot/Makefile | 1 + arch/s390/boot/compressed/Makefile | 1 + arch/s390/kernel/Makefile | 1 + arch/s390/kernel/vdso64/Makefile | 3 +- arch/s390/purgatory/Makefile | 1 + arch/sh/boot/compressed/Makefile | 1 + arch/sh/mm/Makefile | 1 + arch/sparc/vdso/Makefile | 1 + arch/x86/Kconfig | 1 + arch/x86/boot/Makefile | 1 + arch/x86/boot/compressed/Makefile | 1 + arch/x86/entry/vdso/Makefile | 1 + arch/x86/kernel/vmlinux.lds.S | 2 + arch/x86/platform/efi/Makefile | 1 + arch/x86/purgatory/Makefile | 1 + arch/x86/realmode/rm/Makefile | 1 + arch/x86/um/vdso/Makefile | 1 + drivers/firmware/efi/libstub/Makefile | 1 + drivers/s390/char/Makefile | 1 + include/asm-generic/vmlinux.lds.h | 44 +++ kernel/Makefile | 1 + kernel/pgo/Kconfig | 34 +++ kernel/pgo/Makefile | 5 + kernel/pgo/fs.c | 382 ++++++++++++++++++++++++++ kernel/pgo/instrument.c | 147 ++++++++++ kernel/pgo/pgo.h | 206 ++++++++++++++ scripts/Makefile.lib | 10 + 48 files changed, 1017 insertions(+), 9 deletions(-) create mode 100644 Documentation/dev-tools/pgo.rst create mode 100644 kernel/pgo/Kconfig create mode 100644 kernel/pgo/Makefile create mode 100644 kernel/pgo/fs.c create mode 100644 kernel/pgo/instrument.c create mode 100644 kernel/pgo/pgo.hDocumentation/dev-tools/index.rst +1
-0@@ -26,6 +26,7 @@ whole; patches welcome! kgdb kselftest kunit/index + pgo .. only:: subproject and htmlDocumentation/dev-tools/pgo.rst new file +127
-0@@ -0,0 +1,127 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=============================== +Using PGO with the Linux kernel +=============================== + +Clang's profiling kernel support (PGO_) enables profiling of the Linux kernel +when building with Clang. The profiling data is exported via the ``pgo`` +debugfs directory. + +.. _PGO: https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization + + +Preparation +=========== + +Configure the kernel with: + +.. code-block:: make + + CONFIG_DEBUG_FS=y + CONFIG_PGO_CLANG=y + +Note that kernels compiled with profiling flags will be significantly larger +and run slower. + +Profiling data will only become accessible once debugfs has been mounted: + +.. code-block:: sh + + mount -t debugfs none /sys/kernel/debug + + +Customization +============= + +You can enable or disable profiling for individual file and directories by +adding a line similar to the following to the respective kernel Makefile: + +- For a single file (e.g. main.o) + + .. code-block:: make + + PGO_PROFILE_main.o := y + +- For all files in one directory + + .. code-block:: make + + PGO_PROFILE := y + +To exclude files from being profiled use + + .. code-block:: make + + PGO_PROFILE_main.o := n + +and + + .. code-block:: make + + PGO_PROFILE := n + +Only files which are linked to the main kernel image or are compiled as kernel +modules are supported by this mechanism. + + +Files +===== + +The PGO kernel support creates the following files in debugfs: + +``/sys/kernel/debug/pgo`` + Parent directory for all PGO-related files. + +``/sys/kernel/debug/pgo/reset`` + Global reset file: resets all coverage data to zero when written to. + +``/sys/kernel/debug/profraw`` + The raw PGO data that must be processed with ``llvm_profdata``. + + +Workflow +======== + +The PGO kernel can be run on the host or test machines. The data though should +be analyzed with Clang's tools from the same Clang version as the kernel was +compiled. Clang's tolerant of version skew, but it's easier to use the same +Clang version. + +The profiling data is useful for optimizing the kernel, analyzing coverage, +etc. Clang offers tools to perform these tasks. + +Here is an example workflow for profiling an instrumented kernel with PGO and +using the result to optimize the kernel: + +1) Install the kernel on the TEST machine. + +2) Reset the data counters right before running the load tests + + .. code-block:: sh + + echo 1 > /sys/kernel/debug/pgo/reset + +3) Run the load tests. + +4) Collect the raw profile data + + .. code-block:: sh + + cp -a /sys/kernel/debug/pgo/profraw /tmp/vmlinux.profraw + +5) (Optional) Download the raw profile data to the HOST machine. + +6) Process the raw profile data + + .. code-block:: sh + + llvm-profdata merge --output=vmlinux.profdata vmlinux.profraw + + Note that multiple raw profile data files can be merged during this step. + +7) Rebuild the kernel using the profile data (PGO disabled) + + .. code-block:: sh + + make ... KCLAGS=-fprofile-use=vmlinux.profdataMAINTAINERS +9
-0@@ -13955,6 +13955,15 @@ S: Maintained F: include/linux/personality.h F: include/uapi/linux/personality.h +PGO BASED KERNEL PROFILING +M: Sami Tolvanen <[email protected]> +M: Bill Wendling <[email protected]> +R: Nathan Chancellor <[email protected]> +R: Nick Desaulniers <[email protected]> +S: Supported +F: Documentation/dev-tools/pgo.rst +F: kernel/pgo + PHOENIX RC FLIGHT CONTROLLER ADAPTER M: Marcus Folkesson <[email protected]> L: [email protected]Makefile +3
-0@@ -659,6 +659,9 @@ endif # KBUILD_EXTMOD # Defaults to vmlinux, but the arch makefile usually adds further targets all: vmlinux +CFLAGS_PGO_CLANG := -fprofile-generate +export CFLAGS_PGO_CLANG + CFLAGS_GCOV := -fprofile-arcs -ftest-coverage \ $(call cc-option,-fno-tree-loop-im) \ $(call cc-disable-warning,maybe-uninitialized,)arch/Kconfig +1
-0@@ -1106,6 +1106,7 @@ config ARCH_SUPPORTS_DEBUG_PAGEALLOC bool source "kernel/gcov/Kconfig" +source "kernel/pgo/Kconfig" source "scripts/gcc-plugins/Kconfig"arch/arm/boot/bootp/Makefile +1
-0@@ -7,6 +7,7 @@ # GCOV_PROFILE := n +PGO_PROFILE := n LDFLAGS_bootp := --no-undefined -X \ --defsym initrd_phys=$(INITRD_PHYS) \arch/arm/boot/compressed/Makefile +1
-0@@ -24,6 +24,7 @@ OBJS += hyp-stub.o endif GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.arch/arm/vdso/Makefile +2
-1@@ -36,8 +36,9 @@ else CFLAGS_vgettimeofday.o = -O2 -include $(c-gettimeofday-y) endif -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in. KCOV_INSTRUMENT := narch/arm64/kernel/vdso/Makefile +2
-1@@ -41,8 +41,9 @@ ifneq ($(c-gettimeofday-y),) CFLAGS_vgettimeofday.o += -include $(c-gettimeofday-y) endif -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n obj-y += vdso.o targets += vdso.ldsarch/arm64/kvm/hyp/nvhe/Makefile +1
-0@@ -60,6 +60,7 @@ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS), $(KBUILD_CFLAG # compiler instrumentation that inserts callbacks or checks into the code may # cause crashes. Just disable it. GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n UBSAN_SANITIZE := n KCOV_INSTRUMENT := narch/mips/boot/compressed/Makefile +1
-0@@ -37,6 +37,7 @@ KBUILD_AFLAGS := $(KBUILD_AFLAGS) -D__ASSEMBLY__ \ # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in. KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n # decompressor objects (linked with vmlinuz) vmlinuzobjs-y := $(obj)/head.o $(obj)/decompress.o $(obj)/string.oarch/mips/vdso/Makefile +1
-0@@ -66,6 +66,7 @@ ldflags-y := -Bsymbolic --no-undefined -soname=linux-vdso.so.1 \ CFLAGS_REMOVE_vdso.o = -pg GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KCOV_INSTRUMENT := narch/nds32/kernel/vdso/Makefile +2
-2@@ -15,9 +15,9 @@ obj-vdso := $(addprefix $(obj)/, $(obj-vdso)) ccflags-y := -shared -fno-common -fno-builtin -nostdlib -fPIC -Wl,-shared -g \ -Wl,-soname=linux-vdso.so.1 -Wl,--hash-style=sysv -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n - +PGO_PROFILE := n obj-y += vdso.o targets += vdso.ldsarch/parisc/boot/compressed/Makefile +1
-0@@ -7,6 +7,7 @@ KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n targets := vmlinux.lds vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2arch/powerpc/kernel/Makefile +5
-1@@ -153,17 +153,21 @@ endif obj-$(CONFIG_PPC_SECURE_BOOT) += secure_boot.o ima_arch.o secvar-ops.o obj-$(CONFIG_PPC_SECVAR_SYSFS) += secvar-sysfs.o -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive code GCOV_PROFILE_prom_init.o := n +PGO_PROFILE_prom_init.o := n KCOV_INSTRUMENT_prom_init.o := n UBSAN_SANITIZE_prom_init.o := n GCOV_PROFILE_kprobes.o := n +PGO_PROFILE_kprobes.o := n KCOV_INSTRUMENT_kprobes.o := n UBSAN_SANITIZE_kprobes.o := n GCOV_PROFILE_kprobes-ftrace.o := n +PGO_PROFILE_kprobes-ftrace.o := n KCOV_INSTRUMENT_kprobes-ftrace.o := n UBSAN_SANITIZE_kprobes-ftrace.o := n GCOV_PROFILE_syscall_64.o := n +PGO_PROFILE_syscall_64.o := n KCOV_INSTRUMENT_syscall_64.o := n UBSAN_SANITIZE_syscall_64.o := n UBSAN_SANITIZE_vdso.o := narch/powerpc/kernel/trace/Makefile +2
-1@@ -23,7 +23,8 @@ obj-$(CONFIG_TRACING) += trace_clock.o obj-$(CONFIG_PPC64) += $(obj64-y) obj-$(CONFIG_PPC32) += $(obj32-y) -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive code GCOV_PROFILE_ftrace.o := n +PGO_PROFILE_ftrace.o := n KCOV_INSTRUMENT_ftrace.o := n UBSAN_SANITIZE_ftrace.o := narch/powerpc/kernel/vdso32/Makefile +1
-0@@ -34,6 +34,7 @@ targets := $(obj-vdso32) vdso32.so.dbg obj-vdso32 := $(addprefix $(obj)/, $(obj-vdso32)) GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/powerpc/kernel/vdso64/Makefile +1
-0@@ -21,6 +21,7 @@ targets := $(obj-vdso64) vdso64.so.dbg obj-vdso64 := $(addprefix $(obj)/, $(obj-vdso64)) GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/powerpc/kexec/Makefile +2
-1@@ -16,7 +16,8 @@ endif endif -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive code GCOV_PROFILE_core_$(BITS).o := n +PGO_PROFILE_core_$(BITS).o := n KCOV_INSTRUMENT_core_$(BITS).o := n UBSAN_SANITIZE_core_$(BITS).o := narch/powerpc/xmon/Makefile +1
-0@@ -2,6 +2,7 @@ # Makefile for xmon GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/riscv/kernel/vdso/Makefile +2
-1@@ -32,8 +32,9 @@ CPPFLAGS_vdso.lds += -P -C -U$(ARCH) # Disable -pg to prevent insert call site CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n # Force dependencyarch/s390/boot/Makefile +1
-0@@ -5,6 +5,7 @@ KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/s390/boot/compressed/Makefile +1
-0@@ -7,6 +7,7 @@ KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/s390/kernel/Makefile +1
-0@@ -14,6 +14,7 @@ CFLAGS_REMOVE_early.o = $(CC_FLAGS_FTRACE) endif GCOV_PROFILE_early.o := n +PGO_PROFILE_early.o := n KCOV_INSTRUMENT_early.o := n UBSAN_SANITIZE_early.o := n KASAN_SANITIZE_ipl.o := narch/s390/kernel/vdso64/Makefile +2
-1@@ -35,8 +35,9 @@ obj-y += vdso64_wrapper.o targets += vdso64.lds CPPFLAGS_vdso64.lds += -P -C -U$(ARCH) -# Disable gcov profiling, ubsan and kasan for VDSO code +# Disable gcov and PGO profiling, ubsan and kasan for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/s390/purgatory/Makefile +1
-0@@ -17,6 +17,7 @@ $(obj)/mem.o: $(srctree)/arch/s390/lib/mem.S FORCE KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/sh/boot/compressed/Makefile +1
-0@@ -13,6 +13,7 @@ targets := vmlinux vmlinux.bin vmlinux.bin.gz \ OBJECTS = $(obj)/head_32.o $(obj)/misc.o $(obj)/cache.o GCOV_PROFILE := n +PGO_PROFILE := n # # IMAGE_OFFSET is the load offset of the compression loaderarch/sh/mm/Makefile +1
-0@@ -43,3 +43,4 @@ obj-$(CONFIG_UNCACHED_MAPPING) += uncached.o obj-$(CONFIG_HAVE_SRAM_POOL) += sram.o GCOV_PROFILE_pmb.o := n +PGO_PROFILE_pmb.o := narch/sparc/vdso/Makefile +1
-0@@ -115,6 +115,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 -Bsymbolic GCOV_PROFILE := n +PGO_PROFILE := n # # Install the unstripped copies of vdso*.so. If our toolchain supportsarch/x86/Kconfig +1
-0@@ -95,6 +95,7 @@ config X86 select ARCH_SUPPORTS_DEBUG_PAGEALLOC select ARCH_SUPPORTS_NUMA_BALANCING if X86_64 select ARCH_SUPPORTS_KMAP_LOCAL_FORCE_MAP if NR_CPUS <= 4096 + select ARCH_SUPPORTS_PGO_CLANG if X86_64 select ARCH_USE_BUILTIN_BSWAP select ARCH_USE_QUEUED_RWLOCKS select ARCH_USE_QUEUED_SPINLOCKSarch/x86/boot/Makefile +1
-0@@ -71,6 +71,7 @@ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=) KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n $(obj)/bzImage: asflags-y := $(SVGA_MODE)arch/x86/boot/compressed/Makefile +1
-0@@ -54,6 +54,7 @@ CFLAGS_sev-es.o += -I$(objtree)/arch/x86/lib/ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE :=n KBUILD_LDFLAGS := -m elf_$(UTS_MACHINE)arch/x86/entry/vdso/Makefile +1
-0@@ -179,6 +179,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 \ $(call ld-option, --eh-frame-hdr) -Bsymbolic GCOV_PROFILE := n +PGO_PROFILE := n quiet_cmd_vdso_and_check = VDSO $@ cmd_vdso_and_check = $(cmd_vdso); $(cmd_vdso_check)arch/x86/kernel/vmlinux.lds.S +2
-0@@ -184,6 +184,8 @@ SECTIONS BUG_TABLE + PGO_CLANG_DATA + ORC_UNWIND_TABLE . = ALIGN(PAGE_SIZE);arch/x86/platform/efi/Makefile +1
-0@@ -2,6 +2,7 @@ OBJECT_FILES_NON_STANDARD_efi_thunk_$(BITS).o := y KASAN_SANITIZE := n GCOV_PROFILE := n +PGO_PROFILE := n obj-$(CONFIG_EFI) += quirks.o efi.o efi_$(BITS).o efi_stub_$(BITS).o obj-$(CONFIG_EFI_MIXED) += efi_thunk_$(BITS).oarch/x86/purgatory/Makefile +1
-0@@ -23,6 +23,7 @@ targets += purgatory.ro purgatory.chk # Sanitizer, etc. runtimes are unavailable and cannot be linked here. GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n UBSAN_SANITIZE := n KCSAN_SANITIZE := narch/x86/realmode/rm/Makefile +1
-0@@ -76,4 +76,5 @@ KBUILD_CFLAGS := $(REALMODE_CFLAGS) -D_SETUP -D_WAKEUP \ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := narch/x86/um/vdso/Makefile +1
-0@@ -64,6 +64,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -fPIC -shared -Wl,--hash-style=sysv GCOV_PROFILE := n +PGO_PROFILE := n # # Install the unstripped copy of vdso*.so listed in $(vdso-install-y).drivers/firmware/efi/libstub/Makefile +1
-0@@ -40,6 +40,7 @@ KBUILD_CFLAGS := $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS)) GCOV_PROFILE := n +PGO_PROFILE := n # Sanitizer runtimes are unavailable and cannot be linked here. KASAN_SANITIZE := n KCSAN_SANITIZE := ndrivers/s390/char/Makefile +1
-0@@ -9,6 +9,7 @@ CFLAGS_REMOVE_sclp_early_core.o = $(CC_FLAGS_FTRACE) endif GCOV_PROFILE_sclp_early_core.o := n +PGO_PROFILE_sclp_early_core.o := n KCOV_INSTRUMENT_sclp_early_core.o := n UBSAN_SANITIZE_sclp_early_core.o := n KASAN_SANITIZE_sclp_early_core.o := ninclude/asm-generic/vmlinux.lds.h +44
-0@@ -316,6 +316,49 @@#define THERMAL_TABLE (name)#endif +#ifdef CONFIG_PGO_CLANG +#define PGO_CLANG_DATA \ + __llvm_prf_data : AT(ADDR(__llvm_prf_data) - LOAD_OFFSET) { \ + . = ALIGN(8); \ + __llvm_prf_start = .; \ + __llvm_prf_data_start = .; \ + KEEP(*(__llvm_prf_data)) \ + . = ALIGN(8); \ + __llvm_prf_data_end = .; \ + } \ + __llvm_prf_cnts : AT(ADDR(__llvm_prf_cnts) - LOAD_OFFSET) { \ + . = ALIGN(8); \ + __llvm_prf_cnts_start = .; \ + KEEP(*(__llvm_prf_cnts)) \ + . = ALIGN(8); \ + __llvm_prf_cnts_end = .; \ + } \ + __llvm_prf_names : AT(ADDR(__llvm_prf_names) - LOAD_OFFSET) { \ + . = ALIGN(8); \ + __llvm_prf_names_start = .; \ + KEEP(*(__llvm_prf_names)) \ + . = ALIGN(8); \ + __llvm_prf_names_end = .; \ + . = ALIGN(8); \ + } \ + __llvm_prf_vals : AT(ADDR(__llvm_prf_vals) - LOAD_OFFSET) { \ + __llvm_prf_vals_start = .; \ + KEEP(*(__llvm_prf_vals)) \ + . = ALIGN(8); \ + __llvm_prf_vals_end = .; \ + . = ALIGN(8); \ + } \ + __llvm_prf_vnds : AT(ADDR(__llvm_prf_vnds) - LOAD_OFFSET) { \ + __llvm_prf_vnds_start = .; \ + KEEP(*(__llvm_prf_vnds)) \ + . = ALIGN(8); \ + __llvm_prf_vnds_end = .; \ + __llvm_prf_end = .; \ + } +#else +#define PGO_CLANG_DATA +#endif +#define KERNEL_DTB () \ STRUCT_ALIGN(); \ __dtb_start = .; \@@ -1125,6 +1168,7 @@ CONSTRUCTORS \ } \BUG_TABLE \ + PGO_CLANG_DATA#define INIT_TEXT_SECTION (inittext_align) \ . = ALIGN(inittext_align); \kernel/Makefile +1
-0@@ -111,6 +111,7 @@ obj-$(CONFIG_BPF) += bpf/ obj-$(CONFIG_KCSAN) += kcsan/ obj-$(CONFIG_SHADOW_CALL_STACK) += scs.o obj-$(CONFIG_HAVE_STATIC_CALL_INLINE) += static_call.o +obj-$(CONFIG_PGO_CLANG) += pgo/ obj-$(CONFIG_PERF_EVENTS) += events/kernel/pgo/Kconfig new file +34
-0@@ -0,0 +1,34 @@ +# SPDX-License-Identifier: GPL-2.0-only +menu "Profile Guided Optimization (PGO) (EXPERIMENTAL)" + +config ARCH_SUPPORTS_PGO_CLANG + bool + +config PGO_CLANG + bool "Enable clang's PGO-based kernel profiling" + depends on DEBUG_FS + depends on ARCH_SUPPORTS_PGO_CLANG + help + This option enables clang's PGO (Profile Guided Optimization) based + code profiling to better optimize the kernel. + + If unsure, say N. + + Run a representative workload for your application on a kernel + compiled with this option and download the raw profile file from + /sys/kernel/debug/pgo/profraw. This file needs to be processed with + llvm-profdata. It may be merged with other collected raw profiles. + + Copy the resulting profile file into vmlinux.profdata, and enable + KCFLAGS=-fprofile-use=vmlinux.profdata to produce an optimized + kernel. + + Note that a kernel compiled with profiling flags will be + significatnly larger and run slower. Also be sure to exclude files + from profiling which are not linked to the kernel image to prevent + linker errors. + + Note that the debugfs filesystem has to be mounted to access + profiling data. + +endmenukernel/pgo/Makefile new file +5
-0@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +GCOV_PROFILE := n +PGO_PROFILE := n + +obj-y += fs.o instrument.okernel/pgo/fs.c new file +382
-0@@ -0,0 +1,382 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2019 Google, Inc. +* +* Author: +* Sami Tolvanen <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#define pr_fmt (fmt) "pgo: " fmt + +#include <linux/kernel.h> +#include <linux/debugfs.h> +#include <linux/fs.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/vmalloc.h> +#include "pgo.h" + +static struct dentry *directory; + +struct prf_private_data { +void *buffer; +unsigned long size; +}; + +/* +* Raw profile data format: +* +* - llvm_prf_header +* - __llvm_prf_data +* - __llvm_prf_cnts +* - __llvm_prf_names +* - zero padding to 8 bytes +* - for each llvm_prf_data in __llvm_prf_data: +* - llvm_prf_value_data +* - llvm_prf_value_record + site count array +* - llvm_prf_value_node_data +* ... +* ... +* ... +*/ + +static void prf_fill_header (void **buffer) +{ +struct llvm_prf_header *header = *(struct llvm_prf_header **)buffer; + + header->magic =LLVM_PRF_MAGIC ; + header->version =LLVM_PRF_VARIANT_MASK_IR |LLVM_PRF_VERSION ; + header->data_size =prf_data_count (); + header->padding_bytes_before_counters =0 ; + header->counters_size =prf_cnts_count (); + header->padding_bytes_after_counters =0 ; + header->names_size =prf_names_count (); + header->counters_delta = (u64 )__llvm_prf_cnts_start; + header->names_delta = (u64 )__llvm_prf_names_start; + header->value_kind_last =LLVM_PRF_IPVK_LAST ; + + *buffer +=sizeof (*header); +} + +/* +* Copy the source into the buffer, incrementing the pointer into buffer in the +* process. +*/ +static void prf_copy_to_buffer (void **buffer,void *src,unsigned long size) +{ +memcpy (*buffer, src, size); + *buffer += size; +} + +static u32 __prf_get_value_size (struct llvm_prf_data *p,u32 *value_kinds) +{ +struct llvm_prf_value_node **nodes = + (struct llvm_prf_value_node **)p->values; +u32 kinds =0 ; +u32 size =0 ; +unsigned int kind; +unsigned int n; +unsigned int s =0 ; + +for (kind =0 ; kind <ARRAY_SIZE (p->num_value_sites); kind++) { +unsigned int sites = p->num_value_sites[kind]; + +if (!sites) +continue ; + +/* Record + site count array */ + size +=prf_get_value_record_size (sites); + kinds++; + +if (!nodes) +continue ; + +for (n =0 ; n < sites; n++) { +u32 count =0 ; +struct llvm_prf_value_node *site = nodes[s + n]; + +while (site && ++count <=U8_MAX ) + site = site->next; + + size += count * +sizeof (struct llvm_prf_value_node_data ); + } + + s += sites; + } + +if (size) + size +=sizeof (struct llvm_prf_value_data ); + +if (value_kinds) + *value_kinds = kinds; + +return size; +} + +static u32 prf_get_value_size (void ) +{ +u32 size =0 ; +struct llvm_prf_data *p; + +for (p = __llvm_prf_data_start; p < __llvm_prf_data_end; p++) + size +=__prf_get_value_size (p,NULL ); + +return size; +} + +/* Serialize the profiling's value. */ +static void prf_serialize_value (struct llvm_prf_data *p,void **buffer) +{ +struct llvm_prf_value_data header; +struct llvm_prf_value_node **nodes = + (struct llvm_prf_value_node **)p->values; +unsigned int kind; +unsigned int n; +unsigned int s =0 ; + + header.total_size =__prf_get_value_size (p, &header.num_value_kinds); + +if (!header.num_value_kinds) +/* Nothing to write. */ +return ; + +prf_copy_to_buffer (buffer, &header,sizeof (header)); + +for (kind =0 ; kind <ARRAY_SIZE (p->num_value_sites); kind++) { +struct llvm_prf_value_record *record; +u8 *counts; +unsigned int sites = p->num_value_sites[kind]; + +if (!sites) +continue ; + +/* Profiling value record. */ + record = *(struct llvm_prf_value_record **)buffer; + *buffer +=prf_get_value_record_header_size (); + + record->kind = kind; + record->num_value_sites = sites; + +/* Site count array. */ + counts = *(u8 **)buffer; + *buffer +=prf_get_value_record_site_count_size (sites); + +/* +* If we don't have nodes, we can skip updating the site count +* array, because the buffer is zero filled. +*/ +if (!nodes) +continue ; + +for (n =0 ; n < sites; n++) { +u32 count =0 ; +struct llvm_prf_value_node *site = nodes[s + n]; + +while (site && ++count <=U8_MAX ) { +prf_copy_to_buffer (buffer, site, +sizeof (struct llvm_prf_value_node_data )); + site = site->next; + } + + counts[n] = (u8 )count; + } + + s += sites; + } +} + +static void prf_serialize_values (void **buffer) +{ +struct llvm_prf_data *p; + +for (p = __llvm_prf_data_start; p < __llvm_prf_data_end; p++) +prf_serialize_value (p, buffer); +} + +static inline unsigned long prf_get_padding (unsigned long size) +{ +return 8 - (size %8 ); +} + +static unsigned long prf_buffer_size (void ) +{ +return sizeof (struct llvm_prf_header ) + +prf_data_size () + +prf_cnts_size () + +prf_names_size () + +prf_get_padding (prf_names_size ()) + +prf_get_value_size (); +} + +/* Serialize the profling data into a format LLVM's tools can understand. */ +static int prf_serialize (struct prf_private_data *p) +{ +int err =0 ; +void *buffer; + + p->size =prf_buffer_size (); + p->buffer =vzalloc (p->size); + +if (!p->buffer) { + err = -ENOMEM ; + goto out; + } + + buffer = p->buffer; + +prf_fill_header (&buffer); +prf_copy_to_buffer (&buffer, __llvm_prf_data_start,prf_data_size ()); +prf_copy_to_buffer (&buffer, __llvm_prf_cnts_start,prf_cnts_size ()); +prf_copy_to_buffer (&buffer, __llvm_prf_names_start,prf_names_size ()); + buffer +=prf_get_padding (prf_names_size ()); + +prf_serialize_values (&buffer); + +out: +return err; +} + +/* open() implementation for PGO. Creates a copy of the profiling data set. */ +static int prf_open (struct inode *inode,struct file *file) +{ +struct prf_private_data *data; +unsigned long flags; +int err; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) { + err = -ENOMEM ; + goto out; + } + + flags =prf_lock (); + + err =prf_serialize (data); +if (err) { +kfree (data); + goto out_unlock; + } + + file->private_data = data; + +out_unlock: +prf_unlock (flags); +out: +return err; +} + +/* read() implementation for PGO. */ +static ssize_t prf_read (struct file *file,char __user *buf,size_t count, +loff_t *ppos) +{ +struct prf_private_data *data = file->private_data; + +BUG_ON (!data); + +return simple_read_from_buffer (buf, count, ppos, data->buffer, + data->size); +} + +/* release() implementation for PGO. Release resources allocated by open(). */ +static int prf_release (struct inode *inode,struct file *file) +{ +struct prf_private_data *data = file->private_data; + +if (data) { +vfree (data->buffer); +kfree (data); + } + +return 0 ; +} + +static const struct file_operations prf_fops = { + .owner =THIS_MODULE , + .open = prf_open, + .read = prf_read, + .llseek = default_llseek, + .release = prf_release +}; + +/* write() implementation for resetting PGO's profile data. */ +static ssize_t reset_write (struct file *file,const char __user *addr, +size_t len,loff_t *pos) +{ +struct llvm_prf_data *data; + +memset (__llvm_prf_cnts_start,0 ,prf_cnts_size ()); + +for (data = __llvm_prf_data_start; data < __llvm_prf_data_end; ++data) { +struct llvm_prf_value_node **vnodes; +u64 current_vsite_count; +u32 i; + +if (!data->values) +continue ; + + current_vsite_count =0 ; + vnodes = (struct llvm_prf_value_node **)data->values; + +for (i =LLVM_PRF_IPVK_FIRST ; i <=LLVM_PRF_IPVK_LAST ; ++i) + current_vsite_count += data->num_value_sites[i]; + +for (i =0 ; i < current_vsite_count; ++i) { +struct llvm_prf_value_node *current_vnode = vnodes[i]; + +while (current_vnode) { + current_vnode->count =0 ; + current_vnode = current_vnode->next; + } + } + } + +return len; +} + +static const struct file_operations prf_reset_fops = { + .owner =THIS_MODULE , + .write = reset_write, + .llseek = noop_llseek, +}; + +/* Create debugfs entries. */ +static int __init pgo_init (void ) +{ + directory =debugfs_create_dir ("pgo" ,NULL ); +if (!directory) + goto err_remove; + +if (!debugfs_create_file ("profraw" ,0600 , directory,NULL , + &prf_fops)) + goto err_remove; + +if (!debugfs_create_file ("reset" ,0200 , directory,NULL , + &prf_reset_fops)) + goto err_remove; + +return 0 ; + +err_remove: +pr_err ("initialization failed\n" ); +return -EIO ; +} + +/* Remove debufs entries. */ +static void __exit pgo_exit(void) +{ +debugfs_remove_recursive (directory); +} + +module_init (pgo_init); +module_exit (pgo_exit);kernel/pgo/instrument.c new file +147
-0@@ -0,0 +1,147 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2019 Google, Inc. +* +* Author: +* Sami Tolvanen <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#define pr_fmt (fmt) "pgo: " fmt + +#include <linux/kernel.h> +#include <linux/export.h> +#include <linux/spinlock.h> +#include <linux/types.h> +#include "pgo.h" + +/* Lock guarding value node access and serialization. */ +static DEFINE_SPINLOCK (pgo_lock); +static int current_node; + +unsigned long prf_lock (void ) +{ +unsigned long flags; + +spin_lock_irqsave (&pgo_lock, flags); + +return flags; +} + +void prf_unlock (unsigned long flags) +{ +spin_unlock_irqrestore (&pgo_lock, flags); +} + +/* +* Return a newly allocated profiling value node which contains the tracked +* value by the value profiler. +* Note: caller *must* hold pgo_lock. +*/ +static struct llvm_prf_value_node *allocate_node (struct llvm_prf_data *p, +u32 index,u64 value) +{ +if (&__llvm_prf_vnds_start[current_node +1 ] >= __llvm_prf_vnds_end) +return NULL ;/* Out of nodes */ + + current_node++; + +/* Make sure the node is entirely within the section */ +if (&__llvm_prf_vnds_start[current_node] >= __llvm_prf_vnds_end || + &__llvm_prf_vnds_start[current_node +1 ] > __llvm_prf_vnds_end) +return NULL ; + +return &__llvm_prf_vnds_start[current_node]; +} + +/* +* Counts the number of times a target value is seen. +* +* Records the target value for the CounterIndex if not seen before. Otherwise, +* increments the counter associated w/ the target value. +*/ +void __llvm_profile_instrument_target (u64 target_value,void *data,u32 index) +{ +struct llvm_prf_data *p = (struct llvm_prf_data *)data; +struct llvm_prf_value_node **counters; +struct llvm_prf_value_node *curr; +struct llvm_prf_value_node *min =NULL ; +struct llvm_prf_value_node *prev =NULL ; +u64 min_count =U64_MAX ; +u8 values =0 ; +unsigned long flags; + +if (!p || !p->values) +return ; + + counters = (struct llvm_prf_value_node **)p->values; + curr = counters[index]; + +while (curr) { +if (target_value == curr->value) { + curr->count++; +return ; + } + +if (curr->count < min_count) { + min_count = curr->count; + min = curr; + } + + prev = curr; + curr = curr->next; + values++; + } + +if (values >=LLVM_PRF_MAX_NUM_VALS_PER_SITE ) { +if (!min->count || !(--min->count)) { + curr = min; + curr->value = target_value; + curr->count++; + } +return ; + } + +/* Lock when updating the value node structure. */ + flags =prf_lock (); + + curr =allocate_node (p, index, target_value); +if (!curr) + goto out; + + curr->value = target_value; + curr->count++; + +if (!counters[index]) + counters[index] = curr; +else if (prev && !prev->next) + prev->next = curr; + +out: +prf_unlock (flags); +} +EXPORT_SYMBOL (__llvm_profile_instrument_target); + +/* Counts the number of times a range of targets values are seen. */ +void __llvm_profile_instrument_range (u64 target_value,void *data, +u32 index,s64 precise_start, +s64 precise_last,s64 large_value) +{ +if (large_value !=S64_MIN && (s64 )target_value >= large_value) + target_value = large_value; +else if ((s64 )target_value < precise_start || + (s64 )target_value > precise_last) + target_value = precise_last +1 ; + +__llvm_profile_instrument_target (target_value, data, index); +} +EXPORT_SYMBOL (__llvm_profile_instrument_range);kernel/pgo/pgo.h new file +206
-0@@ -0,0 +1,206 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2019 Google, Inc. +* +* Author: +* Sami Tolvanen <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef _PGO_H +#define _PGO_H + +/* +* Note: These internal LLVM definitions must match the compiler version. +* See llvm/include/llvm/ProfileData/InstrProfData.inc in LLVM's source code. +*/ + +#ifdef CONFIG_64BIT +#define LLVM_PRF_MAGIC \ + ((u64)255 << 56 | \ + (u64)'l' << 48 | \ + (u64)'p' << 40 | \ + (u64)'r' << 32 | \ + (u64)'o' << 24 | \ + (u64)'f' << 16 | \ + (u64)'r' << 8 | \ + (u64)129) +#else +#define LLVM_PRF_MAGIC \ + ((u64)255 << 56 | \ + (u64)'l' << 48 | \ + (u64)'p' << 40 | \ + (u64)'r' << 32 | \ + (u64)'o' << 24 | \ + (u64)'f' << 16 | \ + (u64)'R' << 8 | \ + (u64)129) +#endif + +#define LLVM_PRF_VERSION 5 +#define LLVM_PRF_DATA_ALIGN 8 +#define LLVM_PRF_IPVK_FIRST 0 +#define LLVM_PRF_IPVK_LAST 1 +#define LLVM_PRF_MAX_NUM_VALS_PER_SITE 16 + +#define LLVM_PRF_VARIANT_MASK_IR (0x1ull << 56) +#define LLVM_PRF_VARIANT_MASK_CSIR (0x1ull << 57) + +/** +* struct llvm_prf_header - represents the raw profile header data structure. +* @magic: the magic token for the file format. +* @version: the version of the file format. +* @data_size: the number of entries in the profile data section. +* @padding_bytes_before_counters: the number of padding bytes before the +* counters. +* @counters_size: the size in bytes of the LLVM profile section containing the +* counters. +* @padding_bytes_after_counters: the number of padding bytes after the +* counters. +* @names_size: the size in bytes of the LLVM profile section containing the +* counters' names. +* @counters_delta: the beginning of the LLMV profile counters section. +* @names_delta: the beginning of the LLMV profile names section. +* @value_kind_last: the last profile value kind. +*/ +struct llvm_prf_header { +u64 magic; +u64 version; +u64 data_size; +u64 padding_bytes_before_counters; +u64 counters_size; +u64 padding_bytes_after_counters; +u64 names_size; +u64 counters_delta; +u64 names_delta; +u64 value_kind_last; +}; + +/** +* struct llvm_prf_data - represents the per-function control structure. +* @name_ref: the reference to the function's name. +* @func_hash: the hash value of the function. +* @counter_ptr: a pointer to the profile counter. +* @function_ptr: a pointer to the function. +* @values: the profiling values associated with this function. +* @num_counters: the number of counters in the function. +* @num_value_sites: the number of value profile sites. +*/ +struct llvm_prf_data { +const u64 name_ref; +const u64 func_hash; +const void *counter_ptr; +const void *function_ptr; +void *values; +const u32 num_counters; +const u16 num_value_sites[LLVM_PRF_IPVK_LAST +1 ]; +}__aligned(LLVM_PRF_DATA_ALIGN) ; + +/** +* structure llvm_prf_value_node_data - represents the data part of the struct +* llvm_prf_value_node data structure. +* @value: the value counters. +* @count: the counters' count. +*/ +struct llvm_prf_value_node_data { +u64 value; +u64 count; +}; + +/** +* struct llvm_prf_value_node - represents an internal data structure used by +* the value profiler. +* @value: the value counters. +* @count: the counters' count. +* @next: the next value node. +*/ +struct llvm_prf_value_node { +u64 value; +u64 count; +struct llvm_prf_value_node *next; +}; + +/** +* struct llvm_prf_value_data - represents the value profiling data in indexed +* format. +* @total_size: the total size in bytes including this field. +* @num_value_kinds: the number of value profile kinds that has value profile +* data. +*/ +struct llvm_prf_value_data { +u32 total_size; +u32 num_value_kinds; +}; + +/** +* struct llvm_prf_value_record - represents the on-disk layout of the value +* profile data of a particular kind for one function. +* @kind: the kind of the value profile record. +* @num_value_sites: the number of value profile sites. +* @site_count_array: the first element of the array that stores the number +* of profiled values for each value site. +*/ +struct llvm_prf_value_record { +u32 kind; +u32 num_value_sites; +u8 site_count_array[]; +}; + +#define prf_get_value_record_header_size () \ + offsetof(struct llvm_prf_value_record, site_count_array) +#define prf_get_value_record_site_count_size (sites) \ + roundup((sites), 8) +#define prf_get_value_record_size (sites) \ + (prf_get_value_record_header_size() + \ + prf_get_value_record_site_count_size((sites))) + +/* Data sections */ +extern struct llvm_prf_data __llvm_prf_data_start[]; +extern struct llvm_prf_data __llvm_prf_data_end[]; + +extern u64 __llvm_prf_cnts_start[]; +extern u64 __llvm_prf_cnts_end[]; + +extern char __llvm_prf_names_start[]; +extern char __llvm_prf_names_end[]; + +extern struct llvm_prf_value_node __llvm_prf_vnds_start[]; +extern struct llvm_prf_value_node __llvm_prf_vnds_end[]; + +/* Locking for vnodes */ +extern unsigned long prf_lock (void ); +extern void prf_unlock (unsigned long flags); + +#define __DEFINE_PRF_SIZE (s) \ + static inline unsigned long prf_ ## s ## _size(void) \ + { \ + unsigned long start = \ + (unsigned long)__llvm_prf_ ## s ## _start; \ + unsigned long end = \ + (unsigned long)__llvm_prf_ ## s ## _end; \ + return roundup(end - start, \ + sizeof(__llvm_prf_ ## s ## _start[0])); \ + } \ + static inline unsigned long prf_ ## s ## _count(void) \ + { \ + return prf_ ## s ## _size() / \ + sizeof(__llvm_prf_ ## s ## _start[0]); \ + } + +__DEFINE_PRF_SIZE (data); +__DEFINE_PRF_SIZE (cnts); +__DEFINE_PRF_SIZE (names); +__DEFINE_PRF_SIZE (vnds); + +#undef __DEFINE_PRF_SIZE + +#endif /* _PGO_H */ scripts/Makefile.lib +10
-1@@ -143,6 +143,16 @@ _c_flags += $(if $(patsubst n%,, \ $(CFLAGS_GCOV)) endif +# +# Enable clang's PGO profiling flags for a file or directory depending on +# variables PGO_PROFILE_obj.o and PGO_PROFILE. +# +ifeq ($(CONFIG_PGO_CLANG),y) +_c_flags += $(if $(patsubst n%,, \ + $(PGO_PROFILE_$(basetarget).o)$(PGO_PROFILE)y), \ + $(CFLAGS_PGO_CLANG)) +endif + # # Enable address sanitizer flags for kernel except some files or directories # we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE) --2.30.0.284.gd98b1dd5eaa7-goog
--
You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/20210111081821.3041587-1-morbo%40google.com.
Quoted 39 lines · Sedat Dilek
Quoted 24 lines · Bill Wendling' via Clang Built Linux
From: Sami Tolvanen <[email protected]>
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.profrawMultiple raw profiles may be merged during this step.
The data can be used either by the compiler if LTO isn't enabled:
... -fprofile-use=vmlinux.profdata ...or by LLD if LTO is enabled:
... -lto-cs-profile-file=vmlinux.profdata ...Quoted 9 lines
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 and isn't compatible with clang's gcov support in kernel/gcov.
[1] https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization
Hi Bill and Sami,
Quoted 7 lines
I have seen the pull-request in the CBL issue tracker and had some questions in mind.
Good you send this.
First of all, I like to fetch any development stuff easily from a Git repository.
The version in the pull-request in the CBL issue tracker is roughly the same as this patch. (There are some changes, but they aren't functionality changes.)
Quoted 4 lines
Can you offer this, please?
What is the base for your work?
I hope this is (fresh released) Linux v5.11-rc3.
This patch (and the PR on the CBL issue tracker) are from top-of-tree Linux.
Quoted 7 lines
I myself had some experiences with a PGO + ThinLTO optimized LLVM toolchain built with the help of tc-build.
Here it takes very long to build it.This means I have some profile-data archived.
Can I use it?
LLVM is more tolerant of "stale" profile data than gcov, so it's possible that your archived profile data would still work, but I can't guarantee that it will be better than using new profile data.
Quoted 4 lines
Is an own PGO + ThinLTO optimized LLVM toolchain pre-requirement for this or not?
That is one of my important questions.
Do you mean that the LLVM tools (clang, llc, etc.) are compiled with PGO + ThinLTO?
-bw
Quoted 70 lines · Bill Wendling
Quoted 39 lines · Sedat Dilek
Quoted 24 lines · Bill Wendling' via Clang Built Linux
From: Sami Tolvanen <[email protected]>
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.profrawMultiple raw profiles may be merged during this step.
The data can be used either by the compiler if LTO isn't enabled:
... -fprofile-use=vmlinux.profdata ...or by LLD if LTO is enabled:
... -lto-cs-profile-file=vmlinux.profdata ...Quoted 9 lines
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 and isn't compatible with clang's gcov support in kernel/gcov.
[1] https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization
Hi Bill and Sami,
Quoted 7 lines
I have seen the pull-request in the CBL issue tracker and had some questions in mind.
Good you send this.
First of all, I like to fetch any development stuff easily from a Git repository.
The version in the pull-request in the CBL issue tracker is roughly the same as this patch. (There are some changes, but they aren't functionality changes.)
Quoted 4 lines
Can you offer this, please?
What is the base for your work?
I hope this is (fresh released) Linux v5.11-rc3.This patch (and the PR on the CBL issue tracker) are from top-of-tree Linux.
Quoted 7 lines
I myself had some experiences with a PGO + ThinLTO optimized LLVM toolchain built with the help of tc-build.
Here it takes very long to build it.This means I have some profile-data archived.
Can I use it?LLVM is more tolerant of "stale" profile data than gcov, so it's possible that your archived profile data would still work, but I can't guarantee that it will be better than using new profile data.
Quoted 7 lines
Quoted 4 lines
Is an own PGO + ThinLTO optimized LLVM toolchain pre-requirement for this or not?
That is one of my important questions.Do you mean that the LLVM tools (clang, llc, etc.) are compiled with PGO + ThinLTO?
Yes.
- Sedat -
Quoted 75 lines · Sedat Dilek
Quoted 74 lines · Bill Wendling
Quoted 39 lines · Sedat Dilek
Quoted 24 lines · Bill Wendling' via Clang Built Linux
From: Sami Tolvanen <[email protected]>
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.profrawMultiple raw profiles may be merged during this step.
The data can be used either by the compiler if LTO isn't enabled:
... -fprofile-use=vmlinux.profdata ...or by LLD if LTO is enabled:
... -lto-cs-profile-file=vmlinux.profdata ...Quoted 9 lines
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 and isn't compatible with clang's gcov support in kernel/gcov.
[1] https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization
Hi Bill and Sami,
Quoted 7 lines
I have seen the pull-request in the CBL issue tracker and had some questions in mind.
Good you send this.
First of all, I like to fetch any development stuff easily from a Git repository.
The version in the pull-request in the CBL issue tracker is roughly the same as this patch. (There are some changes, but they aren't functionality changes.)
Quoted 4 lines
Can you offer this, please?
What is the base for your work?
I hope this is (fresh released) Linux v5.11-rc3.This patch (and the PR on the CBL issue tracker) are from top-of-tree Linux.
Quoted 7 lines
I myself had some experiences with a PGO + ThinLTO optimized LLVM toolchain built with the help of tc-build.
Here it takes very long to build it.This means I have some profile-data archived.
Can I use it?LLVM is more tolerant of "stale" profile data than gcov, so it's possible that your archived profile data would still work, but I can't guarantee that it will be better than using new profile data.
Quoted 4 lines
Is an own PGO + ThinLTO optimized LLVM toolchain pre-requirement for this or not?
That is one of my important questions.
Quoted 7 lines
Quoted 3 lines
Do you mean that the LLVM tools (clang, llc, etc.) are compiled with PGO + ThinLTO?
Yes.
- Sedat -
No, having an optimized LLVM toolchain is not a requirement of this patchset. It will make compiling the kernel faster but it does nothing more than that.
Cheers,
Nathan
Quoted 15 lines · Bill Wendling' via Clang Built Linux
From: Sami Tolvanen <[email protected]>
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.profrawMultiple raw profiles may be merged during this step.
Quoted 7 lines
The data can be used either by the compiler if LTO isn't enabled:
... -fprofile-use=vmlinux.profdata ...or by LLD if LTO is enabled:
... -lto-cs-profile-file=vmlinux.profdata ...
This LLD option does not exist.
LLD does have some --lto-* options but the -lto-* form is not supported (it clashes with -l) https://reviews.llvm.org/D79371
(There is an earlier -fprofile-instr-generate which does instrumentation in Clang, but the option does not have broad usage. It is used more for code coverage, not for optimization. Noticeably, it does not even implement the Kirchhoff's current law optimization)
-fprofile-use= is used by both regular PGO and context-sensitive PGO (CSPGO).
clang -flto=thin -fprofile-use= passes -plugin-opt=cs-profile-path= to the linker. For regular PGO, this option is effectively a no-op (confirmed with CSPGO main developer).
So I think the "or by LLD if LTO is enabled:" part should be removed.
Quoted 99 lines
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 and isn't compatible with clang's gcov support in kernel/gcov.
[1] https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization
Signed-off-by Sami Tolvanen <[email protected]>Co-developed-by Bill Wendling <[email protected]>Signed-off-by Bill Wendling <[email protected]>---
Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/pgo.rst | 127 +++++++++ MAINTAINERS | 9 + Makefile | 3 + arch/Kconfig | 1 + arch/arm/boot/bootp/Makefile | 1 + arch/arm/boot/compressed/Makefile | 1 + arch/arm/vdso/Makefile | 3 +- arch/arm64/kernel/vdso/Makefile | 3 +- arch/arm64/kvm/hyp/nvhe/Makefile | 1 + arch/mips/boot/compressed/Makefile | 1 + arch/mips/vdso/Makefile | 1 + arch/nds32/kernel/vdso/Makefile | 4 +- arch/parisc/boot/compressed/Makefile | 1 + arch/powerpc/kernel/Makefile | 6 +- arch/powerpc/kernel/trace/Makefile | 3 +- arch/powerpc/kernel/vdso32/Makefile | 1 + arch/powerpc/kernel/vdso64/Makefile | 1 + arch/powerpc/kexec/Makefile | 3 +- arch/powerpc/xmon/Makefile | 1 + arch/riscv/kernel/vdso/Makefile | 3 +- arch/s390/boot/Makefile | 1 + arch/s390/boot/compressed/Makefile | 1 + arch/s390/kernel/Makefile | 1 + arch/s390/kernel/vdso64/Makefile | 3 +- arch/s390/purgatory/Makefile | 1 + arch/sh/boot/compressed/Makefile | 1 + arch/sh/mm/Makefile | 1 + arch/sparc/vdso/Makefile | 1 + arch/x86/Kconfig | 1 + arch/x86/boot/Makefile | 1 + arch/x86/boot/compressed/Makefile | 1 + arch/x86/entry/vdso/Makefile | 1 + arch/x86/kernel/vmlinux.lds.S | 2 + arch/x86/platform/efi/Makefile | 1 + arch/x86/purgatory/Makefile | 1 + arch/x86/realmode/rm/Makefile | 1 + arch/x86/um/vdso/Makefile | 1 + drivers/firmware/efi/libstub/Makefile | 1 + drivers/s390/char/Makefile | 1 + include/asm-generic/vmlinux.lds.h | 44 +++ kernel/Makefile | 1 + kernel/pgo/Kconfig | 34 +++ kernel/pgo/Makefile | 5 + kernel/pgo/fs.c | 382 ++++++++++++++++++++++++++ kernel/pgo/instrument.c | 147 ++++++++++ kernel/pgo/pgo.h | 206 ++++++++++++++ scripts/Makefile.lib | 10 + 48 files changed, 1017 insertions(+), 9 deletions(-)create mode 100644
Documentation/dev-tools/pgo.rstcreate mode 100644kernel/pgo/Kconfigcreate mode 100644kernel/pgo/Makefilecreate mode 100644kernel/pgo/fs.c
create mode 100644kernel/pgo/instrument.ccreate mode 100644kernel/pgo/pgo.hDocumentation/dev-tools/index.rst +1
-0@@ -26,6 +26,7 @@ whole; patches welcome! kgdb kselftest kunit/index + pgo.. only:: subproject and html
Documentation/dev-tools/pgo.rst new file +12
-0@@ -0,0 +1,127 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=============================== +Using PGO with the Linux kernel +=============================== + +Clang's profiling kernel support (PGO_) enables profiling of the Linux kernel +when building with Clang. The profiling data is exported via the ``pgo`` +debugfs directory. + +.. _PGO: https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization +
Documentation/dev-tools/pgo.rst line 22
Quoted 10 lines
Documentation/dev-tools/pgo.rst new file +10
-0+ +Preparation +=========== + +Configure the kernel with: + +.. code-block:: make + + CONFIG_DEBUG_FS=y + CONFIG_PGO_CLANG=y
Clang also supports SamplePGO (-fprofile-sample-use; AutoFDO) and context-sensitive PGO (post-inline, -fcs-profile-generate). Context-sensitive SamplePGO is under development.
If the naming does not make adding future other PGO features difficult, I am happy with CONFIG_PGO_CLANG :)
Aside from the question and the issue above, the description looks good to me.
Quoted 1,507 lines
Documentation/dev-tools/pgo.rst new file +104
-0+Note that kernels compiled with profiling flags will be significantly larger +and run slower. + +Profiling data will only become accessible once debugfs has been mounted: + +.. code-block:: sh + + mount -t debugfs none /sys/kernel/debug + + +Customization +============= + +You can enable or disable profiling for individual file and directories by +adding a line similar to the following to the respective kernel Makefile: + +- For a single file (e.g. main.o) + + .. code-block:: make + + PGO_PROFILE_main.o := y + +- For all files in one directory + + .. code-block:: make + + PGO_PROFILE := y + +To exclude files from being profiled use + + .. code-block:: make + + PGO_PROFILE_main.o := n + +and + + .. code-block:: make + + PGO_PROFILE := n + +Only files which are linked to the main kernel image or are compiled as kernel +modules are supported by this mechanism. + + +Files +===== + +The PGO kernel support creates the following files in debugfs: + +``/sys/kernel/debug/pgo`` + Parent directory for all PGO-related files. + +``/sys/kernel/debug/pgo/reset`` + Global reset file: resets all coverage data to zero when written to. + +``/sys/kernel/debug/profraw`` + The raw PGO data that must be processed with ``llvm_profdata``. + + +Workflow +======== + +The PGO kernel can be run on the host or test machines. The data though should +be analyzed with Clang's tools from the same Clang version as the kernel was +compiled. Clang's tolerant of version skew, but it's easier to use the same +Clang version. + +The profiling data is useful for optimizing the kernel, analyzing coverage, +etc. Clang offers tools to perform these tasks. + +Here is an example workflow for profiling an instrumented kernel with PGO and +using the result to optimize the kernel: + +1) Install the kernel on the TEST machine. + +2) Reset the data counters right before running the load tests + + .. code-block:: sh + + echo 1 > /sys/kernel/debug/pgo/reset + +3) Run the load tests. + +4) Collect the raw profile data + + .. code-block:: sh + + cp -a /sys/kernel/debug/pgo/profraw /tmp/vmlinux.profraw + +5) (Optional) Download the raw profile data to the HOST machine. + +6) Process the raw profile data + + .. code-block:: sh + + llvm-profdata merge --output=vmlinux.profdata vmlinux.profraw + + Note that multiple raw profile data files can be merged during this step. + +7) Rebuild the kernel using the profile data (PGO disabled) + + .. code-block:: sh + + make ... KCLAGS=-fprofile-use=vmlinux.profdataMAINTAINERS +0
-0@@ -13955,6 +13955,15 @@ S: MaintainedF:
include/linux/personality.h
F:include/uapi/linux/personality.hMAINTAINERS +9
-0+PGO BASED KERNEL PROFILING +M: Sami Tolvanen <[email protected]> +M: Bill Wendling <[email protected]> +R: Nathan Chancellor <[email protected]> +R: Nick Desaulniers <[email protected]> +S: Supported +F: Documentation/dev-tools/pgo.rst +F: kernel/pgo +PHOENIX RC FLIGHT CONTROLLER ADAPTER
M: Marcus Folkesson <[email protected]>
L: [email protected]Makefile +0
-0@@ -659,6 +659,9 @@ endif # KBUILD_EXTMOD # Defaults to vmlinux, but the arch makefile usually adds further targetsall: vmlinux
Makefile +3
-0+CFLAGS_PGO_CLANG := -fprofile-generate +export CFLAGS_PGO_CLANG +CFLAGS_GCOV := -fprofile-arcs -ftest-coverage \ $(call cc-option,-fno-tree-loop-im) \ $(call cc-disable-warning,maybe-uninitialized,)arch/Kconfig +0
-0@@ -1106,6 +1106,7 @@ config ARCH_SUPPORTS_DEBUG_PAGEALLOC boolsource "
kernel/gcov/Kconfig"
+source "kernel/pgo/Kconfig"source "
scripts/gcc-plugins/Kconfig"arch/arm/boot/bootp/Makefile +0
-0@@ -7,6 +7,7 @@ #
GCOV_PROFILE:= n
+PGO_PROFILE := nLDFLAGS_bootp := --no-undefined -X \ --defsym initrd_phys=$(INITRD_PHYS) \arch/arm/boot/compressed/Makefile +0
-0@@ -24,6 +24,7 @@ OBJS += hyp-stub.oendif
GCOV_PROFILE:= n
+PGO_PROFILE := nKASAN_SANITIZE:= n# Prevents link failures:
__sanitizer_cov_trace_pc()is not linked in.arch/arm/vdso/Makefile +0
-0@@ -36,8 +36,9 @@ else CFLAGS_vgettimeofday.o = -O2 -include $(c-gettimeofday-y)endif
arch/arm/vdso/Makefile +1
-1-# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code
GCOV_PROFILE:= n
+PGO_PROFILE := n# Prevents link failures:
__sanitizer_cov_trace_pc()is not linked in.KCOV_INSTRUMENT:= narch/arm64/kernel/vdso/Makefile +0
-0@@ -41,8 +41,9 @@ ifneq ($(c-gettimeofday-y),) CFLAGS_vgettimeofday.o += -include $(c-gettimeofday-y)endif
arch/arm64/kernel/vdso/Makefile +1
-1-# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code
GCOV_PROFILE:= n
+PGO_PROFILE := nobj-y += vdso.o
targets +=vdso.ldsarch/arm64/kvm/hyp/nvhe/Makefile +0
-0@@ -60,6 +60,7 @@ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS), $(KBUILD_CFLAG # compiler instrumentation that inserts callbacks or checks into the code may # cause crashes. Just disable it.
GCOV_PROFILE:= n
+PGO_PROFILE := nKASAN_SANITIZE:= nUBSAN_SANITIZE:= nKCOV_INSTRUMENT:= narch/mips/boot/compressed/Makefile +0
-0@@ -37,6 +37,7 @@ KBUILD_AFLAGS := $(KBUILD_AFLAGS) -D__ASSEMBLY__ \ # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.
KCOV_INSTRUMENT:= nGCOV_PROFILE:= n
+PGO_PROFILE := n# decompressor objects (linked with vmlinuz) vmlinuzobjs-y := $(obj)/head.o $(obj)/decompress.o $(obj)/string.oarch/mips/vdso/Makefile +0
-0@@ -66,6 +66,7 @@ ldflags-y := -Bsymbolic --no-undefined -soname=linux-vdso.so.1 \CFLAGS_REMOVE_vdso.o = -pg
GCOV_PROFILE:= n
+PGO_PROFILE := nUBSAN_SANITIZE:= nKCOV_INSTRUMENT:= narch/nds32/kernel/vdso/Makefile +1
-1@@ -15,9 +15,9 @@ obj-vdso := $(addprefix $(obj)/, $(obj-vdso)) ccflags-y := -shared -fno-common -fno-builtin -nostdlib -fPIC -Wl,-shared -g \ -Wl,-soname=linux-vdso.so.1 -Wl,--hash-style=sysv -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code
GCOV_PROFILE:= narch/nds32/kernel/vdso/Makefile +1
-1- +PGO_PROFILE := nobj-y += vdso.o
targets +=vdso.ldsarch/parisc/boot/compressed/Makefile +0
-0@@ -7,6 +7,7 @@
KCOV_INSTRUMENT:= nGCOV_PROFILE:= n
+PGO_PROFILE := nUBSAN_SANITIZE:= ntargets :=
vmlinux.ldsvmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2arch/powerpc/kernel/Makefile +0
-0@@ -153,17 +153,21 @@ endifobj-$(CONFIG_PPC_SECURE_BOOT) += secure_boot.o ima_arch.o secvar-ops.o
obj-$(CONFIG_PPC_SECVAR_SYSFS) += secvar-sysfs.oarch/powerpc/kernel/Makefile +1
-1-# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive codeGCOV_PROFILE_prom_init.o := n
+PGO_PROFILE_prom_init.o := n
KCOV_INSTRUMENT_prom_init.o := n
UBSAN_SANITIZE_prom_init.o := n
GCOV_PROFILE_kprobes.o := n
+PGO_PROFILE_kprobes.o := n
KCOV_INSTRUMENT_kprobes.o := n
UBSAN_SANITIZE_kprobes.o := n
GCOV_PROFILE_kprobes-ftrace.o := n
+PGO_PROFILE_kprobes-ftrace.o := n
KCOV_INSTRUMENT_kprobes-ftrace.o := n
UBSAN_SANITIZE_kprobes-ftrace.o := n
GCOV_PROFILE_syscall_64.o := n
+PGO_PROFILE_syscall_64.o := n
KCOV_INSTRUMENT_syscall_64.o := n
UBSAN_SANITIZE_syscall_64.o := n
UBSAN_SANITIZE_vdso.o := narch/powerpc/kernel/trace/Makefile +1
-1@@ -23,7 +23,8 @@ obj-$(CONFIG_TRACING) += trace_clock.o obj-$(CONFIG_PPC64) += $(obj64-y) obj-$(CONFIG_PPC32) += $(obj32-y) -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive codeGCOV_PROFILE_ftrace.o := n
+PGO_PROFILE_ftrace.o := n
KCOV_INSTRUMENT_ftrace.o := n
UBSAN_SANITIZE_ftrace.o := narch/powerpc/kernel/vdso32/Makefile +0
-0@@ -34,6 +34,7 @@ targets := $(obj-vdso32) vdso32.so.dbg obj-vdso32 := $(addprefix $(obj)/, $(obj-vdso32))
GCOV_PROFILE:= n
+PGO_PROFILE := nKCOV_INSTRUMENT:= nUBSAN_SANITIZE:= nKASAN_SANITIZE:= narch/powerpc/kernel/vdso64/Makefile +0
-0@@ -21,6 +21,7 @@ targets := $(obj-vdso64) vdso64.so.dbg obj-vdso64 := $(addprefix $(obj)/, $(obj-vdso64))
GCOV_PROFILE:= n
+PGO_PROFILE := nKCOV_INSTRUMENT:= nUBSAN_SANITIZE:= nKASAN_SANITIZE:= narch/powerpc/kexec/Makefile +0
-0@@ -16,7 +16,8 @@ endifendif
arch/powerpc/kexec/Makefile +1
-1-# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive codeGCOV_PROFILE_core_$(BITS).o := n
+PGO_PROFILE_core_$(BITS).o := n
KCOV_INSTRUMENT_core_$(BITS).o := n
UBSAN_SANITIZE_core_$(BITS).o := narch/powerpc/xmon/Makefile +0
-0@@ -2,6 +2,7 @@ # Makefile for xmon
GCOV_PROFILE:= n
+PGO_PROFILE := nKCOV_INSTRUMENT:= nUBSAN_SANITIZE:= nKASAN_SANITIZE:= narch/riscv/kernel/vdso/Makefile +0
-0@@ -32,8 +32,9 @@ CPPFLAGS_vdso.lds += -P -C -U$(ARCH) # Disable -pg to prevent insert call siteCFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os
arch/riscv/kernel/vdso/Makefile +1
-1-# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code
GCOV_PROFILE:= n
+PGO_PROFILE := nKCOV_INSTRUMENT:= n# Force dependency
arch/s390/boot/Makefile +0
-0@@ -5,6 +5,7 @@
KCOV_INSTRUMENT:= nGCOV_PROFILE:= n
+PGO_PROFILE := nUBSAN_SANITIZE:= nKASAN_SANITIZE:= narch/s390/boot/compressed/Makefile +0
-0@@ -7,6 +7,7 @@
KCOV_INSTRUMENT:= nGCOV_PROFILE:= n
+PGO_PROFILE := nUBSAN_SANITIZE:= nKASAN_SANITIZE:= narch/s390/kernel/Makefile +0
-0@@ -14,6 +14,7 @@ CFLAGS_REMOVE_early.o = $(CC_FLAGS_FTRACE)endif
GCOV_PROFILE_early.o := n
+PGO_PROFILE_early.o := n
KCOV_INSTRUMENT_early.o := n
UBSAN_SANITIZE_early.o := n
KASAN_SANITIZE_ipl.o := narch/s390/kernel/vdso64/Makefile +0
-0@@ -35,8 +35,9 @@ obj-y += vdso64_wrapper.otargets +=
vdso64.ldsCPPFLAGS_vdso64.lds+= -P -C -U$(ARCH)arch/s390/kernel/vdso64/Makefile +1
-1-# Disable gcov profiling, ubsan and kasan for VDSO code +# Disable gcov and PGO profiling, ubsan and kasan for VDSO code
GCOV_PROFILE:= n
+PGO_PROFILE := nUBSAN_SANITIZE:= nKASAN_SANITIZE:= narch/s390/purgatory/Makefile +0
-0@@ -17,6 +17,7 @@ $(obj)/mem.o: $(srctree)/arch/s390/lib/mem.S FORCE
KCOV_INSTRUMENT:= nGCOV_PROFILE:= n
+PGO_PROFILE := nUBSAN_SANITIZE:= nKASAN_SANITIZE:= narch/sh/boot/compressed/Makefile +0
-0@@ -13,6 +13,7 @@ targets := vmlinux vmlinux.bin vmlinux.bin.gz \OBJECTS = $(obj)/head_32.o $(obj)/misc.o $(obj)/cache.o
GCOV_PROFILE:= n
+PGO_PROFILE := n#
#IMAGE_OFFSETis the load offset of the compression loaderarch/sh/mm/Makefile +0
-0@@ -43,3 +43,4 @@ obj-$(CONFIG_UNCACHED_MAPPING) += uncached.oobj-$(CONFIG_HAVE_SRAM_POOL) += sram.o
GCOV_PROFILE_pmb.o := n
arch/sh/mm/Makefile +1
-0+PGO_PROFILE_pmb.o := narch/sparc/vdso/Makefile +0
-0@@ -115,6 +115,7 @@ quiet_cmd_vdso = VDSO $@
VDSO_LDFLAGS= -shared --hash-style=both --build-id=sha1 -BsymbolicGCOV_PROFILE:= n
+PGO_PROFILE := n#
# Install the unstripped copies of vdso*.so. If our toolchain supportsarch/x86/Kconfig +1
-0@@ -95,6 +95,7 @@ config X86 select ARCH_SUPPORTS_DEBUG_PAGEALLOC select ARCH_SUPPORTS_NUMA_BALANCING if X86_64 select ARCH_SUPPORTS_KMAP_LOCAL_FORCE_MAP if NR_CPUS <= 4096 + select ARCH_SUPPORTS_PGO_CLANG if X86_64 select ARCH_USE_BUILTIN_BSWAP select ARCH_USE_QUEUED_RWLOCKS select ARCH_USE_QUEUED_SPINLOCKSarch/x86/boot/Makefile +0
-0@@ -71,6 +71,7 @@ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=)
KBUILD_CFLAGS+= -fno-asynchronous-unwind-tablesGCOV_PROFILE:= n
+PGO_PROFILE := nUBSAN_SANITIZE:= n$(obj)/bzImage: asflags-y := $(SVGA_MODE)
arch/x86/boot/compressed/Makefile +0
-0@@ -54,6 +54,7 @@ CFLAGS_sev-es.o += -I$(objtree)/arch/x86/lib/
KBUILD_AFLAGS:= $(KBUILD_CFLAGS) -D__ASSEMBLY__GCOV_PROFILE:= n
+PGO_PROFILE := nUBSAN_SANITIZE:=n
KBUILD_LDFLAGS:= -m elf_$(UTS_MACHINE)arch/x86/entry/vdso/Makefile +0
-0@@ -179,6 +179,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 \ $(call ld-option, --eh-frame-hdr) -Bsymbolic
GCOV_PROFILE:= n
+PGO_PROFILE := n
quiet_cmd_vdso_and_check= VDSO $@cmd_vdso_and_check = $(cmd_vdso); $(cmd_vdso_check)arch/x86/kernel/vmlinux.lds.S +2
-0@@ -184,6 +184,8 @@ SECTIONS BUG_TABLE + PGO_CLANG_DATA + ORC_UNWIND_TABLE . = ALIGN(PAGE_SIZE);arch/x86/platform/efi/Makefile +0
-0@@ -2,6 +2,7 @@OBJECT_FILES_NON_STANDARD_efi_thunk_$(BITS).o := y
KASAN_SANITIZE:= nGCOV_PROFILE:= n
+PGO_PROFILE := nobj-$(CONFIG_EFI) += quirks.o efi.o efi_$(BITS).o efi_stub_$(BITS).o
obj-$(CONFIG_EFI_MIXED) += efi_thunk_$(BITS).oarch/x86/purgatory/Makefile +0
-0@@ -23,6 +23,7 @@ targets += purgatory.ro purgatory.chk # Sanitizer, etc. runtimes are unavailable and cannot be linked here.
GCOV_PROFILE:= n
+PGO_PROFILE := nKASAN_SANITIZE:= nUBSAN_SANITIZE:= nKCSAN_SANITIZE:= narch/x86/realmode/rm/Makefile +0
-0@@ -76,4 +76,5 @@ KBUILD_CFLAGS := $(REALMODE_CFLAGS) -D_SETUP -D_WAKEUP \
KBUILD_AFLAGS:= $(KBUILD_CFLAGS) -D__ASSEMBLY__KBUILD_CFLAGS+= -fno-asynchronous-unwind-tablesGCOV_PROFILE:= n
+PGO_PROFILE := nUBSAN_SANITIZE:= narch/x86/um/vdso/Makefile +0
-0@@ -64,6 +64,7 @@ quiet_cmd_vdso = VDSO $@
VDSO_LDFLAGS= -fPIC -shared -Wl,--hash-style=sysvGCOV_PROFILE:= n
+PGO_PROFILE := n#
# Install the unstripped copy of vdso*.so listed in $(vdso-install-y).drivers/firmware/efi/libstub/Makefile +0
-0@@ -40,6 +40,7 @@ KBUILD_CFLAGS := $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS))
GCOV_PROFILE:= n
+PGO_PROFILE := n
# Sanitizer runtimes are unavailable and cannot be linked here.KASAN_SANITIZE:= nKCSAN_SANITIZE:= ndrivers/s390/char/Makefile +0
-0@@ -9,6 +9,7 @@ CFLAGS_REMOVE_sclp_early_core.o = $(CC_FLAGS_FTRACE)endif
GCOV_PROFILE_sclp_early_core.o := n
+PGO_PROFILE_sclp_early_core.o := n
KCOV_INSTRUMENT_sclp_early_core.o := n
UBSAN_SANITIZE_sclp_early_core.o := n
KASAN_SANITIZE_sclp_early_core.o := ninclude/asm-generic/vmlinux.lds.h +44
-0@@ -316,6 +316,49 @@#define THERMAL_TABLE (name)#endif +#ifdef CONFIG_PGO_CLANG +#define PGO_CLANG_DATA \ + __llvm_prf_data : AT(ADDR(__llvm_prf_data) - LOAD_OFFSET) { \ + . = ALIGN(8); \ + __llvm_prf_start = .; \ + __llvm_prf_data_start = .; \ + KEEP(*(__llvm_prf_data)) \ + . = ALIGN(8); \ + __llvm_prf_data_end = .; \ + } \ + __llvm_prf_cnts : AT(ADDR(__llvm_prf_cnts) - LOAD_OFFSET) { \ + . = ALIGN(8); \ + __llvm_prf_cnts_start = .; \ + KEEP(*(__llvm_prf_cnts)) \ + . = ALIGN(8); \ + __llvm_prf_cnts_end = .; \ + } \ + __llvm_prf_names : AT(ADDR(__llvm_prf_names) - LOAD_OFFSET) { \ + . = ALIGN(8); \ + __llvm_prf_names_start = .; \ + KEEP(*(__llvm_prf_names)) \ + . = ALIGN(8); \ + __llvm_prf_names_end = .; \ + . = ALIGN(8); \ + } \ + __llvm_prf_vals : AT(ADDR(__llvm_prf_vals) - LOAD_OFFSET) { \ + __llvm_prf_vals_start = .; \ + KEEP(*(__llvm_prf_vals)) \ + . = ALIGN(8); \ + __llvm_prf_vals_end = .; \ + . = ALIGN(8); \ + } \ + __llvm_prf_vnds : AT(ADDR(__llvm_prf_vnds) - LOAD_OFFSET) { \ + __llvm_prf_vnds_start = .; \ + KEEP(*(__llvm_prf_vnds)) \ + . = ALIGN(8); \ + __llvm_prf_vnds_end = .; \ + __llvm_prf_end = .; \ + } +#else +#define PGO_CLANG_DATA +#endif +#define KERNEL_DTB () \ STRUCT_ALIGN(); \ __dtb_start = .; \@@ -1125,6 +1168,7 @@ CONSTRUCTORS \ } \BUG_TABLE \ + PGO_CLANG_DATA#define INIT_TEXT_SECTION (inittext_align) \ . = ALIGN(inittext_align); \kernel/Makefile +0
-0@@ -111,6 +111,7 @@ obj-$(CONFIG_BPF) += bpf/obj-$(CONFIG_KCSAN) += kcsan/ obj-$(CONFIG_SHADOW_CALL_STACK) += scs.o obj-$(CONFIG_HAVE_STATIC_CALL_INLINE) += static_call.o +obj-$(CONFIG_PGO_CLANG) += pgo/
obj-$(CONFIG_PERF_EVENTS) += events/
kernel/pgo/Kconfig new file +34
-0@@ -0,0 +1,34 @@ +# SPDX-License-Identifier: GPL-2.0-only +menu "Profile Guided Optimization (PGO) (EXPERIMENTAL)" + +config ARCH_SUPPORTS_PGO_CLANG + bool + +config PGO_CLANG + bool "Enable clang's PGO-based kernel profiling" + depends on DEBUG_FS + depends on ARCH_SUPPORTS_PGO_CLANG + help + This option enables clang's PGO (Profile Guided Optimization) based + code profiling to better optimize the kernel. + + If unsure, say N. + + Run a representative workload for your application on a kernel + compiled with this option and download the raw profile file from + /sys/kernel/debug/pgo/profraw. This file needs to be processed with + llvm-profdata. It may be merged with other collected raw profiles. + + Copy the resulting profile file into vmlinux.profdata, and enable + KCFLAGS=-fprofile-use=vmlinux.profdata to produce an optimized + kernel. + + Note that a kernel compiled with profiling flags will be + significatnly larger and run slower. Also be sure to exclude files + from profiling which are not linked to the kernel image to prevent + linker errors. + + Note that the debugfs filesystem has to be mounted to access + profiling data. + +endmenukernel/pgo/Makefile new file +5
-0@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +GCOV_PROFILE := n +PGO_PROFILE := n + +obj-y += fs.o instrument.okernel/pgo/fs.c new file +382
-0@@ -0,0 +1,382 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2019 Google, Inc. +* +* Author: +* Sami Tolvanen <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#define pr_fmt (fmt) "pgo: " fmt + +#include <linux/kernel.h> +#include <linux/debugfs.h> +#include <linux/fs.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/vmalloc.h> +#include "pgo.h" + +static struct dentry *directory; + +struct prf_private_data { +void *buffer; +unsigned long size; +}; + +/* +* Raw profile data format: +* +* - llvm_prf_header +* - __llvm_prf_data +* - __llvm_prf_cnts +* - __llvm_prf_names +* - zero padding to 8 bytes +* - for each llvm_prf_data in __llvm_prf_data: +* - llvm_prf_value_data +* - llvm_prf_value_record + site count array +* - llvm_prf_value_node_data +* ... +* ... +* ... +*/ + +static void prf_fill_header (void **buffer) +{ +struct llvm_prf_header *header = *(struct llvm_prf_header **)buffer; + + header->magic =LLVM_PRF_MAGIC ; + header->version =LLVM_PRF_VARIANT_MASK_IR |LLVM_PRF_VERSION ; + header->data_size =prf_data_count (); + header->padding_bytes_before_counters =0 ; + header->counters_size =prf_cnts_count (); + header->padding_bytes_after_counters =0 ; + header->names_size =prf_names_count (); + header->counters_delta = (u64 )__llvm_prf_cnts_start; + header->names_delta = (u64 )__llvm_prf_names_start; + header->value_kind_last =LLVM_PRF_IPVK_LAST ; + + *buffer +=sizeof (*header); +} + +/* +* Copy the source into the buffer, incrementing the pointer into buffer in the +* process. +*/ +static void prf_copy_to_buffer (void **buffer,void *src,unsigned long size) +{ +memcpy (*buffer, src, size); + *buffer += size; +} + +static u32 __prf_get_value_size (struct llvm_prf_data *p,u32 *value_kinds) +{ +struct llvm_prf_value_node **nodes = + (struct llvm_prf_value_node **)p->values; +u32 kinds =0 ; +u32 size =0 ; +unsigned int kind; +unsigned int n; +unsigned int s =0 ; + +for (kind =0 ; kind <ARRAY_SIZE (p->num_value_sites); kind++) { +unsigned int sites = p->num_value_sites[kind]; + +if (!sites) +continue ; + +/* Record + site count array */ + size +=prf_get_value_record_size (sites); + kinds++; + +if (!nodes) +continue ; + +for (n =0 ; n < sites; n++) { +u32 count =0 ; +struct llvm_prf_value_node *site = nodes[s + n]; + +while (site && ++count <=U8_MAX ) + site = site->next; + + size += count * +sizeof (struct llvm_prf_value_node_data ); + } + + s += sites; + } + +if (size) + size +=sizeof (struct llvm_prf_value_data ); + +if (value_kinds) + *value_kinds = kinds; + +return size; +} + +static u32 prf_get_value_size (void ) +{ +u32 size =0 ; +struct llvm_prf_data *p; + +for (p = __llvm_prf_data_start; p < __llvm_prf_data_end; p++) + size +=__prf_get_value_size (p,NULL ); + +return size; +} + +/* Serialize the profiling's value. */ +static void prf_serialize_value (struct llvm_prf_data *p,void **buffer) +{ +struct llvm_prf_value_data header; +struct llvm_prf_value_node **nodes = + (struct llvm_prf_value_node **)p->values; +unsigned int kind; +unsigned int n; +unsigned int s =0 ; + + header.total_size =__prf_get_value_size (p, &header.num_value_kinds); + +if (!header.num_value_kinds) +/* Nothing to write. */ +return ; + +prf_copy_to_buffer (buffer, &header,sizeof (header)); + +for (kind =0 ; kind <ARRAY_SIZE (p->num_value_sites); kind++) { +struct llvm_prf_value_record *record; +u8 *counts; +unsigned int sites = p->num_value_sites[kind]; + +if (!sites) +continue ; + +/* Profiling value record. */ + record = *(struct llvm_prf_value_record **)buffer; + *buffer +=prf_get_value_record_header_size (); + + record->kind = kind; + record->num_value_sites = sites; + +/* Site count array. */ + counts = *(u8 **)buffer; + *buffer +=prf_get_value_record_site_count_size (sites); + +/* +* If we don't have nodes, we can skip updating the site count +* array, because the buffer is zero filled. +*/ +if (!nodes) +continue ; + +for (n =0 ; n < sites; n++) { +u32 count =0 ; +struct llvm_prf_value_node *site = nodes[s + n]; + +while (site && ++count <=U8_MAX ) { +prf_copy_to_buffer (buffer, site, +sizeof (struct llvm_prf_value_node_data )); + site = site->next; + } + + counts[n] = (u8 )count; + } + + s += sites; + } +} + +static void prf_serialize_values (void **buffer) +{ +struct llvm_prf_data *p; + +for (p = __llvm_prf_data_start; p < __llvm_prf_data_end; p++) +prf_serialize_value (p, buffer); +} + +static inline unsigned long prf_get_padding (unsigned long size) +{ +return 8 - (size %8 ); +} + +static unsigned long prf_buffer_size (void ) +{ +return sizeof (struct llvm_prf_header ) + +prf_data_size () + +prf_cnts_size () + +prf_names_size () + +prf_get_padding (prf_names_size ()) + +prf_get_value_size (); +} + +/* Serialize the profling data into a format LLVM's tools can understand. */ +static int prf_serialize (struct prf_private_data *p) +{ +int err =0 ; +void *buffer; + + p->size =prf_buffer_size (); + p->buffer =vzalloc (p->size); + +if (!p->buffer) { + err = -ENOMEM ; + goto out; + } + + buffer = p->buffer; + +prf_fill_header (&buffer); +prf_copy_to_buffer (&buffer, __llvm_prf_data_start,prf_data_size ()); +prf_copy_to_buffer (&buffer, __llvm_prf_cnts_start,prf_cnts_size ()); +prf_copy_to_buffer (&buffer, __llvm_prf_names_start,prf_names_size ()); + buffer +=prf_get_padding (prf_names_size ()); + +prf_serialize_values (&buffer); + +out: +return err; +} + +/* open() implementation for PGO. Creates a copy of the profiling data set. */ +static int prf_open (struct inode *inode,struct file *file) +{ +struct prf_private_data *data; +unsigned long flags; +int err; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) { + err = -ENOMEM ; + goto out; + } + + flags =prf_lock (); + + err =prf_serialize (data); +if (err) { +kfree (data); + goto out_unlock; + } + + file->private_data = data; + +out_unlock: +prf_unlock (flags); +out: +return err; +} + +/* read() implementation for PGO. */ +static ssize_t prf_read (struct file *file,char __user *buf,size_t count, +loff_t *ppos) +{ +struct prf_private_data *data = file->private_data; + +BUG_ON (!data); + +return simple_read_from_buffer (buf, count, ppos, data->buffer, + data->size); +} + +/* release() implementation for PGO. Release resources allocated by open(). */ +static int prf_release (struct inode *inode,struct file *file) +{ +struct prf_private_data *data = file->private_data; + +if (data) { +vfree (data->buffer); +kfree (data); + } + +return 0 ; +} + +static const struct file_operations prf_fops = { + .owner =THIS_MODULE , + .open = prf_open, + .read = prf_read, + .llseek = default_llseek, + .release = prf_release +}; + +/* write() implementation for resetting PGO's profile data. */ +static ssize_t reset_write (struct file *file,const char __user *addr, +size_t len,loff_t *pos) +{ +struct llvm_prf_data *data; + +memset (__llvm_prf_cnts_start,0 ,prf_cnts_size ()); + +for (data = __llvm_prf_data_start; data < __llvm_prf_data_end; ++data) { +struct llvm_prf_value_node **vnodes; +u64 current_vsite_count; +u32 i; + +if (!data->values) +continue ; + + current_vsite_count =0 ; + vnodes = (struct llvm_prf_value_node **)data->values; + +for (i =LLVM_PRF_IPVK_FIRST ; i <=LLVM_PRF_IPVK_LAST ; ++i) + current_vsite_count += data->num_value_sites[i]; + +for (i =0 ; i < current_vsite_count; ++i) { +struct llvm_prf_value_node *current_vnode = vnodes[i]; + +while (current_vnode) { + current_vnode->count =0 ; + current_vnode = current_vnode->next; + } + } + } + +return len; +} + +static const struct file_operations prf_reset_fops = { + .owner =THIS_MODULE , + .write = reset_write, + .llseek = noop_llseek, +}; + +/* Create debugfs entries. */ +static int __init pgo_init (void ) +{ + directory =debugfs_create_dir ("pgo" ,NULL ); +if (!directory) + goto err_remove; + +if (!debugfs_create_file ("profraw" ,0600 , directory,NULL , + &prf_fops)) + goto err_remove; + +if (!debugfs_create_file ("reset" ,0200 , directory,NULL , + &prf_reset_fops)) + goto err_remove; + +return 0 ; + +err_remove: +pr_err ("initialization failed\n" ); +return -EIO ; +} + +/* Remove debufs entries. */ +static void __exit pgo_exit(void) +{ +debugfs_remove_recursive (directory); +} + +module_init (pgo_init); +module_exit (pgo_exit);kernel/pgo/instrument.c new file +147
-0@@ -0,0 +1,147 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2019 Google, Inc. +* +* Author: +* Sami Tolvanen <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#define pr_fmt (fmt) "pgo: " fmt + +#include <linux/kernel.h> +#include <linux/export.h> +#include <linux/spinlock.h> +#include <linux/types.h> +#include "pgo.h" + +/* Lock guarding value node access and serialization. */ +static DEFINE_SPINLOCK (pgo_lock); +static int current_node; + +unsigned long prf_lock (void ) +{ +unsigned long flags; + +spin_lock_irqsave (&pgo_lock, flags); + +return flags; +} + +void prf_unlock (unsigned long flags) +{ +spin_unlock_irqrestore (&pgo_lock, flags); +} + +/* +* Return a newly allocated profiling value node which contains the tracked +* value by the value profiler. +* Note: caller *must* hold pgo_lock. +*/ +static struct llvm_prf_value_node *allocate_node (struct llvm_prf_data *p, +u32 index,u64 value) +{ +if (&__llvm_prf_vnds_start[current_node +1 ] >= __llvm_prf_vnds_end) +return NULL ;/* Out of nodes */ + + current_node++; + +/* Make sure the node is entirely within the section */ +if (&__llvm_prf_vnds_start[current_node] >= __llvm_prf_vnds_end || + &__llvm_prf_vnds_start[current_node +1 ] > __llvm_prf_vnds_end) +return NULL ; + +return &__llvm_prf_vnds_start[current_node]; +} + +/* +* Counts the number of times a target value is seen. +* +* Records the target value for the CounterIndex if not seen before. Otherwise, +* increments the counter associated w/ the target value. +*/ +void __llvm_profile_instrument_target (u64 target_value,void *data,u32 index) +{ +struct llvm_prf_data *p = (struct llvm_prf_data *)data; +struct llvm_prf_value_node **counters; +struct llvm_prf_value_node *curr; +struct llvm_prf_value_node *min =NULL ; +struct llvm_prf_value_node *prev =NULL ; +u64 min_count =U64_MAX ; +u8 values =0 ; +unsigned long flags; + +if (!p || !p->values) +return ; + + counters = (struct llvm_prf_value_node **)p->values; + curr = counters[index]; + +while (curr) { +if (target_value == curr->value) { + curr->count++; +return ; + } + +if (curr->count < min_count) { + min_count = curr->count; + min = curr; + } + + prev = curr; + curr = curr->next; + values++; + } + +if (values >=LLVM_PRF_MAX_NUM_VALS_PER_SITE ) { +if (!min->count || !(--min->count)) { + curr = min; + curr->value = target_value; + curr->count++; + } +return ; + } + +/* Lock when updating the value node structure. */ + flags =prf_lock (); + + curr =allocate_node (p, index, target_value); +if (!curr) + goto out; + + curr->value = target_value; + curr->count++; + +if (!counters[index]) + counters[index] = curr; +else if (prev && !prev->next) + prev->next = curr; + +out: +prf_unlock (flags); +} +EXPORT_SYMBOL (__llvm_profile_instrument_target); + +/* Counts the number of times a range of targets values are seen. */ +void __llvm_profile_instrument_range (u64 target_value,void *data, +u32 index,s64 precise_start, +s64 precise_last,s64 large_value) +{ +if (large_value !=S64_MIN && (s64 )target_value >= large_value) + target_value = large_value; +else if ((s64 )target_value < precise_start || + (s64 )target_value > precise_last) + target_value = precise_last +1 ; + +__llvm_profile_instrument_target (target_value, data, index); +} +EXPORT_SYMBOL (__llvm_profile_instrument_range);kernel/pgo/pgo.h new file +206
-0@@ -0,0 +1,206 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2019 Google, Inc. +* +* Author: +* Sami Tolvanen <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef _PGO_H +#define _PGO_H + +/* +* Note: These internal LLVM definitions must match the compiler version. +* See llvm/include/llvm/ProfileData/InstrProfData.inc in LLVM's source code. +*/ + +#ifdef CONFIG_64BIT +#define LLVM_PRF_MAGIC \ + ((u64)255 << 56 | \ + (u64)'l' << 48 | \ + (u64)'p' << 40 | \ + (u64)'r' << 32 | \ + (u64)'o' << 24 | \ + (u64)'f' << 16 | \ + (u64)'r' << 8 | \ + (u64)129) +#else +#define LLVM_PRF_MAGIC \ + ((u64)255 << 56 | \ + (u64)'l' << 48 | \ + (u64)'p' << 40 | \ + (u64)'r' << 32 | \ + (u64)'o' << 24 | \ + (u64)'f' << 16 | \ + (u64)'R' << 8 | \ + (u64)129) +#endif + +#define LLVM_PRF_VERSION 5 +#define LLVM_PRF_DATA_ALIGN 8 +#define LLVM_PRF_IPVK_FIRST 0 +#define LLVM_PRF_IPVK_LAST 1 +#define LLVM_PRF_MAX_NUM_VALS_PER_SITE 16 + +#define LLVM_PRF_VARIANT_MASK_IR (0x1ull << 56) +#define LLVM_PRF_VARIANT_MASK_CSIR (0x1ull << 57) + +/** +* struct llvm_prf_header - represents the raw profile header data structure. +* @magic: the magic token for the file format. +* @version: the version of the file format. +* @data_size: the number of entries in the profile data section. +* @padding_bytes_before_counters: the number of padding bytes before the +* counters. +* @counters_size: the size in bytes of the LLVM profile section containing the +* counters. +* @padding_bytes_after_counters: the number of padding bytes after the +* counters. +* @names_size: the size in bytes of the LLVM profile section containing the +* counters' names. +* @counters_delta: the beginning of the LLMV profile counters section. +* @names_delta: the beginning of the LLMV profile names section. +* @value_kind_last: the last profile value kind. +*/ +struct llvm_prf_header { +u64 magic; +u64 version; +u64 data_size; +u64 padding_bytes_before_counters; +u64 counters_size; +u64 padding_bytes_after_counters; +u64 names_size; +u64 counters_delta; +u64 names_delta; +u64 value_kind_last; +}; + +/** +* struct llvm_prf_data - represents the per-function control structure. +* @name_ref: the reference to the function's name. +* @func_hash: the hash value of the function. +* @counter_ptr: a pointer to the profile counter. +* @function_ptr: a pointer to the function. +* @values: the profiling values associated with this function. +* @num_counters: the number of counters in the function. +* @num_value_sites: the number of value profile sites. +*/ +struct llvm_prf_data { +const u64 name_ref; +const u64 func_hash; +const void *counter_ptr; +const void *function_ptr; +void *values; +const u32 num_counters; +const u16 num_value_sites[LLVM_PRF_IPVK_LAST +1 ]; +}__aligned(LLVM_PRF_DATA_ALIGN) ; + +/** +* structure llvm_prf_value_node_data - represents the data part of the struct +* llvm_prf_value_node data structure. +* @value: the value counters. +* @count: the counters' count. +*/ +struct llvm_prf_value_node_data { +u64 value; +u64 count; +}; + +/** +* struct llvm_prf_value_node - represents an internal data structure used by +* the value profiler. +* @value: the value counters. +* @count: the counters' count. +* @next: the next value node. +*/ +struct llvm_prf_value_node { +u64 value; +u64 count; +struct llvm_prf_value_node *next; +}; + +/** +* struct llvm_prf_value_data - represents the value profiling data in indexed +* format. +* @total_size: the total size in bytes including this field. +* @num_value_kinds: the number of value profile kinds that has value profile +* data. +*/ +struct llvm_prf_value_data { +u32 total_size; +u32 num_value_kinds; +}; + +/** +* struct llvm_prf_value_record - represents the on-disk layout of the value +* profile data of a particular kind for one function. +* @kind: the kind of the value profile record. +* @num_value_sites: the number of value profile sites. +* @site_count_array: the first element of the array that stores the number +* of profiled values for each value site. +*/ +struct llvm_prf_value_record { +u32 kind; +u32 num_value_sites; +u8 site_count_array[]; +}; + +#define prf_get_value_record_header_size () \ + offsetof(struct llvm_prf_value_record, site_count_array) +#define prf_get_value_record_site_count_size (sites) \ + roundup((sites), 8) +#define prf_get_value_record_size (sites) \ + (prf_get_value_record_header_size() + \ + prf_get_value_record_site_count_size((sites))) + +/* Data sections */ +extern struct llvm_prf_data __llvm_prf_data_start[]; +extern struct llvm_prf_data __llvm_prf_data_end[]; + +extern u64 __llvm_prf_cnts_start[]; +extern u64 __llvm_prf_cnts_end[]; + +extern char __llvm_prf_names_start[]; +extern char __llvm_prf_names_end[]; + +extern struct llvm_prf_value_node __llvm_prf_vnds_start[]; +extern struct llvm_prf_value_node __llvm_prf_vnds_end[]; + +/* Locking for vnodes */ +extern unsigned long prf_lock (void ); +extern void prf_unlock (unsigned long flags); + +#define __DEFINE_PRF_SIZE (s) \ + static inline unsigned long prf_ ## s ## _size(void) \ + { \ + unsigned long start = \ + (unsigned long)__llvm_prf_ ## s ## _start; \ + unsigned long end = \ + (unsigned long)__llvm_prf_ ## s ## _end; \ + return roundup(end - start, \ + sizeof(__llvm_prf_ ## s ## _start[0])); \ + } \ + static inline unsigned long prf_ ## s ## _count(void) \ + { \ + return prf_ ## s ## _size() / \ + sizeof(__llvm_prf_ ## s ## _start[0]); \ + } + +__DEFINE_PRF_SIZE (data); +__DEFINE_PRF_SIZE (cnts); +__DEFINE_PRF_SIZE (names); +__DEFINE_PRF_SIZE (vnds); + +#undef __DEFINE_PRF_SIZE + +#endif /* _PGO_H */ scripts/Makefile.lib +0
-0@@ -143,6 +143,16 @@ _c_flags += $(if $(patsubst n%,, \ $(CFLAGS_GCOV))endif
scripts/Makefile.lib +10
-0+# +# Enable clang's PGO profiling flags for a file or directory depending on +# variables PGO_PROFILE_obj.o and PGO_PROFILE. +# +ifeq ($(CONFIG_PGO_CLANG),y) +_c_flags += $(if $(patsubst n%,, \ + $(PGO_PROFILE_$(basetarget).o)$(PGO_PROFILE)y), \ + $(CFLAGS_PGO_CLANG)) +endif +#
# Enable address sanitizer flags for kernel except some files or directories# we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE)Signature · 6 lines
2.30.0.284.gd98b1dd5eaa7-goog -- You received this message because you are subscribed to the Google Groups "Clang Built Linux" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/20210111081821.3041587-1-morbo%40google.com.
Quoted 21 lines · Fangrui Song
Quoted 19 lines · Bill Wendling' via Clang Built Linux
From: Sami Tolvanen <[email protected]>
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.profrawMultiple raw profiles may be merged during this step.
The data can be used either by the compiler if LTO isn't enabled:
... -fprofile-use=vmlinux.profdata ...
Quoted 8 lines
Quoted 3 lines
or by LLD if LTO is enabled:
... -lto-cs-profile-file=vmlinux.profdata ...This LLD option does not exist.
LLD does have some--lto-*options but the-lto-*form is not supported (it clashes with -l) https://reviews.llvm.org/D79371
That's strange. I've been using that option for years now. :-) Is this a recent change?
Quoted 6 lines
(There is an earlier -fprofile-instr-generate which does instrumentation in Clang, but the option does not have broad usage. It is used more for code coverage, not for optimization. Noticeably, it does not even implement the Kirchhoff's current law optimization)
Right. I've been told outside of this email that -fprofile-generate is the prefered flag to use.
Quoted 6 lines
-fprofile-use= is used by both regular PGO and context-sensitive PGO (CSPGO).
clang -flto=thin -fprofile-use= passes -plugin-opt=cs-profile-path= to the linker. For regular PGO, this option is effectively a no-op (confirmed with CSPGO main developer).
So I think the "or by LLD if LTO is enabled:" part should be removed.
But what if you specify the linking step explicitly? Linux doesn't call "clang" when linking, but "ld.lld".
-bw
Quoted 24 lines · Bill Wendling
Quoted 23 lines · Fangrui Song
Quoted 21 lines · Bill Wendling' via Clang Built Linux
From: Sami Tolvanen <[email protected]>
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.profrawMultiple raw profiles may be merged during this step.
The data can be used either by the compiler if LTO isn't enabled:
... -fprofile-use=vmlinux.profdata ...or by LLD if LTO is enabled:
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
... -lto-cs-profile-file=vmlinux.profdata ...This LLD option does not exist.
LLD does have some--lto-*options but the-lto-*form is not supported (it clashes with -l) https://reviews.llvm.org/D79371That's strange. I've been using that option for years now. :-) Is this a recent change?
The more frequently used options (specifyed by the clang driver) are -plugin-opt=... (options implemented by LLVMgold.so). -lto-* is rare.
Quoted 11 lines
Quoted 6 lines
(There is an earlier -fprofile-instr-generate which does instrumentation in Clang, but the option does not have broad usage. It is used more for code coverage, not for optimization. Noticeably, it does not even implement the Kirchhoff's current law optimization)
Right. I've been told outside of this email that -fprofile-generate is the prefered flag to use.
Quoted 2 lines
-fprofile-use= is used by both regular PGO and context-sensitive PGO (CSPGO).
Quoted 7 lines
Quoted 4 lines
clang -flto=thin -fprofile-use= passes -plugin-opt=cs-profile-path= to the linker. For regular PGO, this option is effectively a no-op (confirmed with CSPGO main developer).
So I think the "or by LLD if LTO is enabled:" part should be removed.
But what if you specify the linking step explicitly? Linux doesn't call "clang" when linking, but "ld.lld".
Regular PGO+LTO does not need -plugin-opt=cs-profile-path=
CSPGO+LTO needs it.
Because -fprofile-use= may be used by both, Clang driver adds it.
CSPGO is relevant in this this patch, so the linker option does not need to be mentioned.
Quoted 54 lines · Fangrui Song
Quoted 24 lines · Bill Wendling
Quoted 23 lines · Fangrui Song
Quoted 21 lines · Bill Wendling' via Clang Built Linux
From: Sami Tolvanen <[email protected]>
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.profrawMultiple raw profiles may be merged during this step.
The data can be used either by the compiler if LTO isn't enabled:
... -fprofile-use=vmlinux.profdata ...or by LLD if LTO is enabled:
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
... -lto-cs-profile-file=vmlinux.profdata ...This LLD option does not exist.
LLD does have some--lto-*options but the-lto-*form is not supported (it clashes with -l) https://reviews.llvm.org/D79371That's strange. I've been using that option for years now. :-) Is this a recent change?
The more frequently used options (specifyed by the clang driver) are -plugin-opt=... (options implemented by LLVMgold.so).
-lto-*is rare.Quoted 16 lines
Quoted 6 lines
(There is an earlier -fprofile-instr-generate which does instrumentation in Clang, but the option does not have broad usage. It is used more for code coverage, not for optimization. Noticeably, it does not even implement the Kirchhoff's current law optimization)
Right. I've been told outside of this email that -fprofile-generate is the prefered flag to use.
Quoted 6 lines
-fprofile-use= is used by both regular PGO and context-sensitive PGO (CSPGO).
clang -flto=thin -fprofile-use= passes -plugin-opt=cs-profile-path= to the linker. For regular PGO, this option is effectively a no-op (confirmed with CSPGO main developer).
So I think the "or by LLD if LTO is enabled:" part should be removed.
Quoted 7 lines
Quoted 2 lines
But what if you specify the linking step explicitly? Linux doesn't call "clang" when linking, but "ld.lld".
Regular PGO+LTO does not need -plugin-opt=cs-profile-path=
CSPGO+LTO needs it.
Because -fprofile-use= may be used by both, Clang driver adds it.
CSPGO is relevant in this this patch, so the linker option does not need to be mentioned.
I'm still a bit confused. Are you saying that when clang uses -flto=thin -fprofile-use=foo that the profile file "foo" is embedded into the bitcode file so that when the linker's run it'll be used?
This is the workflow:
clang ... -fprofile-use=vmlinux.profdata ... -c -o foo.o foo.c clang ... -fprofile-use=vmlinux.profdata ... -c -o bar.o bar.c ld.lld ... <output file> foo.o bar.o
Are you saying that we don't need to have "-plugin-opt=cs-profile-path=vmlinux.profdata" on the "ld.lld ..." line?
-bw
Quoted 74 lines · Bill Wendling
Quoted 54 lines · Fangrui Song
Quoted 24 lines · Bill Wendling
Quoted 23 lines · Fangrui Song
Quoted 21 lines · Bill Wendling' via Clang Built Linux
From: Sami Tolvanen <[email protected]>
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.profrawMultiple raw profiles may be merged during this step.
The data can be used either by the compiler if LTO isn't enabled:
... -fprofile-use=vmlinux.profdata ...or by LLD if LTO is enabled:
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
... -lto-cs-profile-file=vmlinux.profdata ...This LLD option does not exist.
LLD does have some--lto-*options but the-lto-*form is not supported (it clashes with -l) https://reviews.llvm.org/D79371That's strange. I've been using that option for years now. :-) Is this a recent change?
The more frequently used options (specifyed by the clang driver) are -plugin-opt=... (options implemented by LLVMgold.so).
-lto-*is rare.Quoted 16 lines
Quoted 6 lines
(There is an earlier -fprofile-instr-generate which does instrumentation in Clang, but the option does not have broad usage. It is used more for code coverage, not for optimization. Noticeably, it does not even implement the Kirchhoff's current law optimization)
Right. I've been told outside of this email that -fprofile-generate is the prefered flag to use.
Quoted 6 lines
-fprofile-use= is used by both regular PGO and context-sensitive PGO (CSPGO).
clang -flto=thin -fprofile-use= passes -plugin-opt=cs-profile-path= to the linker. For regular PGO, this option is effectively a no-op (confirmed with CSPGO main developer).
So I think the "or by LLD if LTO is enabled:" part should be removed.
Quoted 7 lines
Quoted 2 lines
But what if you specify the linking step explicitly? Linux doesn't call "clang" when linking, but "ld.lld".
Regular PGO+LTO does not need -plugin-opt=cs-profile-path=
CSPGO+LTO needs it.
Because -fprofile-use= may be used by both, Clang driver adds it.
CSPGO is relevant in this this patch, so the linker option does not need to be mentioned.I'm still a bit confused. Are you saying that when clang uses
-flto=thin -fprofile-use=foothat the profile file "foo" is embedded into the bitcode file so that when the linker's run it'll be used?This is the workflow:
clang ... -fprofile-use=vmlinux.profdata ... -c -o foo.o
foo.cclang ... -fprofile-use=vmlinux.profdata ... -c -o bar.obar.cld.lld ... <output file> foo.o bar.o
Quoted 5 lines
Are you saying that we don't need to have "-plugin-opt=cs-profile-path=vmlinux.profdata" on the "ld.lld ..." line?
-bw
The backend compile step -flto=thin -fprofile-use=foo has all the information.
-plugin-opt=cs-profile-path=vmlinux.profdata is not needed for regular PGO.
Quoted 27 lines · Bill Wendling
From: Sami Tolvanen <[email protected]>
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.profrawMultiple raw profiles may be merged during this step.
The data can be used either by the compiler if LTO isn't enabled:
... -fprofile-use=vmlinux.profdata ...or by LLD if LTO is enabled:
... -lto-cs-profile-file=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.
Quoted 8 lines
Note that this method of profiling the kernel is clang-native and isn't compatible with clang's gcov support in kernel/gcov.
[1] https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization
Signed-off-by Sami Tolvanen <[email protected]>Co-developed-by Bill Wendling <[email protected]>Signed-off-by Bill Wendling <[email protected]>
I took this for a spin against x86_64_defconfig and ran into two issues:
1. https://github.com/ClangBuiltLinux/linux/issues/1252
There is also one in drivers/gpu/drm/i915/i915_query.c. For the time being, I added PGO_PROFILE_... := n for those two files.
2. After doing that, I run into an undefined function error with ld.lld.
How I tested:
$ make -skj"$(nproc)" LLVM=1 defconfig
$ scripts/config -e PGO_CLANG
$ make -skj"$(nproc)" LLVM=1 olddefconfig vmlinux all ld.lld: error: undefined symbol: __llvm_profile_instrument_memop
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
referenced by
head64.carch/x86/kernel/head64.o:(__early_make_pgtable) referenced by head64.c arch/x86/kernel/head64.o:(x86_64_start_kernel) referenced by head64.c arch/x86/kernel/head64.o:(copy_bootdata)referenced 2259 more times
Local diff:
drivers/char/Makefile +1 -0
@@ -4,6 +4,7 @@ # obj-y += mem.o random.o +PGO_PROFILE_random.o := n obj-$(CONFIG_TTY_PRINTK) += ttyprintk.o obj-y += misc.o obj-$(CONFIG_ATARI_DSP56K) += dsp56k.o
drivers/gpu/drm/i915/Makefile +1 -0
@@ -168,6 +168,7 @@ i915-y += \ i915_vma.o \ intel_region_lmem.o \ intel_wopcm.o +PGO_PROFILE_i915_query.o := n # general-purpose microcontroller (GuC) support i915-y += gt/uc/intel_uc.o \
Quoted 34 lines · Nathan Chancellor
Quoted 32 lines · Bill Wendling
From: Sami Tolvanen <[email protected]>
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.profrawMultiple raw profiles may be merged during this step.
The data can be used either by the compiler if LTO isn't enabled:
... -fprofile-use=vmlinux.profdata ...or by LLD if LTO is enabled:
... -lto-cs-profile-file=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 and isn't compatible with clang's gcov support in kernel/gcov.
[1] https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization
Quoted 7 lines
Quoted 3 lines
Signed-off-by Sami Tolvanen <[email protected]>Co-developed-by Bill Wendling <[email protected]>Signed-off-by Bill Wendling <[email protected]>I took this for a spin against
x86_64_defconfigand ran into two issues:
"Cannot split an edge from a CallBrInst"
Looks like that should be fixed first, then we should gate this feature on clang-12.
Quoted 6 lines
There is also one in
drivers/gpu/drm/i915/i915_query.c. For the time being, I addedPGO_PROFILE_... := n for those two files.2. After doing that, I run into an undefined function error with ld.lld.
Quoted 8 lines
How I tested:
$ make -skj"$(nproc)" LLVM=1 defconfig$ scripts/config -e PGO_CLANG$ make -skj"$(nproc)" LLVM=1 olddefconfig vmlinux all ld.lld: error: undefined symbol: __llvm_profile_instrument_memop
Err...that seems like it should be implemented inkernel/pgo/instrument.c in this patch in a v2?
Quoted 27 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
referenced by
head64.carch/x86/kernel/head64.o:(__early_make_pgtable) referenced by head64.c arch/x86/kernel/head64.o:(x86_64_start_kernel) referenced by head64.c arch/x86/kernel/head64.o:(copy_bootdata)referenced 2259 more times
Local diff:
drivers/char/Makefile +1
-0@@ -4,6 +4,7 @@ # obj-y += mem.o random.o +PGO_PROFILE_random.o := n obj-$(CONFIG_TTY_PRINTK) += ttyprintk.o obj-y += misc.o obj-$(CONFIG_ATARI_DSP56K) += dsp56k.odrivers/gpu/drm/i915/Makefile +0
-0
drivers/gpu/drm/i915/Makefile i915-y += \ · line 174
Quoted 7 lines
drivers/gpu/drm/i915/Makefile +1
-0@@ -168,6 +168,7 @@ i915-y += \ i915_vma.o \ intel_region_lmem.o \ intel_wopcm.o +PGO_PROFILE_i915_query.o := n # general-purpose microcontroller (GuC) support i915-y += gt/uc/intel_uc.o \
I'd rather have these both sorted out before landing with PGO disabled on these files.
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 41 lines · Nick Desaulniers
Quoted 38 lines · Nathan Chancellor
Quoted 35 lines · Bill Wendling
From: Sami Tolvanen <[email protected]>
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.profrawMultiple raw profiles may be merged during this step.
The data can be used either by the compiler if LTO isn't enabled:
... -fprofile-use=vmlinux.profdata ...or by LLD if LTO is enabled:
... -lto-cs-profile-file=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 and isn't compatible with clang's gcov support in kernel/gcov.
[1] https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization
Signed-off-by Sami Tolvanen <[email protected]>Co-developed-by Bill Wendling <[email protected]>Signed-off-by Bill Wendling <[email protected]>
Quoted 8 lines
Quoted 3 lines
I took this for a spin against
x86_64_defconfigand ran into two issues:"Cannot split an edge from a CallBrInst"
Looks like that should be fixed first, then we should gate this feature on clang-12.
Weird. I'll investigate.
Quoted 10 lines
Quoted 10 lines
There is also one in
drivers/gpu/drm/i915/i915_query.c. For the time being, I addedPGO_PROFILE_... := n for those two files.2. After doing that, I run into an undefined function error with ld.lld.
How I tested:
$ make -skj"$(nproc)" LLVM=1 defconfig
Quoted 8 lines
Quoted 4 lines
$ scripts/config -e PGO_CLANG$ make -skj"$(nproc)" LLVM=1 olddefconfig vmlinux all ld.lld: error: undefined symbol: __llvm_profile_instrument_memopErr...that seems like it should be implemented in
kernel/pgo/instrument.cin this patch in a v2?
Yes. I'll submit a new V2 with this and other feedback integrated.
Quoted 32 lines
Quoted 32 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
referenced by
head64.carch/x86/kernel/head64.o:(__early_make_pgtable) referenced by head64.c arch/x86/kernel/head64.o:(x86_64_start_kernel) referenced by head64.c arch/x86/kernel/head64.o:(copy_bootdata)referenced 2259 more times
Local diff:
drivers/char/Makefile +1
-0@@ -4,6 +4,7 @@ # obj-y += mem.o random.o +PGO_PROFILE_random.o := n obj-$(CONFIG_TTY_PRINTK) += ttyprintk.o obj-y += misc.o obj-$(CONFIG_ATARI_DSP56K) += dsp56k.odrivers/gpu/drm/i915/Makefile +1
-0@@ -168,6 +168,7 @@ i915-y += \ i915_vma.o \ intel_region_lmem.o \ intel_wopcm.o +PGO_PROFILE_i915_query.o := n
Quoted 6 lines
drivers/gpu/drm/i915/Makefile i915-y += \ · line 174
Quoted 2 lines
drivers/gpu/drm/i915/Makefile +0
-0# general-purpose microcontroller (GuC) support i915-y += gt/uc/intel_uc.o \I'd rather have these both sorted out before landing with PGO disabled on these files.
Agreed.
-bw
PATCH v2 1/1 pgo: add clang's Profile Guided Optimization infrastructure
In reply to Bill WendlingFrom: Sami Tolvanen <[email protected]>
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 and isn't compatible with clang's gcov support in kernel/gcov.
[1] https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization
--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/pgo.rst | 127 +++++++++ MAINTAINERS | 9 + Makefile | 3 + arch/Kconfig | 1 + arch/arm/boot/bootp/Makefile | 1 + arch/arm/boot/compressed/Makefile | 1 + arch/arm/vdso/Makefile | 3 +- arch/arm64/kernel/vdso/Makefile | 3 +- arch/arm64/kvm/hyp/nvhe/Makefile | 1 + arch/mips/boot/compressed/Makefile | 1 + arch/mips/vdso/Makefile | 1 + arch/nds32/kernel/vdso/Makefile | 4 +- arch/parisc/boot/compressed/Makefile | 1 + arch/powerpc/kernel/Makefile | 6 +- arch/powerpc/kernel/trace/Makefile | 3 +- arch/powerpc/kernel/vdso32/Makefile | 1 + arch/powerpc/kernel/vdso64/Makefile | 1 + arch/powerpc/kexec/Makefile | 3 +- arch/powerpc/xmon/Makefile | 1 + arch/riscv/kernel/vdso/Makefile | 3 +- arch/s390/boot/Makefile | 1 + arch/s390/boot/compressed/Makefile | 1 + arch/s390/kernel/Makefile | 1 + arch/s390/kernel/vdso64/Makefile | 3 +- arch/s390/purgatory/Makefile | 1 + arch/sh/boot/compressed/Makefile | 1 + arch/sh/mm/Makefile | 1 + arch/sparc/vdso/Makefile | 1 + arch/x86/Kconfig | 1 + arch/x86/boot/Makefile | 1 + arch/x86/boot/compressed/Makefile | 1 + arch/x86/entry/vdso/Makefile | 1 + arch/x86/kernel/vmlinux.lds.S | 2 + arch/x86/platform/efi/Makefile | 1 + arch/x86/purgatory/Makefile | 1 + arch/x86/realmode/rm/Makefile | 1 + arch/x86/um/vdso/Makefile | 1 + drivers/firmware/efi/libstub/Makefile | 1 + drivers/s390/char/Makefile | 1 + include/asm-generic/vmlinux.lds.h | 44 +++ kernel/Makefile | 1 + kernel/pgo/Kconfig | 34 +++ kernel/pgo/Makefile | 5 + kernel/pgo/fs.c | 382 ++++++++++++++++++++++++++ kernel/pgo/instrument.c | 188 +++++++++++++ kernel/pgo/pgo.h | 206 ++++++++++++++ scripts/Makefile.lib | 10 + 48 files changed, 1058 insertions(+), 9 deletions(-) create mode 100644 Documentation/dev-tools/pgo.rst create mode 100644 kernel/pgo/Kconfig create mode 100644 kernel/pgo/Makefile create mode 100644 kernel/pgo/fs.c create mode 100644 kernel/pgo/instrument.c create mode 100644 kernel/pgo/pgo.h
Documentation/dev-tools/index.rst +1 -0
@@ -26,6 +26,7 @@ whole; patches welcome! kgdb kselftest kunit/index + pgo .. only:: subproject and html
Documentation/dev-tools/pgo.rst new file +127 -0
127 lines not on this page · raw message
MAINTAINERS +9 -0
@@ -13954,6 +13954,15 @@ S: Maintained F: include/linux/personality.h F: include/uapi/linux/personality.h +PGO BASED KERNEL PROFILING +M: Sami Tolvanen <[email protected]> +M: Bill Wendling <[email protected]> +R: Nathan Chancellor <[email protected]> +R: Nick Desaulniers <[email protected]> +S: Supported +F: Documentation/dev-tools/pgo.rst +F: kernel/pgo + PHOENIX RC FLIGHT CONTROLLER ADAPTER M: Marcus Folkesson <[email protected]> L: [email protected]
Makefile +3 -0
@@ -659,6 +659,9 @@ endif # KBUILD_EXTMOD # Defaults to vmlinux, but the arch makefile usually adds further targets all: vmlinux +CFLAGS_PGO_CLANG := -fprofile-generate +export CFLAGS_PGO_CLANG + CFLAGS_GCOV := -fprofile-arcs -ftest-coverage \ $(call cc-option,-fno-tree-loop-im) \ $(call cc-disable-warning,maybe-uninitialized,)
arch/Kconfig +1 -0
@@ -1112,6 +1112,7 @@ config ARCH_SPLIT_ARG64 pairs of 32-bit arguments, select this option. source "kernel/gcov/Kconfig" +source "kernel/pgo/Kconfig" source "scripts/gcc-plugins/Kconfig"
arch/arm/boot/bootp/Makefile +1 -0
@@ -7,6 +7,7 @@ # GCOV_PROFILE := n +PGO_PROFILE := n LDFLAGS_bootp := --no-undefined -X \ --defsym initrd_phys=$(INITRD_PHYS) \
arch/arm/boot/compressed/Makefile +1 -0
@@ -24,6 +24,7 @@ OBJS += hyp-stub.o endif GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.
arch/arm/vdso/Makefile +2 -1
@@ -36,8 +36,9 @@ else CFLAGS_vgettimeofday.o = -O2 -include $(c-gettimeofday-y) endif -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in. KCOV_INSTRUMENT := n
arch/arm64/kernel/vdso/Makefile +2 -1
@@ -41,8 +41,9 @@ ifneq ($(c-gettimeofday-y),) CFLAGS_vgettimeofday.o += -include $(c-gettimeofday-y) endif -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n obj-y += vdso.o targets += vdso.lds
arch/arm64/kvm/hyp/nvhe/Makefile +1 -0
@@ -60,6 +60,7 @@ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS), $(KBUILD_CFLAG # compiler instrumentation that inserts callbacks or checks into the code may # cause crashes. Just disable it. GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n UBSAN_SANITIZE := n KCOV_INSTRUMENT := n
arch/mips/boot/compressed/Makefile +1 -0
@@ -37,6 +37,7 @@ KBUILD_AFLAGS := $(KBUILD_AFLAGS) -D__ASSEMBLY__ \ # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in. KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n # decompressor objects (linked with vmlinuz) vmlinuzobjs-y := $(obj)/head.o $(obj)/decompress.o $(obj)/string.o
arch/mips/vdso/Makefile +1 -0
@@ -66,6 +66,7 @@ ldflags-y := -Bsymbolic --no-undefined -soname=linux-vdso.so.1 \ CFLAGS_REMOVE_vdso.o = -pg GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KCOV_INSTRUMENT := n
arch/nds32/kernel/vdso/Makefile +2 -2
@@ -15,9 +15,9 @@ obj-vdso := $(addprefix $(obj)/, $(obj-vdso)) ccflags-y := -shared -fno-common -fno-builtin -nostdlib -fPIC -Wl,-shared -g \ -Wl,-soname=linux-vdso.so.1 -Wl,--hash-style=sysv -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n - +PGO_PROFILE := n obj-y += vdso.o targets += vdso.lds
arch/parisc/boot/compressed/Makefile +1 -0
@@ -7,6 +7,7 @@ KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n targets := vmlinux.lds vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2
arch/powerpc/kernel/Makefile +5 -1
@@ -153,17 +153,21 @@ endif obj-$(CONFIG_PPC_SECURE_BOOT) += secure_boot.o ima_arch.o secvar-ops.o obj-$(CONFIG_PPC_SECVAR_SYSFS) += secvar-sysfs.o -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive code GCOV_PROFILE_prom_init.o := n +PGO_PROFILE_prom_init.o := n KCOV_INSTRUMENT_prom_init.o := n UBSAN_SANITIZE_prom_init.o := n GCOV_PROFILE_kprobes.o := n +PGO_PROFILE_kprobes.o := n KCOV_INSTRUMENT_kprobes.o := n UBSAN_SANITIZE_kprobes.o := n GCOV_PROFILE_kprobes-ftrace.o := n +PGO_PROFILE_kprobes-ftrace.o := n KCOV_INSTRUMENT_kprobes-ftrace.o := n UBSAN_SANITIZE_kprobes-ftrace.o := n GCOV_PROFILE_syscall_64.o := n +PGO_PROFILE_syscall_64.o := n KCOV_INSTRUMENT_syscall_64.o := n UBSAN_SANITIZE_syscall_64.o := n UBSAN_SANITIZE_vdso.o := n
arch/powerpc/kernel/trace/Makefile +2 -1
@@ -23,7 +23,8 @@ obj-$(CONFIG_TRACING) += trace_clock.o obj-$(CONFIG_PPC64) += $(obj64-y) obj-$(CONFIG_PPC32) += $(obj32-y) -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive code GCOV_PROFILE_ftrace.o := n +PGO_PROFILE_ftrace.o := n KCOV_INSTRUMENT_ftrace.o := n UBSAN_SANITIZE_ftrace.o := n
arch/powerpc/kernel/vdso32/Makefile +1 -0
@@ -34,6 +34,7 @@ targets := $(obj-vdso32) vdso32.so.dbg obj-vdso32 := $(addprefix $(obj)/, $(obj-vdso32)) GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/powerpc/kernel/vdso64/Makefile +1 -0
@@ -21,6 +21,7 @@ targets := $(obj-vdso64) vdso64.so.dbg obj-vdso64 := $(addprefix $(obj)/, $(obj-vdso64)) GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/powerpc/kexec/Makefile +2 -1
@@ -16,7 +16,8 @@ endif endif -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive code GCOV_PROFILE_core_$(BITS).o := n +PGO_PROFILE_core_$(BITS).o := n KCOV_INSTRUMENT_core_$(BITS).o := n UBSAN_SANITIZE_core_$(BITS).o := n
arch/powerpc/xmon/Makefile +1 -0
@@ -2,6 +2,7 @@ # Makefile for xmon GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/riscv/kernel/vdso/Makefile +2 -1
@@ -32,8 +32,9 @@ CPPFLAGS_vdso.lds += -P -C -U$(ARCH) # Disable -pg to prevent insert call site CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n # Force dependency
arch/s390/boot/Makefile +1 -0
@@ -5,6 +5,7 @@ KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/s390/boot/compressed/Makefile +1 -0
@@ -7,6 +7,7 @@ KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/s390/kernel/Makefile +1 -0
@@ -14,6 +14,7 @@ CFLAGS_REMOVE_early.o = $(CC_FLAGS_FTRACE) endif GCOV_PROFILE_early.o := n +PGO_PROFILE_early.o := n KCOV_INSTRUMENT_early.o := n UBSAN_SANITIZE_early.o := n KASAN_SANITIZE_ipl.o := n
arch/s390/kernel/vdso64/Makefile +2 -1
@@ -35,8 +35,9 @@ obj-y += vdso64_wrapper.o targets += vdso64.lds CPPFLAGS_vdso64.lds += -P -C -U$(ARCH) -# Disable gcov profiling, ubsan and kasan for VDSO code +# Disable gcov and PGO profiling, ubsan and kasan for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/s390/purgatory/Makefile +1 -0
@@ -17,6 +17,7 @@ $(obj)/mem.o: $(srctree)/arch/s390/lib/mem.S FORCE KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/sh/boot/compressed/Makefile +1 -0
@@ -13,6 +13,7 @@ targets := vmlinux vmlinux.bin vmlinux.bin.gz \ OBJECTS = $(obj)/head_32.o $(obj)/misc.o $(obj)/cache.o GCOV_PROFILE := n +PGO_PROFILE := n # # IMAGE_OFFSET is the load offset of the compression loader
arch/sh/mm/Makefile +1 -0
@@ -43,3 +43,4 @@ obj-$(CONFIG_UNCACHED_MAPPING) += uncached.o obj-$(CONFIG_HAVE_SRAM_POOL) += sram.o GCOV_PROFILE_pmb.o := n +PGO_PROFILE_pmb.o := n
arch/sparc/vdso/Makefile +1 -0
@@ -115,6 +115,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 -Bsymbolic GCOV_PROFILE := n +PGO_PROFILE := n # # Install the unstripped copies of vdso*.so. If our toolchain supports
arch/x86/Kconfig +1 -0
@@ -96,6 +96,7 @@ config X86 select ARCH_SUPPORTS_DEBUG_PAGEALLOC select ARCH_SUPPORTS_NUMA_BALANCING if X86_64 select ARCH_SUPPORTS_KMAP_LOCAL_FORCE_MAP if NR_CPUS <= 4096 + select ARCH_SUPPORTS_PGO_CLANG if X86_64 select ARCH_USE_BUILTIN_BSWAP select ARCH_USE_QUEUED_RWLOCKS select ARCH_USE_QUEUED_SPINLOCKS
arch/x86/boot/Makefile +1 -0
@@ -71,6 +71,7 @@ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=) KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n $(obj)/bzImage: asflags-y := $(SVGA_MODE)
arch/x86/boot/compressed/Makefile +1 -0
@@ -54,6 +54,7 @@ CFLAGS_sev-es.o += -I$(objtree)/arch/x86/lib/ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE :=n KBUILD_LDFLAGS := -m elf_$(UTS_MACHINE)
arch/x86/entry/vdso/Makefile +1 -0
@@ -179,6 +179,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 \ $(call ld-option, --eh-frame-hdr) -Bsymbolic GCOV_PROFILE := n +PGO_PROFILE := n quiet_cmd_vdso_and_check = VDSO $@ cmd_vdso_and_check = $(cmd_vdso); $(cmd_vdso_check)
arch/x86/kernel/vmlinux.lds.S +2 -0
@@ -184,6 +184,8 @@ SECTIONS BUG_TABLE + PGO_CLANG_DATA + ORC_UNWIND_TABLE . = ALIGN(PAGE_SIZE);
arch/x86/platform/efi/Makefile +1 -0
@@ -2,6 +2,7 @@ OBJECT_FILES_NON_STANDARD_efi_thunk_$(BITS).o := y KASAN_SANITIZE := n GCOV_PROFILE := n +PGO_PROFILE := n obj-$(CONFIG_EFI) += quirks.o efi.o efi_$(BITS).o efi_stub_$(BITS).o obj-$(CONFIG_EFI_MIXED) += efi_thunk_$(BITS).o
arch/x86/purgatory/Makefile +1 -0
@@ -23,6 +23,7 @@ targets += purgatory.ro purgatory.chk # Sanitizer, etc. runtimes are unavailable and cannot be linked here. GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n UBSAN_SANITIZE := n KCSAN_SANITIZE := n
arch/x86/realmode/rm/Makefile +1 -0
@@ -76,4 +76,5 @@ KBUILD_CFLAGS := $(REALMODE_CFLAGS) -D_SETUP -D_WAKEUP \ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n
arch/x86/um/vdso/Makefile +1 -0
@@ -64,6 +64,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -fPIC -shared -Wl,--hash-style=sysv GCOV_PROFILE := n +PGO_PROFILE := n # # Install the unstripped copy of vdso*.so listed in $(vdso-install-y).
drivers/firmware/efi/libstub/Makefile +1 -0
@@ -40,6 +40,7 @@ KBUILD_CFLAGS := $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS)) GCOV_PROFILE := n +PGO_PROFILE := n # Sanitizer runtimes are unavailable and cannot be linked here. KASAN_SANITIZE := n KCSAN_SANITIZE := n
drivers/s390/char/Makefile +1 -0
@@ -9,6 +9,7 @@ CFLAGS_REMOVE_sclp_early_core.o = $(CC_FLAGS_FTRACE) endif GCOV_PROFILE_sclp_early_core.o := n +PGO_PROFILE_sclp_early_core.o := n KCOV_INSTRUMENT_sclp_early_core.o := n UBSAN_SANITIZE_sclp_early_core.o := n KASAN_SANITIZE_sclp_early_core.o := n
include/asm-generic/vmlinux.lds.h +44 -0
56 lines not on this page · raw message
kernel/Makefile +1 -0
@@ -111,6 +111,7 @@ obj-$(CONFIG_BPF) += bpf/ obj-$(CONFIG_KCSAN) += kcsan/ obj-$(CONFIG_SHADOW_CALL_STACK) += scs.o obj-$(CONFIG_HAVE_STATIC_CALL_INLINE) += static_call.o +obj-$(CONFIG_PGO_CLANG) += pgo/ obj-$(CONFIG_PERF_EVENTS) += events/
kernel/pgo/Kconfig new file +34 -0
@@ -0,0 +1,34 @@ +# SPDX-License-Identifier: GPL-2.0-only +menu "Profile Guided Optimization (PGO) (EXPERIMENTAL)" + +config ARCH_SUPPORTS_PGO_CLANG + bool + +config PGO_CLANG + bool "Enable clang's PGO-based kernel profiling" + depends on DEBUG_FS + depends on ARCH_SUPPORTS_PGO_CLANG + help + This option enables clang's PGO (Profile Guided Optimization) based + code profiling to better optimize the kernel. + + If unsure, say N. + + Run a representative workload for your application on a kernel + compiled with this option and download the raw profile file from + /sys/kernel/debug/pgo/profraw. This file needs to be processed with + llvm-profdata. It may be merged with other collected raw profiles. + + Copy the resulting profile file into vmlinux.profdata, and enable + KCFLAGS=-fprofile-use=vmlinux.profdata to produce an optimized + kernel. + + Note that a kernel compiled with profiling flags will be + significatnly larger and run slower. Also be sure to exclude files + from profiling which are not linked to the kernel image to prevent + linker errors. + + Note that the debugfs filesystem has to be mounted to access + profiling data. + +endmenu
kernel/pgo/Makefile new file +5 -0
@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +GCOV_PROFILE := n +PGO_PROFILE := n + +obj-y += fs.o instrument.o
kernel/pgo/fs.c new file +382 -0
382 lines not on this page · raw message
kernel/pgo/instrument.c new file +188 -0
188 lines not on this page · raw message
kernel/pgo/pgo.h new file +206 -0
206 lines not on this page · raw message
scripts/Makefile.lib +10 -0
@@ -143,6 +143,16 @@ _c_flags += $(if $(patsubst n%,, \ $(CFLAGS_GCOV)) endif +# +# Enable clang's PGO profiling flags for a file or directory depending on +# variables PGO_PROFILE_obj.o and PGO_PROFILE. +# +ifeq ($(CONFIG_PGO_CLANG),y) +_c_flags += $(if $(patsubst n%,, \ + $(PGO_PROFILE_$(basetarget).o)$(PGO_PROFILE)y), \ + $(CFLAGS_PGO_CLANG)) +endif + # # Enable address sanitizer flags for kernel except some files or directories # we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE)
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
Quoted 24 lines · Bill Wendling' via Clang Built Linux
From: Sami Tolvanen <[email protected]>
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.profrawMultiple 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.
Quoted 8 lines
Note that this method of profiling the kernel is clang-native and isn't compatible with clang's gcov support in kernel/gcov.
[1] https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization
Signed-off-by Sami Tolvanen <[email protected]>Co-developed-by Bill Wendling <[email protected]>Signed-off-by Bill Wendling <[email protected]>
Hi Bill,
can you add a history for the changelog of v2, please? Thanks.
Regards,
- Sedat -
Quoted 1,646 lines
--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/pgo.rst | 127 +++++++++ MAINTAINERS | 9 + Makefile | 3 + arch/Kconfig | 1 + arch/arm/boot/bootp/Makefile | 1 + arch/arm/boot/compressed/Makefile | 1 + arch/arm/vdso/Makefile | 3 +- arch/arm64/kernel/vdso/Makefile | 3 +- arch/arm64/kvm/hyp/nvhe/Makefile | 1 + arch/mips/boot/compressed/Makefile | 1 + arch/mips/vdso/Makefile | 1 + arch/nds32/kernel/vdso/Makefile | 4 +- arch/parisc/boot/compressed/Makefile | 1 + arch/powerpc/kernel/Makefile | 6 +- arch/powerpc/kernel/trace/Makefile | 3 +- arch/powerpc/kernel/vdso32/Makefile | 1 + arch/powerpc/kernel/vdso64/Makefile | 1 + arch/powerpc/kexec/Makefile | 3 +- arch/powerpc/xmon/Makefile | 1 + arch/riscv/kernel/vdso/Makefile | 3 +- arch/s390/boot/Makefile | 1 + arch/s390/boot/compressed/Makefile | 1 + arch/s390/kernel/Makefile | 1 + arch/s390/kernel/vdso64/Makefile | 3 +- arch/s390/purgatory/Makefile | 1 + arch/sh/boot/compressed/Makefile | 1 + arch/sh/mm/Makefile | 1 + arch/sparc/vdso/Makefile | 1 + arch/x86/Kconfig | 1 + arch/x86/boot/Makefile | 1 + arch/x86/boot/compressed/Makefile | 1 + arch/x86/entry/vdso/Makefile | 1 + arch/x86/kernel/vmlinux.lds.S | 2 + arch/x86/platform/efi/Makefile | 1 + arch/x86/purgatory/Makefile | 1 + arch/x86/realmode/rm/Makefile | 1 + arch/x86/um/vdso/Makefile | 1 + drivers/firmware/efi/libstub/Makefile | 1 + drivers/s390/char/Makefile | 1 + include/asm-generic/vmlinux.lds.h | 44 +++ kernel/Makefile | 1 + kernel/pgo/Kconfig | 34 +++ kernel/pgo/Makefile | 5 + kernel/pgo/fs.c | 382 ++++++++++++++++++++++++++ kernel/pgo/instrument.c | 188 +++++++++++++ kernel/pgo/pgo.h | 206 ++++++++++++++ scripts/Makefile.lib | 10 + 48 files changed, 1058 insertions(+), 9 deletions(-) create mode 100644 Documentation/dev-tools/pgo.rst create mode 100644 kernel/pgo/Kconfig create mode 100644 kernel/pgo/Makefile create mode 100644 kernel/pgo/fs.c create mode 100644 kernel/pgo/instrument.c create mode 100644 kernel/pgo/pgo.hDocumentation/dev-tools/index.rst +1
-0@@ -26,6 +26,7 @@ whole; patches welcome! kgdb kselftest kunit/index + pgo .. only:: subproject and htmlDocumentation/dev-tools/pgo.rst new file +127
-0@@ -0,0 +1,127 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=============================== +Using PGO with the Linux kernel +=============================== + +Clang's profiling kernel support (PGO_) enables profiling of the Linux kernel +when building with Clang. The profiling data is exported via the ``pgo`` +debugfs directory. + +.. _PGO: https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization + + +Preparation +=========== + +Configure the kernel with: + +.. code-block:: make + + CONFIG_DEBUG_FS=y + CONFIG_PGO_CLANG=y + +Note that kernels compiled with profiling flags will be significantly larger +and run slower. + +Profiling data will only become accessible once debugfs has been mounted: + +.. code-block:: sh + + mount -t debugfs none /sys/kernel/debug + + +Customization +============= + +You can enable or disable profiling for individual file and directories by +adding a line similar to the following to the respective kernel Makefile: + +- For a single file (e.g. main.o) + + .. code-block:: make + + PGO_PROFILE_main.o := y + +- For all files in one directory + + .. code-block:: make + + PGO_PROFILE := y + +To exclude files from being profiled use + + .. code-block:: make + + PGO_PROFILE_main.o := n + +and + + .. code-block:: make + + PGO_PROFILE := n + +Only files which are linked to the main kernel image or are compiled as kernel +modules are supported by this mechanism. + + +Files +===== + +The PGO kernel support creates the following files in debugfs: + +``/sys/kernel/debug/pgo`` + Parent directory for all PGO-related files. + +``/sys/kernel/debug/pgo/reset`` + Global reset file: resets all coverage data to zero when written to. + +``/sys/kernel/debug/profraw`` + The raw PGO data that must be processed with ``llvm_profdata``. + + +Workflow +======== + +The PGO kernel can be run on the host or test machines. The data though should +be analyzed with Clang's tools from the same Clang version as the kernel was +compiled. Clang's tolerant of version skew, but it's easier to use the same +Clang version. + +The profiling data is useful for optimizing the kernel, analyzing coverage, +etc. Clang offers tools to perform these tasks. + +Here is an example workflow for profiling an instrumented kernel with PGO and +using the result to optimize the kernel: + +1) Install the kernel on the TEST machine. + +2) Reset the data counters right before running the load tests + + .. code-block:: sh + + echo 1 > /sys/kernel/debug/pgo/reset + +3) Run the load tests. + +4) Collect the raw profile data + + .. code-block:: sh + + cp -a /sys/kernel/debug/pgo/profraw /tmp/vmlinux.profraw + +5) (Optional) Download the raw profile data to the HOST machine. + +6) Process the raw profile data + + .. code-block:: sh + + llvm-profdata merge --output=vmlinux.profdata vmlinux.profraw + + Note that multiple raw profile data files can be merged during this step. + +7) Rebuild the kernel using the profile data (PGO disabled) + + .. code-block:: sh + + make LLVM=1 KCFLAGS=-fprofile-use=vmlinux.profdata ...MAINTAINERS +9
-0@@ -13954,6 +13954,15 @@ S: Maintained F: include/linux/personality.h F: include/uapi/linux/personality.h +PGO BASED KERNEL PROFILING +M: Sami Tolvanen <[email protected]> +M: Bill Wendling <[email protected]> +R: Nathan Chancellor <[email protected]> +R: Nick Desaulniers <[email protected]> +S: Supported +F: Documentation/dev-tools/pgo.rst +F: kernel/pgo + PHOENIX RC FLIGHT CONTROLLER ADAPTER M: Marcus Folkesson <[email protected]> L: [email protected]Makefile +3
-0@@ -659,6 +659,9 @@ endif # KBUILD_EXTMOD # Defaults to vmlinux, but the arch makefile usually adds further targets all: vmlinux +CFLAGS_PGO_CLANG := -fprofile-generate +export CFLAGS_PGO_CLANG + CFLAGS_GCOV := -fprofile-arcs -ftest-coverage \ $(call cc-option,-fno-tree-loop-im) \ $(call cc-disable-warning,maybe-uninitialized,)arch/Kconfig +1
-0@@ -1112,6 +1112,7 @@ config ARCH_SPLIT_ARG64 pairs of 32-bit arguments, select this option. source "kernel/gcov/Kconfig" +source "kernel/pgo/Kconfig" source "scripts/gcc-plugins/Kconfig"arch/arm/boot/bootp/Makefile +1
-0@@ -7,6 +7,7 @@ # GCOV_PROFILE := n +PGO_PROFILE := n LDFLAGS_bootp := --no-undefined -X \ --defsym initrd_phys=$(INITRD_PHYS) \arch/arm/boot/compressed/Makefile +1
-0@@ -24,6 +24,7 @@ OBJS += hyp-stub.o endif GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.arch/arm/vdso/Makefile +2
-1@@ -36,8 +36,9 @@ else CFLAGS_vgettimeofday.o = -O2 -include $(c-gettimeofday-y) endif -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in. KCOV_INSTRUMENT := narch/arm64/kernel/vdso/Makefile +2
-1@@ -41,8 +41,9 @@ ifneq ($(c-gettimeofday-y),) CFLAGS_vgettimeofday.o += -include $(c-gettimeofday-y) endif -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n obj-y += vdso.o targets += vdso.ldsarch/arm64/kvm/hyp/nvhe/Makefile +1
-0@@ -60,6 +60,7 @@ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS), $(KBUILD_CFLAG # compiler instrumentation that inserts callbacks or checks into the code may # cause crashes. Just disable it. GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n UBSAN_SANITIZE := n KCOV_INSTRUMENT := narch/mips/boot/compressed/Makefile +1
-0@@ -37,6 +37,7 @@ KBUILD_AFLAGS := $(KBUILD_AFLAGS) -D__ASSEMBLY__ \ # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in. KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n # decompressor objects (linked with vmlinuz) vmlinuzobjs-y := $(obj)/head.o $(obj)/decompress.o $(obj)/string.oarch/mips/vdso/Makefile +1
-0@@ -66,6 +66,7 @@ ldflags-y := -Bsymbolic --no-undefined -soname=linux-vdso.so.1 \ CFLAGS_REMOVE_vdso.o = -pg GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KCOV_INSTRUMENT := narch/nds32/kernel/vdso/Makefile +2
-2@@ -15,9 +15,9 @@ obj-vdso := $(addprefix $(obj)/, $(obj-vdso)) ccflags-y := -shared -fno-common -fno-builtin -nostdlib -fPIC -Wl,-shared -g \ -Wl,-soname=linux-vdso.so.1 -Wl,--hash-style=sysv -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n - +PGO_PROFILE := n obj-y += vdso.o targets += vdso.ldsarch/parisc/boot/compressed/Makefile +1
-0@@ -7,6 +7,7 @@ KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n targets := vmlinux.lds vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2arch/powerpc/kernel/Makefile +5
-1@@ -153,17 +153,21 @@ endif obj-$(CONFIG_PPC_SECURE_BOOT) += secure_boot.o ima_arch.o secvar-ops.o obj-$(CONFIG_PPC_SECVAR_SYSFS) += secvar-sysfs.o -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive code GCOV_PROFILE_prom_init.o := n +PGO_PROFILE_prom_init.o := n KCOV_INSTRUMENT_prom_init.o := n UBSAN_SANITIZE_prom_init.o := n GCOV_PROFILE_kprobes.o := n +PGO_PROFILE_kprobes.o := n KCOV_INSTRUMENT_kprobes.o := n UBSAN_SANITIZE_kprobes.o := n GCOV_PROFILE_kprobes-ftrace.o := n +PGO_PROFILE_kprobes-ftrace.o := n KCOV_INSTRUMENT_kprobes-ftrace.o := n UBSAN_SANITIZE_kprobes-ftrace.o := n GCOV_PROFILE_syscall_64.o := n +PGO_PROFILE_syscall_64.o := n KCOV_INSTRUMENT_syscall_64.o := n UBSAN_SANITIZE_syscall_64.o := n UBSAN_SANITIZE_vdso.o := narch/powerpc/kernel/trace/Makefile +2
-1@@ -23,7 +23,8 @@ obj-$(CONFIG_TRACING) += trace_clock.o obj-$(CONFIG_PPC64) += $(obj64-y) obj-$(CONFIG_PPC32) += $(obj32-y) -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive code GCOV_PROFILE_ftrace.o := n +PGO_PROFILE_ftrace.o := n KCOV_INSTRUMENT_ftrace.o := n UBSAN_SANITIZE_ftrace.o := narch/powerpc/kernel/vdso32/Makefile +1
-0@@ -34,6 +34,7 @@ targets := $(obj-vdso32) vdso32.so.dbg obj-vdso32 := $(addprefix $(obj)/, $(obj-vdso32)) GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/powerpc/kernel/vdso64/Makefile +1
-0@@ -21,6 +21,7 @@ targets := $(obj-vdso64) vdso64.so.dbg obj-vdso64 := $(addprefix $(obj)/, $(obj-vdso64)) GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/powerpc/kexec/Makefile +2
-1@@ -16,7 +16,8 @@ endif endif -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive code GCOV_PROFILE_core_$(BITS).o := n +PGO_PROFILE_core_$(BITS).o := n KCOV_INSTRUMENT_core_$(BITS).o := n UBSAN_SANITIZE_core_$(BITS).o := narch/powerpc/xmon/Makefile +1
-0@@ -2,6 +2,7 @@ # Makefile for xmon GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/riscv/kernel/vdso/Makefile +2
-1@@ -32,8 +32,9 @@ CPPFLAGS_vdso.lds += -P -C -U$(ARCH) # Disable -pg to prevent insert call site CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n # Force dependencyarch/s390/boot/Makefile +1
-0@@ -5,6 +5,7 @@ KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/s390/boot/compressed/Makefile +1
-0@@ -7,6 +7,7 @@ KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/s390/kernel/Makefile +1
-0@@ -14,6 +14,7 @@ CFLAGS_REMOVE_early.o = $(CC_FLAGS_FTRACE) endif GCOV_PROFILE_early.o := n +PGO_PROFILE_early.o := n KCOV_INSTRUMENT_early.o := n UBSAN_SANITIZE_early.o := n KASAN_SANITIZE_ipl.o := narch/s390/kernel/vdso64/Makefile +2
-1@@ -35,8 +35,9 @@ obj-y += vdso64_wrapper.o targets += vdso64.lds CPPFLAGS_vdso64.lds += -P -C -U$(ARCH) -# Disable gcov profiling, ubsan and kasan for VDSO code +# Disable gcov and PGO profiling, ubsan and kasan for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/s390/purgatory/Makefile +1
-0@@ -17,6 +17,7 @@ $(obj)/mem.o: $(srctree)/arch/s390/lib/mem.S FORCE KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := narch/sh/boot/compressed/Makefile +1
-0@@ -13,6 +13,7 @@ targets := vmlinux vmlinux.bin vmlinux.bin.gz \ OBJECTS = $(obj)/head_32.o $(obj)/misc.o $(obj)/cache.o GCOV_PROFILE := n +PGO_PROFILE := n # # IMAGE_OFFSET is the load offset of the compression loaderarch/sh/mm/Makefile +1
-0@@ -43,3 +43,4 @@ obj-$(CONFIG_UNCACHED_MAPPING) += uncached.o obj-$(CONFIG_HAVE_SRAM_POOL) += sram.o GCOV_PROFILE_pmb.o := n +PGO_PROFILE_pmb.o := narch/sparc/vdso/Makefile +1
-0@@ -115,6 +115,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 -Bsymbolic GCOV_PROFILE := n +PGO_PROFILE := n # # Install the unstripped copies of vdso*.so. If our toolchain supportsarch/x86/Kconfig +1
-0@@ -96,6 +96,7 @@ config X86 select ARCH_SUPPORTS_DEBUG_PAGEALLOC select ARCH_SUPPORTS_NUMA_BALANCING if X86_64 select ARCH_SUPPORTS_KMAP_LOCAL_FORCE_MAP if NR_CPUS <= 4096 + select ARCH_SUPPORTS_PGO_CLANG if X86_64 select ARCH_USE_BUILTIN_BSWAP select ARCH_USE_QUEUED_RWLOCKS select ARCH_USE_QUEUED_SPINLOCKSarch/x86/boot/Makefile +1
-0@@ -71,6 +71,7 @@ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=) KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n $(obj)/bzImage: asflags-y := $(SVGA_MODE)arch/x86/boot/compressed/Makefile +1
-0@@ -54,6 +54,7 @@ CFLAGS_sev-es.o += -I$(objtree)/arch/x86/lib/ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE :=n KBUILD_LDFLAGS := -m elf_$(UTS_MACHINE)arch/x86/entry/vdso/Makefile +1
-0@@ -179,6 +179,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 \ $(call ld-option, --eh-frame-hdr) -Bsymbolic GCOV_PROFILE := n +PGO_PROFILE := n quiet_cmd_vdso_and_check = VDSO $@ cmd_vdso_and_check = $(cmd_vdso); $(cmd_vdso_check)arch/x86/kernel/vmlinux.lds.S +2
-0@@ -184,6 +184,8 @@ SECTIONS BUG_TABLE + PGO_CLANG_DATA + ORC_UNWIND_TABLE . = ALIGN(PAGE_SIZE);arch/x86/platform/efi/Makefile +1
-0@@ -2,6 +2,7 @@ OBJECT_FILES_NON_STANDARD_efi_thunk_$(BITS).o := y KASAN_SANITIZE := n GCOV_PROFILE := n +PGO_PROFILE := n obj-$(CONFIG_EFI) += quirks.o efi.o efi_$(BITS).o efi_stub_$(BITS).o obj-$(CONFIG_EFI_MIXED) += efi_thunk_$(BITS).oarch/x86/purgatory/Makefile +1
-0@@ -23,6 +23,7 @@ targets += purgatory.ro purgatory.chk # Sanitizer, etc. runtimes are unavailable and cannot be linked here. GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n UBSAN_SANITIZE := n KCSAN_SANITIZE := narch/x86/realmode/rm/Makefile +1
-0@@ -76,4 +76,5 @@ KBUILD_CFLAGS := $(REALMODE_CFLAGS) -D_SETUP -D_WAKEUP \ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := narch/x86/um/vdso/Makefile +1
-0@@ -64,6 +64,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -fPIC -shared -Wl,--hash-style=sysv GCOV_PROFILE := n +PGO_PROFILE := n # # Install the unstripped copy of vdso*.so listed in $(vdso-install-y).drivers/firmware/efi/libstub/Makefile +1
-0@@ -40,6 +40,7 @@ KBUILD_CFLAGS := $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS)) GCOV_PROFILE := n +PGO_PROFILE := n # Sanitizer runtimes are unavailable and cannot be linked here. KASAN_SANITIZE := n KCSAN_SANITIZE := ndrivers/s390/char/Makefile +1
-0@@ -9,6 +9,7 @@ CFLAGS_REMOVE_sclp_early_core.o = $(CC_FLAGS_FTRACE) endif GCOV_PROFILE_sclp_early_core.o := n +PGO_PROFILE_sclp_early_core.o := n KCOV_INSTRUMENT_sclp_early_core.o := n UBSAN_SANITIZE_sclp_early_core.o := n KASAN_SANITIZE_sclp_early_core.o := ninclude/asm-generic/vmlinux.lds.h +44
-0@@ -316,6 +316,49 @@#define THERMAL_TABLE (name)#endif +#ifdef CONFIG_PGO_CLANG +#define PGO_CLANG_DATA \ + __llvm_prf_data : AT(ADDR(__llvm_prf_data) - LOAD_OFFSET) { \ + . = ALIGN(8); \ + __llvm_prf_start = .; \ + __llvm_prf_data_start = .; \ + KEEP(*(__llvm_prf_data)) \ + . = ALIGN(8); \ + __llvm_prf_data_end = .; \ + } \ + __llvm_prf_cnts : AT(ADDR(__llvm_prf_cnts) - LOAD_OFFSET) { \ + . = ALIGN(8); \ + __llvm_prf_cnts_start = .; \ + KEEP(*(__llvm_prf_cnts)) \ + . = ALIGN(8); \ + __llvm_prf_cnts_end = .; \ + } \ + __llvm_prf_names : AT(ADDR(__llvm_prf_names) - LOAD_OFFSET) { \ + . = ALIGN(8); \ + __llvm_prf_names_start = .; \ + KEEP(*(__llvm_prf_names)) \ + . = ALIGN(8); \ + __llvm_prf_names_end = .; \ + . = ALIGN(8); \ + } \ + __llvm_prf_vals : AT(ADDR(__llvm_prf_vals) - LOAD_OFFSET) { \ + __llvm_prf_vals_start = .; \ + KEEP(*(__llvm_prf_vals)) \ + . = ALIGN(8); \ + __llvm_prf_vals_end = .; \ + . = ALIGN(8); \ + } \ + __llvm_prf_vnds : AT(ADDR(__llvm_prf_vnds) - LOAD_OFFSET) { \ + __llvm_prf_vnds_start = .; \ + KEEP(*(__llvm_prf_vnds)) \ + . = ALIGN(8); \ + __llvm_prf_vnds_end = .; \ + __llvm_prf_end = .; \ + } +#else +#define PGO_CLANG_DATA +#endif +#define KERNEL_DTB () \ STRUCT_ALIGN(); \ __dtb_start = .; \@@ -1125,6 +1168,7 @@ CONSTRUCTORS \ } \BUG_TABLE \ + PGO_CLANG_DATA#define INIT_TEXT_SECTION (inittext_align) \ . = ALIGN(inittext_align); \kernel/Makefile +1
-0@@ -111,6 +111,7 @@ obj-$(CONFIG_BPF) += bpf/ obj-$(CONFIG_KCSAN) += kcsan/ obj-$(CONFIG_SHADOW_CALL_STACK) += scs.o obj-$(CONFIG_HAVE_STATIC_CALL_INLINE) += static_call.o +obj-$(CONFIG_PGO_CLANG) += pgo/ obj-$(CONFIG_PERF_EVENTS) += events/kernel/pgo/Kconfig new file +34
-0@@ -0,0 +1,34 @@ +# SPDX-License-Identifier: GPL-2.0-only +menu "Profile Guided Optimization (PGO) (EXPERIMENTAL)" + +config ARCH_SUPPORTS_PGO_CLANG + bool + +config PGO_CLANG + bool "Enable clang's PGO-based kernel profiling" + depends on DEBUG_FS + depends on ARCH_SUPPORTS_PGO_CLANG + help + This option enables clang's PGO (Profile Guided Optimization) based + code profiling to better optimize the kernel. + + If unsure, say N. + + Run a representative workload for your application on a kernel + compiled with this option and download the raw profile file from + /sys/kernel/debug/pgo/profraw. This file needs to be processed with + llvm-profdata. It may be merged with other collected raw profiles. + + Copy the resulting profile file into vmlinux.profdata, and enable + KCFLAGS=-fprofile-use=vmlinux.profdata to produce an optimized + kernel. + + Note that a kernel compiled with profiling flags will be + significatnly larger and run slower. Also be sure to exclude files + from profiling which are not linked to the kernel image to prevent + linker errors. + + Note that the debugfs filesystem has to be mounted to access + profiling data. + +endmenukernel/pgo/Makefile new file +5
-0@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +GCOV_PROFILE := n +PGO_PROFILE := n + +obj-y += fs.o instrument.okernel/pgo/fs.c new file +382
-0@@ -0,0 +1,382 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2019 Google, Inc. +* +* Author: +* Sami Tolvanen <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#define pr_fmt (fmt) "pgo: " fmt + +#include <linux/kernel.h> +#include <linux/debugfs.h> +#include <linux/fs.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/vmalloc.h> +#include "pgo.h" + +static struct dentry *directory; + +struct prf_private_data { +void *buffer; +unsigned long size; +}; + +/* +* Raw profile data format: +* +* - llvm_prf_header +* - __llvm_prf_data +* - __llvm_prf_cnts +* - __llvm_prf_names +* - zero padding to 8 bytes +* - for each llvm_prf_data in __llvm_prf_data: +* - llvm_prf_value_data +* - llvm_prf_value_record + site count array +* - llvm_prf_value_node_data +* ... +* ... +* ... +*/ + +static void prf_fill_header (void **buffer) +{ +struct llvm_prf_header *header = *(struct llvm_prf_header **)buffer; + + header->magic =LLVM_PRF_MAGIC ; + header->version =LLVM_PRF_VARIANT_MASK_IR |LLVM_PRF_VERSION ; + header->data_size =prf_data_count (); + header->padding_bytes_before_counters =0 ; + header->counters_size =prf_cnts_count (); + header->padding_bytes_after_counters =0 ; + header->names_size =prf_names_count (); + header->counters_delta = (u64 )__llvm_prf_cnts_start; + header->names_delta = (u64 )__llvm_prf_names_start; + header->value_kind_last =LLVM_PRF_IPVK_LAST ; + + *buffer +=sizeof (*header); +} + +/* +* Copy the source into the buffer, incrementing the pointer into buffer in the +* process. +*/ +static void prf_copy_to_buffer (void **buffer,void *src,unsigned long size) +{ +memcpy (*buffer, src, size); + *buffer += size; +} + +static u32 __prf_get_value_size (struct llvm_prf_data *p,u32 *value_kinds) +{ +struct llvm_prf_value_node **nodes = + (struct llvm_prf_value_node **)p->values; +u32 kinds =0 ; +u32 size =0 ; +unsigned int kind; +unsigned int n; +unsigned int s =0 ; + +for (kind =0 ; kind <ARRAY_SIZE (p->num_value_sites); kind++) { +unsigned int sites = p->num_value_sites[kind]; + +if (!sites) +continue ; + +/* Record + site count array */ + size +=prf_get_value_record_size (sites); + kinds++; + +if (!nodes) +continue ; + +for (n =0 ; n < sites; n++) { +u32 count =0 ; +struct llvm_prf_value_node *site = nodes[s + n]; + +while (site && ++count <=U8_MAX ) + site = site->next; + + size += count * +sizeof (struct llvm_prf_value_node_data ); + } + + s += sites; + } + +if (size) + size +=sizeof (struct llvm_prf_value_data ); + +if (value_kinds) + *value_kinds = kinds; + +return size; +} + +static u32 prf_get_value_size (void ) +{ +u32 size =0 ; +struct llvm_prf_data *p; + +for (p = __llvm_prf_data_start; p < __llvm_prf_data_end; p++) + size +=__prf_get_value_size (p,NULL ); + +return size; +} + +/* Serialize the profiling's value. */ +static void prf_serialize_value (struct llvm_prf_data *p,void **buffer) +{ +struct llvm_prf_value_data header; +struct llvm_prf_value_node **nodes = + (struct llvm_prf_value_node **)p->values; +unsigned int kind; +unsigned int n; +unsigned int s =0 ; + + header.total_size =__prf_get_value_size (p, &header.num_value_kinds); + +if (!header.num_value_kinds) +/* Nothing to write. */ +return ; + +prf_copy_to_buffer (buffer, &header,sizeof (header)); + +for (kind =0 ; kind <ARRAY_SIZE (p->num_value_sites); kind++) { +struct llvm_prf_value_record *record; +u8 *counts; +unsigned int sites = p->num_value_sites[kind]; + +if (!sites) +continue ; + +/* Profiling value record. */ + record = *(struct llvm_prf_value_record **)buffer; + *buffer +=prf_get_value_record_header_size (); + + record->kind = kind; + record->num_value_sites = sites; + +/* Site count array. */ + counts = *(u8 **)buffer; + *buffer +=prf_get_value_record_site_count_size (sites); + +/* +* If we don't have nodes, we can skip updating the site count +* array, because the buffer is zero filled. +*/ +if (!nodes) +continue ; + +for (n =0 ; n < sites; n++) { +u32 count =0 ; +struct llvm_prf_value_node *site = nodes[s + n]; + +while (site && ++count <=U8_MAX ) { +prf_copy_to_buffer (buffer, site, +sizeof (struct llvm_prf_value_node_data )); + site = site->next; + } + + counts[n] = (u8 )count; + } + + s += sites; + } +} + +static void prf_serialize_values (void **buffer) +{ +struct llvm_prf_data *p; + +for (p = __llvm_prf_data_start; p < __llvm_prf_data_end; p++) +prf_serialize_value (p, buffer); +} + +static inline unsigned long prf_get_padding (unsigned long size) +{ +return 8 - (size %8 ); +} + +static unsigned long prf_buffer_size (void ) +{ +return sizeof (struct llvm_prf_header ) + +prf_data_size () + +prf_cnts_size () + +prf_names_size () + +prf_get_padding (prf_names_size ()) + +prf_get_value_size (); +} + +/* Serialize the profling data into a format LLVM's tools can understand. */ +static int prf_serialize (struct prf_private_data *p) +{ +int err =0 ; +void *buffer; + + p->size =prf_buffer_size (); + p->buffer =vzalloc (p->size); + +if (!p->buffer) { + err = -ENOMEM ; + goto out; + } + + buffer = p->buffer; + +prf_fill_header (&buffer); +prf_copy_to_buffer (&buffer, __llvm_prf_data_start,prf_data_size ()); +prf_copy_to_buffer (&buffer, __llvm_prf_cnts_start,prf_cnts_size ()); +prf_copy_to_buffer (&buffer, __llvm_prf_names_start,prf_names_size ()); + buffer +=prf_get_padding (prf_names_size ()); + +prf_serialize_values (&buffer); + +out: +return err; +} + +/* open() implementation for PGO. Creates a copy of the profiling data set. */ +static int prf_open (struct inode *inode,struct file *file) +{ +struct prf_private_data *data; +unsigned long flags; +int err; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) { + err = -ENOMEM ; + goto out; + } + + flags =prf_lock (); + + err =prf_serialize (data); +if (err) { +kfree (data); + goto out_unlock; + } + + file->private_data = data; + +out_unlock: +prf_unlock (flags); +out: +return err; +} + +/* read() implementation for PGO. */ +static ssize_t prf_read (struct file *file,char __user *buf,size_t count, +loff_t *ppos) +{ +struct prf_private_data *data = file->private_data; + +BUG_ON (!data); + +return simple_read_from_buffer (buf, count, ppos, data->buffer, + data->size); +} + +/* release() implementation for PGO. Release resources allocated by open(). */ +static int prf_release (struct inode *inode,struct file *file) +{ +struct prf_private_data *data = file->private_data; + +if (data) { +vfree (data->buffer); +kfree (data); + } + +return 0 ; +} + +static const struct file_operations prf_fops = { + .owner =THIS_MODULE , + .open = prf_open, + .read = prf_read, + .llseek = default_llseek, + .release = prf_release +}; + +/* write() implementation for resetting PGO's profile data. */ +static ssize_t reset_write (struct file *file,const char __user *addr, +size_t len,loff_t *pos) +{ +struct llvm_prf_data *data; + +memset (__llvm_prf_cnts_start,0 ,prf_cnts_size ()); + +for (data = __llvm_prf_data_start; data < __llvm_prf_data_end; ++data) { +struct llvm_prf_value_node **vnodes; +u64 current_vsite_count; +u32 i; + +if (!data->values) +continue ; + + current_vsite_count =0 ; + vnodes = (struct llvm_prf_value_node **)data->values; + +for (i =LLVM_PRF_IPVK_FIRST ; i <=LLVM_PRF_IPVK_LAST ; ++i) + current_vsite_count += data->num_value_sites[i]; + +for (i =0 ; i < current_vsite_count; ++i) { +struct llvm_prf_value_node *current_vnode = vnodes[i]; + +while (current_vnode) { + current_vnode->count =0 ; + current_vnode = current_vnode->next; + } + } + } + +return len; +} + +static const struct file_operations prf_reset_fops = { + .owner =THIS_MODULE , + .write = reset_write, + .llseek = noop_llseek, +}; + +/* Create debugfs entries. */ +static int __init pgo_init (void ) +{ + directory =debugfs_create_dir ("pgo" ,NULL ); +if (!directory) + goto err_remove; + +if (!debugfs_create_file ("profraw" ,0600 , directory,NULL , + &prf_fops)) + goto err_remove; + +if (!debugfs_create_file ("reset" ,0200 , directory,NULL , + &prf_reset_fops)) + goto err_remove; + +return 0 ; + +err_remove: +pr_err ("initialization failed\n" ); +return -EIO ; +} + +/* Remove debufs entries. */ +static void __exit pgo_exit(void) +{ +debugfs_remove_recursive (directory); +} + +module_init (pgo_init); +module_exit (pgo_exit);kernel/pgo/instrument.c new file +188
-0@@ -0,0 +1,188 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2019 Google, Inc. +* +* Author: +* Sami Tolvanen <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#define pr_fmt (fmt) "pgo: " fmt + +#include <linux/kernel.h> +#include <linux/export.h> +#include <linux/spinlock.h> +#include <linux/types.h> +#include "pgo.h" + +/* Lock guarding value node access and serialization. */ +static DEFINE_SPINLOCK (pgo_lock); +static int current_node; + +unsigned long prf_lock (void ) +{ +unsigned long flags; + +spin_lock_irqsave (&pgo_lock, flags); + +return flags; +} + +void prf_unlock (unsigned long flags) +{ +spin_unlock_irqrestore (&pgo_lock, flags); +} + +/* +* Return a newly allocated profiling value node which contains the tracked +* value by the value profiler. +* Note: caller *must* hold pgo_lock. +*/ +static struct llvm_prf_value_node *allocate_node (struct llvm_prf_data *p, +u32 index,u64 value) +{ +if (&__llvm_prf_vnds_start[current_node +1 ] >= __llvm_prf_vnds_end) +return NULL ;/* Out of nodes */ + + current_node++; + +/* Make sure the node is entirely within the section */ +if (&__llvm_prf_vnds_start[current_node] >= __llvm_prf_vnds_end || + &__llvm_prf_vnds_start[current_node +1 ] > __llvm_prf_vnds_end) +return NULL ; + +return &__llvm_prf_vnds_start[current_node]; +} + +/* +* Counts the number of times a target value is seen. +* +* Records the target value for the CounterIndex if not seen before. Otherwise, +* increments the counter associated w/ the target value. +*/ +void __llvm_profile_instrument_target (u64 target_value,void *data,u32 index) +{ +struct llvm_prf_data *p = (struct llvm_prf_data *)data; +struct llvm_prf_value_node **counters; +struct llvm_prf_value_node *curr; +struct llvm_prf_value_node *min =NULL ; +struct llvm_prf_value_node *prev =NULL ; +u64 min_count =U64_MAX ; +u8 values =0 ; +unsigned long flags; + +if (!p || !p->values) +return ; + + counters = (struct llvm_prf_value_node **)p->values; + curr = counters[index]; + +while (curr) { +if (target_value == curr->value) { + curr->count++; +return ; + } + +if (curr->count < min_count) { + min_count = curr->count; + min = curr; + } + + prev = curr; + curr = curr->next; + values++; + } + +if (values >=LLVM_PRF_MAX_NUM_VALS_PER_SITE ) { +if (!min->count || !(--min->count)) { + curr = min; + curr->value = target_value; + curr->count++; + } +return ; + } + +/* Lock when updating the value node structure. */ + flags =prf_lock (); + + curr =allocate_node (p, index, target_value); +if (!curr) + goto out; + + curr->value = target_value; + curr->count++; + +if (!counters[index]) + counters[index] = curr; +else if (prev && !prev->next) + prev->next = curr; + +out: +prf_unlock (flags); +} +EXPORT_SYMBOL (__llvm_profile_instrument_target); + +/* Counts the number of times a range of targets values are seen. */ +void __llvm_profile_instrument_range (u64 target_value,void *data, +u32 index,s64 precise_start, +s64 precise_last,s64 large_value) +{ +if (large_value !=S64_MIN && (s64 )target_value >= large_value) + target_value = large_value; +else if ((s64 )target_value < precise_start || + (s64 )target_value > precise_last) + target_value = precise_last +1 ; + +__llvm_profile_instrument_target (target_value, data, index); +} +EXPORT_SYMBOL (__llvm_profile_instrument_range); + +static inline int inst_prof_popcount (unsigned long long value) +{ + value = value - ((value >>1 ) &0x5555555555555555ULL ); + value = (value &0x3333333333333333ULL ) + + ((value >>2 ) &0x3333333333333333ULL ); + value = (value + (value >>4 )) &0x0F0F0F0F0F0F0F0FULL ; + +return (int )((unsigned long long )(value *0x0101010101010101ULL ) >>56 ); +} + +static u64 inst_prof_get_range_rep_value (u64 value) +{ +if (value <=8 ) +/* The first ranges are individually tracked, us it as is. */ +return value; +else if (value >=513 ) +/* The last range is mapped to its lowest value. */ +return 513 ; +else if (inst_prof_popcount (value) ==1 ) +/* If it's a power of two, use it as is. */ +return value; + +/* Otherwise, take to the previous power of two + 1. */ +return (1 << (64 -__builtin_clzll (value) -1 )) +1 ; +} + +/* +* The target values are partitioned into multiple ranges. The range spec is +* defined in compiler-rt/include/profile/InstrProfData.inc. +*/ +void __llvm_profile_instrument_memop (u64 target_value,void *data, +u32 counter_index) +{ +u64 rep_value; + +/* Map the target value to the representative value of its range. */ + rep_value =inst_prof_get_range_rep_value (target_value); +__llvm_profile_instrument_target (rep_value, data, counter_index); +} +EXPORT_SYMBOL (__llvm_profile_instrument_memop);kernel/pgo/pgo.h new file +206
-0@@ -0,0 +1,206 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2019 Google, Inc. +* +* Author: +* Sami Tolvanen <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef _PGO_H +#define _PGO_H + +/* +* Note: These internal LLVM definitions must match the compiler version. +* See llvm/include/llvm/ProfileData/InstrProfData.inc in LLVM's source code. +*/ + +#ifdef CONFIG_64BIT +#define LLVM_PRF_MAGIC \ + ((u64)255 << 56 | \ + (u64)'l' << 48 | \ + (u64)'p' << 40 | \ + (u64)'r' << 32 | \ + (u64)'o' << 24 | \ + (u64)'f' << 16 | \ + (u64)'r' << 8 | \ + (u64)129) +#else +#define LLVM_PRF_MAGIC \ + ((u64)255 << 56 | \ + (u64)'l' << 48 | \ + (u64)'p' << 40 | \ + (u64)'r' << 32 | \ + (u64)'o' << 24 | \ + (u64)'f' << 16 | \ + (u64)'R' << 8 | \ + (u64)129) +#endif + +#define LLVM_PRF_VERSION 5 +#define LLVM_PRF_DATA_ALIGN 8 +#define LLVM_PRF_IPVK_FIRST 0 +#define LLVM_PRF_IPVK_LAST 1 +#define LLVM_PRF_MAX_NUM_VALS_PER_SITE 16 + +#define LLVM_PRF_VARIANT_MASK_IR (0x1ull << 56) +#define LLVM_PRF_VARIANT_MASK_CSIR (0x1ull << 57) + +/** +* struct llvm_prf_header - represents the raw profile header data structure. +* @magic: the magic token for the file format. +* @version: the version of the file format. +* @data_size: the number of entries in the profile data section. +* @padding_bytes_before_counters: the number of padding bytes before the +* counters. +* @counters_size: the size in bytes of the LLVM profile section containing the +* counters. +* @padding_bytes_after_counters: the number of padding bytes after the +* counters. +* @names_size: the size in bytes of the LLVM profile section containing the +* counters' names. +* @counters_delta: the beginning of the LLMV profile counters section. +* @names_delta: the beginning of the LLMV profile names section. +* @value_kind_last: the last profile value kind. +*/ +struct llvm_prf_header { +u64 magic; +u64 version; +u64 data_size; +u64 padding_bytes_before_counters; +u64 counters_size; +u64 padding_bytes_after_counters; +u64 names_size; +u64 counters_delta; +u64 names_delta; +u64 value_kind_last; +}; + +/** +* struct llvm_prf_data - represents the per-function control structure. +* @name_ref: the reference to the function's name. +* @func_hash: the hash value of the function. +* @counter_ptr: a pointer to the profile counter. +* @function_ptr: a pointer to the function. +* @values: the profiling values associated with this function. +* @num_counters: the number of counters in the function. +* @num_value_sites: the number of value profile sites. +*/ +struct llvm_prf_data { +const u64 name_ref; +const u64 func_hash; +const void *counter_ptr; +const void *function_ptr; +void *values; +const u32 num_counters; +const u16 num_value_sites[LLVM_PRF_IPVK_LAST +1 ]; +}__aligned(LLVM_PRF_DATA_ALIGN) ; + +/** +* structure llvm_prf_value_node_data - represents the data part of the struct +* llvm_prf_value_node data structure. +* @value: the value counters. +* @count: the counters' count. +*/ +struct llvm_prf_value_node_data { +u64 value; +u64 count; +}; + +/** +* struct llvm_prf_value_node - represents an internal data structure used by +* the value profiler. +* @value: the value counters. +* @count: the counters' count. +* @next: the next value node. +*/ +struct llvm_prf_value_node { +u64 value; +u64 count; +struct llvm_prf_value_node *next; +}; + +/** +* struct llvm_prf_value_data - represents the value profiling data in indexed +* format. +* @total_size: the total size in bytes including this field. +* @num_value_kinds: the number of value profile kinds that has value profile +* data. +*/ +struct llvm_prf_value_data { +u32 total_size; +u32 num_value_kinds; +}; + +/** +* struct llvm_prf_value_record - represents the on-disk layout of the value +* profile data of a particular kind for one function. +* @kind: the kind of the value profile record. +* @num_value_sites: the number of value profile sites. +* @site_count_array: the first element of the array that stores the number +* of profiled values for each value site. +*/ +struct llvm_prf_value_record { +u32 kind; +u32 num_value_sites; +u8 site_count_array[]; +}; + +#define prf_get_value_record_header_size () \ + offsetof(struct llvm_prf_value_record, site_count_array) +#define prf_get_value_record_site_count_size (sites) \ + roundup((sites), 8) +#define prf_get_value_record_size (sites) \ + (prf_get_value_record_header_size() + \ + prf_get_value_record_site_count_size((sites))) + +/* Data sections */ +extern struct llvm_prf_data __llvm_prf_data_start[]; +extern struct llvm_prf_data __llvm_prf_data_end[]; + +extern u64 __llvm_prf_cnts_start[]; +extern u64 __llvm_prf_cnts_end[]; + +extern char __llvm_prf_names_start[]; +extern char __llvm_prf_names_end[]; + +extern struct llvm_prf_value_node __llvm_prf_vnds_start[]; +extern struct llvm_prf_value_node __llvm_prf_vnds_end[]; + +/* Locking for vnodes */ +extern unsigned long prf_lock (void ); +extern void prf_unlock (unsigned long flags); + +#define __DEFINE_PRF_SIZE (s) \ + static inline unsigned long prf_ ## s ## _size(void) \ + { \ + unsigned long start = \ + (unsigned long)__llvm_prf_ ## s ## _start; \ + unsigned long end = \ + (unsigned long)__llvm_prf_ ## s ## _end; \ + return roundup(end - start, \ + sizeof(__llvm_prf_ ## s ## _start[0])); \ + } \ + static inline unsigned long prf_ ## s ## _count(void) \ + { \ + return prf_ ## s ## _size() / \ + sizeof(__llvm_prf_ ## s ## _start[0]); \ + } + +__DEFINE_PRF_SIZE (data); +__DEFINE_PRF_SIZE (cnts); +__DEFINE_PRF_SIZE (names); +__DEFINE_PRF_SIZE (vnds); + +#undef __DEFINE_PRF_SIZE + +#endif /* _PGO_H */ scripts/Makefile.lib +10
-1@@ -143,6 +143,16 @@ _c_flags += $(if $(patsubst n%,, \ $(CFLAGS_GCOV)) endif +# +# Enable clang's PGO profiling flags for a file or directory depending on +# variables PGO_PROFILE_obj.o and PGO_PROFILE. +# +ifeq ($(CONFIG_PGO_CLANG),y) +_c_flags += $(if $(patsubst n%,, \ + $(PGO_PROFILE_$(basetarget).o)$(PGO_PROFILE)y), \ + $(CFLAGS_PGO_CLANG)) +endif + # # Enable address sanitizer flags for kernel except some files or directories # we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE) --2.30.0.284.gd98b1dd5eaa7-goog
--
You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/20210112051428.4175583-1-morbo%40google.com.
PATCH v3 1/1 pgo: add clang's Profile Guided Optimization infrastructure
In reply to Bill WendlingFrom: Sami Tolvanen <[email protected]>
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 and isn't compatible with clang's gcov support in kernel/gcov.
[1] https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization
---
v2: - Added "__llvm_profile_instrument_memop" based on Nathan Chancellor's
testing.
- Corrected documentation, re PGO flags when using LTO, based on Fāng-ruì Sòng's comments.
v3: - Added change log section based on Sedat Dilek's comments.
--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/pgo.rst | 127 +++++++++ MAINTAINERS | 9 + Makefile | 3 + arch/Kconfig | 1 + arch/arm/boot/bootp/Makefile | 1 + arch/arm/boot/compressed/Makefile | 1 + arch/arm/vdso/Makefile | 3 +- arch/arm64/kernel/vdso/Makefile | 3 +- arch/arm64/kvm/hyp/nvhe/Makefile | 1 + arch/mips/boot/compressed/Makefile | 1 + arch/mips/vdso/Makefile | 1 + arch/nds32/kernel/vdso/Makefile | 4 +- arch/parisc/boot/compressed/Makefile | 1 + arch/powerpc/kernel/Makefile | 6 +- arch/powerpc/kernel/trace/Makefile | 3 +- arch/powerpc/kernel/vdso32/Makefile | 1 + arch/powerpc/kernel/vdso64/Makefile | 1 + arch/powerpc/kexec/Makefile | 3 +- arch/powerpc/xmon/Makefile | 1 + arch/riscv/kernel/vdso/Makefile | 3 +- arch/s390/boot/Makefile | 1 + arch/s390/boot/compressed/Makefile | 1 + arch/s390/kernel/Makefile | 1 + arch/s390/kernel/vdso64/Makefile | 3 +- arch/s390/purgatory/Makefile | 1 + arch/sh/boot/compressed/Makefile | 1 + arch/sh/mm/Makefile | 1 + arch/sparc/vdso/Makefile | 1 + arch/x86/Kconfig | 1 + arch/x86/boot/Makefile | 1 + arch/x86/boot/compressed/Makefile | 1 + arch/x86/entry/vdso/Makefile | 1 + arch/x86/kernel/vmlinux.lds.S | 2 + arch/x86/platform/efi/Makefile | 1 + arch/x86/purgatory/Makefile | 1 + arch/x86/realmode/rm/Makefile | 1 + arch/x86/um/vdso/Makefile | 1 + drivers/firmware/efi/libstub/Makefile | 1 + drivers/s390/char/Makefile | 1 + include/asm-generic/vmlinux.lds.h | 44 +++ kernel/Makefile | 1 + kernel/pgo/Kconfig | 34 +++ kernel/pgo/Makefile | 5 + kernel/pgo/fs.c | 382 ++++++++++++++++++++++++++ kernel/pgo/instrument.c | 188 +++++++++++++ kernel/pgo/pgo.h | 206 ++++++++++++++ scripts/Makefile.lib | 10 + 48 files changed, 1058 insertions(+), 9 deletions(-) create mode 100644 Documentation/dev-tools/pgo.rst create mode 100644 kernel/pgo/Kconfig create mode 100644 kernel/pgo/Makefile create mode 100644 kernel/pgo/fs.c create mode 100644 kernel/pgo/instrument.c create mode 100644 kernel/pgo/pgo.h
Documentation/dev-tools/index.rst +1 -0
@@ -26,6 +26,7 @@ whole; patches welcome! kgdb kselftest kunit/index + pgo .. only:: subproject and html
Documentation/dev-tools/pgo.rst new file +127 -0
127 lines not on this page · raw message
MAINTAINERS +9 -0
@@ -13954,6 +13954,15 @@ S: Maintained F: include/linux/personality.h F: include/uapi/linux/personality.h +PGO BASED KERNEL PROFILING +M: Sami Tolvanen <[email protected]> +M: Bill Wendling <[email protected]> +R: Nathan Chancellor <[email protected]> +R: Nick Desaulniers <[email protected]> +S: Supported +F: Documentation/dev-tools/pgo.rst +F: kernel/pgo + PHOENIX RC FLIGHT CONTROLLER ADAPTER M: Marcus Folkesson <[email protected]> L: [email protected]
Makefile +3 -0
@@ -659,6 +659,9 @@ endif # KBUILD_EXTMOD # Defaults to vmlinux, but the arch makefile usually adds further targets all: vmlinux +CFLAGS_PGO_CLANG := -fprofile-generate +export CFLAGS_PGO_CLANG + CFLAGS_GCOV := -fprofile-arcs -ftest-coverage \ $(call cc-option,-fno-tree-loop-im) \ $(call cc-disable-warning,maybe-uninitialized,)
arch/Kconfig +1 -0
@@ -1112,6 +1112,7 @@ config ARCH_SPLIT_ARG64 pairs of 32-bit arguments, select this option. source "kernel/gcov/Kconfig" +source "kernel/pgo/Kconfig" source "scripts/gcc-plugins/Kconfig"
arch/arm/boot/bootp/Makefile +1 -0
@@ -7,6 +7,7 @@ # GCOV_PROFILE := n +PGO_PROFILE := n LDFLAGS_bootp := --no-undefined -X \ --defsym initrd_phys=$(INITRD_PHYS) \
arch/arm/boot/compressed/Makefile +1 -0
@@ -24,6 +24,7 @@ OBJS += hyp-stub.o endif GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.
arch/arm/vdso/Makefile +2 -1
@@ -36,8 +36,9 @@ else CFLAGS_vgettimeofday.o = -O2 -include $(c-gettimeofday-y) endif -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in. KCOV_INSTRUMENT := n
arch/arm64/kernel/vdso/Makefile +2 -1
@@ -41,8 +41,9 @@ ifneq ($(c-gettimeofday-y),) CFLAGS_vgettimeofday.o += -include $(c-gettimeofday-y) endif -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n obj-y += vdso.o targets += vdso.lds
arch/arm64/kvm/hyp/nvhe/Makefile +1 -0
@@ -60,6 +60,7 @@ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS), $(KBUILD_CFLAG # compiler instrumentation that inserts callbacks or checks into the code may # cause crashes. Just disable it. GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n UBSAN_SANITIZE := n KCOV_INSTRUMENT := n
arch/mips/boot/compressed/Makefile +1 -0
@@ -37,6 +37,7 @@ KBUILD_AFLAGS := $(KBUILD_AFLAGS) -D__ASSEMBLY__ \ # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in. KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n # decompressor objects (linked with vmlinuz) vmlinuzobjs-y := $(obj)/head.o $(obj)/decompress.o $(obj)/string.o
arch/mips/vdso/Makefile +1 -0
@@ -66,6 +66,7 @@ ldflags-y := -Bsymbolic --no-undefined -soname=linux-vdso.so.1 \ CFLAGS_REMOVE_vdso.o = -pg GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KCOV_INSTRUMENT := n
arch/nds32/kernel/vdso/Makefile +2 -2
@@ -15,9 +15,9 @@ obj-vdso := $(addprefix $(obj)/, $(obj-vdso)) ccflags-y := -shared -fno-common -fno-builtin -nostdlib -fPIC -Wl,-shared -g \ -Wl,-soname=linux-vdso.so.1 -Wl,--hash-style=sysv -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n - +PGO_PROFILE := n obj-y += vdso.o targets += vdso.lds
arch/parisc/boot/compressed/Makefile +1 -0
@@ -7,6 +7,7 @@ KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n targets := vmlinux.lds vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2
arch/powerpc/kernel/Makefile +5 -1
@@ -153,17 +153,21 @@ endif obj-$(CONFIG_PPC_SECURE_BOOT) += secure_boot.o ima_arch.o secvar-ops.o obj-$(CONFIG_PPC_SECVAR_SYSFS) += secvar-sysfs.o -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive code GCOV_PROFILE_prom_init.o := n +PGO_PROFILE_prom_init.o := n KCOV_INSTRUMENT_prom_init.o := n UBSAN_SANITIZE_prom_init.o := n GCOV_PROFILE_kprobes.o := n +PGO_PROFILE_kprobes.o := n KCOV_INSTRUMENT_kprobes.o := n UBSAN_SANITIZE_kprobes.o := n GCOV_PROFILE_kprobes-ftrace.o := n +PGO_PROFILE_kprobes-ftrace.o := n KCOV_INSTRUMENT_kprobes-ftrace.o := n UBSAN_SANITIZE_kprobes-ftrace.o := n GCOV_PROFILE_syscall_64.o := n +PGO_PROFILE_syscall_64.o := n KCOV_INSTRUMENT_syscall_64.o := n UBSAN_SANITIZE_syscall_64.o := n UBSAN_SANITIZE_vdso.o := n
arch/powerpc/kernel/trace/Makefile +2 -1
@@ -23,7 +23,8 @@ obj-$(CONFIG_TRACING) += trace_clock.o obj-$(CONFIG_PPC64) += $(obj64-y) obj-$(CONFIG_PPC32) += $(obj32-y) -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive code GCOV_PROFILE_ftrace.o := n +PGO_PROFILE_ftrace.o := n KCOV_INSTRUMENT_ftrace.o := n UBSAN_SANITIZE_ftrace.o := n
arch/powerpc/kernel/vdso32/Makefile +1 -0
@@ -34,6 +34,7 @@ targets := $(obj-vdso32) vdso32.so.dbg obj-vdso32 := $(addprefix $(obj)/, $(obj-vdso32)) GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/powerpc/kernel/vdso64/Makefile +1 -0
@@ -21,6 +21,7 @@ targets := $(obj-vdso64) vdso64.so.dbg obj-vdso64 := $(addprefix $(obj)/, $(obj-vdso64)) GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/powerpc/kexec/Makefile +2 -1
@@ -16,7 +16,8 @@ endif endif -# Disable GCOV, KCOV & sanitizers in odd or sensitive code +# Disable GCOV, PGO, KCOV & sanitizers in odd or sensitive code GCOV_PROFILE_core_$(BITS).o := n +PGO_PROFILE_core_$(BITS).o := n KCOV_INSTRUMENT_core_$(BITS).o := n UBSAN_SANITIZE_core_$(BITS).o := n
arch/powerpc/xmon/Makefile +1 -0
@@ -2,6 +2,7 @@ # Makefile for xmon GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/riscv/kernel/vdso/Makefile +2 -1
@@ -32,8 +32,9 @@ CPPFLAGS_vdso.lds += -P -C -U$(ARCH) # Disable -pg to prevent insert call site CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os -# Disable gcov profiling for VDSO code +# Disable gcov and PGO profiling for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n KCOV_INSTRUMENT := n # Force dependency
arch/s390/boot/Makefile +1 -0
@@ -5,6 +5,7 @@ KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/s390/boot/compressed/Makefile +1 -0
@@ -7,6 +7,7 @@ KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/s390/kernel/Makefile +1 -0
@@ -14,6 +14,7 @@ CFLAGS_REMOVE_early.o = $(CC_FLAGS_FTRACE) endif GCOV_PROFILE_early.o := n +PGO_PROFILE_early.o := n KCOV_INSTRUMENT_early.o := n UBSAN_SANITIZE_early.o := n KASAN_SANITIZE_ipl.o := n
arch/s390/kernel/vdso64/Makefile +2 -1
@@ -35,8 +35,9 @@ obj-y += vdso64_wrapper.o targets += vdso64.lds CPPFLAGS_vdso64.lds += -P -C -U$(ARCH) -# Disable gcov profiling, ubsan and kasan for VDSO code +# Disable gcov and PGO profiling, ubsan and kasan for VDSO code GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/s390/purgatory/Makefile +1 -0
@@ -17,6 +17,7 @@ $(obj)/mem.o: $(srctree)/arch/s390/lib/mem.S FORCE KCOV_INSTRUMENT := n GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n KASAN_SANITIZE := n
arch/sh/boot/compressed/Makefile +1 -0
@@ -13,6 +13,7 @@ targets := vmlinux vmlinux.bin vmlinux.bin.gz \ OBJECTS = $(obj)/head_32.o $(obj)/misc.o $(obj)/cache.o GCOV_PROFILE := n +PGO_PROFILE := n # # IMAGE_OFFSET is the load offset of the compression loader
arch/sh/mm/Makefile +1 -0
@@ -43,3 +43,4 @@ obj-$(CONFIG_UNCACHED_MAPPING) += uncached.o obj-$(CONFIG_HAVE_SRAM_POOL) += sram.o GCOV_PROFILE_pmb.o := n +PGO_PROFILE_pmb.o := n
arch/sparc/vdso/Makefile +1 -0
@@ -115,6 +115,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 -Bsymbolic GCOV_PROFILE := n +PGO_PROFILE := n # # Install the unstripped copies of vdso*.so. If our toolchain supports
arch/x86/Kconfig +1 -0
@@ -96,6 +96,7 @@ config X86 select ARCH_SUPPORTS_DEBUG_PAGEALLOC select ARCH_SUPPORTS_NUMA_BALANCING if X86_64 select ARCH_SUPPORTS_KMAP_LOCAL_FORCE_MAP if NR_CPUS <= 4096 + select ARCH_SUPPORTS_PGO_CLANG if X86_64 select ARCH_USE_BUILTIN_BSWAP select ARCH_USE_QUEUED_RWLOCKS select ARCH_USE_QUEUED_SPINLOCKS
arch/x86/boot/Makefile +1 -0
@@ -71,6 +71,7 @@ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=) KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n $(obj)/bzImage: asflags-y := $(SVGA_MODE)
arch/x86/boot/compressed/Makefile +1 -0
@@ -54,6 +54,7 @@ CFLAGS_sev-es.o += -I$(objtree)/arch/x86/lib/ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE :=n KBUILD_LDFLAGS := -m elf_$(UTS_MACHINE)
arch/x86/entry/vdso/Makefile +1 -0
@@ -179,6 +179,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 \ $(call ld-option, --eh-frame-hdr) -Bsymbolic GCOV_PROFILE := n +PGO_PROFILE := n quiet_cmd_vdso_and_check = VDSO $@ cmd_vdso_and_check = $(cmd_vdso); $(cmd_vdso_check)
arch/x86/kernel/vmlinux.lds.S +2 -0
@@ -184,6 +184,8 @@ SECTIONS BUG_TABLE + PGO_CLANG_DATA + ORC_UNWIND_TABLE . = ALIGN(PAGE_SIZE);
arch/x86/platform/efi/Makefile +1 -0
@@ -2,6 +2,7 @@ OBJECT_FILES_NON_STANDARD_efi_thunk_$(BITS).o := y KASAN_SANITIZE := n GCOV_PROFILE := n +PGO_PROFILE := n obj-$(CONFIG_EFI) += quirks.o efi.o efi_$(BITS).o efi_stub_$(BITS).o obj-$(CONFIG_EFI_MIXED) += efi_thunk_$(BITS).o
arch/x86/purgatory/Makefile +1 -0
@@ -23,6 +23,7 @@ targets += purgatory.ro purgatory.chk # Sanitizer, etc. runtimes are unavailable and cannot be linked here. GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n UBSAN_SANITIZE := n KCSAN_SANITIZE := n
arch/x86/realmode/rm/Makefile +1 -0
@@ -76,4 +76,5 @@ KBUILD_CFLAGS := $(REALMODE_CFLAGS) -D_SETUP -D_WAKEUP \ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n
arch/x86/um/vdso/Makefile +1 -0
@@ -64,6 +64,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -fPIC -shared -Wl,--hash-style=sysv GCOV_PROFILE := n +PGO_PROFILE := n # # Install the unstripped copy of vdso*.so listed in $(vdso-install-y).
drivers/firmware/efi/libstub/Makefile +1 -0
@@ -40,6 +40,7 @@ KBUILD_CFLAGS := $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS)) GCOV_PROFILE := n +PGO_PROFILE := n # Sanitizer runtimes are unavailable and cannot be linked here. KASAN_SANITIZE := n KCSAN_SANITIZE := n
drivers/s390/char/Makefile +1 -0
@@ -9,6 +9,7 @@ CFLAGS_REMOVE_sclp_early_core.o = $(CC_FLAGS_FTRACE) endif GCOV_PROFILE_sclp_early_core.o := n +PGO_PROFILE_sclp_early_core.o := n KCOV_INSTRUMENT_sclp_early_core.o := n UBSAN_SANITIZE_sclp_early_core.o := n KASAN_SANITIZE_sclp_early_core.o := n
include/asm-generic/vmlinux.lds.h +44 -0
56 lines not on this page · raw message
kernel/Makefile +1 -0
@@ -111,6 +111,7 @@ obj-$(CONFIG_BPF) += bpf/ obj-$(CONFIG_KCSAN) += kcsan/ obj-$(CONFIG_SHADOW_CALL_STACK) += scs.o obj-$(CONFIG_HAVE_STATIC_CALL_INLINE) += static_call.o +obj-$(CONFIG_PGO_CLANG) += pgo/ obj-$(CONFIG_PERF_EVENTS) += events/
kernel/pgo/Kconfig new file +34 -0
@@ -0,0 +1,34 @@ +# SPDX-License-Identifier: GPL-2.0-only +menu "Profile Guided Optimization (PGO) (EXPERIMENTAL)" + +config ARCH_SUPPORTS_PGO_CLANG + bool + +config PGO_CLANG + bool "Enable clang's PGO-based kernel profiling" + depends on DEBUG_FS + depends on ARCH_SUPPORTS_PGO_CLANG + help + This option enables clang's PGO (Profile Guided Optimization) based + code profiling to better optimize the kernel. + + If unsure, say N. + + Run a representative workload for your application on a kernel + compiled with this option and download the raw profile file from + /sys/kernel/debug/pgo/profraw. This file needs to be processed with + llvm-profdata. It may be merged with other collected raw profiles. + + Copy the resulting profile file into vmlinux.profdata, and enable + KCFLAGS=-fprofile-use=vmlinux.profdata to produce an optimized + kernel. + + Note that a kernel compiled with profiling flags will be + significatnly larger and run slower. Also be sure to exclude files + from profiling which are not linked to the kernel image to prevent + linker errors. + + Note that the debugfs filesystem has to be mounted to access + profiling data. + +endmenu
kernel/pgo/Makefile new file +5 -0
@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +GCOV_PROFILE := n +PGO_PROFILE := n + +obj-y += fs.o instrument.o
kernel/pgo/fs.c new file +382 -0
382 lines not on this page · raw message
kernel/pgo/instrument.c new file +188 -0
188 lines not on this page · raw message
kernel/pgo/pgo.h new file +206 -0
206 lines not on this page · raw message
scripts/Makefile.lib +10 -0
@@ -143,6 +143,16 @@ _c_flags += $(if $(patsubst n%,, \ $(CFLAGS_GCOV)) endif +# +# Enable clang's PGO profiling flags for a file or directory depending on +# variables PGO_PROFILE_obj.o and PGO_PROFILE. +# +ifeq ($(CONFIG_PGO_CLANG),y) +_c_flags += $(if $(patsubst n%,, \ + $(PGO_PROFILE_$(basetarget).o)$(PGO_PROFILE)y), \ + $(CFLAGS_PGO_CLANG)) +endif + # # Enable address sanitizer flags for kernel except some files or directories # we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE)
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
kernel test robot
build botI love your patch! Perhaps something to improve:
Hi Bill,
I love your patch! Perhaps something to improve:
[auto build test WARNING on linus/master]
[also build test WARNING on v5.11-rc3]
[cannot apply to powerpc/next s390/features tip/x86/core next-20210111] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Bill-Wendling/pgo-add-clang-s-Profile-Guided-Optimization-infrastructure/20210112-133315 base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git a0d54b4f5b219fb31f0776e9f53aa137e78ae431 config: x86_64-allyesconfig (attached as .config) compiler: gcc-9 (Debian 9.3.0-15) 9.3.0 reproduce (this is a W=1 build): # https://github.com/0day-ci/linux/commit/6ab85bae7667afd0aa68c6442b7ca5c369fa1088 git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags linux-review Bill-Wendling/pgo-add-clang-s-Profile-Guided-Optimization-infrastructure/20210112-133315 git checkout 6ab85bae7667afd0aa68c6442b7ca5c369fa1088 # save the attached .config to linux build tree make W=1 ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
kernel/pgo/instrument.c:72:6: warning: no previous prototype for '__llvm_profile_instrument_target' [-Wmissing-prototypes]
72 | void __llvm_profile_instrument_target(u64 target_value, void *data, u32 index)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
kernel/pgo/instrument.c:135:6: warning: no previous prototype for '__llvm_profile_instrument_range' [-Wmissing-prototypes]
135 | void __llvm_profile_instrument_range(u64 target_value, void *data,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> kernel/pgo/instrument.c:179:6: warning: no previous prototype for '__llvm_profile_instrument_memop' [-Wmissing-prototypes]
179 | void __llvm_profile_instrument_memop(u64 target_value, void *data,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
vim +/__llvm_profile_instrument_memop +179 kernel/pgo/instrument.c
174
175 /*
176 * The target values are partitioned into multiple ranges. The range spec is
177 * defined in compiler-rt/include/profile/InstrProfData.inc.
178 */
> 179 void __llvm_profile_instrument_memop(u64 target_value, void *data,
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Quoted 11 lines · kernel test robot
Hi Bill,
I love your patch! Perhaps something to improve:
[auto build test WARNING on linus/master]
[also build test WARNING on v5.11-rc3]
[cannot apply to powerpc/next s390/features tip/x86/core next-20210111] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
Quoted 4 lines
url: https://github.com/0day-ci/linux/commits/Bill-Wendling/pgo-add-clang-s-Profile-Guided-Optimization-infrastructure/20210112-133315 base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git a0d54b4f5b219fb31f0776e9f53aa137e78ae431 config: x86_64-allyesconfig (attached as .config) compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
Hmmm... This should probably be gated on CC_IS_CLANG? Or even better CLANG_VERSION >= 120000 due to
https://github.com/ClangBuiltLinux/linux/issues/1252?
Quoted 19 lines
reproduce (this is a W=1 build):
# https://github.com/0day-ci/linux/commit/6ab85bae7667afd0aa68c6442b7ca5c369fa1088 git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags linux-review Bill-Wendling/pgo-add-clang-s-Profile-Guided-Optimization-infrastructure/20210112-133315 git checkout 6ab85bae7667afd0aa68c6442b7ca5c369fa1088 # save the attached .config to linux build tree make W=1 ARCH=x86_64If you fix the issue, kindly add following tag as appropriate
Reported-by kernel test robot <[email protected]>All warnings (new ones prefixed by >>):
kernel/pgo/instrument.c:72:6: warning: no previous prototype for '__llvm_profile_instrument_target' [-Wmissing-prototypes]
72 | void__llvm_profile_instrument_target(u64 target_value, void *data, u32 index)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
kernel/pgo/instrument.c:135:6: warning: no previous prototype for '__llvm_profile_instrument_range' [-Wmissing-prototypes]
135 | void __llvm_profile_instrument_range(u64target_value, void *data,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 4 lines
Quoted 1 line
Quoted 1 line
kernel/pgo/instrument.c:179:6: warning: no previous prototype for '__llvm_profile_instrument_memop' [-Wmissing-prototypes]179 | void __llvm_profile_instrument_memop(u64
target_value, void *data,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I still think that this warning will show up with clang at W=1. Given that these are compiler inserted functions, the prototypes don't matter but we could shut it up by just putting the prototypes right above the functions like was done in commit 1e1b6d63d634 ("lib/string.c: implement stpcpy").
Cheers,
Nathan
PATCH v4 1/1 pgo: add clang's Profile Guided Optimization infrastructure
In reply to Bill WendlingFrom: Sami Tolvanen <[email protected]>
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
---
v2: - Added "__llvm_profile_instrument_memop" based on Nathan Chancellor's
testing.
- Corrected documentation, re PGO flags when using LTO, based on Fangrui Song's comments.
v3: - Added change log section based on Sedat Dilek's comments.
v4: - Remove non-x86 Makfile changes and se "hweight64" instead of using our
own popcount implementation, based on Nick Desaulniers's comment.
--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/pgo.rst | 127 +++++++++ MAINTAINERS | 9 + Makefile | 3 + arch/Kconfig | 1 + arch/x86/Kconfig | 1 + arch/x86/boot/Makefile | 1 + arch/x86/boot/compressed/Makefile | 1 + arch/x86/entry/vdso/Makefile | 1 + arch/x86/kernel/vmlinux.lds.S | 2 + arch/x86/platform/efi/Makefile | 1 + arch/x86/purgatory/Makefile | 1 + arch/x86/realmode/rm/Makefile | 1 + arch/x86/um/vdso/Makefile | 1 + drivers/firmware/efi/libstub/Makefile | 1 + include/asm-generic/vmlinux.lds.h | 44 +++ kernel/Makefile | 1 + kernel/pgo/Kconfig | 34 +++ kernel/pgo/Makefile | 5 + kernel/pgo/fs.c | 382 ++++++++++++++++++++++++++ kernel/pgo/instrument.c | 185 +++++++++++++ kernel/pgo/pgo.h | 206 ++++++++++++++ scripts/Makefile.lib | 10 + 23 files changed, 1019 insertions(+) create mode 100644 Documentation/dev-tools/pgo.rst create mode 100644 kernel/pgo/Kconfig create mode 100644 kernel/pgo/Makefile create mode 100644 kernel/pgo/fs.c create mode 100644 kernel/pgo/instrument.c create mode 100644 kernel/pgo/pgo.h
Documentation/dev-tools/index.rst +1 -0
@@ -26,6 +26,7 @@ whole; patches welcome! kgdb kselftest kunit/index + pgo .. only:: subproject and html
Documentation/dev-tools/pgo.rst new file +127 -0
127 lines not on this page · raw message
MAINTAINERS +9 -0
@@ -13954,6 +13954,15 @@ S: Maintained F: include/linux/personality.h F: include/uapi/linux/personality.h +PGO BASED KERNEL PROFILING +M: Sami Tolvanen <[email protected]> +M: Bill Wendling <[email protected]> +R: Nathan Chancellor <[email protected]> +R: Nick Desaulniers <[email protected]> +S: Supported +F: Documentation/dev-tools/pgo.rst +F: kernel/pgo + PHOENIX RC FLIGHT CONTROLLER ADAPTER M: Marcus Folkesson <[email protected]> L: [email protected]
Makefile +3 -0
@@ -659,6 +659,9 @@ endif # KBUILD_EXTMOD # Defaults to vmlinux, but the arch makefile usually adds further targets all: vmlinux +CFLAGS_PGO_CLANG := -fprofile-generate +export CFLAGS_PGO_CLANG + CFLAGS_GCOV := -fprofile-arcs -ftest-coverage \ $(call cc-option,-fno-tree-loop-im) \ $(call cc-disable-warning,maybe-uninitialized,)
arch/Kconfig +1 -0
@@ -1112,6 +1112,7 @@ config ARCH_SPLIT_ARG64 pairs of 32-bit arguments, select this option. source "kernel/gcov/Kconfig" +source "kernel/pgo/Kconfig" source "scripts/gcc-plugins/Kconfig"
arch/x86/Kconfig +1 -0
@@ -96,6 +96,7 @@ config X86 select ARCH_SUPPORTS_DEBUG_PAGEALLOC select ARCH_SUPPORTS_NUMA_BALANCING if X86_64 select ARCH_SUPPORTS_KMAP_LOCAL_FORCE_MAP if NR_CPUS <= 4096 + select ARCH_SUPPORTS_PGO_CLANG if X86_64 select ARCH_USE_BUILTIN_BSWAP select ARCH_USE_QUEUED_RWLOCKS select ARCH_USE_QUEUED_SPINLOCKS
arch/x86/boot/Makefile +1 -0
@@ -71,6 +71,7 @@ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=) KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n $(obj)/bzImage: asflags-y := $(SVGA_MODE)
arch/x86/boot/compressed/Makefile +1 -0
@@ -54,6 +54,7 @@ CFLAGS_sev-es.o += -I$(objtree)/arch/x86/lib/ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE :=n KBUILD_LDFLAGS := -m elf_$(UTS_MACHINE)
arch/x86/entry/vdso/Makefile +1 -0
@@ -179,6 +179,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 \ $(call ld-option, --eh-frame-hdr) -Bsymbolic GCOV_PROFILE := n +PGO_PROFILE := n quiet_cmd_vdso_and_check = VDSO $@ cmd_vdso_and_check = $(cmd_vdso); $(cmd_vdso_check)
arch/x86/kernel/vmlinux.lds.S +2 -0
@@ -184,6 +184,8 @@ SECTIONS BUG_TABLE + PGO_CLANG_DATA + ORC_UNWIND_TABLE . = ALIGN(PAGE_SIZE);
arch/x86/platform/efi/Makefile +1 -0
@@ -2,6 +2,7 @@ OBJECT_FILES_NON_STANDARD_efi_thunk_$(BITS).o := y KASAN_SANITIZE := n GCOV_PROFILE := n +PGO_PROFILE := n obj-$(CONFIG_EFI) += quirks.o efi.o efi_$(BITS).o efi_stub_$(BITS).o obj-$(CONFIG_EFI_MIXED) += efi_thunk_$(BITS).o
arch/x86/purgatory/Makefile +1 -0
@@ -23,6 +23,7 @@ targets += purgatory.ro purgatory.chk # Sanitizer, etc. runtimes are unavailable and cannot be linked here. GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n UBSAN_SANITIZE := n KCSAN_SANITIZE := n
arch/x86/realmode/rm/Makefile +1 -0
@@ -76,4 +76,5 @@ KBUILD_CFLAGS := $(REALMODE_CFLAGS) -D_SETUP -D_WAKEUP \ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n
arch/x86/um/vdso/Makefile +1 -0
@@ -64,6 +64,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -fPIC -shared -Wl,--hash-style=sysv GCOV_PROFILE := n +PGO_PROFILE := n # # Install the unstripped copy of vdso*.so listed in $(vdso-install-y).
drivers/firmware/efi/libstub/Makefile +1 -0
@@ -40,6 +40,7 @@ KBUILD_CFLAGS := $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS)) GCOV_PROFILE := n +PGO_PROFILE := n # Sanitizer runtimes are unavailable and cannot be linked here. KASAN_SANITIZE := n KCSAN_SANITIZE := n
include/asm-generic/vmlinux.lds.h +44 -0
56 lines not on this page · raw message
kernel/Makefile +1 -0
@@ -111,6 +111,7 @@ obj-$(CONFIG_BPF) += bpf/ obj-$(CONFIG_KCSAN) += kcsan/ obj-$(CONFIG_SHADOW_CALL_STACK) += scs.o obj-$(CONFIG_HAVE_STATIC_CALL_INLINE) += static_call.o +obj-$(CONFIG_PGO_CLANG) += pgo/ obj-$(CONFIG_PERF_EVENTS) += events/
kernel/pgo/Kconfig new file +34 -0
@@ -0,0 +1,34 @@ +# SPDX-License-Identifier: GPL-2.0-only +menu "Profile Guided Optimization (PGO) (EXPERIMENTAL)" + +config ARCH_SUPPORTS_PGO_CLANG + bool + +config PGO_CLANG + bool "Enable clang's PGO-based kernel profiling" + depends on DEBUG_FS + depends on ARCH_SUPPORTS_PGO_CLANG + help + This option enables clang's PGO (Profile Guided Optimization) based + code profiling to better optimize the kernel. + + If unsure, say N. + + Run a representative workload for your application on a kernel + compiled with this option and download the raw profile file from + /sys/kernel/debug/pgo/profraw. This file needs to be processed with + llvm-profdata. It may be merged with other collected raw profiles. + + Copy the resulting profile file into vmlinux.profdata, and enable + KCFLAGS=-fprofile-use=vmlinux.profdata to produce an optimized + kernel. + + Note that a kernel compiled with profiling flags will be + significatnly larger and run slower. Also be sure to exclude files + from profiling which are not linked to the kernel image to prevent + linker errors. + + Note that the debugfs filesystem has to be mounted to access + profiling data. + +endmenu
kernel/pgo/Makefile new file +5 -0
@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +GCOV_PROFILE := n +PGO_PROFILE := n + +obj-y += fs.o instrument.o
kernel/pgo/fs.c new file +382 -0
382 lines not on this page · raw message
kernel/pgo/instrument.c new file +185 -0
185 lines not on this page · raw message
kernel/pgo/pgo.h new file +206 -0
206 lines not on this page · raw message
scripts/Makefile.lib +10 -0
@@ -143,6 +143,16 @@ _c_flags += $(if $(patsubst n%,, \ $(CFLAGS_GCOV)) endif +# +# Enable clang's PGO profiling flags for a file or directory depending on +# variables PGO_PROFILE_obj.o and PGO_PROFILE. +# +ifeq ($(CONFIG_PGO_CLANG),y) +_c_flags += $(if $(patsubst n%,, \ + $(PGO_PROFILE_$(basetarget).o)$(PGO_PROFILE)y), \ + $(CFLAGS_PGO_CLANG)) +endif + # # Enable address sanitizer flags for kernel except some files or directories # we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE)
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
Hi Bill,
Quoted 26 lines · Bill Wendling
From: Sami Tolvanen <[email protected]>
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.profrawMultiple 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.
Quoted 6 lines
[1] https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization
Signed-off-by Sami Tolvanen <[email protected]>Co-developed-by Bill Wendling <[email protected]>Signed-off-by Bill Wendling <[email protected]>Change-Id Ic78e69c682286d3a44c4549a0138578c98138b77
Small nit: This should be removed.
I applied this patch on top of v5.11-rc3, built it with LLVM 12 (f1d5cbbdee5526bc86eac0a5652b115d9bc158e5 + D94470) with Microsoft's WSL 5.4 config [1] + CONFIG_PGO_CLANG=y, and ran it on WSL2.
$ zgrep PGO /proc/config.gz # Profile Guided Optimization (PGO) (EXPERIMENTAL) CONFIG_ARCH_SUPPORTS_PGO_CLANG=y CONFIG_PGO_CLANG=y # end of Profile Guided Optimization (PGO) (EXPERIMENTAL)
However, I see an issue with actually using the data:
$ sudo -s # mount -t debugfs none /sys/kernel/debug # cp -a /sys/kernel/debug/pgo/profraw vmlinux.profraw # chown nathan:nathan vmlinux.profraw # exit $ tc-build/build/llvm/stage1/bin/llvm-profdata merge --output=vmlinux.profdata vmlinux.profraw warning: vmlinux.profraw: Invalid instrumentation profile data (bad magic) error: No profiles could be merged.
Am I holding it wrong? :) Note, this is virtualized, I do not have any "real" x86 hardware that I can afford to test on right now.
[1]: https://github.com/microsoft/WSL2-Linux-Kernel/raw/linux-msft-wsl-5.4.y/Microsoft/config-wsl
Cheers,
Nathan
Quoted 30 lines · Nathan Chancellor
Hi Bill,
Quoted 26 lines · Bill Wendling
From: Sami Tolvanen <[email protected]>
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.profrawMultiple 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.
Quoted 9 lines
Quoted 6 lines
[1] https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization
Signed-off-by Sami Tolvanen <[email protected]>Co-developed-by Bill Wendling <[email protected]>Signed-off-by Bill Wendling <[email protected]>Change-Id Ic78e69c682286d3a44c4549a0138578c98138b77Small nit: This should be removed.
Grrr....The git hook keeps adding it in there. :-(
Quoted 21 lines
I applied this patch on top of v5.11-rc3, built it with LLVM 12 (
f1d5cbbdee5526bc86eac0a5652b115d9bc158e5+ D94470) with Microsoft's WSL 5.4 config [1] +CONFIG_PGO_CLANG=y, and ran it on WSL2.$ zgrep PGO /proc/config.gz # Profile Guided Optimization (PGO) (EXPERIMENTAL) CONFIG_ARCH_SUPPORTS_PGO_CLANG=y CONFIG_PGO_CLANG=y # end of Profile Guided Optimization (PGO) (EXPERIMENTAL)However, I see an issue with actually using the data:
$ sudo -s # mount -t debugfs none /sys/kernel/debug # cp -a /sys/kernel/debug/pgo/profraw vmlinux.profraw # chown nathan:nathan vmlinux.profraw # exit $ tc-build/build/llvm/stage1/bin/llvm-profdata merge --output=vmlinux.profdata vmlinux.profraw warning: vmlinux.profraw: Invalid instrumentation profile data (bad magic) error: No profiles could be merged.
Quoted 5 lines
Am I holding it wrong? :) Note, this is virtualized, I do not have any "real" x86 hardware that I can afford to test on right now.
[1]: https://github.com/microsoft/WSL2-Linux-Kernel/raw/linux-msft-wsl-5.4.y/Microsoft/config-wsl
Could you send me the vmlinux.profraw file? (Don't CC this list.)
-bw
Quoted 12 lines · Nathan Chancellor
However, I see an issue with actually using the data:
$ sudo -s # mount -t debugfs none /sys/kernel/debug # cp -a /sys/kernel/debug/pgo/profraw vmlinux.profraw # chown nathan:nathan vmlinux.profraw # exit $ tc-build/build/llvm/stage1/bin/llvm-profdata merge --output=vmlinux.profdata vmlinux.profraw warning: vmlinux.profraw: Invalid instrumentation profile data (bad magic) error: No profiles could be merged.
Quoted 2 lines
Am I holding it wrong? :) Note, this is virtualized, I do not have any "real" x86 hardware that I can afford to test on right now.
Same.
I think the magic calculation in this patch may differ from upstream
llvm: https://github.com/llvm/llvm-project/blob/49142991a685bd427d7e877c29c77371dfb7634c/llvm/include/llvm/ProfileData/SampleProf.h#L96-L101
vs this patch:
+ * Note: These internal LLVM definitions must match the compiler version.
+ * See llvm/include/llvm/ProfileData/InstrProfData.inc in LLVM's source code.
+ */
+
+#ifdef CONFIG_64BIT
+ #define LLVM_PRF_MAGIC \
+ ((u64)255 << 56 | \
+ (u64)'l' << 48 | \ + (u64)'p' << 40 | \ + (u64)'r' << 32 | \ + (u64)'o' << 24 | \ + (u64)'f' << 16 | \ + (u64)'r' << 8 | \
+ (u64)129)
+#else
+ #define LLVM_PRF_MAGIC \
+ ((u64)255 << 56 | \
+ (u64)'l' << 48 | \ + (u64)'p' << 40 | \ + (u64)'r' << 32 | \ + (u64)'o' << 24 | \ + (u64)'f' << 16 | \ + (u64)'R' << 8 | \
+ (u64)129)
+#endif
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 15 lines · Nick Desaulniers
Quoted 12 lines · Nathan Chancellor
However, I see an issue with actually using the data:
$ sudo -s # mount -t debugfs none /sys/kernel/debug # cp -a /sys/kernel/debug/pgo/profraw vmlinux.profraw # chown nathan:nathan vmlinux.profraw # exit $ tc-build/build/llvm/stage1/bin/llvm-profdata merge --output=vmlinux.profdata vmlinux.profraw warning: vmlinux.profraw: Invalid instrumentation profile data (bad magic) error: No profiles could be merged.
Quoted 7 lines
Quoted 2 lines
Am I holding it wrong? :) Note, this is virtualized, I do not have any "real" x86 hardware that I can afford to test on right now.
Same.
I think the magic calculation in this patch may differ from upstream
llvm: https://github.com/llvm/llvm-project/blob/49142991a685bd427d7e877c29c77371dfb7634c/llvm/include/llvm/ProfileData/SampleProf.h#L96-L101
Err...it looks like it was the padding calculation. With that fixed up, we can query the profile data to get insights on the most heavily called functions. Here's what my top 20 are (reset, then watch 10 minutes worth of cat videos on youtube while running find / and sleeping at my desk). Anything curious stand out to anyone?
$ llvm-profdata show -topn=20 /tmp/vmlinux.profraw Instrumentation level: IR entry_first = 0 Total functions: 48970 Maximum function count: 62070879 Maximum internal block count: 83221158 Top 20 functions with the largest internal block counts: drivers/tty/n_tty.c:n_tty_write, max count = 83221158 rcu_read_unlock_strict, max count = 62070879 _cond_resched, max count = 25486882 rcu_all_qs, max count = 25451477 drivers/cpuidle/poll_state.c:poll_idle, max count = 23618576 _raw_spin_unlock_irqrestore, max count = 18874121 drivers/cpuidle/governors/menu.c:menu_select, max count = 18721624 _raw_spin_lock_irqsave, max count = 18509161 memchr, max count = 15525452 _raw_spin_lock, max count = 15484254 __mod_memcg_state, max count = 14604619 __mod_memcg_lruvec_state, max count = 14602783 fs/ext4/hash.c:str2hashbuf_signed, max count = 14098424 __mod_lruvec_state, max count = 12527154 __mod_node_page_state, max count = 12525172 native_sched_clock, max count = 8904692 sched_clock_cpu, max count = 8895832 sched_clock, max count = 8894627 kernel/entry/common.c:exit_to_user_mode_prepare, max count = 8289031 fpregs_assert_state_consistent, max count = 8287198
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 22 lines
Quoted 20 lines · Nick Desaulniers
Quoted 12 lines · Nathan Chancellor
However, I see an issue with actually using the data:
$ sudo -s # mount -t debugfs none /sys/kernel/debug # cp -a /sys/kernel/debug/pgo/profraw vmlinux.profraw # chown nathan:nathan vmlinux.profraw # exit $ tc-build/build/llvm/stage1/bin/llvm-profdata merge --output=vmlinux.profdata vmlinux.profraw warning: vmlinux.profraw: Invalid instrumentation profile data (bad magic) error: No profiles could be merged.Quoted 2 lines
Am I holding it wrong? :) Note, this is virtualized, I do not have any "real" x86 hardware that I can afford to test on right now.
Same.
Quoted 8 lines
Quoted 2 lines
I think the magic calculation in this patch may differ from upstream
llvm: https://github.com/llvm/llvm-project/blob/49142991a685bd427d7e877c29c77371dfb7634c/llvm/include/llvm/ProfileData/SampleProf.h#L96-L101Err...it looks like it was the padding calculation. With that fixed up, we can query the profile data to get insights on the most heavily called functions. Here's what my top 20 are (reset, then watch 10 minutes worth of cat videos on youtube while running
find /and sleeping at my desk). Anything curious stand out to anyone?
Hello world from my personal laptop whose kernel was rebuilt with profiling data! Wow, I can run find / and watch cat videos on youtube so fast! Users will love this! /s
Checking the sections sizes of .text.hot. and .text.unlikely. looks good!
Quoted 32 lines
$ llvm-profdata show -topn=20 /tmp/vmlinux.profraw Instrumentation level: IR entry_first = 0 Total functions: 48970 Maximum function count: 62070879 Maximum internal block count: 83221158 Top 20 functions with the largest internal block counts: drivers/tty/n_tty.c:n_tty_write, max count = 83221158 rcu_read_unlock_strict, max count = 62070879 _cond_resched, max count = 25486882 rcu_all_qs, max count = 25451477 drivers/cpuidle/poll_state.c:poll_idle, max count = 23618576 _raw_spin_unlock_irqrestore, max count = 18874121 drivers/cpuidle/governors/menu.c:menu_select, max count = 18721624 _raw_spin_lock_irqsave, max count = 18509161 memchr, max count = 15525452 _raw_spin_lock, max count = 15484254 __mod_memcg_state, max count = 14604619 __mod_memcg_lruvec_state, max count = 14602783 fs/ext4/hash.c:str2hashbuf_signed, max count = 14098424 __mod_lruvec_state, max count = 12527154 __mod_node_page_state, max count = 12525172 native_sched_clock, max count = 8904692 sched_clock_cpu, max count = 8895832 sched_clock, max count = 8894627 kernel/entry/common.c:exit_to_user_mode_prepare, max count = 8289031 fpregs_assert_state_consistent, max count = 8287198Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 32 lines · Nick Desaulniers
Quoted 23 lines
Quoted 20 lines · Nick Desaulniers
Quoted 12 lines · Nathan Chancellor
However, I see an issue with actually using the data:
$ sudo -s # mount -t debugfs none /sys/kernel/debug # cp -a /sys/kernel/debug/pgo/profraw vmlinux.profraw # chown nathan:nathan vmlinux.profraw # exit $ tc-build/build/llvm/stage1/bin/llvm-profdata merge --output=vmlinux.profdata vmlinux.profraw warning: vmlinux.profraw: Invalid instrumentation profile data (bad magic) error: No profiles could be merged.Quoted 2 lines
Am I holding it wrong? :) Note, this is virtualized, I do not have any "real" x86 hardware that I can afford to test on right now.
Same.
Quoted 8 lines
Quoted 2 lines
I think the magic calculation in this patch may differ from upstream
llvm: https://github.com/llvm/llvm-project/blob/49142991a685bd427d7e877c29c77371dfb7634c/llvm/include/llvm/ProfileData/SampleProf.h#L96-L101Err...it looks like it was the padding calculation. With that fixed up, we can query the profile data to get insights on the most heavily called functions. Here's what my top 20 are (reset, then watch 10 minutes worth of cat videos on youtube while running
find /and sleeping at my desk). Anything curious stand out to anyone?
Quoted 7 lines
Hello world from my personal laptop whose kernel was rebuilt with profiling data! Wow, I can run
find /and watch cat videos on youtube so fast! Users will love this! /sChecking the sections sizes of .text.hot. and .text.unlikely. looks good!
I love cat videos on youtube and do find parallelly...
I must try this :-)!
Might be good to write up an instruction (README) for followers?
- Sedat -
Quoted 37 lines
Quoted 28 lines
$ llvm-profdata show -topn=20 /tmp/vmlinux.profraw Instrumentation level: IR entry_first = 0 Total functions: 48970 Maximum function count: 62070879 Maximum internal block count: 83221158 Top 20 functions with the largest internal block counts: drivers/tty/n_tty.c:n_tty_write, max count = 83221158 rcu_read_unlock_strict, max count = 62070879 _cond_resched, max count = 25486882 rcu_all_qs, max count = 25451477 drivers/cpuidle/poll_state.c:poll_idle, max count = 23618576 _raw_spin_unlock_irqrestore, max count = 18874121 drivers/cpuidle/governors/menu.c:menu_select, max count = 18721624 _raw_spin_lock_irqsave, max count = 18509161 memchr, max count = 15525452 _raw_spin_lock, max count = 15484254 __mod_memcg_state, max count = 14604619 __mod_memcg_lruvec_state, max count = 14602783 fs/ext4/hash.c:str2hashbuf_signed, max count = 14098424 __mod_lruvec_state, max count = 12527154 __mod_node_page_state, max count = 12525172 native_sched_clock, max count = 8904692 sched_clock_cpu, max count = 8895832 sched_clock, max count = 8894627 kernel/entry/common.c:exit_to_user_mode_prepare, max count = 8289031 fpregs_assert_state_consistent, max count = 8287198Quoted 4 lines
--
Thanks,
~Nick Desaulniers--
You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/20210116001324.2865-1-nick.desaulniers%40gmail.com.
Quoted 32 lines · Nick Desaulniers
Quoted 23 lines
Quoted 20 lines · Nick Desaulniers
Quoted 12 lines · Nathan Chancellor
However, I see an issue with actually using the data:
$ sudo -s # mount -t debugfs none /sys/kernel/debug # cp -a /sys/kernel/debug/pgo/profraw vmlinux.profraw # chown nathan:nathan vmlinux.profraw # exit $ tc-build/build/llvm/stage1/bin/llvm-profdata merge --output=vmlinux.profdata vmlinux.profraw warning: vmlinux.profraw: Invalid instrumentation profile data (bad magic) error: No profiles could be merged.Quoted 2 lines
Am I holding it wrong? :) Note, this is virtualized, I do not have any "real" x86 hardware that I can afford to test on right now.
Same.
Quoted 8 lines
Quoted 2 lines
I think the magic calculation in this patch may differ from upstream
llvm: https://github.com/llvm/llvm-project/blob/49142991a685bd427d7e877c29c77371dfb7634c/llvm/include/llvm/ProfileData/SampleProf.h#L96-L101Err...it looks like it was the padding calculation. With that fixed up, we can query the profile data to get insights on the most heavily called functions. Here's what my top 20 are (reset, then watch 10 minutes worth of cat videos on youtube while running
find /and sleeping at my desk). Anything curious stand out to anyone?
Quoted 7 lines
Hello world from my personal laptop whose kernel was rebuilt with profiling data! Wow, I can run
find /and watch cat videos on youtube so fast! Users will love this! /sChecking the sections sizes of .text.hot. and .text.unlikely. looks good!
Is that the latest status of Bill's patch?
Or do you have me a lore link?
- Sedat -
[1] https://github.com/gwelymernans/linux/commits/gwelymernans/linux
Quoted 37 lines
Quoted 28 lines
$ llvm-profdata show -topn=20 /tmp/vmlinux.profraw Instrumentation level: IR entry_first = 0 Total functions: 48970 Maximum function count: 62070879 Maximum internal block count: 83221158 Top 20 functions with the largest internal block counts: drivers/tty/n_tty.c:n_tty_write, max count = 83221158 rcu_read_unlock_strict, max count = 62070879 _cond_resched, max count = 25486882 rcu_all_qs, max count = 25451477 drivers/cpuidle/poll_state.c:poll_idle, max count = 23618576 _raw_spin_unlock_irqrestore, max count = 18874121 drivers/cpuidle/governors/menu.c:menu_select, max count = 18721624 _raw_spin_lock_irqsave, max count = 18509161 memchr, max count = 15525452 _raw_spin_lock, max count = 15484254 __mod_memcg_state, max count = 14604619 __mod_memcg_lruvec_state, max count = 14602783 fs/ext4/hash.c:str2hashbuf_signed, max count = 14098424 __mod_lruvec_state, max count = 12527154 __mod_node_page_state, max count = 12525172 native_sched_clock, max count = 8904692 sched_clock_cpu, max count = 8895832 sched_clock, max count = 8894627 kernel/entry/common.c:exit_to_user_mode_prepare, max count = 8289031 fpregs_assert_state_consistent, max count = 8287198Quoted 4 lines
--
Thanks,
~Nick Desaulniers--
You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/20210116001324.2865-1-nick.desaulniers%40gmail.com.
Quoted 39 lines · Sedat Dilek
Quoted 36 lines · Nick Desaulniers
Quoted 23 lines
Quoted 20 lines · Nick Desaulniers
Quoted 12 lines · Nathan Chancellor
However, I see an issue with actually using the data:
$ sudo -s # mount -t debugfs none /sys/kernel/debug # cp -a /sys/kernel/debug/pgo/profraw vmlinux.profraw # chown nathan:nathan vmlinux.profraw # exit $ tc-build/build/llvm/stage1/bin/llvm-profdata merge --output=vmlinux.profdata vmlinux.profraw warning: vmlinux.profraw: Invalid instrumentation profile data (bad magic) error: No profiles could be merged.Quoted 2 lines
Am I holding it wrong? :) Note, this is virtualized, I do not have any "real" x86 hardware that I can afford to test on right now.
Same.
Quoted 8 lines
Quoted 2 lines
I think the magic calculation in this patch may differ from upstream
llvm: https://github.com/llvm/llvm-project/blob/49142991a685bd427d7e877c29c77371dfb7634c/llvm/include/llvm/ProfileData/SampleProf.h#L96-L101Err...it looks like it was the padding calculation. With that fixed up, we can query the profile data to get insights on the most heavily called functions. Here's what my top 20 are (reset, then watch 10 minutes worth of cat videos on youtube while running
find /and sleeping at my desk). Anything curious stand out to anyone?Hello world from my personal laptop whose kernel was rebuilt with profiling data! Wow, I can run
find /and watch cat videos on youtube so fast! Users will love this! /s
Quoted 8 lines
Quoted 3 lines
Checking the sections sizes of .text.hot. and .text.unlikely. looks good!
Is that the latest status of Bill's patch?
Or do you have me a lore link?
I tried with the message-id of Bill's initial email:
link="https://lore.kernel.org/r/[email protected]" b4 -d am $link
This gives me:
v4_20210112_morbo_pgo_add_clang_s_profile_guided_optimization_infrastructure.mbx
- Sedat -
Quoted 41 lines
[1] https://github.com/gwelymernans/linux/commits/gwelymernans/linux
Quoted 37 lines
Quoted 28 lines
$ llvm-profdata show -topn=20 /tmp/vmlinux.profraw Instrumentation level: IR entry_first = 0 Total functions: 48970 Maximum function count: 62070879 Maximum internal block count: 83221158 Top 20 functions with the largest internal block counts: drivers/tty/n_tty.c:n_tty_write, max count = 83221158 rcu_read_unlock_strict, max count = 62070879 _cond_resched, max count = 25486882 rcu_all_qs, max count = 25451477 drivers/cpuidle/poll_state.c:poll_idle, max count = 23618576 _raw_spin_unlock_irqrestore, max count = 18874121 drivers/cpuidle/governors/menu.c:menu_select, max count = 18721624 _raw_spin_lock_irqsave, max count = 18509161 memchr, max count = 15525452 _raw_spin_lock, max count = 15484254 __mod_memcg_state, max count = 14604619 __mod_memcg_lruvec_state, max count = 14602783 fs/ext4/hash.c:str2hashbuf_signed, max count = 14098424 __mod_lruvec_state, max count = 12527154 __mod_node_page_state, max count = 12525172 native_sched_clock, max count = 8904692 sched_clock_cpu, max count = 8895832 sched_clock, max count = 8894627 kernel/entry/common.c:exit_to_user_mode_prepare, max count = 8289031 fpregs_assert_state_consistent, max count = 8287198Quoted 4 lines
--
Thanks,
~Nick Desaulniers--
You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/20210116001324.2865-1-nick.desaulniers%40gmail.com.
Quoted 32 lines · Nick Desaulniers
Quoted 23 lines
Quoted 20 lines · Nick Desaulniers
Quoted 12 lines · Nathan Chancellor
However, I see an issue with actually using the data:
$ sudo -s # mount -t debugfs none /sys/kernel/debug # cp -a /sys/kernel/debug/pgo/profraw vmlinux.profraw # chown nathan:nathan vmlinux.profraw # exit $ tc-build/build/llvm/stage1/bin/llvm-profdata merge --output=vmlinux.profdata vmlinux.profraw warning: vmlinux.profraw: Invalid instrumentation profile data (bad magic) error: No profiles could be merged.Quoted 2 lines
Am I holding it wrong? :) Note, this is virtualized, I do not have any "real" x86 hardware that I can afford to test on right now.
Same.
Quoted 8 lines
Quoted 2 lines
I think the magic calculation in this patch may differ from upstream
llvm: https://github.com/llvm/llvm-project/blob/49142991a685bd427d7e877c29c77371dfb7634c/llvm/include/llvm/ProfileData/SampleProf.h#L96-L101Err...it looks like it was the padding calculation. With that fixed up, we can query the profile data to get insights on the most heavily called functions. Here's what my top 20 are (reset, then watch 10 minutes worth of cat videos on youtube while running
find /and sleeping at my desk). Anything curious stand out to anyone?
Quoted 7 lines
Hello world from my personal laptop whose kernel was rebuilt with profiling data! Wow, I can run
find /and watch cat videos on youtube so fast! Users will love this! /sChecking the sections sizes of .text.hot. and .text.unlikely. looks good!
On each rebuild I need to pass to make ...?
LLVM=1 -fprofile-use=vmlinux.profdata
Did you try together with passing LLVM_IAS=1 to make?
- Sedat -
Quoted 37 lines
Quoted 28 lines
$ llvm-profdata show -topn=20 /tmp/vmlinux.profraw Instrumentation level: IR entry_first = 0 Total functions: 48970 Maximum function count: 62070879 Maximum internal block count: 83221158 Top 20 functions with the largest internal block counts: drivers/tty/n_tty.c:n_tty_write, max count = 83221158 rcu_read_unlock_strict, max count = 62070879 _cond_resched, max count = 25486882 rcu_all_qs, max count = 25451477 drivers/cpuidle/poll_state.c:poll_idle, max count = 23618576 _raw_spin_unlock_irqrestore, max count = 18874121 drivers/cpuidle/governors/menu.c:menu_select, max count = 18721624 _raw_spin_lock_irqsave, max count = 18509161 memchr, max count = 15525452 _raw_spin_lock, max count = 15484254 __mod_memcg_state, max count = 14604619 __mod_memcg_lruvec_state, max count = 14602783 fs/ext4/hash.c:str2hashbuf_signed, max count = 14098424 __mod_lruvec_state, max count = 12527154 __mod_node_page_state, max count = 12525172 native_sched_clock, max count = 8904692 sched_clock_cpu, max count = 8895832 sched_clock, max count = 8894627 kernel/entry/common.c:exit_to_user_mode_prepare, max count = 8289031 fpregs_assert_state_consistent, max count = 8287198Quoted 4 lines
--
Thanks,
~Nick Desaulniers--
You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/20210116001324.2865-1-nick.desaulniers%40gmail.com.
Quoted 184 lines · Bill Wendling
From: Sami Tolvanen <[email protected]>
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.profrawMultiple 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 <[email protected]>Co-developed-by Bill Wendling <[email protected]>Signed-off-by Bill Wendling <[email protected]>Change-Id Ic78e69c682286d3a44c4549a0138578c98138b77---
v2: - Added "__llvm_profile_instrument_memop" based on Nathan Chancellor's
testing.
- Corrected documentation, re PGO flags when using LTO, based on Fangrui Song's comments.
v3: - Added change log section based on Sedat Dilek's comments.
v4: - Remove non-x86 Makfile changes and se "hweight64" instead of using our
own popcount implementation, based on Nick Desaulniers's comment.--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/pgo.rst | 127 +++++++++ MAINTAINERS | 9 + Makefile | 3 + arch/Kconfig | 1 + arch/x86/Kconfig | 1 + arch/x86/boot/Makefile | 1 + arch/x86/boot/compressed/Makefile | 1 + arch/x86/entry/vdso/Makefile | 1 + arch/x86/kernel/vmlinux.lds.S | 2 + arch/x86/platform/efi/Makefile | 1 + arch/x86/purgatory/Makefile | 1 + arch/x86/realmode/rm/Makefile | 1 + arch/x86/um/vdso/Makefile | 1 + drivers/firmware/efi/libstub/Makefile | 1 + include/asm-generic/vmlinux.lds.h | 44 +++ kernel/Makefile | 1 + kernel/pgo/Kconfig | 34 +++ kernel/pgo/Makefile | 5 + kernel/pgo/fs.c | 382 ++++++++++++++++++++++++++ kernel/pgo/instrument.c | 185 +++++++++++++ kernel/pgo/pgo.h | 206 ++++++++++++++ scripts/Makefile.lib | 10 + 23 files changed, 1019 insertions(+) create mode 100644 Documentation/dev-tools/pgo.rst create mode 100644 kernel/pgo/Kconfig create mode 100644 kernel/pgo/Makefile create mode 100644 kernel/pgo/fs.c create mode 100644 kernel/pgo/instrument.c create mode 100644 kernel/pgo/pgo.hDocumentation/dev-tools/index.rst +1
-0@@ -26,6 +26,7 @@ whole; patches welcome! kgdb kselftest kunit/index + pgo .. only:: subproject and htmlDocumentation/dev-tools/pgo.rst new file +93
-0@@ -0,0 +1,127 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=============================== +Using PGO with the Linux kernel +=============================== + +Clang's profiling kernel support (PGO_) enables profiling of the Linux kernel +when building with Clang. The profiling data is exported via the ``pgo`` +debugfs directory. + +.. _PGO: https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization + + +Preparation +=========== + +Configure the kernel with: + +.. code-block:: make + + CONFIG_DEBUG_FS=y + CONFIG_PGO_CLANG=y + +Note that kernels compiled with profiling flags will be significantly larger +and run slower. + +Profiling data will only become accessible once debugfs has been mounted: + +.. code-block:: sh + + mount -t debugfs none /sys/kernel/debug + + +Customization +============= + +You can enable or disable profiling for individual file and directories by +adding a line similar to the following to the respective kernel Makefile: + +- For a single file (e.g. main.o) + + .. code-block:: make + + PGO_PROFILE_main.o := y + +- For all files in one directory + + .. code-block:: make + + PGO_PROFILE := y + +To exclude files from being profiled use + + .. code-block:: make + + PGO_PROFILE_main.o := n + +and + + .. code-block:: make + + PGO_PROFILE := n + +Only files which are linked to the main kernel image or are compiled as kernel +modules are supported by this mechanism. + + +Files +===== + +The PGO kernel support creates the following files in debugfs: + +``/sys/kernel/debug/pgo`` + Parent directory for all PGO-related files. + +``/sys/kernel/debug/pgo/reset`` + Global reset file: resets all coverage data to zero when written to. + +``/sys/kernel/debug/profraw`` + The raw PGO data that must be processed with ``llvm_profdata``. + + +Workflow +======== + +The PGO kernel can be run on the host or test machines. The data though should +be analyzed with Clang's tools from the same Clang version as the kernel was +compiled. Clang's tolerant of version skew, but it's easier to use the same +Clang version. + +The profiling data is useful for optimizing the kernel, analyzing coverage, +etc. Clang offers tools to perform these tasks. +
Documentation/dev-tools/pgo.rst line 103
Quoted 10 lines
Documentation/dev-tools/pgo.rst new file +10
-0+Here is an example workflow for profiling an instrumented kernel with PGO and +using the result to optimize the kernel: + +1) Install the kernel on the TEST machine. + +2) Reset the data counters right before running the load tests + + .. code-block:: sh + + $ echo 1 > /sys/kernel/debug/pgo/reset
Maybe I'm a noob, but I had to:
$ mkdir -p /sys/kernel/debug $ mount -t debugfs none /sys/kernel/debug
first. That might trip up future travelers (like myself, I'm prone to forget these unless they're in my shell history).
Quoted 197 lines
Documentation/dev-tools/pgo.rst new file +24
-0+ +3) Run the load tests. + +4) Collect the raw profile data + + .. code-block:: sh + + $ cp -a /sys/kernel/debug/pgo/profraw /tmp/vmlinux.profraw + +5) (Optional) Download the raw profile data to the HOST machine. + +6) Process the raw profile data + + .. code-block:: sh + + $ llvm-profdata merge --output=vmlinux.profdata vmlinux.profraw + + Note that multiple raw profile data files can be merged during this step. + +7) Rebuild the kernel using the profile data (PGO disabled) + + .. code-block:: sh + + $ make LLVM=1 KCFLAGS=-fprofile-use=vmlinux.profdata ...MAINTAINERS +9
-0@@ -13954,6 +13954,15 @@ S: Maintained F: include/linux/personality.h F: include/uapi/linux/personality.h +PGO BASED KERNEL PROFILING +M: Sami Tolvanen <[email protected]> +M: Bill Wendling <[email protected]> +R: Nathan Chancellor <[email protected]> +R: Nick Desaulniers <[email protected]> +S: Supported +F: Documentation/dev-tools/pgo.rst +F: kernel/pgo + PHOENIX RC FLIGHT CONTROLLER ADAPTER M: Marcus Folkesson <[email protected]> L: [email protected]Makefile +3
-0@@ -659,6 +659,9 @@ endif # KBUILD_EXTMOD # Defaults to vmlinux, but the arch makefile usually adds further targets all: vmlinux +CFLAGS_PGO_CLANG := -fprofile-generate +export CFLAGS_PGO_CLANG + CFLAGS_GCOV := -fprofile-arcs -ftest-coverage \ $(call cc-option,-fno-tree-loop-im) \ $(call cc-disable-warning,maybe-uninitialized,)arch/Kconfig +1
-0@@ -1112,6 +1112,7 @@ config ARCH_SPLIT_ARG64 pairs of 32-bit arguments, select this option. source "kernel/gcov/Kconfig" +source "kernel/pgo/Kconfig" source "scripts/gcc-plugins/Kconfig"arch/x86/Kconfig +1
-0@@ -96,6 +96,7 @@ config X86 select ARCH_SUPPORTS_DEBUG_PAGEALLOC select ARCH_SUPPORTS_NUMA_BALANCING if X86_64 select ARCH_SUPPORTS_KMAP_LOCAL_FORCE_MAP if NR_CPUS <= 4096 + select ARCH_SUPPORTS_PGO_CLANG if X86_64 select ARCH_USE_BUILTIN_BSWAP select ARCH_USE_QUEUED_RWLOCKS select ARCH_USE_QUEUED_SPINLOCKSarch/x86/boot/Makefile +1
-0@@ -71,6 +71,7 @@ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=) KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n $(obj)/bzImage: asflags-y := $(SVGA_MODE)arch/x86/boot/compressed/Makefile +1
-0@@ -54,6 +54,7 @@ CFLAGS_sev-es.o += -I$(objtree)/arch/x86/lib/ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE :=n KBUILD_LDFLAGS := -m elf_$(UTS_MACHINE)arch/x86/entry/vdso/Makefile +1
-0@@ -179,6 +179,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 \ $(call ld-option, --eh-frame-hdr) -Bsymbolic GCOV_PROFILE := n +PGO_PROFILE := n quiet_cmd_vdso_and_check = VDSO $@ cmd_vdso_and_check = $(cmd_vdso); $(cmd_vdso_check)arch/x86/kernel/vmlinux.lds.S +2
-0@@ -184,6 +184,8 @@ SECTIONS BUG_TABLE + PGO_CLANG_DATA + ORC_UNWIND_TABLE . = ALIGN(PAGE_SIZE);arch/x86/platform/efi/Makefile +1
-0@@ -2,6 +2,7 @@ OBJECT_FILES_NON_STANDARD_efi_thunk_$(BITS).o := y KASAN_SANITIZE := n GCOV_PROFILE := n +PGO_PROFILE := n obj-$(CONFIG_EFI) += quirks.o efi.o efi_$(BITS).o efi_stub_$(BITS).o obj-$(CONFIG_EFI_MIXED) += efi_thunk_$(BITS).oarch/x86/purgatory/Makefile +1
-0@@ -23,6 +23,7 @@ targets += purgatory.ro purgatory.chk # Sanitizer, etc. runtimes are unavailable and cannot be linked here. GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n UBSAN_SANITIZE := n KCSAN_SANITIZE := narch/x86/realmode/rm/Makefile +1
-0@@ -76,4 +76,5 @@ KBUILD_CFLAGS := $(REALMODE_CFLAGS) -D_SETUP -D_WAKEUP \ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := narch/x86/um/vdso/Makefile +1
-0@@ -64,6 +64,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -fPIC -shared -Wl,--hash-style=sysv GCOV_PROFILE := n +PGO_PROFILE := n # # Install the unstripped copy of vdso*.so listed in $(vdso-install-y).drivers/firmware/efi/libstub/Makefile +1
-0@@ -40,6 +40,7 @@ KBUILD_CFLAGS := $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS)) GCOV_PROFILE := n +PGO_PROFILE := n # Sanitizer runtimes are unavailable and cannot be linked here. KASAN_SANITIZE := n KCSAN_SANITIZE := ninclude/asm-generic/vmlinux.lds.h +0
-0@@ -316,6 +316,49 @@#define THERMAL_TABLE (name)#endif
include/asm-generic/vmlinux.lds.h line 328
Quoted 10 lines
include/asm-generic/vmlinux.lds.h +10
-0+#ifdef CONFIG_PGO_CLANG +#define PGO_CLANG_DATA \ + __llvm_prf_data : AT(ADDR(__llvm_prf_data) - LOAD_OFFSET) { \ + . = ALIGN(8); \ + __llvm_prf_start = .; \ + __llvm_prf_data_start = .; \ + KEEP(*(__llvm_prf_data)) \ + . = ALIGN(8); \ + __llvm_prf_data_end = .; \ + } \
So if I do a build of
$ make CC=clang I observe the following error: `.discard.text' referenced in section `__llvm_prf_data' of arch/x86/kernel/setup.o: defined in discarded section `.discard.text' of arch/x86/kernel/setup.o `.discard.text' referenced in section `__llvm_prf_data' of arch/x86/mm/init.o: defined in discarded section `.discard.text' of arch/x86/mm/init.o
This can be investigated more via:
$ llvm-objdump -Dr -j __llvm_prf_data arch/x86/mm/init.o | grep discard
0000000000000168: R_X86_64_64 .discard.textSo looks like a relocation is referencing something in .discard.text.
$ llvm-objdump -Dr -j .discard.text arch/x86/mm/init.o
...
0000000000000000 <__brk_reservation_fn_early_pgt_alloc__>:
0: 48 83 05 00 00 00 00 01 addq $1, (%rip) # 8
<__brk_reservation_fn_early_pgt_alloc__+0x8>
0000000000000003: R_X86_64_PC32 __llvm_prf_cnts+0xf3
8: c3 retqLooks like arch/x86/include/asm/setup.h defines the macro RESERVE_BRK which defines a static function in the .discard.text section. Is there a function attribute that we can use to say "please don't profile this one function?"
For arch/x86/kernel/setup.o it looks like the same issue with __brk_reservation_fn_dmi_alloc__.
More specifically, this warning goes away when using LLD:
$ make CC=clang LD=ld.lld
Not sure yet why these warnings are only observed when using BFD? Maybe LLD should also be producing this diagnostic, but is not?
Anyways, I was able to build+boot profiling mode binaries built with:
$ make LLVM=1 defconfig+PGO $ make LLVM=1 LLVM_IAS=1 defconfig+PGO
It would be good to resolve/investigate the above error with BFD and fix it, or make this config also depend on LLD for now. ie.
$ make CC=clang defconfig+PGO
I'm going to try rebuilding+booting with the profile data now, and will report back.
Quoted 62 lines
include/asm-generic/vmlinux.lds.h +34
-0+ __llvm_prf_cnts : AT(ADDR(__llvm_prf_cnts) - LOAD_OFFSET) { \ + . = ALIGN(8); \ + __llvm_prf_cnts_start = .; \ +KEEP (*(__llvm_prf_cnts)) \ + . = ALIGN(8); \ + __llvm_prf_cnts_end = .; \ + } \ + __llvm_prf_names : AT(ADDR(__llvm_prf_names) - LOAD_OFFSET) { \ + . = ALIGN(8); \ + __llvm_prf_names_start = .; \ +KEEP (*(__llvm_prf_names)) \ + . = ALIGN(8); \ + __llvm_prf_names_end = .; \ + . = ALIGN(8); \ + } \ + __llvm_prf_vals : AT(ADDR(__llvm_prf_vals) - LOAD_OFFSET) { \ + __llvm_prf_vals_start = .; \ +KEEP (*(__llvm_prf_vals)) \ + . = ALIGN(8); \ + __llvm_prf_vals_end = .; \ + . = ALIGN(8); \ + } \ + __llvm_prf_vnds : AT(ADDR(__llvm_prf_vnds) - LOAD_OFFSET) { \ + __llvm_prf_vnds_start = .; \ +KEEP (*(__llvm_prf_vnds)) \ + . = ALIGN(8); \ + __llvm_prf_vnds_end = .; \ + __llvm_prf_end = .; \ + } +#else +#define PGO_CLANG_DATA +#endif +#define KERNEL_DTB () \ STRUCT_ALIGN(); \ __dtb_start = .; \@@ -1125,6 +1168,7 @@ CONSTRUCTORS \ } \BUG_TABLE \ + PGO_CLANG_DATA#define INIT_TEXT_SECTION (inittext_align) \ . = ALIGN(inittext_align); \kernel/Makefile +1
-0@@ -111,6 +111,7 @@ obj-$(CONFIG_BPF) += bpf/ obj-$(CONFIG_KCSAN) += kcsan/ obj-$(CONFIG_SHADOW_CALL_STACK) += scs.o obj-$(CONFIG_HAVE_STATIC_CALL_INLINE) += static_call.o +obj-$(CONFIG_PGO_CLANG) += pgo/ obj-$(CONFIG_PERF_EVENTS) += events/kernel/pgo/Kconfig new file +0
-0
kernel/pgo/Kconfig line 10
Quoted 10 lines
kernel/pgo/Kconfig new file +10
-0@@ -0,0 +1,34 @@ +# SPDX-License-Identifier: GPL-2.0-only +menu "Profile Guided Optimization (PGO) (EXPERIMENTAL)" + +config ARCH_SUPPORTS_PGO_CLANG + bool + +config PGO_CLANG + bool "Enable clang's PGO-based kernel profiling" + depends on DEBUG_FS + depends on ARCH_SUPPORTS_PGO_CLANG
probably should additionally:
depends on CLANG_VERSION >= 120000
I'm observing the same failed assertion as Nathan trying to build x86_64 defconfig (drivers/gpu/drm/i915/i915_query.c), that should be fixed by:
https://reviews.llvm.org/D94470
I think that would also help prevent this config from being selectable if not using CC=clang?
Quoted 7 lines
kernel/pgo/Kconfig new file +7
-0+ help + This option enables clang's PGO (Profile Guided Optimization) based + code profiling to better optimize the kernel. + + If unsure, say N. + + Run a representative workload for your application on a kernel
kernel/pgo/Kconfig line 27
Quoted 10 lines
kernel/pgo/Kconfig new file +10
-0+ compiled with this option and download the raw profile file from + /sys/kernel/debug/pgo/profraw. This file needs to be processed with + llvm-profdata. It may be merged with other collected raw profiles. + + Copy the resulting profile file into vmlinux.profdata, and enable + KCFLAGS=-fprofile-use=vmlinux.profdata to produce an optimized + kernel. + + Note that a kernel compiled with profiling flags will be + significatnly larger and run slower. Also be sure to exclude files
^ typo: significantly
Quoted 236 lines
kernel/pgo/Kconfig new file +7
-0+ from profiling which are not linked to the kernel image to prevent + linker errors. + + Note that the debugfs filesystem has to be mounted to access + profiling data. + +endmenukernel/pgo/Makefile new file +5
-0@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +GCOV_PROFILE := n +PGO_PROFILE := n + +obj-y += fs.o instrument.okernel/pgo/fs.c new file +212
-0@@ -0,0 +1,382 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2019 Google, Inc. +* +* Author: +* Sami Tolvanen <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#define pr_fmt (fmt) "pgo: " fmt + +#include <linux/kernel.h> +#include <linux/debugfs.h> +#include <linux/fs.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/vmalloc.h> +#include "pgo.h" + +static struct dentry *directory; + +struct prf_private_data { +void *buffer; +unsigned long size; +}; + +/* +* Raw profile data format: +* +* - llvm_prf_header +* - __llvm_prf_data +* - __llvm_prf_cnts +* - __llvm_prf_names +* - zero padding to 8 bytes +* - for each llvm_prf_data in __llvm_prf_data: +* - llvm_prf_value_data +* - llvm_prf_value_record + site count array +* - llvm_prf_value_node_data +* ... +* ... +* ... +*/ + +static void prf_fill_header (void **buffer) +{ +struct llvm_prf_header *header = *(struct llvm_prf_header **)buffer; + + header->magic =LLVM_PRF_MAGIC ; + header->version =LLVM_PRF_VARIANT_MASK_IR |LLVM_PRF_VERSION ; + header->data_size =prf_data_count (); + header->padding_bytes_before_counters =0 ; + header->counters_size =prf_cnts_count (); + header->padding_bytes_after_counters =0 ; + header->names_size =prf_names_count (); + header->counters_delta = (u64 )__llvm_prf_cnts_start; + header->names_delta = (u64 )__llvm_prf_names_start; + header->value_kind_last =LLVM_PRF_IPVK_LAST ; + + *buffer +=sizeof (*header); +} + +/* +* Copy the source into the buffer, incrementing the pointer into buffer in the +* process. +*/ +static void prf_copy_to_buffer (void **buffer,void *src,unsigned long size) +{ +memcpy (*buffer, src, size); + *buffer += size; +} + +static u32 __prf_get_value_size (struct llvm_prf_data *p,u32 *value_kinds) +{ +struct llvm_prf_value_node **nodes = + (struct llvm_prf_value_node **)p->values; +u32 kinds =0 ; +u32 size =0 ; +unsigned int kind; +unsigned int n; +unsigned int s =0 ; + +for (kind =0 ; kind <ARRAY_SIZE (p->num_value_sites); kind++) { +unsigned int sites = p->num_value_sites[kind]; + +if (!sites) +continue ; + +/* Record + site count array */ + size +=prf_get_value_record_size (sites); + kinds++; + +if (!nodes) +continue ; + +for (n =0 ; n < sites; n++) { +u32 count =0 ; +struct llvm_prf_value_node *site = nodes[s + n]; + +while (site && ++count <=U8_MAX ) + site = site->next; + + size += count * +sizeof (struct llvm_prf_value_node_data ); + } + + s += sites; + } + +if (size) + size +=sizeof (struct llvm_prf_value_data ); + +if (value_kinds) + *value_kinds = kinds; + +return size; +} + +static u32 prf_get_value_size (void ) +{ +u32 size =0 ; +struct llvm_prf_data *p; + +for (p = __llvm_prf_data_start; p < __llvm_prf_data_end; p++) + size +=__prf_get_value_size (p,NULL ); + +return size; +} + +/* Serialize the profiling's value. */ +static void prf_serialize_value (struct llvm_prf_data *p,void **buffer) +{ +struct llvm_prf_value_data header; +struct llvm_prf_value_node **nodes = + (struct llvm_prf_value_node **)p->values; +unsigned int kind; +unsigned int n; +unsigned int s =0 ; + + header.total_size =__prf_get_value_size (p, &header.num_value_kinds); + +if (!header.num_value_kinds) +/* Nothing to write. */ +return ; + +prf_copy_to_buffer (buffer, &header,sizeof (header)); + +for (kind =0 ; kind <ARRAY_SIZE (p->num_value_sites); kind++) { +struct llvm_prf_value_record *record; +u8 *counts; +unsigned int sites = p->num_value_sites[kind]; + +if (!sites) +continue ; + +/* Profiling value record. */ + record = *(struct llvm_prf_value_record **)buffer; + *buffer +=prf_get_value_record_header_size (); + + record->kind = kind; + record->num_value_sites = sites; + +/* Site count array. */ + counts = *(u8 **)buffer; + *buffer +=prf_get_value_record_site_count_size (sites); + +/* +* If we don't have nodes, we can skip updating the site count +* array, because the buffer is zero filled. +*/ +if (!nodes) +continue ; + +for (n =0 ; n < sites; n++) { +u32 count =0 ; +struct llvm_prf_value_node *site = nodes[s + n]; + +while (site && ++count <=U8_MAX ) { +prf_copy_to_buffer (buffer, site, +sizeof (struct llvm_prf_value_node_data )); + site = site->next; + } + + counts[n] = (u8 )count; + } + + s += sites; + } +} + +static void prf_serialize_values (void **buffer) +{ +struct llvm_prf_data *p; + +for (p = __llvm_prf_data_start; p < __llvm_prf_data_end; p++) +prf_serialize_value (p, buffer); +} + +static inline unsigned long prf_get_padding (unsigned long size) +{ +return 8 - (size %8 ); +} + +static unsigned long prf_buffer_size (void )
kernel/pgo/fs.c line 222
Quoted 10 lines
kernel/pgo/fs.c new file +10
-0+{ +return sizeof (struct llvm_prf_header ) + +prf_data_size () + +prf_cnts_size () + +prf_names_size () + +prf_get_padding (prf_names_size ()) + +prf_get_value_size (); +} + +/* Serialize the profling data into a format LLVM's tools can understand. */
^ typo: profiling
Quoted 143 lines
kernel/pgo/fs.c new file +143
-0+static int prf_serialize (struct prf_private_data *p) +{ +int err =0 ; +void *buffer; + + p->size =prf_buffer_size (); + p->buffer =vzalloc (p->size); + +if (!p->buffer) { + err = -ENOMEM ; + goto out; + } + + buffer = p->buffer; + +prf_fill_header (&buffer); +prf_copy_to_buffer (&buffer, __llvm_prf_data_start,prf_data_size ()); +prf_copy_to_buffer (&buffer, __llvm_prf_cnts_start,prf_cnts_size ()); +prf_copy_to_buffer (&buffer, __llvm_prf_names_start,prf_names_size ()); + buffer +=prf_get_padding (prf_names_size ()); + +prf_serialize_values (&buffer); + +out: +return err; +} + +/* open() implementation for PGO. Creates a copy of the profiling data set. */ +static int prf_open (struct inode *inode,struct file *file) +{ +struct prf_private_data *data; +unsigned long flags; +int err; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) { + err = -ENOMEM ; + goto out; + } + + flags =prf_lock (); + + err =prf_serialize (data); +if (err) { +kfree (data); + goto out_unlock; + } + + file->private_data = data; + +out_unlock: +prf_unlock (flags); +out: +return err; +} + +/* read() implementation for PGO. */ +static ssize_t prf_read (struct file *file,char __user *buf,size_t count, +loff_t *ppos) +{ +struct prf_private_data *data = file->private_data; + +BUG_ON (!data); + +return simple_read_from_buffer (buf, count, ppos, data->buffer, + data->size); +} + +/* release() implementation for PGO. Release resources allocated by open(). */ +static int prf_release (struct inode *inode,struct file *file) +{ +struct prf_private_data *data = file->private_data; + +if (data) { +vfree (data->buffer); +kfree (data); + } + +return 0 ; +} + +static const struct file_operations prf_fops = { + .owner =THIS_MODULE , + .open = prf_open, + .read = prf_read, + .llseek = default_llseek, + .release = prf_release +}; + +/* write() implementation for resetting PGO's profile data. */ +static ssize_t reset_write (struct file *file,const char __user *addr, +size_t len,loff_t *pos) +{ +struct llvm_prf_data *data; + +memset (__llvm_prf_cnts_start,0 ,prf_cnts_size ()); + +for (data = __llvm_prf_data_start; data < __llvm_prf_data_end; ++data) { +struct llvm_prf_value_node **vnodes; +u64 current_vsite_count; +u32 i; + +if (!data->values) +continue ; + + current_vsite_count =0 ; + vnodes = (struct llvm_prf_value_node **)data->values; + +for (i =LLVM_PRF_IPVK_FIRST ; i <=LLVM_PRF_IPVK_LAST ; ++i) + current_vsite_count += data->num_value_sites[i]; + +for (i =0 ; i < current_vsite_count; ++i) { +struct llvm_prf_value_node *current_vnode = vnodes[i]; + +while (current_vnode) { + current_vnode->count =0 ; + current_vnode = current_vnode->next; + } + } + } + +return len; +} + +static const struct file_operations prf_reset_fops = { + .owner =THIS_MODULE , + .write = reset_write, + .llseek = noop_llseek, +}; + +/* Create debugfs entries. */ +static int __init pgo_init (void ) +{ + directory =debugfs_create_dir ("pgo" ,NULL ); +if (!directory) + goto err_remove; + +if (!debugfs_create_file ("profraw" ,0600 , directory,NULL , + &prf_fops)) + goto err_remove; + +if (!debugfs_create_file ("reset" ,0200 , directory,NULL , + &prf_reset_fops))
kernel/pgo/fs.c line 375
Quoted 10 lines
kernel/pgo/fs.c new file +10
-0+ goto err_remove; + +return 0 ; + +err_remove: +pr_err ("initialization failed\n" ); +return -EIO ; +} + +/* Remove debufs entries. */
^ typo: debugfs
Quoted 434 lines
kernel/pgo/fs.c new file +7
-0+static void __exit pgo_exit(void) +{ +debugfs_remove_recursive (directory); +} + +module_init (pgo_init); +module_exit (pgo_exit);kernel/pgo/instrument.c new file +185
-0@@ -0,0 +1,185 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2019 Google, Inc. +* +* Author: +* Sami Tolvanen <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#define pr_fmt (fmt) "pgo: " fmt + +#include <linux/bitops.h> +#include <linux/kernel.h> +#include <linux/export.h> +#include <linux/spinlock.h> +#include <linux/types.h> +#include "pgo.h" + +/* Lock guarding value node access and serialization. */ +static DEFINE_SPINLOCK (pgo_lock); +static int current_node; + +unsigned long prf_lock (void ) +{ +unsigned long flags; + +spin_lock_irqsave (&pgo_lock, flags); + +return flags; +} + +void prf_unlock (unsigned long flags) +{ +spin_unlock_irqrestore (&pgo_lock, flags); +} + +/* +* Return a newly allocated profiling value node which contains the tracked +* value by the value profiler. +* Note: caller *must* hold pgo_lock. +*/ +static struct llvm_prf_value_node *allocate_node (struct llvm_prf_data *p, +u32 index,u64 value) +{ +if (&__llvm_prf_vnds_start[current_node +1 ] >= __llvm_prf_vnds_end) +return NULL ;/* Out of nodes */ + + current_node++; + +/* Make sure the node is entirely within the section */ +if (&__llvm_prf_vnds_start[current_node] >= __llvm_prf_vnds_end || + &__llvm_prf_vnds_start[current_node +1 ] > __llvm_prf_vnds_end) +return NULL ; + +return &__llvm_prf_vnds_start[current_node]; +} + +/* +* Counts the number of times a target value is seen. +* +* Records the target value for the CounterIndex if not seen before. Otherwise, +* increments the counter associated w/ the target value. +*/ +void __llvm_profile_instrument_target (u64 target_value,void *data,u32 index); +void __llvm_profile_instrument_target (u64 target_value,void *data,u32 index) +{ +struct llvm_prf_data *p = (struct llvm_prf_data *)data; +struct llvm_prf_value_node **counters; +struct llvm_prf_value_node *curr; +struct llvm_prf_value_node *min =NULL ; +struct llvm_prf_value_node *prev =NULL ; +u64 min_count =U64_MAX ; +u8 values =0 ; +unsigned long flags; + +if (!p || !p->values) +return ; + + counters = (struct llvm_prf_value_node **)p->values; + curr = counters[index]; + +while (curr) { +if (target_value == curr->value) { + curr->count++; +return ; + } + +if (curr->count < min_count) { + min_count = curr->count; + min = curr; + } + + prev = curr; + curr = curr->next; + values++; + } + +if (values >=LLVM_PRF_MAX_NUM_VALS_PER_SITE ) { +if (!min->count || !(--min->count)) { + curr = min; + curr->value = target_value; + curr->count++; + } +return ; + } + +/* Lock when updating the value node structure. */ + flags =prf_lock (); + + curr =allocate_node (p, index, target_value); +if (!curr) + goto out; + + curr->value = target_value; + curr->count++; + +if (!counters[index]) + counters[index] = curr; +else if (prev && !prev->next) + prev->next = curr; + +out: +prf_unlock (flags); +} +EXPORT_SYMBOL (__llvm_profile_instrument_target); + +/* Counts the number of times a range of targets values are seen. */ +void __llvm_profile_instrument_range (u64 target_value,void *data, +u32 index,s64 precise_start, +s64 precise_last,s64 large_value); +void __llvm_profile_instrument_range (u64 target_value,void *data, +u32 index,s64 precise_start, +s64 precise_last,s64 large_value) +{ +if (large_value !=S64_MIN && (s64 )target_value >= large_value) + target_value = large_value; +else if ((s64 )target_value < precise_start || + (s64 )target_value > precise_last) + target_value = precise_last +1 ; + +__llvm_profile_instrument_target (target_value, data, index); +} +EXPORT_SYMBOL (__llvm_profile_instrument_range); + +static u64 inst_prof_get_range_rep_value (u64 value) +{ +if (value <=8 ) +/* The first ranges are individually tracked, us it as is. */ +return value; +else if (value >=513 ) +/* The last range is mapped to its lowest value. */ +return 513 ; +else if (hweight64 (value) ==1 ) +/* If it's a power of two, use it as is. */ +return value; + +/* Otherwise, take to the previous power of two + 1. */ +return (1 << (64 -__builtin_clzll (value) -1 )) +1 ; +} + +/* +* The target values are partitioned into multiple ranges. The range spec is +* defined in compiler-rt/include/profile/InstrProfData.inc. +*/ +void __llvm_profile_instrument_memop (u64 target_value,void *data, +u32 counter_index); +void __llvm_profile_instrument_memop (u64 target_value,void *data, +u32 counter_index) +{ +u64 rep_value; + +/* Map the target value to the representative value of its range. */ + rep_value =inst_prof_get_range_rep_value (target_value); +__llvm_profile_instrument_target (rep_value, data, counter_index); +} +EXPORT_SYMBOL (__llvm_profile_instrument_memop);kernel/pgo/pgo.h new file +206
-0@@ -0,0 +1,206 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2019 Google, Inc. +* +* Author: +* Sami Tolvanen <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef _PGO_H +#define _PGO_H + +/* +* Note: These internal LLVM definitions must match the compiler version. +* See llvm/include/llvm/ProfileData/InstrProfData.inc in LLVM's source code. +*/ + +#ifdef CONFIG_64BIT +#define LLVM_PRF_MAGIC \ + ((u64)255 << 56 | \ + (u64)'l' << 48 | \ + (u64)'p' << 40 | \ + (u64)'r' << 32 | \ + (u64)'o' << 24 | \ + (u64)'f' << 16 | \ + (u64)'r' << 8 | \ + (u64)129) +#else +#define LLVM_PRF_MAGIC \ + ((u64)255 << 56 | \ + (u64)'l' << 48 | \ + (u64)'p' << 40 | \ + (u64)'r' << 32 | \ + (u64)'o' << 24 | \ + (u64)'f' << 16 | \ + (u64)'R' << 8 | \ + (u64)129) +#endif + +#define LLVM_PRF_VERSION 5 +#define LLVM_PRF_DATA_ALIGN 8 +#define LLVM_PRF_IPVK_FIRST 0 +#define LLVM_PRF_IPVK_LAST 1 +#define LLVM_PRF_MAX_NUM_VALS_PER_SITE 16 + +#define LLVM_PRF_VARIANT_MASK_IR (0x1ull << 56) +#define LLVM_PRF_VARIANT_MASK_CSIR (0x1ull << 57) + +/** +* struct llvm_prf_header - represents the raw profile header data structure. +* @magic: the magic token for the file format. +* @version: the version of the file format. +* @data_size: the number of entries in the profile data section. +* @padding_bytes_before_counters: the number of padding bytes before the +* counters. +* @counters_size: the size in bytes of the LLVM profile section containing the +* counters. +* @padding_bytes_after_counters: the number of padding bytes after the +* counters. +* @names_size: the size in bytes of the LLVM profile section containing the +* counters' names. +* @counters_delta: the beginning of the LLMV profile counters section. +* @names_delta: the beginning of the LLMV profile names section. +* @value_kind_last: the last profile value kind. +*/ +struct llvm_prf_header { +u64 magic; +u64 version; +u64 data_size; +u64 padding_bytes_before_counters; +u64 counters_size; +u64 padding_bytes_after_counters; +u64 names_size; +u64 counters_delta; +u64 names_delta; +u64 value_kind_last; +}; + +/** +* struct llvm_prf_data - represents the per-function control structure. +* @name_ref: the reference to the function's name. +* @func_hash: the hash value of the function. +* @counter_ptr: a pointer to the profile counter. +* @function_ptr: a pointer to the function. +* @values: the profiling values associated with this function. +* @num_counters: the number of counters in the function. +* @num_value_sites: the number of value profile sites. +*/ +struct llvm_prf_data { +const u64 name_ref; +const u64 func_hash; +const void *counter_ptr; +const void *function_ptr; +void *values; +const u32 num_counters; +const u16 num_value_sites[LLVM_PRF_IPVK_LAST +1 ]; +}__aligned(LLVM_PRF_DATA_ALIGN) ; + +/** +* structure llvm_prf_value_node_data - represents the data part of the struct +* llvm_prf_value_node data structure. +* @value: the value counters. +* @count: the counters' count. +*/ +struct llvm_prf_value_node_data { +u64 value; +u64 count; +}; + +/** +* struct llvm_prf_value_node - represents an internal data structure used by +* the value profiler. +* @value: the value counters. +* @count: the counters' count. +* @next: the next value node. +*/ +struct llvm_prf_value_node { +u64 value; +u64 count; +struct llvm_prf_value_node *next; +}; + +/** +* struct llvm_prf_value_data - represents the value profiling data in indexed +* format. +* @total_size: the total size in bytes including this field. +* @num_value_kinds: the number of value profile kinds that has value profile +* data. +*/ +struct llvm_prf_value_data { +u32 total_size; +u32 num_value_kinds; +}; + +/** +* struct llvm_prf_value_record - represents the on-disk layout of the value +* profile data of a particular kind for one function. +* @kind: the kind of the value profile record. +* @num_value_sites: the number of value profile sites. +* @site_count_array: the first element of the array that stores the number +* of profiled values for each value site. +*/ +struct llvm_prf_value_record { +u32 kind; +u32 num_value_sites; +u8 site_count_array[]; +}; + +#define prf_get_value_record_header_size () \ + offsetof(struct llvm_prf_value_record, site_count_array) +#define prf_get_value_record_site_count_size (sites) \ + roundup((sites), 8) +#define prf_get_value_record_size (sites) \ + (prf_get_value_record_header_size() + \ + prf_get_value_record_site_count_size((sites))) + +/* Data sections */ +extern struct llvm_prf_data __llvm_prf_data_start[]; +extern struct llvm_prf_data __llvm_prf_data_end[]; + +extern u64 __llvm_prf_cnts_start[]; +extern u64 __llvm_prf_cnts_end[]; + +extern char __llvm_prf_names_start[]; +extern char __llvm_prf_names_end[]; + +extern struct llvm_prf_value_node __llvm_prf_vnds_start[]; +extern struct llvm_prf_value_node __llvm_prf_vnds_end[]; + +/* Locking for vnodes */ +extern unsigned long prf_lock (void ); +extern void prf_unlock (unsigned long flags); + +#define __DEFINE_PRF_SIZE (s) \ + static inline unsigned long prf_ ## s ## _size(void) \ + { \ + unsigned long start = \ + (unsigned long)__llvm_prf_ ## s ## _start; \ + unsigned long end = \ + (unsigned long)__llvm_prf_ ## s ## _end; \ + return roundup(end - start, \ + sizeof(__llvm_prf_ ## s ## _start[0])); \ + } \ + static inline unsigned long prf_ ## s ## _count(void) \ + { \ + return prf_ ## s ## _size() / \ + sizeof(__llvm_prf_ ## s ## _start[0]); \ + } + +__DEFINE_PRF_SIZE (data); +__DEFINE_PRF_SIZE (cnts); +__DEFINE_PRF_SIZE (names); +__DEFINE_PRF_SIZE (vnds); + +#undef __DEFINE_PRF_SIZE + +#endif /* _PGO_H */ scripts/Makefile.lib +10
-1@@ -143,6 +143,16 @@ _c_flags += $(if $(patsubst n%,, \ $(CFLAGS_GCOV)) endif +# +# Enable clang's PGO profiling flags for a file or directory depending on +# variables PGO_PROFILE_obj.o and PGO_PROFILE. +# +ifeq ($(CONFIG_PGO_CLANG),y) +_c_flags += $(if $(patsubst n%,, \ + $(PGO_PROFILE_$(basetarget).o)$(PGO_PROFILE)y), \ + $(CFLAGS_PGO_CLANG)) +endif + # # Enable address sanitizer flags for kernel except some files or directories # we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE) --2.30.0.284.gd98b1dd5eaa7-goog
Signature · 2 lines
Thanks, ~Nick Desaulniers
PATCH v5 1/1 pgo: add clang's Profile Guided Optimization infrastructure
In reply to Bill WendlingFrom: Sami Tolvanen <[email protected]>
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
---
v2: - Added "__llvm_profile_instrument_memop" based on Nathan Chancellor's
testing.
- Corrected documentation, re PGO flags when using LTO, based on Fangrui Song's comments.
v3: - Added change log section based on Sedat Dilek's comments.
v4: - Remove non-x86 Makfile changes and se "hweight64" instead of using our
own popcount implementation, based on Nick Desaulniers's comment.
v5: - Correct padding calculation, discovered by Nathan Chancellor.
--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/pgo.rst | 127 +++++++++ MAINTAINERS | 9 + Makefile | 3 + arch/Kconfig | 1 + arch/x86/Kconfig | 1 + arch/x86/boot/Makefile | 1 + arch/x86/boot/compressed/Makefile | 1 + arch/x86/crypto/Makefile | 2 + arch/x86/entry/vdso/Makefile | 1 + arch/x86/kernel/vmlinux.lds.S | 2 + arch/x86/platform/efi/Makefile | 1 + arch/x86/purgatory/Makefile | 1 + arch/x86/realmode/rm/Makefile | 1 + arch/x86/um/vdso/Makefile | 1 + drivers/firmware/efi/libstub/Makefile | 1 + include/asm-generic/vmlinux.lds.h | 44 +++ kernel/Makefile | 1 + kernel/pgo/Kconfig | 35 +++ kernel/pgo/Makefile | 5 + kernel/pgo/fs.c | 382 ++++++++++++++++++++++++++ kernel/pgo/instrument.c | 185 +++++++++++++ kernel/pgo/pgo.h | 206 ++++++++++++++ scripts/Makefile.lib | 10 + 24 files changed, 1022 insertions(+) create mode 100644 Documentation/dev-tools/pgo.rst create mode 100644 kernel/pgo/Kconfig create mode 100644 kernel/pgo/Makefile create mode 100644 kernel/pgo/fs.c create mode 100644 kernel/pgo/instrument.c create mode 100644 kernel/pgo/pgo.h
Documentation/dev-tools/index.rst +1 -0
@@ -26,6 +26,7 @@ whole; patches welcome! kgdb kselftest kunit/index + pgo .. only:: subproject and html
Documentation/dev-tools/pgo.rst new file +127 -0
127 lines not on this page · raw message
MAINTAINERS +9 -0
@@ -13948,6 +13948,15 @@ S: Maintained F: include/linux/personality.h F: include/uapi/linux/personality.h +PGO BASED KERNEL PROFILING +M: Sami Tolvanen <[email protected]> +M: Bill Wendling <[email protected]> +R: Nathan Chancellor <[email protected]> +R: Nick Desaulniers <[email protected]> +S: Supported +F: Documentation/dev-tools/pgo.rst +F: kernel/pgo + PHOENIX RC FLIGHT CONTROLLER ADAPTER M: Marcus Folkesson <[email protected]> L: [email protected]
Makefile +3 -0
@@ -659,6 +659,9 @@ endif # KBUILD_EXTMOD # Defaults to vmlinux, but the arch makefile usually adds further targets all: vmlinux +CFLAGS_PGO_CLANG := -fprofile-generate +export CFLAGS_PGO_CLANG + CFLAGS_GCOV := -fprofile-arcs -ftest-coverage \ $(call cc-option,-fno-tree-loop-im) \ $(call cc-disable-warning,maybe-uninitialized,)
arch/Kconfig +1 -0
@@ -1112,6 +1112,7 @@ config ARCH_SPLIT_ARG64 pairs of 32-bit arguments, select this option. source "kernel/gcov/Kconfig" +source "kernel/pgo/Kconfig" source "scripts/gcc-plugins/Kconfig"
arch/x86/Kconfig +1 -0
@@ -96,6 +96,7 @@ config X86 select ARCH_SUPPORTS_DEBUG_PAGEALLOC select ARCH_SUPPORTS_NUMA_BALANCING if X86_64 select ARCH_SUPPORTS_KMAP_LOCAL_FORCE_MAP if NR_CPUS <= 4096 + select ARCH_SUPPORTS_PGO_CLANG if X86_64 select ARCH_USE_BUILTIN_BSWAP select ARCH_USE_QUEUED_RWLOCKS select ARCH_USE_QUEUED_SPINLOCKS
arch/x86/boot/Makefile +1 -0
@@ -71,6 +71,7 @@ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=) KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n $(obj)/bzImage: asflags-y := $(SVGA_MODE)
arch/x86/boot/compressed/Makefile +1 -0
@@ -54,6 +54,7 @@ CFLAGS_sev-es.o += -I$(objtree)/arch/x86/lib/ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE :=n KBUILD_LDFLAGS := -m elf_$(UTS_MACHINE)
arch/x86/crypto/Makefile +2 -0
@@ -4,6 +4,8 @@ OBJECT_FILES_NON_STANDARD := y +PGO_PROFILE_curve25519-x86_64.o := n + obj-$(CONFIG_CRYPTO_GLUE_HELPER_X86) += glue_helper.o obj-$(CONFIG_CRYPTO_TWOFISH_586) += twofish-i586.o
arch/x86/entry/vdso/Makefile +1 -0
@@ -179,6 +179,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 \ $(call ld-option, --eh-frame-hdr) -Bsymbolic GCOV_PROFILE := n +PGO_PROFILE := n quiet_cmd_vdso_and_check = VDSO $@ cmd_vdso_and_check = $(cmd_vdso); $(cmd_vdso_check)
arch/x86/kernel/vmlinux.lds.S +2 -0
@@ -184,6 +184,8 @@ SECTIONS BUG_TABLE + PGO_CLANG_DATA + ORC_UNWIND_TABLE . = ALIGN(PAGE_SIZE);
arch/x86/platform/efi/Makefile +1 -0
@@ -2,6 +2,7 @@ OBJECT_FILES_NON_STANDARD_efi_thunk_$(BITS).o := y KASAN_SANITIZE := n GCOV_PROFILE := n +PGO_PROFILE := n obj-$(CONFIG_EFI) += quirks.o efi.o efi_$(BITS).o efi_stub_$(BITS).o obj-$(CONFIG_EFI_MIXED) += efi_thunk_$(BITS).o
arch/x86/purgatory/Makefile +1 -0
@@ -23,6 +23,7 @@ targets += purgatory.ro purgatory.chk # Sanitizer, etc. runtimes are unavailable and cannot be linked here. GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n UBSAN_SANITIZE := n KCSAN_SANITIZE := n
arch/x86/realmode/rm/Makefile +1 -0
@@ -76,4 +76,5 @@ KBUILD_CFLAGS := $(REALMODE_CFLAGS) -D_SETUP -D_WAKEUP \ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n
arch/x86/um/vdso/Makefile +1 -0
@@ -64,6 +64,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -fPIC -shared -Wl,--hash-style=sysv GCOV_PROFILE := n +PGO_PROFILE := n # # Install the unstripped copy of vdso*.so listed in $(vdso-install-y).
drivers/firmware/efi/libstub/Makefile +1 -0
@@ -40,6 +40,7 @@ KBUILD_CFLAGS := $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS)) GCOV_PROFILE := n +PGO_PROFILE := n # Sanitizer runtimes are unavailable and cannot be linked here. KASAN_SANITIZE := n KCSAN_SANITIZE := n
include/asm-generic/vmlinux.lds.h +44 -0
56 lines not on this page · raw message
kernel/Makefile +1 -0
@@ -111,6 +111,7 @@ obj-$(CONFIG_BPF) += bpf/ obj-$(CONFIG_KCSAN) += kcsan/ obj-$(CONFIG_SHADOW_CALL_STACK) += scs.o obj-$(CONFIG_HAVE_STATIC_CALL_INLINE) += static_call.o +obj-$(CONFIG_PGO_CLANG) += pgo/ obj-$(CONFIG_PERF_EVENTS) += events/
kernel/pgo/Kconfig new file +35 -0
@@ -0,0 +1,35 @@ +# SPDX-License-Identifier: GPL-2.0-only +menu "Profile Guided Optimization (PGO) (EXPERIMENTAL)" + +config ARCH_SUPPORTS_PGO_CLANG + bool + +config PGO_CLANG + bool "Enable clang's PGO-based kernel profiling" + depends on DEBUG_FS + depends on ARCH_SUPPORTS_PGO_CLANG + depends on CC_IS_CLANG && CLANG_VERSION >= 120000 + help + This option enables clang's PGO (Profile Guided Optimization) based + code profiling to better optimize the kernel. + + If unsure, say N. + + Run a representative workload for your application on a kernel + compiled with this option and download the raw profile file from + /sys/kernel/debug/pgo/profraw. This file needs to be processed with + llvm-profdata. It may be merged with other collected raw profiles. + + Copy the resulting profile file into vmlinux.profdata, and enable + KCFLAGS=-fprofile-use=vmlinux.profdata to produce an optimized + kernel. + + Note that a kernel compiled with profiling flags will be + significantly larger and run slower. Also be sure to exclude files + from profiling which are not linked to the kernel image to prevent + linker errors. + + Note that the debugfs filesystem has to be mounted to access + profiling data. + +endmenu
kernel/pgo/Makefile new file +5 -0
@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +GCOV_PROFILE := n +PGO_PROFILE := n + +obj-y += fs.o instrument.o
kernel/pgo/fs.c new file +382 -0
382 lines not on this page · raw message
kernel/pgo/instrument.c new file +185 -0
185 lines not on this page · raw message
kernel/pgo/pgo.h new file +206 -0
206 lines not on this page · raw message
scripts/Makefile.lib +10 -0
@@ -143,6 +143,16 @@ _c_flags += $(if $(patsubst n%,, \ $(CFLAGS_GCOV)) endif +# +# Enable clang's PGO profiling flags for a file or directory depending on +# variables PGO_PROFILE_obj.o and PGO_PROFILE. +# +ifeq ($(CONFIG_PGO_CLANG),y) +_c_flags += $(if $(patsubst n%,, \ + $(PGO_PROFILE_$(basetarget).o)$(PGO_PROFILE)y), \ + $(CFLAGS_PGO_CLANG)) +endif + # # Enable address sanitizer flags for kernel except some files or directories # we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE)
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
Quoted 186 lines · Bill Wendling' via Clang Built Linux
From: Sami Tolvanen <[email protected]>
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.profrawMultiple 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 <[email protected]>Co-developed-by Bill Wendling <[email protected]>Signed-off-by Bill Wendling <[email protected]>---
v2: - Added "__llvm_profile_instrument_memop" based on Nathan Chancellor's
testing.
- Corrected documentation, re PGO flags when using LTO, based on Fangrui Song's comments.
v3: - Added change log section based on Sedat Dilek's comments.
v4: - Remove non-x86 Makfile changes and se "hweight64" instead of using our
own popcount implementation, based on Nick Desaulniers's comment.
v5: - Correct padding calculation, discovered by Nathan Chancellor.--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/pgo.rst | 127 +++++++++ MAINTAINERS | 9 + Makefile | 3 + arch/Kconfig | 1 + arch/x86/Kconfig | 1 + arch/x86/boot/Makefile | 1 + arch/x86/boot/compressed/Makefile | 1 + arch/x86/crypto/Makefile | 2 + arch/x86/entry/vdso/Makefile | 1 + arch/x86/kernel/vmlinux.lds.S | 2 + arch/x86/platform/efi/Makefile | 1 + arch/x86/purgatory/Makefile | 1 + arch/x86/realmode/rm/Makefile | 1 + arch/x86/um/vdso/Makefile | 1 + drivers/firmware/efi/libstub/Makefile | 1 + include/asm-generic/vmlinux.lds.h | 44 +++ kernel/Makefile | 1 + kernel/pgo/Kconfig | 35 +++ kernel/pgo/Makefile | 5 + kernel/pgo/fs.c | 382 ++++++++++++++++++++++++++ kernel/pgo/instrument.c | 185 +++++++++++++ kernel/pgo/pgo.h | 206 ++++++++++++++ scripts/Makefile.lib | 10 + 24 files changed, 1022 insertions(+) create mode 100644 Documentation/dev-tools/pgo.rst create mode 100644 kernel/pgo/Kconfig create mode 100644 kernel/pgo/Makefile create mode 100644 kernel/pgo/fs.c create mode 100644 kernel/pgo/instrument.c create mode 100644 kernel/pgo/pgo.hDocumentation/dev-tools/index.rst +1
-0@@ -26,6 +26,7 @@ whole; patches welcome! kgdb kselftest kunit/index + pgo .. only:: subproject and htmlDocumentation/dev-tools/pgo.rst new file +94
-0@@ -0,0 +1,127 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=============================== +Using PGO with the Linux kernel +=============================== + +Clang's profiling kernel support (PGO_) enables profiling of the Linux kernel +when building with Clang. The profiling data is exported via the ``pgo`` +debugfs directory. + +.. _PGO: https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization + + +Preparation +=========== + +Configure the kernel with: + +.. code-block:: make + + CONFIG_DEBUG_FS=y + CONFIG_PGO_CLANG=y + +Note that kernels compiled with profiling flags will be significantly larger +and run slower. + +Profiling data will only become accessible once debugfs has been mounted: + +.. code-block:: sh + + mount -t debugfs none /sys/kernel/debug + + +Customization +============= + +You can enable or disable profiling for individual file and directories by +adding a line similar to the following to the respective kernel Makefile: + +- For a single file (e.g. main.o) + + .. code-block:: make + + PGO_PROFILE_main.o := y + +- For all files in one directory + + .. code-block:: make + + PGO_PROFILE := y + +To exclude files from being profiled use + + .. code-block:: make + + PGO_PROFILE_main.o := n + +and + + .. code-block:: make + + PGO_PROFILE := n + +Only files which are linked to the main kernel image or are compiled as kernel +modules are supported by this mechanism. + + +Files +===== + +The PGO kernel support creates the following files in debugfs: + +``/sys/kernel/debug/pgo`` + Parent directory for all PGO-related files. + +``/sys/kernel/debug/pgo/reset`` + Global reset file: resets all coverage data to zero when written to. + +``/sys/kernel/debug/profraw`` + The raw PGO data that must be processed with ``llvm_profdata``. + + +Workflow +======== + +The PGO kernel can be run on the host or test machines. The data though should +be analyzed with Clang's tools from the same Clang version as the kernel was +compiled. Clang's tolerant of version skew, but it's easier to use the same +Clang version. + +The profiling data is useful for optimizing the kernel, analyzing coverage, +etc. Clang offers tools to perform these tasks. + +Here is an example workflow for profiling an instrumented kernel with PGO and
Documentation/dev-tools/pgo.rst line 104
Quoted 10 lines
Documentation/dev-tools/pgo.rst new file +10
-0+using the result to optimize the kernel: + +1) Install the kernel on the TEST machine. + +2) Reset the data counters right before running the load tests + + .. code-block:: sh + + $ echo 1 > /sys/kernel/debug/pgo/reset +
I do not get this...
# mount | grep debugfs debugfs on /sys/kernel/debug type debugfs (rw,nosuid,nodev,noexec,relatime)
After the load-test...?
echo 0 > /sys/kernel/debug/pgo/reset
Documentation/dev-tools/pgo.rst line 112
Quoted 8 lines
Documentation/dev-tools/pgo.rst new file +8
-0+3) Run the load tests. + +4) Collect the raw profile data + + .. code-block:: sh + + $ cp -a /sys/kernel/debug/pgo/profraw /tmp/vmlinux.profraw +
This is only 4,9M small and seen from the date 5mins before I run the echo-1 line.
# ll /sys/kernel/debug/pgo insgesamt 0 drwxr-xr-x 2 root root 0 16. Jan 17:29 . drwx------ 41 root root 0 16. Jan 17:29 .. -rw------- 1 root root 0 16. Jan 17:29 profraw --w------- 1 root root 0 16. Jan 18:19 reset
# cp -a /sys/kernel/debug/pgo/profraw /tmp/vmlinux.profraw
# ll /tmp/vmlinux.profraw -rw------- 1 root root 4,9M 16. Jan 17:29 /tmp/vmlinux.profraw
For me there was no prof-data collected from my defconfig kernel-build.
Documentation/dev-tools/pgo.rst line 120
Quoted 8 lines
Documentation/dev-tools/pgo.rst new file +8
-0+5) (Optional) Download the raw profile data to the HOST machine. + +6) Process the raw profile data + + .. code-block:: sh + + $ llvm-profdata merge --output=vmlinux.profdata vmlinux.profraw +
Is that executed in /path/to/linux/git?
Documentation/dev-tools/pgo.rst line 127
Quoted 7 lines
Documentation/dev-tools/pgo.rst new file +7
-0+ Note that multiple raw profile data files can be merged during this step. + +7) Rebuild the kernel using the profile data (PGO disabled) + + .. code-block:: sh + + $ make LLVM=1 KCFLAGS=-fprofile-use=vmlinux.profdata ...
How big is vmlinux.profdata (make defconfig)?
Do I need to do a full defconfig build or can I stop the build after let me say 10mins?
- Sedat -
Quoted 1,123 lines
MAINTAINERS +9
-0@@ -13948,6 +13948,15 @@ S: Maintained F: include/linux/personality.h F: include/uapi/linux/personality.h +PGO BASED KERNEL PROFILING +M: Sami Tolvanen <[email protected]> +M: Bill Wendling <[email protected]> +R: Nathan Chancellor <[email protected]> +R: Nick Desaulniers <[email protected]> +S: Supported +F: Documentation/dev-tools/pgo.rst +F: kernel/pgo + PHOENIX RC FLIGHT CONTROLLER ADAPTER M: Marcus Folkesson <[email protected]> L: [email protected]Makefile +3
-0@@ -659,6 +659,9 @@ endif # KBUILD_EXTMOD # Defaults to vmlinux, but the arch makefile usually adds further targets all: vmlinux +CFLAGS_PGO_CLANG := -fprofile-generate +export CFLAGS_PGO_CLANG + CFLAGS_GCOV := -fprofile-arcs -ftest-coverage \ $(call cc-option,-fno-tree-loop-im) \ $(call cc-disable-warning,maybe-uninitialized,)arch/Kconfig +1
-0@@ -1112,6 +1112,7 @@ config ARCH_SPLIT_ARG64 pairs of 32-bit arguments, select this option. source "kernel/gcov/Kconfig" +source "kernel/pgo/Kconfig" source "scripts/gcc-plugins/Kconfig"arch/x86/Kconfig +1
-0@@ -96,6 +96,7 @@ config X86 select ARCH_SUPPORTS_DEBUG_PAGEALLOC select ARCH_SUPPORTS_NUMA_BALANCING if X86_64 select ARCH_SUPPORTS_KMAP_LOCAL_FORCE_MAP if NR_CPUS <= 4096 + select ARCH_SUPPORTS_PGO_CLANG if X86_64 select ARCH_USE_BUILTIN_BSWAP select ARCH_USE_QUEUED_RWLOCKS select ARCH_USE_QUEUED_SPINLOCKSarch/x86/boot/Makefile +1
-0@@ -71,6 +71,7 @@ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=) KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := n $(obj)/bzImage: asflags-y := $(SVGA_MODE)arch/x86/boot/compressed/Makefile +1
-0@@ -54,6 +54,7 @@ CFLAGS_sev-es.o += -I$(objtree)/arch/x86/lib/ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE :=n KBUILD_LDFLAGS := -m elf_$(UTS_MACHINE)arch/x86/crypto/Makefile +2
-0@@ -4,6 +4,8 @@ OBJECT_FILES_NON_STANDARD := y +PGO_PROFILE_curve25519-x86_64.o := n + obj-$(CONFIG_CRYPTO_GLUE_HELPER_X86) += glue_helper.o obj-$(CONFIG_CRYPTO_TWOFISH_586) += twofish-i586.oarch/x86/entry/vdso/Makefile +1
-0@@ -179,6 +179,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 \ $(call ld-option, --eh-frame-hdr) -Bsymbolic GCOV_PROFILE := n +PGO_PROFILE := n quiet_cmd_vdso_and_check = VDSO $@ cmd_vdso_and_check = $(cmd_vdso); $(cmd_vdso_check)arch/x86/kernel/vmlinux.lds.S +2
-0@@ -184,6 +184,8 @@ SECTIONS BUG_TABLE + PGO_CLANG_DATA + ORC_UNWIND_TABLE . = ALIGN(PAGE_SIZE);arch/x86/platform/efi/Makefile +1
-0@@ -2,6 +2,7 @@ OBJECT_FILES_NON_STANDARD_efi_thunk_$(BITS).o := y KASAN_SANITIZE := n GCOV_PROFILE := n +PGO_PROFILE := n obj-$(CONFIG_EFI) += quirks.o efi.o efi_$(BITS).o efi_stub_$(BITS).o obj-$(CONFIG_EFI_MIXED) += efi_thunk_$(BITS).oarch/x86/purgatory/Makefile +1
-0@@ -23,6 +23,7 @@ targets += purgatory.ro purgatory.chk # Sanitizer, etc. runtimes are unavailable and cannot be linked here. GCOV_PROFILE := n +PGO_PROFILE := n KASAN_SANITIZE := n UBSAN_SANITIZE := n KCSAN_SANITIZE := narch/x86/realmode/rm/Makefile +1
-0@@ -76,4 +76,5 @@ KBUILD_CFLAGS := $(REALMODE_CFLAGS) -D_SETUP -D_WAKEUP \ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += -fno-asynchronous-unwind-tables GCOV_PROFILE := n +PGO_PROFILE := n UBSAN_SANITIZE := narch/x86/um/vdso/Makefile +1
-0@@ -64,6 +64,7 @@ quiet_cmd_vdso = VDSO $@ VDSO_LDFLAGS = -fPIC -shared -Wl,--hash-style=sysv GCOV_PROFILE := n +PGO_PROFILE := n # # Install the unstripped copy of vdso*.so listed in $(vdso-install-y).drivers/firmware/efi/libstub/Makefile +1
-0@@ -40,6 +40,7 @@ KBUILD_CFLAGS := $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS)) GCOV_PROFILE := n +PGO_PROFILE := n # Sanitizer runtimes are unavailable and cannot be linked here. KASAN_SANITIZE := n KCSAN_SANITIZE := ninclude/asm-generic/vmlinux.lds.h +44
-0@@ -316,6 +316,49 @@#define THERMAL_TABLE (name)#endif +#ifdef CONFIG_PGO_CLANG +#define PGO_CLANG_DATA \ + __llvm_prf_data : AT(ADDR(__llvm_prf_data) - LOAD_OFFSET) { \ + . = ALIGN(8); \ + __llvm_prf_start = .; \ + __llvm_prf_data_start = .; \ + KEEP(*(__llvm_prf_data)) \ + . = ALIGN(8); \ + __llvm_prf_data_end = .; \ + } \ + __llvm_prf_cnts : AT(ADDR(__llvm_prf_cnts) - LOAD_OFFSET) { \ + . = ALIGN(8); \ + __llvm_prf_cnts_start = .; \ + KEEP(*(__llvm_prf_cnts)) \ + . = ALIGN(8); \ + __llvm_prf_cnts_end = .; \ + } \ + __llvm_prf_names : AT(ADDR(__llvm_prf_names) - LOAD_OFFSET) { \ + . = ALIGN(8); \ + __llvm_prf_names_start = .; \ + KEEP(*(__llvm_prf_names)) \ + . = ALIGN(8); \ + __llvm_prf_names_end = .; \ + . = ALIGN(8); \ + } \ + __llvm_prf_vals : AT(ADDR(__llvm_prf_vals) - LOAD_OFFSET) { \ + __llvm_prf_vals_start = .; \ + KEEP(*(__llvm_prf_vals)) \ + . = ALIGN(8); \ + __llvm_prf_vals_end = .; \ + . = ALIGN(8); \ + } \ + __llvm_prf_vnds : AT(ADDR(__llvm_prf_vnds) - LOAD_OFFSET) { \ + __llvm_prf_vnds_start = .; \ + KEEP(*(__llvm_prf_vnds)) \ + . = ALIGN(8); \ + __llvm_prf_vnds_end = .; \ + __llvm_prf_end = .; \ + } +#else +#define PGO_CLANG_DATA +#endif +#define KERNEL_DTB () \ STRUCT_ALIGN(); \ __dtb_start = .; \@@ -1125,6 +1168,7 @@ CONSTRUCTORS \ } \BUG_TABLE \ + PGO_CLANG_DATA#define INIT_TEXT_SECTION (inittext_align) \ . = ALIGN(inittext_align); \kernel/Makefile +1
-0@@ -111,6 +111,7 @@ obj-$(CONFIG_BPF) += bpf/ obj-$(CONFIG_KCSAN) += kcsan/ obj-$(CONFIG_SHADOW_CALL_STACK) += scs.o obj-$(CONFIG_HAVE_STATIC_CALL_INLINE) += static_call.o +obj-$(CONFIG_PGO_CLANG) += pgo/ obj-$(CONFIG_PERF_EVENTS) += events/kernel/pgo/Kconfig new file +35
-0@@ -0,0 +1,35 @@ +# SPDX-License-Identifier: GPL-2.0-only +menu "Profile Guided Optimization (PGO) (EXPERIMENTAL)" + +config ARCH_SUPPORTS_PGO_CLANG + bool + +config PGO_CLANG + bool "Enable clang's PGO-based kernel profiling" + depends on DEBUG_FS + depends on ARCH_SUPPORTS_PGO_CLANG + depends on CC_IS_CLANG && CLANG_VERSION >= 120000 + help + This option enables clang's PGO (Profile Guided Optimization) based + code profiling to better optimize the kernel. + + If unsure, say N. + + Run a representative workload for your application on a kernel + compiled with this option and download the raw profile file from + /sys/kernel/debug/pgo/profraw. This file needs to be processed with + llvm-profdata. It may be merged with other collected raw profiles. + + Copy the resulting profile file into vmlinux.profdata, and enable + KCFLAGS=-fprofile-use=vmlinux.profdata to produce an optimized + kernel. + + Note that a kernel compiled with profiling flags will be + significantly larger and run slower. Also be sure to exclude files + from profiling which are not linked to the kernel image to prevent + linker errors. + + Note that the debugfs filesystem has to be mounted to access + profiling data. + +endmenukernel/pgo/Makefile new file +5
-0@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +GCOV_PROFILE := n +PGO_PROFILE := n + +obj-y += fs.o instrument.okernel/pgo/fs.c new file +382
-0@@ -0,0 +1,382 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2019 Google, Inc. +* +* Author: +* Sami Tolvanen <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#define pr_fmt (fmt) "pgo: " fmt + +#include <linux/kernel.h> +#include <linux/debugfs.h> +#include <linux/fs.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/vmalloc.h> +#include "pgo.h" + +static struct dentry *directory; + +struct prf_private_data { +void *buffer; +unsigned long size; +}; + +/* +* Raw profile data format: +* +* - llvm_prf_header +* - __llvm_prf_data +* - __llvm_prf_cnts +* - __llvm_prf_names +* - zero padding to 8 bytes +* - for each llvm_prf_data in __llvm_prf_data: +* - llvm_prf_value_data +* - llvm_prf_value_record + site count array +* - llvm_prf_value_node_data +* ... +* ... +* ... +*/ + +static void prf_fill_header (void **buffer) +{ +struct llvm_prf_header *header = *(struct llvm_prf_header **)buffer; + + header->magic =LLVM_PRF_MAGIC ; + header->version =LLVM_PRF_VARIANT_MASK_IR |LLVM_PRF_VERSION ; + header->data_size =prf_data_count (); + header->padding_bytes_before_counters =0 ; + header->counters_size =prf_cnts_count (); + header->padding_bytes_after_counters =0 ; + header->names_size =prf_names_count (); + header->counters_delta = (u64 )__llvm_prf_cnts_start; + header->names_delta = (u64 )__llvm_prf_names_start; + header->value_kind_last =LLVM_PRF_IPVK_LAST ; + + *buffer +=sizeof (*header); +} + +/* +* Copy the source into the buffer, incrementing the pointer into buffer in the +* process. +*/ +static void prf_copy_to_buffer (void **buffer,void *src,unsigned long size) +{ +memcpy (*buffer, src, size); + *buffer += size; +} + +static u32 __prf_get_value_size (struct llvm_prf_data *p,u32 *value_kinds) +{ +struct llvm_prf_value_node **nodes = + (struct llvm_prf_value_node **)p->values; +u32 kinds =0 ; +u32 size =0 ; +unsigned int kind; +unsigned int n; +unsigned int s =0 ; + +for (kind =0 ; kind <ARRAY_SIZE (p->num_value_sites); kind++) { +unsigned int sites = p->num_value_sites[kind]; + +if (!sites) +continue ; + +/* Record + site count array */ + size +=prf_get_value_record_size (sites); + kinds++; + +if (!nodes) +continue ; + +for (n =0 ; n < sites; n++) { +u32 count =0 ; +struct llvm_prf_value_node *site = nodes[s + n]; + +while (site && ++count <=U8_MAX ) + site = site->next; + + size += count * +sizeof (struct llvm_prf_value_node_data ); + } + + s += sites; + } + +if (size) + size +=sizeof (struct llvm_prf_value_data ); + +if (value_kinds) + *value_kinds = kinds; + +return size; +} + +static u32 prf_get_value_size (void ) +{ +u32 size =0 ; +struct llvm_prf_data *p; + +for (p = __llvm_prf_data_start; p < __llvm_prf_data_end; p++) + size +=__prf_get_value_size (p,NULL ); + +return size; +} + +/* Serialize the profiling's value. */ +static void prf_serialize_value (struct llvm_prf_data *p,void **buffer) +{ +struct llvm_prf_value_data header; +struct llvm_prf_value_node **nodes = + (struct llvm_prf_value_node **)p->values; +unsigned int kind; +unsigned int n; +unsigned int s =0 ; + + header.total_size =__prf_get_value_size (p, &header.num_value_kinds); + +if (!header.num_value_kinds) +/* Nothing to write. */ +return ; + +prf_copy_to_buffer (buffer, &header,sizeof (header)); + +for (kind =0 ; kind <ARRAY_SIZE (p->num_value_sites); kind++) { +struct llvm_prf_value_record *record; +u8 *counts; +unsigned int sites = p->num_value_sites[kind]; + +if (!sites) +continue ; + +/* Profiling value record. */ + record = *(struct llvm_prf_value_record **)buffer; + *buffer +=prf_get_value_record_header_size (); + + record->kind = kind; + record->num_value_sites = sites; + +/* Site count array. */ + counts = *(u8 **)buffer; + *buffer +=prf_get_value_record_site_count_size (sites); + +/* +* If we don't have nodes, we can skip updating the site count +* array, because the buffer is zero filled. +*/ +if (!nodes) +continue ; + +for (n =0 ; n < sites; n++) { +u32 count =0 ; +struct llvm_prf_value_node *site = nodes[s + n]; + +while (site && ++count <=U8_MAX ) { +prf_copy_to_buffer (buffer, site, +sizeof (struct llvm_prf_value_node_data )); + site = site->next; + } + + counts[n] = (u8 )count; + } + + s += sites; + } +} + +static void prf_serialize_values (void **buffer) +{ +struct llvm_prf_data *p; + +for (p = __llvm_prf_data_start; p < __llvm_prf_data_end; p++) +prf_serialize_value (p, buffer); +} + +static inline unsigned long prf_get_padding (unsigned long size) +{ +return 7 & (8 - size %8 ); +} + +static unsigned long prf_buffer_size (void ) +{ +return sizeof (struct llvm_prf_header ) + +prf_data_size () + +prf_cnts_size () + +prf_names_size () + +prf_get_padding (prf_names_size ()) + +prf_get_value_size (); +} + +/* Serialize the profiling data into a format LLVM's tools can understand. */ +static int prf_serialize (struct prf_private_data *p) +{ +int err =0 ; +void *buffer; + + p->size =prf_buffer_size (); + p->buffer =vzalloc (p->size); + +if (!p->buffer) { + err = -ENOMEM ; + goto out; + } + + buffer = p->buffer; + +prf_fill_header (&buffer); +prf_copy_to_buffer (&buffer, __llvm_prf_data_start,prf_data_size ()); +prf_copy_to_buffer (&buffer, __llvm_prf_cnts_start,prf_cnts_size ()); +prf_copy_to_buffer (&buffer, __llvm_prf_names_start,prf_names_size ()); + buffer +=prf_get_padding (prf_names_size ()); + +prf_serialize_values (&buffer); + +out: +return err; +} + +/* open() implementation for PGO. Creates a copy of the profiling data set. */ +static int prf_open (struct inode *inode,struct file *file) +{ +struct prf_private_data *data; +unsigned long flags; +int err; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) { + err = -ENOMEM ; + goto out; + } + + flags =prf_lock (); + + err =prf_serialize (data); +if (err) { +kfree (data); + goto out_unlock; + } + + file->private_data = data; + +out_unlock: +prf_unlock (flags); +out: +return err; +} + +/* read() implementation for PGO. */ +static ssize_t prf_read (struct file *file,char __user *buf,size_t count, +loff_t *ppos) +{ +struct prf_private_data *data = file->private_data; + +BUG_ON (!data); + +return simple_read_from_buffer (buf, count, ppos, data->buffer, + data->size); +} + +/* release() implementation for PGO. Release resources allocated by open(). */ +static int prf_release (struct inode *inode,struct file *file) +{ +struct prf_private_data *data = file->private_data; + +if (data) { +vfree (data->buffer); +kfree (data); + } + +return 0 ; +} + +static const struct file_operations prf_fops = { + .owner =THIS_MODULE , + .open = prf_open, + .read = prf_read, + .llseek = default_llseek, + .release = prf_release +}; + +/* write() implementation for resetting PGO's profile data. */ +static ssize_t reset_write (struct file *file,const char __user *addr, +size_t len,loff_t *pos) +{ +struct llvm_prf_data *data; + +memset (__llvm_prf_cnts_start,0 ,prf_cnts_size ()); + +for (data = __llvm_prf_data_start; data < __llvm_prf_data_end; ++data) { +struct llvm_prf_value_node **vnodes; +u64 current_vsite_count; +u32 i; + +if (!data->values) +continue ; + + current_vsite_count =0 ; + vnodes = (struct llvm_prf_value_node **)data->values; + +for (i =LLVM_PRF_IPVK_FIRST ; i <=LLVM_PRF_IPVK_LAST ; ++i) + current_vsite_count += data->num_value_sites[i]; + +for (i =0 ; i < current_vsite_count; ++i) { +struct llvm_prf_value_node *current_vnode = vnodes[i]; + +while (current_vnode) { + current_vnode->count =0 ; + current_vnode = current_vnode->next; + } + } + } + +return len; +} + +static const struct file_operations prf_reset_fops = { + .owner =THIS_MODULE , + .write = reset_write, + .llseek = noop_llseek, +}; + +/* Create debugfs entries. */ +static int __init pgo_init (void ) +{ + directory =debugfs_create_dir ("pgo" ,NULL ); +if (!directory) + goto err_remove; + +if (!debugfs_create_file ("profraw" ,0600 , directory,NULL , + &prf_fops)) + goto err_remove; + +if (!debugfs_create_file ("reset" ,0200 , directory,NULL , + &prf_reset_fops)) + goto err_remove; + +return 0 ; + +err_remove: +pr_err ("initialization failed\n" ); +return -EIO ; +} + +/* Remove debugfs entries. */ +static void __exit pgo_exit(void) +{ +debugfs_remove_recursive (directory); +} + +module_init (pgo_init); +module_exit (pgo_exit);kernel/pgo/instrument.c new file +185
-0@@ -0,0 +1,185 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2019 Google, Inc. +* +* Author: +* Sami Tolvanen <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#define pr_fmt (fmt) "pgo: " fmt + +#include <linux/bitops.h> +#include <linux/kernel.h> +#include <linux/export.h> +#include <linux/spinlock.h> +#include <linux/types.h> +#include "pgo.h" + +/* Lock guarding value node access and serialization. */ +static DEFINE_SPINLOCK (pgo_lock); +static int current_node; + +unsigned long prf_lock (void ) +{ +unsigned long flags; + +spin_lock_irqsave (&pgo_lock, flags); + +return flags; +} + +void prf_unlock (unsigned long flags) +{ +spin_unlock_irqrestore (&pgo_lock, flags); +} + +/* +* Return a newly allocated profiling value node which contains the tracked +* value by the value profiler. +* Note: caller *must* hold pgo_lock. +*/ +static struct llvm_prf_value_node *allocate_node (struct llvm_prf_data *p, +u32 index,u64 value) +{ +if (&__llvm_prf_vnds_start[current_node +1 ] >= __llvm_prf_vnds_end) +return NULL ;/* Out of nodes */ + + current_node++; + +/* Make sure the node is entirely within the section */ +if (&__llvm_prf_vnds_start[current_node] >= __llvm_prf_vnds_end || + &__llvm_prf_vnds_start[current_node +1 ] > __llvm_prf_vnds_end) +return NULL ; + +return &__llvm_prf_vnds_start[current_node]; +} + +/* +* Counts the number of times a target value is seen. +* +* Records the target value for the CounterIndex if not seen before. Otherwise, +* increments the counter associated w/ the target value. +*/ +void __llvm_profile_instrument_target (u64 target_value,void *data,u32 index); +void __llvm_profile_instrument_target (u64 target_value,void *data,u32 index) +{ +struct llvm_prf_data *p = (struct llvm_prf_data *)data; +struct llvm_prf_value_node **counters; +struct llvm_prf_value_node *curr; +struct llvm_prf_value_node *min =NULL ; +struct llvm_prf_value_node *prev =NULL ; +u64 min_count =U64_MAX ; +u8 values =0 ; +unsigned long flags; + +if (!p || !p->values) +return ; + + counters = (struct llvm_prf_value_node **)p->values; + curr = counters[index]; + +while (curr) { +if (target_value == curr->value) { + curr->count++; +return ; + } + +if (curr->count < min_count) { + min_count = curr->count; + min = curr; + } + + prev = curr; + curr = curr->next; + values++; + } + +if (values >=LLVM_PRF_MAX_NUM_VALS_PER_SITE ) { +if (!min->count || !(--min->count)) { + curr = min; + curr->value = target_value; + curr->count++; + } +return ; + } + +/* Lock when updating the value node structure. */ + flags =prf_lock (); + + curr =allocate_node (p, index, target_value); +if (!curr) + goto out; + + curr->value = target_value; + curr->count++; + +if (!counters[index]) + counters[index] = curr; +else if (prev && !prev->next) + prev->next = curr; + +out: +prf_unlock (flags); +} +EXPORT_SYMBOL (__llvm_profile_instrument_target); + +/* Counts the number of times a range of targets values are seen. */ +void __llvm_profile_instrument_range (u64 target_value,void *data, +u32 index,s64 precise_start, +s64 precise_last,s64 large_value); +void __llvm_profile_instrument_range (u64 target_value,void *data, +u32 index,s64 precise_start, +s64 precise_last,s64 large_value) +{ +if (large_value !=S64_MIN && (s64 )target_value >= large_value) + target_value = large_value; +else if ((s64 )target_value < precise_start || + (s64 )target_value > precise_last) + target_value = precise_last +1 ; + +__llvm_profile_instrument_target (target_value, data, index); +} +EXPORT_SYMBOL (__llvm_profile_instrument_range); + +static u64 inst_prof_get_range_rep_value (u64 value) +{ +if (value <=8 ) +/* The first ranges are individually tracked, us it as is. */ +return value; +else if (value >=513 ) +/* The last range is mapped to its lowest value. */ +return 513 ; +else if (hweight64 (value) ==1 ) +/* If it's a power of two, use it as is. */ +return value; + +/* Otherwise, take to the previous power of two + 1. */ +return (1 << (64 -__builtin_clzll (value) -1 )) +1 ; +} + +/* +* The target values are partitioned into multiple ranges. The range spec is +* defined in compiler-rt/include/profile/InstrProfData.inc. +*/ +void __llvm_profile_instrument_memop (u64 target_value,void *data, +u32 counter_index); +void __llvm_profile_instrument_memop (u64 target_value,void *data, +u32 counter_index) +{ +u64 rep_value; + +/* Map the target value to the representative value of its range. */ + rep_value =inst_prof_get_range_rep_value (target_value); +__llvm_profile_instrument_target (rep_value, data, counter_index); +} +EXPORT_SYMBOL (__llvm_profile_instrument_memop);kernel/pgo/pgo.h new file +206
-0@@ -0,0 +1,206 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2019 Google, Inc. +* +* Author: +* Sami Tolvanen <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef _PGO_H +#define _PGO_H + +/* +* Note: These internal LLVM definitions must match the compiler version. +* See llvm/include/llvm/ProfileData/InstrProfData.inc in LLVM's source code. +*/ + +#ifdef CONFIG_64BIT +#define LLVM_PRF_MAGIC \ + ((u64)255 << 56 | \ + (u64)'l' << 48 | \ + (u64)'p' << 40 | \ + (u64)'r' << 32 | \ + (u64)'o' << 24 | \ + (u64)'f' << 16 | \ + (u64)'r' << 8 | \ + (u64)129) +#else +#define LLVM_PRF_MAGIC \ + ((u64)255 << 56 | \ + (u64)'l' << 48 | \ + (u64)'p' << 40 | \ + (u64)'r' << 32 | \ + (u64)'o' << 24 | \ + (u64)'f' << 16 | \ + (u64)'R' << 8 | \ + (u64)129) +#endif + +#define LLVM_PRF_VERSION 5 +#define LLVM_PRF_DATA_ALIGN 8 +#define LLVM_PRF_IPVK_FIRST 0 +#define LLVM_PRF_IPVK_LAST 1 +#define LLVM_PRF_MAX_NUM_VALS_PER_SITE 16 + +#define LLVM_PRF_VARIANT_MASK_IR (0x1ull << 56) +#define LLVM_PRF_VARIANT_MASK_CSIR (0x1ull << 57) + +/** +* struct llvm_prf_header - represents the raw profile header data structure. +* @magic: the magic token for the file format. +* @version: the version of the file format. +* @data_size: the number of entries in the profile data section. +* @padding_bytes_before_counters: the number of padding bytes before the +* counters. +* @counters_size: the size in bytes of the LLVM profile section containing the +* counters. +* @padding_bytes_after_counters: the number of padding bytes after the +* counters. +* @names_size: the size in bytes of the LLVM profile section containing the +* counters' names. +* @counters_delta: the beginning of the LLMV profile counters section. +* @names_delta: the beginning of the LLMV profile names section. +* @value_kind_last: the last profile value kind. +*/ +struct llvm_prf_header { +u64 magic; +u64 version; +u64 data_size; +u64 padding_bytes_before_counters; +u64 counters_size; +u64 padding_bytes_after_counters; +u64 names_size; +u64 counters_delta; +u64 names_delta; +u64 value_kind_last; +}; + +/** +* struct llvm_prf_data - represents the per-function control structure. +* @name_ref: the reference to the function's name. +* @func_hash: the hash value of the function. +* @counter_ptr: a pointer to the profile counter. +* @function_ptr: a pointer to the function. +* @values: the profiling values associated with this function. +* @num_counters: the number of counters in the function. +* @num_value_sites: the number of value profile sites. +*/ +struct llvm_prf_data { +const u64 name_ref; +const u64 func_hash; +const void *counter_ptr; +const void *function_ptr; +void *values; +const u32 num_counters; +const u16 num_value_sites[LLVM_PRF_IPVK_LAST +1 ]; +}__aligned(LLVM_PRF_DATA_ALIGN) ; + +/** +* structure llvm_prf_value_node_data - represents the data part of the struct +* llvm_prf_value_node data structure. +* @value: the value counters. +* @count: the counters' count. +*/ +struct llvm_prf_value_node_data { +u64 value; +u64 count; +}; + +/** +* struct llvm_prf_value_node - represents an internal data structure used by +* the value profiler. +* @value: the value counters. +* @count: the counters' count. +* @next: the next value node. +*/ +struct llvm_prf_value_node { +u64 value; +u64 count; +struct llvm_prf_value_node *next; +}; + +/** +* struct llvm_prf_value_data - represents the value profiling data in indexed +* format. +* @total_size: the total size in bytes including this field. +* @num_value_kinds: the number of value profile kinds that has value profile +* data. +*/ +struct llvm_prf_value_data { +u32 total_size; +u32 num_value_kinds; +}; + +/** +* struct llvm_prf_value_record - represents the on-disk layout of the value +* profile data of a particular kind for one function. +* @kind: the kind of the value profile record. +* @num_value_sites: the number of value profile sites. +* @site_count_array: the first element of the array that stores the number +* of profiled values for each value site. +*/ +struct llvm_prf_value_record { +u32 kind; +u32 num_value_sites; +u8 site_count_array[]; +}; + +#define prf_get_value_record_header_size () \ + offsetof(struct llvm_prf_value_record, site_count_array) +#define prf_get_value_record_site_count_size (sites) \ + roundup((sites), 8) +#define prf_get_value_record_size (sites) \ + (prf_get_value_record_header_size() + \ + prf_get_value_record_site_count_size((sites))) + +/* Data sections */ +extern struct llvm_prf_data __llvm_prf_data_start[]; +extern struct llvm_prf_data __llvm_prf_data_end[]; + +extern u64 __llvm_prf_cnts_start[]; +extern u64 __llvm_prf_cnts_end[]; + +extern char __llvm_prf_names_start[]; +extern char __llvm_prf_names_end[]; + +extern struct llvm_prf_value_node __llvm_prf_vnds_start[]; +extern struct llvm_prf_value_node __llvm_prf_vnds_end[]; + +/* Locking for vnodes */ +extern unsigned long prf_lock (void ); +extern void prf_unlock (unsigned long flags); + +#define __DEFINE_PRF_SIZE (s) \ + static inline unsigned long prf_ ## s ## _size(void) \ + { \ + unsigned long start = \ + (unsigned long)__llvm_prf_ ## s ## _start; \ + unsigned long end = \ + (unsigned long)__llvm_prf_ ## s ## _end; \ + return roundup(end - start, \ + sizeof(__llvm_prf_ ## s ## _start[0])); \ + } \ + static inline unsigned long prf_ ## s ## _count(void) \ + { \ + return prf_ ## s ## _size() / \ + sizeof(__llvm_prf_ ## s ## _start[0]); \ + } + +__DEFINE_PRF_SIZE (data); +__DEFINE_PRF_SIZE (cnts); +__DEFINE_PRF_SIZE (names); +__DEFINE_PRF_SIZE (vnds); + +#undef __DEFINE_PRF_SIZE + +#endif /* _PGO_H */ scripts/Makefile.lib +10
-1@@ -143,6 +143,16 @@ _c_flags += $(if $(patsubst n%,, \ $(CFLAGS_GCOV)) endif +# +# Enable clang's PGO profiling flags for a file or directory depending on +# variables PGO_PROFILE_obj.o and PGO_PROFILE. +# +ifeq ($(CONFIG_PGO_CLANG),y) +_c_flags += $(if $(patsubst n%,, \ + $(PGO_PROFILE_$(basetarget).o)$(PGO_PROFILE)y), \ + $(CFLAGS_PGO_CLANG)) +endif + # # Enable address sanitizer flags for kernel except some files or directories # we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE) --2.30.0.284.gd98b1dd5eaa7-goog
--
You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/20210116094357.3620352-1-morbo%40google.com.