* [PATCH v7 0/7] Add AutoFDO and Propeller support for Clang build
@ 2024-11-02 17:51 Rong Xu
2024-11-02 17:51 ` [PATCH v7 1/7] Add AutoFDO " Rong Xu
` (7 more replies)
0 siblings, 8 replies; 30+ messages in thread
From: Rong Xu @ 2024-11-02 17:51 UTC (permalink / raw)
To: Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Rong Xu, Sami Tolvanen,
Thomas Gleixner, Wei Yang, workflows, Miguel Ojeda,
Maksim Panchenko, David S. Miller, Andreas Larsson, Yonghong Song,
Yabin Cui, Krzysztof Pszeniczny, Sriraman Tallam,
Stephane Eranian
Cc: x86, linux-arch, sparclinux, linux-doc, linux-kbuild,
linux-kernel, llvm
Hi,
This patch series is to integrate AutoFDO and Propeller support into
the Linux kernel. AutoFDO is a profile-guided optimization technique
that leverages hardware sampling to enhance binary performance.
Unlike Instrumentation-based FDO (iFDO), AutoFDO offers a user-friendly
and straightforward application process. While iFDO generally yields
superior profile quality and performance, our findings reveal that
AutoFDO achieves remarkable effectiveness, bringing performance close
to iFDO for benchmark applications.
Propeller is a profile-guided, post-link optimizer that improves
the performance of large-scale applications compiled with LLVM. It
operates by relinking the binary based on an additional round of runtime
profiles, enabling precise optimizations that are not possible at
compile time. Similar to AutoFDO, Propeller too utilizes hardware
sampling to collect profiles and apply post-link optimizations to improve
the benchmark’s performance over and above AutoFDO.
Our empirical data demonstrates significant performance improvements
with AutoFDO and Propeller, up to 10% on microbenchmarks and up to 5%
on large warehouse-scale benchmarks. This makes a strong case for their
inclusion as supported features in the upstream kernel.
Background
A significant fraction of fleet processing cycles (excluding idle time)
from data center workloads are attributable to the kernel. Ware-house
scale workloads maximize performance by optimizing the production kernel
using iFDO (a.k.a instrumented PGO, Profile Guided Optimization).
iFDO can significantly enhance application performance but its use
within the kernel has raised concerns. AutoFDO is a variant of FDO that
uses the hardware’s Performance Monitoring Unit (PMU) to collect
profiling data. While AutoFDO typically yields smaller performance
gains than iFDO, it presents unique benefits for optimizing kernels.
AutoFDO eliminates the need for instrumented kernels, allowing a single
optimized kernel to serve both execution and profile collection. It also
minimizes slowdown during profile collection, potentially yielding
higher-fidelity profiling, especially for time-sensitive code, compared
to iFDO. Additionally, AutoFDO profiles can be obtained from production
environments via the hardware’s PMU whereas iFDO profiles require
carefully curated load tests that are representative of real-world
traffic.
AutoFDO facilitates profile collection across diverse targets.
Preliminary studies indicate significant variation in kernel hot spots
within Google’s infrastructure, suggesting potential performance gains
through target-specific kernel customization.
Furthermore, other advanced compiler optimization techniques, including
ThinLTO and Propeller can be stacked on top of AutoFDO, similar to iFDO.
ThinLTO achieves better runtime performance through whole-program
analysis and cross module optimizations. The main difference between
traditional LTO and ThinLTO is that the latter is scalable in time and
memory.
This patch series adds AutoFDO and Propeller support to the kernel. The
actual solution comes in six parts:
[P 1] Add the build support for using AutoFDO in Clang
Add the basic support for AutoFDO build and provide the
instructions for using AutoFDO.
[P 2] Fix objtool for bogus warnings when -ffunction-sections is enabled
[P 3] Adjust symbol ordering in text output sections
[P 4] Add markers for text_unlikely and text_hot sections
[P 5] Enable –ffunction-sections for the AutoFDO build
[P 6] Enable Machine Function Split (MFS) optimization for AutoFDO
[P 7] Add Propeller configuration to the kernel build
Patch 1 provides basic AutoFDO build support. Patches 2 to 6 further
enhance the performance of AutoFDO builds and are functionally dependent
on Patch 1. Patch 7 enables support for Propeller and is dependent on
patch 2 to patch 4.
Caveats
AutoFDO is compatible with both GCC and Clang, but the patches in this
series are exclusively applicable to LLVM 17 or newer for AutoFDO and
LLVM 19 or newer for Propeller. For profile conversion, two different
tools could be used, llvm_profgen or create_llvm_prof. llvm_profgen
needs to be the LLVM 19 or newer, or just the LLVM trunk. Alternatively,
create_llvm_prof v0.30.1 or newer can be used instead of llvm-profgen.
Additionally, the build is only supported on x86 platforms equipped
with PMU capabilities, such as LBR on Intel machines. More
specifically:
* Intel platforms: works on every platform that supports LBR;
we have tested on Skylake.
* AMD platforms: tested on AMD Zen3 with the BRS feature. The kernel
needs to be configured with “CONFIG_PERF_EVENTS_AMD_BRS=y", To
check, use
$ cat /proc/cpuinfo | grep “ brs”
For the AMD Zen4, AMD LBRV2 is supported, but we suspect a bug with
AMD LBRv2 implementation in Genoa which blocks the usage.
For ARM, we plan to send patches for SPE-based Propeller when
AutoFDO for Arm is ready.
Experiments and Results
Experiments were conducted to compare the performance of AutoFDO-optimized
kernel images (version 6.9.x) against default builds.. The evaluation
encompassed both open source microbenchmarks and real-world production
services from Google and Meta. The selected microbenchmarks included Neper,
a network subsystem benchmark, and UnixBench which is a comprehensive suite
for assessing various kernel operations.
For Neper, AutoFDO optimization resulted in a 6.1% increase in throughput
and a 10.6% reduction in latency. UnixBench saw a 2.2% improvement in its
index score under low system load and a 2.6% improvement under high system
load.
For further details on the improvements observed in Google and Meta's
production services, please refer to the LLVM discourse post:
https://discourse.llvm.org/t/optimizing-the-linux-kernel-with-autofdo-including-thinlto-and-propeller/79108
Thanks,
Rong Xu and Han Shen
---
Change-Logs in V2:
Rebased to commit e32cde8d2bd7 ("Merge tag 'sched_ext-for-6.12-rc1-fixes-1'
of git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext")
1. [P 0]: moved the Propeller description to the top (Peter Zijlstra)
2. [P 1]: (1) Makefile: fixed file order (Masahiro Yamada)
(2) scripts/Makefile.lib: used is-kernel-object to exclude
files (Masahiro Yamada)
(3) scripts/Makefile.autofdo: improved the code (Masahiro Yamada)
(4) scripts/Makefile.autofdo: handled when DEBUG_INFO disabled
(Nick Desaulniers)
3. [P 2]: tools/objtool/elf.c: updated the comments (Peter Zijlstra)
4. [P 3]: include/asm-generic/vmlinux.lds.h:
(1) explicit set cold text function aligned (Peter Zijlstra and
Peter Anvin)
(2) set hot-text page aligned
5. [P 6]: (1) include/asm-generic/vmlinux.lds.h: made Propeller not
depending on AutoFDO
(2) Makefile: fixed file order (Masahiro Yamada)
(3) scripts/Makefile.lib: used is-kernel-object to exclude
files (Masahiro Yamada). This removed the change in
arch/x86/platform/efi/Makefile,
drivers/firmware/efi/libstub/Makefile, and
arch/x86/boot/compressed/Makefile.
And this also addressed the comment from Arnd Bergmann
regarding arch/x86/purgatory/Makefile
(4) scripts/Makefile.propeller: improved the code
(Masahiro Yamada)
Change-Logs in V3:
Rebased to commit eb952c47d154 ("Merge tag 'for-6.12-rc2-tag' of
git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux")
Integrated the following changes suggested by Mike Rapoport.
1. [P 1]: autofdo.rst: removed code-block directives and used "::"
2. [P 6]: propeller.rst: removed code-block directives and use "::"
Change-Logs in V4:
1. [P 1]: autofdo.rst: fixed a typo for create_llvm_prof command.
Change-Logs in V5:
Added "Tested-by: Yonghong Song <yonghong.song@linux.dev>" to all patches.
Integrated the following changes suggested by Masahiro Yamada.
1. [P 0]: (1) moved ARM related remark from patch 6 to here
2. [P 1]: (1) autofdo.rst: improved the documentation
(2) scripts/Makefile.autofdo: improved comments and used ifdef
instead of ifeq
3. [P 3]: Make the layout change unconditionally
4. [P 4]: Split the patch into two: this patch only added the markers, and
the AutoFDO change went to new P_5
5. [P 7]: (1) propeller.rst: improved the documentation
(2) scripts/Makefile.propeller: improved comments and used ifdef
instead of ifeq
(3) arch/Kconfig: made Propeller build independent of AutoFDO
build
(4) moved ARM related remarks to the cover letter
Change-Logs in V6:
Added "Tested-by: Yabin Cui <yabinc@google.com>" to AutoFDO patches.
1. [P 3]: (1) changed patch title
(2) fixed the build error in sparc64
Change-Logs in V7:
Rebased to commit 11066801dd4b7 (Merge tag 'linux_kselftest-fixes-6.12-rc6'
of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest)
Added "Tested-by: Nathan Chancellor <nathan@kernel.org>" to [P 1] to [P 7]
Added "Reviewed-by: Kees Cook <kees@kernel.org>" to [P 1] to [P 7]
Added "Acked-by: Josh Poimboeuf <jpoimboe@kernel.org>" to [P 2]
Integrated the following changes suggested by Masahiro Yamada.
1. [P 1]: autofdo.rst: fixed format
2. [P 3]: commit message: described the rationale behind the new layout
Rong Xu (7):
Add AutoFDO support for Clang build
objtool: Fix unreachable instruction warnings for weak functions
Adjust symbol ordering in text output section
Add markers for text_unlikely and text_hot sections
AutoFDO: Enable -ffunction-sections for the AutoFDO build
AutoFDO: Enable machine function split optimization for AutoFDO
Add Propeller configuration for kernel build
Documentation/dev-tools/autofdo.rst | 168 ++++++++++++++++++++++++++
Documentation/dev-tools/index.rst | 2 +
Documentation/dev-tools/propeller.rst | 162 +++++++++++++++++++++++++
MAINTAINERS | 14 +++
Makefile | 2 +
arch/Kconfig | 39 ++++++
arch/sparc/kernel/vmlinux.lds.S | 5 +
arch/x86/Kconfig | 2 +
arch/x86/kernel/vmlinux.lds.S | 4 +
include/asm-generic/vmlinux.lds.h | 49 ++++++--
scripts/Makefile.autofdo | 24 ++++
scripts/Makefile.lib | 20 +++
scripts/Makefile.propeller | 28 +++++
tools/objtool/check.c | 2 +
tools/objtool/elf.c | 15 ++-
15 files changed, 520 insertions(+), 16 deletions(-)
create mode 100644 Documentation/dev-tools/autofdo.rst
create mode 100644 Documentation/dev-tools/propeller.rst
create mode 100644 scripts/Makefile.autofdo
create mode 100644 scripts/Makefile.propeller
base-commit: 11066801dd4b7c4d75fce65c812723a80c1481ae
--
2.47.0.163.g1226f6d8fa-goog
^ permalink raw reply [flat|nested] 30+ messages in thread
* [PATCH v7 1/7] Add AutoFDO support for Clang build
2024-11-02 17:51 [PATCH v7 0/7] Add AutoFDO and Propeller support for Clang build Rong Xu
@ 2024-11-02 17:51 ` Rong Xu
2024-11-02 19:46 ` Peter Jung
2024-11-02 17:51 ` [PATCH v7 2/7] objtool: Fix unreachable instruction warnings for weak functions Rong Xu
` (6 subsequent siblings)
7 siblings, 1 reply; 30+ messages in thread
From: Rong Xu @ 2024-11-02 17:51 UTC (permalink / raw)
To: Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Rong Xu, Sami Tolvanen,
Thomas Gleixner, Wei Yang, workflows, Miguel Ojeda,
Maksim Panchenko, David S. Miller, Andreas Larsson, Yonghong Song,
Yabin Cui, Krzysztof Pszeniczny, Sriraman Tallam,
Stephane Eranian
Cc: x86, linux-arch, sparclinux, linux-doc, linux-kbuild,
linux-kernel, llvm
Add the build support for using Clang's AutoFDO. Building the kernel
with AutoFDO does not reduce the optimization level from the
compiler. AutoFDO uses hardware sampling to gather information about
the frequency of execution of different code paths within a binary.
This information is then used to guide the compiler's optimization
decisions, resulting in a more efficient binary. Experiments
showed that the kernel can improve up to 10% in latency.
The support requires a Clang compiler after LLVM 17. This submission
is limited to x86 platforms that support PMU features like LBR on
Intel machines and AMD Zen3 BRS. Support for SPE on ARM 1,
and BRBE on ARM 1 is part of planned future work.
Here is an example workflow for AutoFDO kernel:
1) Build the kernel on the host machine with LLVM enabled, for example,
$ make menuconfig LLVM=1
Turn on AutoFDO build config:
CONFIG_AUTOFDO_CLANG=y
With a configuration that has LLVM enabled, use the following
command:
scripts/config -e AUTOFDO_CLANG
After getting the config, build with
$ make LLVM=1
2) Install the kernel on the test machine.
3) Run the load tests. The '-c' option in perf specifies the sample
event period. We suggest using a suitable prime number,
like 500009, for this purpose.
For Intel platforms:
$ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c <count> \
-o <perf_file> -- <loadtest>
For AMD platforms:
The supported system are: Zen3 with BRS, or Zen4 with amd_lbr_v2
For Zen3:
$ cat proc/cpuinfo | grep " brs"
For Zen4:
$ cat proc/cpuinfo | grep amd_lbr_v2
$ perf record --pfm-events RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k -a \
-N -b -c <count> -o <perf_file> -- <loadtest>
4) (Optional) Download the raw perf file to the host machine.
5) To generate an AutoFDO profile, two offline tools are available:
create_llvm_prof and llvm_profgen. The create_llvm_prof tool is part
of the AutoFDO project and can be found on GitHub
(https://github.com/google/autofdo), version v0.30.1 or later. The
llvm_profgen tool is included in the LLVM compiler itself. It's
important to note that the version of llvm_profgen doesn't need to
match the version of Clang. It needs to be the LLVM 19 release or
later, or from the LLVM trunk.
$ llvm-profgen --kernel --binary=<vmlinux> --perfdata=<perf_file> \
-o <profile_file>
or
$ create_llvm_prof --binary=<vmlinux> --profile=<perf_file> \
--format=extbinary --out=<profile_file>
Note that multiple AutoFDO profile files can be merged into one via:
$ llvm-profdata merge -o <profile_file> <profile_1> ... <profile_n>
6) Rebuild the kernel using the AutoFDO profile file with the same config
as step 1, (Note CONFIG_AUTOFDO_CLANG needs to be enabled):
$ make LLVM=1 CLANG_AUTOFDO_PROFILE=<profile_file>
Co-developed-by: Han Shen <shenhan@google.com>
Signed-off-by: Han Shen <shenhan@google.com>
Signed-off-by: Rong Xu <xur@google.com>
Suggested-by: Sriraman Tallam <tmsriram@google.com>
Suggested-by: Krzysztof Pszeniczny <kpszeniczny@google.com>
Suggested-by: Nick Desaulniers <ndesaulniers@google.com>
Suggested-by: Stephane Eranian <eranian@google.com>
Tested-by: Yonghong Song <yonghong.song@linux.dev>
Tested-by: Yabin Cui <yabinc@google.com>
Tested-by: Nathan Chancellor <nathan@kernel.org>
Reviewed-by: Kees Cook <kees@kernel.org>
---
Documentation/dev-tools/autofdo.rst | 168 ++++++++++++++++++++++++++++
Documentation/dev-tools/index.rst | 1 +
MAINTAINERS | 7 ++
Makefile | 1 +
arch/Kconfig | 20 ++++
arch/x86/Kconfig | 1 +
scripts/Makefile.autofdo | 22 ++++
scripts/Makefile.lib | 10 ++
tools/objtool/check.c | 1 +
9 files changed, 231 insertions(+)
create mode 100644 Documentation/dev-tools/autofdo.rst
create mode 100644 scripts/Makefile.autofdo
diff --git a/Documentation/dev-tools/autofdo.rst b/Documentation/dev-tools/autofdo.rst
new file mode 100644
index 0000000000000..1f0a451e9ccd3
--- /dev/null
+++ b/Documentation/dev-tools/autofdo.rst
@@ -0,0 +1,168 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+===================================
+Using AutoFDO with the Linux kernel
+===================================
+
+This enables AutoFDO build support for the kernel when using
+the Clang compiler. AutoFDO (Auto-Feedback-Directed Optimization)
+is a type of profile-guided optimization (PGO) used to enhance the
+performance of binary executables. It gathers information about the
+frequency of execution of various code paths within a binary using
+hardware sampling. This data is then used to guide the compiler's
+optimization decisions, resulting in a more efficient binary. AutoFDO
+is a powerful optimization technique, and data indicates that it can
+significantly improve kernel performance. It's especially beneficial
+for workloads affected by front-end stalls.
+
+For AutoFDO builds, unlike non-FDO builds, the user must supply a
+profile. Acquiring an AutoFDO profile can be done in several ways.
+AutoFDO profiles are created by converting hardware sampling using
+the "perf" tool. It is crucial that the workload used to create these
+perf files is representative; they must exhibit runtime
+characteristics similar to the workloads that are intended to be
+optimized. Failure to do so will result in the compiler optimizing
+for the wrong objective.
+
+The AutoFDO profile often encapsulates the program's behavior. If the
+performance-critical codes are architecture-independent, the profile
+can be applied across platforms to achieve performance gains. For
+instance, using the profile generated on Intel architecture to build
+a kernel for AMD architecture can also yield performance improvements.
+
+There are two methods for acquiring a representative profile:
+(1) Sample real workloads using a production environment.
+(2) Generate the profile using a representative load test.
+When enabling the AutoFDO build configuration without providing an
+AutoFDO profile, the compiler only modifies the dwarf information in
+the kernel without impacting runtime performance. It's advisable to
+use a kernel binary built with the same AutoFDO configuration to
+collect the perf profile. While it's possible to use a kernel built
+with different options, it may result in inferior performance.
+
+One can collect profiles using AutoFDO build for the previous kernel.
+AutoFDO employs relative line numbers to match the profiles, offering
+some tolerance for source changes. This mode is commonly used in a
+production environment for profile collection.
+
+In a profile collection based on a load test, the AutoFDO collection
+process consists of the following steps:
+
+#. Initial build: The kernel is built with AutoFDO options
+ without a profile.
+
+#. Profiling: The above kernel is then run with a representative
+ workload to gather execution frequency data. This data is
+ collected using hardware sampling, via perf. AutoFDO is most
+ effective on platforms supporting advanced PMU features like
+ LBR on Intel machines.
+
+#. AutoFDO profile generation: Perf output file is converted to
+ the AutoFDO profile via offline tools.
+
+The support requires a Clang compiler LLVM 17 or later.
+
+Preparation
+===========
+
+Configure the kernel with::
+
+ CONFIG_AUTOFDO_CLANG=y
+
+Customization
+=============
+
+The default CONFIG_AUTOFDO_CLANG setting covers kernel space objects for
+AutoFDO builds. One can, however, enable or disable AutoFDO build for
+individual files and directories by adding a line similar to the following
+to the respective kernel Makefile:
+
+- For enabling a single file (e.g. foo.o) ::
+
+ AUTOFDO_PROFILE_foo.o := y
+
+- For enabling all files in one directory ::
+
+ AUTOFDO_PROFILE := y
+
+- For disabling one file ::
+
+ AUTOFDO_PROFILE_foo.o := n
+
+- For disabling all files in one directory ::
+
+ AUTOFDO_PROFILE := n
+
+Workflow
+========
+
+Here is an example workflow for AutoFDO kernel:
+
+1) Build the kernel on the host machine with LLVM enabled,
+ for example, ::
+
+ $ make menuconfig LLVM=1
+
+ Turn on AutoFDO build config::
+
+ CONFIG_AUTOFDO_CLANG=y
+
+ With a configuration that with LLVM enabled, use the following command::
+
+ $ scripts/config -e AUTOFDO_CLANG
+
+ After getting the config, build with ::
+
+ $ make LLVM=1
+
+2) Install the kernel on the test machine.
+
+3) Run the load tests. The '-c' option in perf specifies the sample
+ event period. We suggest using a suitable prime number, like 500009,
+ for this purpose.
+
+ - For Intel platforms::
+
+ $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c <count> -o <perf_file> -- <loadtest>
+
+ - For AMD platforms:
+
+ The supported systems are: Zen3 with BRS, or Zen4 with amd_lbr_v2. To check,
+
+ For Zen3::
+
+ $ cat proc/cpuinfo | grep " brs"
+
+ For Zen4::
+
+ $ cat proc/cpuinfo | grep amd_lbr_v2
+
+ The following command generated the perf data file::
+
+ $ perf record --pfm-events RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k -a -N -b -c <count> -o <perf_file> -- <loadtest>
+
+4) (Optional) Download the raw perf file to the host machine.
+
+5) To generate an AutoFDO profile, two offline tools are available:
+ create_llvm_prof and llvm_profgen. The create_llvm_prof tool is part
+ of the AutoFDO project and can be found on GitHub
+ (https://github.com/google/autofdo), version v0.30.1 or later.
+ The llvm_profgen tool is included in the LLVM compiler itself. It's
+ important to note that the version of llvm_profgen doesn't need to match
+ the version of Clang. It needs to be the LLVM 19 release of Clang
+ or later, or just from the LLVM trunk. ::
+
+ $ llvm-profgen --kernel --binary=<vmlinux> --perfdata=<perf_file> -o <profile_file>
+
+ or ::
+
+ $ create_llvm_prof --binary=<vmlinux> --profile=<perf_file> --format=extbinary --out=<profile_file>
+
+ Note that multiple AutoFDO profile files can be merged into one via::
+
+ $ llvm-profdata merge -o <profile_file> <profile_1> <profile_2> ... <profile_n>
+
+6) Rebuild the kernel using the AutoFDO profile file with the same config as step 1,
+ (Note CONFIG_AUTOFDO_CLANG needs to be enabled)::
+
+ $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<profile_file>
diff --git a/Documentation/dev-tools/index.rst b/Documentation/dev-tools/index.rst
index 53d4d124f9c52..6945644f7008a 100644
--- a/Documentation/dev-tools/index.rst
+++ b/Documentation/dev-tools/index.rst
@@ -34,6 +34,7 @@ Documentation/dev-tools/testing-overview.rst
ktap
checkuapi
gpio-sloppy-logic-analyzer
+ autofdo
.. only:: subproject and html
diff --git a/MAINTAINERS b/MAINTAINERS
index a274079502426..d6ea49433747a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3665,6 +3665,13 @@ F: kernel/audit*
F: lib/*audit.c
K: \baudit_[a-z_0-9]\+\b
+AUTOFDO BUILD
+M: Rong Xu <xur@google.com>
+M: Han Shen <shenhan@google.com>
+S: Supported
+F: Documentation/dev-tools/autofdo.rst
+F: scripts/Makefile.autofdo
+
AUXILIARY BUS DRIVER
M: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
R: Dave Ertman <david.m.ertman@intel.com>
diff --git a/Makefile b/Makefile
index 5e04e4abffd88..b89d87b5dca79 100644
--- a/Makefile
+++ b/Makefile
@@ -1018,6 +1018,7 @@ include-$(CONFIG_KMSAN) += scripts/Makefile.kmsan
include-$(CONFIG_UBSAN) += scripts/Makefile.ubsan
include-$(CONFIG_KCOV) += scripts/Makefile.kcov
include-$(CONFIG_RANDSTRUCT) += scripts/Makefile.randstruct
+include-$(CONFIG_AUTOFDO_CLANG) += scripts/Makefile.autofdo
include-$(CONFIG_GCC_PLUGINS) += scripts/Makefile.gcc-plugins
include $(addprefix $(srctree)/, $(include-y))
diff --git a/arch/Kconfig b/arch/Kconfig
index bd9f095d69fa0..8dca3b5e6ef53 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -811,6 +811,26 @@ config LTO_CLANG_THIN
If unsure, say Y.
endchoice
+config ARCH_SUPPORTS_AUTOFDO_CLANG
+ bool
+
+config AUTOFDO_CLANG
+ bool "Enable Clang's AutoFDO build (EXPERIMENTAL)"
+ depends on ARCH_SUPPORTS_AUTOFDO_CLANG
+ depends on CC_IS_CLANG && CLANG_VERSION >= 170000
+ help
+ This option enables Clang’s AutoFDO build. When
+ an AutoFDO profile is specified in variable
+ CLANG_AUTOFDO_PROFILE during the build process,
+ Clang uses the profile to optimize the kernel.
+
+ If no profile is specified, AutoFDO options are
+ still passed to Clang to facilitate the collection
+ of perf data for creating an AutoFDO profile in
+ subsequent builds.
+
+ If unsure, say N.
+
config ARCH_SUPPORTS_CFI_CLANG
bool
help
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 16354dfa6d965..9dc87661fb373 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -126,6 +126,7 @@ config X86
select ARCH_SUPPORTS_LTO_CLANG
select ARCH_SUPPORTS_LTO_CLANG_THIN
select ARCH_SUPPORTS_RT
+ select ARCH_SUPPORTS_AUTOFDO_CLANG
select ARCH_USE_BUILTIN_BSWAP
select ARCH_USE_CMPXCHG_LOCKREF if X86_CMPXCHG64
select ARCH_USE_MEMTEST
diff --git a/scripts/Makefile.autofdo b/scripts/Makefile.autofdo
new file mode 100644
index 0000000000000..ff96a63fea7cd
--- /dev/null
+++ b/scripts/Makefile.autofdo
@@ -0,0 +1,22 @@
+# SPDX-License-Identifier: GPL-2.0
+
+# Enable available and selected Clang AutoFDO features.
+
+CFLAGS_AUTOFDO_CLANG := -fdebug-info-for-profiling -mllvm -enable-fs-discriminator=true -mllvm -improved-fs-discriminator=true
+
+ifndef CONFIG_DEBUG_INFO
+ CFLAGS_AUTOFDO_CLANG += -gmlt
+endif
+
+ifdef CLANG_AUTOFDO_PROFILE
+ CFLAGS_AUTOFDO_CLANG += -fprofile-sample-use=$(CLANG_AUTOFDO_PROFILE)
+endif
+
+ifdef CONFIG_LTO_CLANG_THIN
+ ifdef CLANG_AUTOFDO_PROFILE
+ KBUILD_LDFLAGS += --lto-sample-profile=$(CLANG_AUTOFDO_PROFILE)
+ endif
+ KBUILD_LDFLAGS += --mllvm=-enable-fs-discriminator=true --mllvm=-improved-fs-discriminator=true -plugin-opt=thinlto
+endif
+
+export CFLAGS_AUTOFDO_CLANG
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 01a9f567d5af4..2d0942c1a0277 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -191,6 +191,16 @@ _c_flags += $(if $(patsubst n%,, \
-D__KCSAN_INSTRUMENT_BARRIERS__)
endif
+#
+# Enable AutoFDO build flags except some files or directories we don't want to
+# enable (depends on variables AUTOFDO_PROFILE_obj.o and AUTOFDO_PROFILE).
+#
+ifeq ($(CONFIG_AUTOFDO_CLANG),y)
+_c_flags += $(if $(patsubst n%,, \
+ $(AUTOFDO_PROFILE_$(target-stem).o)$(AUTOFDO_PROFILE)$(is-kernel-object)), \
+ $(CFLAGS_AUTOFDO_CLANG))
+endif
+
# $(src) for including checkin headers from generated source files
# $(obj) for including generated headers from checkin source files
ifeq ($(KBUILD_EXTMOD),)
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 6604f5d038aad..4c5229991e1e0 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -4557,6 +4557,7 @@ static int validate_ibt(struct objtool_file *file)
!strcmp(sec->name, "__jump_table") ||
!strcmp(sec->name, "__mcount_loc") ||
!strcmp(sec->name, ".kcfi_traps") ||
+ !strcmp(sec->name, ".llvm.call-graph-profile") ||
strstr(sec->name, "__patchable_function_entries"))
continue;
--
2.47.0.163.g1226f6d8fa-goog
^ permalink raw reply related [flat|nested] 30+ messages in thread
* [PATCH v7 2/7] objtool: Fix unreachable instruction warnings for weak functions
2024-11-02 17:51 [PATCH v7 0/7] Add AutoFDO and Propeller support for Clang build Rong Xu
2024-11-02 17:51 ` [PATCH v7 1/7] Add AutoFDO " Rong Xu
@ 2024-11-02 17:51 ` Rong Xu
2024-11-02 17:51 ` [PATCH v7 3/7] Adjust symbol ordering in text output section Rong Xu
` (5 subsequent siblings)
7 siblings, 0 replies; 30+ messages in thread
From: Rong Xu @ 2024-11-02 17:51 UTC (permalink / raw)
To: Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Rong Xu, Sami Tolvanen,
Thomas Gleixner, Wei Yang, workflows, Miguel Ojeda,
Maksim Panchenko, David S. Miller, Andreas Larsson, Yonghong Song,
Yabin Cui, Krzysztof Pszeniczny, Sriraman Tallam,
Stephane Eranian
Cc: x86, linux-arch, sparclinux, linux-doc, linux-kbuild,
linux-kernel, llvm
In the presence of both weak and strong function definitions, the
linker drops the weak symbol in favor of a strong symbol, but
leaves the code in place. Code in ignore_unreachable_insn() has
some heuristics to suppress the warning, but it does not work when
-ffunction-sections is enabled.
Suppose function foo has both strong and weak definitions.
Case 1: The strong definition has an annotated section name,
like .init.text. Only the weak definition will be placed into
.text.foo. But since the section has no symbols, there will be no
"hole" in the section.
Case 2: Both sections are without an annotated section name.
Both will be placed into .text.foo section, but there will be only one
symbol (the strong one). If the weak code is before the strong code,
there is no "hole" as it fails to find the right-most symbol before
the offset.
The fix is to use the first node to compute the hole if hole.sym
is empty. If there is no symbol in the section, the first node
will be NULL, in which case, -1 is returned to skip the whole
section.
Co-developed-by: Han Shen <shenhan@google.com>
Signed-off-by: Han Shen <shenhan@google.com>
Signed-off-by: Rong Xu <xur@google.com>
Suggested-by: Sriraman Tallam <tmsriram@google.com>
Suggested-by: Krzysztof Pszeniczny <kpszeniczny@google.com>
Tested-by: Yonghong Song <yonghong.song@linux.dev>
Tested-by: Yabin Cui <yabinc@google.com>
Tested-by: Nathan Chancellor <nathan@kernel.org>
Reviewed-by: Kees Cook <kees@kernel.org>
Acked-by: Josh Poimboeuf <jpoimboe@kernel.org>
---
tools/objtool/elf.c | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 3d27983dc908d..6f64d611faea9 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -224,12 +224,17 @@ int find_symbol_hole_containing(const struct section *sec, unsigned long offset)
if (n)
return 0; /* not a hole */
- /* didn't find a symbol for which @offset is after it */
- if (!hole.sym)
- return 0; /* not a hole */
+ /*
+ * @offset >= sym->offset + sym->len, find symbol after it.
+ * When hole.sym is empty, use the first node to compute the hole.
+ * If there is no symbol in the section, the first node will be NULL,
+ * in which case, -1 is returned to skip the whole section.
+ */
+ if (hole.sym)
+ n = rb_next(&hole.sym->node);
+ else
+ n = rb_first_cached(&sec->symbol_tree);
- /* @offset >= sym->offset + sym->len, find symbol after it */
- n = rb_next(&hole.sym->node);
if (!n)
return -1; /* until end of address space */
--
2.47.0.163.g1226f6d8fa-goog
^ permalink raw reply related [flat|nested] 30+ messages in thread
* [PATCH v7 3/7] Adjust symbol ordering in text output section
2024-11-02 17:51 [PATCH v7 0/7] Add AutoFDO and Propeller support for Clang build Rong Xu
2024-11-02 17:51 ` [PATCH v7 1/7] Add AutoFDO " Rong Xu
2024-11-02 17:51 ` [PATCH v7 2/7] objtool: Fix unreachable instruction warnings for weak functions Rong Xu
@ 2024-11-02 17:51 ` Rong Xu
2024-12-01 14:31 ` [PATCH v7 3/7] Adjust symbol ordering in text output section [openrisc boot failure] Guenter Roeck
2024-11-02 17:51 ` [PATCH v7 4/7] Add markers for text_unlikely and text_hot sections Rong Xu
` (4 subsequent siblings)
7 siblings, 1 reply; 30+ messages in thread
From: Rong Xu @ 2024-11-02 17:51 UTC (permalink / raw)
To: Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Rong Xu, Sami Tolvanen,
Thomas Gleixner, Wei Yang, workflows, Miguel Ojeda,
Maksim Panchenko, David S. Miller, Andreas Larsson, Yonghong Song,
Yabin Cui, Krzysztof Pszeniczny, Sriraman Tallam,
Stephane Eranian
Cc: x86, linux-arch, sparclinux, linux-doc, linux-kbuild,
linux-kernel, llvm
When the -ffunction-sections compiler option is enabled, each function
is placed in a separate section named .text.function_name rather than
putting all functions in a single .text section.
However, using -function-sections can cause problems with the
linker script. The comments included in include/asm-generic/vmlinux.lds.h
note these issues.:
“TEXT_MAIN here will match .text.fixup and .text.unlikely if dead
code elimination is enabled, so these sections should be converted
to use ".." first.”
It is unclear whether there is a straightforward method for converting
a suffix to "..".
This patch modifies the order of subsections within the text output
section. Specifically, it changes current order:
.text.hot, .text, .text_unlikely, .text.unknown, .text.asan
to the new order:
.text.asan, .text.unknown, .text_unlikely, .text.hot, .text
Here is the rationale behind the new layout:
The majority of the code resides in three sections: .text.hot, .text,
and .text.unlikely, with .text.unknown containing a negligible amount.
.text.asan is only generated in ASAN builds.
The primary goal is to group code segments based on their execution
frequency (hotness).
First, we want to place .text.hot adjacent to .text. Since we cannot put
.text.hot after .text (Due to constraints with -ffunction-sections,
placing .text.hot after .text is problematic), we need to put
.text.hot before .text.
Then it comes to .text.unlikely, we cannot put it after .text (same
-ffunction-sections issue) . Therefore, we position .text.unlikely
before .text.hot.
.text.unknown and .tex.asan follow the same logic.
This revised ordering effectively reverses the original arrangement (for
.text.unlikely, .text.unknown, and .tex.asan), maintaining a similar level
of affinity between sections.
It also places .text.hot section at the beginning of a page to better
utilize the TLB entry.
Note that the limitation arises because the linker script employs glob
patterns instead of regular expressions for string matching. While there
is a method to maintain the current order using complex patterns, this
significantly complicates the pattern and increases the likelihood of
errors.
This patch also changes vmlinux.lds.S for the sparc64 architecture to
accommodate specific symbol placement requirements.
Co-developed-by: Han Shen <shenhan@google.com>
Signed-off-by: Han Shen <shenhan@google.com>
Signed-off-by: Rong Xu <xur@google.com>
Suggested-by: Sriraman Tallam <tmsriram@google.com>
Suggested-by: Krzysztof Pszeniczny <kpszeniczny@google.com>
Tested-by: Yonghong Song <yonghong.song@linux.dev>
Tested-by: Yabin Cui <yabinc@google.com>
Tested-by: Nathan Chancellor <nathan@kernel.org>
Reviewed-by: Kees Cook <kees@kernel.org>
---
arch/sparc/kernel/vmlinux.lds.S | 5 +++++
include/asm-generic/vmlinux.lds.h | 19 ++++++++++++-------
2 files changed, 17 insertions(+), 7 deletions(-)
diff --git a/arch/sparc/kernel/vmlinux.lds.S b/arch/sparc/kernel/vmlinux.lds.S
index d317a843f7ea9..f1b86eb303404 100644
--- a/arch/sparc/kernel/vmlinux.lds.S
+++ b/arch/sparc/kernel/vmlinux.lds.S
@@ -48,6 +48,11 @@ SECTIONS
{
_text = .;
HEAD_TEXT
+ ALIGN_FUNCTION();
+#ifdef CONFIG_SPARC64
+ /* Match text section symbols in head_64.S first */
+ *head_64.o(.text)
+#endif
TEXT_TEXT
SCHED_TEXT
LOCK_TEXT
diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index eeadbaeccf88b..fd901951549c0 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -553,19 +553,24 @@
* .text section. Map to function alignment to avoid address changes
* during second ld run in second ld pass when generating System.map
*
- * TEXT_MAIN here will match .text.fixup and .text.unlikely if dead
- * code elimination is enabled, so these sections should be converted
- * to use ".." first.
+ * TEXT_MAIN here will match symbols with a fixed pattern (for example,
+ * .text.hot or .text.unlikely) if dead code elimination or
+ * function-section is enabled. Match these symbols first before
+ * TEXT_MAIN to ensure they are grouped together.
+ *
+ * Also placing .text.hot section at the beginning of a page, this
+ * would help the TLB performance.
*/
#define TEXT_TEXT \
ALIGN_FUNCTION(); \
+ *(.text.asan.* .text.tsan.*) \
+ *(.text.unknown .text.unknown.*) \
+ *(.text.unlikely .text.unlikely.*) \
+ . = ALIGN(PAGE_SIZE); \
*(.text.hot .text.hot.*) \
*(TEXT_MAIN .text.fixup) \
- *(.text.unlikely .text.unlikely.*) \
- *(.text.unknown .text.unknown.*) \
NOINSTR_TEXT \
- *(.ref.text) \
- *(.text.asan.* .text.tsan.*)
+ *(.ref.text)
/* sched.text is aling to function alignment to secure we have same
--
2.47.0.163.g1226f6d8fa-goog
^ permalink raw reply related [flat|nested] 30+ messages in thread
* [PATCH v7 4/7] Add markers for text_unlikely and text_hot sections
2024-11-02 17:51 [PATCH v7 0/7] Add AutoFDO and Propeller support for Clang build Rong Xu
` (2 preceding siblings ...)
2024-11-02 17:51 ` [PATCH v7 3/7] Adjust symbol ordering in text output section Rong Xu
@ 2024-11-02 17:51 ` Rong Xu
2024-11-02 17:51 ` [PATCH v7 5/7] AutoFDO: Enable -ffunction-sections for the AutoFDO build Rong Xu
` (3 subsequent siblings)
7 siblings, 0 replies; 30+ messages in thread
From: Rong Xu @ 2024-11-02 17:51 UTC (permalink / raw)
To: Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Rong Xu, Sami Tolvanen,
Thomas Gleixner, Wei Yang, workflows, Miguel Ojeda,
Maksim Panchenko, David S. Miller, Andreas Larsson, Yonghong Song,
Yabin Cui, Krzysztof Pszeniczny, Sriraman Tallam,
Stephane Eranian
Cc: x86, linux-arch, sparclinux, linux-doc, linux-kbuild,
linux-kernel, llvm
Add markers like __hot_text_start, __hot_text_end, __unlikely_text_start,
and __unlikely_text_end which will be included in System.map. These markers
indicate how the compiler groups functions, providing valuable information
to developers about the layout and optimization of the code.
Co-developed-by: Han Shen <shenhan@google.com>
Signed-off-by: Han Shen <shenhan@google.com>
Signed-off-by: Rong Xu <xur@google.com>
Suggested-by: Sriraman Tallam <tmsriram@google.com>
Tested-by: Yonghong Song <yonghong.song@linux.dev>
Tested-by: Yabin Cui <yabinc@google.com>
Tested-by: Nathan Chancellor <nathan@kernel.org>
Reviewed-by: Kees Cook <kees@kernel.org>
---
include/asm-generic/vmlinux.lds.h | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index fd901951549c0..e02973f3b4189 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -549,6 +549,16 @@
__cpuidle_text_end = .; \
__noinstr_text_end = .;
+#define TEXT_UNLIKELY \
+ __unlikely_text_start = .; \
+ *(.text.unlikely .text.unlikely.*) \
+ __unlikely_text_end = .;
+
+#define TEXT_HOT \
+ __hot_text_start = .; \
+ *(.text.hot .text.hot.*) \
+ __hot_text_end = .;
+
/*
* .text section. Map to function alignment to avoid address changes
* during second ld run in second ld pass when generating System.map
@@ -565,9 +575,9 @@
ALIGN_FUNCTION(); \
*(.text.asan.* .text.tsan.*) \
*(.text.unknown .text.unknown.*) \
- *(.text.unlikely .text.unlikely.*) \
+ TEXT_UNLIKELY \
. = ALIGN(PAGE_SIZE); \
- *(.text.hot .text.hot.*) \
+ TEXT_HOT \
*(TEXT_MAIN .text.fixup) \
NOINSTR_TEXT \
*(.ref.text)
--
2.47.0.163.g1226f6d8fa-goog
^ permalink raw reply related [flat|nested] 30+ messages in thread
* [PATCH v7 5/7] AutoFDO: Enable -ffunction-sections for the AutoFDO build
2024-11-02 17:51 [PATCH v7 0/7] Add AutoFDO and Propeller support for Clang build Rong Xu
` (3 preceding siblings ...)
2024-11-02 17:51 ` [PATCH v7 4/7] Add markers for text_unlikely and text_hot sections Rong Xu
@ 2024-11-02 17:51 ` Rong Xu
2024-11-02 17:51 ` [PATCH v7 6/7] AutoFDO: Enable machine function split optimization for AutoFDO Rong Xu
` (2 subsequent siblings)
7 siblings, 0 replies; 30+ messages in thread
From: Rong Xu @ 2024-11-02 17:51 UTC (permalink / raw)
To: Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Rong Xu, Sami Tolvanen,
Thomas Gleixner, Wei Yang, workflows, Miguel Ojeda,
Maksim Panchenko, David S. Miller, Andreas Larsson, Yonghong Song,
Yabin Cui, Krzysztof Pszeniczny, Sriraman Tallam,
Stephane Eranian
Cc: x86, linux-arch, sparclinux, linux-doc, linux-kbuild,
linux-kernel, llvm
Enable -ffunction-sections by default for the AutoFDO build.
With -ffunction-sections, the compiler places each function in its own
section named .text.function_name instead of placing all functions in
the .text section. In the AutoFDO build, this allows the linker to
utilize profile information to reorganize functions for improved
utilization of iCache and iTLB.
Co-developed-by: Han Shen <shenhan@google.com>
Signed-off-by: Han Shen <shenhan@google.com>
Signed-off-by: Rong Xu <xur@google.com>
Suggested-by: Sriraman Tallam <tmsriram@google.com>
Tested-by: Yonghong Song <yonghong.song@linux.dev>
Tested-by: Yabin Cui <yabinc@google.com>
Tested-by: Nathan Chancellor <nathan@kernel.org>
Reviewed-by: Kees Cook <kees@kernel.org>
---
include/asm-generic/vmlinux.lds.h | 11 +++++++++--
scripts/Makefile.autofdo | 2 +-
2 files changed, 10 insertions(+), 3 deletions(-)
diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index e02973f3b4189..bd64fdedabd2f 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -95,18 +95,25 @@
* With LTO_CLANG, the linker also splits sections by default, so we need
* these macros to combine the sections during the final link.
*
+ * With AUTOFDO_CLANG, by default, the linker splits text sections and
+ * regroups functions into subsections.
+ *
* RODATA_MAIN is not used because existing code already defines .rodata.x
* sections to be brought in with rodata.
*/
-#if defined(CONFIG_LD_DEAD_CODE_DATA_ELIMINATION) || defined(CONFIG_LTO_CLANG)
+#if defined(CONFIG_LD_DEAD_CODE_DATA_ELIMINATION) || defined(CONFIG_LTO_CLANG) || \
+defined(CONFIG_AUTOFDO_CLANG)
#define TEXT_MAIN .text .text.[0-9a-zA-Z_]*
+#else
+#define TEXT_MAIN .text
+#endif
+#if defined(CONFIG_LD_DEAD_CODE_DATA_ELIMINATION) || defined(CONFIG_LTO_CLANG)
#define DATA_MAIN .data .data.[0-9a-zA-Z_]* .data..L* .data..compoundliteral* .data.$__unnamed_* .data.$L*
#define SDATA_MAIN .sdata .sdata.[0-9a-zA-Z_]*
#define RODATA_MAIN .rodata .rodata.[0-9a-zA-Z_]* .rodata..L*
#define BSS_MAIN .bss .bss.[0-9a-zA-Z_]* .bss..L* .bss..compoundliteral*
#define SBSS_MAIN .sbss .sbss.[0-9a-zA-Z_]*
#else
-#define TEXT_MAIN .text
#define DATA_MAIN .data
#define SDATA_MAIN .sdata
#define RODATA_MAIN .rodata
diff --git a/scripts/Makefile.autofdo b/scripts/Makefile.autofdo
index ff96a63fea7cd..6155d6fc4ca7f 100644
--- a/scripts/Makefile.autofdo
+++ b/scripts/Makefile.autofdo
@@ -9,7 +9,7 @@ ifndef CONFIG_DEBUG_INFO
endif
ifdef CLANG_AUTOFDO_PROFILE
- CFLAGS_AUTOFDO_CLANG += -fprofile-sample-use=$(CLANG_AUTOFDO_PROFILE)
+ CFLAGS_AUTOFDO_CLANG += -fprofile-sample-use=$(CLANG_AUTOFDO_PROFILE) -ffunction-sections
endif
ifdef CONFIG_LTO_CLANG_THIN
--
2.47.0.163.g1226f6d8fa-goog
^ permalink raw reply related [flat|nested] 30+ messages in thread
* [PATCH v7 6/7] AutoFDO: Enable machine function split optimization for AutoFDO
2024-11-02 17:51 [PATCH v7 0/7] Add AutoFDO and Propeller support for Clang build Rong Xu
` (4 preceding siblings ...)
2024-11-02 17:51 ` [PATCH v7 5/7] AutoFDO: Enable -ffunction-sections for the AutoFDO build Rong Xu
@ 2024-11-02 17:51 ` Rong Xu
2024-11-02 17:51 ` [PATCH v7 7/7] Add Propeller configuration for kernel build Rong Xu
2024-11-06 16:08 ` [PATCH v7 0/7] Add AutoFDO and Propeller support for Clang build Masahiro Yamada
7 siblings, 0 replies; 30+ messages in thread
From: Rong Xu @ 2024-11-02 17:51 UTC (permalink / raw)
To: Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Rong Xu, Sami Tolvanen,
Thomas Gleixner, Wei Yang, workflows, Miguel Ojeda,
Maksim Panchenko, David S. Miller, Andreas Larsson, Yonghong Song,
Yabin Cui, Krzysztof Pszeniczny, Sriraman Tallam,
Stephane Eranian
Cc: x86, linux-arch, sparclinux, linux-doc, linux-kbuild,
linux-kernel, llvm
Enable the machine function split optimization for AutoFDO in Clang.
Machine function split (MFS) is a pass in the Clang compiler that
splits a function into hot and cold parts. The linker groups all
cold blocks across functions together. This decreases hot code
fragmentation and improves iCache and iTLB utilization.
MFS requires a profile so this is enabled only for the AutoFDO builds.
Co-developed-by: Han Shen <shenhan@google.com>
Signed-off-by: Han Shen <shenhan@google.com>
Signed-off-by: Rong Xu <xur@google.com>
Suggested-by: Sriraman Tallam <tmsriram@google.com>
Suggested-by: Krzysztof Pszeniczny <kpszeniczny@google.com>
Tested-by: Yonghong Song <yonghong.song@linux.dev>
Tested-by: Yabin Cui <yabinc@google.com>
Tested-by: Nathan Chancellor <nathan@kernel.org>
Reviewed-by: Kees Cook <kees@kernel.org>
---
include/asm-generic/vmlinux.lds.h | 7 ++++++-
scripts/Makefile.autofdo | 2 ++
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index bd64fdedabd2f..8a0bb3946cf05 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -556,6 +556,11 @@ defined(CONFIG_AUTOFDO_CLANG)
__cpuidle_text_end = .; \
__noinstr_text_end = .;
+#define TEXT_SPLIT \
+ __split_text_start = .; \
+ *(.text.split .text.split.[0-9a-zA-Z_]*) \
+ __split_text_end = .;
+
#define TEXT_UNLIKELY \
__unlikely_text_start = .; \
*(.text.unlikely .text.unlikely.*) \
@@ -582,6 +587,7 @@ defined(CONFIG_AUTOFDO_CLANG)
ALIGN_FUNCTION(); \
*(.text.asan.* .text.tsan.*) \
*(.text.unknown .text.unknown.*) \
+ TEXT_SPLIT \
TEXT_UNLIKELY \
. = ALIGN(PAGE_SIZE); \
TEXT_HOT \
@@ -589,7 +595,6 @@ defined(CONFIG_AUTOFDO_CLANG)
NOINSTR_TEXT \
*(.ref.text)
-
/* sched.text is aling to function alignment to secure we have same
* address even at second ld pass when generating System.map */
#define SCHED_TEXT \
diff --git a/scripts/Makefile.autofdo b/scripts/Makefile.autofdo
index 6155d6fc4ca7f..1caf2457e585c 100644
--- a/scripts/Makefile.autofdo
+++ b/scripts/Makefile.autofdo
@@ -10,6 +10,7 @@ endif
ifdef CLANG_AUTOFDO_PROFILE
CFLAGS_AUTOFDO_CLANG += -fprofile-sample-use=$(CLANG_AUTOFDO_PROFILE) -ffunction-sections
+ CFLAGS_AUTOFDO_CLANG += -fsplit-machine-functions
endif
ifdef CONFIG_LTO_CLANG_THIN
@@ -17,6 +18,7 @@ ifdef CONFIG_LTO_CLANG_THIN
KBUILD_LDFLAGS += --lto-sample-profile=$(CLANG_AUTOFDO_PROFILE)
endif
KBUILD_LDFLAGS += --mllvm=-enable-fs-discriminator=true --mllvm=-improved-fs-discriminator=true -plugin-opt=thinlto
+ KBUILD_LDFLAGS += -plugin-opt=-split-machine-functions
endif
export CFLAGS_AUTOFDO_CLANG
--
2.47.0.163.g1226f6d8fa-goog
^ permalink raw reply related [flat|nested] 30+ messages in thread
* [PATCH v7 7/7] Add Propeller configuration for kernel build
2024-11-02 17:51 [PATCH v7 0/7] Add AutoFDO and Propeller support for Clang build Rong Xu
` (5 preceding siblings ...)
2024-11-02 17:51 ` [PATCH v7 6/7] AutoFDO: Enable machine function split optimization for AutoFDO Rong Xu
@ 2024-11-02 17:51 ` Rong Xu
2024-11-07 20:45 ` Nathan Chancellor
2024-12-12 21:20 ` Yonghong Song
2024-11-06 16:08 ` [PATCH v7 0/7] Add AutoFDO and Propeller support for Clang build Masahiro Yamada
7 siblings, 2 replies; 30+ messages in thread
From: Rong Xu @ 2024-11-02 17:51 UTC (permalink / raw)
To: Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Rong Xu, Sami Tolvanen,
Thomas Gleixner, Wei Yang, workflows, Miguel Ojeda,
Maksim Panchenko, David S. Miller, Andreas Larsson, Yonghong Song,
Yabin Cui, Krzysztof Pszeniczny, Sriraman Tallam,
Stephane Eranian
Cc: x86, linux-arch, sparclinux, linux-doc, linux-kbuild,
linux-kernel, llvm
Add the build support for using Clang's Propeller optimizer. Like
AutoFDO, Propeller uses hardware sampling to gather information
about the frequency of execution of different code paths within a
binary. This information is then used to guide the compiler's
optimization decisions, resulting in a more efficient binary.
The support requires a Clang compiler LLVM 19 or later, and the
create_llvm_prof tool
(https://github.com/google/autofdo/releases/tag/v0.30.1). This
commit is limited to x86 platforms that support PMU features
like LBR on Intel machines and AMD Zen3 BRS.
Here is an example workflow for building an AutoFDO+Propeller
optimized kernel:
1) Build the kernel on the host machine, with AutoFDO and Propeller
build config
CONFIG_AUTOFDO_CLANG=y
CONFIG_PROPELLER_CLANG=y
then
$ make LLVM=1 CLANG_AUTOFDO_PROFILE=<autofdo_profile>
“<autofdo_profile>” is the profile collected when doing a non-Propeller
AutoFDO build. This step builds a kernel that has the same optimization
level as AutoFDO, plus a metadata section that records basic block
information. This kernel image runs as fast as an AutoFDO optimized
kernel.
2) Install the kernel on test/production machines.
3) Run the load tests. The '-c' option in perf specifies the sample
event period. We suggest using a suitable prime number,
like 500009, for this purpose.
For Intel platforms:
$ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c <count> \
-o <perf_file> -- <loadtest>
For AMD platforms:
The supported system are: Zen3 with BRS, or Zen4 with amd_lbr_v2
# To see if Zen3 support LBR:
$ cat proc/cpuinfo | grep " brs"
# To see if Zen4 support LBR:
$ cat proc/cpuinfo | grep amd_lbr_v2
# If the result is yes, then collect the profile using:
$ perf record --pfm-events RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k -a \
-N -b -c <count> -o <perf_file> -- <loadtest>
4) (Optional) Download the raw perf file to the host machine.
5) Generate Propeller profile:
$ create_llvm_prof --binary=<vmlinux> --profile=<perf_file> \
--format=propeller --propeller_output_module_name \
--out=<propeller_profile_prefix>_cc_profile.txt \
--propeller_symorder=<propeller_profile_prefix>_ld_profile.txt
“create_llvm_prof” is the profile conversion tool, and a prebuilt
binary for linux can be found on
https://github.com/google/autofdo/releases/tag/v0.30.1 (can also build
from source).
"<propeller_profile_prefix>" can be something like
"/home/user/dir/any_string".
This command generates a pair of Propeller profiles:
"<propeller_profile_prefix>_cc_profile.txt" and
"<propeller_profile_prefix>_ld_profile.txt".
6) Rebuild the kernel using the AutoFDO and Propeller profile files.
CONFIG_AUTOFDO_CLANG=y
CONFIG_PROPELLER_CLANG=y
and
$ make LLVM=1 CLANG_AUTOFDO_PROFILE=<autofdo_profile> \
CLANG_PROPELLER_PROFILE_PREFIX=<propeller_profile_prefix>
Co-developed-by: Han Shen <shenhan@google.com>
Signed-off-by: Han Shen <shenhan@google.com>
Signed-off-by: Rong Xu <xur@google.com>
Suggested-by: Sriraman Tallam <tmsriram@google.com>
Suggested-by: Krzysztof Pszeniczny <kpszeniczny@google.com>
Suggested-by: Nick Desaulniers <ndesaulniers@google.com>
Suggested-by: Stephane Eranian <eranian@google.com>
Tested-by: Yonghong Song <yonghong.song@linux.dev>
Tested-by: Nathan Chancellor <nathan@kernel.org>
Reviewed-by: Kees Cook <kees@kernel.org>
---
Documentation/dev-tools/index.rst | 1 +
Documentation/dev-tools/propeller.rst | 162 ++++++++++++++++++++++++++
MAINTAINERS | 7 ++
Makefile | 1 +
arch/Kconfig | 19 +++
arch/x86/Kconfig | 1 +
arch/x86/kernel/vmlinux.lds.S | 4 +
include/asm-generic/vmlinux.lds.h | 6 +-
scripts/Makefile.lib | 10 ++
scripts/Makefile.propeller | 28 +++++
tools/objtool/check.c | 1 +
11 files changed, 237 insertions(+), 3 deletions(-)
create mode 100644 Documentation/dev-tools/propeller.rst
create mode 100644 scripts/Makefile.propeller
diff --git a/Documentation/dev-tools/index.rst b/Documentation/dev-tools/index.rst
index 6945644f7008a..3c0ac08b27091 100644
--- a/Documentation/dev-tools/index.rst
+++ b/Documentation/dev-tools/index.rst
@@ -35,6 +35,7 @@ Documentation/dev-tools/testing-overview.rst
checkuapi
gpio-sloppy-logic-analyzer
autofdo
+ propeller
.. only:: subproject and html
diff --git a/Documentation/dev-tools/propeller.rst b/Documentation/dev-tools/propeller.rst
new file mode 100644
index 0000000000000..92195958e3dbc
--- /dev/null
+++ b/Documentation/dev-tools/propeller.rst
@@ -0,0 +1,162 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+=====================================
+Using Propeller with the Linux kernel
+=====================================
+
+This enables Propeller build support for the kernel when using Clang
+compiler. Propeller is a profile-guided optimization (PGO) method used
+to optimize binary executables. Like AutoFDO, it utilizes hardware
+sampling to gather information about the frequency of execution of
+different code paths within a binary. Unlike AutoFDO, this information
+is then used right before linking phase to optimize (among others)
+block layout within and across functions.
+
+A few important notes about adopting Propeller optimization:
+
+#. Although it can be used as a standalone optimization step, it is
+ strongly recommended to apply Propeller on top of AutoFDO,
+ AutoFDO+ThinLTO or Instrument FDO. The rest of this document
+ assumes this paradigm.
+
+#. Propeller uses another round of profiling on top of
+ AutoFDO/AutoFDO+ThinLTO/iFDO. The whole build process involves
+ "build-afdo - train-afdo - build-propeller - train-propeller -
+ build-optimized".
+
+#. Propeller requires LLVM 19 release or later for Clang/Clang++
+ and the linker(ld.lld).
+
+#. In addition to LLVM toolchain, Propeller requires a profiling
+ conversion tool: https://github.com/google/autofdo with a release
+ after v0.30.1: https://github.com/google/autofdo/releases/tag/v0.30.1.
+
+The Propeller optimization process involves the following steps:
+
+#. Initial building: Build the AutoFDO or AutoFDO+ThinLTO binary as
+ you would normally do, but with a set of compile-time / link-time
+ flags, so that a special metadata section is created within the
+ kernel binary. The special section is only intend to be used by the
+ profiling tool, it is not part of the runtime image, nor does it
+ change kernel run time text sections.
+
+#. Profiling: The above kernel is then run with a representative
+ workload to gather execution frequency data. This data is collected
+ using hardware sampling, via perf. Propeller is most effective on
+ platforms supporting advanced PMU features like LBR on Intel
+ machines. This step is the same as profiling the kernel for AutoFDO
+ (the exact perf parameters can be different).
+
+#. Propeller profile generation: Perf output file is converted to a
+ pair of Propeller profiles via an offline tool.
+
+#. Optimized build: Build the AutoFDO or AutoFDO+ThinLTO optimized
+ binary as you would normally do, but with a compile-time /
+ link-time flag to pick up the Propeller compile time and link time
+ profiles. This build step uses 3 profiles - the AutoFDO profile,
+ the Propeller compile-time profile and the Propeller link-time
+ profile.
+
+#. Deployment: The optimized kernel binary is deployed and used
+ in production environments, providing improved performance
+ and reduced latency.
+
+Preparation
+===========
+
+Configure the kernel with::
+
+ CONFIG_AUTOFDO_CLANG=y
+ CONFIG_PROPELLER_CLANG=y
+
+Customization
+=============
+
+The default CONFIG_PROPELLER_CLANG setting covers kernel space objects
+for Propeller builds. One can, however, enable or disable Propeller build
+for individual files and directories by adding a line similar to the
+following to the respective kernel Makefile:
+
+- For enabling a single file (e.g. foo.o)::
+
+ PROPELLER_PROFILE_foo.o := y
+
+- For enabling all files in one directory::
+
+ PROPELLER_PROFILE := y
+
+- For disabling one file::
+
+ PROPELLER_PROFILE_foo.o := n
+
+- For disabling all files in one directory::
+
+ PROPELLER__PROFILE := n
+
+
+Workflow
+========
+
+Here is an example workflow for building an AutoFDO+Propeller kernel:
+
+1) Assuming an AutoFDO profile is already collected following
+ instructions in the AutoFDO document, build the kernel on the host
+ machine, with AutoFDO and Propeller build configs ::
+
+ CONFIG_AUTOFDO_CLANG=y
+ CONFIG_PROPELLER_CLANG=y
+
+ and ::
+
+ $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<autofdo-profile-name>
+
+2) Install the kernel on the test machine.
+
+3) Run the load tests. The '-c' option in perf specifies the sample
+ event period. We suggest using a suitable prime number, like 500009,
+ for this purpose.
+
+ - For Intel platforms::
+
+ $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c <count> -o <perf_file> -- <loadtest>
+
+ - For AMD platforms::
+
+ $ perf record --pfm-event RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k -a -N -b -c <count> -o <perf_file> -- <loadtest>
+
+ Note you can repeat the above steps to collect multiple <perf_file>s.
+
+4) (Optional) Download the raw perf file(s) to the host machine.
+
+5) Use the create_llvm_prof tool (https://github.com/google/autofdo) to
+ generate Propeller profile. ::
+
+ $ create_llvm_prof --binary=<vmlinux> --profile=<perf_file>
+ --format=propeller --propeller_output_module_name
+ --out=<propeller_profile_prefix>_cc_profile.txt
+ --propeller_symorder=<propeller_profile_prefix>_ld_profile.txt
+
+ "<propeller_profile_prefix>" can be something like "/home/user/dir/any_string".
+
+ This command generates a pair of Propeller profiles:
+ "<propeller_profile_prefix>_cc_profile.txt" and
+ "<propeller_profile_prefix>_ld_profile.txt".
+
+ If there are more than 1 perf_file collected in the previous step,
+ you can create a temp list file "<perf_file_list>" with each line
+ containing one perf file name and run::
+
+ $ create_llvm_prof --binary=<vmlinux> --profile=@<perf_file_list>
+ --format=propeller --propeller_output_module_name
+ --out=<propeller_profile_prefix>_cc_profile.txt
+ --propeller_symorder=<propeller_profile_prefix>_ld_profile.txt
+
+6) Rebuild the kernel using the AutoFDO and Propeller
+ profiles. ::
+
+ CONFIG_AUTOFDO_CLANG=y
+ CONFIG_PROPELLER_CLANG=y
+
+ and ::
+
+ $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<profile_file> CLANG_PROPELLER_PROFILE_PREFIX=<propeller_profile_prefix>
diff --git a/MAINTAINERS b/MAINTAINERS
index d6ea49433747a..42e3af0791e15 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -18449,6 +18449,13 @@ S: Maintained
F: include/linux/psi*
F: kernel/sched/psi.c
+PROPELLER BUILD
+M: Rong Xu <xur@google.com>
+M: Han Shen <shenhan@google.com>
+S: Supported
+F: Documentation/dev-tools/propeller.rst
+F: scripts/Makefile.propeller
+
PRINTK
M: Petr Mladek <pmladek@suse.com>
R: Steven Rostedt <rostedt@goodmis.org>
diff --git a/Makefile b/Makefile
index b89d87b5dca79..b2830b27e1a7f 100644
--- a/Makefile
+++ b/Makefile
@@ -1019,6 +1019,7 @@ include-$(CONFIG_UBSAN) += scripts/Makefile.ubsan
include-$(CONFIG_KCOV) += scripts/Makefile.kcov
include-$(CONFIG_RANDSTRUCT) += scripts/Makefile.randstruct
include-$(CONFIG_AUTOFDO_CLANG) += scripts/Makefile.autofdo
+include-$(CONFIG_PROPELLER_CLANG) += scripts/Makefile.propeller
include-$(CONFIG_GCC_PLUGINS) += scripts/Makefile.gcc-plugins
include $(addprefix $(srctree)/, $(include-y))
diff --git a/arch/Kconfig b/arch/Kconfig
index 8dca3b5e6ef53..00551f340dbe3 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -831,6 +831,25 @@ config AUTOFDO_CLANG
If unsure, say N.
+config ARCH_SUPPORTS_PROPELLER_CLANG
+ bool
+
+config PROPELLER_CLANG
+ bool "Enable Clang's Propeller build"
+ depends on ARCH_SUPPORTS_PROPELLER_CLANG
+ depends on CC_IS_CLANG && CLANG_VERSION >= 190000
+ help
+ This option enables Clang’s Propeller build. When the Propeller
+ profiles is specified in variable CLANG_PROPELLER_PROFILE_PREFIX
+ during the build process, Clang uses the profiles to optimize
+ the kernel.
+
+ If no profile is specified, Propeller options are still passed
+ to Clang to facilitate the collection of perf data for creating
+ the Propeller profiles in subsequent builds.
+
+ If unsure, say N.
+
config ARCH_SUPPORTS_CFI_CLANG
bool
help
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 9dc87661fb373..89b8fc452a7cf 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -127,6 +127,7 @@ config X86
select ARCH_SUPPORTS_LTO_CLANG_THIN
select ARCH_SUPPORTS_RT
select ARCH_SUPPORTS_AUTOFDO_CLANG
+ select ARCH_SUPPORTS_PROPELLER_CLANG if X86_64
select ARCH_USE_BUILTIN_BSWAP
select ARCH_USE_CMPXCHG_LOCKREF if X86_CMPXCHG64
select ARCH_USE_MEMTEST
diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S
index b8c5741d2fb48..cf22081601ed6 100644
--- a/arch/x86/kernel/vmlinux.lds.S
+++ b/arch/x86/kernel/vmlinux.lds.S
@@ -443,6 +443,10 @@ SECTIONS
STABS_DEBUG
DWARF_DEBUG
+#ifdef CONFIG_PROPELLER_CLANG
+ .llvm_bb_addr_map : { *(.llvm_bb_addr_map) }
+#endif
+
ELF_DETAILS
DISCARDS
diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index 8a0bb3946cf05..c995474e4c649 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -95,14 +95,14 @@
* With LTO_CLANG, the linker also splits sections by default, so we need
* these macros to combine the sections during the final link.
*
- * With AUTOFDO_CLANG, by default, the linker splits text sections and
- * regroups functions into subsections.
+ * With AUTOFDO_CLANG and PROPELLER_CLANG, by default, the linker splits
+ * text sections and regroups functions into subsections.
*
* RODATA_MAIN is not used because existing code already defines .rodata.x
* sections to be brought in with rodata.
*/
#if defined(CONFIG_LD_DEAD_CODE_DATA_ELIMINATION) || defined(CONFIG_LTO_CLANG) || \
-defined(CONFIG_AUTOFDO_CLANG)
+defined(CONFIG_AUTOFDO_CLANG) || defined(CONFIG_PROPELLER_CLANG)
#define TEXT_MAIN .text .text.[0-9a-zA-Z_]*
#else
#define TEXT_MAIN .text
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 2d0942c1a0277..e7859ad90224a 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -201,6 +201,16 @@ _c_flags += $(if $(patsubst n%,, \
$(CFLAGS_AUTOFDO_CLANG))
endif
+#
+# Enable Propeller build flags except some files or directories we don't want to
+# enable (depends on variables AUTOFDO_PROPELLER_obj.o and PROPELLER_PROFILE).
+#
+ifdef CONFIG_PROPELLER_CLANG
+_c_flags += $(if $(patsubst n%,, \
+ $(AUTOFDO_PROFILE_$(target-stem).o)$(AUTOFDO_PROFILE)$(PROPELLER_PROFILE))$(is-kernel-object), \
+ $(CFLAGS_PROPELLER_CLANG))
+endif
+
# $(src) for including checkin headers from generated source files
# $(obj) for including generated headers from checkin source files
ifeq ($(KBUILD_EXTMOD),)
diff --git a/scripts/Makefile.propeller b/scripts/Makefile.propeller
new file mode 100644
index 0000000000000..344190717e471
--- /dev/null
+++ b/scripts/Makefile.propeller
@@ -0,0 +1,28 @@
+# SPDX-License-Identifier: GPL-2.0
+
+# Enable available and selected Clang Propeller features.
+ifdef CLANG_PROPELLER_PROFILE_PREFIX
+ CFLAGS_PROPELLER_CLANG := -fbasic-block-sections=list=$(CLANG_PROPELLER_PROFILE_PREFIX)_cc_profile.txt -ffunction-sections
+ KBUILD_LDFLAGS += --symbol-ordering-file=$(CLANG_PROPELLER_PROFILE_PREFIX)_ld_profile.txt --no-warn-symbol-ordering
+else
+ CFLAGS_PROPELLER_CLANG := -fbasic-block-sections=labels
+endif
+
+# Propeller requires debug information to embed module names in the profiles.
+# If CONFIG_DEBUG_INFO is not enabled, set -gmlt option. Skip this for AutoFDO,
+# as the option should already be set.
+ifndef CONFIG_DEBUG_INFO
+ ifndef CONFIG_AUTOFDO_CLANG
+ CFLAGS_PROPELLER_CLANG += -gmlt
+ endif
+endif
+
+ifdef CONFIG_LTO_CLANG_THIN
+ ifdef CLANG_PROPELLER_PROFILE_PREFIX
+ KBUILD_LDFLAGS += --lto-basic-block-sections=$(CLANG_PROPELLER_PROFILE_PREFIX)_cc_profile.txt
+ else
+ KBUILD_LDFLAGS += --lto-basic-block-sections=labels
+ endif
+endif
+
+export CFLAGS_PROPELLER_CLANG
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 4c5229991e1e0..05a0fb4a3d1a0 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -4558,6 +4558,7 @@ static int validate_ibt(struct objtool_file *file)
!strcmp(sec->name, "__mcount_loc") ||
!strcmp(sec->name, ".kcfi_traps") ||
!strcmp(sec->name, ".llvm.call-graph-profile") ||
+ !strcmp(sec->name, ".llvm_bb_addr_map") ||
strstr(sec->name, "__patchable_function_entries"))
continue;
--
2.47.0.163.g1226f6d8fa-goog
^ permalink raw reply related [flat|nested] 30+ messages in thread
* Re: [PATCH v7 1/7] Add AutoFDO support for Clang build
2024-11-02 17:51 ` [PATCH v7 1/7] Add AutoFDO " Rong Xu
@ 2024-11-02 19:46 ` Peter Jung
2024-11-02 19:53 ` Peter Jung
0 siblings, 1 reply; 30+ messages in thread
From: Peter Jung @ 2024-11-02 19:46 UTC (permalink / raw)
To: Rong Xu, Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Sami Tolvanen, Thomas Gleixner,
Wei Yang, workflows, Miguel Ojeda, Maksim Panchenko,
David S. Miller, Andreas Larsson, Yonghong Song, Yabin Cui,
Krzysztof Pszeniczny, Sriraman Tallam, Stephane Eranian
Cc: x86, linux-arch, sparclinux, linux-doc, linux-kbuild,
linux-kernel, llvm
On 02.11.24 18:51, Rong Xu wrote:
> Add the build support for using Clang's AutoFDO. Building the kernel
> with AutoFDO does not reduce the optimization level from the
> compiler. AutoFDO uses hardware sampling to gather information about
> the frequency of execution of different code paths within a binary.
> This information is then used to guide the compiler's optimization
> decisions, resulting in a more efficient binary. Experiments
> showed that the kernel can improve up to 10% in latency.
>
> The support requires a Clang compiler after LLVM 17. This submission
> is limited to x86 platforms that support PMU features like LBR on
> Intel machines and AMD Zen3 BRS. Support for SPE on ARM 1,
> and BRBE on ARM 1 is part of planned future work.
>
> Here is an example workflow for AutoFDO kernel:
>
> 1) Build the kernel on the host machine with LLVM enabled, for example,
> $ make menuconfig LLVM=1
> Turn on AutoFDO build config:
> CONFIG_AUTOFDO_CLANG=y
> With a configuration that has LLVM enabled, use the following
> command:
> scripts/config -e AUTOFDO_CLANG
> After getting the config, build with
> $ make LLVM=1
>
> 2) Install the kernel on the test machine.
>
> 3) Run the load tests. The '-c' option in perf specifies the sample
> event period. We suggest using a suitable prime number,
> like 500009, for this purpose.
> For Intel platforms:
> $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c <count> \
> -o <perf_file> -- <loadtest>
> For AMD platforms:
> The supported system are: Zen3 with BRS, or Zen4 with amd_lbr_v2
> For Zen3:
> $ cat proc/cpuinfo | grep " brs"
> For Zen4:
> $ cat proc/cpuinfo | grep amd_lbr_v2
> $ perf record --pfm-events RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k -a \
> -N -b -c <count> -o <perf_file> -- <loadtest>
>
> 4) (Optional) Download the raw perf file to the host machine.
>
> 5) To generate an AutoFDO profile, two offline tools are available:
> create_llvm_prof and llvm_profgen. The create_llvm_prof tool is part
> of the AutoFDO project and can be found on GitHub
> (https://github.com/google/autofdo), version v0.30.1 or later. The
> llvm_profgen tool is included in the LLVM compiler itself. It's
> important to note that the version of llvm_profgen doesn't need to
> match the version of Clang. It needs to be the LLVM 19 release or
> later, or from the LLVM trunk.
> $ llvm-profgen --kernel --binary=<vmlinux> --perfdata=<perf_file> \
> -o <profile_file>
> or
> $ create_llvm_prof --binary=<vmlinux> --profile=<perf_file> \
> --format=extbinary --out=<profile_file>
>
> Note that multiple AutoFDO profile files can be merged into one via:
> $ llvm-profdata merge -o <profile_file> <profile_1> ... <profile_n>
>
> 6) Rebuild the kernel using the AutoFDO profile file with the same config
> as step 1, (Note CONFIG_AUTOFDO_CLANG needs to be enabled):
> $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<profile_file>
>
> Co-developed-by: Han Shen<shenhan@google.com>
> Signed-off-by: Han Shen<shenhan@google.com>
> Signed-off-by: Rong Xu<xur@google.com>
> Suggested-by: Sriraman Tallam<tmsriram@google.com>
> Suggested-by: Krzysztof Pszeniczny<kpszeniczny@google.com>
> Suggested-by: Nick Desaulniers<ndesaulniers@google.com>
> Suggested-by: Stephane Eranian<eranian@google.com>
> Tested-by: Yonghong Song<yonghong.song@linux.dev>
> Tested-by: Yabin Cui<yabinc@google.com>
> Tested-by: Nathan Chancellor<nathan@kernel.org>
> Reviewed-by: Kees Cook<kees@kernel.org>
Tested-by: Peter Jung <ptr1337@cachyos.org>
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 1/7] Add AutoFDO support for Clang build
2024-11-02 19:46 ` Peter Jung
@ 2024-11-02 19:53 ` Peter Jung
2024-11-04 4:50 ` Han Shen
0 siblings, 1 reply; 30+ messages in thread
From: Peter Jung @ 2024-11-02 19:53 UTC (permalink / raw)
To: Rong Xu, Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Sami Tolvanen, Thomas Gleixner,
Wei Yang, workflows, Miguel Ojeda, Maksim Panchenko,
David S. Miller, Andreas Larsson, Yonghong Song, Yabin Cui,
Krzysztof Pszeniczny, Sriraman Tallam, Stephane Eranian
Cc: x86, linux-arch, sparclinux, linux-doc, linux-kbuild,
linux-kernel, llvm
On 02.11.24 20:46, Peter Jung wrote:
>
>
> On 02.11.24 18:51, Rong Xu wrote:
>> Add the build support for using Clang's AutoFDO. Building the kernel
>> with AutoFDO does not reduce the optimization level from the
>> compiler. AutoFDO uses hardware sampling to gather information about
>> the frequency of execution of different code paths within a binary.
>> This information is then used to guide the compiler's optimization
>> decisions, resulting in a more efficient binary. Experiments
>> showed that the kernel can improve up to 10% in latency.
>>
>> The support requires a Clang compiler after LLVM 17. This submission
>> is limited to x86 platforms that support PMU features like LBR on
>> Intel machines and AMD Zen3 BRS. Support for SPE on ARM 1,
>> and BRBE on ARM 1 is part of planned future work.
>>
>> Here is an example workflow for AutoFDO kernel:
>>
>> 1) Build the kernel on the host machine with LLVM enabled, for example,
>> $ make menuconfig LLVM=1
>> Turn on AutoFDO build config:
>> CONFIG_AUTOFDO_CLANG=y
>> With a configuration that has LLVM enabled, use the following
>> command:
>> scripts/config -e AUTOFDO_CLANG
>> After getting the config, build with
>> $ make LLVM=1
>>
>> 2) Install the kernel on the test machine.
>>
>> 3) Run the load tests. The '-c' option in perf specifies the sample
>> event period. We suggest using a suitable prime number,
>> like 500009, for this purpose.
>> For Intel platforms:
>> $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c
>> <count> \
>> -o <perf_file> -- <loadtest>
>> For AMD platforms:
>> The supported system are: Zen3 with BRS, or Zen4 with amd_lbr_v2
>> For Zen3:
>> $ cat proc/cpuinfo | grep " brs"
>> For Zen4:
>> $ cat proc/cpuinfo | grep amd_lbr_v2
>> $ perf record --pfm-events RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k
>> -a \
>> -N -b -c <count> -o <perf_file> -- <loadtest>
>>
>> 4) (Optional) Download the raw perf file to the host machine.
>>
>> 5) To generate an AutoFDO profile, two offline tools are available:
>> create_llvm_prof and llvm_profgen. The create_llvm_prof tool is part
>> of the AutoFDO project and can be found on GitHub
>> (https://github.com/google/autofdo), version v0.30.1 or later. The
>> llvm_profgen tool is included in the LLVM compiler itself. It's
>> important to note that the version of llvm_profgen doesn't need to
>> match the version of Clang. It needs to be the LLVM 19 release or
>> later, or from the LLVM trunk.
>> $ llvm-profgen --kernel --binary=<vmlinux> --
>> perfdata=<perf_file> \
>> -o <profile_file>
>> or
>> $ create_llvm_prof --binary=<vmlinux> --profile=<perf_file> \
>> --format=extbinary --out=<profile_file>
>>
>> Note that multiple AutoFDO profile files can be merged into one via:
>> $ llvm-profdata merge -o <profile_file> <profile_1> ...
>> <profile_n>
>>
>> 6) Rebuild the kernel using the AutoFDO profile file with the same config
>> as step 1, (Note CONFIG_AUTOFDO_CLANG needs to be enabled):
>> $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<profile_file>
>>
>> Co-developed-by: Han Shen<shenhan@google.com>
>> Signed-off-by: Han Shen<shenhan@google.com>
>> Signed-off-by: Rong Xu<xur@google.com>
>> Suggested-by: Sriraman Tallam<tmsriram@google.com>
>> Suggested-by: Krzysztof Pszeniczny<kpszeniczny@google.com>
>> Suggested-by: Nick Desaulniers<ndesaulniers@google.com>
>> Suggested-by: Stephane Eranian<eranian@google.com>
>> Tested-by: Yonghong Song<yonghong.song@linux.dev>
>> Tested-by: Yabin Cui<yabinc@google.com>
>> Tested-by: Nathan Chancellor<nathan@kernel.org>
>> Reviewed-by: Kees Cook<kees@kernel.org>
>
> Tested-by: Peter Jung <ptr1337@cachyos.org>
>
The compilations and testing with the "make pacman-pkg" function from
the kernel worked fine.
One problem I do face:
When I apply a AutoFDO profile together with the PKGBUILD [1] from
archlinux im running into issues at "module_install" at the packaging.
See following log:
```
make[2]: *** [scripts/Makefile.modinst:125:
/tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/arch/x86/kvm/kvm.ko]
Error 1
make[2]: *** Deleting file
'/tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/arch/x86/kvm/kvm.ko'
INSTALL
/tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/crypto/cryptd.ko
make[2]: *** Waiting for unfinished jobs....
```
This can be fixed with removed "INSTALL_MOD_STRIP=1" to the passed
parameters of module_install.
This explicitly only happens, if a profile is passed - otherwise the
packaging works without problems.
Regards,
Peter Jung
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 1/7] Add AutoFDO support for Clang build
2024-11-02 19:53 ` Peter Jung
@ 2024-11-04 4:50 ` Han Shen
2024-11-04 16:05 ` Peter Jung
2024-11-04 17:30 ` Peter Jung
0 siblings, 2 replies; 30+ messages in thread
From: Han Shen @ 2024-11-04 4:50 UTC (permalink / raw)
To: Peter Jung
Cc: Rong Xu, Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Sami Tolvanen, Thomas Gleixner,
Wei Yang, workflows, Miguel Ojeda, Maksim Panchenko,
David S. Miller, Andreas Larsson, Yonghong Song, Yabin Cui,
Krzysztof Pszeniczny, Sriraman Tallam, Stephane Eranian, x86,
linux-arch, sparclinux, linux-doc, linux-kbuild, linux-kernel,
llvm
Hi Peter, thanks for reporting the issue. I am trying to reproduce it
in the up-to-date archlinux environment. Below is what I have:
0. pacman -Syu
1. cloned archlinux build files from
https://aur.archlinux.org/linux-mainline.git the newest mainline
version is 6.12rc5-1.
2. changed the PKGBUILD file to include the patches series
3. changed the "config" to turn on clang autofdo
4. collected afdo profiles
5. MAKEFLAGS="-j48 V=1 LLVM=1 CLANG_AUTOFDO_PROFILE=$(pwd)/perf.afdo" \
makepkg -s --skipinteg --skippgp
6. install and reboot
The above steps succeeded.
You mentioned the error happens at "module_install", can you instruct
me how to execute the "module_install" step?
Thanks,
Han
On Sat, Nov 2, 2024 at 12:53 PM Peter Jung <ptr1337@cachyos.org> wrote:
>
>
>
> On 02.11.24 20:46, Peter Jung wrote:
> >
> >
> > On 02.11.24 18:51, Rong Xu wrote:
> >> Add the build support for using Clang's AutoFDO. Building the kernel
> >> with AutoFDO does not reduce the optimization level from the
> >> compiler. AutoFDO uses hardware sampling to gather information about
> >> the frequency of execution of different code paths within a binary.
> >> This information is then used to guide the compiler's optimization
> >> decisions, resulting in a more efficient binary. Experiments
> >> showed that the kernel can improve up to 10% in latency.
> >>
> >> The support requires a Clang compiler after LLVM 17. This submission
> >> is limited to x86 platforms that support PMU features like LBR on
> >> Intel machines and AMD Zen3 BRS. Support for SPE on ARM 1,
> >> and BRBE on ARM 1 is part of planned future work.
> >>
> >> Here is an example workflow for AutoFDO kernel:
> >>
> >> 1) Build the kernel on the host machine with LLVM enabled, for example,
> >> $ make menuconfig LLVM=1
> >> Turn on AutoFDO build config:
> >> CONFIG_AUTOFDO_CLANG=y
> >> With a configuration that has LLVM enabled, use the following
> >> command:
> >> scripts/config -e AUTOFDO_CLANG
> >> After getting the config, build with
> >> $ make LLVM=1
> >>
> >> 2) Install the kernel on the test machine.
> >>
> >> 3) Run the load tests. The '-c' option in perf specifies the sample
> >> event period. We suggest using a suitable prime number,
> >> like 500009, for this purpose.
> >> For Intel platforms:
> >> $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c
> >> <count> \
> >> -o <perf_file> -- <loadtest>
> >> For AMD platforms:
> >> The supported system are: Zen3 with BRS, or Zen4 with amd_lbr_v2
> >> For Zen3:
> >> $ cat proc/cpuinfo | grep " brs"
> >> For Zen4:
> >> $ cat proc/cpuinfo | grep amd_lbr_v2
> >> $ perf record --pfm-events RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k
> >> -a \
> >> -N -b -c <count> -o <perf_file> -- <loadtest>
> >>
> >> 4) (Optional) Download the raw perf file to the host machine.
> >>
> >> 5) To generate an AutoFDO profile, two offline tools are available:
> >> create_llvm_prof and llvm_profgen. The create_llvm_prof tool is part
> >> of the AutoFDO project and can be found on GitHub
> >> (https://github.com/google/autofdo), version v0.30.1 or later. The
> >> llvm_profgen tool is included in the LLVM compiler itself. It's
> >> important to note that the version of llvm_profgen doesn't need to
> >> match the version of Clang. It needs to be the LLVM 19 release or
> >> later, or from the LLVM trunk.
> >> $ llvm-profgen --kernel --binary=<vmlinux> --
> >> perfdata=<perf_file> \
> >> -o <profile_file>
> >> or
> >> $ create_llvm_prof --binary=<vmlinux> --profile=<perf_file> \
> >> --format=extbinary --out=<profile_file>
> >>
> >> Note that multiple AutoFDO profile files can be merged into one via:
> >> $ llvm-profdata merge -o <profile_file> <profile_1> ...
> >> <profile_n>
> >>
> >> 6) Rebuild the kernel using the AutoFDO profile file with the same config
> >> as step 1, (Note CONFIG_AUTOFDO_CLANG needs to be enabled):
> >> $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<profile_file>
> >>
> >> Co-developed-by: Han Shen<shenhan@google.com>
> >> Signed-off-by: Han Shen<shenhan@google.com>
> >> Signed-off-by: Rong Xu<xur@google.com>
> >> Suggested-by: Sriraman Tallam<tmsriram@google.com>
> >> Suggested-by: Krzysztof Pszeniczny<kpszeniczny@google.com>
> >> Suggested-by: Nick Desaulniers<ndesaulniers@google.com>
> >> Suggested-by: Stephane Eranian<eranian@google.com>
> >> Tested-by: Yonghong Song<yonghong.song@linux.dev>
> >> Tested-by: Yabin Cui<yabinc@google.com>
> >> Tested-by: Nathan Chancellor<nathan@kernel.org>
> >> Reviewed-by: Kees Cook<kees@kernel.org>
> >
> > Tested-by: Peter Jung <ptr1337@cachyos.org>
> >
>
> The compilations and testing with the "make pacman-pkg" function from
> the kernel worked fine.
>
> One problem I do face:
> When I apply a AutoFDO profile together with the PKGBUILD [1] from
> archlinux im running into issues at "module_install" at the packaging.
>
> See following log:
> ```
> make[2]: *** [scripts/Makefile.modinst:125:
> /tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/arch/x86/kvm/kvm.ko]
> Error 1
> make[2]: *** Deleting file
> '/tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/arch/x86/kvm/kvm.ko'
> INSTALL
> /tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/crypto/cryptd.ko
> make[2]: *** Waiting for unfinished jobs....
> ```
>
>
> This can be fixed with removed "INSTALL_MOD_STRIP=1" to the passed
> parameters of module_install.
>
> This explicitly only happens, if a profile is passed - otherwise the
> packaging works without problems.
>
> Regards,
>
> Peter Jung
>
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 1/7] Add AutoFDO support for Clang build
2024-11-04 4:50 ` Han Shen
@ 2024-11-04 16:05 ` Peter Jung
2024-11-04 17:30 ` Peter Jung
1 sibling, 0 replies; 30+ messages in thread
From: Peter Jung @ 2024-11-04 16:05 UTC (permalink / raw)
To: Han Shen
Cc: Rong Xu, Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Sami Tolvanen, Thomas Gleixner,
Wei Yang, workflows, Miguel Ojeda, Maksim Panchenko,
David S. Miller, Andreas Larsson, Yonghong Song, Yabin Cui,
Krzysztof Pszeniczny, Sriraman Tallam, Stephane Eranian, x86,
linux-arch, sparclinux, linux-doc, linux-kbuild, linux-kernel,
llvm
Hi Han,
Thanks for the test. I will look into my current setup again and will
let you know.
Please do not see this as blocker for now. :)
Regards,
Peter
On 04.11.24 05:50, Han Shen wrote:
> Hi Peter, thanks for reporting the issue. I am trying to reproduce it
> in the up-to-date archlinux environment. Below is what I have:
> 0. pacman -Syu
> 1. cloned archlinux build files from
> https://aur.archlinux.org/linux-mainline.git the newest mainline
> version is 6.12rc5-1.
> 2. changed the PKGBUILD file to include the patches series
> 3. changed the "config" to turn on clang autofdo
> 4. collected afdo profiles
> 5. MAKEFLAGS="-j48 V=1 LLVM=1 CLANG_AUTOFDO_PROFILE=$(pwd)/perf.afdo" \
> makepkg -s --skipinteg --skippgp
> 6. install and reboot
> The above steps succeeded.
> You mentioned the error happens at "module_install", can you instruct
> me how to execute the "module_install" step?
>
> Thanks,
> Han
>
> On Sat, Nov 2, 2024 at 12:53 PM Peter Jung<ptr1337@cachyos.org> wrote:
>>
>>
>> On 02.11.24 20:46, Peter Jung wrote:
>>>
>>> On 02.11.24 18:51, Rong Xu wrote:
>>>> Add the build support for using Clang's AutoFDO. Building the kernel
>>>> with AutoFDO does not reduce the optimization level from the
>>>> compiler. AutoFDO uses hardware sampling to gather information about
>>>> the frequency of execution of different code paths within a binary.
>>>> This information is then used to guide the compiler's optimization
>>>> decisions, resulting in a more efficient binary. Experiments
>>>> showed that the kernel can improve up to 10% in latency.
>>>>
>>>> The support requires a Clang compiler after LLVM 17. This submission
>>>> is limited to x86 platforms that support PMU features like LBR on
>>>> Intel machines and AMD Zen3 BRS. Support for SPE on ARM 1,
>>>> and BRBE on ARM 1 is part of planned future work.
>>>>
>>>> Here is an example workflow for AutoFDO kernel:
>>>>
>>>> 1) Build the kernel on the host machine with LLVM enabled, for example,
>>>> $ make menuconfig LLVM=1
>>>> Turn on AutoFDO build config:
>>>> CONFIG_AUTOFDO_CLANG=y
>>>> With a configuration that has LLVM enabled, use the following
>>>> command:
>>>> scripts/config -e AUTOFDO_CLANG
>>>> After getting the config, build with
>>>> $ make LLVM=1
>>>>
>>>> 2) Install the kernel on the test machine.
>>>>
>>>> 3) Run the load tests. The '-c' option in perf specifies the sample
>>>> event period. We suggest using a suitable prime number,
>>>> like 500009, for this purpose.
>>>> For Intel platforms:
>>>> $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c
>>>> <count> \
>>>> -o <perf_file> -- <loadtest>
>>>> For AMD platforms:
>>>> The supported system are: Zen3 with BRS, or Zen4 with amd_lbr_v2
>>>> For Zen3:
>>>> $ cat proc/cpuinfo | grep " brs"
>>>> For Zen4:
>>>> $ cat proc/cpuinfo | grep amd_lbr_v2
>>>> $ perf record --pfm-events RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k
>>>> -a \
>>>> -N -b -c <count> -o <perf_file> -- <loadtest>
>>>>
>>>> 4) (Optional) Download the raw perf file to the host machine.
>>>>
>>>> 5) To generate an AutoFDO profile, two offline tools are available:
>>>> create_llvm_prof and llvm_profgen. The create_llvm_prof tool is part
>>>> of the AutoFDO project and can be found on GitHub
>>>> (https://github.com/google/autofdo), version v0.30.1 or later. The
>>>> llvm_profgen tool is included in the LLVM compiler itself. It's
>>>> important to note that the version of llvm_profgen doesn't need to
>>>> match the version of Clang. It needs to be the LLVM 19 release or
>>>> later, or from the LLVM trunk.
>>>> $ llvm-profgen --kernel --binary=<vmlinux> --
>>>> perfdata=<perf_file> \
>>>> -o <profile_file>
>>>> or
>>>> $ create_llvm_prof --binary=<vmlinux> --profile=<perf_file> \
>>>> --format=extbinary --out=<profile_file>
>>>>
>>>> Note that multiple AutoFDO profile files can be merged into one via:
>>>> $ llvm-profdata merge -o <profile_file> <profile_1> ...
>>>> <profile_n>
>>>>
>>>> 6) Rebuild the kernel using the AutoFDO profile file with the same config
>>>> as step 1, (Note CONFIG_AUTOFDO_CLANG needs to be enabled):
>>>> $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<profile_file>
>>>>
>>>> Co-developed-by: Han Shen<shenhan@google.com>
>>>> Signed-off-by: Han Shen<shenhan@google.com>
>>>> Signed-off-by: Rong Xu<xur@google.com>
>>>> Suggested-by: Sriraman Tallam<tmsriram@google.com>
>>>> Suggested-by: Krzysztof Pszeniczny<kpszeniczny@google.com>
>>>> Suggested-by: Nick Desaulniers<ndesaulniers@google.com>
>>>> Suggested-by: Stephane Eranian<eranian@google.com>
>>>> Tested-by: Yonghong Song<yonghong.song@linux.dev>
>>>> Tested-by: Yabin Cui<yabinc@google.com>
>>>> Tested-by: Nathan Chancellor<nathan@kernel.org>
>>>> Reviewed-by: Kees Cook<kees@kernel.org>
>>> Tested-by: Peter Jung<ptr1337@cachyos.org>
>>>
>> The compilations and testing with the "make pacman-pkg" function from
>> the kernel worked fine.
>>
>> One problem I do face:
>> When I apply a AutoFDO profile together with the PKGBUILD [1] from
>> archlinux im running into issues at "module_install" at the packaging.
>>
>> See following log:
>> ```
>> make[2]: *** [scripts/Makefile.modinst:125:
>> /tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/arch/x86/kvm/kvm.ko]
>> Error 1
>> make[2]: *** Deleting file
>> '/tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/arch/x86/kvm/kvm.ko'
>> INSTALL
>> /tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/crypto/cryptd.ko
>> make[2]: *** Waiting for unfinished jobs....
>> ```
>>
>>
>> This can be fixed with removed "INSTALL_MOD_STRIP=1" to the passed
>> parameters of module_install.
>>
>> This explicitly only happens, if a profile is passed - otherwise the
>> packaging works without problems.
>>
>> Regards,
>>
>> Peter Jung
>>
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 1/7] Add AutoFDO support for Clang build
2024-11-04 4:50 ` Han Shen
2024-11-04 16:05 ` Peter Jung
@ 2024-11-04 17:30 ` Peter Jung
2024-11-04 20:24 ` Han Shen
1 sibling, 1 reply; 30+ messages in thread
From: Peter Jung @ 2024-11-04 17:30 UTC (permalink / raw)
To: Han Shen
Cc: Rong Xu, Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Sami Tolvanen, Thomas Gleixner,
Wei Yang, workflows, Miguel Ojeda, Maksim Panchenko,
David S. Miller, Andreas Larsson, Yonghong Song, Yabin Cui,
Krzysztof Pszeniczny, Sriraman Tallam, Stephane Eranian, x86,
linux-arch, sparclinux, linux-doc, linux-kbuild, linux-kernel,
llvm
Hi Han,
I have tested your provided method, but the AutoFDO profile (lld does
not get lto-sample-profile=$pathtoprofile passed) nor Clang as compiler
gets used.
Please replace following PKGBUILD and config from linux-mainline with
the provided one in the gist. The patch is also included there.
https://gist.github.com/ptr1337/c92728bb273f7dbc2817db75eedec9ed
The main change I am doing here, is passing following to the build array
and replacing "make all":
make LLVM=1 LLVM_IAS=1 CLANG_AUTOFDO_PROFILE=${srcdir}/perf.afdo all
When compiling the kernel with makepkg, this results at the packaging to
following issue and can be reliable reproduced.
Regards,
Peter
On 04.11.24 05:50, Han Shen wrote:
> Hi Peter, thanks for reporting the issue. I am trying to reproduce it
> in the up-to-date archlinux environment. Below is what I have:
> 0. pacman -Syu
> 1. cloned archlinux build files from
> https://aur.archlinux.org/linux-mainline.git the newest mainline
> version is 6.12rc5-1.
> 2. changed the PKGBUILD file to include the patches series
> 3. changed the "config" to turn on clang autofdo
> 4. collected afdo profiles
> 5. MAKEFLAGS="-j48 V=1 LLVM=1 CLANG_AUTOFDO_PROFILE=$(pwd)/perf.afdo" \
> makepkg -s --skipinteg --skippgp
> 6. install and reboot
> The above steps succeeded.
> You mentioned the error happens at "module_install", can you instruct
> me how to execute the "module_install" step?
>
> Thanks,
> Han
>
> On Sat, Nov 2, 2024 at 12:53 PM Peter Jung<ptr1337@cachyos.org> wrote:
>>
>>
>> On 02.11.24 20:46, Peter Jung wrote:
>>>
>>> On 02.11.24 18:51, Rong Xu wrote:
>>>> Add the build support for using Clang's AutoFDO. Building the kernel
>>>> with AutoFDO does not reduce the optimization level from the
>>>> compiler. AutoFDO uses hardware sampling to gather information about
>>>> the frequency of execution of different code paths within a binary.
>>>> This information is then used to guide the compiler's optimization
>>>> decisions, resulting in a more efficient binary. Experiments
>>>> showed that the kernel can improve up to 10% in latency.
>>>>
>>>> The support requires a Clang compiler after LLVM 17. This submission
>>>> is limited to x86 platforms that support PMU features like LBR on
>>>> Intel machines and AMD Zen3 BRS. Support for SPE on ARM 1,
>>>> and BRBE on ARM 1 is part of planned future work.
>>>>
>>>> Here is an example workflow for AutoFDO kernel:
>>>>
>>>> 1) Build the kernel on the host machine with LLVM enabled, for example,
>>>> $ make menuconfig LLVM=1
>>>> Turn on AutoFDO build config:
>>>> CONFIG_AUTOFDO_CLANG=y
>>>> With a configuration that has LLVM enabled, use the following
>>>> command:
>>>> scripts/config -e AUTOFDO_CLANG
>>>> After getting the config, build with
>>>> $ make LLVM=1
>>>>
>>>> 2) Install the kernel on the test machine.
>>>>
>>>> 3) Run the load tests. The '-c' option in perf specifies the sample
>>>> event period. We suggest using a suitable prime number,
>>>> like 500009, for this purpose.
>>>> For Intel platforms:
>>>> $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c
>>>> <count> \
>>>> -o <perf_file> -- <loadtest>
>>>> For AMD platforms:
>>>> The supported system are: Zen3 with BRS, or Zen4 with amd_lbr_v2
>>>> For Zen3:
>>>> $ cat proc/cpuinfo | grep " brs"
>>>> For Zen4:
>>>> $ cat proc/cpuinfo | grep amd_lbr_v2
>>>> $ perf record --pfm-events RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k
>>>> -a \
>>>> -N -b -c <count> -o <perf_file> -- <loadtest>
>>>>
>>>> 4) (Optional) Download the raw perf file to the host machine.
>>>>
>>>> 5) To generate an AutoFDO profile, two offline tools are available:
>>>> create_llvm_prof and llvm_profgen. The create_llvm_prof tool is part
>>>> of the AutoFDO project and can be found on GitHub
>>>> (https://github.com/google/autofdo), version v0.30.1 or later. The
>>>> llvm_profgen tool is included in the LLVM compiler itself. It's
>>>> important to note that the version of llvm_profgen doesn't need to
>>>> match the version of Clang. It needs to be the LLVM 19 release or
>>>> later, or from the LLVM trunk.
>>>> $ llvm-profgen --kernel --binary=<vmlinux> --
>>>> perfdata=<perf_file> \
>>>> -o <profile_file>
>>>> or
>>>> $ create_llvm_prof --binary=<vmlinux> --profile=<perf_file> \
>>>> --format=extbinary --out=<profile_file>
>>>>
>>>> Note that multiple AutoFDO profile files can be merged into one via:
>>>> $ llvm-profdata merge -o <profile_file> <profile_1> ...
>>>> <profile_n>
>>>>
>>>> 6) Rebuild the kernel using the AutoFDO profile file with the same config
>>>> as step 1, (Note CONFIG_AUTOFDO_CLANG needs to be enabled):
>>>> $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<profile_file>
>>>>
>>>> Co-developed-by: Han Shen<shenhan@google.com>
>>>> Signed-off-by: Han Shen<shenhan@google.com>
>>>> Signed-off-by: Rong Xu<xur@google.com>
>>>> Suggested-by: Sriraman Tallam<tmsriram@google.com>
>>>> Suggested-by: Krzysztof Pszeniczny<kpszeniczny@google.com>
>>>> Suggested-by: Nick Desaulniers<ndesaulniers@google.com>
>>>> Suggested-by: Stephane Eranian<eranian@google.com>
>>>> Tested-by: Yonghong Song<yonghong.song@linux.dev>
>>>> Tested-by: Yabin Cui<yabinc@google.com>
>>>> Tested-by: Nathan Chancellor<nathan@kernel.org>
>>>> Reviewed-by: Kees Cook<kees@kernel.org>
>>> Tested-by: Peter Jung<ptr1337@cachyos.org>
>>>
>> The compilations and testing with the "make pacman-pkg" function from
>> the kernel worked fine.
>>
>> One problem I do face:
>> When I apply a AutoFDO profile together with the PKGBUILD [1] from
>> archlinux im running into issues at "module_install" at the packaging.
>>
>> See following log:
>> ```
>> make[2]: *** [scripts/Makefile.modinst:125:
>> /tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/arch/x86/kvm/kvm.ko]
>> Error 1
>> make[2]: *** Deleting file
>> '/tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/arch/x86/kvm/kvm.ko'
>> INSTALL
>> /tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/crypto/cryptd.ko
>> make[2]: *** Waiting for unfinished jobs....
>> ```
>>
>>
>> This can be fixed with removed "INSTALL_MOD_STRIP=1" to the passed
>> parameters of module_install.
>>
>> This explicitly only happens, if a profile is passed - otherwise the
>> packaging works without problems.
>>
>> Regards,
>>
>> Peter Jung
>>
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 1/7] Add AutoFDO support for Clang build
2024-11-04 17:30 ` Peter Jung
@ 2024-11-04 20:24 ` Han Shen
2024-11-05 7:25 ` Rong Xu
0 siblings, 1 reply; 30+ messages in thread
From: Han Shen @ 2024-11-04 20:24 UTC (permalink / raw)
To: Peter Jung
Cc: Rong Xu, Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Sami Tolvanen, Thomas Gleixner,
Wei Yang, workflows, Miguel Ojeda, Maksim Panchenko,
David S. Miller, Andreas Larsson, Yonghong Song, Yabin Cui,
Krzysztof Pszeniczny, Sriraman Tallam, Stephane Eranian, x86,
linux-arch, sparclinux, linux-doc, linux-kbuild, linux-kernel,
llvm
Hi Peter,
Thanks for providing the detailed reproduce.
Now I can see the error (after I synced to 6.12.0-rc6, I was using rc5).
I'll look into that and report back.
> I have tested your provided method, but the AutoFDO profile (lld does
not get lto-sample-profile=$pathtoprofile passed)
I see. You also turned on ThinLTO, which I didn't, so the profile was
only used during compilation, not passed to lld.
Thanks,
Han
On Mon, Nov 4, 2024 at 9:31 AM Peter Jung <ptr1337@cachyos.org> wrote:
>
> Hi Han,
>
> I have tested your provided method, but the AutoFDO profile (lld does
> not get lto-sample-profile=$pathtoprofile passed) nor Clang as compiler
> gets used.
> Please replace following PKGBUILD and config from linux-mainline with
> the provided one in the gist. The patch is also included there.
>
> https://gist.github.com/ptr1337/c92728bb273f7dbc2817db75eedec9ed
>
> The main change I am doing here, is passing following to the build array
> and replacing "make all":
>
> make LLVM=1 LLVM_IAS=1 CLANG_AUTOFDO_PROFILE=${srcdir}/perf.afdo all
>
> When compiling the kernel with makepkg, this results at the packaging to
> following issue and can be reliable reproduced.
>
> Regards,
>
> Peter
>
>
> On 04.11.24 05:50, Han Shen wrote:
> > Hi Peter, thanks for reporting the issue. I am trying to reproduce it
> > in the up-to-date archlinux environment. Below is what I have:
> > 0. pacman -Syu
> > 1. cloned archlinux build files from
> > https://aur.archlinux.org/linux-mainline.git the newest mainline
> > version is 6.12rc5-1.
> > 2. changed the PKGBUILD file to include the patches series
> > 3. changed the "config" to turn on clang autofdo
> > 4. collected afdo profiles
> > 5. MAKEFLAGS="-j48 V=1 LLVM=1 CLANG_AUTOFDO_PROFILE=$(pwd)/perf.afdo" \
> > makepkg -s --skipinteg --skippgp
> > 6. install and reboot
> > The above steps succeeded.
> > You mentioned the error happens at "module_install", can you instruct
> > me how to execute the "module_install" step?
> >
> > Thanks,
> > Han
> >
> > On Sat, Nov 2, 2024 at 12:53 PM Peter Jung<ptr1337@cachyos.org> wrote:
> >>
> >>
> >> On 02.11.24 20:46, Peter Jung wrote:
> >>>
> >>> On 02.11.24 18:51, Rong Xu wrote:
> >>>> Add the build support for using Clang's AutoFDO. Building the kernel
> >>>> with AutoFDO does not reduce the optimization level from the
> >>>> compiler. AutoFDO uses hardware sampling to gather information about
> >>>> the frequency of execution of different code paths within a binary.
> >>>> This information is then used to guide the compiler's optimization
> >>>> decisions, resulting in a more efficient binary. Experiments
> >>>> showed that the kernel can improve up to 10% in latency.
> >>>>
> >>>> The support requires a Clang compiler after LLVM 17. This submission
> >>>> is limited to x86 platforms that support PMU features like LBR on
> >>>> Intel machines and AMD Zen3 BRS. Support for SPE on ARM 1,
> >>>> and BRBE on ARM 1 is part of planned future work.
> >>>>
> >>>> Here is an example workflow for AutoFDO kernel:
> >>>>
> >>>> 1) Build the kernel on the host machine with LLVM enabled, for example,
> >>>> $ make menuconfig LLVM=1
> >>>> Turn on AutoFDO build config:
> >>>> CONFIG_AUTOFDO_CLANG=y
> >>>> With a configuration that has LLVM enabled, use the following
> >>>> command:
> >>>> scripts/config -e AUTOFDO_CLANG
> >>>> After getting the config, build with
> >>>> $ make LLVM=1
> >>>>
> >>>> 2) Install the kernel on the test machine.
> >>>>
> >>>> 3) Run the load tests. The '-c' option in perf specifies the sample
> >>>> event period. We suggest using a suitable prime number,
> >>>> like 500009, for this purpose.
> >>>> For Intel platforms:
> >>>> $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c
> >>>> <count> \
> >>>> -o <perf_file> -- <loadtest>
> >>>> For AMD platforms:
> >>>> The supported system are: Zen3 with BRS, or Zen4 with amd_lbr_v2
> >>>> For Zen3:
> >>>> $ cat proc/cpuinfo | grep " brs"
> >>>> For Zen4:
> >>>> $ cat proc/cpuinfo | grep amd_lbr_v2
> >>>> $ perf record --pfm-events RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k
> >>>> -a \
> >>>> -N -b -c <count> -o <perf_file> -- <loadtest>
> >>>>
> >>>> 4) (Optional) Download the raw perf file to the host machine.
> >>>>
> >>>> 5) To generate an AutoFDO profile, two offline tools are available:
> >>>> create_llvm_prof and llvm_profgen. The create_llvm_prof tool is part
> >>>> of the AutoFDO project and can be found on GitHub
> >>>> (https://github.com/google/autofdo), version v0.30.1 or later. The
> >>>> llvm_profgen tool is included in the LLVM compiler itself. It's
> >>>> important to note that the version of llvm_profgen doesn't need to
> >>>> match the version of Clang. It needs to be the LLVM 19 release or
> >>>> later, or from the LLVM trunk.
> >>>> $ llvm-profgen --kernel --binary=<vmlinux> --
> >>>> perfdata=<perf_file> \
> >>>> -o <profile_file>
> >>>> or
> >>>> $ create_llvm_prof --binary=<vmlinux> --profile=<perf_file> \
> >>>> --format=extbinary --out=<profile_file>
> >>>>
> >>>> Note that multiple AutoFDO profile files can be merged into one via:
> >>>> $ llvm-profdata merge -o <profile_file> <profile_1> ...
> >>>> <profile_n>
> >>>>
> >>>> 6) Rebuild the kernel using the AutoFDO profile file with the same config
> >>>> as step 1, (Note CONFIG_AUTOFDO_CLANG needs to be enabled):
> >>>> $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<profile_file>
> >>>>
> >>>> Co-developed-by: Han Shen<shenhan@google.com>
> >>>> Signed-off-by: Han Shen<shenhan@google.com>
> >>>> Signed-off-by: Rong Xu<xur@google.com>
> >>>> Suggested-by: Sriraman Tallam<tmsriram@google.com>
> >>>> Suggested-by: Krzysztof Pszeniczny<kpszeniczny@google.com>
> >>>> Suggested-by: Nick Desaulniers<ndesaulniers@google.com>
> >>>> Suggested-by: Stephane Eranian<eranian@google.com>
> >>>> Tested-by: Yonghong Song<yonghong.song@linux.dev>
> >>>> Tested-by: Yabin Cui<yabinc@google.com>
> >>>> Tested-by: Nathan Chancellor<nathan@kernel.org>
> >>>> Reviewed-by: Kees Cook<kees@kernel.org>
> >>> Tested-by: Peter Jung<ptr1337@cachyos.org>
> >>>
> >> The compilations and testing with the "make pacman-pkg" function from
> >> the kernel worked fine.
> >>
> >> One problem I do face:
> >> When I apply a AutoFDO profile together with the PKGBUILD [1] from
> >> archlinux im running into issues at "module_install" at the packaging.
> >>
> >> See following log:
> >> ```
> >> make[2]: *** [scripts/Makefile.modinst:125:
> >> /tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/arch/x86/kvm/kvm.ko]
> >> Error 1
> >> make[2]: *** Deleting file
> >> '/tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/arch/x86/kvm/kvm.ko'
> >> INSTALL
> >> /tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/crypto/cryptd.ko
> >> make[2]: *** Waiting for unfinished jobs....
> >> ```
> >>
> >>
> >> This can be fixed with removed "INSTALL_MOD_STRIP=1" to the passed
> >> parameters of module_install.
> >>
> >> This explicitly only happens, if a profile is passed - otherwise the
> >> packaging works without problems.
> >>
> >> Regards,
> >>
> >> Peter Jung
> >>
>
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 1/7] Add AutoFDO support for Clang build
2024-11-04 20:24 ` Han Shen
@ 2024-11-05 7:25 ` Rong Xu
2024-11-05 14:33 ` Peter Jung
0 siblings, 1 reply; 30+ messages in thread
From: Rong Xu @ 2024-11-05 7:25 UTC (permalink / raw)
To: Han Shen
Cc: Peter Jung, Alice Ryhl, Andrew Morton, Arnd Bergmann,
Bill Wendling, Borislav Petkov, Breno Leitao, Brian Gerst,
Dave Hansen, David Li, Heiko Carstens, H. Peter Anvin,
Ingo Molnar, Jann Horn, Jonathan Corbet, Josh Poimboeuf,
Juergen Gross, Justin Stitt, Kees Cook, Masahiro Yamada,
Mike Rapoport (IBM), Nathan Chancellor, Nick Desaulniers,
Nicolas Schier, Paul E. McKenney, Peter Zijlstra, Sami Tolvanen,
Thomas Gleixner, Wei Yang, workflows, Miguel Ojeda,
Maksim Panchenko, David S. Miller, Andreas Larsson, Yonghong Song,
Yabin Cui, Krzysztof Pszeniczny, Sriraman Tallam,
Stephane Eranian, x86, linux-arch, sparclinux, linux-doc,
linux-kbuild, linux-kernel, llvm
We debugged this issue and we found the failure seems to only happen
with strip (version 2.43) in binutil.
For a profile-use compilation, either with -fprofile-use (PGO or
iFDO), or -fprofile-sample-use (AutoFDO),
an ELF section of .llvm.call-graph-profile is created for the object.
For some reasons (like to save space?),
the relocations in this section are of type "rel', rather the more
common "rela" type.
In this case,
$ readelf -r kvm.ko |grep llvm.call-graph-profile
Relocation section '.rel.llvm.call-graph-profile' at offset 0xf62a00
contains 4 entries:
strip (v2.43.0) has difficulty handling the relocations in
.rel.llvm.call-graph-profile -- it silently failed with --strip-debug.
But strip (v.2.42) has no issue with kvm.ko. The strip in llvm (i.e.
llvm-strip) also passes with kvm.ko
I compared binutil/strip source code for version v2.43.0 and v2.42.
The different is around here:
In v2.42 of bfd/elfcode.h
1618 if ((entsize == sizeof (Elf_External_Rela)
1619 && ebd->elf_info_to_howto != NULL)
1620 || ebd->elf_info_to_howto_rel == NULL)
1621 res = ebd->elf_info_to_howto (abfd, relent, &rela);
1622 else
1623 res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
In v2.43.0 of bfd/elfcode.h
1618 if (entsize == sizeof (Elf_External_Rela)
1619 && ebd->elf_info_to_howto != NULL)
1620 res = ebd->elf_info_to_howto (abfd, relent, &rela);
1621 else if (ebd->elf_info_to_howto_rel != NULL)
1622 res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
In the 2.43 strip, line 1618 is false and line 1621 is also false.
"res" is returned as false and the program exits with -1.
While in 2.42, line 1620 is true and we get "res" from line 1621 and
program functions correctly.
I'm not familiar with binutil code base and don't know the reason for
removing line 1620.
I can file a bug for binutil for people to further investigate this.
It seems to me that this issue should not be a blocker for our patch.
Regards,
-Rong
On Mon, Nov 4, 2024 at 12:24 PM Han Shen <shenhan@google.com> wrote:
>
> Hi Peter,
> Thanks for providing the detailed reproduce.
> Now I can see the error (after I synced to 6.12.0-rc6, I was using rc5).
> I'll look into that and report back.
>
> > I have tested your provided method, but the AutoFDO profile (lld does
> not get lto-sample-profile=$pathtoprofile passed)
>
> I see. You also turned on ThinLTO, which I didn't, so the profile was
> only used during compilation, not passed to lld.
>
> Thanks,
> Han
>
> On Mon, Nov 4, 2024 at 9:31 AM Peter Jung <ptr1337@cachyos.org> wrote:
> >
> > Hi Han,
> >
> > I have tested your provided method, but the AutoFDO profile (lld does
> > not get lto-sample-profile=$pathtoprofile passed) nor Clang as compiler
> > gets used.
> > Please replace following PKGBUILD and config from linux-mainline with
> > the provided one in the gist. The patch is also included there.
> >
> > https://gist.github.com/ptr1337/c92728bb273f7dbc2817db75eedec9ed
> >
> > The main change I am doing here, is passing following to the build array
> > and replacing "make all":
> >
> > make LLVM=1 LLVM_IAS=1 CLANG_AUTOFDO_PROFILE=${srcdir}/perf.afdo all
> >
> > When compiling the kernel with makepkg, this results at the packaging to
> > following issue and can be reliable reproduced.
> >
> > Regards,
> >
> > Peter
> >
> >
> > On 04.11.24 05:50, Han Shen wrote:
> > > Hi Peter, thanks for reporting the issue. I am trying to reproduce it
> > > in the up-to-date archlinux environment. Below is what I have:
> > > 0. pacman -Syu
> > > 1. cloned archlinux build files from
> > > https://aur.archlinux.org/linux-mainline.git the newest mainline
> > > version is 6.12rc5-1.
> > > 2. changed the PKGBUILD file to include the patches series
> > > 3. changed the "config" to turn on clang autofdo
> > > 4. collected afdo profiles
> > > 5. MAKEFLAGS="-j48 V=1 LLVM=1 CLANG_AUTOFDO_PROFILE=$(pwd)/perf.afdo" \
> > > makepkg -s --skipinteg --skippgp
> > > 6. install and reboot
> > > The above steps succeeded.
> > > You mentioned the error happens at "module_install", can you instruct
> > > me how to execute the "module_install" step?
> > >
> > > Thanks,
> > > Han
> > >
> > > On Sat, Nov 2, 2024 at 12:53 PM Peter Jung<ptr1337@cachyos.org> wrote:
> > >>
> > >>
> > >> On 02.11.24 20:46, Peter Jung wrote:
> > >>>
> > >>> On 02.11.24 18:51, Rong Xu wrote:
> > >>>> Add the build support for using Clang's AutoFDO. Building the kernel
> > >>>> with AutoFDO does not reduce the optimization level from the
> > >>>> compiler. AutoFDO uses hardware sampling to gather information about
> > >>>> the frequency of execution of different code paths within a binary.
> > >>>> This information is then used to guide the compiler's optimization
> > >>>> decisions, resulting in a more efficient binary. Experiments
> > >>>> showed that the kernel can improve up to 10% in latency.
> > >>>>
> > >>>> The support requires a Clang compiler after LLVM 17. This submission
> > >>>> is limited to x86 platforms that support PMU features like LBR on
> > >>>> Intel machines and AMD Zen3 BRS. Support for SPE on ARM 1,
> > >>>> and BRBE on ARM 1 is part of planned future work.
> > >>>>
> > >>>> Here is an example workflow for AutoFDO kernel:
> > >>>>
> > >>>> 1) Build the kernel on the host machine with LLVM enabled, for example,
> > >>>> $ make menuconfig LLVM=1
> > >>>> Turn on AutoFDO build config:
> > >>>> CONFIG_AUTOFDO_CLANG=y
> > >>>> With a configuration that has LLVM enabled, use the following
> > >>>> command:
> > >>>> scripts/config -e AUTOFDO_CLANG
> > >>>> After getting the config, build with
> > >>>> $ make LLVM=1
> > >>>>
> > >>>> 2) Install the kernel on the test machine.
> > >>>>
> > >>>> 3) Run the load tests. The '-c' option in perf specifies the sample
> > >>>> event period. We suggest using a suitable prime number,
> > >>>> like 500009, for this purpose.
> > >>>> For Intel platforms:
> > >>>> $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c
> > >>>> <count> \
> > >>>> -o <perf_file> -- <loadtest>
> > >>>> For AMD platforms:
> > >>>> The supported system are: Zen3 with BRS, or Zen4 with amd_lbr_v2
> > >>>> For Zen3:
> > >>>> $ cat proc/cpuinfo | grep " brs"
> > >>>> For Zen4:
> > >>>> $ cat proc/cpuinfo | grep amd_lbr_v2
> > >>>> $ perf record --pfm-events RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k
> > >>>> -a \
> > >>>> -N -b -c <count> -o <perf_file> -- <loadtest>
> > >>>>
> > >>>> 4) (Optional) Download the raw perf file to the host machine.
> > >>>>
> > >>>> 5) To generate an AutoFDO profile, two offline tools are available:
> > >>>> create_llvm_prof and llvm_profgen. The create_llvm_prof tool is part
> > >>>> of the AutoFDO project and can be found on GitHub
> > >>>> (https://github.com/google/autofdo), version v0.30.1 or later. The
> > >>>> llvm_profgen tool is included in the LLVM compiler itself. It's
> > >>>> important to note that the version of llvm_profgen doesn't need to
> > >>>> match the version of Clang. It needs to be the LLVM 19 release or
> > >>>> later, or from the LLVM trunk.
> > >>>> $ llvm-profgen --kernel --binary=<vmlinux> --
> > >>>> perfdata=<perf_file> \
> > >>>> -o <profile_file>
> > >>>> or
> > >>>> $ create_llvm_prof --binary=<vmlinux> --profile=<perf_file> \
> > >>>> --format=extbinary --out=<profile_file>
> > >>>>
> > >>>> Note that multiple AutoFDO profile files can be merged into one via:
> > >>>> $ llvm-profdata merge -o <profile_file> <profile_1> ...
> > >>>> <profile_n>
> > >>>>
> > >>>> 6) Rebuild the kernel using the AutoFDO profile file with the same config
> > >>>> as step 1, (Note CONFIG_AUTOFDO_CLANG needs to be enabled):
> > >>>> $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<profile_file>
> > >>>>
> > >>>> Co-developed-by: Han Shen<shenhan@google.com>
> > >>>> Signed-off-by: Han Shen<shenhan@google.com>
> > >>>> Signed-off-by: Rong Xu<xur@google.com>
> > >>>> Suggested-by: Sriraman Tallam<tmsriram@google.com>
> > >>>> Suggested-by: Krzysztof Pszeniczny<kpszeniczny@google.com>
> > >>>> Suggested-by: Nick Desaulniers<ndesaulniers@google.com>
> > >>>> Suggested-by: Stephane Eranian<eranian@google.com>
> > >>>> Tested-by: Yonghong Song<yonghong.song@linux.dev>
> > >>>> Tested-by: Yabin Cui<yabinc@google.com>
> > >>>> Tested-by: Nathan Chancellor<nathan@kernel.org>
> > >>>> Reviewed-by: Kees Cook<kees@kernel.org>
> > >>> Tested-by: Peter Jung<ptr1337@cachyos.org>
> > >>>
> > >> The compilations and testing with the "make pacman-pkg" function from
> > >> the kernel worked fine.
> > >>
> > >> One problem I do face:
> > >> When I apply a AutoFDO profile together with the PKGBUILD [1] from
> > >> archlinux im running into issues at "module_install" at the packaging.
> > >>
> > >> See following log:
> > >> ```
> > >> make[2]: *** [scripts/Makefile.modinst:125:
> > >> /tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/arch/x86/kvm/kvm.ko]
> > >> Error 1
> > >> make[2]: *** Deleting file
> > >> '/tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/arch/x86/kvm/kvm.ko'
> > >> INSTALL
> > >> /tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/crypto/cryptd.ko
> > >> make[2]: *** Waiting for unfinished jobs....
> > >> ```
> > >>
> > >>
> > >> This can be fixed with removed "INSTALL_MOD_STRIP=1" to the passed
> > >> parameters of module_install.
> > >>
> > >> This explicitly only happens, if a profile is passed - otherwise the
> > >> packaging works without problems.
> > >>
> > >> Regards,
> > >>
> > >> Peter Jung
> > >>
> >
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 1/7] Add AutoFDO support for Clang build
2024-11-05 7:25 ` Rong Xu
@ 2024-11-05 14:33 ` Peter Jung
2024-11-05 14:56 ` Peter Jung
0 siblings, 1 reply; 30+ messages in thread
From: Peter Jung @ 2024-11-05 14:33 UTC (permalink / raw)
To: Rong Xu, Han Shen
Cc: Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Sami Tolvanen, Thomas Gleixner,
Wei Yang, workflows, Miguel Ojeda, Maksim Panchenko,
David S. Miller, Andreas Larsson, Yonghong Song, Yabin Cui,
Krzysztof Pszeniczny, Sriraman Tallam, Stephane Eranian, x86,
linux-arch, sparclinux, linux-doc, linux-kbuild, linux-kernel,
llvm
Hi Rong,
Glad that you were able to reproduce the issue!
Thanks for finding the root cause as well as the part of the code. This
really helps.
I was able to do a successful packaging with binutils 2.42.
Lets forward this to the binutils tracker and hope this will be soon
solved. :)
I have tested this also on the latest commit
(e1e4078ac59740a79cd709d61872abe15aba0087) and the issue is also
reproducible there.
Thanks for your time! I dont see this as blocker. :)
It gets time to get this series merged :P
Best regards,
Peter
On 05.11.24 08:25, Rong Xu wrote:
> We debugged this issue and we found the failure seems to only happen
> with strip (version 2.43) in binutil.
>
> For a profile-use compilation, either with -fprofile-use (PGO or
> iFDO), or -fprofile-sample-use (AutoFDO),
> an ELF section of .llvm.call-graph-profile is created for the object.
> For some reasons (like to save space?),
> the relocations in this section are of type "rel', rather the more
> common "rela" type.
>
> In this case,
> $ readelf -r kvm.ko |grep llvm.call-graph-profile
> Relocation section '.rel.llvm.call-graph-profile' at offset 0xf62a00
> contains 4 entries:
>
> strip (v2.43.0) has difficulty handling the relocations in
> .rel.llvm.call-graph-profile -- it silently failed with --strip-debug.
> But strip (v.2.42) has no issue with kvm.ko. The strip in llvm (i.e.
> llvm-strip) also passes with kvm.ko
>
> I compared binutil/strip source code for version v2.43.0 and v2.42.
> The different is around here:
> In v2.42 of bfd/elfcode.h
> 1618 if ((entsize == sizeof (Elf_External_Rela)
> 1619 && ebd->elf_info_to_howto != NULL)
> 1620 || ebd->elf_info_to_howto_rel == NULL)
> 1621 res = ebd->elf_info_to_howto (abfd, relent, &rela);
> 1622 else
> 1623 res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
>
> In v2.43.0 of bfd/elfcode.h
> 1618 if (entsize == sizeof (Elf_External_Rela)
> 1619 && ebd->elf_info_to_howto != NULL)
> 1620 res = ebd->elf_info_to_howto (abfd, relent, &rela);
> 1621 else if (ebd->elf_info_to_howto_rel != NULL)
> 1622 res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
>
> In the 2.43 strip, line 1618 is false and line 1621 is also false.
> "res" is returned as false and the program exits with -1.
>
> While in 2.42, line 1620 is true and we get "res" from line 1621 and
> program functions correctly.
>
> I'm not familiar with binutil code base and don't know the reason for
> removing line 1620.
> I can file a bug for binutil for people to further investigate this.
>
> It seems to me that this issue should not be a blocker for our patch.
>
> Regards,
>
> -Rong
>
>
>
>
>
> On Mon, Nov 4, 2024 at 12:24 PM Han Shen<shenhan@google.com> wrote:
>> Hi Peter,
>> Thanks for providing the detailed reproduce.
>> Now I can see the error (after I synced to 6.12.0-rc6, I was using rc5).
>> I'll look into that and report back.
>>
>>> I have tested your provided method, but the AutoFDO profile (lld does
>> not get lto-sample-profile=$pathtoprofile passed)
>>
>> I see. You also turned on ThinLTO, which I didn't, so the profile was
>> only used during compilation, not passed to lld.
>>
>> Thanks,
>> Han
>>
>> On Mon, Nov 4, 2024 at 9:31 AM Peter Jung<ptr1337@cachyos.org> wrote:
>>> Hi Han,
>>>
>>> I have tested your provided method, but the AutoFDO profile (lld does
>>> not get lto-sample-profile=$pathtoprofile passed) nor Clang as compiler
>>> gets used.
>>> Please replace following PKGBUILD and config from linux-mainline with
>>> the provided one in the gist. The patch is also included there.
>>>
>>> https://gist.github.com/ptr1337/c92728bb273f7dbc2817db75eedec9ed
>>>
>>> The main change I am doing here, is passing following to the build array
>>> and replacing "make all":
>>>
>>> make LLVM=1 LLVM_IAS=1 CLANG_AUTOFDO_PROFILE=${srcdir}/perf.afdo all
>>>
>>> When compiling the kernel with makepkg, this results at the packaging to
>>> following issue and can be reliable reproduced.
>>>
>>> Regards,
>>>
>>> Peter
>>>
>>>
>>> On 04.11.24 05:50, Han Shen wrote:
>>>> Hi Peter, thanks for reporting the issue. I am trying to reproduce it
>>>> in the up-to-date archlinux environment. Below is what I have:
>>>> 0. pacman -Syu
>>>> 1. cloned archlinux build files from
>>>> https://aur.archlinux.org/linux-mainline.git the newest mainline
>>>> version is 6.12rc5-1.
>>>> 2. changed the PKGBUILD file to include the patches series
>>>> 3. changed the "config" to turn on clang autofdo
>>>> 4. collected afdo profiles
>>>> 5. MAKEFLAGS="-j48 V=1 LLVM=1 CLANG_AUTOFDO_PROFILE=$(pwd)/perf.afdo" \
>>>> makepkg -s --skipinteg --skippgp
>>>> 6. install and reboot
>>>> The above steps succeeded.
>>>> You mentioned the error happens at "module_install", can you instruct
>>>> me how to execute the "module_install" step?
>>>>
>>>> Thanks,
>>>> Han
>>>>
>>>> On Sat, Nov 2, 2024 at 12:53 PM Peter Jung<ptr1337@cachyos.org> wrote:
>>>>>
>>>>> On 02.11.24 20:46, Peter Jung wrote:
>>>>>> On 02.11.24 18:51, Rong Xu wrote:
>>>>>>> Add the build support for using Clang's AutoFDO. Building the kernel
>>>>>>> with AutoFDO does not reduce the optimization level from the
>>>>>>> compiler. AutoFDO uses hardware sampling to gather information about
>>>>>>> the frequency of execution of different code paths within a binary.
>>>>>>> This information is then used to guide the compiler's optimization
>>>>>>> decisions, resulting in a more efficient binary. Experiments
>>>>>>> showed that the kernel can improve up to 10% in latency.
>>>>>>>
>>>>>>> The support requires a Clang compiler after LLVM 17. This submission
>>>>>>> is limited to x86 platforms that support PMU features like LBR on
>>>>>>> Intel machines and AMD Zen3 BRS. Support for SPE on ARM 1,
>>>>>>> and BRBE on ARM 1 is part of planned future work.
>>>>>>>
>>>>>>> Here is an example workflow for AutoFDO kernel:
>>>>>>>
>>>>>>> 1) Build the kernel on the host machine with LLVM enabled, for example,
>>>>>>> $ make menuconfig LLVM=1
>>>>>>> Turn on AutoFDO build config:
>>>>>>> CONFIG_AUTOFDO_CLANG=y
>>>>>>> With a configuration that has LLVM enabled, use the following
>>>>>>> command:
>>>>>>> scripts/config -e AUTOFDO_CLANG
>>>>>>> After getting the config, build with
>>>>>>> $ make LLVM=1
>>>>>>>
>>>>>>> 2) Install the kernel on the test machine.
>>>>>>>
>>>>>>> 3) Run the load tests. The '-c' option in perf specifies the sample
>>>>>>> event period. We suggest using a suitable prime number,
>>>>>>> like 500009, for this purpose.
>>>>>>> For Intel platforms:
>>>>>>> $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c
>>>>>>> <count> \
>>>>>>> -o <perf_file> -- <loadtest>
>>>>>>> For AMD platforms:
>>>>>>> The supported system are: Zen3 with BRS, or Zen4 with amd_lbr_v2
>>>>>>> For Zen3:
>>>>>>> $ cat proc/cpuinfo | grep " brs"
>>>>>>> For Zen4:
>>>>>>> $ cat proc/cpuinfo | grep amd_lbr_v2
>>>>>>> $ perf record --pfm-events RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k
>>>>>>> -a \
>>>>>>> -N -b -c <count> -o <perf_file> -- <loadtest>
>>>>>>>
>>>>>>> 4) (Optional) Download the raw perf file to the host machine.
>>>>>>>
>>>>>>> 5) To generate an AutoFDO profile, two offline tools are available:
>>>>>>> create_llvm_prof and llvm_profgen. The create_llvm_prof tool is part
>>>>>>> of the AutoFDO project and can be found on GitHub
>>>>>>> (https://github.com/google/autofdo), version v0.30.1 or later. The
>>>>>>> llvm_profgen tool is included in the LLVM compiler itself. It's
>>>>>>> important to note that the version of llvm_profgen doesn't need to
>>>>>>> match the version of Clang. It needs to be the LLVM 19 release or
>>>>>>> later, or from the LLVM trunk.
>>>>>>> $ llvm-profgen --kernel --binary=<vmlinux> --
>>>>>>> perfdata=<perf_file> \
>>>>>>> -o <profile_file>
>>>>>>> or
>>>>>>> $ create_llvm_prof --binary=<vmlinux> --profile=<perf_file> \
>>>>>>> --format=extbinary --out=<profile_file>
>>>>>>>
>>>>>>> Note that multiple AutoFDO profile files can be merged into one via:
>>>>>>> $ llvm-profdata merge -o <profile_file> <profile_1> ...
>>>>>>> <profile_n>
>>>>>>>
>>>>>>> 6) Rebuild the kernel using the AutoFDO profile file with the same config
>>>>>>> as step 1, (Note CONFIG_AUTOFDO_CLANG needs to be enabled):
>>>>>>> $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<profile_file>
>>>>>>>
>>>>>>> Co-developed-by: Han Shen<shenhan@google.com>
>>>>>>> Signed-off-by: Han Shen<shenhan@google.com>
>>>>>>> Signed-off-by: Rong Xu<xur@google.com>
>>>>>>> Suggested-by: Sriraman Tallam<tmsriram@google.com>
>>>>>>> Suggested-by: Krzysztof Pszeniczny<kpszeniczny@google.com>
>>>>>>> Suggested-by: Nick Desaulniers<ndesaulniers@google.com>
>>>>>>> Suggested-by: Stephane Eranian<eranian@google.com>
>>>>>>> Tested-by: Yonghong Song<yonghong.song@linux.dev>
>>>>>>> Tested-by: Yabin Cui<yabinc@google.com>
>>>>>>> Tested-by: Nathan Chancellor<nathan@kernel.org>
>>>>>>> Reviewed-by: Kees Cook<kees@kernel.org>
>>>>>> Tested-by: Peter Jung<ptr1337@cachyos.org>
>>>>>>
>>>>> The compilations and testing with the "make pacman-pkg" function from
>>>>> the kernel worked fine.
>>>>>
>>>>> One problem I do face:
>>>>> When I apply a AutoFDO profile together with the PKGBUILD [1] from
>>>>> archlinux im running into issues at "module_install" at the packaging.
>>>>>
>>>>> See following log:
>>>>> ```
>>>>> make[2]: *** [scripts/Makefile.modinst:125:
>>>>> /tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/arch/x86/kvm/kvm.ko]
>>>>> Error 1
>>>>> make[2]: *** Deleting file
>>>>> '/tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/arch/x86/kvm/kvm.ko'
>>>>> INSTALL
>>>>> /tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/crypto/cryptd.ko
>>>>> make[2]: *** Waiting for unfinished jobs....
>>>>> ```
>>>>>
>>>>>
>>>>> This can be fixed with removed "INSTALL_MOD_STRIP=1" to the passed
>>>>> parameters of module_install.
>>>>>
>>>>> This explicitly only happens, if a profile is passed - otherwise the
>>>>> packaging works without problems.
>>>>>
>>>>> Regards,
>>>>>
>>>>> Peter Jung
>>>>>
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 1/7] Add AutoFDO support for Clang build
2024-11-05 14:33 ` Peter Jung
@ 2024-11-05 14:56 ` Peter Jung
2024-11-05 17:19 ` Peter Jung
0 siblings, 1 reply; 30+ messages in thread
From: Peter Jung @ 2024-11-05 14:56 UTC (permalink / raw)
To: Rong Xu, Han Shen
Cc: Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Sami Tolvanen, Thomas Gleixner,
Wei Yang, workflows, Miguel Ojeda, Maksim Panchenko,
David S. Miller, Andreas Larsson, Yonghong Song, Yabin Cui,
Krzysztof Pszeniczny, Sriraman Tallam, Stephane Eranian, x86,
linux-arch, sparclinux, linux-doc, linux-kbuild, linux-kernel,
llvm
You were right - reverting commit:
https://github.com/bminor/binutils-gdb/commit/b20ab53f81db7eefa0db00d14f06c04527ac324c
from the 2.43 branch does fix the packaging.
I will forward this to an issue at their bugzilla.
On 05.11.24 15:33, Peter Jung wrote:
> Hi Rong,
>
> Glad that you were able to reproduce the issue!
> Thanks for finding the root cause as well as the part of the code. This
> really helps.
>
> I was able to do a successful packaging with binutils 2.42.
> Lets forward this to the binutils tracker and hope this will be soon
> solved. 🙂
>
> I have tested this also on the latest commit
> (e1e4078ac59740a79cd709d61872abe15aba0087) and the issue is also
> reproducible there.
>
> Thanks for your time! I dont see this as blocker. 🙂
> It gets time to get this series merged :P
>
> Best regards,
>
> Peter
>
>
>
> On 05.11.24 08:25, Rong Xu wrote:
>> We debugged this issue and we found the failure seems to only happen
>> with strip (version 2.43) in binutil.
>>
>> For a profile-use compilation, either with -fprofile-use (PGO or
>> iFDO), or -fprofile-sample-use (AutoFDO),
>> an ELF section of .llvm.call-graph-profile is created for the object.
>> For some reasons (like to save space?),
>> the relocations in this section are of type "rel', rather the more
>> common "rela" type.
>>
>> In this case,
>> $ readelf -r kvm.ko |grep llvm.call-graph-profile
>> Relocation section '.rel.llvm.call-graph-profile' at offset 0xf62a00
>> contains 4 entries:
>>
>> strip (v2.43.0) has difficulty handling the relocations in
>> .rel.llvm.call-graph-profile -- it silently failed with --strip-debug.
>> But strip (v.2.42) has no issue with kvm.ko. The strip in llvm (i.e.
>> llvm-strip) also passes with kvm.ko
>>
>> I compared binutil/strip source code for version v2.43.0 and v2.42.
>> The different is around here:
>> In v2.42 of bfd/elfcode.h
>> 1618 if ((entsize == sizeof (Elf_External_Rela)
>> 1619 && ebd->elf_info_to_howto != NULL)
>> 1620 || ebd->elf_info_to_howto_rel == NULL)
>> 1621 res = ebd->elf_info_to_howto (abfd, relent, &rela);
>> 1622 else
>> 1623 res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
>>
>> In v2.43.0 of bfd/elfcode.h
>> 1618 if (entsize == sizeof (Elf_External_Rela)
>> 1619 && ebd->elf_info_to_howto != NULL)
>> 1620 res = ebd->elf_info_to_howto (abfd, relent, &rela);
>> 1621 else if (ebd->elf_info_to_howto_rel != NULL)
>> 1622 res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
>>
>> In the 2.43 strip, line 1618 is false and line 1621 is also false.
>> "res" is returned as false and the program exits with -1.
>>
>> While in 2.42, line 1620 is true and we get "res" from line 1621 and
>> program functions correctly.
>>
>> I'm not familiar with binutil code base and don't know the reason for
>> removing line 1620.
>> I can file a bug for binutil for people to further investigate this.
>>
>> It seems to me that this issue should not be a blocker for our patch.
>>
>> Regards,
>>
>> -Rong
>>
>>
>>
>>
>>
>> On Mon, Nov 4, 2024 at 12:24 PM Han Shen<shenhan@google.com> wrote:
>>> Hi Peter,
>>> Thanks for providing the detailed reproduce.
>>> Now I can see the error (after I synced to 6.12.0-rc6, I was using rc5).
>>> I'll look into that and report back.
>>>
>>>> I have tested your provided method, but the AutoFDO profile (lld does
>>> not get lto-sample-profile=$pathtoprofile passed)
>>>
>>> I see. You also turned on ThinLTO, which I didn't, so the profile was
>>> only used during compilation, not passed to lld.
>>>
>>> Thanks,
>>> Han
>>>
>>> On Mon, Nov 4, 2024 at 9:31 AM Peter Jung<ptr1337@cachyos.org> wrote:
>>>> Hi Han,
>>>>
>>>> I have tested your provided method, but the AutoFDO profile (lld does
>>>> not get lto-sample-profile=$pathtoprofile passed) nor Clang as
>>>> compiler
>>>> gets used.
>>>> Please replace following PKGBUILD and config from linux-mainline with
>>>> the provided one in the gist. The patch is also included there.
>>>>
>>>> https://gist.github.com/ptr1337/c92728bb273f7dbc2817db75eedec9ed
>>>>
>>>> The main change I am doing here, is passing following to the build
>>>> array
>>>> and replacing "make all":
>>>>
>>>> make LLVM=1 LLVM_IAS=1 CLANG_AUTOFDO_PROFILE=${srcdir}/perf.afdo all
>>>>
>>>> When compiling the kernel with makepkg, this results at the
>>>> packaging to
>>>> following issue and can be reliable reproduced.
>>>>
>>>> Regards,
>>>>
>>>> Peter
>>>>
>>>>
>>>> On 04.11.24 05:50, Han Shen wrote:
>>>>> Hi Peter, thanks for reporting the issue. I am trying to reproduce it
>>>>> in the up-to-date archlinux environment. Below is what I have:
>>>>> 0. pacman -Syu
>>>>> 1. cloned archlinux build files from
>>>>> https://aur.archlinux.org/linux-mainline.git the newest mainline
>>>>> version is 6.12rc5-1.
>>>>> 2. changed the PKGBUILD file to include the patches series
>>>>> 3. changed the "config" to turn on clang autofdo
>>>>> 4. collected afdo profiles
>>>>> 5. MAKEFLAGS="-j48 V=1 LLVM=1 CLANG_AUTOFDO_PROFILE=$(pwd)/
>>>>> perf.afdo" \
>>>>> makepkg -s --skipinteg --skippgp
>>>>> 6. install and reboot
>>>>> The above steps succeeded.
>>>>> You mentioned the error happens at "module_install", can you instruct
>>>>> me how to execute the "module_install" step?
>>>>>
>>>>> Thanks,
>>>>> Han
>>>>>
>>>>> On Sat, Nov 2, 2024 at 12:53 PM Peter Jung<ptr1337@cachyos.org> wrote:
>>>>>>
>>>>>> On 02.11.24 20:46, Peter Jung wrote:
>>>>>>> On 02.11.24 18:51, Rong Xu wrote:
>>>>>>>> Add the build support for using Clang's AutoFDO. Building the
>>>>>>>> kernel
>>>>>>>> with AutoFDO does not reduce the optimization level from the
>>>>>>>> compiler. AutoFDO uses hardware sampling to gather information
>>>>>>>> about
>>>>>>>> the frequency of execution of different code paths within a binary.
>>>>>>>> This information is then used to guide the compiler's optimization
>>>>>>>> decisions, resulting in a more efficient binary. Experiments
>>>>>>>> showed that the kernel can improve up to 10% in latency.
>>>>>>>>
>>>>>>>> The support requires a Clang compiler after LLVM 17. This
>>>>>>>> submission
>>>>>>>> is limited to x86 platforms that support PMU features like LBR on
>>>>>>>> Intel machines and AMD Zen3 BRS. Support for SPE on ARM 1,
>>>>>>>> and BRBE on ARM 1 is part of planned future work.
>>>>>>>>
>>>>>>>> Here is an example workflow for AutoFDO kernel:
>>>>>>>>
>>>>>>>> 1) Build the kernel on the host machine with LLVM enabled, for
>>>>>>>> example,
>>>>>>>> $ make menuconfig LLVM=1
>>>>>>>> Turn on AutoFDO build config:
>>>>>>>> CONFIG_AUTOFDO_CLANG=y
>>>>>>>> With a configuration that has LLVM enabled, use the
>>>>>>>> following
>>>>>>>> command:
>>>>>>>> scripts/config -e AUTOFDO_CLANG
>>>>>>>> After getting the config, build with
>>>>>>>> $ make LLVM=1
>>>>>>>>
>>>>>>>> 2) Install the kernel on the test machine.
>>>>>>>>
>>>>>>>> 3) Run the load tests. The '-c' option in perf specifies the sample
>>>>>>>> event period. We suggest using a suitable prime number,
>>>>>>>> like 500009, for this purpose.
>>>>>>>> For Intel platforms:
>>>>>>>> $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c
>>>>>>>> <count> \
>>>>>>>> -o <perf_file> -- <loadtest>
>>>>>>>> For AMD platforms:
>>>>>>>> The supported system are: Zen3 with BRS, or Zen4 with
>>>>>>>> amd_lbr_v2
>>>>>>>> For Zen3:
>>>>>>>> $ cat proc/cpuinfo | grep " brs"
>>>>>>>> For Zen4:
>>>>>>>> $ cat proc/cpuinfo | grep amd_lbr_v2
>>>>>>>> $ perf record --pfm-events
>>>>>>>> RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k
>>>>>>>> -a \
>>>>>>>> -N -b -c <count> -o <perf_file> -- <loadtest>
>>>>>>>>
>>>>>>>> 4) (Optional) Download the raw perf file to the host machine.
>>>>>>>>
>>>>>>>> 5) To generate an AutoFDO profile, two offline tools are available:
>>>>>>>> create_llvm_prof and llvm_profgen. The create_llvm_prof
>>>>>>>> tool is part
>>>>>>>> of the AutoFDO project and can be found on GitHub
>>>>>>>> (https://github.com/google/autofdo), version v0.30.1 or
>>>>>>>> later. The
>>>>>>>> llvm_profgen tool is included in the LLVM compiler itself.
>>>>>>>> It's
>>>>>>>> important to note that the version of llvm_profgen doesn't
>>>>>>>> need to
>>>>>>>> match the version of Clang. It needs to be the LLVM 19
>>>>>>>> release or
>>>>>>>> later, or from the LLVM trunk.
>>>>>>>> $ llvm-profgen --kernel --binary=<vmlinux> --
>>>>>>>> perfdata=<perf_file> \
>>>>>>>> -o <profile_file>
>>>>>>>> or
>>>>>>>> $ create_llvm_prof --binary=<vmlinux> --
>>>>>>>> profile=<perf_file> \
>>>>>>>> --format=extbinary --out=<profile_file>
>>>>>>>>
>>>>>>>> Note that multiple AutoFDO profile files can be merged
>>>>>>>> into one via:
>>>>>>>> $ llvm-profdata merge -o <profile_file> <profile_1> ...
>>>>>>>> <profile_n>
>>>>>>>>
>>>>>>>> 6) Rebuild the kernel using the AutoFDO profile file with the
>>>>>>>> same config
>>>>>>>> as step 1, (Note CONFIG_AUTOFDO_CLANG needs to be enabled):
>>>>>>>> $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<profile_file>
>>>>>>>>
>>>>>>>> Co-developed-by: Han Shen<shenhan@google.com>
>>>>>>>> Signed-off-by: Han Shen<shenhan@google.com>
>>>>>>>> Signed-off-by: Rong Xu<xur@google.com>
>>>>>>>> Suggested-by: Sriraman Tallam<tmsriram@google.com>
>>>>>>>> Suggested-by: Krzysztof Pszeniczny<kpszeniczny@google.com>
>>>>>>>> Suggested-by: Nick Desaulniers<ndesaulniers@google.com>
>>>>>>>> Suggested-by: Stephane Eranian<eranian@google.com>
>>>>>>>> Tested-by: Yonghong Song<yonghong.song@linux.dev>
>>>>>>>> Tested-by: Yabin Cui<yabinc@google.com>
>>>>>>>> Tested-by: Nathan Chancellor<nathan@kernel.org>
>>>>>>>> Reviewed-by: Kees Cook<kees@kernel.org>
>>>>>>> Tested-by: Peter Jung<ptr1337@cachyos.org>
>>>>>>>
>>>>>> The compilations and testing with the "make pacman-pkg" function from
>>>>>> the kernel worked fine.
>>>>>>
>>>>>> One problem I do face:
>>>>>> When I apply a AutoFDO profile together with the PKGBUILD [1] from
>>>>>> archlinux im running into issues at "module_install" at the
>>>>>> packaging.
>>>>>>
>>>>>> See following log:
>>>>>> ```
>>>>>> make[2]: *** [scripts/Makefile.modinst:125:
>>>>>> /tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-
>>>>>> autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/
>>>>>> arch/x86/kvm/kvm.ko]
>>>>>> Error 1
>>>>>> make[2]: *** Deleting file
>>>>>> '/tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-
>>>>>> autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/
>>>>>> arch/x86/kvm/kvm.ko'
>>>>>> INSTALL
>>>>>> /tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-
>>>>>> autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/
>>>>>> crypto/cryptd.ko
>>>>>> make[2]: *** Waiting for unfinished jobs....
>>>>>> ```
>>>>>>
>>>>>>
>>>>>> This can be fixed with removed "INSTALL_MOD_STRIP=1" to the passed
>>>>>> parameters of module_install.
>>>>>>
>>>>>> This explicitly only happens, if a profile is passed - otherwise the
>>>>>> packaging works without problems.
>>>>>>
>>>>>> Regards,
>>>>>>
>>>>>> Peter Jung
>>>>>>
>
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 1/7] Add AutoFDO support for Clang build
2024-11-05 14:56 ` Peter Jung
@ 2024-11-05 17:19 ` Peter Jung
2024-11-05 17:51 ` Rong Xu
0 siblings, 1 reply; 30+ messages in thread
From: Peter Jung @ 2024-11-05 17:19 UTC (permalink / raw)
To: Rong Xu, Han Shen
Cc: Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Sami Tolvanen, Thomas Gleixner,
Wei Yang, workflows, Miguel Ojeda, Maksim Panchenko,
David S. Miller, Andreas Larsson, Yonghong Song, Yabin Cui,
Krzysztof Pszeniczny, Sriraman Tallam, Stephane Eranian, x86,
linux-arch, sparclinux, linux-doc, linux-kbuild, linux-kernel,
llvm
Here the bugreport, in case someone wants to track it:
https://sourceware.org/bugzilla/show_bug.cgi?id=32340
On 05.11.24 15:56, Peter Jung wrote:
> You were right - reverting commit:
> https://github.com/bminor/binutils-gdb/commit/
> b20ab53f81db7eefa0db00d14f06c04527ac324c from the 2.43 branch does fix
> the packaging.
>
> I will forward this to an issue at their bugzilla.
>
> On 05.11.24 15:33, Peter Jung wrote:
>> Hi Rong,
>>
>> Glad that you were able to reproduce the issue!
>> Thanks for finding the root cause as well as the part of the code.
>> This really helps.
>>
>> I was able to do a successful packaging with binutils 2.42.
>> Lets forward this to the binutils tracker and hope this will be soon
>> solved. 🙂
>>
>> I have tested this also on the latest commit
>> (e1e4078ac59740a79cd709d61872abe15aba0087) and the issue is also
>> reproducible there.
>>
>> Thanks for your time! I dont see this as blocker. 🙂
>> It gets time to get this series merged :P
>>
>> Best regards,
>>
>> Peter
>>
>>
>>
>> On 05.11.24 08:25, Rong Xu wrote:
>>> We debugged this issue and we found the failure seems to only happen
>>> with strip (version 2.43) in binutil.
>>>
>>> For a profile-use compilation, either with -fprofile-use (PGO or
>>> iFDO), or -fprofile-sample-use (AutoFDO),
>>> an ELF section of .llvm.call-graph-profile is created for the object.
>>> For some reasons (like to save space?),
>>> the relocations in this section are of type "rel', rather the more
>>> common "rela" type.
>>>
>>> In this case,
>>> $ readelf -r kvm.ko |grep llvm.call-graph-profile
>>> Relocation section '.rel.llvm.call-graph-profile' at offset 0xf62a00
>>> contains 4 entries:
>>>
>>> strip (v2.43.0) has difficulty handling the relocations in
>>> .rel.llvm.call-graph-profile -- it silently failed with --strip-debug.
>>> But strip (v.2.42) has no issue with kvm.ko. The strip in llvm (i.e.
>>> llvm-strip) also passes with kvm.ko
>>>
>>> I compared binutil/strip source code for version v2.43.0 and v2.42.
>>> The different is around here:
>>> In v2.42 of bfd/elfcode.h
>>> 1618 if ((entsize == sizeof (Elf_External_Rela)
>>> 1619 && ebd->elf_info_to_howto != NULL)
>>> 1620 || ebd->elf_info_to_howto_rel == NULL)
>>> 1621 res = ebd->elf_info_to_howto (abfd, relent, &rela);
>>> 1622 else
>>> 1623 res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
>>>
>>> In v2.43.0 of bfd/elfcode.h
>>> 1618 if (entsize == sizeof (Elf_External_Rela)
>>> 1619 && ebd->elf_info_to_howto != NULL)
>>> 1620 res = ebd->elf_info_to_howto (abfd, relent, &rela);
>>> 1621 else if (ebd->elf_info_to_howto_rel != NULL)
>>> 1622 res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
>>>
>>> In the 2.43 strip, line 1618 is false and line 1621 is also false.
>>> "res" is returned as false and the program exits with -1.
>>>
>>> While in 2.42, line 1620 is true and we get "res" from line 1621 and
>>> program functions correctly.
>>>
>>> I'm not familiar with binutil code base and don't know the reason for
>>> removing line 1620.
>>> I can file a bug for binutil for people to further investigate this.
>>>
>>> It seems to me that this issue should not be a blocker for our patch.
>>>
>>> Regards,
>>>
>>> -Rong
>>>
>>>
>>>
>>>
>>>
>>> On Mon, Nov 4, 2024 at 12:24 PM Han Shen<shenhan@google.com> wrote:
>>>> Hi Peter,
>>>> Thanks for providing the detailed reproduce.
>>>> Now I can see the error (after I synced to 6.12.0-rc6, I was using
>>>> rc5).
>>>> I'll look into that and report back.
>>>>
>>>>> I have tested your provided method, but the AutoFDO profile (lld does
>>>> not get lto-sample-profile=$pathtoprofile passed)
>>>>
>>>> I see. You also turned on ThinLTO, which I didn't, so the profile was
>>>> only used during compilation, not passed to lld.
>>>>
>>>> Thanks,
>>>> Han
>>>>
>>>> On Mon, Nov 4, 2024 at 9:31 AM Peter Jung<ptr1337@cachyos.org> wrote:
>>>>> Hi Han,
>>>>>
>>>>> I have tested your provided method, but the AutoFDO profile (lld does
>>>>> not get lto-sample-profile=$pathtoprofile passed) nor Clang as
>>>>> compiler
>>>>> gets used.
>>>>> Please replace following PKGBUILD and config from linux-mainline with
>>>>> the provided one in the gist. The patch is also included there.
>>>>>
>>>>> https://gist.github.com/ptr1337/c92728bb273f7dbc2817db75eedec9ed
>>>>>
>>>>> The main change I am doing here, is passing following to the build
>>>>> array
>>>>> and replacing "make all":
>>>>>
>>>>> make LLVM=1 LLVM_IAS=1 CLANG_AUTOFDO_PROFILE=${srcdir}/perf.afdo all
>>>>>
>>>>> When compiling the kernel with makepkg, this results at the
>>>>> packaging to
>>>>> following issue and can be reliable reproduced.
>>>>>
>>>>> Regards,
>>>>>
>>>>> Peter
>>>>>
>>>>>
>>>>> On 04.11.24 05:50, Han Shen wrote:
>>>>>> Hi Peter, thanks for reporting the issue. I am trying to reproduce it
>>>>>> in the up-to-date archlinux environment. Below is what I have:
>>>>>> 0. pacman -Syu
>>>>>> 1. cloned archlinux build files from
>>>>>> https://aur.archlinux.org/linux-mainline.git the newest mainline
>>>>>> version is 6.12rc5-1.
>>>>>> 2. changed the PKGBUILD file to include the patches series
>>>>>> 3. changed the "config" to turn on clang autofdo
>>>>>> 4. collected afdo profiles
>>>>>> 5. MAKEFLAGS="-j48 V=1 LLVM=1 CLANG_AUTOFDO_PROFILE=$(pwd)/
>>>>>> perf.afdo" \
>>>>>> makepkg -s --skipinteg --skippgp
>>>>>> 6. install and reboot
>>>>>> The above steps succeeded.
>>>>>> You mentioned the error happens at "module_install", can you instruct
>>>>>> me how to execute the "module_install" step?
>>>>>>
>>>>>> Thanks,
>>>>>> Han
>>>>>>
>>>>>> On Sat, Nov 2, 2024 at 12:53 PM Peter Jung<ptr1337@cachyos.org>
>>>>>> wrote:
>>>>>>>
>>>>>>> On 02.11.24 20:46, Peter Jung wrote:
>>>>>>>> On 02.11.24 18:51, Rong Xu wrote:
>>>>>>>>> Add the build support for using Clang's AutoFDO. Building the
>>>>>>>>> kernel
>>>>>>>>> with AutoFDO does not reduce the optimization level from the
>>>>>>>>> compiler. AutoFDO uses hardware sampling to gather information
>>>>>>>>> about
>>>>>>>>> the frequency of execution of different code paths within a
>>>>>>>>> binary.
>>>>>>>>> This information is then used to guide the compiler's optimization
>>>>>>>>> decisions, resulting in a more efficient binary. Experiments
>>>>>>>>> showed that the kernel can improve up to 10% in latency.
>>>>>>>>>
>>>>>>>>> The support requires a Clang compiler after LLVM 17. This
>>>>>>>>> submission
>>>>>>>>> is limited to x86 platforms that support PMU features like LBR on
>>>>>>>>> Intel machines and AMD Zen3 BRS. Support for SPE on ARM 1,
>>>>>>>>> and BRBE on ARM 1 is part of planned future work.
>>>>>>>>>
>>>>>>>>> Here is an example workflow for AutoFDO kernel:
>>>>>>>>>
>>>>>>>>> 1) Build the kernel on the host machine with LLVM enabled, for
>>>>>>>>> example,
>>>>>>>>> $ make menuconfig LLVM=1
>>>>>>>>> Turn on AutoFDO build config:
>>>>>>>>> CONFIG_AUTOFDO_CLANG=y
>>>>>>>>> With a configuration that has LLVM enabled, use the
>>>>>>>>> following
>>>>>>>>> command:
>>>>>>>>> scripts/config -e AUTOFDO_CLANG
>>>>>>>>> After getting the config, build with
>>>>>>>>> $ make LLVM=1
>>>>>>>>>
>>>>>>>>> 2) Install the kernel on the test machine.
>>>>>>>>>
>>>>>>>>> 3) Run the load tests. The '-c' option in perf specifies the
>>>>>>>>> sample
>>>>>>>>> event period. We suggest using a suitable prime number,
>>>>>>>>> like 500009, for this purpose.
>>>>>>>>> For Intel platforms:
>>>>>>>>> $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c
>>>>>>>>> <count> \
>>>>>>>>> -o <perf_file> -- <loadtest>
>>>>>>>>> For AMD platforms:
>>>>>>>>> The supported system are: Zen3 with BRS, or Zen4 with
>>>>>>>>> amd_lbr_v2
>>>>>>>>> For Zen3:
>>>>>>>>> $ cat proc/cpuinfo | grep " brs"
>>>>>>>>> For Zen4:
>>>>>>>>> $ cat proc/cpuinfo | grep amd_lbr_v2
>>>>>>>>> $ perf record --pfm-events
>>>>>>>>> RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k
>>>>>>>>> -a \
>>>>>>>>> -N -b -c <count> -o <perf_file> -- <loadtest>
>>>>>>>>>
>>>>>>>>> 4) (Optional) Download the raw perf file to the host machine.
>>>>>>>>>
>>>>>>>>> 5) To generate an AutoFDO profile, two offline tools are
>>>>>>>>> available:
>>>>>>>>> create_llvm_prof and llvm_profgen. The create_llvm_prof
>>>>>>>>> tool is part
>>>>>>>>> of the AutoFDO project and can be found on GitHub
>>>>>>>>> (https://github.com/google/autofdo), version v0.30.1 or
>>>>>>>>> later. The
>>>>>>>>> llvm_profgen tool is included in the LLVM compiler
>>>>>>>>> itself. It's
>>>>>>>>> important to note that the version of llvm_profgen
>>>>>>>>> doesn't need to
>>>>>>>>> match the version of Clang. It needs to be the LLVM 19
>>>>>>>>> release or
>>>>>>>>> later, or from the LLVM trunk.
>>>>>>>>> $ llvm-profgen --kernel --binary=<vmlinux> --
>>>>>>>>> perfdata=<perf_file> \
>>>>>>>>> -o <profile_file>
>>>>>>>>> or
>>>>>>>>> $ create_llvm_prof --binary=<vmlinux> --
>>>>>>>>> profile=<perf_file> \
>>>>>>>>> --format=extbinary --out=<profile_file>
>>>>>>>>>
>>>>>>>>> Note that multiple AutoFDO profile files can be merged
>>>>>>>>> into one via:
>>>>>>>>> $ llvm-profdata merge -o <profile_file> <profile_1> ...
>>>>>>>>> <profile_n>
>>>>>>>>>
>>>>>>>>> 6) Rebuild the kernel using the AutoFDO profile file with the
>>>>>>>>> same config
>>>>>>>>> as step 1, (Note CONFIG_AUTOFDO_CLANG needs to be enabled):
>>>>>>>>> $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<profile_file>
>>>>>>>>>
>>>>>>>>> Co-developed-by: Han Shen<shenhan@google.com>
>>>>>>>>> Signed-off-by: Han Shen<shenhan@google.com>
>>>>>>>>> Signed-off-by: Rong Xu<xur@google.com>
>>>>>>>>> Suggested-by: Sriraman Tallam<tmsriram@google.com>
>>>>>>>>> Suggested-by: Krzysztof Pszeniczny<kpszeniczny@google.com>
>>>>>>>>> Suggested-by: Nick Desaulniers<ndesaulniers@google.com>
>>>>>>>>> Suggested-by: Stephane Eranian<eranian@google.com>
>>>>>>>>> Tested-by: Yonghong Song<yonghong.song@linux.dev>
>>>>>>>>> Tested-by: Yabin Cui<yabinc@google.com>
>>>>>>>>> Tested-by: Nathan Chancellor<nathan@kernel.org>
>>>>>>>>> Reviewed-by: Kees Cook<kees@kernel.org>
>>>>>>>> Tested-by: Peter Jung<ptr1337@cachyos.org>
>>>>>>>>
>>>>>>> The compilations and testing with the "make pacman-pkg" function
>>>>>>> from
>>>>>>> the kernel worked fine.
>>>>>>>
>>>>>>> One problem I do face:
>>>>>>> When I apply a AutoFDO profile together with the PKGBUILD [1] from
>>>>>>> archlinux im running into issues at "module_install" at the
>>>>>>> packaging.
>>>>>>>
>>>>>>> See following log:
>>>>>>> ```
>>>>>>> make[2]: *** [scripts/Makefile.modinst:125:
>>>>>>> /tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-
>>>>>>> autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/
>>>>>>> arch/x86/kvm/kvm.ko]
>>>>>>> Error 1
>>>>>>> make[2]: *** Deleting file
>>>>>>> '/tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-
>>>>>>> autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/
>>>>>>> arch/x86/kvm/kvm.ko'
>>>>>>> INSTALL
>>>>>>> /tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-
>>>>>>> autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/
>>>>>>> crypto/cryptd.ko
>>>>>>> make[2]: *** Waiting for unfinished jobs....
>>>>>>> ```
>>>>>>>
>>>>>>>
>>>>>>> This can be fixed with removed "INSTALL_MOD_STRIP=1" to the passed
>>>>>>> parameters of module_install.
>>>>>>>
>>>>>>> This explicitly only happens, if a profile is passed - otherwise the
>>>>>>> packaging works without problems.
>>>>>>>
>>>>>>> Regards,
>>>>>>>
>>>>>>> Peter Jung
>>>>>>>
>>
>
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 1/7] Add AutoFDO support for Clang build
2024-11-05 17:19 ` Peter Jung
@ 2024-11-05 17:51 ` Rong Xu
0 siblings, 0 replies; 30+ messages in thread
From: Rong Xu @ 2024-11-05 17:51 UTC (permalink / raw)
To: Peter Jung
Cc: Han Shen, Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Sami Tolvanen, Thomas Gleixner,
Wei Yang, workflows, Miguel Ojeda, Maksim Panchenko,
David S. Miller, Andreas Larsson, Yonghong Song, Yabin Cui,
Krzysztof Pszeniczny, Sriraman Tallam, Stephane Eranian, x86,
linux-arch, sparclinux, linux-doc, linux-kbuild, linux-kernel,
llvm
Hi Peter, thanks for verifying and filing the bug report!
-Rong
On Tue, Nov 5, 2024 at 9:19 AM Peter Jung <ptr1337@cachyos.org> wrote:
>
> Here the bugreport, in case someone wants to track it:
>
> https://sourceware.org/bugzilla/show_bug.cgi?id=32340
>
> On 05.11.24 15:56, Peter Jung wrote:
> > You were right - reverting commit:
> > https://github.com/bminor/binutils-gdb/commit/
> > b20ab53f81db7eefa0db00d14f06c04527ac324c from the 2.43 branch does fix
> > the packaging.
> >
> > I will forward this to an issue at their bugzilla.
> >
> > On 05.11.24 15:33, Peter Jung wrote:
> >> Hi Rong,
> >>
> >> Glad that you were able to reproduce the issue!
> >> Thanks for finding the root cause as well as the part of the code.
> >> This really helps.
> >>
> >> I was able to do a successful packaging with binutils 2.42.
> >> Lets forward this to the binutils tracker and hope this will be soon
> >> solved. 🙂
> >>
> >> I have tested this also on the latest commit
> >> (e1e4078ac59740a79cd709d61872abe15aba0087) and the issue is also
> >> reproducible there.
> >>
> >> Thanks for your time! I dont see this as blocker. 🙂
> >> It gets time to get this series merged :P
> >>
> >> Best regards,
> >>
> >> Peter
> >>
> >>
> >>
> >> On 05.11.24 08:25, Rong Xu wrote:
> >>> We debugged this issue and we found the failure seems to only happen
> >>> with strip (version 2.43) in binutil.
> >>>
> >>> For a profile-use compilation, either with -fprofile-use (PGO or
> >>> iFDO), or -fprofile-sample-use (AutoFDO),
> >>> an ELF section of .llvm.call-graph-profile is created for the object.
> >>> For some reasons (like to save space?),
> >>> the relocations in this section are of type "rel', rather the more
> >>> common "rela" type.
> >>>
> >>> In this case,
> >>> $ readelf -r kvm.ko |grep llvm.call-graph-profile
> >>> Relocation section '.rel.llvm.call-graph-profile' at offset 0xf62a00
> >>> contains 4 entries:
> >>>
> >>> strip (v2.43.0) has difficulty handling the relocations in
> >>> .rel.llvm.call-graph-profile -- it silently failed with --strip-debug.
> >>> But strip (v.2.42) has no issue with kvm.ko. The strip in llvm (i.e.
> >>> llvm-strip) also passes with kvm.ko
> >>>
> >>> I compared binutil/strip source code for version v2.43.0 and v2.42.
> >>> The different is around here:
> >>> In v2.42 of bfd/elfcode.h
> >>> 1618 if ((entsize == sizeof (Elf_External_Rela)
> >>> 1619 && ebd->elf_info_to_howto != NULL)
> >>> 1620 || ebd->elf_info_to_howto_rel == NULL)
> >>> 1621 res = ebd->elf_info_to_howto (abfd, relent, &rela);
> >>> 1622 else
> >>> 1623 res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
> >>>
> >>> In v2.43.0 of bfd/elfcode.h
> >>> 1618 if (entsize == sizeof (Elf_External_Rela)
> >>> 1619 && ebd->elf_info_to_howto != NULL)
> >>> 1620 res = ebd->elf_info_to_howto (abfd, relent, &rela);
> >>> 1621 else if (ebd->elf_info_to_howto_rel != NULL)
> >>> 1622 res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
> >>>
> >>> In the 2.43 strip, line 1618 is false and line 1621 is also false.
> >>> "res" is returned as false and the program exits with -1.
> >>>
> >>> While in 2.42, line 1620 is true and we get "res" from line 1621 and
> >>> program functions correctly.
> >>>
> >>> I'm not familiar with binutil code base and don't know the reason for
> >>> removing line 1620.
> >>> I can file a bug for binutil for people to further investigate this.
> >>>
> >>> It seems to me that this issue should not be a blocker for our patch.
> >>>
> >>> Regards,
> >>>
> >>> -Rong
> >>>
> >>>
> >>>
> >>>
> >>>
> >>> On Mon, Nov 4, 2024 at 12:24 PM Han Shen<shenhan@google.com> wrote:
> >>>> Hi Peter,
> >>>> Thanks for providing the detailed reproduce.
> >>>> Now I can see the error (after I synced to 6.12.0-rc6, I was using
> >>>> rc5).
> >>>> I'll look into that and report back.
> >>>>
> >>>>> I have tested your provided method, but the AutoFDO profile (lld does
> >>>> not get lto-sample-profile=$pathtoprofile passed)
> >>>>
> >>>> I see. You also turned on ThinLTO, which I didn't, so the profile was
> >>>> only used during compilation, not passed to lld.
> >>>>
> >>>> Thanks,
> >>>> Han
> >>>>
> >>>> On Mon, Nov 4, 2024 at 9:31 AM Peter Jung<ptr1337@cachyos.org> wrote:
> >>>>> Hi Han,
> >>>>>
> >>>>> I have tested your provided method, but the AutoFDO profile (lld does
> >>>>> not get lto-sample-profile=$pathtoprofile passed) nor Clang as
> >>>>> compiler
> >>>>> gets used.
> >>>>> Please replace following PKGBUILD and config from linux-mainline with
> >>>>> the provided one in the gist. The patch is also included there.
> >>>>>
> >>>>> https://gist.github.com/ptr1337/c92728bb273f7dbc2817db75eedec9ed
> >>>>>
> >>>>> The main change I am doing here, is passing following to the build
> >>>>> array
> >>>>> and replacing "make all":
> >>>>>
> >>>>> make LLVM=1 LLVM_IAS=1 CLANG_AUTOFDO_PROFILE=${srcdir}/perf.afdo all
> >>>>>
> >>>>> When compiling the kernel with makepkg, this results at the
> >>>>> packaging to
> >>>>> following issue and can be reliable reproduced.
> >>>>>
> >>>>> Regards,
> >>>>>
> >>>>> Peter
> >>>>>
> >>>>>
> >>>>> On 04.11.24 05:50, Han Shen wrote:
> >>>>>> Hi Peter, thanks for reporting the issue. I am trying to reproduce it
> >>>>>> in the up-to-date archlinux environment. Below is what I have:
> >>>>>> 0. pacman -Syu
> >>>>>> 1. cloned archlinux build files from
> >>>>>> https://aur.archlinux.org/linux-mainline.git the newest mainline
> >>>>>> version is 6.12rc5-1.
> >>>>>> 2. changed the PKGBUILD file to include the patches series
> >>>>>> 3. changed the "config" to turn on clang autofdo
> >>>>>> 4. collected afdo profiles
> >>>>>> 5. MAKEFLAGS="-j48 V=1 LLVM=1 CLANG_AUTOFDO_PROFILE=$(pwd)/
> >>>>>> perf.afdo" \
> >>>>>> makepkg -s --skipinteg --skippgp
> >>>>>> 6. install and reboot
> >>>>>> The above steps succeeded.
> >>>>>> You mentioned the error happens at "module_install", can you instruct
> >>>>>> me how to execute the "module_install" step?
> >>>>>>
> >>>>>> Thanks,
> >>>>>> Han
> >>>>>>
> >>>>>> On Sat, Nov 2, 2024 at 12:53 PM Peter Jung<ptr1337@cachyos.org>
> >>>>>> wrote:
> >>>>>>>
> >>>>>>> On 02.11.24 20:46, Peter Jung wrote:
> >>>>>>>> On 02.11.24 18:51, Rong Xu wrote:
> >>>>>>>>> Add the build support for using Clang's AutoFDO. Building the
> >>>>>>>>> kernel
> >>>>>>>>> with AutoFDO does not reduce the optimization level from the
> >>>>>>>>> compiler. AutoFDO uses hardware sampling to gather information
> >>>>>>>>> about
> >>>>>>>>> the frequency of execution of different code paths within a
> >>>>>>>>> binary.
> >>>>>>>>> This information is then used to guide the compiler's optimization
> >>>>>>>>> decisions, resulting in a more efficient binary. Experiments
> >>>>>>>>> showed that the kernel can improve up to 10% in latency.
> >>>>>>>>>
> >>>>>>>>> The support requires a Clang compiler after LLVM 17. This
> >>>>>>>>> submission
> >>>>>>>>> is limited to x86 platforms that support PMU features like LBR on
> >>>>>>>>> Intel machines and AMD Zen3 BRS. Support for SPE on ARM 1,
> >>>>>>>>> and BRBE on ARM 1 is part of planned future work.
> >>>>>>>>>
> >>>>>>>>> Here is an example workflow for AutoFDO kernel:
> >>>>>>>>>
> >>>>>>>>> 1) Build the kernel on the host machine with LLVM enabled, for
> >>>>>>>>> example,
> >>>>>>>>> $ make menuconfig LLVM=1
> >>>>>>>>> Turn on AutoFDO build config:
> >>>>>>>>> CONFIG_AUTOFDO_CLANG=y
> >>>>>>>>> With a configuration that has LLVM enabled, use the
> >>>>>>>>> following
> >>>>>>>>> command:
> >>>>>>>>> scripts/config -e AUTOFDO_CLANG
> >>>>>>>>> After getting the config, build with
> >>>>>>>>> $ make LLVM=1
> >>>>>>>>>
> >>>>>>>>> 2) Install the kernel on the test machine.
> >>>>>>>>>
> >>>>>>>>> 3) Run the load tests. The '-c' option in perf specifies the
> >>>>>>>>> sample
> >>>>>>>>> event period. We suggest using a suitable prime number,
> >>>>>>>>> like 500009, for this purpose.
> >>>>>>>>> For Intel platforms:
> >>>>>>>>> $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c
> >>>>>>>>> <count> \
> >>>>>>>>> -o <perf_file> -- <loadtest>
> >>>>>>>>> For AMD platforms:
> >>>>>>>>> The supported system are: Zen3 with BRS, or Zen4 with
> >>>>>>>>> amd_lbr_v2
> >>>>>>>>> For Zen3:
> >>>>>>>>> $ cat proc/cpuinfo | grep " brs"
> >>>>>>>>> For Zen4:
> >>>>>>>>> $ cat proc/cpuinfo | grep amd_lbr_v2
> >>>>>>>>> $ perf record --pfm-events
> >>>>>>>>> RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k
> >>>>>>>>> -a \
> >>>>>>>>> -N -b -c <count> -o <perf_file> -- <loadtest>
> >>>>>>>>>
> >>>>>>>>> 4) (Optional) Download the raw perf file to the host machine.
> >>>>>>>>>
> >>>>>>>>> 5) To generate an AutoFDO profile, two offline tools are
> >>>>>>>>> available:
> >>>>>>>>> create_llvm_prof and llvm_profgen. The create_llvm_prof
> >>>>>>>>> tool is part
> >>>>>>>>> of the AutoFDO project and can be found on GitHub
> >>>>>>>>> (https://github.com/google/autofdo), version v0.30.1 or
> >>>>>>>>> later. The
> >>>>>>>>> llvm_profgen tool is included in the LLVM compiler
> >>>>>>>>> itself. It's
> >>>>>>>>> important to note that the version of llvm_profgen
> >>>>>>>>> doesn't need to
> >>>>>>>>> match the version of Clang. It needs to be the LLVM 19
> >>>>>>>>> release or
> >>>>>>>>> later, or from the LLVM trunk.
> >>>>>>>>> $ llvm-profgen --kernel --binary=<vmlinux> --
> >>>>>>>>> perfdata=<perf_file> \
> >>>>>>>>> -o <profile_file>
> >>>>>>>>> or
> >>>>>>>>> $ create_llvm_prof --binary=<vmlinux> --
> >>>>>>>>> profile=<perf_file> \
> >>>>>>>>> --format=extbinary --out=<profile_file>
> >>>>>>>>>
> >>>>>>>>> Note that multiple AutoFDO profile files can be merged
> >>>>>>>>> into one via:
> >>>>>>>>> $ llvm-profdata merge -o <profile_file> <profile_1> ...
> >>>>>>>>> <profile_n>
> >>>>>>>>>
> >>>>>>>>> 6) Rebuild the kernel using the AutoFDO profile file with the
> >>>>>>>>> same config
> >>>>>>>>> as step 1, (Note CONFIG_AUTOFDO_CLANG needs to be enabled):
> >>>>>>>>> $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<profile_file>
> >>>>>>>>>
> >>>>>>>>> Co-developed-by: Han Shen<shenhan@google.com>
> >>>>>>>>> Signed-off-by: Han Shen<shenhan@google.com>
> >>>>>>>>> Signed-off-by: Rong Xu<xur@google.com>
> >>>>>>>>> Suggested-by: Sriraman Tallam<tmsriram@google.com>
> >>>>>>>>> Suggested-by: Krzysztof Pszeniczny<kpszeniczny@google.com>
> >>>>>>>>> Suggested-by: Nick Desaulniers<ndesaulniers@google.com>
> >>>>>>>>> Suggested-by: Stephane Eranian<eranian@google.com>
> >>>>>>>>> Tested-by: Yonghong Song<yonghong.song@linux.dev>
> >>>>>>>>> Tested-by: Yabin Cui<yabinc@google.com>
> >>>>>>>>> Tested-by: Nathan Chancellor<nathan@kernel.org>
> >>>>>>>>> Reviewed-by: Kees Cook<kees@kernel.org>
> >>>>>>>> Tested-by: Peter Jung<ptr1337@cachyos.org>
> >>>>>>>>
> >>>>>>> The compilations and testing with the "make pacman-pkg" function
> >>>>>>> from
> >>>>>>> the kernel worked fine.
> >>>>>>>
> >>>>>>> One problem I do face:
> >>>>>>> When I apply a AutoFDO profile together with the PKGBUILD [1] from
> >>>>>>> archlinux im running into issues at "module_install" at the
> >>>>>>> packaging.
> >>>>>>>
> >>>>>>> See following log:
> >>>>>>> ```
> >>>>>>> make[2]: *** [scripts/Makefile.modinst:125:
> >>>>>>> /tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-
> >>>>>>> autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/
> >>>>>>> arch/x86/kvm/kvm.ko]
> >>>>>>> Error 1
> >>>>>>> make[2]: *** Deleting file
> >>>>>>> '/tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-
> >>>>>>> autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/
> >>>>>>> arch/x86/kvm/kvm.ko'
> >>>>>>> INSTALL
> >>>>>>> /tmp/makepkg/linux-cachyos-rc-autofdo/pkg/linux-cachyos-rc-
> >>>>>>> autofdo/usr/lib/modules/6.12.0-rc5-5-cachyos-rc-autofdo/kernel/
> >>>>>>> crypto/cryptd.ko
> >>>>>>> make[2]: *** Waiting for unfinished jobs....
> >>>>>>> ```
> >>>>>>>
> >>>>>>>
> >>>>>>> This can be fixed with removed "INSTALL_MOD_STRIP=1" to the passed
> >>>>>>> parameters of module_install.
> >>>>>>>
> >>>>>>> This explicitly only happens, if a profile is passed - otherwise the
> >>>>>>> packaging works without problems.
> >>>>>>>
> >>>>>>> Regards,
> >>>>>>>
> >>>>>>> Peter Jung
> >>>>>>>
> >>
> >
>
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 0/7] Add AutoFDO and Propeller support for Clang build
2024-11-02 17:51 [PATCH v7 0/7] Add AutoFDO and Propeller support for Clang build Rong Xu
` (6 preceding siblings ...)
2024-11-02 17:51 ` [PATCH v7 7/7] Add Propeller configuration for kernel build Rong Xu
@ 2024-11-06 16:08 ` Masahiro Yamada
2024-11-06 19:00 ` Rong Xu
7 siblings, 1 reply; 30+ messages in thread
From: Masahiro Yamada @ 2024-11-06 16:08 UTC (permalink / raw)
To: Rong Xu
Cc: Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Mike Rapoport (IBM), Nathan Chancellor,
Nick Desaulniers, Nicolas Schier, Paul E. McKenney,
Peter Zijlstra, Sami Tolvanen, Thomas Gleixner, Wei Yang,
workflows, Miguel Ojeda, Maksim Panchenko, David S. Miller,
Andreas Larsson, Yonghong Song, Yabin Cui, Krzysztof Pszeniczny,
Sriraman Tallam, Stephane Eranian, x86, linux-arch, sparclinux,
linux-doc, linux-kbuild, linux-kernel, llvm
On Sun, Nov 3, 2024 at 2:51 AM Rong Xu <xur@google.com> wrote:
>
> Hi,
>
> This patch series is to integrate AutoFDO and Propeller support into
> the Linux kernel. AutoFDO is a profile-guided optimization technique
> that leverages hardware sampling to enhance binary performance.
> Unlike Instrumentation-based FDO (iFDO), AutoFDO offers a user-friendly
> and straightforward application process. While iFDO generally yields
> superior profile quality and performance, our findings reveal that
> AutoFDO achieves remarkable effectiveness, bringing performance close
> to iFDO for benchmark applications.
>
> Propeller is a profile-guided, post-link optimizer that improves
> the performance of large-scale applications compiled with LLVM. It
> operates by relinking the binary based on an additional round of runtime
> profiles, enabling precise optimizations that are not possible at
> compile time. Similar to AutoFDO, Propeller too utilizes hardware
> sampling to collect profiles and apply post-link optimizations to improve
> the benchmark’s performance over and above AutoFDO.
>
> Our empirical data demonstrates significant performance improvements
> with AutoFDO and Propeller, up to 10% on microbenchmarks and up to 5%
> on large warehouse-scale benchmarks. This makes a strong case for their
> inclusion as supported features in the upstream kernel.
>
> Background
>
> A significant fraction of fleet processing cycles (excluding idle time)
> from data center workloads are attributable to the kernel. Ware-house
> scale workloads maximize performance by optimizing the production kernel
> using iFDO (a.k.a instrumented PGO, Profile Guided Optimization).
>
> iFDO can significantly enhance application performance but its use
> within the kernel has raised concerns. AutoFDO is a variant of FDO that
> uses the hardware’s Performance Monitoring Unit (PMU) to collect
> profiling data. While AutoFDO typically yields smaller performance
> gains than iFDO, it presents unique benefits for optimizing kernels.
>
> AutoFDO eliminates the need for instrumented kernels, allowing a single
> optimized kernel to serve both execution and profile collection. It also
> minimizes slowdown during profile collection, potentially yielding
> higher-fidelity profiling, especially for time-sensitive code, compared
> to iFDO. Additionally, AutoFDO profiles can be obtained from production
> environments via the hardware’s PMU whereas iFDO profiles require
> carefully curated load tests that are representative of real-world
> traffic.
>
> AutoFDO facilitates profile collection across diverse targets.
> Preliminary studies indicate significant variation in kernel hot spots
> within Google’s infrastructure, suggesting potential performance gains
> through target-specific kernel customization.
>
> Furthermore, other advanced compiler optimization techniques, including
> ThinLTO and Propeller can be stacked on top of AutoFDO, similar to iFDO.
> ThinLTO achieves better runtime performance through whole-program
> analysis and cross module optimizations. The main difference between
> traditional LTO and ThinLTO is that the latter is scalable in time and
> memory.
>
> This patch series adds AutoFDO and Propeller support to the kernel. The
> actual solution comes in six parts:
>
> [P 1] Add the build support for using AutoFDO in Clang
>
> Add the basic support for AutoFDO build and provide the
> instructions for using AutoFDO.
>
> [P 2] Fix objtool for bogus warnings when -ffunction-sections is enabled
>
> [P 3] Adjust symbol ordering in text output sections
>
> [P 4] Add markers for text_unlikely and text_hot sections
>
> [P 5] Enable –ffunction-sections for the AutoFDO build
>
> [P 6] Enable Machine Function Split (MFS) optimization for AutoFDO
>
> [P 7] Add Propeller configuration to the kernel build
>
> Patch 1 provides basic AutoFDO build support. Patches 2 to 6 further
> enhance the performance of AutoFDO builds and are functionally dependent
> on Patch 1. Patch 7 enables support for Propeller and is dependent on
> patch 2 to patch 4.
>
> Caveats
>
> AutoFDO is compatible with both GCC and Clang, but the patches in this
> series are exclusively applicable to LLVM 17 or newer for AutoFDO and
> LLVM 19 or newer for Propeller. For profile conversion, two different
> tools could be used, llvm_profgen or create_llvm_prof. llvm_profgen
> needs to be the LLVM 19 or newer, or just the LLVM trunk. Alternatively,
> create_llvm_prof v0.30.1 or newer can be used instead of llvm-profgen.
>
> Additionally, the build is only supported on x86 platforms equipped
> with PMU capabilities, such as LBR on Intel machines. More
> specifically:
> * Intel platforms: works on every platform that supports LBR;
> we have tested on Skylake.
> * AMD platforms: tested on AMD Zen3 with the BRS feature. The kernel
> needs to be configured with “CONFIG_PERF_EVENTS_AMD_BRS=y", To
> check, use
> $ cat /proc/cpuinfo | grep “ brs”
> For the AMD Zen4, AMD LBRV2 is supported, but we suspect a bug with
> AMD LBRv2 implementation in Genoa which blocks the usage.
>
> For ARM, we plan to send patches for SPE-based Propeller when
> AutoFDO for Arm is ready.
>
> Experiments and Results
>
> Experiments were conducted to compare the performance of AutoFDO-optimized
> kernel images (version 6.9.x) against default builds.. The evaluation
> encompassed both open source microbenchmarks and real-world production
> services from Google and Meta. The selected microbenchmarks included Neper,
> a network subsystem benchmark, and UnixBench which is a comprehensive suite
> for assessing various kernel operations.
>
> For Neper, AutoFDO optimization resulted in a 6.1% increase in throughput
> and a 10.6% reduction in latency. UnixBench saw a 2.2% improvement in its
> index score under low system load and a 2.6% improvement under high system
> load.
>
> For further details on the improvements observed in Google and Meta's
> production services, please refer to the LLVM discourse post:
> https://discourse.llvm.org/t/optimizing-the-linux-kernel-with-autofdo-including-thinlto-and-propeller/79108
>
> Thanks,
>
> Rong Xu and Han Shen
I applied this series to linux-kbuild.
As I mentioned before, I do not like #ifdef because
it hides (not fixes) issues only for default cases.
--
Best Regards
Masahiro Yamada
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 0/7] Add AutoFDO and Propeller support for Clang build
2024-11-06 16:08 ` [PATCH v7 0/7] Add AutoFDO and Propeller support for Clang build Masahiro Yamada
@ 2024-11-06 19:00 ` Rong Xu
2024-11-07 14:57 ` Masahiro Yamada
0 siblings, 1 reply; 30+ messages in thread
From: Rong Xu @ 2024-11-06 19:00 UTC (permalink / raw)
To: Masahiro Yamada
Cc: Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Mike Rapoport (IBM), Nathan Chancellor,
Nick Desaulniers, Nicolas Schier, Paul E. McKenney,
Peter Zijlstra, Sami Tolvanen, Thomas Gleixner, Wei Yang,
workflows, Miguel Ojeda, Maksim Panchenko, David S. Miller,
Andreas Larsson, Yonghong Song, Yabin Cui, Krzysztof Pszeniczny,
Sriraman Tallam, Stephane Eranian, x86, linux-arch, sparclinux,
linux-doc, linux-kbuild, linux-kernel, llvm
On Wed, Nov 6, 2024 at 8:09 AM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> On Sun, Nov 3, 2024 at 2:51 AM Rong Xu <xur@google.com> wrote:
> >
> > Hi,
> >
> > This patch series is to integrate AutoFDO and Propeller support into
> > the Linux kernel. AutoFDO is a profile-guided optimization technique
> > that leverages hardware sampling to enhance binary performance.
> > Unlike Instrumentation-based FDO (iFDO), AutoFDO offers a user-friendly
> > and straightforward application process. While iFDO generally yields
> > superior profile quality and performance, our findings reveal that
> > AutoFDO achieves remarkable effectiveness, bringing performance close
> > to iFDO for benchmark applications.
> >
> > Propeller is a profile-guided, post-link optimizer that improves
> > the performance of large-scale applications compiled with LLVM. It
> > operates by relinking the binary based on an additional round of runtime
> > profiles, enabling precise optimizations that are not possible at
> > compile time. Similar to AutoFDO, Propeller too utilizes hardware
> > sampling to collect profiles and apply post-link optimizations to improve
> > the benchmark’s performance over and above AutoFDO.
> >
> > Our empirical data demonstrates significant performance improvements
> > with AutoFDO and Propeller, up to 10% on microbenchmarks and up to 5%
> > on large warehouse-scale benchmarks. This makes a strong case for their
> > inclusion as supported features in the upstream kernel.
> >
> > Background
> >
> > A significant fraction of fleet processing cycles (excluding idle time)
> > from data center workloads are attributable to the kernel. Ware-house
> > scale workloads maximize performance by optimizing the production kernel
> > using iFDO (a.k.a instrumented PGO, Profile Guided Optimization).
> >
> > iFDO can significantly enhance application performance but its use
> > within the kernel has raised concerns. AutoFDO is a variant of FDO that
> > uses the hardware’s Performance Monitoring Unit (PMU) to collect
> > profiling data. While AutoFDO typically yields smaller performance
> > gains than iFDO, it presents unique benefits for optimizing kernels.
> >
> > AutoFDO eliminates the need for instrumented kernels, allowing a single
> > optimized kernel to serve both execution and profile collection. It also
> > minimizes slowdown during profile collection, potentially yielding
> > higher-fidelity profiling, especially for time-sensitive code, compared
> > to iFDO. Additionally, AutoFDO profiles can be obtained from production
> > environments via the hardware’s PMU whereas iFDO profiles require
> > carefully curated load tests that are representative of real-world
> > traffic.
> >
> > AutoFDO facilitates profile collection across diverse targets.
> > Preliminary studies indicate significant variation in kernel hot spots
> > within Google’s infrastructure, suggesting potential performance gains
> > through target-specific kernel customization.
> >
> > Furthermore, other advanced compiler optimization techniques, including
> > ThinLTO and Propeller can be stacked on top of AutoFDO, similar to iFDO.
> > ThinLTO achieves better runtime performance through whole-program
> > analysis and cross module optimizations. The main difference between
> > traditional LTO and ThinLTO is that the latter is scalable in time and
> > memory.
> >
> > This patch series adds AutoFDO and Propeller support to the kernel. The
> > actual solution comes in six parts:
> >
> > [P 1] Add the build support for using AutoFDO in Clang
> >
> > Add the basic support for AutoFDO build and provide the
> > instructions for using AutoFDO.
> >
> > [P 2] Fix objtool for bogus warnings when -ffunction-sections is enabled
> >
> > [P 3] Adjust symbol ordering in text output sections
> >
> > [P 4] Add markers for text_unlikely and text_hot sections
> >
> > [P 5] Enable –ffunction-sections for the AutoFDO build
> >
> > [P 6] Enable Machine Function Split (MFS) optimization for AutoFDO
> >
> > [P 7] Add Propeller configuration to the kernel build
> >
> > Patch 1 provides basic AutoFDO build support. Patches 2 to 6 further
> > enhance the performance of AutoFDO builds and are functionally dependent
> > on Patch 1. Patch 7 enables support for Propeller and is dependent on
> > patch 2 to patch 4.
> >
> > Caveats
> >
> > AutoFDO is compatible with both GCC and Clang, but the patches in this
> > series are exclusively applicable to LLVM 17 or newer for AutoFDO and
> > LLVM 19 or newer for Propeller. For profile conversion, two different
> > tools could be used, llvm_profgen or create_llvm_prof. llvm_profgen
> > needs to be the LLVM 19 or newer, or just the LLVM trunk. Alternatively,
> > create_llvm_prof v0.30.1 or newer can be used instead of llvm-profgen.
> >
> > Additionally, the build is only supported on x86 platforms equipped
> > with PMU capabilities, such as LBR on Intel machines. More
> > specifically:
> > * Intel platforms: works on every platform that supports LBR;
> > we have tested on Skylake.
> > * AMD platforms: tested on AMD Zen3 with the BRS feature. The kernel
> > needs to be configured with “CONFIG_PERF_EVENTS_AMD_BRS=y", To
> > check, use
> > $ cat /proc/cpuinfo | grep “ brs”
> > For the AMD Zen4, AMD LBRV2 is supported, but we suspect a bug with
> > AMD LBRv2 implementation in Genoa which blocks the usage.
> >
> > For ARM, we plan to send patches for SPE-based Propeller when
> > AutoFDO for Arm is ready.
> >
> > Experiments and Results
> >
> > Experiments were conducted to compare the performance of AutoFDO-optimized
> > kernel images (version 6.9.x) against default builds.. The evaluation
> > encompassed both open source microbenchmarks and real-world production
> > services from Google and Meta. The selected microbenchmarks included Neper,
> > a network subsystem benchmark, and UnixBench which is a comprehensive suite
> > for assessing various kernel operations.
> >
> > For Neper, AutoFDO optimization resulted in a 6.1% increase in throughput
> > and a 10.6% reduction in latency. UnixBench saw a 2.2% improvement in its
> > index score under low system load and a 2.6% improvement under high system
> > load.
> >
> > For further details on the improvements observed in Google and Meta's
> > production services, please refer to the LLVM discourse post:
> > https://discourse.llvm.org/t/optimizing-the-linux-kernel-with-autofdo-including-thinlto-and-propeller/79108
> >
> > Thanks,
> >
> > Rong Xu and Han Shen
>
>
> I applied this series to linux-kbuild.
>
Thanks for taking the patch!
> As I mentioned before, I do not like #ifdef because
> it hides (not fixes) issues only for default cases.
We followed the suggestion and removed most of the #if (or #ifdef) in
the linker script.
I just checked: there are two #ifdef remaining:
(1) in the propeller patch for .llvm_bb_addr_map
(2) in linker script patch for arch/sparc/kernel/vmlinux.lds.S.
I think it's likely safe to remove the checks for head_64.o in
non-SPARC64 builds and .llvm_bb_addr_map symbols in non-propeller builds.
SPARC64 builds should always produce head_64.o, and non-SPARC64
builds shouldn't.
Propeller builds always generate .llvm_bb_addr_map symbols, and the
linker will omit the section if it's empty in non-propeller builds.
Keeping the checks is harmless and might slightly reduce linker
workload for matching.
But If you'd prefer to remove them, I'm happy to provide a patch.
Best regards,
-Rong
>
> --
> Best Regards
> Masahiro Yamada
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 0/7] Add AutoFDO and Propeller support for Clang build
2024-11-06 19:00 ` Rong Xu
@ 2024-11-07 14:57 ` Masahiro Yamada
2024-11-07 18:44 ` Rong Xu
0 siblings, 1 reply; 30+ messages in thread
From: Masahiro Yamada @ 2024-11-07 14:57 UTC (permalink / raw)
To: Rong Xu
Cc: Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Mike Rapoport (IBM), Nathan Chancellor,
Nick Desaulniers, Nicolas Schier, Paul E. McKenney,
Peter Zijlstra, Sami Tolvanen, Thomas Gleixner, Wei Yang,
workflows, Miguel Ojeda, Maksim Panchenko, David S. Miller,
Andreas Larsson, Yonghong Song, Yabin Cui, Krzysztof Pszeniczny,
Sriraman Tallam, Stephane Eranian, x86, linux-arch, sparclinux,
linux-doc, linux-kbuild, linux-kernel, llvm
On Thu, Nov 7, 2024 at 4:00 AM Rong Xu <xur@google.com> wrote:
>
> On Wed, Nov 6, 2024 at 8:09 AM Masahiro Yamada <masahiroy@kernel.org> wrote:
> >
> > On Sun, Nov 3, 2024 at 2:51 AM Rong Xu <xur@google.com> wrote:
> > >
> > > Hi,
> > >
> > > This patch series is to integrate AutoFDO and Propeller support into
> > > the Linux kernel. AutoFDO is a profile-guided optimization technique
> > > that leverages hardware sampling to enhance binary performance.
> > > Unlike Instrumentation-based FDO (iFDO), AutoFDO offers a user-friendly
> > > and straightforward application process. While iFDO generally yields
> > > superior profile quality and performance, our findings reveal that
> > > AutoFDO achieves remarkable effectiveness, bringing performance close
> > > to iFDO for benchmark applications.
> > >
> > > Propeller is a profile-guided, post-link optimizer that improves
> > > the performance of large-scale applications compiled with LLVM. It
> > > operates by relinking the binary based on an additional round of runtime
> > > profiles, enabling precise optimizations that are not possible at
> > > compile time. Similar to AutoFDO, Propeller too utilizes hardware
> > > sampling to collect profiles and apply post-link optimizations to improve
> > > the benchmark’s performance over and above AutoFDO.
> > >
> > > Our empirical data demonstrates significant performance improvements
> > > with AutoFDO and Propeller, up to 10% on microbenchmarks and up to 5%
> > > on large warehouse-scale benchmarks. This makes a strong case for their
> > > inclusion as supported features in the upstream kernel.
> > >
> > > Background
> > >
> > > A significant fraction of fleet processing cycles (excluding idle time)
> > > from data center workloads are attributable to the kernel. Ware-house
> > > scale workloads maximize performance by optimizing the production kernel
> > > using iFDO (a.k.a instrumented PGO, Profile Guided Optimization).
> > >
> > > iFDO can significantly enhance application performance but its use
> > > within the kernel has raised concerns. AutoFDO is a variant of FDO that
> > > uses the hardware’s Performance Monitoring Unit (PMU) to collect
> > > profiling data. While AutoFDO typically yields smaller performance
> > > gains than iFDO, it presents unique benefits for optimizing kernels.
> > >
> > > AutoFDO eliminates the need for instrumented kernels, allowing a single
> > > optimized kernel to serve both execution and profile collection. It also
> > > minimizes slowdown during profile collection, potentially yielding
> > > higher-fidelity profiling, especially for time-sensitive code, compared
> > > to iFDO. Additionally, AutoFDO profiles can be obtained from production
> > > environments via the hardware’s PMU whereas iFDO profiles require
> > > carefully curated load tests that are representative of real-world
> > > traffic.
> > >
> > > AutoFDO facilitates profile collection across diverse targets.
> > > Preliminary studies indicate significant variation in kernel hot spots
> > > within Google’s infrastructure, suggesting potential performance gains
> > > through target-specific kernel customization.
> > >
> > > Furthermore, other advanced compiler optimization techniques, including
> > > ThinLTO and Propeller can be stacked on top of AutoFDO, similar to iFDO.
> > > ThinLTO achieves better runtime performance through whole-program
> > > analysis and cross module optimizations. The main difference between
> > > traditional LTO and ThinLTO is that the latter is scalable in time and
> > > memory.
> > >
> > > This patch series adds AutoFDO and Propeller support to the kernel. The
> > > actual solution comes in six parts:
> > >
> > > [P 1] Add the build support for using AutoFDO in Clang
> > >
> > > Add the basic support for AutoFDO build and provide the
> > > instructions for using AutoFDO.
> > >
> > > [P 2] Fix objtool for bogus warnings when -ffunction-sections is enabled
> > >
> > > [P 3] Adjust symbol ordering in text output sections
> > >
> > > [P 4] Add markers for text_unlikely and text_hot sections
> > >
> > > [P 5] Enable –ffunction-sections for the AutoFDO build
> > >
> > > [P 6] Enable Machine Function Split (MFS) optimization for AutoFDO
> > >
> > > [P 7] Add Propeller configuration to the kernel build
> > >
> > > Patch 1 provides basic AutoFDO build support. Patches 2 to 6 further
> > > enhance the performance of AutoFDO builds and are functionally dependent
> > > on Patch 1. Patch 7 enables support for Propeller and is dependent on
> > > patch 2 to patch 4.
> > >
> > > Caveats
> > >
> > > AutoFDO is compatible with both GCC and Clang, but the patches in this
> > > series are exclusively applicable to LLVM 17 or newer for AutoFDO and
> > > LLVM 19 or newer for Propeller. For profile conversion, two different
> > > tools could be used, llvm_profgen or create_llvm_prof. llvm_profgen
> > > needs to be the LLVM 19 or newer, or just the LLVM trunk. Alternatively,
> > > create_llvm_prof v0.30.1 or newer can be used instead of llvm-profgen.
> > >
> > > Additionally, the build is only supported on x86 platforms equipped
> > > with PMU capabilities, such as LBR on Intel machines. More
> > > specifically:
> > > * Intel platforms: works on every platform that supports LBR;
> > > we have tested on Skylake.
> > > * AMD platforms: tested on AMD Zen3 with the BRS feature. The kernel
> > > needs to be configured with “CONFIG_PERF_EVENTS_AMD_BRS=y", To
> > > check, use
> > > $ cat /proc/cpuinfo | grep “ brs”
> > > For the AMD Zen4, AMD LBRV2 is supported, but we suspect a bug with
> > > AMD LBRv2 implementation in Genoa which blocks the usage.
> > >
> > > For ARM, we plan to send patches for SPE-based Propeller when
> > > AutoFDO for Arm is ready.
> > >
> > > Experiments and Results
> > >
> > > Experiments were conducted to compare the performance of AutoFDO-optimized
> > > kernel images (version 6.9.x) against default builds.. The evaluation
> > > encompassed both open source microbenchmarks and real-world production
> > > services from Google and Meta. The selected microbenchmarks included Neper,
> > > a network subsystem benchmark, and UnixBench which is a comprehensive suite
> > > for assessing various kernel operations.
> > >
> > > For Neper, AutoFDO optimization resulted in a 6.1% increase in throughput
> > > and a 10.6% reduction in latency. UnixBench saw a 2.2% improvement in its
> > > index score under low system load and a 2.6% improvement under high system
> > > load.
> > >
> > > For further details on the improvements observed in Google and Meta's
> > > production services, please refer to the LLVM discourse post:
> > > https://discourse.llvm.org/t/optimizing-the-linux-kernel-with-autofdo-including-thinlto-and-propeller/79108
> > >
> > > Thanks,
> > >
> > > Rong Xu and Han Shen
> >
> >
> > I applied this series to linux-kbuild.
> >
>
> Thanks for taking the patch!
>
> > As I mentioned before, I do not like #ifdef because
> > it hides (not fixes) issues only for default cases.
>
> We followed the suggestion and removed most of the #if (or #ifdef) in
> the linker script.
> I just checked: there are two #ifdef remaining:
> (1) in the propeller patch for .llvm_bb_addr_map
> (2) in linker script patch for arch/sparc/kernel/vmlinux.lds.S.
>
> I think it's likely safe to remove the checks for head_64.o in
> non-SPARC64 builds and .llvm_bb_addr_map symbols in non-propeller builds.
>
> SPARC64 builds should always produce head_64.o, and non-SPARC64
> builds shouldn't.
>
> Propeller builds always generate .llvm_bb_addr_map symbols, and the
> linker will omit the section if it's empty in non-propeller builds.
>
> Keeping the checks is harmless and might slightly reduce linker
> workload for matching.
> But If you'd prefer to remove them, I'm happy to provide a patch.
I am talking about the #ifdef in include/asm-generic/vmlinux.lds.h
Yeah, it is me who (reluctantly) accepted cb87481ee89d.
Now, the #ifdef has become a little more complicated.
The default case is safe, but there are hidden issues.
Some issues are easy to fix, so I sent some patches.
https://lore.kernel.org/linux-kbuild/20241106161445.189399-1-masahiroy@kernel.org/T/#t
https://lore.kernel.org/linux-kbuild/20241106161445.189399-1-masahiroy@kernel.org/T/#m4e4fa70386696e903b68d3fe1d7277e9a63fbefe
https://lore.kernel.org/linux-kbuild/20241107111519.GA15424@willie-the-truck/T/#mccf6d49ddd11c90dcc583d7a68934bb3311da880
For example, see e41f501d3912.
When CONFIG_LD_DEAD_CODE_DATA_ELIMINATION=y or
CONFIG_LTO_CLANG=y or CONFIG_AUTOFDO_CLANG=y or
CONFIG_PROPELLER_CLANG=y, the .text.startup sections
will go to TEXT_MAIN instead of INIT_TEXT.
This is not a fatal issue, but we cannot reuse memory for .text.startup
sections.
Removing the #ifdef (i.e. reverting cb87481ee89d) is more difficult
because we need to take a closer look at potential impacts for all
architectures.
I understood you did not want to take a risk to break random architectures,
so I decided to postpone the #ifdef issue and accept your patch set.
--
Best Regards
Masahiro Yamada
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 0/7] Add AutoFDO and Propeller support for Clang build
2024-11-07 14:57 ` Masahiro Yamada
@ 2024-11-07 18:44 ` Rong Xu
0 siblings, 0 replies; 30+ messages in thread
From: Rong Xu @ 2024-11-07 18:44 UTC (permalink / raw)
To: Masahiro Yamada
Cc: Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Mike Rapoport (IBM), Nathan Chancellor,
Nick Desaulniers, Nicolas Schier, Paul E. McKenney,
Peter Zijlstra, Sami Tolvanen, Thomas Gleixner, Wei Yang,
workflows, Miguel Ojeda, Maksim Panchenko, David S. Miller,
Andreas Larsson, Yonghong Song, Yabin Cui, Krzysztof Pszeniczny,
Sriraman Tallam, Stephane Eranian, x86, linux-arch, sparclinux,
linux-doc, linux-kbuild, linux-kernel, llvm
Thanks for the explanation.
On Thu, Nov 7, 2024 at 6:58 AM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> On Thu, Nov 7, 2024 at 4:00 AM Rong Xu <xur@google.com> wrote:
> >
> > On Wed, Nov 6, 2024 at 8:09 AM Masahiro Yamada <masahiroy@kernel.org> wrote:
> > >
> > > On Sun, Nov 3, 2024 at 2:51 AM Rong Xu <xur@google.com> wrote:
> > > >
> > > > Hi,
> > > >
> > > > This patch series is to integrate AutoFDO and Propeller support into
> > > > the Linux kernel. AutoFDO is a profile-guided optimization technique
> > > > that leverages hardware sampling to enhance binary performance.
> > > > Unlike Instrumentation-based FDO (iFDO), AutoFDO offers a user-friendly
> > > > and straightforward application process. While iFDO generally yields
> > > > superior profile quality and performance, our findings reveal that
> > > > AutoFDO achieves remarkable effectiveness, bringing performance close
> > > > to iFDO for benchmark applications.
> > > >
> > > > Propeller is a profile-guided, post-link optimizer that improves
> > > > the performance of large-scale applications compiled with LLVM. It
> > > > operates by relinking the binary based on an additional round of runtime
> > > > profiles, enabling precise optimizations that are not possible at
> > > > compile time. Similar to AutoFDO, Propeller too utilizes hardware
> > > > sampling to collect profiles and apply post-link optimizations to improve
> > > > the benchmark’s performance over and above AutoFDO.
> > > >
> > > > Our empirical data demonstrates significant performance improvements
> > > > with AutoFDO and Propeller, up to 10% on microbenchmarks and up to 5%
> > > > on large warehouse-scale benchmarks. This makes a strong case for their
> > > > inclusion as supported features in the upstream kernel.
> > > >
> > > > Background
> > > >
> > > > A significant fraction of fleet processing cycles (excluding idle time)
> > > > from data center workloads are attributable to the kernel. Ware-house
> > > > scale workloads maximize performance by optimizing the production kernel
> > > > using iFDO (a.k.a instrumented PGO, Profile Guided Optimization).
> > > >
> > > > iFDO can significantly enhance application performance but its use
> > > > within the kernel has raised concerns. AutoFDO is a variant of FDO that
> > > > uses the hardware’s Performance Monitoring Unit (PMU) to collect
> > > > profiling data. While AutoFDO typically yields smaller performance
> > > > gains than iFDO, it presents unique benefits for optimizing kernels.
> > > >
> > > > AutoFDO eliminates the need for instrumented kernels, allowing a single
> > > > optimized kernel to serve both execution and profile collection. It also
> > > > minimizes slowdown during profile collection, potentially yielding
> > > > higher-fidelity profiling, especially for time-sensitive code, compared
> > > > to iFDO. Additionally, AutoFDO profiles can be obtained from production
> > > > environments via the hardware’s PMU whereas iFDO profiles require
> > > > carefully curated load tests that are representative of real-world
> > > > traffic.
> > > >
> > > > AutoFDO facilitates profile collection across diverse targets.
> > > > Preliminary studies indicate significant variation in kernel hot spots
> > > > within Google’s infrastructure, suggesting potential performance gains
> > > > through target-specific kernel customization.
> > > >
> > > > Furthermore, other advanced compiler optimization techniques, including
> > > > ThinLTO and Propeller can be stacked on top of AutoFDO, similar to iFDO.
> > > > ThinLTO achieves better runtime performance through whole-program
> > > > analysis and cross module optimizations. The main difference between
> > > > traditional LTO and ThinLTO is that the latter is scalable in time and
> > > > memory.
> > > >
> > > > This patch series adds AutoFDO and Propeller support to the kernel. The
> > > > actual solution comes in six parts:
> > > >
> > > > [P 1] Add the build support for using AutoFDO in Clang
> > > >
> > > > Add the basic support for AutoFDO build and provide the
> > > > instructions for using AutoFDO.
> > > >
> > > > [P 2] Fix objtool for bogus warnings when -ffunction-sections is enabled
> > > >
> > > > [P 3] Adjust symbol ordering in text output sections
> > > >
> > > > [P 4] Add markers for text_unlikely and text_hot sections
> > > >
> > > > [P 5] Enable –ffunction-sections for the AutoFDO build
> > > >
> > > > [P 6] Enable Machine Function Split (MFS) optimization for AutoFDO
> > > >
> > > > [P 7] Add Propeller configuration to the kernel build
> > > >
> > > > Patch 1 provides basic AutoFDO build support. Patches 2 to 6 further
> > > > enhance the performance of AutoFDO builds and are functionally dependent
> > > > on Patch 1. Patch 7 enables support for Propeller and is dependent on
> > > > patch 2 to patch 4.
> > > >
> > > > Caveats
> > > >
> > > > AutoFDO is compatible with both GCC and Clang, but the patches in this
> > > > series are exclusively applicable to LLVM 17 or newer for AutoFDO and
> > > > LLVM 19 or newer for Propeller. For profile conversion, two different
> > > > tools could be used, llvm_profgen or create_llvm_prof. llvm_profgen
> > > > needs to be the LLVM 19 or newer, or just the LLVM trunk. Alternatively,
> > > > create_llvm_prof v0.30.1 or newer can be used instead of llvm-profgen.
> > > >
> > > > Additionally, the build is only supported on x86 platforms equipped
> > > > with PMU capabilities, such as LBR on Intel machines. More
> > > > specifically:
> > > > * Intel platforms: works on every platform that supports LBR;
> > > > we have tested on Skylake.
> > > > * AMD platforms: tested on AMD Zen3 with the BRS feature. The kernel
> > > > needs to be configured with “CONFIG_PERF_EVENTS_AMD_BRS=y", To
> > > > check, use
> > > > $ cat /proc/cpuinfo | grep “ brs”
> > > > For the AMD Zen4, AMD LBRV2 is supported, but we suspect a bug with
> > > > AMD LBRv2 implementation in Genoa which blocks the usage.
> > > >
> > > > For ARM, we plan to send patches for SPE-based Propeller when
> > > > AutoFDO for Arm is ready.
> > > >
> > > > Experiments and Results
> > > >
> > > > Experiments were conducted to compare the performance of AutoFDO-optimized
> > > > kernel images (version 6.9.x) against default builds.. The evaluation
> > > > encompassed both open source microbenchmarks and real-world production
> > > > services from Google and Meta. The selected microbenchmarks included Neper,
> > > > a network subsystem benchmark, and UnixBench which is a comprehensive suite
> > > > for assessing various kernel operations.
> > > >
> > > > For Neper, AutoFDO optimization resulted in a 6.1% increase in throughput
> > > > and a 10.6% reduction in latency. UnixBench saw a 2.2% improvement in its
> > > > index score under low system load and a 2.6% improvement under high system
> > > > load.
> > > >
> > > > For further details on the improvements observed in Google and Meta's
> > > > production services, please refer to the LLVM discourse post:
> > > > https://discourse.llvm.org/t/optimizing-the-linux-kernel-with-autofdo-including-thinlto-and-propeller/79108
> > > >
> > > > Thanks,
> > > >
> > > > Rong Xu and Han Shen
> > >
> > >
> > > I applied this series to linux-kbuild.
> > >
> >
> > Thanks for taking the patch!
> >
> > > As I mentioned before, I do not like #ifdef because
> > > it hides (not fixes) issues only for default cases.
> >
> > We followed the suggestion and removed most of the #if (or #ifdef) in
> > the linker script.
> > I just checked: there are two #ifdef remaining:
> > (1) in the propeller patch for .llvm_bb_addr_map
> > (2) in linker script patch for arch/sparc/kernel/vmlinux.lds.S.
> >
> > I think it's likely safe to remove the checks for head_64.o in
> > non-SPARC64 builds and .llvm_bb_addr_map symbols in non-propeller builds.
> >
> > SPARC64 builds should always produce head_64.o, and non-SPARC64
> > builds shouldn't.
> >
> > Propeller builds always generate .llvm_bb_addr_map symbols, and the
> > linker will omit the section if it's empty in non-propeller builds.
> >
> > Keeping the checks is harmless and might slightly reduce linker
> > workload for matching.
> > But If you'd prefer to remove them, I'm happy to provide a patch.
>
>
> I am talking about the #ifdef in include/asm-generic/vmlinux.lds.h
>
>
> Yeah, it is me who (reluctantly) accepted cb87481ee89d.
>
> Now, the #ifdef has become a little more complicated.
> The default case is safe, but there are hidden issues.
>
> Some issues are easy to fix, so I sent some patches.
> https://lore.kernel.org/linux-kbuild/20241106161445.189399-1-masahiroy@kernel.org/T/#t
> https://lore.kernel.org/linux-kbuild/20241106161445.189399-1-masahiroy@kernel.org/T/#m4e4fa70386696e903b68d3fe1d7277e9a63fbefe
> https://lore.kernel.org/linux-kbuild/20241107111519.GA15424@willie-the-truck/T/#mccf6d49ddd11c90dcc583d7a68934bb3311da880
I did notice the issues for .data.* -- that is one of the reasons we
separated text from data in our patch.
>
> For example, see e41f501d3912.
>
> When CONFIG_LD_DEAD_CODE_DATA_ELIMINATION=y or
> CONFIG_LTO_CLANG=y or CONFIG_AUTOFDO_CLANG=y or
> CONFIG_PROPELLER_CLANG=y, the .text.startup sections
> will go to TEXT_MAIN instead of INIT_TEXT.
> This is not a fatal issue, but we cannot reuse memory for .text.startup
> sections.
>
> Removing the #ifdef (i.e. reverting cb87481ee89d) is more difficult
> because we need to take a closer look at potential impacts for all
> architectures.
I'm not sure if there is a naming convention for section names in the kernel.
For special sections, we should avoid using .text.* or .data.*,
instead, using "..', or use
other prefixes.
The compiler can generate sections names like .text.hot.*", ".text.unknown.*",
".text.unlikely.*", ".text.split.*", ".text.startup." or
".text.exit. It seems we've
addressed most of them except .text.startup and .text.exit.
For text.startup and .text.exit, have you considered renaming the
sections within
the linker script -- they are fixed strings and should be able to be renamed.
>
> I understood you did not want to take a risk to break random architectures,
> so I decided to postpone the #ifdef issue and accept your patch set.
Thanks for the understanding!
>
> --
> Best Regards
> Masahiro Yamada
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 7/7] Add Propeller configuration for kernel build
2024-11-02 17:51 ` [PATCH v7 7/7] Add Propeller configuration for kernel build Rong Xu
@ 2024-11-07 20:45 ` Nathan Chancellor
2024-11-07 21:54 ` Rong Xu
2024-12-12 21:20 ` Yonghong Song
1 sibling, 1 reply; 30+ messages in thread
From: Nathan Chancellor @ 2024-11-07 20:45 UTC (permalink / raw)
To: Rong Xu
Cc: Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM), Nick Desaulniers,
Nicolas Schier, Paul E. McKenney, Peter Zijlstra, Sami Tolvanen,
Thomas Gleixner, Wei Yang, workflows, Miguel Ojeda,
Maksim Panchenko, David S. Miller, Andreas Larsson, Yonghong Song,
Yabin Cui, Krzysztof Pszeniczny, Sriraman Tallam,
Stephane Eranian, x86, linux-arch, sparclinux, linux-doc,
linux-kbuild, linux-kernel, llvm
Hi Rong,
On Sat, Nov 02, 2024 at 10:51:14AM -0700, Rong Xu wrote:
> diff --git a/scripts/Makefile.propeller b/scripts/Makefile.propeller
> new file mode 100644
> index 0000000000000..344190717e471
> --- /dev/null
> +++ b/scripts/Makefile.propeller
> @@ -0,0 +1,28 @@
> +# SPDX-License-Identifier: GPL-2.0
> +
> +# Enable available and selected Clang Propeller features.
> +ifdef CLANG_PROPELLER_PROFILE_PREFIX
> + CFLAGS_PROPELLER_CLANG := -fbasic-block-sections=list=$(CLANG_PROPELLER_PROFILE_PREFIX)_cc_profile.txt -ffunction-sections
> + KBUILD_LDFLAGS += --symbol-ordering-file=$(CLANG_PROPELLER_PROFILE_PREFIX)_ld_profile.txt --no-warn-symbol-ordering
> +else
> + CFLAGS_PROPELLER_CLANG := -fbasic-block-sections=labels
> +endif
It appears that '-fbasic-block-sections=labels' has been deprecated in
the main branch of LLVM, as I see a warning repeated over and over when
building allmodconfig:
clang: warning: argument '-fbasic-block-sections=labels' is deprecated, use '-fbasic-block-address-map' instead [-Wdeprecated]
https://github.com/llvm/llvm-project/commit/7b7747dc1d3da1a829503ea9505b4cecce4f5bda
Sorry that I missed this during testing, as I was only using clang-19 at
the time.
I think you can send a fixup on top of Masahiro's branch:
https://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-kbuild.git/log/?h=kbuild
> +# Propeller requires debug information to embed module names in the profiles.
> +# If CONFIG_DEBUG_INFO is not enabled, set -gmlt option. Skip this for AutoFDO,
> +# as the option should already be set.
> +ifndef CONFIG_DEBUG_INFO
> + ifndef CONFIG_AUTOFDO_CLANG
> + CFLAGS_PROPELLER_CLANG += -gmlt
> + endif
> +endif
> +
> +ifdef CONFIG_LTO_CLANG_THIN
> + ifdef CLANG_PROPELLER_PROFILE_PREFIX
> + KBUILD_LDFLAGS += --lto-basic-block-sections=$(CLANG_PROPELLER_PROFILE_PREFIX)_cc_profile.txt
> + else
> + KBUILD_LDFLAGS += --lto-basic-block-sections=labels
I think this might have a similar problem but I have not tested.
> + endif
> +endif
> +
> +export CFLAGS_PROPELLER_CLANG
Cheers,
Nathan
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 7/7] Add Propeller configuration for kernel build
2024-11-07 20:45 ` Nathan Chancellor
@ 2024-11-07 21:54 ` Rong Xu
0 siblings, 0 replies; 30+ messages in thread
From: Rong Xu @ 2024-11-07 21:54 UTC (permalink / raw)
To: Nathan Chancellor
Cc: Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM), Nick Desaulniers,
Nicolas Schier, Paul E. McKenney, Peter Zijlstra, Sami Tolvanen,
Thomas Gleixner, Wei Yang, workflows, Miguel Ojeda,
Maksim Panchenko, David S. Miller, Andreas Larsson, Yonghong Song,
Yabin Cui, Krzysztof Pszeniczny, Sriraman Tallam,
Stephane Eranian, x86, linux-arch, sparclinux, linux-doc,
linux-kbuild, linux-kernel, llvm
Thanks Nathan for finding this out!
We changed the propeller option with this patch:
https://github.com/llvm/llvm-project/pull/110039
Currently, this patch is only in the ToT clang (v20) and not yet
released in v19.
I'll add a compiler version check to the patch: if the clang version >= 20,
use the new option.
If this patch is later released in v19.x clang, I'll have to update the check
accordingly.
If I don't hear objections, I'll send a fixup on top of Masahiro's branch.
Thanks,
-Rong
On Thu, Nov 7, 2024 at 12:45 PM Nathan Chancellor <nathan@kernel.org> wrote:
>
> Hi Rong,
>
> On Sat, Nov 02, 2024 at 10:51:14AM -0700, Rong Xu wrote:
> > diff --git a/scripts/Makefile.propeller b/scripts/Makefile.propeller
> > new file mode 100644
> > index 0000000000000..344190717e471
> > --- /dev/null
> > +++ b/scripts/Makefile.propeller
> > @@ -0,0 +1,28 @@
> > +# SPDX-License-Identifier: GPL-2.0
> > +
> > +# Enable available and selected Clang Propeller features.
> > +ifdef CLANG_PROPELLER_PROFILE_PREFIX
> > + CFLAGS_PROPELLER_CLANG := -fbasic-block-sections=list=$(CLANG_PROPELLER_PROFILE_PREFIX)_cc_profile.txt -ffunction-sections
> > + KBUILD_LDFLAGS += --symbol-ordering-file=$(CLANG_PROPELLER_PROFILE_PREFIX)_ld_profile.txt --no-warn-symbol-ordering
> > +else
> > + CFLAGS_PROPELLER_CLANG := -fbasic-block-sections=labels
> > +endif
>
> It appears that '-fbasic-block-sections=labels' has been deprecated in
> the main branch of LLVM, as I see a warning repeated over and over when
> building allmodconfig:
>
> clang: warning: argument '-fbasic-block-sections=labels' is deprecated, use '-fbasic-block-address-map' instead [-Wdeprecated]
>
> https://github.com/llvm/llvm-project/commit/7b7747dc1d3da1a829503ea9505b4cecce4f5bda
>
> Sorry that I missed this during testing, as I was only using clang-19 at
> the time.
>
> I think you can send a fixup on top of Masahiro's branch:
>
> https://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-kbuild.git/log/?h=kbuild
>
> > +# Propeller requires debug information to embed module names in the profiles.
> > +# If CONFIG_DEBUG_INFO is not enabled, set -gmlt option. Skip this for AutoFDO,
> > +# as the option should already be set.
> > +ifndef CONFIG_DEBUG_INFO
> > + ifndef CONFIG_AUTOFDO_CLANG
> > + CFLAGS_PROPELLER_CLANG += -gmlt
> > + endif
> > +endif
> > +
> > +ifdef CONFIG_LTO_CLANG_THIN
> > + ifdef CLANG_PROPELLER_PROFILE_PREFIX
> > + KBUILD_LDFLAGS += --lto-basic-block-sections=$(CLANG_PROPELLER_PROFILE_PREFIX)_cc_profile.txt
> > + else
> > + KBUILD_LDFLAGS += --lto-basic-block-sections=labels
>
> I think this might have a similar problem but I have not tested.
>
> > + endif
> > +endif
> > +
> > +export CFLAGS_PROPELLER_CLANG
>
> Cheers,
> Nathan
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 3/7] Adjust symbol ordering in text output section [openrisc boot failure]
2024-11-02 17:51 ` [PATCH v7 3/7] Adjust symbol ordering in text output section Rong Xu
@ 2024-12-01 14:31 ` Guenter Roeck
2024-12-02 6:39 ` Masahiro Yamada
0 siblings, 1 reply; 30+ messages in thread
From: Guenter Roeck @ 2024-12-01 14:31 UTC (permalink / raw)
To: Rong Xu
Cc: Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Sami Tolvanen, Thomas Gleixner,
Wei Yang, workflows, Miguel Ojeda, Maksim Panchenko,
David S. Miller, Andreas Larsson, Yonghong Song, Yabin Cui,
Krzysztof Pszeniczny, Sriraman Tallam, Stephane Eranian, x86,
linux-arch, sparclinux, linux-doc, linux-kbuild, linux-kernel,
llvm, Jonas Bonn, Stefan Kristiansson, Stafford Horne,
linux-openrisc
Hi,
On Sat, Nov 02, 2024 at 10:51:10AM -0700, Rong Xu wrote:
> When the -ffunction-sections compiler option is enabled, each function
> is placed in a separate section named .text.function_name rather than
> putting all functions in a single .text section.
>
...
>
> Co-developed-by: Han Shen <shenhan@google.com>
> Signed-off-by: Han Shen <shenhan@google.com>
> Signed-off-by: Rong Xu <xur@google.com>
> Suggested-by: Sriraman Tallam <tmsriram@google.com>
> Suggested-by: Krzysztof Pszeniczny <kpszeniczny@google.com>
> Tested-by: Yonghong Song <yonghong.song@linux.dev>
> Tested-by: Yabin Cui <yabinc@google.com>
> Tested-by: Nathan Chancellor <nathan@kernel.org>
> Reviewed-by: Kees Cook <kees@kernel.org>
With this patch in the tree, the openrisck qemu emulation using
or1ksim_defconfig fails to boot. There is no log output, even with
earlycon enabled.
Bisect log attached.
Guenter
---
# bad: [bcc8eda6d34934d80b96adb8dc4ff5dfc632a53a] Merge tag 'turbostat-2024.11.30' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux
# good: [2ba9f676d0a2e408aef14d679984c26373bf37b7] Merge tag 'drm-next-2024-11-29' of https://gitlab.freedesktop.org/drm/kernel
git bisect start 'HEAD' '2ba9f676d0a2'
# good: [831c1926ee728c3e747255f7c0f434762e8e863d] Merge tag 'uml-for-linus-6.13-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/uml/linux
git bisect good 831c1926ee728c3e747255f7c0f434762e8e863d
# bad: [6a34dfa15d6edf7e78b8118d862d2db0889cf669] Merge tag 'kbuild-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-kbuild
git bisect bad 6a34dfa15d6edf7e78b8118d862d2db0889cf669
# bad: [e397a603e49cc7c7c113fad9f55a09637f290c34] kbuild: switch from lz4c to lz4 for compression
git bisect bad e397a603e49cc7c7c113fad9f55a09637f290c34
# good: [d6a91e28d11902e6cd5715633ed6f9b6df75de32] kconfig: qconf: remove unnecessary mode check in ConfigItem::updateMenu()
git bisect good d6a91e28d11902e6cd5715633ed6f9b6df75de32
# bad: [0afd73c5f5c606b0f8f8ff036e4f5d6c4b788d02] kbuild: replace two $(abs_objtree) with $(CURDIR) in top Makefile
git bisect bad 0afd73c5f5c606b0f8f8ff036e4f5d6c4b788d02
# bad: [db0b2991ae1aac5ca985ec6fd8ff9bd9b2126c9b] vmlinux.lds.h: Add markers for text_unlikely and text_hot sections
git bisect bad db0b2991ae1aac5ca985ec6fd8ff9bd9b2126c9b
# good: [315ad8780a129e82e2c5c65ee6e970d91a577acb] kbuild: Add AutoFDO support for Clang build
git bisect good 315ad8780a129e82e2c5c65ee6e970d91a577acb
# good: [52892ed6b03a14b961c1df783ed05763758abc73] MIPS: Place __kernel_entry at the beginning of text section
git bisect good 52892ed6b03a14b961c1df783ed05763758abc73
# bad: [0043ecea2399ffc8bfd99ed9dbbe766e7c79293c] vmlinux.lds.h: Adjust symbol ordering in text output section
git bisect bad 0043ecea2399ffc8bfd99ed9dbbe766e7c79293c
# first bad commit: [0043ecea2399ffc8bfd99ed9dbbe766e7c79293c] vmlinux.lds.h: Adjust symbol ordering in text output section
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 3/7] Adjust symbol ordering in text output section [openrisc boot failure]
2024-12-01 14:31 ` [PATCH v7 3/7] Adjust symbol ordering in text output section [openrisc boot failure] Guenter Roeck
@ 2024-12-02 6:39 ` Masahiro Yamada
0 siblings, 0 replies; 30+ messages in thread
From: Masahiro Yamada @ 2024-12-02 6:39 UTC (permalink / raw)
To: Guenter Roeck
Cc: Rong Xu, Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Mike Rapoport (IBM), Nathan Chancellor,
Nick Desaulniers, Nicolas Schier, Paul E. McKenney,
Peter Zijlstra, Sami Tolvanen, Thomas Gleixner, Wei Yang,
workflows, Miguel Ojeda, Maksim Panchenko, David S. Miller,
Andreas Larsson, Yonghong Song, Yabin Cui, Krzysztof Pszeniczny,
Sriraman Tallam, Stephane Eranian, x86, linux-arch, sparclinux,
linux-doc, linux-kbuild, linux-kernel, llvm, Jonas Bonn,
Stefan Kristiansson, Stafford Horne, linux-openrisc
On Sun, Dec 1, 2024 at 11:31 PM Guenter Roeck <linux@roeck-us.net> wrote:
>
> Hi,
>
> On Sat, Nov 02, 2024 at 10:51:10AM -0700, Rong Xu wrote:
> > When the -ffunction-sections compiler option is enabled, each function
> > is placed in a separate section named .text.function_name rather than
> > putting all functions in a single .text section.
> >
> ...
> >
> > Co-developed-by: Han Shen <shenhan@google.com>
> > Signed-off-by: Han Shen <shenhan@google.com>
> > Signed-off-by: Rong Xu <xur@google.com>
> > Suggested-by: Sriraman Tallam <tmsriram@google.com>
> > Suggested-by: Krzysztof Pszeniczny <kpszeniczny@google.com>
> > Tested-by: Yonghong Song <yonghong.song@linux.dev>
> > Tested-by: Yabin Cui <yabinc@google.com>
> > Tested-by: Nathan Chancellor <nathan@kernel.org>
> > Reviewed-by: Kees Cook <kees@kernel.org>
>
> With this patch in the tree, the openrisck qemu emulation using
> or1ksim_defconfig fails to boot. There is no log output, even with
> earlycon enabled.
Thanks for the report.
I posted a fix.
https://lore.kernel.org/all/20241202062909.2194341-1-masahiroy@kernel.org/T/#u
--
Best Regards
Masahiro Yamada
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 7/7] Add Propeller configuration for kernel build
2024-11-02 17:51 ` [PATCH v7 7/7] Add Propeller configuration for kernel build Rong Xu
2024-11-07 20:45 ` Nathan Chancellor
@ 2024-12-12 21:20 ` Yonghong Song
2024-12-12 21:34 ` Nathan Chancellor
1 sibling, 1 reply; 30+ messages in thread
From: Yonghong Song @ 2024-12-12 21:20 UTC (permalink / raw)
To: Rong Xu, Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM),
Nathan Chancellor, Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Sami Tolvanen, Thomas Gleixner,
Wei Yang, workflows, Miguel Ojeda, Maksim Panchenko,
David S. Miller, Andreas Larsson, Yabin Cui, Krzysztof Pszeniczny,
Sriraman Tallam, Stephane Eranian
Cc: x86, linux-arch, sparclinux, linux-doc, linux-kbuild,
linux-kernel, llvm
On 11/2/24 10:51 AM, Rong Xu wrote:
> Add the build support for using Clang's Propeller optimizer. Like
> AutoFDO, Propeller uses hardware sampling to gather information
> about the frequency of execution of different code paths within a
> binary. This information is then used to guide the compiler's
> optimization decisions, resulting in a more efficient binary.
>
> The support requires a Clang compiler LLVM 19 or later, and the
> create_llvm_prof tool
> (https://github.com/google/autofdo/releases/tag/v0.30.1). This
> commit is limited to x86 platforms that support PMU features
> like LBR on Intel machines and AMD Zen3 BRS.
>
> Here is an example workflow for building an AutoFDO+Propeller
> optimized kernel:
>
> 1) Build the kernel on the host machine, with AutoFDO and Propeller
> build config
> CONFIG_AUTOFDO_CLANG=y
> CONFIG_PROPELLER_CLANG=y
> then
> $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<autofdo_profile>
>
> “<autofdo_profile>” is the profile collected when doing a non-Propeller
> AutoFDO build. This step builds a kernel that has the same optimization
> level as AutoFDO, plus a metadata section that records basic block
> information. This kernel image runs as fast as an AutoFDO optimized
> kernel.
>
> 2) Install the kernel on test/production machines.
>
> 3) Run the load tests. The '-c' option in perf specifies the sample
> event period. We suggest using a suitable prime number,
> like 500009, for this purpose.
> For Intel platforms:
> $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c <count> \
> -o <perf_file> -- <loadtest>
> For AMD platforms:
> The supported system are: Zen3 with BRS, or Zen4 with amd_lbr_v2
> # To see if Zen3 support LBR:
> $ cat proc/cpuinfo | grep " brs"
> # To see if Zen4 support LBR:
> $ cat proc/cpuinfo | grep amd_lbr_v2
> # If the result is yes, then collect the profile using:
> $ perf record --pfm-events RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k -a \
> -N -b -c <count> -o <perf_file> -- <loadtest>
>
> 4) (Optional) Download the raw perf file to the host machine.
>
> 5) Generate Propeller profile:
> $ create_llvm_prof --binary=<vmlinux> --profile=<perf_file> \
> --format=propeller --propeller_output_module_name \
> --out=<propeller_profile_prefix>_cc_profile.txt \
> --propeller_symorder=<propeller_profile_prefix>_ld_profile.txt
>
> “create_llvm_prof” is the profile conversion tool, and a prebuilt
> binary for linux can be found on
> https://github.com/google/autofdo/releases/tag/v0.30.1 (can also build
> from source).
>
> "<propeller_profile_prefix>" can be something like
> "/home/user/dir/any_string".
>
> This command generates a pair of Propeller profiles:
> "<propeller_profile_prefix>_cc_profile.txt" and
> "<propeller_profile_prefix>_ld_profile.txt".
>
> 6) Rebuild the kernel using the AutoFDO and Propeller profile files.
> CONFIG_AUTOFDO_CLANG=y
> CONFIG_PROPELLER_CLANG=y
> and
> $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<autofdo_profile> \
> CLANG_PROPELLER_PROFILE_PREFIX=<propeller_profile_prefix>
>
> Co-developed-by: Han Shen <shenhan@google.com>
> Signed-off-by: Han Shen <shenhan@google.com>
> Signed-off-by: Rong Xu <xur@google.com>
> Suggested-by: Sriraman Tallam <tmsriram@google.com>
> Suggested-by: Krzysztof Pszeniczny <kpszeniczny@google.com>
> Suggested-by: Nick Desaulniers <ndesaulniers@google.com>
> Suggested-by: Stephane Eranian <eranian@google.com>
> Tested-by: Yonghong Song <yonghong.song@linux.dev>
> Tested-by: Nathan Chancellor <nathan@kernel.org>
> Reviewed-by: Kees Cook <kees@kernel.org>
> ---
> Documentation/dev-tools/index.rst | 1 +
> Documentation/dev-tools/propeller.rst | 162 ++++++++++++++++++++++++++
> MAINTAINERS | 7 ++
> Makefile | 1 +
> arch/Kconfig | 19 +++
> arch/x86/Kconfig | 1 +
> arch/x86/kernel/vmlinux.lds.S | 4 +
> include/asm-generic/vmlinux.lds.h | 6 +-
> scripts/Makefile.lib | 10 ++
> scripts/Makefile.propeller | 28 +++++
> tools/objtool/check.c | 1 +
> 11 files changed, 237 insertions(+), 3 deletions(-)
> create mode 100644 Documentation/dev-tools/propeller.rst
> create mode 100644 scripts/Makefile.propeller
>
> diff --git a/Documentation/dev-tools/index.rst b/Documentation/dev-tools/index.rst
> index 6945644f7008a..3c0ac08b27091 100644
> --- a/Documentation/dev-tools/index.rst
> +++ b/Documentation/dev-tools/index.rst
> @@ -35,6 +35,7 @@ Documentation/dev-tools/testing-overview.rst
> checkuapi
> gpio-sloppy-logic-analyzer
> autofdo
> + propeller
>
>
> .. only:: subproject and html
> diff --git a/Documentation/dev-tools/propeller.rst b/Documentation/dev-tools/propeller.rst
> new file mode 100644
> index 0000000000000..92195958e3dbc
> --- /dev/null
> +++ b/Documentation/dev-tools/propeller.rst
> @@ -0,0 +1,162 @@
> +.. SPDX-License-Identifier: GPL-2.0
> +
> +=====================================
> +Using Propeller with the Linux kernel
> +=====================================
> +
> +This enables Propeller build support for the kernel when using Clang
> +compiler. Propeller is a profile-guided optimization (PGO) method used
> +to optimize binary executables. Like AutoFDO, it utilizes hardware
> +sampling to gather information about the frequency of execution of
> +different code paths within a binary. Unlike AutoFDO, this information
> +is then used right before linking phase to optimize (among others)
> +block layout within and across functions.
> +
> +A few important notes about adopting Propeller optimization:
> +
> +#. Although it can be used as a standalone optimization step, it is
> + strongly recommended to apply Propeller on top of AutoFDO,
> + AutoFDO+ThinLTO or Instrument FDO. The rest of this document
> + assumes this paradigm.
> +
> +#. Propeller uses another round of profiling on top of
> + AutoFDO/AutoFDO+ThinLTO/iFDO. The whole build process involves
> + "build-afdo - train-afdo - build-propeller - train-propeller -
> + build-optimized".
> +
> +#. Propeller requires LLVM 19 release or later for Clang/Clang++
> + and the linker(ld.lld).
> +
> +#. In addition to LLVM toolchain, Propeller requires a profiling
> + conversion tool: https://github.com/google/autofdo with a release
> + after v0.30.1: https://github.com/google/autofdo/releases/tag/v0.30.1.
> +
> +The Propeller optimization process involves the following steps:
> +
> +#. Initial building: Build the AutoFDO or AutoFDO+ThinLTO binary as
> + you would normally do, but with a set of compile-time / link-time
> + flags, so that a special metadata section is created within the
> + kernel binary. The special section is only intend to be used by the
> + profiling tool, it is not part of the runtime image, nor does it
> + change kernel run time text sections.
> +
> +#. Profiling: The above kernel is then run with a representative
> + workload to gather execution frequency data. This data is collected
> + using hardware sampling, via perf. Propeller is most effective on
> + platforms supporting advanced PMU features like LBR on Intel
> + machines. This step is the same as profiling the kernel for AutoFDO
> + (the exact perf parameters can be different).
> +
> +#. Propeller profile generation: Perf output file is converted to a
> + pair of Propeller profiles via an offline tool.
> +
> +#. Optimized build: Build the AutoFDO or AutoFDO+ThinLTO optimized
> + binary as you would normally do, but with a compile-time /
> + link-time flag to pick up the Propeller compile time and link time
> + profiles. This build step uses 3 profiles - the AutoFDO profile,
> + the Propeller compile-time profile and the Propeller link-time
> + profile.
> +
> +#. Deployment: The optimized kernel binary is deployed and used
> + in production environments, providing improved performance
> + and reduced latency.
> +
> +Preparation
> +===========
> +
> +Configure the kernel with::
> +
> + CONFIG_AUTOFDO_CLANG=y
> + CONFIG_PROPELLER_CLANG=y
> +
> +Customization
> +=============
> +
> +The default CONFIG_PROPELLER_CLANG setting covers kernel space objects
> +for Propeller builds. One can, however, enable or disable Propeller build
> +for individual files and directories by adding a line similar to the
> +following to the respective kernel Makefile:
> +
> +- For enabling a single file (e.g. foo.o)::
> +
> + PROPELLER_PROFILE_foo.o := y
> +
> +- For enabling all files in one directory::
> +
> + PROPELLER_PROFILE := y
> +
> +- For disabling one file::
> +
> + PROPELLER_PROFILE_foo.o := n
> +
> +- For disabling all files in one directory::
> +
> + PROPELLER__PROFILE := n
> +
> +
> +Workflow
> +========
> +
> +Here is an example workflow for building an AutoFDO+Propeller kernel:
> +
> +1) Assuming an AutoFDO profile is already collected following
> + instructions in the AutoFDO document, build the kernel on the host
> + machine, with AutoFDO and Propeller build configs ::
> +
> + CONFIG_AUTOFDO_CLANG=y
> + CONFIG_PROPELLER_CLANG=y
> +
> + and ::
> +
> + $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<autofdo-profile-name>
> +
> +2) Install the kernel on the test machine.
> +
> +3) Run the load tests. The '-c' option in perf specifies the sample
> + event period. We suggest using a suitable prime number, like 500009,
> + for this purpose.
> +
> + - For Intel platforms::
> +
> + $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c <count> -o <perf_file> -- <loadtest>
> +
> + - For AMD platforms::
> +
> + $ perf record --pfm-event RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k -a -N -b -c <count> -o <perf_file> -- <loadtest>
> +
> + Note you can repeat the above steps to collect multiple <perf_file>s.
> +
> +4) (Optional) Download the raw perf file(s) to the host machine.
> +
> +5) Use the create_llvm_prof tool (https://github.com/google/autofdo) to
> + generate Propeller profile. ::
> +
> + $ create_llvm_prof --binary=<vmlinux> --profile=<perf_file>
> + --format=propeller --propeller_output_module_name
> + --out=<propeller_profile_prefix>_cc_profile.txt
> + --propeller_symorder=<propeller_profile_prefix>_ld_profile.txt
Prevously I am using perf-6.8.5-0.hs1.hsx.el9.x86_64 and it works fine.
Now in my system, the perf is upgraded to 6.12.gadc218676eef
[root@twshared7248.15.atn5 ~]# perf --version
perf version 6.12.gadc218676eef
and create_llvm_prof does not work any more.
The command to collect sampling data:
# perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c 500009 -- stress --cpu 36 --io 36 --vm 36 --vm-bytes 128M --timeout 300s
stress: info: [536354] dispatching hogs: 36 cpu, 36 io, 36 vm, 0 hdd
stress: info: [536354] successful run completed in 300s
[ perf record: Woken up 2210 times to write data ]
[ perf record: Captured and wrote 562.529 MB perf.data (701971 samples) ]
# uname -r
6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39
The kernel is a 6.11 lto kernel.
I then run the following command:
$ cat ../run.sh
# perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c 500009 \
# -- stress --cpu 36 --io 36 --vm 36 --vm-bytes 128M --timeout 300s
# good: perf-6.8.5-0.hs1.hsx.el9.x86_64
# <propeller_profile_prefix>: /tmp/propeller
./create_llvm_prof --binary=vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39 \
--profile=perf.data \
--format=propeller --propeller_output_module_name \
--out=/tmp/propeller_cc_profile.txt \
--propeller_symorder=/tmp/propeller_ld_profile.txt
$ ./run.sh
WARNING: Logging before InitGoogleLogging() is written to STDERR
I20241212 13:12:18.401772 463318 llvm_propeller_binary_content.cc:376] 'vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39' is PIE: 0
I20241212 13:12:18.403692 463318 llvm_propeller_binary_content.cc:380] 'vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39' is relocatable: 0
I20241212 13:12:18.404873 463318 llvm_propeller_binary_content.cc:388] Build Id found in 'vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39': eaacd5a14abc48cf832b3ad0fa6c64635ab569a8
I20241212 13:12:18.521499 463318 llvm_propeller_binary_content.cc:376] 'vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39' is PIE: 0
I20241212 13:12:18.521530 463318 llvm_propeller_binary_content.cc:380] 'vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39' is relocatable: 0
I20241212 13:12:18.521553 463318 llvm_propeller_binary_content.cc:388] Build Id found in 'vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39': eaacd5a14abc48cf832b3ad0fa6c64635ab569a8
I20241212 13:12:18.521611 463318 llvm_propeller_perf_lbr_aggregator.cc:51] Parsing [1/1] perf.data ...
[ERROR:/home/runner/work/autofdo/autofdo/third_party/perf_data_converter/src/quipper/perf_reader.cc:1386] Event size 132 after uint64_t alignment of the filename length is greater than event size 128 reported by perf for the buildid event of type 0
W20241212 13:12:18.521708 463318 llvm_propeller_perf_lbr_aggregator.cc:55] Skipped profile [1/1] perf.data: FAILED_PRECONDITION: Failed to read perf data file: [1/1] perf.data
W20241212 13:12:18.521718 463318 llvm_propeller_perf_lbr_aggregator.cc:67] Too few branch records in perf data.
E20241212 13:12:18.554437 463318 create_llvm_prof.cc:238] FAILED_PRECONDITION: No perf file is parsed, cannot proceed.
Could you help take a look why perf 12 does not work with create_llvm_prof?
The create_llvm_prof is downloaded from https://github.com/google/autofdo/releases/tag/v0.30.1.
> +
> + "<propeller_profile_prefix>" can be something like "/home/user/dir/any_string".
> +
> + This command generates a pair of Propeller profiles:
> + "<propeller_profile_prefix>_cc_profile.txt" and
> + "<propeller_profile_prefix>_ld_profile.txt".
> +
> + If there are more than 1 perf_file collected in the previous step,
> + you can create a temp list file "<perf_file_list>" with each line
> + containing one perf file name and run::
> +
> + $ create_llvm_prof --binary=<vmlinux> --profile=@<perf_file_list>
> + --format=propeller --propeller_output_module_name
> + --out=<propeller_profile_prefix>_cc_profile.txt
> + --propeller_symorder=<propeller_profile_prefix>_ld_profile.txt
> +
> +6) Rebuild the kernel using the AutoFDO and Propeller
> + profiles. ::
> +
> + CONFIG_AUTOFDO_CLANG=y
> + CONFIG_PROPELLER_CLANG=y
> +
> + and ::
> +
> + $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<profile_file> CLANG_PROPELLER_PROFILE_PREFIX=<propeller_profile_prefix>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index d6ea49433747a..42e3af0791e15 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -18449,6 +18449,13 @@ S: Maintained
> F: include/linux/psi*
> F: kernel/sched/psi.c
>
[...]
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 7/7] Add Propeller configuration for kernel build
2024-12-12 21:20 ` Yonghong Song
@ 2024-12-12 21:34 ` Nathan Chancellor
2024-12-12 22:03 ` Rong Xu
0 siblings, 1 reply; 30+ messages in thread
From: Nathan Chancellor @ 2024-12-12 21:34 UTC (permalink / raw)
To: Yonghong Song
Cc: Rong Xu, Alice Ryhl, Andrew Morton, Arnd Bergmann, Bill Wendling,
Borislav Petkov, Breno Leitao, Brian Gerst, Dave Hansen, David Li,
Han Shen, Heiko Carstens, H. Peter Anvin, Ingo Molnar, Jann Horn,
Jonathan Corbet, Josh Poimboeuf, Juergen Gross, Justin Stitt,
Kees Cook, Masahiro Yamada, Mike Rapoport (IBM), Nick Desaulniers,
Nicolas Schier, Paul E. McKenney, Peter Zijlstra, Sami Tolvanen,
Thomas Gleixner, Wei Yang, workflows, Miguel Ojeda,
Maksim Panchenko, David S. Miller, Andreas Larsson, Yabin Cui,
Krzysztof Pszeniczny, Sriraman Tallam, Stephane Eranian, x86,
linux-arch, sparclinux, linux-doc, linux-kbuild, linux-kernel,
llvm, Peter Jung
On Thu, Dec 12, 2024 at 01:20:46PM -0800, Yonghong Song wrote:
...
> > +5) Use the create_llvm_prof tool (https://github.com/google/autofdo) to
> > + generate Propeller profile. ::
> > +
> > + $ create_llvm_prof --binary=<vmlinux> --profile=<perf_file>
> > + --format=propeller --propeller_output_module_name
> > + --out=<propeller_profile_prefix>_cc_profile.txt
> > + --propeller_symorder=<propeller_profile_prefix>_ld_profile.txt
>
> Prevously I am using perf-6.8.5-0.hs1.hsx.el9.x86_64 and it works fine.
> Now in my system, the perf is upgraded to 6.12.gadc218676eef
>
> [root@twshared7248.15.atn5 ~]# perf --version
> perf version 6.12.gadc218676eef
>
> and create_llvm_prof does not work any more.
>
> The command to collect sampling data:
>
> # perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c 500009 -- stress --cpu 36 --io 36 --vm 36 --vm-bytes 128M --timeout 300s
> stress: info: [536354] dispatching hogs: 36 cpu, 36 io, 36 vm, 0 hdd
> stress: info: [536354] successful run completed in 300s
> [ perf record: Woken up 2210 times to write data ]
> [ perf record: Captured and wrote 562.529 MB perf.data (701971 samples) ]
> # uname -r
> 6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39
>
> The kernel is a 6.11 lto kernel.
>
> I then run the following command:
> $ cat ../run.sh
> # perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c 500009 \
> # -- stress --cpu 36 --io 36 --vm 36 --vm-bytes 128M --timeout 300s
> # good: perf-6.8.5-0.hs1.hsx.el9.x86_64
>
> # <propeller_profile_prefix>: /tmp/propeller
> ./create_llvm_prof --binary=vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39 \
> --profile=perf.data \
> --format=propeller --propeller_output_module_name \
> --out=/tmp/propeller_cc_profile.txt \
> --propeller_symorder=/tmp/propeller_ld_profile.txt
>
> $ ./run.sh
> WARNING: Logging before InitGoogleLogging() is written to STDERR
> I20241212 13:12:18.401772 463318 llvm_propeller_binary_content.cc:376] 'vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39' is PIE: 0
> I20241212 13:12:18.403692 463318 llvm_propeller_binary_content.cc:380] 'vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39' is relocatable: 0
> I20241212 13:12:18.404873 463318 llvm_propeller_binary_content.cc:388] Build Id found in 'vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39': eaacd5a14abc48cf832b3ad0fa6c64635ab569a8
> I20241212 13:12:18.521499 463318 llvm_propeller_binary_content.cc:376] 'vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39' is PIE: 0
> I20241212 13:12:18.521530 463318 llvm_propeller_binary_content.cc:380] 'vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39' is relocatable: 0
> I20241212 13:12:18.521553 463318 llvm_propeller_binary_content.cc:388] Build Id found in 'vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39': eaacd5a14abc48cf832b3ad0fa6c64635ab569a8
> I20241212 13:12:18.521611 463318 llvm_propeller_perf_lbr_aggregator.cc:51] Parsing [1/1] perf.data ...
> [ERROR:/home/runner/work/autofdo/autofdo/third_party/perf_data_converter/src/quipper/perf_reader.cc:1386] Event size 132 after uint64_t alignment of the filename length is greater than event size 128 reported by perf for the buildid event of type 0
> W20241212 13:12:18.521708 463318 llvm_propeller_perf_lbr_aggregator.cc:55] Skipped profile [1/1] perf.data: FAILED_PRECONDITION: Failed to read perf data file: [1/1] perf.data
> W20241212 13:12:18.521718 463318 llvm_propeller_perf_lbr_aggregator.cc:67] Too few branch records in perf data.
> E20241212 13:12:18.554437 463318 create_llvm_prof.cc:238] FAILED_PRECONDITION: No perf file is parsed, cannot proceed.
>
>
> Could you help take a look why perf 12 does not work with create_llvm_prof?
> The create_llvm_prof is downloaded from https://github.com/google/autofdo/releases/tag/v0.30.1.
I think Peter may have reported the same issue on GitHub?
https://github.com/google/autofdo/issues/233
I wonder if this is a kernel side or perf tool regression?
Cheers,
Nathan
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v7 7/7] Add Propeller configuration for kernel build
2024-12-12 21:34 ` Nathan Chancellor
@ 2024-12-12 22:03 ` Rong Xu
0 siblings, 0 replies; 30+ messages in thread
From: Rong Xu @ 2024-12-12 22:03 UTC (permalink / raw)
To: Nathan Chancellor
Cc: Yonghong Song, Alice Ryhl, Andrew Morton, Arnd Bergmann,
Bill Wendling, Borislav Petkov, Breno Leitao, Brian Gerst,
Dave Hansen, David Li, Han Shen, Heiko Carstens, H. Peter Anvin,
Ingo Molnar, Jann Horn, Jonathan Corbet, Josh Poimboeuf,
Juergen Gross, Justin Stitt, Kees Cook, Masahiro Yamada,
Mike Rapoport (IBM), Nick Desaulniers, Nicolas Schier,
Paul E. McKenney, Peter Zijlstra, Sami Tolvanen, Thomas Gleixner,
Wei Yang, workflows, Miguel Ojeda, Maksim Panchenko,
David S. Miller, Andreas Larsson, Yabin Cui, Krzysztof Pszeniczny,
Sriraman Tallam, Stephane Eranian, x86, linux-arch, sparclinux,
linux-doc, linux-kbuild, linux-kernel, llvm, Peter Jung
We will take a look at this issue and get back to you. Thanks for
reporting this.
Best Regards,
-Rong
On Thu, Dec 12, 2024 at 1:34 PM Nathan Chancellor <nathan@kernel.org> wrote:
>
> On Thu, Dec 12, 2024 at 01:20:46PM -0800, Yonghong Song wrote:
> ...
> > > +5) Use the create_llvm_prof tool (https://github.com/google/autofdo) to
> > > + generate Propeller profile. ::
> > > +
> > > + $ create_llvm_prof --binary=<vmlinux> --profile=<perf_file>
> > > + --format=propeller --propeller_output_module_name
> > > + --out=<propeller_profile_prefix>_cc_profile.txt
> > > + --propeller_symorder=<propeller_profile_prefix>_ld_profile.txt
> >
> > Prevously I am using perf-6.8.5-0.hs1.hsx.el9.x86_64 and it works fine.
> > Now in my system, the perf is upgraded to 6.12.gadc218676eef
> >
> > [root@twshared7248.15.atn5 ~]# perf --version
> > perf version 6.12.gadc218676eef
> >
> > and create_llvm_prof does not work any more.
> >
> > The command to collect sampling data:
> >
> > # perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c 500009 -- stress --cpu 36 --io 36 --vm 36 --vm-bytes 128M --timeout 300s
> > stress: info: [536354] dispatching hogs: 36 cpu, 36 io, 36 vm, 0 hdd
> > stress: info: [536354] successful run completed in 300s
> > [ perf record: Woken up 2210 times to write data ]
> > [ perf record: Captured and wrote 562.529 MB perf.data (701971 samples) ]
> > # uname -r
> > 6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39
> >
> > The kernel is a 6.11 lto kernel.
> >
> > I then run the following command:
> > $ cat ../run.sh
> > # perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c 500009 \
> > # -- stress --cpu 36 --io 36 --vm 36 --vm-bytes 128M --timeout 300s
> > # good: perf-6.8.5-0.hs1.hsx.el9.x86_64
> >
> > # <propeller_profile_prefix>: /tmp/propeller
> > ./create_llvm_prof --binary=vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39 \
> > --profile=perf.data \
> > --format=propeller --propeller_output_module_name \
> > --out=/tmp/propeller_cc_profile.txt \
> > --propeller_symorder=/tmp/propeller_ld_profile.txt
> >
> > $ ./run.sh
> > WARNING: Logging before InitGoogleLogging() is written to STDERR
> > I20241212 13:12:18.401772 463318 llvm_propeller_binary_content.cc:376] 'vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39' is PIE: 0
> > I20241212 13:12:18.403692 463318 llvm_propeller_binary_content.cc:380] 'vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39' is relocatable: 0
> > I20241212 13:12:18.404873 463318 llvm_propeller_binary_content.cc:388] Build Id found in 'vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39': eaacd5a14abc48cf832b3ad0fa6c64635ab569a8
> > I20241212 13:12:18.521499 463318 llvm_propeller_binary_content.cc:376] 'vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39' is PIE: 0
> > I20241212 13:12:18.521530 463318 llvm_propeller_binary_content.cc:380] 'vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39' is relocatable: 0
> > I20241212 13:12:18.521553 463318 llvm_propeller_binary_content.cc:388] Build Id found in 'vmlinux-6.11.1-0_fbk0_lto_rc19_612_gb572dfac1b39': eaacd5a14abc48cf832b3ad0fa6c64635ab569a8
> > I20241212 13:12:18.521611 463318 llvm_propeller_perf_lbr_aggregator.cc:51] Parsing [1/1] perf.data ...
> > [ERROR:/home/runner/work/autofdo/autofdo/third_party/perf_data_converter/src/quipper/perf_reader.cc:1386] Event size 132 after uint64_t alignment of the filename length is greater than event size 128 reported by perf for the buildid event of type 0
> > W20241212 13:12:18.521708 463318 llvm_propeller_perf_lbr_aggregator.cc:55] Skipped profile [1/1] perf.data: FAILED_PRECONDITION: Failed to read perf data file: [1/1] perf.data
> > W20241212 13:12:18.521718 463318 llvm_propeller_perf_lbr_aggregator.cc:67] Too few branch records in perf data.
> > E20241212 13:12:18.554437 463318 create_llvm_prof.cc:238] FAILED_PRECONDITION: No perf file is parsed, cannot proceed.
> >
> >
> > Could you help take a look why perf 12 does not work with create_llvm_prof?
> > The create_llvm_prof is downloaded from https://github.com/google/autofdo/releases/tag/v0.30.1.
>
> I think Peter may have reported the same issue on GitHub?
>
> https://github.com/google/autofdo/issues/233
>
> I wonder if this is a kernel side or perf tool regression?
>
> Cheers,
> Nathan
^ permalink raw reply [flat|nested] 30+ messages in thread
end of thread, other threads:[~2024-12-12 22:03 UTC | newest]
Thread overview: 30+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-02 17:51 [PATCH v7 0/7] Add AutoFDO and Propeller support for Clang build Rong Xu
2024-11-02 17:51 ` [PATCH v7 1/7] Add AutoFDO " Rong Xu
2024-11-02 19:46 ` Peter Jung
2024-11-02 19:53 ` Peter Jung
2024-11-04 4:50 ` Han Shen
2024-11-04 16:05 ` Peter Jung
2024-11-04 17:30 ` Peter Jung
2024-11-04 20:24 ` Han Shen
2024-11-05 7:25 ` Rong Xu
2024-11-05 14:33 ` Peter Jung
2024-11-05 14:56 ` Peter Jung
2024-11-05 17:19 ` Peter Jung
2024-11-05 17:51 ` Rong Xu
2024-11-02 17:51 ` [PATCH v7 2/7] objtool: Fix unreachable instruction warnings for weak functions Rong Xu
2024-11-02 17:51 ` [PATCH v7 3/7] Adjust symbol ordering in text output section Rong Xu
2024-12-01 14:31 ` [PATCH v7 3/7] Adjust symbol ordering in text output section [openrisc boot failure] Guenter Roeck
2024-12-02 6:39 ` Masahiro Yamada
2024-11-02 17:51 ` [PATCH v7 4/7] Add markers for text_unlikely and text_hot sections Rong Xu
2024-11-02 17:51 ` [PATCH v7 5/7] AutoFDO: Enable -ffunction-sections for the AutoFDO build Rong Xu
2024-11-02 17:51 ` [PATCH v7 6/7] AutoFDO: Enable machine function split optimization for AutoFDO Rong Xu
2024-11-02 17:51 ` [PATCH v7 7/7] Add Propeller configuration for kernel build Rong Xu
2024-11-07 20:45 ` Nathan Chancellor
2024-11-07 21:54 ` Rong Xu
2024-12-12 21:20 ` Yonghong Song
2024-12-12 21:34 ` Nathan Chancellor
2024-12-12 22:03 ` Rong Xu
2024-11-06 16:08 ` [PATCH v7 0/7] Add AutoFDO and Propeller support for Clang build Masahiro Yamada
2024-11-06 19:00 ` Rong Xu
2024-11-07 14:57 ` Masahiro Yamada
2024-11-07 18:44 ` Rong Xu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).