From patchwork Sun Jul 28 20:29:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Rong Xu X-Patchwork-Id: 13744048 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E68A57346F for ; Sun, 28 Jul 2024 20:30:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722198652; cv=none; b=Y/VCVFYaf2ZRRVsdqqUFFX9IrTfl2VKpYHJZeeWygDzOh1pNCflsRN1cTpE7q4IlotDwG28l6+J79JCkXcQwx+VLCtdhdQiXw0F5FFWktymIoGlLYfxLjhG/Z2o0X0XG+dzxdta+C6ljJmlkSbV4/NuH5qUVkvDZOmVXBphMEwI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722198652; c=relaxed/simple; bh=1slrsF4Hlf8Hp/QXTPOV94EZfMBZN7LxrqA/xBkKf3M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=U3vdG8QO64CzEmJPnwposRJS3JDtNywd7s0+fPHTZ40QDlgWT41m//jueoBd6fsB8gmwlKJiWoThteE1Pscbf+gUOl3bkkfIWorFg7a8FfpThkKLu9P2MVJkYs6i7bMfGPg4ytzb3gXdRIloCYnDkTTBWUSkZ6Hy92WF8DB3S/4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--xur.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=HeKaN+rQ; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--xur.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="HeKaN+rQ" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e05e9e4dfbeso3117561276.1 for ; Sun, 28 Jul 2024 13:30:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722198649; x=1722803449; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=QVHCT9RvsK3VuE7zEDFg518c3S1iQ9syBfgTZJ1sM2A=; b=HeKaN+rQDkrYNnt4dSwJOFMxwG/dJnniwWOnJvsO+aE2OQaymEVui80kE/7PyMlEx7 wjmeYjWgHYCyUbwT0+ekxk6i09djpwrROrGB6nSrj6U5hXe4e3AeRPONS1zeFnB+tbTd 4fnIX1XuOS6wHcdXfv8pCeFsU+3Syi6SaiH773+yyhoodG1dHzrXABvZVmjqID3Lrglv 7lpsGGn82Lbtcds5iDGg3rOYBmrK/OZQWqaMbGXpI1bwh3psip030UXLNAov+TdDTJUv 4ZpBnpLUzmvleOy0DwMojBSbIjoDikDpudMoV8EWc6goJPCrcco/0U9q4oaEBgQtuSh6 kIpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722198649; x=1722803449; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=QVHCT9RvsK3VuE7zEDFg518c3S1iQ9syBfgTZJ1sM2A=; b=xVOaFRRITCCggShU0yBvB1pSiQeFDQ2MPdwLz58Z7GerzrY+ODEstOkMCb/OX22MeU P7Yz0PH13/YduXHKsbX/zYkbLPDCSAXgDe/XMc212YsQ7phgsXa+RoRqkycfgV0QpwvM KHzjiVLpqZCkZaN8cnjU7lq8VgfTDoIwstNV79brd86XwxQ9fX3p17sMm6dNrMfbHRN1 O2qNbTBuX4JPLqBTTuIyWVfv7RyEPQwwU+uJBeBHCuNIyxfFvdMhXu5qrRXRl7gOdVUW M7pa2PSCI5wLUgT2/diI1EDBPB8aVeV4B0X9CQQeEP5tJMLdG30U+7ORWgaJXiYhvkaS Oa0w== X-Forwarded-Encrypted: i=1; AJvYcCWyyYK+TM+cz0DC6YGGqSpPezf+FOYJzH1RATtscLmgXdESEwNxpsGRZWJ2p8vss11EqNtfpINX/X4LaasjLD6Jn0QU1z4ikVVx8OOk X-Gm-Message-State: AOJu0YwRd1NX0FPgsPrf96Cmo4WNeJpNPPQfC4p9K+U6rSqwc98trTJB UcwIpJrtZp/mj0oVgAz9BYhJTsUEb5hSudodNZCUj8l83iM0URHHkW7LXqhwo1vUDw== X-Google-Smtp-Source: AGHT+IGo5aItSeb/vpdkLjP2AUowaXTsHznCqCbQFmsWxXPcX/0j5k8mMdMzFSuWVf/Jc9tAP6jYCv0= X-Received: from xur.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:2330]) (user=xur job=sendgmr) by 2002:a25:9707:0:b0:e08:a80c:8742 with SMTP id 3f1490d57ef6-e0b54467758mr135369276.5.1722198648804; Sun, 28 Jul 2024 13:30:48 -0700 (PDT) Date: Sun, 28 Jul 2024 13:29:54 -0700 In-Reply-To: <20240728203001.2551083-1-xur@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240728203001.2551083-1-xur@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240728203001.2551083-2-xur@google.com> Subject: [PATCH 1/6] Add AutoFDO support for Clang build From: Rong Xu To: Rong Xu , Han Shen , Sriraman Tallam , David Li , Jonathan Corbet , Masahiro Yamada , Nathan Chancellor , Nicolas Schier , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Ard Biesheuvel , Arnd Bergmann , Josh Poimboeuf , Peter Zijlstra , Nick Desaulniers , Bill Wendling , Justin Stitt , Vegard Nossum , John Moon , Andrew Morton , Heiko Carstens , Luis Chamberlain , Samuel Holland , Mike Rapoport , "Paul E . McKenney" , Rafael Aquini , Petr Pavlu , Eric DeVolder , Bjorn Helgaas , Randy Dunlap , Benjamin Segall , Breno Leitao , Wei Yang , Brian Gerst , Juergen Gross , Palmer Dabbelt , Alexandre Ghiti , Kees Cook , Sami Tolvanen , Xiao Wang , Jan Kiszka Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, llvm@lists.linux.dev, Krzysztof Pszeniczny , Stephane Eranian 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 \ -o -- 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 -o -- 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= --perfdata= \ -o or $ create_llvm_prof --binary= --profile= \ --format=extbinary -o Note that multiple AutoFDO profile files can be merged into one via: $ llvm-profdata merge -o ... 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= Co-developed-by: Han Shen Signed-off-by: Han Shen Signed-off-by: Rong Xu Suggested-by: Sriraman Tallam Suggested-by: Krzysztof Pszeniczny Suggested-by: Nick Desaulniers Suggested-by: Stephane Eranian --- Documentation/dev-tools/autofdo.rst | 203 ++++++++++++++++++++++++++++ Documentation/dev-tools/index.rst | 1 + MAINTAINERS | 7 + Makefile | 1 + arch/Kconfig | 20 +++ arch/x86/Kconfig | 1 + scripts/Makefile.autofdo | 20 +++ scripts/Makefile.lib | 10 ++ tools/objtool/check.c | 1 + 9 files changed, 264 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 000000000000..e3f7ee8ee6bb --- /dev/null +++ b/Documentation/dev-tools/autofdo.rst @@ -0,0 +1,203 @@ +.. 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: + + .. code-block:: make + + CONFIG_AUTOFDO_CLANG=y + +Customization +============= + +You can enable or disable AutoFDO build for individual file and directories by +adding a line similar to the following to the respective kernel Makefile: + +- For enabling a single file (e.g. foo.o) + + .. code-block:: make + + AUTOFDO_PROFILE_foo.o := y + +- For enabling all files in one directory + + .. code-block:: make + + AUTOFDO_PROFILE := y + +- For disabling one file + + .. code-block:: make + + AUTOFDO_PROFILE_foo.o := n + +- For disabling all files in one directory + + .. code-block:: make + + 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, + + .. code-block:: make + + $ make menuconfig LLVM=1 + + + Turn on AutoFDO build config: + + .. code-block:: make + + CONFIG_AUTOFDO_CLANG=y + + With a configuration that with LLVM enabled, use the following command: + + .. code-block:: sh + + $ scripts/config -e AUTOFDO_CLANG + + After getting the config, build with + + .. code-block:: make + + $ 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: + + .. code-block:: sh + + $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c -o -- + + - For AMD platforms: For Intel platforms: + The supported systems are: Zen3 with BRS, or Zen4 with amd_lbr_v2. To check, + For Zen3: + + .. code-block:: sh + + $ cat proc/cpuinfo | grep " brs" + + For Zen4: + + .. code-block:: sh + + $ cat proc/cpuinfo | grep amd_lbr_v2 + + The following command generated the perf data file: + + .. code-block:: sh + + $ perf record --pfm-events RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k -a -N -b \ + -c -o -- + +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. + + .. code-block:: sh + + $ llvm-profgen --kernel --binary= --perfdata= -o + + or + .. code-block:: sh + + $ create_llvm_prof --binary= --profile= --format=extbinary -o + + Note that multiple AutoFDO profile files can be merged into one via: + + .. code-block:: sh + + $ llvm-profdata merge -o ... + + +6) Rebuild the kernel using the AutoFDO profile file with the same config as step 1, + (Note CONFIG_AUTOFDO_CLANG needs to be enabled): + + .. code-block:: sh + + $ make LLVM=1 CLANG_AUTOFDO_PROFILE= +M: Han Shen +S: Supported +F: Documentation/dev-tools/autofdo.rst +F: scripts/Makefile.autofdo + AUXILIARY BUS DRIVER M: Greg Kroah-Hartman R: Dave Ertman diff --git a/Makefile b/Makefile index 06aa6402b385..5ae30cc94a26 100644 --- a/Makefile +++ b/Makefile @@ -1024,6 +1024,7 @@ include-$(CONFIG_UBSAN) += scripts/Makefile.ubsan include-$(CONFIG_KCOV) += scripts/Makefile.kcov include-$(CONFIG_RANDSTRUCT) += scripts/Makefile.randstruct include-$(CONFIG_GCC_PLUGINS) += scripts/Makefile.gcc-plugins +include-$(CONFIG_AUTOFDO_CLANG) += scripts/Makefile.autofdo include $(addprefix $(srctree)/, $(include-y)) diff --git a/arch/Kconfig b/arch/Kconfig index 975dd22a2dbd..e12599c4ab63 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -802,6 +802,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 1d7122a1883e..dca526b1364f 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -122,6 +122,7 @@ config X86 select ARCH_USES_CFI_TRAPS if X86_64 && CFI_CLANG select ARCH_SUPPORTS_LTO_CLANG select ARCH_SUPPORTS_LTO_CLANG_THIN + 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 000000000000..882fa7e801ba --- /dev/null +++ b/scripts/Makefile.autofdo @@ -0,0 +1,20 @@ +# 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 + +ifdef CLANG_AUTOFDO_PROFILE +CFLAGS_AUTOFDO_CLANG += -fprofile-sample-use=$(CLANG_AUTOFDO_PROFILE) +endif + +ifdef CONFIG_LTO_CLANG +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 +endif + +export CFLAGS_AUTOFDO_CLANG diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib index 9f06f6aaf7fc..c2cab5adaf25 100644 --- a/scripts/Makefile.lib +++ b/scripts/Makefile.lib @@ -209,6 +209,16 @@ _c_flags += $(if $(patsubst n%,, \ -D__KCSAN_INSTRUMENT_BARRIERS__) endif +# +# Enable Clang's AutoFDO build flags for a file or directory depending 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)y), \ + $(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 0a33d9195b7a..254913498c3c 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -4488,6 +4488,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; From patchwork Sun Jul 28 20:29:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rong Xu X-Patchwork-Id: 13744049 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 81AA577F2F for ; Sun, 28 Jul 2024 20:30:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722198655; cv=none; b=BQeqPaxf5iyG7Kq2u+9iXZlOPQIn6Bvdvqm5+rDeXh8878q+ms6/AKvG/oUiBzdWVI1avTytwK6iZTi/QN/zo+jFPbe93DddmvPTbdW5LmOuhtkQ3jQjAezasg3oa+PdYj/fPVC7bTiWY1txpw2swm/fu1eZXM37TYbRxrRj3lw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722198655; c=relaxed/simple; bh=FBqzozWJhNvJ4oM6y6ThYCUCO6Sc4TGcBQGQrbyCu+w=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KNj1YhlKLTMTQSsx/zVxgEDF3c+FQXrDqKUGyXJdXbxQm2y7BqDZT4ZZrcYfqJDQDHOxKtO1OY9lAqooUJx1ogZB5qHl2XPdcWMVMHO3WWEPqkAv6CpYj9s9khqltSiYnV2VJZ2rmGpr2WT05SzzWh4NJXvS2p5Y3Ei+pdC1vV0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--xur.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=HAVlKJ+S; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--xur.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="HAVlKJ+S" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e087ed145caso2544865276.3 for ; Sun, 28 Jul 2024 13:30:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722198653; x=1722803453; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dHGeCL3vH0qLBFDWm8avFROR6jr66E4NssJlgZilgo4=; b=HAVlKJ+SJ0zr2QU0Hj8jIIqFPWrKyHppDOHwHx0bYw71kSbxvOi3wzNGlfIldXotVY XJn4QUqdeqqVQZ7aV8FWy1AvyABNTrsSIon8fGaLifNKG3jpdFsqbicdAOgbEQRbftfn 5UhiLxdmlMs5fBykUeB80RsiSKur/ydE6sFCwaDE7vuAjlFw/2fKwcAg8Vp//FZ9Cpuv n+dylMmRXCxKHoyVV3HstJvQXrE5x17xbL9C52iQu8ER9uTENgITIch54+NqnTng4GcX dD2gl07jrln7rJVH/QFxi5qAxXCsAkEAjUlNTffGJd4gyMepzFg68ddzIl6NeZDJB1WC KUeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722198653; x=1722803453; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dHGeCL3vH0qLBFDWm8avFROR6jr66E4NssJlgZilgo4=; b=DQm5LXuuSNsfZtHnPLmIQhe/V3x4TS9mxxaranWYMVt3rcqyyXY08GlLDHnNYynTg5 VmfzwPh2cgHJF5BwTFMNMHqcqXBOJ9KK9oPvcTUwibohszxpeT08D66MS8/EwG1fK6gg 5NUmapd8JgpAa0B+iltkE5fPxXiqJNErQX1g2h0qw63FwqQYKcTye2gJx0ULnCEFoQfF bRsScj6YGEN050pdHvhzRTN+NYn32cpMMBQRF0b1nmIEw1SuuNGdZU81ynPXZSKdCr5M zXvePCJnI0GkkrRQeIUBFp7Ydd90s4qNZvDmZ7C2oMiXg9y6krqGSTa0wvJoo5nOSaxL JA/w== X-Forwarded-Encrypted: i=1; AJvYcCXYPAOLfbamC/RzFs1oWsSJlQtmfr7wgNAF3u0RZo/fpAB6ZW3uPHqGUYBh5MGnXvHyOpPo0NrgNWRj5GL5X7efaxrJo0I5sMUo4J8w X-Gm-Message-State: AOJu0YyOIKWo283d2OmHHLWhMJHKH6hR8orrt7HBvFkOOksGkICSZFgK cD2tByCJ5JvSdi9HaUKdLfjvi4ARSI8T+hrzYnCvPpQIXd26wAEfZ7E+HdPJUJkMCA== X-Google-Smtp-Source: AGHT+IFbB1GtuSWiJ25Z5CJHBsZeUzd/7OcS+ZwDnzKKXYJCUjO8F45P4dz+nwWaMnszX5UCfblv1II= X-Received: from xur.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:2330]) (user=xur job=sendgmr) by 2002:a05:6902:1248:b0:e0b:10e9:88f8 with SMTP id 3f1490d57ef6-e0b545590famr362781276.8.1722198652426; Sun, 28 Jul 2024 13:30:52 -0700 (PDT) Date: Sun, 28 Jul 2024 13:29:55 -0700 In-Reply-To: <20240728203001.2551083-1-xur@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240728203001.2551083-1-xur@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240728203001.2551083-3-xur@google.com> Subject: [PATCH 2/6] objtool: Fix unreachable instruction warnings for weak funcitons From: Rong Xu To: Rong Xu , Han Shen , Sriraman Tallam , David Li , Jonathan Corbet , Masahiro Yamada , Nathan Chancellor , Nicolas Schier , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Ard Biesheuvel , Arnd Bergmann , Josh Poimboeuf , Peter Zijlstra , Nick Desaulniers , Bill Wendling , Justin Stitt , Vegard Nossum , John Moon , Andrew Morton , Heiko Carstens , Luis Chamberlain , Samuel Holland , Mike Rapoport , "Paul E . McKenney" , Rafael Aquini , Petr Pavlu , Eric DeVolder , Bjorn Helgaas , Randy Dunlap , Benjamin Segall , Breno Leitao , Wei Yang , Brian Gerst , Juergen Gross , Palmer Dabbelt , Alexandre Ghiti , Kees Cook , Sami Tolvanen , Xiao Wang , Jan Kiszka Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, llvm@lists.linux.dev, Krzysztof Pszeniczny 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 Signed-off-by: Han Shen Signed-off-by: Rong Xu Suggested-by: Sriraman Tallam Suggested-by: Krzysztof Pszeniczny --- tools/objtool/elf.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 3d27983dc908..fa88bb254ccc 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -224,12 +224,15 @@ 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. + * Use the first node in rb_tree when hole.sym is NULL. + */ + 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 */ From patchwork Sun Jul 28 20:29:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Rong Xu X-Patchwork-Id: 13744050 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C850878C84 for ; Sun, 28 Jul 2024 20:30:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722198658; cv=none; b=kkeTm/nsGZzTAm/o6htOjhQWaYxDZjsnZY+DqdrsApsfcnGAETbF3ajR0/nmKEpbvX9h4mcVy2CpKdetZMDo96jvKgBH8hU5xWy+4wQQ05KdUIdm4R0J1MFrisfkdO4fK4cwM8QSoBkT/bMxRntam01Et+RG3IaJNN5ZDKDMK60= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722198658; c=relaxed/simple; bh=5P/ds+u1Nc4DlKNiDUD6ZuYs75VWIvFx1DNGM0tCz1Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RAGc8xm21UQ48U3ozVYSi9S9cQ+KqBdok9QRBSHI3+cRvYOxhuDy/1Rfdrd9eBTve9o8dBRYTKUFPcUdrxxq28miYOCJyERcW6fnrEyVBtF99dBbOn2EejYW6qr4j1RTex2u9mh+Fp+/kXVKCoF0/R1fL9/thFP+NBmMbCTDdA0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--xur.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cBfLi/ND; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--xur.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cBfLi/ND" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-7a732cdd03fso2010122a12.0 for ; Sun, 28 Jul 2024 13:30:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722198656; x=1722803456; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=o2mnjrwU8q3IWuUcbnv2YL1wGLUV3JerNGbL3ta/V8U=; b=cBfLi/NDtBWBjPzpPP+v5+G1124d8hHZzbXbgoiu4pqz+FAKvbw9E1ZLyEQgrBXN25 QbpBDIRhwXvjGIFkLpOyTIhPDblH6z/L1doVuTsiqPZBd3276WcIB7fjno5tWbyx2LVp o68jtHuRaLpUhnyQpxWw/xwyAg+WMpY61e+epj7knLvBbskeNjDP3nsirkhsx/Uhe9SK cmvyKn7Jm0OiGkRZf/q5fyR1FTwd5Hz+yuctGrsZsUQCxLf1Y1wEd50xCsjePOvSawfc btR56IofvZ/TgXqTUer2V6Vh4qKhnwGLvDoQerI4gDdZzE1I6CVclK+x3p6QTbCmJgC7 dH1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722198656; x=1722803456; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=o2mnjrwU8q3IWuUcbnv2YL1wGLUV3JerNGbL3ta/V8U=; b=WTTw+f9Y8tFLZg3Y/jr9jBYyDhiOZYnoqhLAXmG9jQEXzdsDkDMzTpsoHnKhuErSeR YPmd/wom8alm5GIpmIl8iXls66ZVNr9p3CwtKLy/rmoqpGadQqJRyHHwhg1xxlXz9lZB 56VdNplcN+jDON8tojKw0ufZykQu4cS523PX0atXPvIS0hpMN876s1mf4NhL4bst+9nB aifFjjbufzNMcunsGgSVJDaXIoFiknKhj7143htmjUAC4IGCQNf7el24kyMLO61RE8eX S2kU9Xn4JXlVz6330Ju/q8jZzLq/Iso/+mXrX6ESu06LNUMWD/+vdD8bZkXdOozQ8vMc os8g== X-Forwarded-Encrypted: i=1; AJvYcCURtK9HkHARF4S7AS7xhBO5oKRyuD10I/J3Jy/6h6TSQES0QiPbO5fQGlmwdfWaTXpHb9onY9n64yQjH5rU1IWpNeRNxZV3iPSMt0oQ X-Gm-Message-State: AOJu0YyOJuTPIuQG2ON9S0FIalU0YYGiQYCO52x1Iv4JOw81FnXu7Q0w vm5PCo2rvMoIqdBFtiEo0YsqrV9/r7OIvxJ6X+qw09MnFUmZ4hsFCIuv6/W4E0k2FA== X-Google-Smtp-Source: AGHT+IF+cnWDEef0AOr5iV5UnZQ0Nmg6qihUgkJJVOtSeHchBxmd2+NRGmp/GGHZSyrSRRs3cz4mBGg= X-Received: from xur.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:2330]) (user=xur job=sendgmr) by 2002:a63:e903:0:b0:75c:49a4:2a2c with SMTP id 41be03b00d2f7-7ac8f942a86mr12318a12.7.1722198655784; Sun, 28 Jul 2024 13:30:55 -0700 (PDT) Date: Sun, 28 Jul 2024 13:29:56 -0700 In-Reply-To: <20240728203001.2551083-1-xur@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240728203001.2551083-1-xur@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240728203001.2551083-4-xur@google.com> Subject: [PATCH 3/6] Change the symbols order when --ffuntion-sections is enabled From: Rong Xu To: Rong Xu , Han Shen , Sriraman Tallam , David Li , Jonathan Corbet , Masahiro Yamada , Nathan Chancellor , Nicolas Schier , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Ard Biesheuvel , Arnd Bergmann , Josh Poimboeuf , Peter Zijlstra , Nick Desaulniers , Bill Wendling , Justin Stitt , Vegard Nossum , John Moon , Andrew Morton , Heiko Carstens , Luis Chamberlain , Samuel Holland , Mike Rapoport , "Paul E . McKenney" , Rafael Aquini , Petr Pavlu , Eric DeVolder , Bjorn Helgaas , Randy Dunlap , Benjamin Segall , Breno Leitao , Wei Yang , Brian Gerst , Juergen Gross , Palmer Dabbelt , Alexandre Ghiti , Kees Cook , Sami Tolvanen , Xiao Wang , Jan Kiszka Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, llvm@lists.linux.dev, Krzysztof Pszeniczny 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 when the -ffunction-sections flag is enabled. Specifically, it repositions sections with certain fixed patterns (for example .text.unlikely) before TEXT_MAIN, ensuring that they are grouped and matched together. 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. Co-developed-by: Han Shen Signed-off-by: Han Shen Signed-off-by: Rong Xu Suggested-by: Sriraman Tallam Suggested-by: Krzysztof Pszeniczny --- include/asm-generic/vmlinux.lds.h | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h index 5703526d6ebf..f3de66bda293 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -582,9 +582,21 @@ * 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. + * code elimination or function-section is enabled. Match these symbols + * first when in these builds. */ +#if defined(CONFIG_LD_DEAD_CODE_DATA_ELIMINATION) || defined(CONFIG_LTO_CLANG) +#define TEXT_TEXT \ + *(.text.asan.* .text.tsan.*) \ + *(.text.unknown .text.unknown.*) \ + *(.text.unlikely .text.unlikely.*) \ + ALIGN_FUNCTION(); \ + *(.text.hot .text.hot.*) \ + *(TEXT_MAIN .text.fixup) \ + NOINSTR_TEXT \ + *(.ref.text) \ + MEM_KEEP(init.text*) +#else #define TEXT_TEXT \ ALIGN_FUNCTION(); \ *(.text.hot .text.hot.*) \ @@ -594,7 +606,8 @@ NOINSTR_TEXT \ *(.ref.text) \ *(.text.asan.* .text.tsan.*) \ - MEM_KEEP(init.text*) \ + MEM_KEEP(init.text*) +#endif /* sched.text is aling to function alignment to secure we have same From patchwork Sun Jul 28 20:29:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rong Xu X-Patchwork-Id: 13744051 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6B8677BB15 for ; Sun, 28 Jul 2024 20:31:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722198662; cv=none; b=gVsFlEs61ijbHFxNWT3Odn76hw/SVRokVWcq6sX3EVH5kPKdooX4UX9TamSwKoPYnIqIJGUDip8N9W1QJdgdzYWOa7xgLtaHYlCUQkcFTgawL5uE7Z7HZwsPAHkjhvr4LFJhaqtTkn4IiIx4EOesfRCXAF+iOI9tPdl9CH0kjgs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722198662; c=relaxed/simple; bh=beGZIYM3TPtatxrKmLYPAD0/CJjAgMIMbsJ074Ik9tg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RKGmifxzgOcjGM4Y14LuYUdEC6kxH6V00zfTGD2PdhgmWvAzfbpekeD4Jr9Kn2NjPUdWDX4wsy2KWBU9W+5uBunzyMK/p1wQnZ+neNLmFBgoYKEUV8B/SXmOYc5F+FTEfONdzjr3TlgGmMSTyt/LWak9APxwr0/GtIWZVnMa6Bw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--xur.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Cbw9W6ak; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--xur.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Cbw9W6ak" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e0b3742b30bso2768018276.1 for ; Sun, 28 Jul 2024 13:31:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722198659; x=1722803459; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Nypg1QHLbTqa/1p2hSGPQd9/Gf8EHrTdP6hUDec9EdE=; b=Cbw9W6akg0cNIlC5zMjJAlxlLpwYzGB4rcfnqydC9Jze3jMb2XKqKzGywKdmsn/ijN c6MhSTyYHyEAmibBRh9dGqbOGFNum3nck1HFGOVA7duZoxDFns+AxrCKucm4tkyTgn6m K5oHXQhhQKUpCdP93JbAk3kYLeo5iMoJBXN9Y3pakzBjoBT63FrRkzA2GaanOCaXXENX ykkQGAP9vfGLVmDJaQgxg7lJ/Z1gxWfVIwXRrq8pzDMJggfmixLuHZZNFaZLeASdGd9I oiWiiteyOxAjMHTIhX6t1izT/TlPofqhWVMWpGZyvre1tO/+TyFNyfOSed1VXklQ6SqX nsFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722198659; x=1722803459; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Nypg1QHLbTqa/1p2hSGPQd9/Gf8EHrTdP6hUDec9EdE=; b=nd8iiAxqwQxp0gLrpSy4KvCrvFEzXaqE2VFg+kx4qaEI6gdOOzJC6lqhNNzU0GOasW L+7fHwVfrBbTO+BzcRCArlvjlbP9MPOvlBdJaBY5RcMqGZCDTtLwjBNipreAdZv4G5JZ 9jfELw+kI7MUcDJQr3XsJaP7b7EWbQWUhH40B5z9rvslkPfgMhYIyIAWHTfgfyxQhpl1 vdXuubX70O0iPLFTkRAZ/N79hr0711fgaLye5RY3DT8o33qQ7Zye27BOsnLIn1v8nQ9d 9EcS3QaDsjCCrRVf0QRODoEdvuQHOVOWitadT+dP/RSJtCvtvD/KT+vLxdwLpcqDdn65 cpFA== X-Forwarded-Encrypted: i=1; AJvYcCVSWTK3JZqnX/5bqcJewkUovT0a8TNv2gun8pjgEuGbv+nxl58SwZBiHbiFvv2LFF/gOcmhNxlpC2VkRo9NuxDNgY8nIsf1WOC6L0sv X-Gm-Message-State: AOJu0YxYkubHlEEA1csLQZOAi9OZL6n9SzPmDO76GH1sEXSuqDpVwTey VSD8HGlNBUIDpL4x6+PXYE2XN7zkltyBZxQbokrpoeBSXHjz+PYBvckID2ytyulgoQ== X-Google-Smtp-Source: AGHT+IGhcfBcAn7qASZpEvCq18PhN0EBXHuUDxWLgxYpaHcb/kQ/zFsB5MAYY4oJLzn0xh4JiWgv4U8= X-Received: from xur.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:2330]) (user=xur job=sendgmr) by 2002:a25:6844:0:b0:e0b:3432:73d4 with SMTP id 3f1490d57ef6-e0b5560e6b3mr58714276.1.1722198659209; Sun, 28 Jul 2024 13:30:59 -0700 (PDT) Date: Sun, 28 Jul 2024 13:29:57 -0700 In-Reply-To: <20240728203001.2551083-1-xur@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240728203001.2551083-1-xur@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240728203001.2551083-5-xur@google.com> Subject: [PATCH 4/6] AutoFDO: Enable -ffunction-sections for the AutoFDO build From: Rong Xu To: Rong Xu , Han Shen , Sriraman Tallam , David Li , Jonathan Corbet , Masahiro Yamada , Nathan Chancellor , Nicolas Schier , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Ard Biesheuvel , Arnd Bergmann , Josh Poimboeuf , Peter Zijlstra , Nick Desaulniers , Bill Wendling , Justin Stitt , Vegard Nossum , John Moon , Andrew Morton , Heiko Carstens , Luis Chamberlain , Samuel Holland , Mike Rapoport , "Paul E . McKenney" , Rafael Aquini , Petr Pavlu , Eric DeVolder , Bjorn Helgaas , Randy Dunlap , Benjamin Segall , Breno Leitao , Wei Yang , Brian Gerst , Juergen Gross , Palmer Dabbelt , Alexandre Ghiti , Kees Cook , Sami Tolvanen , Xiao Wang , Jan Kiszka Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, llvm@lists.linux.dev 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 Signed-off-by: Han Shen Signed-off-by: Rong Xu Suggested-by: Sriraman Tallam --- include/asm-generic/vmlinux.lds.h | 37 ++++++++++++++++++++++++------- scripts/Makefile.autofdo | 2 +- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h index f3de66bda293..97c8399e5532 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 LTO_CLANG, the linker also splits sections by default, so we need + * these macros to combine the sections during the final link. + * * 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..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 @@ -577,6 +584,20 @@ __cpuidle_text_end = .; \ __noinstr_text_end = .; +#ifdef CONFIG_AUTOFDO_CLANG +#define TEXT_HOT \ + __hot_text_start = .; \ + *(.text.hot .text.hot.*) \ + __hot_text_end = .; +#define TEXT_UNLIKELY \ + __unlikely_text_start = .; \ + *(.text.unlikely .text.unlikely.*) \ + __unlikely_text_end = .; +#else +#define TEXT_HOT *(.text.hot .text.hot.*) +#define TEXT_UNLIKELY *(.text.unlikely .text.unlikely.*) +#endif + /* * .text section. Map to function alignment to avoid address changes * during second ld run in second ld pass when generating System.map @@ -585,13 +606,14 @@ * code elimination or function-section is enabled. Match these symbols * first when in these builds. */ -#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_TEXT \ *(.text.asan.* .text.tsan.*) \ *(.text.unknown .text.unknown.*) \ - *(.text.unlikely .text.unlikely.*) \ + TEXT_UNLIKELY \ ALIGN_FUNCTION(); \ - *(.text.hot .text.hot.*) \ + TEXT_HOT \ *(TEXT_MAIN .text.fixup) \ NOINSTR_TEXT \ *(.ref.text) \ @@ -599,9 +621,9 @@ #else #define TEXT_TEXT \ ALIGN_FUNCTION(); \ - *(.text.hot .text.hot.*) \ + TEXT_HOT \ *(TEXT_MAIN .text.fixup) \ - *(.text.unlikely .text.unlikely.*) \ + TEXT_UNLIKELY \ *(.text.unknown .text.unknown.*) \ NOINSTR_TEXT \ *(.ref.text) \ @@ -609,7 +631,6 @@ MEM_KEEP(init.text*) #endif - /* 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 882fa7e801ba..f765bd9e81d7 100644 --- a/scripts/Makefile.autofdo +++ b/scripts/Makefile.autofdo @@ -5,7 +5,7 @@ CFLAGS_AUTOFDO_CLANG := -fdebug-info-for-profiling -mllvm -enable-fs-discriminator=true -mllvm -improved-fs-discriminator=true 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 From patchwork Sun Jul 28 20:29:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rong Xu X-Patchwork-Id: 13744052 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 248D07D3F5 for ; Sun, 28 Jul 2024 20:31:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722198665; cv=none; b=lwOFGYfyDDxWyNXxtuUhXpnx4crV6qEFQDgLwNNhh3ZcfRYFb3kM23s3wjQvEfmGn0BSBtIctMYvngunhK5NkSc1vBr+Mnq+OhW1eDjedn4gA/DEEnOFz7+D4D4gs4D34EUMvcQ7B7BsdaoLOLLM4BiFgBLaJdehemJxcyIujIM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722198665; c=relaxed/simple; bh=jgG0TSTo1c+hpUBT22yEfSilFpbm6otN2MQr5+RKpfs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=g30sXWn1DEd/dTuekplUlByvMB8IRjBpJUeJyZ+Srz2dmls2jje1nRqTbcFZzoCl7rUIDTSdoPI9rmu02dVWHDulj9efLZqnBIE1Z6c/O/CMS+8y/+CE0y3SurEGl8XtA2MdqY888SvwBuoFeOW9ql30aBRgEyRVrUHW7G6C6vY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--xur.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=f6BjeUdH; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--xur.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="f6BjeUdH" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-66890dbb7b8so46458847b3.0 for ; Sun, 28 Jul 2024 13:31:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722198663; x=1722803463; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Tokg3sFCTTXy/Tg/kyuYTkBel/otDfHpuf9TI5sSKRc=; b=f6BjeUdHR36Kg9BILRErBF2TT4LyUUUeWe/2pIZ5RJ+8bUz3/NbWjd66ppYH8KLMcg wybYF/uZjne3GMXh1E+X1zAYra/WCpKEUBjZOVjjzz0/kOx4veBmncDp4c0wvtTsPXg5 Q7gXKDwUc91Ffz70gcy8siG+Z0Z8BHkr4mNeJMykTEEKau1hFbkhlp/9cmPjSSyWsQTe MpRwuxKr7a7JOtgdouDxBAK4YVsVaopo8toXBrxqtqBLzkmJ/gJ/volGOdGKRdngDgrw IAbShFBlEXNFgyUQgorAI/NfrnUVubUQo28Ed5qPYkmr/8FVyRU0slQDy9KU844a+4q/ YX4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722198663; x=1722803463; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Tokg3sFCTTXy/Tg/kyuYTkBel/otDfHpuf9TI5sSKRc=; b=iTmTozmudMI2sGt4K5Wb89KWb44Rv5fMFQLvtat6/p38GxpOpxegUkCTgczHCs2kBZ MAa3fqAGUqK3ig1i8/InIASPvyNeUyUSegWpDLmowKrftW3HDAWitQr04mv7Y8vla++7 zLuakPhPckNBvQcGHbNtL7RMO1/1dfdpdfZvFmQIs2oOTPJm4vVOUGV9HJLr9eR2gGAg TpyboAYXw8j0ULyGp7eeOnSnxYKn93Ud4Jq2NPbXrzYJ6zl8PRPVlV43CKEbxiuoTLLj /QlW2LurULbSQ5sCMtMLZ0wrnCInK7qYnb96YAIXrgB0tp5jVNRz0qnHfA73HRIFcrfy VrmQ== X-Forwarded-Encrypted: i=1; AJvYcCXNfY9qQ+iLsrx6VM9Gwl2uQTzUQ5tWZ/tEJJ480hbcFZBcpaOIGi0JAVHEXqROHEytPzgfXl4I50gOZWYu9NKFn5KYpkMOa0l2r9HN X-Gm-Message-State: AOJu0Yz6FprvZaZr4aI2/2a9w30g4SVy8MipkJK5gHwR6KFmB+4laL/j w19QCtCUSzT+7aUbYoH2Ut4TzIsRNQ7TaogeqI4zYMwlIgh2kTdv1LfIOpFud6KpcQ== X-Google-Smtp-Source: AGHT+IGFJCBpCGKio3GvIQMtPQU0FwuQLnvFXOWRFKgHtvK/N12utXtZSpDgbRkZ6jpM9dmXhNWYM7k= X-Received: from xur.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:2330]) (user=xur job=sendgmr) by 2002:a05:690c:827:b0:667:8a45:d0f9 with SMTP id 00721157ae682-67a004a2775mr1250767b3.0.1722198663143; Sun, 28 Jul 2024 13:31:03 -0700 (PDT) Date: Sun, 28 Jul 2024 13:29:58 -0700 In-Reply-To: <20240728203001.2551083-1-xur@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240728203001.2551083-1-xur@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240728203001.2551083-6-xur@google.com> Subject: [PATCH 5/6] AutoFDO: Enable machine function split optimization for AutoFDO From: Rong Xu To: Rong Xu , Han Shen , Sriraman Tallam , David Li , Jonathan Corbet , Masahiro Yamada , Nathan Chancellor , Nicolas Schier , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Ard Biesheuvel , Arnd Bergmann , Josh Poimboeuf , Peter Zijlstra , Nick Desaulniers , Bill Wendling , Justin Stitt , Vegard Nossum , John Moon , Andrew Morton , Heiko Carstens , Luis Chamberlain , Samuel Holland , Mike Rapoport , "Paul E . McKenney" , Rafael Aquini , Petr Pavlu , Eric DeVolder , Bjorn Helgaas , Randy Dunlap , Benjamin Segall , Breno Leitao , Wei Yang , Brian Gerst , Juergen Gross , Palmer Dabbelt , Alexandre Ghiti , Kees Cook , Sami Tolvanen , Xiao Wang , Jan Kiszka Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, llvm@lists.linux.dev, Krzysztof Pszeniczny 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 Signed-off-by: Han Shen Signed-off-by: Rong Xu Suggested-by: Sriraman Tallam Suggested-by: Krzysztof Pszeniczny --- include/asm-generic/vmlinux.lds.h | 6 ++++++ scripts/Makefile.autofdo | 2 ++ 2 files changed, 8 insertions(+) diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h index 97c8399e5532..7d9dc8a3c046 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -593,9 +593,14 @@ defined(CONFIG_AUTOFDO_CLANG) __unlikely_text_start = .; \ *(.text.unlikely .text.unlikely.*) \ __unlikely_text_end = .; +#define TEXT_SPLIT \ + __split_text_start = .; \ + *(.text.split .text.split.[0-9a-zA-Z_]*) \ + __split_text_end = .; #else #define TEXT_HOT *(.text.hot .text.hot.*) #define TEXT_UNLIKELY *(.text.unlikely .text.unlikely.*) +#define TEXT_SPLIT #endif /* @@ -611,6 +616,7 @@ defined(CONFIG_AUTOFDO_CLANG) #define TEXT_TEXT \ *(.text.asan.* .text.tsan.*) \ *(.text.unknown .text.unknown.*) \ + TEXT_SPLIT \ TEXT_UNLIKELY \ ALIGN_FUNCTION(); \ TEXT_HOT \ diff --git a/scripts/Makefile.autofdo b/scripts/Makefile.autofdo index f765bd9e81d7..80ad06689947 100644 --- a/scripts/Makefile.autofdo +++ b/scripts/Makefile.autofdo @@ -6,6 +6,7 @@ CFLAGS_AUTOFDO_CLANG := -fdebug-info-for-profiling -mllvm -enable-fs-discriminat 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 @@ -14,6 +15,7 @@ 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 +KBUILD_LDFLAGS += -plugin-opt=-split-machine-functions endif endif From patchwork Sun Jul 28 20:29:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Rong Xu X-Patchwork-Id: 13744053 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B58DE7E591 for ; Sun, 28 Jul 2024 20:31:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722198670; cv=none; b=aC5DfHO7R8F0FmLrEySq974/9HBVBJd775SI1aoB/gSuoc56nhNAS354JtDdrhjSVcxxFg/3JLKKxxKks4WLhINsZVhk/Srl1KFtxg1PSRp3MJuUttJZkQjpc6JIf0RDZwxtfH1SeozDB5agIT6aRAiuBUxzBlgUD5yCNPHzEog= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722198670; c=relaxed/simple; bh=XwREC3Zn27Mwzwot+SrxEwvb50Hp1zKL3+sNJmz6Mf8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CoICP4K0LbXQb6hhZMK5wFIUmFCcNkhrLFPKcAW11lHfmRmBWxvQl7AqRVJswdo00XTwoTF568Bx+XexgkIrZCljVXaJafq32I4u8NiSQk04/mPNysR0LJ4Ga3im5buML5IpKk86KOVmbs3KpWafFgFRzBKdwgcOfbHPX1wvM4o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--xur.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=GB13msEA; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--xur.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GB13msEA" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-64b70c4a269so42653077b3.1 for ; Sun, 28 Jul 2024 13:31:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722198667; x=1722803467; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=c7mCEB24MiuOLEIX0+MZEpfyFZDjGobAIp2IOnV2iDg=; b=GB13msEAQC9SelAKNqj0jhCnF/f7CXgWeNjXaCTJdr0HVqMc4IRDdnx2UvEpdMJqzH QuQhdd00u26CCHwQQXO1koV65Ri4xMT69l+gVS6BAMBhMfNBvrlixM/xuF+0V3C68yQT /9wUCm7oIlNcx9ho4c9M5QUJHmO2j0ColfLeYEK+/kvqvCIWaLII0+c7MeAtMDpGOqEq bQ2+eigJZdM6nsHhnibaDH7KpjxF5L1KuJz9++mJAu5TB+TYu+Eb/xSGUu98KKNEz/9/ zn/zHq0xkLMcdjJ2bB/tHeyZzaDC1WQ0KX3zsF3Dxm4aJNo/PbQjsKHXZTdxtK7TX+V1 JePg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722198667; x=1722803467; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=c7mCEB24MiuOLEIX0+MZEpfyFZDjGobAIp2IOnV2iDg=; b=aGMeEO2cW2N3CIowwwFqOzoI4yKhT9ru2xNqEVk5FN3v7AdUSnWwBLryEH61l9d8B+ 8RQYSn1FdU/S3piXCVjHPwUQsGSURU6D9YSrdvdTOe4NaluHkPpcxBhJ9U+wzfwWMEK5 4GR2O9G81M5enKJzKW7a7qEVyp5UJThWB74YlqmDKoqvxSpci0IKz/S44XmPmolQWyL7 qvcvSLMrkZHoGmeqwn58aeUMfPKKTRC5TUCpw6etf5OVBp6wTg7QiL60+zS6ThF2uGPk vj0ExulBzOyf2VWNXFPJaPzP7gMqyFr5wxOge2X09sos203oUKcy3Jtyl4xHP+SfiuWq spog== X-Forwarded-Encrypted: i=1; AJvYcCWJQtnHjEYdy4CgS9SFywpmW1OldJ+e6/Wh1GXTVf9tBVUK7bzTVem8f++7ukcvCxhy/KM4puSgq6m4uPSsVilwoFx3Wvb466Nt9wrs X-Gm-Message-State: AOJu0Yzx+MMc2xcFNy6q9Bt7uE/KVoVO66ZkZVVdMbB5Ds34qTxrArb/ lJhInUBdSWgYRjGTP9mIESiyAuNn5vTwiFPXjqU/nztDDhQYUK/uHyO+US8D4bnE9g== X-Google-Smtp-Source: AGHT+IGu4sbDUjEAKDB0hLyn3rxjDGmQnEZfdZL/ZgIho/aHfAuBV4gpPSxV5Bc3IDQzXkaJsNIU3DI= X-Received: from xur.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:2330]) (user=xur job=sendgmr) by 2002:a05:6902:1029:b0:e0b:9b5:8647 with SMTP id 3f1490d57ef6-e0b544ec4ddmr11322276.8.1722198666571; Sun, 28 Jul 2024 13:31:06 -0700 (PDT) Date: Sun, 28 Jul 2024 13:29:59 -0700 In-Reply-To: <20240728203001.2551083-1-xur@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240728203001.2551083-1-xur@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240728203001.2551083-7-xur@google.com> Subject: [PATCH 6/6] Add Propeller configuration for kernel build. From: Rong Xu To: Rong Xu , Han Shen , Sriraman Tallam , David Li , Jonathan Corbet , Masahiro Yamada , Nathan Chancellor , Nicolas Schier , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Ard Biesheuvel , Arnd Bergmann , Josh Poimboeuf , Peter Zijlstra , Nick Desaulniers , Bill Wendling , Justin Stitt , Vegard Nossum , John Moon , Andrew Morton , Heiko Carstens , Luis Chamberlain , Samuel Holland , Mike Rapoport , "Paul E . McKenney" , Rafael Aquini , Petr Pavlu , Eric DeVolder , Bjorn Helgaas , Randy Dunlap , Benjamin Segall , Breno Leitao , Wei Yang , Brian Gerst , Juergen Gross , Palmer Dabbelt , Alexandre Ghiti , Kees Cook , Sami Tolvanen , Xiao Wang , Jan Kiszka Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, llvm@lists.linux.dev, Krzysztof Pszeniczny , Stephane Eranian 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 submission is limited to x86 platforms that support PMU features like LBR on Intel machines and AMD Zen3 BRS. For Arm, we plan to send patches for SPE-based Propeller when AutoFDO for Arm is ready. 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=” 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 \ -o -- 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 -o -- 4) (Optional) Download the raw perf file to the HOST machine. 5) Generate Propeller profile: $ create_llvm_prof --binary= --profile= \ --format=propeller --propeller_output_module_name \ --out=_cc_profile.txt \ --propeller_symorder=_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). "" can be something like "/home/user/dir/any_string". This command generates a pair of Propeller profiles: "_cc_profile.txt" and "_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= \ CLANG_PROPELLER_PROFILE_PREFIX= Co-developed-by: Han Shen Signed-off-by: Han Shen Signed-off-by: Rong Xu Suggested-by: Sriraman Tallam Suggested-by: Krzysztof Pszeniczny Suggested-by: Nick Desaulniers Suggested-by: Stephane Eranian --- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/propeller.rst | 188 ++++++++++++++++++++++++++ MAINTAINERS | 7 + Makefile | 1 + arch/Kconfig | 22 +++ arch/x86/Kconfig | 1 + arch/x86/boot/compressed/Makefile | 3 + arch/x86/kernel/vmlinux.lds.S | 4 + arch/x86/platform/efi/Makefile | 1 + drivers/firmware/efi/libstub/Makefile | 2 + include/asm-generic/vmlinux.lds.h | 8 +- scripts/Makefile.lib | 10 ++ scripts/Makefile.propeller | 25 ++++ tools/objtool/check.c | 1 + 14 files changed, 270 insertions(+), 4 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 46636e4efe15..16e33eadb73b 100644 --- a/Documentation/dev-tools/index.rst +++ b/Documentation/dev-tools/index.rst @@ -33,6 +33,7 @@ Documentation/dev-tools/testing-overview.rst ktap checkuapi 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 000000000000..15ef0e6d973e --- /dev/null +++ b/Documentation/dev-tools/propeller.rst @@ -0,0 +1,188 @@ +.. 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: + + .. code-block:: make + + CONFIG_AUTOFDO_CLANG=y + CONFIG_PROPELLER_CLANG=y + +Customization +============= + +You can enable or disable Propeller build for individual file and +directories by adding a line similar to the following to the +respective kernel Makefile: + +- For enabling a single file (e.g. foo.o) + + .. code-block:: make + + PROPELLER_PROFILE_foo.o := y + +- For enabling all files in one directory + + .. code-block:: make + + PROPELLER_PROFILE := y + +- For disabling one file + + .. code-block:: make + + PROPELLER_PROFILE_foo.o := n + +- For disabling all files in one directory + + .. code-block:: make + + 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: + + .. code-block:: make + + CONFIG_AUTOFDO_CLANG=y + CONFIG_PROPELLER_CLANG=y + + and + + .. code-block:: sh + + $ make LLVM=1 CLANG_AUTOFDO_PROFILE= + +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: + + .. code-block:: sh + + $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c \ + -o -- + + - For AMD platforms: + + .. code-block:: sh + + $ perf record --pfm-event RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k \ + -a -N -b -c -o -- + + Note you can repeat the above steps to collect multiple 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. + + .. code-block:: sh + + $ create_llvm_prof --binary= --profile= \ + --format=propeller --propeller_output_module_name \ + --out=_cc_profile.txt \ + --propeller_symorder=_ld_profile.txt + + "" can be something like + "/home/user/dir/any_string". + + This command generates a pair of Propeller profiles: + "_cc_profile.txt" and + "_ld_profile.txt". + + If there are more than 1 perf_file collected in the previous step, + you can create a temp list file "" with each line + containing one perf file name and run: + + .. code-block:: sh + + $ create_llvm_prof --binary= --profile=@ \ + --format=propeller --propeller_output_module_name \ + --out=_cc_profile.txt \ + --propeller_symorder=_ld_profile.txt + +6) Rebuild the kernel using the AutoFDO and Propeller profiles. + + .. code-block:: make + + CONFIG_AUTOFDO_CLANG=y + CONFIG_PROPELLER_CLANG=y + + and + + .. code-block:: sh + + $ make LLVM=1 CLANG_AUTOFDO_PROFILE= CLANG_PROPELLER_PROFILE_PREFIX= diff --git a/MAINTAINERS b/MAINTAINERS index 8a89e7f0d9d5..0c7f3cebe4fe 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -17974,6 +17974,13 @@ S: Maintained F: include/linux/psi* F: kernel/sched/psi.c +PROPELLER BUILD +M: Rong Xu +M: Han Shen +S: Supported +F: Documentation/dev-tools/propeller.rst +F: scripts/Makefile.propeller + PRINTK M: Petr Mladek R: Steven Rostedt diff --git a/Makefile b/Makefile index 5ae30cc94a26..85a96d973f20 100644 --- a/Makefile +++ b/Makefile @@ -1025,6 +1025,7 @@ include-$(CONFIG_KCOV) += scripts/Makefile.kcov include-$(CONFIG_RANDSTRUCT) += scripts/Makefile.randstruct include-$(CONFIG_GCC_PLUGINS) += scripts/Makefile.gcc-plugins include-$(CONFIG_AUTOFDO_CLANG) += scripts/Makefile.autofdo +include-$(CONFIG_PROPELLER_CLANG) += scripts/Makefile.propeller include $(addprefix $(srctree)/, $(include-y)) diff --git a/arch/Kconfig b/arch/Kconfig index e12599c4ab63..5b136e904400 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -822,6 +822,28 @@ 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 AUTOFDO_CLANG + depends on CC_IS_CLANG && CLANG_VERSION >= 190000 + help + This option enables Clang’s Propeller build which + is on top of AutoFDO 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, Proepller 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 dca526b1364f..6fb5269d39b0 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -123,6 +123,7 @@ config X86 select ARCH_SUPPORTS_LTO_CLANG select ARCH_SUPPORTS_LTO_CLANG_THIN 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/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile index f2051644de94..35d19b4e6361 100644 --- a/arch/x86/boot/compressed/Makefile +++ b/arch/x86/boot/compressed/Makefile @@ -17,6 +17,9 @@ # (see scripts/Makefile.lib size_append) # compressed vmlinux.bin.all + u32 size of vmlinux.bin.all +# Do not run Propeller optimizer for early boot code. +PROPELLER_PROFILE := n + targets := vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma \ vmlinux.bin.xz vmlinux.bin.lzo vmlinux.bin.lz4 vmlinux.bin.zst diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S index 3509afc6a672..167dd05323cf 100644 --- a/arch/x86/kernel/vmlinux.lds.S +++ b/arch/x86/kernel/vmlinux.lds.S @@ -440,6 +440,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/arch/x86/platform/efi/Makefile b/arch/x86/platform/efi/Makefile index 543df9a1379d..e0c846b6d636 100644 --- a/arch/x86/platform/efi/Makefile +++ b/arch/x86/platform/efi/Makefile @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 KASAN_SANITIZE := n GCOV_PROFILE := n +PROPELLER_PROFILE := n obj-$(CONFIG_EFI) += memmap.o quirks.o efi.o efi_$(BITS).o \ efi_stub_$(BITS).o diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile index 06f0428a723c..55ca5250df1a 100644 --- a/drivers/firmware/efi/libstub/Makefile +++ b/drivers/firmware/efi/libstub/Makefile @@ -56,6 +56,8 @@ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_CFI), $(KBUILD_CFLAGS)) # disable LTO KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_LTO), $(KBUILD_CFLAGS)) +PROPELLER_PROFILE := n + lib-y := efi-stub-helper.o gop.o secureboot.o tpm.o \ file.o mem.o random.o randomalloc.o pci.o \ skip_spaces.o lib-cmdline.o lib-ctype.o \ diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h index 7d9dc8a3c046..ea3d8bf51edd 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 LTO_CLANG, the linker also splits sections by default, so we need - * these macros to combine the sections during the final link. + * CONFIG_AUTOFD_CLANG and CONFIG_PROPELLER_CLANG will also split text sections + * and cluster them in the linking time. * * 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 @@ -612,7 +612,7 @@ defined(CONFIG_AUTOFDO_CLANG) * first when in these builds. */ #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_TEXT \ *(.text.asan.* .text.tsan.*) \ *(.text.unknown .text.unknown.*) \ diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib index c2cab5adaf25..e239fa709c20 100644 --- a/scripts/Makefile.lib +++ b/scripts/Makefile.lib @@ -219,6 +219,16 @@ _c_flags += $(if $(patsubst n%,, \ $(CFLAGS_AUTOFDO_CLANG)) endif +# +# Enable Clang's Propeller build flags for a file or directory depending on +# variables AUTOFDO_PROPELLER_obj.o and PROPELLER_PROFILE. +# +ifeq ($(CONFIG_PROPELLER_CLANG),y) +_c_flags += $(if $(patsubst n%,, \ + $(AUTOFDO_PROFILE_$(basetarget).o)$(AUTOFDO_PROFILE)$(PGO_PROFILE)$(PROPELLER_PROFILE)y), \ + $(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 000000000000..0c9318be5f64 --- /dev/null +++ b/scripts/Makefile.propeller @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: GPL-2.0 + +# Enable available and selected Clang Propeller features. +# Propeller required debug information to embed module names in the profiles. +CFLAGS_PROPELLER_CLANG := -fdebug-info-for-profiling + +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 + +ifdef CONFIG_LTO_CLANG +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 +else +endif + +export CFLAGS_PROPELLER_CLANG diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 254913498c3c..7cea8ba53cf4 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -4489,6 +4489,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;