From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3F3F53C4B6F; Tue, 8 Sep 2026 20:55:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788900945; cv=none; b=uFsZYp40YuS2eB8cBPlaV5a14LOSvu1YAvvmuDHV5cs/POgdbBkfQTD67PcOXlipeaKYhIW6T5Ryc4LveJU1J/FtY8puHpCKv6ni2/HClE0FRlePppyG+CGE1++Lzb9K8SrbcNl9FKlyVuic0+exgdmMlIFh4IYxpJuH9fCL6SE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788900945; c=relaxed/simple; bh=QfW8/LMHKqPTf4R2pTPKjpcy4p10+jQ9pbl+SDhevC4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=SuSvD+pht///GAQ/0rJLepjfAjsTF/xcW0cGpgcuBHx/n+YLeo0uH1rXN4fq2ZNYaQEnZgofd2wUHddz4KDV7iRjPYdblJX79ZsnV9Dp6ymBJNAjlCHIxzOzsw0ol5XaJZ1q9iZhzN3hkd7N4XXTcDQGcDsmJplBUwtEqn0tJ4Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gcuaImta; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gcuaImta" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4539B1F00A3E; Tue, 8 Sep 2026 20:55:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788900944; bh=7lHsIzzOaaem51+m++n0/DPnuKeNqmOjNP5os70+l/E=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=gcuaImtaBdcvz4CtAgmXpjnv01updFVNTW9WrGABIcDEyojIPAOsJCbV00b87f+1y j731oQYOWTNlFnkqkt4tAP7FENM+Jea0i9yZwJaTmXdW4Jnebjt6KILmgBa79eY7Wb AYs0eoObe/OL3BO+4fv9qd775cOTxEm5cp7yekzIPAI6KSrFnuz9NBnVXpV9zj2V7f IxYOAu6NkUYoPI/Su0ZN1y6dBZDYb8K6R2cVZnwazlJ5a7h1EgyXBF2Q3hxPxb6sWD wDsVU9oltHTEIhNu+HRTNsz3Q5/Xmefqdlc2ZWGg0p6j/GsT1cQpBmQl1uwshl0ooA mWpYEDYCsfnPw== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:01 +0100 Subject: [PATCH 01/23] scripts/mksysmap: drop the MODULE_INFO() symbols from kallsyms Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-1-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=1471; i=ljs@kernel.org; h=from:subject:message-id; bh=QfW8/LMHKqPTf4R2pTPKjpcy4p10+jQ9pbl+SDhevC4=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlJk+2pIZFHpy309v2WveEjvYda2cs/aYH+TcLjFzs 8mmvJAJHaUsDGJcDLJiiizPv4jvDxIJm9d5wd8NZg4rE8gQBi5OAZhI9RZGhtbpq42+L52/y/bk hEeZ91+buM15GnnuZeJ1nbfC50OLN3xkZGh/cZ31aezHKXeSZ0/wqbOcIHrcomFdBee8DKOAmNU 3T3IDAA== X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 Commit 3e86e4d74c04 ("kbuild: keep .modinfo section in vmlinux.unstripped") keeps .modinfo symbols out of System.map and kallsyms, which assumes unique IDs have a format like '__UNIQUE_ID_modinfo123'. However, commit afb026b6d35c ("compiler: Tweak __UNIQUE_ID() naming"), sent in the same cycle, changes this to '__UNIQUE_ID_modinfo_123'. As a result this regexp has never matched and every kernel since v6.18 has carried one kallsyms entries for every MODULE_INFO() declaration in the kernel whether the modules are compiled or not. That's 5,810 entries for an x86 defconfig build and 15,200 for arm64. On x86 defconfig that is 113 KiB of kallsyms tables and 32 KiB of bzImage, and every lookup walks past them. Fix the pattern. Fixes: 3e86e4d74c04 ("kbuild: keep .modinfo section in vmlinux.unstripped") No measurable change in build time, the smaller tables are not on any path the build waits for. Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) Reviewed-by: Nathan Chancellor Reviewed-by: Nicolas Schier --- scripts/mksysmap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/mksysmap b/scripts/mksysmap index c4531eacde20..56a8b8bbdb37 100755 --- a/scripts/mksysmap +++ b/scripts/mksysmap @@ -83,7 +83,7 @@ / _SDA2_BASE_$/d =20 # MODULE_INFO() -/ __UNIQUE_ID_modinfo[0-9]*$/d +/ __UNIQUE_ID_modinfo_[0-9]*$/d =20 # ------------------------------------------------------------------------= --- # Ignored patterns --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB1F33C2796; Tue, 8 Sep 2026 20:55:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788900956; cv=none; b=N0NAnvEZUFBbDv60s+dJbRu7L7E88xZllHbOnZFN0jWsdLsLoQEuch6lceVagj73XD38AhdqJi01Ior8dR4AqDC7dBoAPDXiwSEStPLS4XKaiX/pMwuxhM+YYVokBU5kgoaI1F5NbHWfSy0oeDIUFIrgIJhDFd/G1CyPN1gj7bM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788900956; c=relaxed/simple; bh=pcsijpekNt+DxklwcjLik4aRG5CCfOXcviThDorMTJE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lX0Qpb80WnL143UwfEBNIgRqvPGT4OIv5sZLh1FdWk8EXbn1S1x/uoGk+dyznGddHHjNMqA31KCeW+f/lYHMkS/aZEXTPDojW0iywYivgJ9KdOUAraKaU1W4Vetvfd3LvrelbAUVjsNnjzdSCk3j/mgt/tu06/DvanediM26EKk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QyylYyUh; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QyylYyUh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 769721F00A3A; Tue, 8 Sep 2026 20:55:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788900955; bh=Y87Ru0xfYAGxRwASt8W2uZE0vluulQEq4vWCkr2qehk=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=QyylYyUh712yA+wXv0MkQw/+SKQgxmL401+ubHQJwu+ZIzBE8vWqhj+V4R6TMaiHM Y60hJq5rQ3/d9d5CNityd7xig6O+4487SsyyzAbI5j9JWEqg1fBPN4mW7Y0xf3E57W oEGnEeBaKJnD4+eeJylQ06J8g6mUkNnGrMBWzpyf+bPrZStFN+yY63wBlvPS6YeupJ SmNBcrI1XpyFQx9QaXKXWXDETpxFWmsyd/pCLcfeZuXv9msSqXrxIyTaDgz7ecm0jm QsJMlDs7cKdvveUp91Za2I0LitMt5Y1sCLbcdrH1DKRVcK09/RvD8ADXffWwOfymjH IyU9PE5QiiKvw== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:02 +0100 Subject: [PATCH 02/23] scripts/mksysmap: fix escape of '$' in the __pi_ pattern Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-2-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=1197; i=ljs@kernel.org; h=from:subject:message-id; bh=pcsijpekNt+DxklwcjLik4aRG5CCfOXcviThDorMTJE=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlJker97TJDyrb4PQ+6XPvAN8LNze3ZELTohu0v29J yLUj39BRykLgxgXg6yYIsvzL+L7g0TC5nVe8HeDmcPKBDKEgYtTACZy/i0jw08mn4fvXq4u+Fa8 i/Fq9u+vctwXl2RL3vaT5v0085vVum2MDK9+Kl4Xyq24lPJZ8b3/vJj9veGOjkp52yRuKrG/+/v 8BhsA X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 Commit b18b047002b7 ("kbuild: change scripts/mksysmap into sed script") converted scripts/mksysmap from a shell script to a sed script. However an error was made - escaping of '$' required \\ escaping in shell but only \ in a sed script. This was mostly corrected in commit 7a6c355b55c0 ("scripts/mksysmap: Fix escape chars '$'"), but this fix missed arm64 PIE namespace local symbols like __pi_$x and __pi_$d which appear in System.mapand /proc/kallsyms: $ grep __pi_\\$ /proc/kallsyms | sort -u 0000000000000000 d __pi_$d 0000000000000000 t __pi_$x Fix the escaping properly. Fixes: b18b047002b7 ("kbuild: change scripts/mksysmap into sed script") No measurable change in build time. Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) Reviewed-by: Nicolas Schier --- scripts/mksysmap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/mksysmap b/scripts/mksysmap index 56a8b8bbdb37..856b26ba2ac0 100755 --- a/scripts/mksysmap +++ b/scripts/mksysmap @@ -35,7 +35,7 @@ / __efistub_/d =20 # arm64 local symbols in PIE namespace -/ __pi_\\$/d +/ __pi_\$/d / __pi_\.L/d =20 # arm64 local symbols in non-VHE KVM namespace --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 12B053C342F; Tue, 8 Sep 2026 20:56:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788900968; cv=none; b=sdg90u7qyh8270i4hAEIaSqNkgUrbHO+kiaTd0e+o9Ynq7BM8PrZpc0xq+AuthN9NMgL1dDnBDNbakpfcISxNGzztcsngZr7GH/hcwecPVJBJDt88GNjjDLubnw55LPoleG8KRhjz7uve5hkGNHBKhis9oPONQVAz42j9LIhjpI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788900968; c=relaxed/simple; bh=AdYautbMzzj34OfFQGbA/YGqCu4yxRtSGWlFBnNeFJ0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=P8byO1qr+yCea8JEg9+6ZbtKapjw3afksZZh9+huSLqbtFRq/uXg8jdFwpFI6nAVfQpv2g0tAZVwZjbm9uPHTEaCa2146qMUUcm964RXBAMt7MXXmLR0+h3gXtWEVUs5A2r7F9cXix6Es/rj+3Cqu0IdB8LlH62C7axPUDNydrA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=InM9gDQi; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="InM9gDQi" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AC0FF1F00A3E; Tue, 8 Sep 2026 20:55:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788900966; bh=JQp99GofxJEXpnCPDFs13IBgEN0wkowoaUfRuslLHtk=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=InM9gDQiTbJiLhrjldb108eNIcHHYnfr54iP2707in33IvvcvzT32e13zxqAfc3vr c/o8qzoDOEzC2sUY4L9m7p9Iy0a+N/yxAIcKGyVzKsbNldffmvBzzGDz/4Fv2mmdAP dmTNcpxtGuNSZ+WHrNagdNsgqbpS+UAtloxY/lO9F3aHGVJIX6D/lU2dFb8TWFQmBc VUO6c2HW4PzfZqTZ0J4aT1ohM3QN/F772Wx4FJNZtfzLBvVsmLLVIRX8vJg7VaYkqE j9pCh07MAqWmCNAEccDkQzy7pJe8U6JgGmn5rxH9mQsPmugr0ls99c46FCyad+3Gnv t7nr7kK3XOUxQ== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:03 +0100 Subject: [PATCH 03/23] kallsyms: index symbols by token to speed up table compression Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-3-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=10123; i=ljs@kernel.org; h=from:subject:message-id; bh=AdYautbMzzj34OfFQGbA/YGqCu4yxRtSGWlFBnNeFJ0=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlJmeP3TQbr+Iw8cGp9lcErX3nfS2PosobhJOVbf8u aK7cfbXjlIWBjEuBlkxRZbnX8T3B4mEzeu84O8GM4eVCWQIAxenAEzEzYLhD/+kEss0/X8dQv6s GT+9WoQ+7mi2s3i9seg57+WQuPSgyYwMPxYLljNoLMnxWCBgp/CQv/nNmUsJX23TDJxzztX3+J/ mAgA= X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 The kallsyms program compresses symbols by figuring out the most commonly used substrings in all of the input symbols then uses special character codes to represent them. For instance, 0xf7 might end up representing "write_", then every single symbol that contains "write_" can use 0xf7 as a shorthand and save 5 bytes each time. 'Special' character codes are any byte value that is not used in any symbol, either due to being an invalid character, or not being present in any symbol (e.g. if no symbol contains 'z', then 'z' can be used as special character). It does this by first figuring out which special characters are available in insert_real_symbols_in_table(), then iterating through every available special character, counting how many times each pair of adjacent characters appear in symbols in build_initial_token_table(). These adjacent pairs are known as 'tokens'. Token counts are initially obtained by build_initial_token_table(), then optimize_result() calls find_best_token() to determine the token that appeared the most number of times and assigns it the next special character. Finally, optimize_result() calls compress_symbols() to replace every token in every symbol with its special character, which updates token_profit[] as it does so. This process is repeated for each remaining available special character, with tokens now perhaps containing previously assigned special characters (e.g. if 'wr' was assigned 0x80, then the token representing 'wri' would be '\x80i'). This compresses that token by 50% in each symbol it appears in (two bytes are now represented by one) and thus by repeatedly doing this kallsyms obtains good symbol compression. However, compress_symbols() is seriously inefficient - it iterates through EVERY symbol for EVERY special character assignment, i.e. ~256 * nr_symbols. Modern x86-64 kernels, for instance, have ~158,000 symbols, so millions of iterations are performed, most of which are entirely unnecessary (tokens don't appear in most symbols). In practice kallsyms spends half its runtime doing this, two or three times per vmlinux link step. Fix this by tracking which symbols each token appears in token_syms[], and only compress symbols which actually need to be updated. Each time a token is compressed that token can no longer appear in any symbol, so that token_syms[] entry can be freed. However new token_syms[] entries must be created for each new token containing the assigned special character, but this is bounded by the number of replacements in the symbol which is very small. In testing on an x86-64 platform using clang, each kallsyms invocation dropped from 0.59s to 0.33s with CONFIG_KALLSYMS_ALL set and from 0.38s to 0.22s without it set. The data was carefully checked and verified to be byte-for-byte identical for six symbol sets (two vmlinux passes, vmlinux.o, three userspace binaries) with all option combinations. As part of this change, additionally refactor the code to be a little easier to follow. kallsyms runs two or three times on the serial tail of every build that links vmlinux, no-op builds do not link and are unchanged. Whole build, 128-thread Threadripper 9980X, best of N runs: before after delta ------------------------------- x86 defconfig, touch mm/vma.c, gcc 11.4s 10.8s -0.55s (-5%) x86 defconfig, touch mm/vma.c, clang 11.4s 10.7s -0.66s (-6%) x86 defconfig, clean, gcc 30.3s 29.5s -0.80s (-3%) x86 defconfig, clean, clang 30.3s 29.7s -0.62s (-2%) x86 allmodconfig, touch mm/vma.c, gcc 46.2s 45.3s -0.91s (-2%) x86 allmodconfig, touch mm/vma.c, clang 44.2s 42.9s -1.3s (-3%) Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- scripts/kallsyms.c | 138 +++++++++++++++++++++++++++++++++++++++++++++++--= ---- 1 file changed, 124 insertions(+), 14 deletions(-) diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 494852ade6d8..350d118c3b9e 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -58,12 +58,47 @@ static unsigned int table_size, table_cnt; static int all_symbols; static int pc_relative; =20 +/* A dynamic array of symbols, encoded by symbol index. */ +struct sym_arr { + unsigned int *sym_indexes; + unsigned int cnt, cap; +}; + static int token_profit[0x10000]; +static struct sym_arr token_syms[0x10000]; =20 /* the table that holds the result of the compression */ static unsigned char best_table[256][2]; static unsigned char best_table_len[256]; =20 +static unsigned int sym_arr_last(const struct sym_arr *arr) +{ + return arr->cnt ? arr->sym_indexes[arr->cnt - 1] : UINT_MAX; +} + +static void sym_arr_maybe_expand(struct sym_arr *arr) +{ + if (arr->cap > arr->cnt) + return; + + arr->cap =3D arr->cap ? arr->cap * 2 : 16; + arr->sym_indexes =3D xrealloc(arr->sym_indexes, + arr->cap * sizeof(*arr->sym_indexes)); +} + +static void sym_arr_add(struct sym_arr *arr, unsigned int sym_idx) +{ + sym_arr_maybe_expand(arr); + arr->sym_indexes[arr->cnt++] =3D sym_idx; +} + +static void sym_arr_free(struct sym_arr *arr) +{ + free(arr->sym_indexes); + arr->sym_indexes =3D NULL; + arr->cnt =3D 0; + arr->cap =3D 0; +} =20 static void usage(void) { @@ -458,6 +493,15 @@ static void write_src(void) printf("\n"); } =20 +static unsigned int token_index(unsigned char first, unsigned char second) +{ + return first + (second << 8); +} + +static unsigned int sym_token_index(const unsigned char *symbol, int first= _idx) +{ + return token_index(symbol[first_idx], symbol[first_idx + 1]); +} =20 /* table lookup compression functions */ =20 @@ -467,7 +511,7 @@ static void learn_symbol(const unsigned char *symbol, i= nt len) int i; =20 for (i =3D 0; i < len - 1; i++) - token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++; + token_profit[sym_token_index(symbol, i)]++; } =20 /* decrease the count for all the possible tokens in a symbol */ @@ -476,16 +520,76 @@ static void forget_symbol(const unsigned char *symbol= , int len) int i; =20 for (i =3D 0; i < len - 1; i++) - token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--; + token_profit[sym_token_index(symbol, i)]--; +} + +static void token_add_symbol(unsigned int token_idx, unsigned int sym_idx) +{ + struct sym_arr *arr =3D &token_syms[token_idx]; + + /* Symbol indexes kept in sorted order, check for duplicate. */ + if (sym_arr_last(arr) =3D=3D sym_idx) + return; + + sym_arr_add(arr, sym_idx); +} + +static void symbol_index_all_tokens(const unsigned char *symbol, int len, + unsigned int sym_idx) +{ + int i; + + for (i =3D 0; i < len - 1; i++) { + const unsigned int token_idx =3D sym_token_index(symbol, i); + + token_add_symbol(token_idx, sym_idx); + } +} + +/* + * The symbol just got compressed. The only parts of the symbol that chang= ed + * meaningfully are those containing the newly assigned compressed char, so + * index those. + */ +static void symbol_index_new_tokens(const unsigned char *symbol, int len, + unsigned int sym_idx, int compressed_chr) +{ + int i; + + for (i =3D 0; i < len - 1; i++) { + const unsigned int token_idx =3D sym_token_index(symbol, i); + + if (symbol[i] =3D=3D compressed_chr || + symbol[i + 1] =3D=3D compressed_chr) + token_add_symbol(token_idx, sym_idx); + } } =20 -/* do the initial token count */ static void build_initial_token_table(void) { unsigned int i; =20 for (i =3D 0; i < table_cnt; i++) learn_symbol(table[i]->sym, table[i]->len); + + /* + * The initial occurrence counts tell us exactly how much memory should + * be reserved for each token's symbol array. + */ + for (i =3D 0; i < ARRAY_SIZE(token_syms); i++) { + const int nr_syms =3D token_profit[i]; + + if (!nr_syms) + continue; + + token_syms[i].cap =3D nr_syms; + token_syms[i].sym_indexes =3D + xmalloc(nr_syms * sizeof(unsigned int)); + } + + /* For every symbol, index every token -> symbol it is present in. */ + for (i =3D 0; i < table_cnt; i++) + symbol_index_all_tokens(table[i]->sym, table[i]->len, i); } =20 static unsigned char *find_token(unsigned char *str, int len, @@ -502,27 +606,30 @@ static unsigned char *find_token(unsigned char *str, = int len, =20 /* replace a given token in all the valid symbols. Use the sampled symbols * to update the counts */ -static void compress_symbols(const unsigned char *str, int idx) +static void compress_symbols(const unsigned char *str, int compressed_chr) { - unsigned int i, len, size; + const unsigned int token_idx =3D sym_token_index(str, 0); + struct sym_arr *arr =3D &token_syms[token_idx]; + unsigned int sym_idx, j, len, size; unsigned char *p1, *p2; =20 - for (i =3D 0; i < table_cnt; i++) { + /* Iterate through all symbols this token is found in and compress. */ + for (j =3D 0; j < arr->cnt; j++) { + sym_idx =3D arr->sym_indexes[j]; =20 - len =3D table[i]->len; - p1 =3D table[i]->sym; + len =3D table[sym_idx]->len; + p1 =3D table[sym_idx]->sym; =20 - /* find the token on the symbol */ p2 =3D find_token(p1, len, str); if (!p2) continue; =20 /* decrease the counts for this symbol's tokens */ - forget_symbol(table[i]->sym, len); + forget_symbol(table[sym_idx]->sym, len); =20 size =3D len; =20 do { - *p2 =3D idx; + *p2 =3D compressed_chr; p2++; size -=3D (p2 - p1); memmove(p2, p2 + 1, size); @@ -536,11 +643,14 @@ static void compress_symbols(const unsigned char *str= , int idx) =20 } while (p2); =20 - table[i]->len =3D len; + table[sym_idx]->len =3D len; =20 - /* increase the counts for this symbol's new tokens */ - learn_symbol(table[i]->sym, len); + learn_symbol(table[sym_idx]->sym, len); + symbol_index_new_tokens(table[sym_idx]->sym, len, sym_idx, + compressed_chr); } + + sym_arr_free(arr); /* No symbol contains this token any more. */ } =20 /* search the token with the maximum profit */ --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2C4E73D3326; Tue, 8 Sep 2026 20:56:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788900979; cv=none; b=kBcMJzsogYMkhbJHDjKF1gBsthzjNkFhRHHJtPh6zqPsTeRrB+uqw2ZYcW6iW7MZ5Dfxdm6wSIs4NffAQoKN3obHQuKxg+7yN2dWa20zW0Q/NhHtsaOn0L6kp5sL/XqTZ0pipZv0HkbokDAzF6QHdXELP67i1VC9galBtcraxmY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788900979; c=relaxed/simple; bh=qlI+vtmJCyqlOtepyI2T6wkgajjnp3VDGZ2zqEVGU5k=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gQUCPdusRvFGnlxwBLjf1a0JHrFQlCMZBdjUS4VASEexKKb5BQcUjrUigi4dfx3rTOB7itKtIMC+ZIgPZoY3Kq29B24qAK1IXIDZb1avNTodmMQk2l1MD+Kz3EQYlQbHuR3JKs4vbyaaiR1w/KAK1kutOIbpapM3dyKBZPvv8Os= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RO4VT5Sp; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="RO4VT5Sp" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E9CD41F00A3A; Tue, 8 Sep 2026 20:56:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788900977; bh=gzOTfxSGylhKOntm1gQtYgz8iaN6ExlVVoWePFdIyaU=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=RO4VT5Sp7Sz2IeKw7Z0XAlH8xzntgrKZT4ZGArJJgdkmJLC0D0eEwYzcerI1gi8Le TMxew8PSqEod5Aq3Xuv1s5niu3ckEBaUAn2yyPnxjS1Mre7LJrI+BbVWtF90RXxAbv weU8CgHS8yRwbnEo+I70jMfPawtjOhY//TZKsCX51vXsUEWRSu+90aBsU5JRwZU/Np LcAYdzco6wAekH4dLPr9A8N1dcnJcQBLgVzbO36zYKxUC9GwNoPKMRzB74Ovs9J6US g15V0rkMUXDjFqL0MWJ/5cdi/Y2rPKXi/w+j0klBK5BowkRuVtBJZgjAD+519B7wAQ +jOMr51D1K5fA== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:04 +0100 Subject: [PATCH 04/23] kallsyms: output binary data to speed output and kallsyms assembly Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-4-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=8922; i=ljs@kernel.org; h=from:subject:message-id; bh=qlI+vtmJCyqlOtepyI2T6wkgajjnp3VDGZ2zqEVGU5k=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlJlNFlBsyjzodtpulmNMHNexqB1iaUXs5dt79u58U WwkzL+2o5SFQYyLQVZMkeX5F/H9QSJh8zov+LvBzGFlAhnCwMUpABO5p8zIsCFw8coXKtv3NYc3 s11NFZzzaJ6S5jS7jqMBulqiOe112gz/s9Qy7pRdm7vcuFBe81zVOiXbWewcNdxcPGWP8pl5iia yAAA= X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 kallsyms generates an assembly file that consists mostly of .byte entries containing compressed names, token strings and name-sorted sequence numbers. For an x86-64 build with 158k symbols that is a 37 MiB .S file which takes 0.57s to assemble each of the two to three times it is built over a kernel build. Each time it is generated it also takes kallsyms a similar amount of time to output it. Avoid this overhead by instead outputting this data as binary and importing it into the assembly using the .incbin directive. Tables that are wider than a byte remain part of the assembly to ensure endianness and relative relocations are performed correctly. With this change, the output assembly file shrinks from 37 MiB to 9.8 MiB, with a 2.6 MiB binary data file alongside it, and the object remains identical. The generated binary file is deleted correctly on build clean along with all other ephemeral data. On an x86-64 system with CONFIG_KALLSYMS_ALL set: before after delta scripts/kallsyms 0.24s 0.18s 0.06s assemble 0.57s 0.16s 0.41s Per kallsyms invocation/assembly, for a total of 0.47s time saving upon invocation. An incremental build on the same system was reduced from 11.15s to 9.65s, indicating a total of 1.5 seconds saved over the build. The kallsyms runs and their assembly are on the serial tail of every build that links vmlinux, no-op builds are unchanged. Whole build, 128-thread Threadripper 9980X, best of N runs: before after delta ------------------------------- x86 defconfig, touch mm/vma.c, gcc 10.8s 9.9s -0.92s (-8%) x86 defconfig, touch mm/vma.c, clang 10.7s 9.5s -1.2s (-11%) x86 defconfig, clean, gcc 29.5s 28.7s -0.81s (-3%) x86 defconfig, clean, clang 29.7s 28.6s -1.1s (-4%) x86 allmodconfig, touch mm/vma.c, gcc 45.3s 44.0s -1.3s (-3%) x86 allmodconfig, touch mm/vma.c, clang 42.9s 40.2s -2.7s (-6%) Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- scripts/kallsyms.c | 97 ++++++++++++++++++++++++++++++++++++++-------= ---- scripts/link-vmlinux.sh | 2 +- 2 files changed, 77 insertions(+), 22 deletions(-) diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 350d118c3b9e..61c5eb537ed4 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -5,7 +5,10 @@ * This software may be used and distributed according to the terms * of the GNU General Public License, incorporated herein by reference. * - * Usage: kallsyms [--all-symbols] in.map > out.S + * Usage: kallsyms [--all-symbols] [--pc-relative] in.map out.bin > out.S + * + * The byte tables go to out.bin and are pulled into out.S with .incb= in; + * wider tables stay assembler source for endianness and relocations. * * Table compression uses all the unused char codes on the symbols and * maps these to the most used substrings (tokens). For instance, it might @@ -102,7 +105,7 @@ static void sym_arr_free(struct sym_arr *arr) =20 static void usage(void) { - fprintf(stderr, "Usage: kallsyms [--all-symbols] in.map > out.S\n"); + fprintf(stderr, "Usage: kallsyms [--all-symbols] [--pc-relative] in.map o= ut.bin > out.S\n"); exit(1); } =20 @@ -319,6 +322,40 @@ static void output_label(const char *label) printf("%s:\n", label); } =20 +static void write_bin(FILE *file, const void *data, size_t len) +{ + if (fwrite(data, 1, len, file) =3D=3D len) + return; + + perror("kallsyms: write"); + exit(EXIT_FAILURE); +} + +static void write_byte_bin(FILE *file, unsigned char byte) +{ + write_bin(file, &byte, 1); +} + +static long bin_pos(FILE *file) +{ + const long pos =3D ftell(file); + + if (pos < 0) { + perror("kallsyms: ftell"); + exit(EXIT_FAILURE); + } + + return pos; +} + +static void write_incbin(const char *filename, long start, long end) +{ + if (start >=3D end) + return; + + printf("\t.incbin \"%s\", %ld, %ld\n", filename, start, end - start); +} + /* uncompress a compressed symbol. When this function is called, the best = table * might still be compressed itself, so the function needs to be recursive= */ static int expand_symbol(const unsigned char *data, int len, char *result) @@ -371,11 +408,12 @@ static void sort_symbols_by_name(void) qsort(table, table_cnt, sizeof(table[0]), compare_names); } =20 -static void write_src(void) +static void write_src(FILE *out_bin_file, const char *out_bin_name) { - unsigned int i, k, off; + unsigned int i, off; unsigned int best_idx[256]; unsigned int *markers, markers_cnt; + long bin_start; char buf[KSYM_NAME_LEN]; =20 printf("\t.section .rodata, \"a\"\n"); @@ -390,6 +428,7 @@ static void write_src(void) markers =3D xmalloc(sizeof(*markers) * markers_cnt); =20 output_label("kallsyms_names"); + bin_start =3D bin_pos(out_bin_file); off =3D 0; for (i =3D 0; i < table_cnt; i++) { if ((i & 0xFF) =3D=3D 0) @@ -413,26 +452,24 @@ static void write_src(void) /* Encode length with ULEB128. */ if (table[i]->len <=3D 0x7F) { /* Most symbols use a single byte for the length. */ - printf("\t.byte 0x%02x", table[i]->len); + write_byte_bin(out_bin_file, table[i]->len); off +=3D table[i]->len + 1; } else { /* "Big" symbols use two bytes. */ - printf("\t.byte 0x%02x, 0x%02x", - (table[i]->len & 0x7F) | 0x80, - (table[i]->len >> 7) & 0x7F); + write_byte_bin(out_bin_file, (table[i]->len & 0x7F) | 0x80); + write_byte_bin(out_bin_file, (table[i]->len >> 7) & 0x7F); off +=3D table[i]->len + 2; } - for (k =3D 0; k < table[i]->len; k++) - printf(", 0x%02x", table[i]->sym[k]); + write_bin(out_bin_file, table[i]->sym, table[i]->len); =20 /* * Now that we wrote out the compressed symbol name, restore the - * original name and print it in the comment. + * original name for the comments below. */ expand_symbol(table[i]->sym, table[i]->len, buf); strcpy((char *)table[i]->sym, buf); - printf("\t/* %s */\n", table[i]->sym); } + write_incbin(out_bin_name, bin_start, bin_pos(out_bin_file)); printf(".size kallsyms_names, . - kallsyms_names\n"); printf("\n"); =20 @@ -445,13 +482,15 @@ static void write_src(void) free(markers); =20 output_label("kallsyms_token_table"); + bin_start =3D bin_pos(out_bin_file); off =3D 0; for (i =3D 0; i < 256; i++) { best_idx[i] =3D off; expand_symbol(best_table[i], best_table_len[i], buf); - printf("\t.asciz\t\"%s\"\n", buf); + write_bin(out_bin_file, buf, strlen(buf) + 1); off +=3D strlen(buf) + 1; } + write_incbin(out_bin_name, bin_start, bin_pos(out_bin_file)); printf(".size kallsyms_token_table, . - kallsyms_token_table\n"); printf("\n"); =20 @@ -484,12 +523,13 @@ static void write_src(void) =20 sort_symbols_by_name(); output_label("kallsyms_seqs_of_names"); - for (i =3D 0; i < table_cnt; i++) - printf("\t.byte 0x%02x, 0x%02x, 0x%02x\t/* %s */\n", - (unsigned char)(table[i]->seq >> 16), - (unsigned char)(table[i]->seq >> 8), - (unsigned char)(table[i]->seq >> 0), - table[i]->sym); + bin_start =3D bin_pos(out_bin_file); + for (i =3D 0; i < table_cnt; i++) { + write_byte_bin(out_bin_file, table[i]->seq >> 16); + write_byte_bin(out_bin_file, table[i]->seq >> 8); + write_byte_bin(out_bin_file, table[i]->seq >> 0); + } + write_incbin(out_bin_name, bin_start, bin_pos(out_bin_file)); printf("\n"); } =20 @@ -798,6 +838,9 @@ static void sort_symbols(void) =20 int main(int argc, char **argv) { + const char *out_bin_name; + FILE *out_bin_file; + while (1) { static const struct option long_options[] =3D { {"all-symbols", no_argument, &all_symbols, 1}, @@ -813,14 +856,26 @@ int main(int argc, char **argv) usage(); } =20 - if (optind >=3D argc) + if (optind + 2 !=3D argc) usage(); =20 + out_bin_name =3D argv[optind + 1]; + out_bin_file =3D fopen(out_bin_name, "w"); + if (!out_bin_file) { + perror(out_bin_name); + exit(EXIT_FAILURE); + } + read_map(argv[optind]); shrink_table(); sort_symbols(); optimize_token_table(); - write_src(); + write_src(out_bin_file, out_bin_name); + + if (fclose(out_bin_file)) { + perror(out_bin_name); + exit(EXIT_FAILURE); + } =20 return 0; } diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index ab0b8125c8cb..e88604150d2c 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -132,7 +132,7 @@ kallsyms() fi =20 info KSYMS "${2}.S" - scripts/kallsyms ${kallsymopt} "${1}" > "${2}.S" + scripts/kallsyms ${kallsymopt} "${1}" "${2}.bin" > "${2}.S" =20 info AS "${2}.o" ${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \ --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 66DF13D566D; Tue, 8 Sep 2026 20:56:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788900990; cv=none; b=BGWFBu5p4Wr+TsAiDDwJBNvGDGS+3Nez81ueZtBZ+R46q/7uKhr0Ow1PEWC+CHxXrvjMbuSNhAuosMrh4ev+amNHlTS2F2CS8xUZRYzTvUxyMDZ6xDLUUySnJiBZISjrQc/CsfKAd/xh25cVAbyCX7yS+K6HrJyZjdxbVQF1jCQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788900990; c=relaxed/simple; bh=+YC6B/ny9rqyw1zvOOCE6GV0YlVsnVMYtVTlqw1TgyE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ncaHqZ1ZW1MN5HtM5gfa5KXJxoXVuG8eNQWllucBKQholRT+YtQcei311XL/qNDo+TxYXvejiGjEl3EyW0G0jqosM9gK9GfkwAb5ew+r/dzbMYIgiKI9YreEnbMB408whmvj9E6g+w82E6GgoL6RAI58LsiRky03TffpB2GNrQU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QbIBrSnu; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QbIBrSnu" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 38DC21F00A3D; Tue, 8 Sep 2026 20:56:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788900988; bh=/F3fN1xidqNujETTJgCNbEUYtVAv2rcdbViMVFAjqF8=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=QbIBrSnubamD1DbmvIXjDEyo81QHoyZ2CWFoSEqQeQqTMwlbFdics9Bll1CNoyehT YoEk42otBss+KLYrsPYs3HnWXCsWvZgvlytLNhF5Jm+TL4if12YrFWQKfPTgvcLSss 01FMbd8/qexU3c95zGCgphn76T6oR1Q3PYzyyETtzZP2hQL4oh1u2h79oO1Ff+lYW3 lknXUtGE45dcDhHeYr2jlTsP1VpKj5SlNRoYLXhFiV6g4985H2qVf7RTQz9yqXeY21 ti6fWS5XhxTzKGYtDEAzx4NJyYbcitHuv6LENci2AVbOZUzeATV87B+FF5PXEBHzfP glm1hCCM3KAuQ== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:05 +0100 Subject: [PATCH 05/23] kbuild: do not sort nm output where the order is irrelevant Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-5-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=4987; i=ljs@kernel.org; h=from:subject:message-id; bh=+YC6B/ny9rqyw1zvOOCE6GV0YlVsnVMYtVTlqw1TgyE=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlJl9mhmzI3Z3aqti/UHmCSJnH7WrT44+vmGJqe77a tsbX902dZSyMIhxMciKKbI8/yK+P0gkbF7nBX83mDmsTCBDGLg4BWAinw4w/OGYFXHTXaX9sWCb +LpA+/17ZHUbCoIEL/etmNaSfTk6dTYjQ8dBjiZRdf7gn3E7/dL8Xs6/M3XbNDaTAwdTL77cMl1 xKjMA X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 Unless instructed otherwise, nm sorts by name. There are places where this is unnecessary - an invocation from scripts/sorttable every vmlinux link, scripts/check-function-names.sh run after every vmlinux.o link, and the x86 VOFFSET and ZOFFSET listings between vmlinux and bzImage. Both GNU nm and llvm-nm accept the same '-p' parameter to disable sorting in these instances, so use that to prevent this unnecessary work. Each nm run on its own, x86-64, median of 5: GNU nm 2.47 llvm-nm 22 before after delta before after delta defconfig sorttable, nm -S vmlinux 0.080s 0.039s -0.041s 0.265s 0.113s -0.152s check-function-names.sh 0.072s 0.033s -0.039s 0.244s 0.102s -0.142s VOFFSET 0.077s 0.034s -0.043s 0.261s 0.113s -0.148s ZOFFSET 0.007s 0.005s -0.002s 0.005s 0.005s 0.000s TOTAL -0.125s -0.442s allmodconfig sorttable, nm -S vmlinux 0.156s 0.063s -0.093s 0.539s 0.223s -0.316s check-function-names.sh 0.149s 0.054s -0.095s 0.537s 0.217s -0.320s VOFFSET 0.146s 0.052s -0.094s 0.524s 0.221s -0.303s ZOFFSET 0.007s 0.005s -0.002s 0.006s 0.004s -0.002s TOTAL -0.284s -0.941s llvm-nm appears to be a lot slower than GNU nm, so these builds naturally improve the most. The four run one after another in the serial tail of every build that links vmlinux so impact kernel builds directly. In an allmodconfig the decompressor is built in parallel while the modules, so only the two before the vmlinux link contribute to build time. The build outputs remain unchanged. Whole build, 128-thread Threadripper 9980X, median of 3: before after delta ---------------------------------- x86 defconfig, touch mm/vma.c, gcc 10.02s 9.83s -0.19s (-2%) x86 defconfig, touch mm/vma.c, clang 9.52s 9.24s -0.28s (-3%) x86 defconfig, clean, gcc 28.71s 28.58s -0.13s (-0.5%) x86 defconfig, clean, clang 28.69s 28.31s -0.38s (-1%) x86 allmodconfig, touch mm/vma.c, cl 40.52s 39.83s -0.69s (-2%) Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- arch/x86/boot/Makefile | 2 +- arch/x86/boot/compressed/Makefile | 2 +- scripts/check-function-names.sh | 3 ++- scripts/link-vmlinux.sh | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/arch/x86/boot/Makefile b/arch/x86/boot/Makefile index 3f9fb3698d66..4a9bce32586d 100644 --- a/arch/x86/boot/Makefile +++ b/arch/x86/boot/Makefile @@ -74,7 +74,7 @@ SETUP_OBJS =3D $(addprefix $(obj)/,$(setup-y)) sed-zoffset :=3D -e 's/^\([0-9a-fA-F]*\) [a-zA-Z] \(startup_32\|efi.._stub= _entry\|efi\(32\)\?_pe_entry\|input_data\|kernel_info\|_end\|_ehead\|_text\= |_e\?data\|_e\?sbat\|z_.*\)$$/\#define ZO_\2 0x\1/p' =20 quiet_cmd_zoffset =3D ZOFFSET $@ - cmd_zoffset =3D $(NM) $< | sed -n $(sed-zoffset) > $@ + cmd_zoffset =3D $(NM) -p $< | sed -n $(sed-zoffset) > $@ =20 targets +=3D zoffset.h $(obj)/zoffset.h: $(obj)/compressed/vmlinux FORCE diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/M= akefile index 06934f9691d6..6ec5e031db1d 100644 --- a/arch/x86/boot/compressed/Makefile +++ b/arch/x86/boot/compressed/Makefile @@ -76,7 +76,7 @@ HOST_EXTRACFLAGS +=3D -I$(srctree)/tools/include sed-voffset :=3D -e 's/^\([0-9a-fA-F]*\) [ABbCDGRSTtVW] \(_text\|__start_r= odata\|_sinittext\|__inittext_end\|__bss_start\|_end\)$$/\#define VO_\2 _AC= (0x\1,UL)/p' =20 quiet_cmd_voffset =3D VOFFSET $@ - cmd_voffset =3D $(NM) $< | sed -n $(sed-voffset) > $@ + cmd_voffset =3D $(NM) -p $< | sed -n $(sed-voffset) > $@ =20 targets +=3D ../voffset.h =20 diff --git a/scripts/check-function-names.sh b/scripts/check-function-names= .sh index 08071133e5a5..94883e690627 100755 --- a/scripts/check-function-names.sh +++ b/scripts/check-function-names.sh @@ -13,7 +13,8 @@ if [ ! -f "$objfile" ]; then exit 1 fi =20 -bad_symbols=3D$(${NM:-nm} "$objfile" | awk '$2 ~ /^[TtWw]$/ {print $3}' | = grep -E '^(startup|exit|split|unlikely|hot|unknown)(\.|$)') +bad_symbols=3D$(${NM:-nm} -p "$objfile" | awk '$2 ~ /^[TtWw]$/ {print $3}'= | + grep -E '^(startup|exit|split|unlikely|hot|unknown)(\.|$)') =20 if [ -n "$bad_symbols" ]; then echo "$bad_symbols" | while read -r sym; do diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index e88604150d2c..970ca10f8fa9 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -161,7 +161,7 @@ mksysmap() =20 sorttable() { - ${NM} -S ${1} > .tmp_vmlinux.nm-sort + ${NM} -p -S ${1} > .tmp_vmlinux.nm-sort ${objtree}/scripts/sorttable -s .tmp_vmlinux.nm-sort ${1} } =20 --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8B90A3D79E2; Tue, 8 Sep 2026 20:56:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901001; cv=none; b=urih2cfanEXBMZICvs2QVdTMAT7GF952BMV38WIpG/BbTy0ja1k3IA3TUxggZqbcfW7Ztel36K0GNFJXh9GOaLCCbKIzs+PBrXj6ES58QIHhE+H3uK50BvhM4vRPwfQHAUgzucWV2h2GUid0C58cjxo8ZE984YwjkKuJ89bVPoo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901001; c=relaxed/simple; bh=N13ZqN50eOU7uY3QR0adbXAgcslOg42oxhOb+TbiiNY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=rDMcDVwPC1AIpulPML4oNvR8Qp+gLWUFgYP8zU2MlExix51BOUummMDKr83W/nT4Iho9I+PehXvOZeqQIzzG9Q0kJs+eMfkkz7GHMY5eCFWvEhrqUYYCqky804haGXLfGvp209SamAah+dVEbk29V4+OWompa4HsXfkdqYx2BpA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ia7otobC; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Ia7otobC" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6D80F1F00A3A; Tue, 8 Sep 2026 20:56:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788901000; bh=i4mmShyVZGlgCcDLfFsSbEgGMvI3VWrUjGiN2LoMKxY=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=Ia7otobCwNkhAtHa/irExbcNB39utNz/W3ids/Ovxn6+xF9yjVuSRttrN3bvH3Q88 eEUZ47xB5oUWgq73QBzX+L5v4SNhDmB8A/tobfOM2Lr2Ctw13MxEcooPf/B9vpxj8S Co85Z+fqnbnrJIWyihZVcv2KJwi5weDsPwwhfZaANjyYd7nkLmO98km3QVVAIBkz2K bunSAccE/1l/tzbk6UQMnLrwAFgBjckXWiBugdVPaiP19tJHqEDaYifEUYetj4UK6q sy0IFHIS+L1yHhDjd62WWvz0/Y0QNHUXwZqBsJJXLEpJ+2RnYopzAk7D70CoL42Gj/ uL3Jpl3v1nl8A== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:06 +0100 Subject: [PATCH 06/23] kbuild: only emit vmlinux relocations when required Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-6-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=3331; i=ljs@kernel.org; h=from:subject:message-id; bh=N13ZqN50eOU7uY3QR0adbXAgcslOg42oxhOb+TbiiNY=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlJkt3xWaebJaeGL+Iy2LKP8PquXzJH3nb+NPU+v7v +ri19ncHaUsDGJcDLJiiizPv4jvDxIJm9d5wd8NZg4rE8gQBi5OAZhIVSYjw28ziSzFyOPHLuxN d+HOdz7m1O692N/s6NnTf+UL4/Q0VzD8s3+t5hF984P3AqmC154yZ368DXzBtN93Ts1vtiDP7ms R/AA= X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 A kernel build consists of more than one linking pass on vmlinux.o and vmlinux, at minimum two, and with CONFIG_KALLSYMS and BTF enabled on x86-64 for example there are 5 such stages. For architectures that build their own relocation tables (x86, riscv, mips, s390), vmlinux is linked with the --emit-relocs parameter specified. However, this is only required on the final vmlinux link. Symbol tables of trial links preceding it don't need it because they already check that System.map matches kallsyms symbols on each build. GNU ld is slow at emitting relocation tables, so this results in a reduction in build time. Whole build, 128-thread Threadripper 9980X, best of N runs: before after delta ------------------------------- x86 defconfig, touch mm/vma.c, gcc 9.8s 9.4s -0.43s (-4%) x86 defconfig, clean, gcc 28.6s 28.1s -0.47s (-2%) x86 allmodconfig, touch mm/vma.c, gcc 44.0s 42.7s -1.3s (-3%) Note that this has little impact on LLVM ld which performs this operation more efficiently. Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- Makefile | 4 +++- scripts/Makefile.vmlinux | 3 ++- scripts/link-vmlinux.sh | 6 ++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 66654fa71655..85eb2fd026cb 100644 --- a/Makefile +++ b/Makefile @@ -1263,8 +1263,10 @@ LDFLAGS_vmlinux +=3D --orphan-handling=3D$(CONFIG_LD= _ORPHAN_WARN_LEVEL) endif =20 ifneq ($(CONFIG_ARCH_VMLINUX_NEEDS_RELOCS),) -LDFLAGS_vmlinux +=3D --emit-relocs --discard-none +LDFLAGS_vmlinux +=3D --discard-none +LDFLAGS_vmlinux_relocs :=3D --emit-relocs endif +export LDFLAGS_vmlinux_relocs =20 # Align the architecture of userspace programs with the kernel USERFLAGS_FROM_KERNEL :=3D --target=3D% diff --git a/scripts/Makefile.vmlinux b/scripts/Makefile.vmlinux index fcae1e432d9a..4b54aaeca65d 100644 --- a/scripts/Makefile.vmlinux +++ b/scripts/Makefile.vmlinux @@ -64,7 +64,8 @@ ARCH_POSTLINK :=3D $(wildcard $(srctree)/arch/$(SRCARCH)/= Makefile.postlink) =20 # Final link of vmlinux with optional arch pass after final link cmd_link_vmlinux =3D \ - $< "$(LD)" "$(KBUILD_LDFLAGS)" "$(LDFLAGS_vmlinux)" "$@"; \ + $< "$(LD)" "$(KBUILD_LDFLAGS)" "$(LDFLAGS_vmlinux)" "$@" \ + "$(LDFLAGS_vmlinux_relocs)"; \ $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true) =20 targets +=3D vmlinux.unstripped .vmlinux.export.o diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index 970ca10f8fa9..09c5222ccb94 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -32,6 +32,7 @@ LD=3D"$1" KBUILD_LDFLAGS=3D"$2" LDFLAGS_vmlinux=3D"$3" VMLINUX=3D"$4" +LDFLAGS_vmlinux_relocs=3D"$5" =20 is_enabled() { grep -q "^$1=3Dy" include/config/auto.conf @@ -96,6 +97,11 @@ vmlinux_link() ldflags=3D"${ldflags} ${wl}--strip-debug" fi =20 + # Only the final link actually requires the relocations. + if [ "${output}" =3D "${VMLINUX}" ] && [ -n "${LDFLAGS_vmlinux_relocs}" ]= ; then + ldflags=3D"${ldflags} ${wl}${LDFLAGS_vmlinux_relocs}" + fi + if [ -n "${generate_map}" ]; then ldflags=3D"${ldflags} ${wl}-Map=3Dvmlinux.map" fi --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D750F395AE4; Tue, 8 Sep 2026 20:56:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901013; cv=none; b=u5vFzf5pj3M9ATEj9XxyprDBBo80BtMwYkNTzD5YKtMhvouuDR82vmIOvsAWi2WEPq5OEMINCtACAWRCN9Y560NR2dqiMSB4d45TvENnn/iYs+bTMjY/1Vj6nH+ao4x94YtFzfqbV9zZe9S3NU5uqm6S5TPOtBfcEZWtDTDLwR0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901013; c=relaxed/simple; bh=QDaPegKaob+cY6D7aU/vMKnocWM5p0Hu2c7ORW6alF0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QYm/Q6pq3giTgr2uVuuadK0Ct20vtwA0HPmHKbEA48FR7FV1snBDXsUGnG/I/6rm7pyo/xHKzdvUeL9kfLt6ZSedPbjcfmXa7n167PAdJDx1O5B2SYGn90wa3d+pmutlLqeozB5I17/v50Vq0OK/jZR7JVY0LMCEdcCzkCuTU8I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Vh3qWOcs; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Vh3qWOcs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9DA541F00A3D; Tue, 8 Sep 2026 20:56:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788901011; bh=CifvSQTpmhSSExkWa1yobxbpugzhtdiAK3RRyWGMh1s=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=Vh3qWOcssUtX3KJbTSlaYmMCCKf+xtwSE2Gk7dMPY+2wjYYmveHn+vU+6RF19TRop avudRjYQfY66mEvQqBiTS1h1BAyzhK9cU8BzkloYa5wD/Bmu5KRVMfonZTkfBeqNTy fde3XTAUkIHuaxKBaWqghxhruUZIJka5JS1t/kX9luTYLG0Q2mBew8N9fJaDET3ju4 fCvZQgrE53EBIK9aZxTPCPJiSKG45cCrndZrcrEXAOE2Fn3jlyrK0ixMdD5SXbOh97 DNbCykOGipb28Q8mapJGvagTTnNOgraRpaRIgH5oagmlOIF+UGCbe8RQjO+YRiCvF2 D9mCe4zY3I+dw== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:07 +0100 Subject: [PATCH 07/23] elf-parse: add section flags, symbol binding and a read-only mapping Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-7-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=6366; i=ljs@kernel.org; h=from:subject:message-id; bh=QDaPegKaob+cY6D7aU/vMKnocWM5p0Hu2c7ORW6alF0=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlJkZynmzLLvsX98d/E+QaXdN/Db3T8cl7jTPmS7QX HD8xjvGjlIWBjEuBlkxRZbnX8T3B4mEzeu84O8GM4eVCWQIAxenAEwkdCHDX5HpH+4oTp7/8G9W 3dK1hzJCczzrzG4fZbvvpsJovvu6bQIjw80fp3K+xZ0SOuH6qaTvb3fEurfTPgYzPy8y21VkX6R WzQUA X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 Extend elf-parse to be able to read the symbol table of vmlinux in kallyms. This requires the ability to open ELF files in read-only mode, so provide elf_map_ro() to do so. It also requires accessors for section flags and symbol bindings, so provide these via shdr_flags() and sym_bind(). Also, check for the file being an ELF file first in elf_parse(). This is the logical thing to check for first, but additionally prevents kallsyms from having to check this it self. Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- scripts/elf-parse.c | 48 +++++++++++++++++++++++++++++++++++------------- scripts/elf-parse.h | 19 +++++++++++++++++++ 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/scripts/elf-parse.c b/scripts/elf-parse.c index 99869ff91a8c..87aa95b1e5e1 100644 --- a/scripts/elf-parse.c +++ b/scripts/elf-parse.c @@ -17,15 +17,21 @@ struct elf_funcs elf_parser; * Get the whole file as a programming convenience in order to avoid * malloc+lseek+read+free of many pieces. If successful, then mmap * avoids copying unused pieces; else just read the whole file. - * Open for both read and write. + * Open for both read and write if writable is true, otherwise open + * read-only. */ -static void *map_file(char const *fname, size_t *size) +static void *map_file(char const *fname, size_t *size, bool writable) { - int fd; + int fd, prot =3D PROT_READ, flags =3D MAP_PRIVATE; struct stat sb; void *addr =3D NULL; =20 - fd =3D open(fname, O_RDWR); + if (writable) { + prot |=3D PROT_WRITE; + flags =3D MAP_SHARED; + } + + fd =3D open(fname, writable ? O_RDWR : O_RDONLY); if (fd < 0) { perror(fname); return NULL; @@ -39,7 +45,7 @@ static void *map_file(char const *fname, size_t *size) goto out; } =20 - addr =3D mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + addr =3D mmap(0, sb.st_size, prot, flags, fd, 0); if (addr =3D=3D MAP_FAILED) { fprintf(stderr, "Could not mmap file: %s\n", fname); goto out; @@ -57,6 +63,12 @@ static int elf_parse(const char *fname, void *addr, uint= 32_t types) Elf_Ehdr *ehdr =3D addr; uint16_t type; =20 + if (memcmp(ELFMAG, ehdr->e32.e_ident, SELFMAG) !=3D 0 || + ehdr->e32.e_ident[EI_VERSION] !=3D EV_CURRENT) { + fprintf(stderr, "unrecognized ELF file %s\n", fname); + return -1; + } + switch (ehdr->e32.e_ident[EI_DATA]) { case ELFDATA2LSB: elf_parser.r =3D rle; @@ -78,12 +90,6 @@ static int elf_parse(const char *fname, void *addr, uint= 32_t types) return -1; } =20 - if (memcmp(ELFMAG, ehdr->e32.e_ident, SELFMAG) !=3D 0 || - ehdr->e32.e_ident[EI_VERSION] !=3D EV_CURRENT) { - fprintf(stderr, "unrecognized ELF file %s\n", fname); - return -1; - } - type =3D elf_parser.r2(&ehdr->e32.e_type); if (!((1 << type) & types)) { fprintf(stderr, "Invalid ELF type file %s\n", fname); @@ -103,7 +109,9 @@ static int elf_parse(const char *fname, void *addr, uin= t32_t types) elf_parser.shdr_name =3D shdr32_name; elf_parser.shdr_type =3D shdr32_type; elf_parser.shdr_entsize =3D shdr32_entsize; + elf_parser.shdr_flags =3D shdr32_flags; elf_parser.sym_type =3D sym32_type; + elf_parser.sym_bind =3D sym32_bind; elf_parser.sym_name =3D sym32_name; elf_parser.sym_value =3D sym32_value; elf_parser.sym_shndx =3D sym32_shndx; @@ -133,7 +141,9 @@ static int elf_parse(const char *fname, void *addr, uin= t32_t types) elf_parser.shdr_name =3D shdr64_name; elf_parser.shdr_type =3D shdr64_type; elf_parser.shdr_entsize =3D shdr64_entsize; + elf_parser.shdr_flags =3D shdr64_flags; elf_parser.sym_type =3D sym64_type; + elf_parser.sym_bind =3D sym64_bind; elf_parser.sym_name =3D sym64_name; elf_parser.sym_value =3D sym64_value; elf_parser.sym_shndx =3D sym64_shndx; @@ -174,12 +184,13 @@ int elf_map_long_size(void *addr) return ehdr->e32.e_ident[EI_CLASS] =3D=3D ELFCLASS32 ? 4 : 8; } =20 -void *elf_map(char const *fname, size_t *size, uint32_t types) +static void *__elf_map(char const *fname, size_t *size, uint32_t types, + bool writable) { void *addr; int ret; =20 - addr =3D map_file(fname, size); + addr =3D map_file(fname, size, writable); if (!addr) return NULL; =20 @@ -192,6 +203,17 @@ void *elf_map(char const *fname, size_t *size, uint32_= t types) return addr; } =20 +void *elf_map(char const *fname, size_t *size, uint32_t types) +{ + return __elf_map(fname, size, types, true); +} + +/* For tools that only read the file. */ +void *elf_map_ro(char const *fname, size_t *size, uint32_t types) +{ + return __elf_map(fname, size, types, false); +} + void elf_unmap(void *addr, size_t size) { munmap(addr, size); diff --git a/scripts/elf-parse.h b/scripts/elf-parse.h index f4411e03069d..453286e63032 100644 --- a/scripts/elf-parse.h +++ b/scripts/elf-parse.h @@ -37,10 +37,12 @@ struct elf_funcs { uint64_t (*shdr_offset)(Elf_Shdr *shdr); uint64_t (*shdr_size)(Elf_Shdr *shdr); uint64_t (*shdr_entsize)(Elf_Shdr *shdr); + uint64_t (*shdr_flags)(Elf_Shdr *shdr); uint32_t (*shdr_link)(Elf_Shdr *shdr); uint32_t (*shdr_name)(Elf_Shdr *shdr); uint32_t (*shdr_type)(Elf_Shdr *shdr); uint8_t (*sym_type)(Elf_Sym *sym); + uint8_t (*sym_bind)(Elf_Sym *sym); uint32_t (*sym_name)(Elf_Sym *sym); uint64_t (*sym_value)(Elf_Sym *sym); uint16_t (*sym_shndx)(Elf_Sym *sym); @@ -143,6 +145,7 @@ SHDR_ADDR(addr) SHDR_ADDR(offset) SHDR_ADDR(size) SHDR_ADDR(entsize) +SHDR_ADDR(flags) =20 SHDR_WORD(link) SHDR_WORD(name) @@ -211,6 +214,21 @@ static inline uint8_t sym_type(Elf_Sym *sym) return elf_parser.sym_type(sym); } =20 +static inline uint8_t sym64_bind(Elf_Sym *sym) +{ + return ELF64_ST_BIND(sym->e64.st_info); +} + +static inline uint8_t sym32_bind(Elf_Sym *sym) +{ + return ELF32_ST_BIND(sym->e32.st_info); +} + +static inline uint8_t sym_bind(Elf_Sym *sym) +{ + return elf_parser.sym_bind(sym); +} + SYM_ADDR(value) SYM_WORD(name) SYM_HALF(shndx) @@ -298,6 +316,7 @@ static inline void w8le(uint64_t val, uint64_t *x) } =20 void *elf_map(char const *fname, size_t *size, uint32_t types); +void *elf_map_ro(char const *fname, size_t *size, uint32_t types); void elf_unmap(void *addr, size_t size); int elf_map_machine(void *addr); int elf_map_long_size(void *addr); --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 04A12398910; Tue, 8 Sep 2026 20:57:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901025; cv=none; b=ho7EV0yz2N7lS3tShwdepRXVkkOafEY84iEExB4hW1SwT3VsAVnpvggcRpQOxEK/A57AMtgQyiHxXfjzVM2H4OjPrJsp8oWvK4XZ5wcYOOLF1o2qANhpoLM7kIqdpTHq66p8ydcaRHdFSDWeKosqfTOpMPef4UKagsDifosxBT4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901025; c=relaxed/simple; bh=5y00115SFcZ8vzv72gceOoSm73xT//Tz2uvCbUEWkt8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=JU1jCWr9ehKwEz6gP6O6ZgTN3be58IF6TWsOAuq1MGp1CpDc01qzGmfNOOWOopgikXH9KHmMiXdCVfvwYviaVV07IKtMv84LRw4J9ESu93rbMhCuhjh5rgt2eF7HBHpzKKrYZekRfy7fI3SN1Nkv1RseLBvChaCwHuulU53KROY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KoNf5wDA; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KoNf5wDA" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D31861F00A3A; Tue, 8 Sep 2026 20:56:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788901022; bh=fXBQrvZ0NKNGhD+rPpCfQGuHIcOyFVA6H3XYu2yHzWg=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=KoNf5wDAnQDx/Z8vtBXK4pZkC9xDY0f5cv8FlmQPINLxHaDhCyWAiTjKWaPs/DSaP 0XSnc9BrpchDwlKxL67oWhqdEVdBIxeaTCWrMVlQE69mGuz1ISi2vkOrna+cJPdHLo YXNFhmO7wtwnKXtCISd7Bkcaw5YehOMe8Pk3/xt/ZSQlXXF51OdCelexwy9lYeFdAz BxGztDFS3oim1xwdE5S19vAIfcdHQoHFCkg1vBsHZfuAX9Ll1DpNfk06kAvWyS1U5/ 12FdUnCjO1hLeWYcGjwGQAiiW9kncE71cIjclOz3Iwa0mj5ffo+6E5DQghJywpinRI Hpg6LJERJ2kSQ== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:08 +0100 Subject: [PATCH 08/23] kallsyms: reimplement mksysmap in C Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-8-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=24381; i=ljs@kernel.org; h=from:subject:message-id; bh=5y00115SFcZ8vzv72gceOoSm73xT//Tz2uvCbUEWkt8=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlJkl+NZ+sq5b/6vkUKTzow5Jq2dX925Jdf4cVMeXf HVP4Je9HaUsDGJcDLJiiizPv4jvDxIJm9d5wd8NZg4rE8gQBi5OAZhIsTEjw4XdEft7Yk8nLovP VTTZ0fj510JBl/KP1j/avrnfsA9+dIuR4dG7urwjK99OL5OcMXHtjlX3ShSCcpctZgjtbPZqP33 oIR8A X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 mksysmap is a sed script consisting of 30 patterns which link-vmlinux.sh uses to generate *.syms files, and which kallsyms is then called against to generate *.kallsyms files, with the final vmlinux build ultimately generating System.map. For an x86-64 allmodconfig build, this involves three nm runs over a 250 MiB file and half a million lines written and read each time - 0.5s per pass for llvm-nm, and 0.2s for GNU nm, with parsing on top of that. This is unnecessary, instead have kallsyms simply read the ELF file directly making use of the existing elf-parse library in scripts/. This changes kallsyms such that its input is no longer the output from nm, but rather an ELF file. However, if the input file is empty, it outputs an empty table, which retains the same behaviour on first pass that the build system expects. System.map is byte-identical to nm | mksysmap for GNU nm and llvm-nm on two x86 configurations each, and for llvm-nm on arm64, arm, s390 and loongarch defconfigs, so are the kallsyms tables of every pass. Relinking vmlinux, link steps included: before after allmodconfig clang 9.1s 7.9s allmodconfig gcc 7.8s 7.6s defconfig clang 3.7s 3.0s defconfig gcc 3.5s 3.4s Whole build, 128-thread Threadripper 9980X, best of N runs: before after delta ------------------------------- x86 defconfig, touch mm/vma.c, gcc 9.4s 8.9s -0.45s (-5%) x86 defconfig, touch mm/vma.c, clang 9.2s 8.1s -1.1s (-12%) x86 defconfig, clean, gcc 28.1s 27.6s -0.51s (-2%) x86 defconfig, clean, clang 28.2s 27.2s -1.0s (-4%) x86 allmodconfig, touch mm/vma.c, gcc 42.7s 42.0s -0.70s (-2%) x86 allmodconfig, touch mm/vma.c, clang 40.2s 38.1s -2.1s (-5%) Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- scripts/Makefile | 4 +- scripts/kallsyms-sysmap.c | 269 ++++++++++++++++++++++++++++++++++++++++++= ++++ scripts/kallsyms.c | 162 ++++++++++++++++------------ scripts/kallsyms.h | 44 ++++++++ scripts/link-vmlinux.sh | 15 +-- scripts/mksysmap | 94 ---------------- 6 files changed, 415 insertions(+), 173 deletions(-) diff --git a/scripts/Makefile b/scripts/Makefile index 3434a82a119f..d46932113b5f 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -3,7 +3,7 @@ # scripts contains sources for various helper programs used throughout # the kernel for the build process. =20 -hostprogs-always-$(CONFIG_KALLSYMS) +=3D kallsyms +hostprogs-always-y +=3D kallsyms hostprogs-always-$(BUILD_C_RECORDMCOUNT) +=3D recordmcount hostprogs-always-$(CONFIG_BUILDTIME_TABLE_SORT) +=3D sorttable hostprogs-always-$(CONFIG_ASN1) +=3D asn1_compiler @@ -13,6 +13,7 @@ hostprogs-always-$(CONFIG_RUST_KERNEL_DOCTESTS) +=3D rus= tdoc_test_builder hostprogs-always-$(CONFIG_RUST_KERNEL_DOCTESTS) +=3D rustdoc_test_gen hostprogs-always-$(CONFIG_TRACEPOINTS) +=3D tracepoint-update =20 +kallsyms-objs :=3D kallsyms.o kallsyms-sysmap.o elf-parse.o sorttable-objs :=3D sorttable.o elf-parse.o tracepoint-update-objs :=3D tracepoint-update.o elf-parse.o =20 @@ -30,6 +31,7 @@ rustdoc_test_builder-rust :=3D y rustdoc_test_gen-rust :=3D y =20 HOSTCFLAGS_tracepoint-update.o =3D -I$(srctree)/tools/include +HOSTCFLAGS_kallsyms-sysmap.o =3D -I$(srctree)/tools/include HOSTCFLAGS_elf-parse.o =3D -I$(srctree)/tools/include HOSTCFLAGS_sorttable.o =3D -I$(srctree)/tools/include HOSTLDLIBS_sorttable =3D -lpthread diff --git a/scripts/kallsyms-sysmap.c b/scripts/kallsyms-sysmap.c new file mode 100644 index 000000000000..64b2e11d0344 --- /dev/null +++ b/scripts/kallsyms-sysmap.c @@ -0,0 +1,269 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Obtain symbols from vmlinux for usage by kallsyms. Replaces mksysmap. + * + * To retain compatibility, it provides the same output as nm, only faster. + */ + +#include +#include +#include +#include +#include + +#include "elf-parse.h" +#include "kallsyms.h" + +/* The mapped file and its symbol table. */ +struct elf_file { + void *base; + size_t size; + const char *shdrs; + unsigned int shnum, shentsize; + const char *shstrtab; + Elf_Shdr *symtab; + const char *strtab; + size_t nr_syms; +}; + +/* What mksysmap dropped from System.map, by name. */ +static const char *const sysmap_omit_prefixes[] =3D { + "$", ".L", "__efistub_", "__pi_$", "__pi_.L", "__kvm_nvhe_$", + "__kvm_nvhe_.L", "__kcfi_typeid_", "__kvm_nvhe___kcfi_typeid_", + "__pi___kcfi_typeid_", "__crc_", "__kstrtab_", "__kstrtabns_", + "__mod_device_table__", +}; +static const char *const sysmap_omit_suffixes[] =3D { + "_from_arm", "_from_thumb", "_veneer", +}; +static const char *const sysmap_omit_names[] =3D { + "L0", "_SDA_BASE_", "_SDA2_BASE_", +}; + +/* __*Thunk_: the linker's range extension thunks on arm. */ +static bool is_range_thunk(const char *name) +{ + const char *p; + + if (!string_starts_with(name, "__")) + return false; + for (p =3D name + 2; isalnum((unsigned char)*p); p++) + ; + return p - name >=3D 7 && *p =3D=3D '_' && strncmp(p - 5, "Thunk", 5) =3D= =3D 0; +} + +/* __UNIQUE_ID_modinfo_: the MODULE_INFO() strings of built-in code. */ +static bool is_modinfo_id(const char *name) +{ + static const char prefix[] =3D "__UNIQUE_ID_modinfo_"; + const char *p; + + if (!string_starts_with(name, prefix)) + return false; + for (p =3D name + strlen(prefix); isdigit((unsigned char)*p); p++) + ; + return !*p; +} + +static bool sysmap_omits(const char *name, char type) +{ + size_t i; + + /* Absolute, undefined and debugging symbols. */ + if (type =3D=3D 'a' || type =3D=3D 'N' || type =3D=3D 'U' || type =3D=3D = 'w') + return true; + + for (i =3D 0; i < ARRAY_SIZE(sysmap_omit_prefixes); i++) + if (string_starts_with(name, sysmap_omit_prefixes[i])) + return true; + for (i =3D 0; i < ARRAY_SIZE(sysmap_omit_suffixes); i++) + if (string_ends_with(name, sysmap_omit_suffixes[i])) + return true; + for (i =3D 0; i < ARRAY_SIZE(sysmap_omit_names); i++) + if (strcmp(name, sysmap_omit_names[i]) =3D=3D 0) + return true; + + return is_range_thunk(name) || is_modinfo_id(name) || + strstr(name, ".long_branch.") || strstr(name, ".plt_branch."); +} + +static Elf_Shdr *elf_section(const struct elf_file *elf, unsigned int inde= x) +{ + return (Elf_Shdr *)(elf->shdrs + (size_t)index * elf->shentsize); +} + +static const char *elf_section_name(const struct elf_file *elf, Elf_Shdr *= shdr) +{ + return elf->shstrtab + shdr_name(shdr); +} + +static Elf_Sym *elf_symbol(const struct elf_file *elf, size_t index) +{ + const char *base =3D elf->base; + + return (Elf_Sym *)(base + shdr_offset(elf->symtab) + + index * shdr_entsize(elf->symtab)); +} + +/* nm's letter for a symbol defined in a section, as BFD classifies it. */ +static char section_symbol_type(Elf_Shdr *shdr, const char *secname) +{ + static const char *const debug_prefixes[] =3D { + ".debug", ".zdebug", ".gnu.debuglto_.debug_", + ".gnu.linkonce.wi.", ".line", ".stab", + }; + const uint64_t flags =3D shdr_flags(shdr); + size_t i; + + if (flags & SHF_EXECINSTR) + return 't'; + if (flags & SHF_ALLOC) { + if (shdr_type(shdr) =3D=3D SHT_NOBITS) + return 'b'; + return flags & SHF_WRITE ? 'd' : 'r'; + } + for (i =3D 0; i < ARRAY_SIZE(debug_prefixes); i++) + if (string_starts_with(secname, debug_prefixes[i])) + return 'N'; + if (shdr_type(shdr) !=3D SHT_NOBITS && !(flags & SHF_WRITE)) + return 'n'; + return '?'; +} + +/* The letter nm prints for a symbol, or 0 for one it leaves out. */ +static char elf_symbol_type(const struct elf_file *elf, Elf_Sym *sym) +{ + unsigned int bind =3D sym_bind(sym), type =3D sym_type(sym); + unsigned int shndx =3D sym_shndx(sym); + Elf_Shdr *shdr; + char c; + + if (type =3D=3D STT_SECTION || type =3D=3D STT_FILE) + return 0; + if (shndx =3D=3D SHN_COMMON) + return 'C'; + if (shndx =3D=3D SHN_UNDEF) { + if (bind =3D=3D STB_WEAK) + return type =3D=3D STT_OBJECT ? 'v' : 'w'; + return 'U'; + } + if (type =3D=3D STT_GNU_IFUNC) + return 'i'; + if (bind =3D=3D STB_WEAK) + return type =3D=3D STT_OBJECT ? 'V' : 'W'; + if (bind =3D=3D STB_GNU_UNIQUE) + return 'u'; + if (bind !=3D STB_GLOBAL && bind !=3D STB_LOCAL) + return '?'; + + if (shndx =3D=3D SHN_ABS) { + c =3D 'a'; + } else if (shndx < elf->shnum) { + shdr =3D elf_section(elf, shndx); + c =3D section_symbol_type(shdr, elf_section_name(elf, shdr)); + } else { + return '?'; + } + + return bind =3D=3D STB_GLOBAL ? toupper(c) : c; +} + +/* nm -n order: by address, then by name. */ +static int compare_symbols(const void *a, const void *b) +{ + const struct sysmap_symbol *sa =3D a, *sb =3D b; + + if (sa->addr !=3D sb->addr) + return sa->addr < sb->addr ? -1 : 1; + return strcmp(sa->name, sb->name); +} + +static void elf_open(struct elf_file *elf, const char *path) +{ + Elf_Ehdr *ehdr; + unsigned int i; + + elf->base =3D elf_map_ro(path, &elf->size, (1 << ET_EXEC) | (1 << ET_DYN)= ); + if (!elf->base) + exit(EXIT_FAILURE); + + ehdr =3D elf->base; + elf->shdrs =3D (const char *)elf->base + ehdr_shoff(ehdr); + elf->shnum =3D ehdr_shnum(ehdr); + elf->shentsize =3D ehdr_shentsize(ehdr); + elf->shstrtab =3D (const char *)elf->base + + shdr_offset(elf_section(elf, ehdr_shstrndx(ehdr))); + + for (i =3D 0; i < elf->shnum && !elf->symtab; i++) + if (shdr_type(elf_section(elf, i)) =3D=3D SHT_SYMTAB) + elf->symtab =3D elf_section(elf, i); + + if (!elf->symtab) { + fprintf(stderr, "%s: no symbol table\n", path); + exit(EXIT_FAILURE); + } + + elf->strtab =3D (const char *)elf->base + + shdr_offset(elf_section(elf, shdr_link(elf->symtab))); + elf->nr_syms =3D shdr_size(elf->symtab) / shdr_entsize(elf->symtab); +} + +/* The symbols "nm -n | mksysmap" would list, in that order. */ +static struct sysmap_symbol *elf_read_symbols(const struct elf_file *elf, + size_t *nr_kept) +{ + struct sysmap_symbol *syms =3D xmalloc(elf->nr_syms * sizeof(*syms)); + size_t i, n =3D 0; + + for (i =3D 1; i < elf->nr_syms; i++) { + Elf_Sym *sym =3D elf_symbol(elf, i); + const char *name =3D elf->strtab + sym_name(sym); + char type =3D elf_symbol_type(elf, sym); + + if (!type || sysmap_omits(name, type)) + continue; + + syms[n].addr =3D sym_value(sym); + syms[n].name =3D name; + syms[n].type =3D type; + n++; + } + + qsort(syms, n, sizeof(*syms), compare_symbols); + *nr_kept =3D n; + return syms; +} + +struct sysmap *sysmap_read(const char *path) +{ + struct sysmap *map =3D xcalloc(1, sizeof(*map)); + struct elf_file elf =3D {}; + + elf_open(&elf, path); + map->syms =3D elf_read_symbols(&elf, &map->nr_syms); + map->addr_width =3D elf_map_long_size(elf.base) * 2; + /* The names point into the mapping; keep it until the map is freed. */ + map->file =3D elf.base; + map->file_size =3D elf.size; + + return map; +} + +void sysmap_write(const struct sysmap *map, FILE *out) +{ + size_t i; + + for (i =3D 0; i < map->nr_syms; i++) { + const struct sysmap_symbol *s =3D &map->syms[i]; + + fprintf(out, "%0*llx %c %s\n", map->addr_width, s->addr, s->type, + s->name); + } +} + +void sysmap_free(struct sysmap *map) +{ + free(map->syms); + elf_unmap(map->file, map->file_size); + free(map); +} diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 61c5eb537ed4..b383696c3d81 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -5,7 +5,12 @@ * This software may be used and distributed according to the terms * of the GNU General Public License, incorporated herein by reference. * - * Usage: kallsyms [--all-symbols] [--pc-relative] in.map out.bin > out.S + * Usage: kallsyms [--all-symbols] [--pc-relative] [--sysmap=3Dout.map] in= out.bin > out.S + * kallsyms --sysmap=3Dout.map in + * + * in is vmlinux; an empty file stands for the first link, which has = no + * symbols yet, and gives an empty table. --sysmap also writes the symbols + * in System.map format. * * The byte tables go to out.bin and are pulled into out.S with .incb= in; * wider tables stay assembler source for endianness and relocations. @@ -21,7 +26,6 @@ * */ =20 -#include #include #include #include @@ -29,10 +33,10 @@ #include #include #include - +#include #include =20 -#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) +#include "kallsyms.h" =20 #define KSYM_NAME_LEN 512 =20 @@ -105,11 +109,13 @@ static void sym_arr_free(struct sym_arr *arr) =20 static void usage(void) { - fprintf(stderr, "Usage: kallsyms [--all-symbols] [--pc-relative] in.map o= ut.bin > out.S\n"); + fprintf(stderr, "Usage: kallsyms [--all-symbols] [--pc-relative] [--sysma= p=3Dout.map]\n" + " in out.bin > out.S\n" + " kallsyms --sysmap=3Dout.map vmlinux\n"); exit(1); } =20 -static char *sym_name(const struct sym_entry *s) +static char *sym_entry_name(const struct sym_entry *s) { return (char *)s->sym + 1; } @@ -150,37 +156,12 @@ static void check_symbol_range(const char *sym, unsig= ned long long addr, } } =20 -static struct sym_entry *read_symbol(FILE *in, char **buf, size_t *buf_len) +static struct sym_entry *add_symbol(unsigned long long addr, char type, + const char *name) { - char *name, type, *p; - unsigned long long addr; - size_t len; - ssize_t readlen; + size_t len =3D strlen(name); struct sym_entry *sym; =20 - errno =3D 0; - readlen =3D getline(buf, buf_len, in); - if (readlen < 0) { - if (errno) { - perror("read_symbol"); - exit(EXIT_FAILURE); - } - return NULL; - } - - if ((*buf)[readlen - 1] =3D=3D '\n') - (*buf)[readlen - 1] =3D 0; - - addr =3D strtoull(*buf, &p, 16); - - if (*buf =3D=3D p || *p++ !=3D ' ' || !isascii((type =3D *p++)) || *p++ != =3D ' ') { - fprintf(stderr, "line format error\n"); - exit(EXIT_FAILURE); - } - - name =3D p; - len =3D strlen(name); - if (len >=3D KSYM_NAME_LEN) { fprintf(stderr, "Symbol %s too long for kallsyms (%zu >=3D %d).\n" "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n", @@ -205,7 +186,7 @@ static struct sym_entry *read_symbol(FILE *in, char **b= uf, size_t *buf_len) sym->addr =3D addr; sym->len =3D len; sym->sym[0] =3D type; - strcpy(sym_name(sym), name); + strcpy(sym_entry_name(sym), name); =20 return sym; } @@ -226,14 +207,9 @@ static int symbol_in_range(const struct sym_entry *s, return 0; } =20 -static bool string_starts_with(const char *s, const char *prefix) -{ - return strncmp(s, prefix, strlen(prefix)) =3D=3D 0; -} - static int symbol_valid(const struct sym_entry *s) { - const char *name =3D sym_name(s); + const char *name =3D sym_entry_name(s); =20 /* if --all-symbols is not specified, then symbols outside the text * and inittext sections are discarded */ @@ -283,36 +259,55 @@ static void shrink_table(void) table_cnt =3D pos; } =20 -static void read_map(const char *in) +static void add_table_entry(struct sym_entry *sym) { - FILE *fp; - struct sym_entry *sym; - char *buf =3D NULL; - size_t buflen =3D 0; + sym->seq =3D table_cnt; =20 - fp =3D fopen(in, "r"); - if (!fp) { - perror(in); - exit(1); + if (table_cnt >=3D table_size) { + table_size +=3D 10000; + table =3D xrealloc(table, sizeof(*table) * table_size); } =20 - while (!feof(fp)) { - sym =3D read_symbol(fp, &buf, &buflen); - if (!sym) - continue; + table[table_cnt++] =3D sym; +} =20 - sym->seq =3D table_cnt; +static bool file_is_empty(const char *path) +{ + struct stat st; =20 - if (table_cnt >=3D table_size) { - table_size +=3D 10000; - table =3D xrealloc(table, sizeof(*table) * table_size); - } + if (stat(path, &st)) { + perror(path); + exit(EXIT_FAILURE); + } + + return st.st_size =3D=3D 0; +} + +/* + * Read the symbols from vmlinux, writing System.map if asked to. The first + * link has no symbols yet: an empty file gives an empty table. + */ +static void read_elf(const char *path, FILE *sysmap_out) +{ + struct sysmap *map; + size_t i; + + if (file_is_empty(path)) + return; + + map =3D sysmap_read(path); + if (sysmap_out) + sysmap_write(map, sysmap_out); + + for (i =3D 0; i < map->nr_syms; i++) { + const struct sysmap_symbol *s =3D &map->syms[i]; + struct sym_entry *sym =3D add_symbol(s->addr, s->type, s->name); =20 - table[table_cnt++] =3D sym; + if (sym) + add_table_entry(sym); } =20 - free(buf); - fclose(fp); + sysmap_free(map); } =20 static void output_label(const char *label) @@ -389,7 +384,7 @@ static int compare_names(const void *a, const void *b) const struct sym_entry *sa =3D *(const struct sym_entry **)a; const struct sym_entry *sb =3D *(const struct sym_entry **)b; =20 - ret =3D strcmp(sym_name(sa), sym_name(sb)); + ret =3D strcmp(sym_entry_name(sa), sym_entry_name(sb)); if (!ret) { if (sa->addr > sb->addr) return 1; @@ -765,7 +760,7 @@ static void optimize_token_table(void) /* guess for "linker script provide" symbol */ static int may_be_linker_script_provide_symbol(const struct sym_entry *se) { - const char *symbol =3D sym_name(se); + const char *symbol =3D sym_entry_name(se); int len =3D se->len - 1; =20 if (len < 8) @@ -822,8 +817,8 @@ static int compare_symbols(const void *a, const void *b) return wa - wb; =20 /* sort by the number of prefix underscores */ - wa =3D strspn(sym_name(sa), "_"); - wb =3D strspn(sym_name(sb), "_"); + wa =3D strspn(sym_entry_name(sa), "_"); + wb =3D strspn(sym_entry_name(sb), "_"); if (wa !=3D wb) return wa - wb; =20 @@ -838,13 +833,14 @@ static void sort_symbols(void) =20 int main(int argc, char **argv) { - const char *out_bin_name; - FILE *out_bin_file; + const char *in, *sysmap =3D NULL, *out_bin_name; + FILE *sysmap_out =3D NULL, *out_bin_file; =20 while (1) { static const struct option long_options[] =3D { {"all-symbols", no_argument, &all_symbols, 1}, {"pc-relative", no_argument, &pc_relative, 1}, + {"sysmap", required_argument, NULL, 's'}, {}, }; =20 @@ -852,13 +848,33 @@ int main(int argc, char **argv) =20 if (c =3D=3D -1) break; - if (c !=3D 0) + if (c =3D=3D 's') + sysmap =3D optarg; + else if (c !=3D 0) usage(); } =20 - if (optind + 2 !=3D argc) + if (optind + 2 !=3D argc && !(sysmap && optind + 1 =3D=3D argc)) usage(); =20 + in =3D argv[optind]; + if (sysmap) { + sysmap_out =3D fopen(sysmap, "w"); + if (!sysmap_out) { + perror(sysmap); + exit(EXIT_FAILURE); + } + } + + if (optind + 1 =3D=3D argc) { + read_elf(in, sysmap_out); + if (fclose(sysmap_out)) { + perror(sysmap); + exit(EXIT_FAILURE); + } + return 0; + } + out_bin_name =3D argv[optind + 1]; out_bin_file =3D fopen(out_bin_name, "w"); if (!out_bin_file) { @@ -866,7 +882,11 @@ int main(int argc, char **argv) exit(EXIT_FAILURE); } =20 - read_map(argv[optind]); + read_elf(in, sysmap_out); + if (sysmap_out && fclose(sysmap_out)) { + perror(sysmap); + exit(EXIT_FAILURE); + } shrink_table(); sort_symbols(); optimize_token_table(); diff --git a/scripts/kallsyms.h b/scripts/kallsyms.h new file mode 100644 index 000000000000..12096978075f --- /dev/null +++ b/scripts/kallsyms.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef KALLSYMS_H +#define KALLSYMS_H + +#include +#include +#include +#include + +#include + +static inline bool string_starts_with(const char *s, const char *prefix) +{ + return strncmp(s, prefix, strlen(prefix)) =3D=3D 0; +} + +static inline bool string_ends_with(const char *s, const char *suffix) +{ + size_t len =3D strlen(s), suffix_len =3D strlen(suffix); + + return len >=3D suffix_len && strcmp(s + len - suffix_len, suffix) =3D=3D= 0; +} + +/* A symbol as nm lists it. */ +struct sysmap_symbol { + unsigned long long addr; + const char *name; + char type; +}; + +/* The symbols of an ELF file that System.map lists, in its order. */ +struct sysmap { + struct sysmap_symbol *syms; + size_t nr_syms; + int addr_width; /* hex digits of an address */ + void *file; /* the mapping the names point into */ + size_t file_size; +}; + +struct sysmap *sysmap_read(const char *path); +void sysmap_write(const struct sysmap *map, FILE *out); +void sysmap_free(struct sysmap *map); + +#endif /* KALLSYMS_H */ diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index 09c5222ccb94..7e9d0676f59f 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -124,11 +124,15 @@ kallsymso_changed() ! cmp -s "${kallsymso_prev}.sym" "${kallsymso}.sym" } =20 -# Create ${2}.o file with all symbols from the ${1} object file +# Create ${2}.o with the kallsyms tables for ${1} (the vmlinux, or an empty +# listing for the first pass); list the symbols used in ${3} if given. kallsyms() { local kallsymopt; =20 + if [ -n "${3:-}" ]; then + kallsymopt=3D"--sysmap=3D${3}" + fi if is_enabled CONFIG_KALLSYMS_ALL; then kallsymopt=3D"${kallsymopt} --all-symbols" fi @@ -151,18 +155,15 @@ kallsyms() # Perform kallsyms for the given temporary vmlinux. sysmap_and_kallsyms() { - mksysmap "${1}" "${1}.syms" - kallsyms "${1}.syms" "${1}.kallsyms" - + kallsyms "${1}" "${1}.kallsyms" "${1}.syms" kallsyms_sysmap=3D${1}.syms } =20 # Create map file with all symbols from ${1} -# See mksymap for additional details mksysmap() { - info NM ${2} - ${NM} -n "${1}" | sed -f "${srctree}/scripts/mksysmap" > "${2}" + info SYSMAP ${2} + scripts/kallsyms --sysmap=3D"${2}" "${1}" } =20 sorttable() diff --git a/scripts/mksysmap b/scripts/mksysmap deleted file mode 100755 index 856b26ba2ac0..000000000000 --- a/scripts/mksysmap +++ /dev/null @@ -1,94 +0,0 @@ -#!/bin/sed -f -# SPDX-License-Identifier: GPL-2.0-only -# -# sed script to filter out symbols that are not needed for System.map, -# or not suitable for kallsyms. The input should be 'nm -n '. -# -# System.map is used by module-init tools and some debugging -# tools to retrieve the actual addresses of symbols in the kernel. -# -# readprofile starts reading symbols when _stext is found, and -# continue until it finds a symbol which is not either of 'T', 't', -# 'W' or 'w'. -# -# ------------------------------------------------------------------------= --- -# Ignored symbol types -# - -# a: local absolute symbols -# N: debugging symbols -# U: undefined global symbols -# w: local weak symbols -/ [aNUw] /d - -# ------------------------------------------------------------------------= --- -# Ignored prefixes -# (do not forget a space before each pattern) - -# local symbols for ARM, MIPS, etc. -/ \$/d - -# local labels, .LBB, .Ltmpxxx, .L__unnamed_xx, .LASANPC, etc. -/ \.L/d - -# arm64 EFI stub namespace -/ __efistub_/d - -# arm64 local symbols in PIE namespace -/ __pi_\$/d -/ __pi_\.L/d - -# arm64 local symbols in non-VHE KVM namespace -/ __kvm_nvhe_\$/d -/ __kvm_nvhe_\.L/d - -# lld arm/aarch64/mips thunks -/ __[[:alnum:]]*Thunk_/d - -# CFI type identifiers -/ __kcfi_typeid_/d -/ __kvm_nvhe___kcfi_typeid_/d -/ __pi___kcfi_typeid_/d - -# CRC from modversions -/ __crc_/d - -# EXPORT_SYMBOL (symbol name) -/ __kstrtab_/d - -# EXPORT_SYMBOL (namespace) -/ __kstrtabns_/d - -# MODULE_DEVICE_TABLE (symbol name) -/ __mod_device_table__/d - -# ------------------------------------------------------------------------= --- -# Ignored suffixes -# (do not forget '$' after each pattern) - -# arm -/_from_arm$/d -/_from_thumb$/d -/_veneer$/d - -# ------------------------------------------------------------------------= --- -# Ignored symbols (exact match) -# (do not forget a space before and '$' after each pattern) - -# for LoongArch? -/ L0$/d - -# ppc -/ _SDA_BASE_$/d -/ _SDA2_BASE_$/d - -# MODULE_INFO() -/ __UNIQUE_ID_modinfo_[0-9]*$/d - -# ------------------------------------------------------------------------= --- -# Ignored patterns -# (symbols that contain the pattern are ignored) - -# ppc stub -/\.long_branch\./d -/\.plt_branch\./d --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2256E3B27C7; Tue, 8 Sep 2026 20:57:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901035; cv=none; b=kS4FL5ecHTdtxZ610CSqLDcZKhGxnZG3jckNJLMzVmUAl66DqPW9RQ7QdUgSNx8fXb9EhY6gvcYOH69EXaTEBPRdq7voO+y+Kv3zcD6IWgknO/q2HwRSWMb9q/7Qf6+VOTa0BM6Bmns8ecZ/zMmo+5FDdWmWTj5B/uUqeUFc8fA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901035; c=relaxed/simple; bh=MbJ/+6TmwtBwAFA3foaiJYQ20esYkNk6sPYlzgWUs0Q=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=PMtM6g36w7yHlEpMhuCbNrC98ICqZ/csTYY6mEBHgCrs8MBFew3wsVIj45/hTsi5VZlBH6jCV8f+BuqNVRIKY1/z7G6PWuxSsPBBnlRC8YY8YLE5wg1CpX+79hvvN/pVtM6nDvA/zzToXOgXLHA2/YqMJ2ywZ00Tve3l3gVgtlw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FXfJGq/q; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FXfJGq/q" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 16A4A1F00A3D; Tue, 8 Sep 2026 20:57:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788901033; bh=GYh5ADlkGLy+2VaSmAelr1Xekd9caswVRBA8jASHD2s=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=FXfJGq/qY4JmJQuVyW2SpUy9+ytBVLe2S+qVuwH4rYbalkg/HocfiX84Ngdc6lVga vHwHd9434u13Te2c7pP9dukLigUKNecK6PDB3SX7PHIN+p5yybvca6tvdJL8UUUdBp zbZgJFz/ZLTPlK0ddGk161TUz6q7CneS82OhmjlKtHSahTn2hSzO4ZgIK1LaT1VDNw XCqRc+ECGGJMw0mHZOUQZIrUeJGN9xGoLn+I2sk+4BTgeLQPwEe+XUtOrI8HQyef0F mkW4zW40JU/TVZ0lzZAPzPt2ZzmWwcWl3XokXyaOl7X69g1AxhbapDEoGgt1mUBWX7 fdYkzsvM/1b4w== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:09 +0100 Subject: [PATCH 09/23] kbuild: do not allocate .modinfo in vmlinux Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-9-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=4632; i=ljs@kernel.org; h=from:subject:message-id; bh=MbJ/+6TmwtBwAFA3foaiJYQ20esYkNk6sPYlzgWUs0Q=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlJn1XnoTy/us7THn/v8RVX+XTrB3Wan2+vZps7D/2 kaXZPW5O0pZGMS4GGTFFFmefxHfHyQSNq/zgr8bzBxWJpAhDFycAjCRMw8ZGdafYxPw61h16A/D 3IeJjP63F138mL0o+o/alu0Bf64lHZ3NyDBV/3Zv6yO/02fcggQX9Xe4cp6t3mXzmq+wRyhq6Wn HmWwA X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 Commit 3e86e4d74c04 ("kbuild: keep .modinfo section in vmlinux.unstripped") placed MODULE_INFO() strings in the .modinfo section in vmlinux.unstripped, however it left the section allocatable, so it gets assigned an address and on arm64, arm and riscv, the section is tagged as PT_LOAD. This is useless as the data is ultimately stripped anyway. Doing this results in a lot of unnecessary work - each pass copies the whole file, 450 MIB with relocations for an x86 allmodconfig build and 250 MiB for an arm64 allmodconfig build. This adds ~0.7s on the serial tail of every build for arm64 and ~0.2s for x86 (the tail is single-threaded work done after parallel work has finished). Nothing requires .modinfo to exist at an address, so fix this by using --dump-section which prevents the allocation. Image, bzImage, System.map and modules.builtin.modinfo are unchanged and the stripped vmlinux differs only in its program headers. The objcopy passes are on the serial tail of every build that links vmlinux, no-op builds are unchanged. Whole build, 128-thread Threadripper 9980X, best of N runs: before after delta ------------------------------- x86 defconfig, touch mm/vma.c, gcc 8.9s 8.8s -0.16s (-2%) x86 defconfig, touch mm/vma.c, clang 8.1s 7.9s -0.20s (-2%) x86 defconfig, clean, gcc 27.6s 27.5s -0.13s (0%) x86 defconfig, clean, clang 27.2s 27.0s -0.18s (-1%) x86 allmodconfig, touch mm/vma.c, gcc 42.0s 41.6s -0.40s (-1%) Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- include/asm-generic/vmlinux.lds.h | 2 +- scripts/Makefile.vmlinux | 24 ++++++------------------ 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinu= x.lds.h index b2988aa12f66..7be9e0321867 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -855,7 +855,7 @@ KLP_SYMID =20 #define MODINFO \ - .modinfo : { *(.modinfo) . =3D ALIGN(8); } + .modinfo (INFO) : { *(.modinfo) . =3D ALIGN(8); } =20 #ifdef CONFIG_GENERIC_BUG #define BUG_TABLE \ diff --git a/scripts/Makefile.vmlinux b/scripts/Makefile.vmlinux index 4b54aaeca65d..1fc7a0ca8eaf 100644 --- a/scripts/Makefile.vmlinux +++ b/scripts/Makefile.vmlinux @@ -90,11 +90,9 @@ remove-section-$(CONFIG_ARCH_VMLINUX_NEEDS_RELOCS) +=3D = '.rel.*' =20 remove-symbols :=3D -w --strip-unneeded-symbol=3D'__mod_device_table__*' =20 -# To avoid warnings: "empty loadable segment detected at ..." from GNU obj= copy, -# it is necessary to remove the PT_LOAD flag from the segment. +# none of the removed sections is allocated, so no segment is left empty quiet_cmd_strip_relocs =3D OBJCOPY $@ - cmd_strip_relocs =3D $(OBJCOPY) $(patsubst %,--set-section-flags %= =3Dnoload,$(remove-section-y)) $< $@; \ - $(OBJCOPY) $(addprefix --remove-section=3D,$(remo= ve-section-y)) $(remove-symbols) $@ + cmd_strip_relocs =3D $(OBJCOPY) $(addprefix --remove-section=3D,$(re= move-section-y)) $(remove-symbols) $< $@ =20 targets +=3D vmlinux vmlinux: vmlinux.unstripped FORCE @@ -103,21 +101,11 @@ vmlinux: vmlinux.unstripped FORCE # modules.builtin.modinfo # ------------------------------------------------------------------------= --- =20 -# .modinfo in vmlinux.unstripped is aligned to 8 bytes for compatibility w= ith -# tools that expect vmlinux to have sufficiently aligned sections but the -# additional bytes used for padding .modinfo to satisfy this requirement b= reak -# certain versions of kmod with -# -# depmod: ERROR: kmod_builtin_iter_next: unexpected string without modna= me prefix -# -# Strip the trailing padding bytes after extracting .modinfo to comply with -# what kmod expects to parse. +# use --dump-section to include non-allocated sections quiet_cmd_modules_builtin_modinfo =3D GEN $@ - cmd_modules_builtin_modinfo =3D $(cmd_objcopy); \ - sed -i 's/\x00\+$$/\x00/g' $@; \ - chmod -x $@ - -OBJCOPYFLAGS_modules.builtin.modinfo :=3D -j .modinfo -O binary + cmd_modules_builtin_modinfo =3D $(OBJCOPY) -O binary -j .modinfo --d= ump-section .modinfo=3D$@ $< $@.tmp; \ + rm -f $@.tmp; \ + sed -i 's/\x00\+$$/\x00/g' $@ =20 targets +=3D modules.builtin.modinfo modules.builtin.modinfo: vmlinux.unstripped FORCE --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7784E3B6BF2; Tue, 8 Sep 2026 20:57:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901046; cv=none; b=kUdwZ+1UM/Sdhb3TVp+h7WC/+sUgUTkAWp6IdCHMymXJrKk6SJWBxd4c738zten6amXcCWH/VmB27phqvViXWUhyJvSks/ExOpXA5gTdI/Z4ZmIOdaj3JuWEpDGkAasRwQTe4dqDglcpVTk99wqWwIxqehT2y4FETv5NZZ/f+P0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901046; c=relaxed/simple; bh=TtGzRcvdpH1o/Q/maX3eAqRdjLYh9rcQLyvEGgPc5lo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fmzrLWbM5CmXVmH+/FJIVrjcW4r/IK+oITWjAcA3MuuCYGh+IFnnAMGCbROSF1R3W2oEz6Xs4f9jVBpbCqHCxutSpZT4gzYiBsD+0fISIXpr6VFDHZxYnmUSfK9DP976Z5ojUXbrridtiDr0Jw88Ix5sJ5uKvYGo8Ela/tj+scU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EUt/Q2g+; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EUt/Q2g+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4FC6D1F00A3A; Tue, 8 Sep 2026 20:57:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788901045; bh=Zp+1QGtqTWMKHztfUixkIr/p7k1hkTe9DSbsity3M/E=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=EUt/Q2g+NKaQ6S9lFZONrRdJapaT1x9rVJ0A9T48zaQRh1S544VNiCfglDSAJxYOe 0VJfwfGqdWhSMhOtk3C+HRU0yiUsplApEb3u1NGmEb/A447ROZaiCvc7jGZJSxxQ0i 57CIJfVZLHcZ2u8R4p7iBirtj7QRoylDHEfSmUnJ25duqw5thHuFFDqwSgasODWmYe KhqeJ7XNBet3kWpmY8O6OQOUthcFVEZiFWdFZ0mhrigAf1m3KihCiOekibd8CjQekA /B+299FwQ65IAfJ02Mme2/7KTRNgDDpVdxacj6iHcv/EfqS1JiQXdrk8q1fk02QGli 4vfQMWnG3EfdQ== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:10 +0100 Subject: [PATCH 10/23] kbuild: cache list, composite object state per object Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-10-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=3457; i=ljs@kernel.org; h=from:subject:message-id; bh=TtGzRcvdpH1o/Q/maX3eAqRdjLYh9rcQLyvEGgPc5lo=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlJlf+Pln5qwZhy76pPI49jarLjh5M5ZvurHztdCMS SsP9jy71lHKwiDGxSArpsjy/Iv4/iCRsHmdF/zdYOawMoEMYeDiFICJ/HjByHAyt/PJ57JakwfH fQXXqjz6pHfwseSa+DMnwn5m//vOn83HyHB+Thzvs+ALT57z2jRyv/Uq6tktxSN/0PrlyzLbFT9 2nGEAAA== X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 When each object's command line is expanded, kbuild has to figure out on multiple occasions whether it's built-in or part of a module and which composite object it belongs to. This causes the time spent on each directory in the kernel tree to grow O(n^2) with its object count, which is especially problematic for instance in drivers/gpu/drm/amd/amdgpu with 310 objects. No-op builds (i.e. make -j $(nproc) when nothing has changed) are particularly impacted by this. Fix the issue by caching this data and looking it up instead of getting it over and over again. This has a particularly large impact on allmodconfig builds. Whole build, 128-thread Threadripper 9980X, best of N runs: before after delta ------------------------------- x86 defconfig, no-op make, gcc 0.94s 0.92s -0.02s (-2%) x86 defconfig, no-op make, clang 1.1s 1.1s -0.01s (-1%) x86 allmodconfig, no-op make, gcc 13.0s 12.3s -0.64s (-5%) x86 allmodconfig, no-op make, clang 13.8s 13.2s -0.64s (-5%) x86 allmodconfig, touch mm/vma.c, gcc 41.6s 40.9s -0.70s (-2%) x86 allmodconfig, touch mm/vma.c, clang 38.1s 37.6s -0.52s (-1%) Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- scripts/Makefile.build | 8 ++++++++ scripts/Makefile.lib | 7 +++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/scripts/Makefile.build b/scripts/Makefile.build index 4349108e75e1..2cabfe85b798 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -122,6 +122,14 @@ multi-obj-m :=3D $(addprefix $(obj)/, $(multi-obj-m)) subdir-ym :=3D $(addprefix $(obj)/, $(subdir-ym)) endif =20 +# Cache which list each object is in and which composite objects it belong= s to, +# once per object for $(part-of-builtin), $(part-of-module) and $(modname-= multi). +$(foreach o, $(real-obj-y) $(lib-y), $(eval part-of-builtin_$o :=3D y)) +$(foreach o, $(real-obj-m), $(eval part-of-module_$o :=3D y)) +$(foreach m, $(multi-obj-ym), \ + $(foreach o, $(call suffix-search, $m, .o, -objs -y -m), \ + $(eval modname-multi_$o +=3D $(m:.o=3D)))) + ifndef obj $(warning kbuild: Makefile.build is included improperly) endif diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib index 0a4fdd8bd975..2f447bc25e7b 100644 --- a/scripts/Makefile.lib +++ b/scripts/Makefile.lib @@ -2,8 +2,7 @@ =20 # Finds the multi-part object the current object will be linked into. # If the object belongs to two or more multi-part objects, list them all. -modname-multi =3D $(sort $(foreach m,$(multi-obj-ym),\ - $(if $(filter $*.o, $(call suffix-search, $m, .o, -objs -y -m)),$(m:.o= =3D)))) +modname-multi =3D $(sort $(modname-multi_$*.o)) =20 __modname =3D $(or $(modname-multi),$(basetarget)) =20 @@ -149,8 +148,8 @@ endif # If $(is-kernel-object) is 'y', this object will be linked to vmlinux or = modules is-kernel-object =3D $(or $(part-of-builtin),$(part-of-module)) =20 -part-of-builtin =3D $(if $(filter $(basename $@).o, $(real-obj-y) $(lib-y)= ),y) -part-of-module =3D $(if $(filter $(basename $@).o, $(real-obj-m)),y) +part-of-builtin =3D $(part-of-builtin_$(basename $@).o) +part-of-module =3D $(part-of-module_$(basename $@).o) quiet_modtag =3D $(if $(part-of-module),[M], ) =20 modkern_cflags =3D \ --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 661AC3B813E; Tue, 8 Sep 2026 20:57:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901058; cv=none; b=oMIBLMXMGJ4cq08qeGIFzDnAmm2mzcksgmL/iMVNYBMEsbNLr0ARt0mCC9sO/pAcKPWsNrccN4IBCJ0tk7PVxpnROrKmBQu0uAPsg4oCgFzWoQtCR8cDz/SBSwbEsyl96mDntOMGBns3+VHZ61x0R4V4Et1lmA28Jb1Eei/RSfU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901058; c=relaxed/simple; bh=3Cp789Qzg/2Uz15/C3u2Bw4I8Rl6HRSQYoS+pryen5Q=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=K/IMa10RgnypEwYCti83oxNy0rQA2OsPX7kZzxErXlqi7GxnwLC4DDfugQNf5KYBiIPHXqzhaYZhbQV0y7p/mxS+imS2PoelNCEkZ4D86iMl+Ubsdv9ZdDGlndwl/C26GxYyueVYeeXe5LBYCFeCMVVQuUt6zH7XN4lgwQmdHAc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NbW7qJuy; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NbW7qJuy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 912471F00A3D; Tue, 8 Sep 2026 20:57:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788901056; bh=9isrcnLmhtata8dZfOmdld+lea1z4WrKQed+1TerD+k=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=NbW7qJuyJW7QTa1ogakxC7UDLVMNhzkxVFN58FQfsXXQCPXvQZZxKqTBBmOXyOHNZ SR3Izq8zrRI5quNYfhunyRfOSOde6P3s2vA5Jb+ww/SghdxPz+dc6yMkmSxr0oTmtW L5OUVXLDjLrq/mhgQ/o2t/oTHRfp05xou+Yg7MZMxomtSL78+ayS+nwB9q1L/de9I9 xNoX8mfd5SU6BprTQ4UrgO+sXwZ3ugpCiE+9fKt8Ele7hqZy4A8/SSjf1D27v4hLb5 5jRsJfgiZokaQnHGv6tqjzJ8S4ltg5vlMmlTpJ60CMOVL4d3EFNvxJFRD8r9BbYxnC QvtOPqyGASJNw== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:11 +0100 Subject: [PATCH 11/23] kbuild: implement and use depcheck to check dependency timestamps Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-11-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=15831; i=ljs@kernel.org; h=from:subject:message-id; bh=3Cp789Qzg/2Uz15/C3u2Bw4I8Rl6HRSQYoS+pryen5Q=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlJl/jFCueNzrOTNkxZ9HnNb7diqdfaDsafMhgKXWb q8Th/3cjlIWBjEuBlkxRZbnX8T3B4mEzeu84O8GM4eVCWQIAxenAEzkiw7Df89PUdIdSqVvLy+r +lz5kYnz457y8EO7NNiOPH5wLUiwXYaR4TT7U0afd+eUZnJ/i7i2t/ChD18uW8G0izOKVhTcmlY /kRcA X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 Each object's .cmd file lists its header dependencies, which often consists of over a thousand dependencies. When little has changed in the tree, this is what make spends most of its time doing, spending over a second, single-threaded when parsing larger directory trees. It performs considerably more work that is actually necessary to get the job done. This can be done faster in C, so implement scripts/basic/depcheck to do so. It does as little work as possible, reading the .cmd files from a directory's targets and running stat on each dependency only a single time. It generates a fragment holding only the saved command line for a target whose dependencies all exist and are older than it, and the entire .cmd file for any other. That way, Makefile.build simply includes the fragment and the amount of work make has to do is significantly reduced when there is not much work to do. If the operation fails, kbuild falls back to using .cmd files. The result is exactly the same as before. For drivers/gpu/drm/amd/amdgpu in an allmodconfig tree the fragment is a fifth of the size of the .cmd files, make reads it in 10ms instead of 380ms, and the instance goes from 2.2s to 0.4s. Whole build, 128-thread Threadripper 9980X, best of N runs: before after delta ------------------------------- x86 defconfig, no-op make, gcc 0.92s 0.62s -0.29s (-32%) x86 defconfig, no-op make, clang 1.1s 0.78s -0.31s (-29%) x86 allmodconfig, no-op make, gcc 12.3s 11.2s -1.2s (-10%) x86 allmodconfig, no-op make, clang 13.2s 11.9s -1.3s (-10%) x86 allmodconfig, touch mm/vma.c, gcc 40.9s 40.6s -0.3s (-1%) x86 allmodconfig, touch mm/vma.c, clang 37.6s 36.5s -1.0s (-3%) Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- Makefile | 1 + scripts/Makefile.build | 14 +- scripts/basic/.gitignore | 1 + scripts/basic/Makefile | 2 +- scripts/basic/depcheck.c | 442 +++++++++++++++++++++++++++++++++++++++++++= ++++ 5 files changed, 458 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 85eb2fd026cb..56e56bea53c3 100644 --- a/Makefile +++ b/Makefile @@ -2239,6 +2239,7 @@ clean: $(clean-dirs) $(call cmd,rmfiles) @find . $(RCS_FIND_IGNORE) \ \( -name '*.[aios]' -o -name '*.rsi' -o -name '*.ko' -o -name '.*.cmd' \ + -o -name '.depcheck.mk' -o -name '.depcheck.mk.tmp' \ -o -name '*.ko.*' -o -name '*.o.thinlto.bc' \ -o -name '*.dtb' -o -name '*.dtbo' \ -o -name '*.dtb.S' -o -name '*.dtbo.S' \ diff --git a/scripts/Makefile.build b/scripts/Makefile.build index 2cabfe85b798..b9093b39cc2f 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -592,7 +592,19 @@ $(obj)/: $(if $(KBUILD_BUILTIN), $(targets-for-builtin= )) \ =20 existing-targets :=3D $(wildcard $(sort $(targets))) =20 --include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd) +cmd-files :=3D $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).= cmd) + +# depcheck checks timestamps first and outputs only saved command lines of +# up-to-date targets, falling back to .cmd files if it fails. +depcheck :=3D $(wildcard $(objtree)/scripts/basic/depcheck) + +ifneq ($(and $(depcheck),$(cmd-files)),) +ifeq ($(shell $(depcheck) $(obj)/.depcheck.mk $(cmd-files) && echo ok),ok) +cmd-files :=3D $(obj)/.depcheck.mk +endif +endif + +-include $(cmd-files) =20 # Create directories for object files if they do not exist obj-dirs :=3D $(sort $(patsubst %/,%, $(dir $(targets)))) diff --git a/scripts/basic/.gitignore b/scripts/basic/.gitignore index 07c195f605a1..761ee14f9477 100644 --- a/scripts/basic/.gitignore +++ b/scripts/basic/.gitignore @@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only +/depcheck /fixdep /randstruct.seed diff --git a/scripts/basic/Makefile b/scripts/basic/Makefile index fb8e2c38fbc7..ff98780e474d 100644 --- a/scripts/basic/Makefile +++ b/scripts/basic/Makefile @@ -2,7 +2,7 @@ # # fixdep: used to generate dependency information during build process =20 -hostprogs-always-y +=3D fixdep +hostprogs-always-y +=3D fixdep depcheck =20 # randstruct: the seed is needed before building the gcc-plugin or # before running a Clang kernel build. diff --git a/scripts/basic/depcheck.c b/scripts/basic/depcheck.c new file mode 100644 index 000000000000..5f9bbdff8803 --- /dev/null +++ b/scripts/basic/depcheck.c @@ -0,0 +1,442 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * depcheck - check the dependency timestamps of a directory's targets so = that + * make reads only what it needs from their .cmd files. + * + * fixdep writes a .cmd file as: + * + * savedcmd_dir/foo.o :=3D + * + * source_dir/foo.o :=3D dir/foo.c + * + * deps_dir/foo.o :=3D \ + * include/linux/bar.h \ + * $(wildcard include/config/BAZ) \ + * + * dir/foo.o: $(deps_dir/foo.o) + * + * $(deps_dir/foo.o): + * + * and kbuild may append rules of its own after that, such as one making t= he + * target depend on objtool. + * + * For a target that exists and is newer than every dependency listed, mak= e can + * have nothing to do with the list, so it is left out and only what prece= des + * and follows it is passed on; for anything else the .cmd file is passed = on in + * full. + * + * Usage: depcheck <.cmd files...> + * + * The output is a makefile fragment to include in place of the .cmd files= ; a + * non-zero exit status means the caller should include those instead. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +/* What fixdep writes, see above. */ +#define DEPS_PREFIX "deps_" +#define DEPS_RULE_PREFIX "$(" DEPS_PREFIX +#define RULE_SUFFIX ":" +#define LINE_CONTINUATION " \\" +#define WILDCARD_OPEN "$(wildcard " +#define WILDCARD_CLOSE ")" +#define CMD_SUFFIX ".cmd" + +/* A line of a .cmd file, without its newline. */ +struct line { + const char *text; + size_t len; +}; + +static bool is_blank(char chr) +{ + return chr =3D=3D ' ' || chr =3D=3D '\t'; +} + +static bool str_ends_with(const char *str, const char *suffix) +{ + const size_t len =3D strlen(str), suffix_len =3D strlen(suffix); + + return len >=3D suffix_len && !strcmp(str + len - suffix_len, suffix); +} + +static bool line_starts_with(const struct line *line, const char *prefix) +{ + const size_t len =3D strlen(prefix); + + return line->len >=3D len && !memcmp(line->text, prefix, len); +} + +static bool line_ends_with(const struct line *line, const char *suffix) +{ + const size_t len =3D strlen(suffix); + + return line->len >=3D len && + !memcmp(line->text + line->len - len, suffix, len); +} + +static bool line_is_blank(const struct line *line) +{ + size_t i; + + for (i =3D 0; i < line->len; i++) + if (!is_blank(line->text[i])) + return false; + + return true; +} + +/* Whether the line ends in " \", continuing the list on the next line. */ +static bool line_is_continued(const struct line *line) +{ + return line_ends_with(line, LINE_CONTINUATION); +} + +static void line_strip_continuation(struct line *line) +{ + if (line_is_continued(line)) + line->len -=3D strlen(LINE_CONTINUATION); +} + +static void line_trim(struct line *line) +{ + while (line->len && is_blank(line->text[0])) { + line->text++; + line->len--; + } + while (line->len && is_blank(line->text[line->len - 1])) + line->len--; +} + +/* Take the next line out of [*pos, end); false once there are none left. = */ +static bool next_line(const char **pos, const char *end, struct line *line) +{ + const char *newline; + + if (*pos >=3D end) + return false; + + newline =3D memchr(*pos, '\n', end - *pos); + line->text =3D *pos; + line->len =3D (newline ? newline : end) - *pos; + *pos =3D newline ? newline + 1 : end; + + return true; +} + +/* Describes a dependency file. */ +struct dep { + struct hlist_node hnode; + struct timespec mtime; + bool exists; + char path[]; +}; + +static HASHTABLE_DEFINE(dep_table, 1U << 16); + +static const struct dep *lookup_dep(const char *path) +{ + const unsigned int key =3D hash_str(path); + struct dep *dep; + struct stat st; + + hash_for_each_possible(dep_table, dep, hnode, key) { + if (!strcmp(dep->path, path)) + return dep; + } + + dep =3D xmalloc(sizeof(*dep) + strlen(path) + 1); + strcpy(dep->path, path); + dep->exists =3D !stat(path, &st); + if (dep->exists) + dep->mtime =3D st.st_mtim; + hash_add(dep_table, &dep->hnode, key); + + return dep; +} + +/* Strictly newer, as make compares timestamps. */ +static bool newer(const struct timespec *time_a, const struct timespec *ti= me_b) +{ + if (time_a->tv_sec !=3D time_b->tv_sec) + return time_a->tv_sec > time_b->tv_sec; + + return time_a->tv_nsec > time_b->tv_nsec; +} + +/* + * $(wildcard include/config/FOO) is a prerequisite only while FOO is set: + * unwrap it and say that it is optional. + */ +static bool line_unwrap_wildcard(struct line *line) +{ + if (!line_starts_with(line, WILDCARD_OPEN) || + !line_ends_with(line, WILDCARD_CLOSE)) + return false; + + line->text +=3D strlen(WILDCARD_OPEN); + line->len -=3D strlen(WILDCARD_OPEN) + strlen(WILDCARD_CLOSE); + + return true; +} + +/* fixdep doubles '$' and escapes '#' in a path: undo that into path[]. */ +static bool unescape_path(const struct line *line, char *path, size_t size) +{ + size_t i, out_len =3D 0; + + if (line->len >=3D size) + return false; + + for (i =3D 0; i < line->len; i++) { + const char chr =3D line->text[i]; + const char next_chr =3D i + 1 < line->len ? line->text[i + 1] : '\0'; + + if ((chr =3D=3D '$' && next_chr =3D=3D '$') || (chr =3D=3D '\\' && next_= chr =3D=3D '#')) + i++; + path[out_len++] =3D line->text[i]; + } + path[out_len] =3D '\0'; + + return true; +} + +/* Is the path contained in line older than the target? */ +static bool dep_is_fresh(struct line line, const struct timespec *target) +{ + char path[PATH_MAX]; + const struct dep *dep; + bool optional; + + line_strip_continuation(&line); + line_trim(&line); + if (!line.len) + return false; + + optional =3D line_unwrap_wildcard(&line); + if (!line.len || !unescape_path(&line, path, sizeof(path))) + return false; + + dep =3D lookup_dep(path); + if (!dep->exists) + return optional; + + return !newer(&dep->mtime, target); +} + +/* + * Parse a dependency list which consists of one file per line and determi= ne if + * all dependencies are 'fresh', i.e. older than the target. + */ +static bool deps_are_fresh(const char *pos, const char *end, + const struct timespec *target) +{ + struct line line; + + while (next_line(&pos, end, &line)) { + if (line_is_blank(&line)) + return true; + if (!dep_is_fresh(line, target)) + return false; + if (!line_is_continued(&line)) + return true; + } + + return true; +} + +/* + * Find the "deps_" line in the input. + * + * On success returns true and *deps_off is set to its offset and *list is= set + * to the list's own lines start or NULL if the line is not continued. + * + * Otherwise returns false if the prefix cannot be found. + */ +static bool find_deps(const char *buf, size_t len, size_t *deps_off, + const char **list) +{ + const char *pos =3D buf, *end =3D buf + len; + struct line line; + + while (next_line(&pos, end, &line)) { + if (!line_starts_with(&line, DEPS_PREFIX)) + continue; + + *deps_off =3D line.text - buf; + *list =3D line_is_continued(&line) ? pos : NULL; + return true; + } + + return false; +} + +/* + * Is this the target of a .cmd file, i.e. 'dir/.name.cmd names dir/name.'? + */ +static bool target_of(const char *cmd_path, char *target, size_t size) +{ + const char *slash =3D strrchr(cmd_path, '/'); + const char *base =3D slash ? slash + 1 : cmd_path; + const size_t dir_len =3D base - cmd_path; + size_t name_len; + + if (base[0] !=3D '.' || !str_ends_with(base, CMD_SUFFIX)) + return false; + + name_len =3D strlen(base) - strlen(".") - strlen(CMD_SUFFIX); + if (!name_len) + return false; + + return snprintf(target, size, "%.*s%.*s", (int)dir_len, cmd_path, + (int)name_len, base + 1) < (int)size; +} + +/* + * Does the .cmd file's target exists and is it newer than everything in i= ts + * dependency list? + * + * Returns true if so and sets *deps_off to the start of the list, otherwi= se + * returns false. + */ +static bool target_is_fresh(const char *cmd_path, const char *buf, size_t = len, + size_t *deps_off) +{ + char target[PATH_MAX]; + struct stat st; + const char *list; + + if (!target_of(cmd_path, target, sizeof(target)) || stat(target, &st)) + return false; + if (!find_deps(buf, len, deps_off, &list)) + return false; + if (!list) + return true; + + return deps_are_fresh(list, buf + len, &st.st_mtim); +} + +static char *read_file(const char *path, size_t *len) +{ + FILE *file =3D fopen(path, "r"); + struct stat st; + char *buf; + size_t nr_read =3D 0; + + if (!file) + return NULL; + if (fstat(fileno(file), &st)) { + fclose(file); + return NULL; + } + + buf =3D xmalloc(st.st_size + 1); + while (nr_read < (size_t)st.st_size) { + const size_t chunk =3D fread(buf + nr_read, 1, st.st_size - nr_read, fil= e); + + if (!chunk) + break; + nr_read +=3D chunk; + } + fclose(file); + + buf[nr_read] =3D '\0'; + *len =3D nr_read; + return buf; +} + +/* + * Find the end of the dependency block: the offset just past its closing + * "$(deps_x):" line. Anything after that was appended by kbuild and is no= t for + * us to judge. Returns len if the line cannot be found. + */ +static size_t deps_block_end(const char *buf, size_t len, size_t deps_off) +{ + const char *pos =3D buf + deps_off, *end =3D buf + len; + struct line line; + + while (next_line(&pos, end, &line)) { + if (line_starts_with(&line, DEPS_RULE_PREFIX) && + line_ends_with(&line, RULE_SUFFIX)) + return pos - buf; + } + + return len; +} + +/* The target is fresh: pass on everything but the dependency block. */ +static void write_without_deps(FILE *out, const char *buf, size_t len, + size_t deps_off) +{ + const size_t tail_off =3D deps_block_end(buf, len, deps_off); + + fwrite(buf, 1, deps_off, out); + fputc('\n', out); + fwrite(buf + tail_off, 1, len - tail_off, out); +} + +/* + * Generate a fragment for make from one .cmd file - without the dependency + * block if the target is fresh, otherwise all of it. + */ +static void process(FILE *out, const char *cmd_path) +{ + size_t len, deps_off; + char *buf; + + buf =3D read_file(cmd_path, &len); + if (!buf) + return; + + if (target_is_fresh(cmd_path, buf, len, &deps_off)) + write_without_deps(out, buf, len, deps_off); + else + fwrite(buf, 1, len, out); + + free(buf); +} + +int main(int argc, char **argv) +{ + char tmp_path[PATH_MAX]; + FILE *out; + int i; + + if (argc < 2) { + fprintf(stderr, "usage: %s <.cmd files...>\n", argv[0]); + return 1; + } + + if (snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", argv[1]) >=3D + (int)sizeof(tmp_path)) { + fprintf(stderr, "%s: path too long\n", argv[1]); + return 1; + } + + out =3D fopen(tmp_path, "w"); + if (!out) { + perror(tmp_path); + return 1; + } + + for (i =3D 2; i < argc; i++) + process(out, argv[i]); + + if (fclose(out) || rename(tmp_path, argv[1])) { + perror(argv[1]); + unlink(tmp_path); + return 1; + } + + return 0; +} --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9E7B6376A1E; Tue, 8 Sep 2026 20:57:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901069; cv=none; b=l/WSAx7LVdGXUs5Z8D7BAZ7DNPUC8mVmE03j6I62kkV7RrJxtQTGpb+dUiWVVSmHMGd5aYppEhifsZnpf0xBivX66kK14oVVCw6f6dhGZIruwHywCdupKOEsnBGxXni9atqND3toAoh3OTDFtYydxAqOI8yoh7oIkxZDQoMTpjM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901069; c=relaxed/simple; bh=r7VENpndNaAt1k1zEv1dPwhpRhgGzMHgAB3PtXYAN2s=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZjLbnTnVJXcsTskvDGPCQknRP6vgqkCxWobce8j7VhhiMFbiZnwOtxDSNv9oWTDJKAAPiJEe5e/iNu4q+sy3zuq9nJ9zWVri+x615W6NixCIgBgq4L29CL94wgMLtY0/j/Rgk6NjFQ+j2BKL9vu41uWFdKQ4xoplkHvpIgzIKV0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iHbGsrn/; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iHbGsrn/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C1E1D1F00A3A; Tue, 8 Sep 2026 20:57:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788901067; bh=PernGqwpWrFsOgiWh9Bsze8TcH7Js8itUUkLvIDwUOA=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=iHbGsrn/YzSQmh5Z0FnT8moN1ScmCdpZzO/YQK7yfXBN8SnW8eQG2YNhpuWWPs2sy XdmDs9mhrc5esMv+8qjenNvsT5oHpCFjYJX3P66VM2Kt1Eq3E3yDiu+NUiMqPMVqYc U+5SkM0Cj5GIN9/3s0OzMIHzLqp3QxPjOSmweo8Xf/yIgmekZF8dWZhF0/a5NIHlLB wx0Ht8AebSHq++w2Hr9z6eUJJNupIUkO2H9g7l2/XIsbFT+ymwU3Rg18Y/DHSDHRim Y6lj8z/4860zEXYEJCUnXJvyUNCLsU4mNbuuHAkSfdGz4RHRlpjpQNiApsoYN0EeN0 CaGkHT4lUr3nQ== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:12 +0100 Subject: [PATCH 12/23] kbuild: avoid re-running compiler and linker probes Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-12-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=19050; i=ljs@kernel.org; h=from:subject:message-id; bh=r7VENpndNaAt1k1zEv1dPwhpRhgGzMHgAB3PtXYAN2s=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlJmziVj8j03lLI4t7iplPbZzM6/Em+bzwSfqODqyH 5+xlWHqKGVhEONikBVTZHn+RXx/kEjYvM4L/m4wc1iZQIYwcHEKwETEPzP8sw0Tdn17z9g6UOzA kTVai7YofZUwnMfgOOPCk2MWAi9cYxgZbgSF+1jG35G4/zfOaHXx2W+SMRHHRYMWP7wgXXV+cX0 IKwA= X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 Each kernel make invocation begins with ~30 compiler and linker runs each of which performs duplicate probe for a number of compiler and linker options. This is useless work - the compiler and its version is known, so use these to determine which options are available, once. A convention already exists for this - CC_HAS_xxx, LD_HAS_xxx in Kconfig files (for example, CC_HAS_COUNTED_BY), so convert these probes to Kconfig options where appropriate. With gcc and clang, defconfig and allmodconfig, the recorded command lines are unchanged, a build with nothing to do rebuilds nothing and W=3D1 continues to function correctly. Doing this improves all builds, but has a particularly positive impact on no-op builds (builds where nothing has changed). Whole build, 128-thread Threadripper 9980X, best of N runs: before after delta ------------------------------- x86 defconfig, no-op make, gcc 0.62s 0.46s -0.17s (-27%) x86 defconfig, no-op make, clang 0.78s 0.53s -0.25s (-32%) x86 defconfig, touch mm/vma.c, gcc 8.7s 8.5s -0.17s (-2%) x86 defconfig, touch mm/vma.c, clang 7.9s 7.6s -0.25s (-3%) x86 defconfig, clean, clang 27.0s 26.8s -0.26s (-1%) x86 allmodconfig, no-op make, gcc 11.2s 11.0s -0.18s (-2%) x86 allmodconfig, no-op make, clang 11.9s 11.6s -0.28s (-2%) x86 allmodconfig, touch mm/vma.c, clang 36.5s 36.2s -0.31s (-1%) Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- Makefile | 23 +++++------ arch/arm64/kernel/pi/Makefile | 2 +- arch/riscv/kernel/pi/Makefile | 2 +- arch/x86/Kconfig | 17 ++++++++ arch/x86/Makefile | 12 +++--- drivers/firmware/efi/libstub/Makefile | 2 +- init/Kconfig | 77 +++++++++++++++++++++++++++++++= ++++ scripts/Makefile.warn | 28 ++++++------- 8 files changed, 128 insertions(+), 35 deletions(-) diff --git a/Makefile b/Makefile index 56e56bea53c3..088e901f0a72 100644 --- a/Makefile +++ b/Makefile @@ -946,8 +946,8 @@ KBUILD_RUSTFLAGS +=3D -Coverflow-checks=3D$(if $(CONFIG= _RUST_OVERFLOW_CHECKS),y,n) ifdef CONFIG_CC_IS_GCC # gcc-10 renamed --param=3Dallow-store-data-races=3D0 to # -fno-allow-store-data-races. -KBUILD_CFLAGS +=3D $(call cc-option,--param=3Dallow-store-data-races=3D0) -KBUILD_CFLAGS +=3D $(call cc-option,-fno-allow-store-data-races) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_ALLOW_STORE_DATA_RACES_PARAM),--pa= ram=3Dallow-store-data-races=3D0) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_NO_ALLOW_STORE_DATA_RACES),-fno-al= low-store-data-races) endif =20 ifdef CONFIG_READABLE_ASM @@ -1011,18 +1011,18 @@ endif endif =20 # Explicitly clear padding bits during variable initialization -KBUILD_CFLAGS +=3D $(call cc-option,-fzero-init-padding-bits=3Dall) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_ZERO_INIT_PADDING_BITS),-fzero-ini= t-padding-bits=3Dall) =20 # While VLAs have been removed, GCC produces unreachable stack probes # for the randomize_kstack_offset feature. Disable it for all compilers. -KBUILD_CFLAGS +=3D $(call cc-option, -fno-stack-clash-protection) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_NO_STACK_CLASH_PROTECTION),-fno-st= ack-clash-protection) =20 # Get details on warnings generated due to GCC value tracking. -KBUILD_CFLAGS +=3D $(call cc-option, -fdiagnostics-show-context=3D2) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_DIAGNOSTICS_SHOW_CONTEXT),-fdiagno= stics-show-context=3D2) =20 # Show inlining notes for __attribute__((warning/error)) call chains. # GCC supports this unconditionally while Clang 23+ provides a flag. -KBUILD_CFLAGS +=3D $(call cc-option, -fdiagnostics-show-inlining-chain) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_DIAGNOSTICS_SHOW_INLINING_CHAIN),-= fdiagnostics-show-inlining-chain) =20 # Clear used registers at func exit (to reduce data lifetime and ROP gadge= ts). ifdef CONFIG_ZERO_CALL_USED_REGS @@ -1033,7 +1033,7 @@ ifdef CONFIG_FUNCTION_TRACER ifdef CONFIG_FTRACE_MCOUNT_USE_CC CC_FLAGS_FTRACE +=3D -mrecord-mcount ifdef CONFIG_HAVE_NOP_MCOUNT - ifeq ($(call cc-option-yn, -mnop-mcount),y) + ifdef CONFIG_CC_HAS_MNOP_MCOUNT CC_FLAGS_FTRACE +=3D -mnop-mcount CC_FLAGS_USING +=3D -DCC_USING_NOP_MCOUNT endif @@ -1051,8 +1051,7 @@ ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT endif endif ifdef CONFIG_HAVE_FENTRY - # s390-linux-gnu-gcc did not support -mfentry until gcc-9. - ifeq ($(call cc-option-yn, -mfentry),y) + ifdef CONFIG_CC_HAS_MFENTRY CC_FLAGS_FTRACE +=3D -mfentry CC_FLAGS_USING +=3D -DCC_USING_FENTRY endif @@ -1160,7 +1159,7 @@ NOSTDINC_FLAGS +=3D -nostdinc # the kernel uses only C99 flexible arrays for dynamically sized trailing # arrays. Enforce this for everything that may examine structure sizes and # perform bounds checking. -KBUILD_CFLAGS +=3D $(call cc-option, -fstrict-flex-arrays=3D3) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_STRICT_FLEX_ARRAYS),-fstrict-flex-= arrays=3D3) =20 # disable invalid "can't wrap" optimizations for signed / pointers KBUILD_CFLAGS +=3D -fno-strict-overflow @@ -1240,11 +1239,11 @@ LDFLAGS_vmlinux +=3D --build-id=3Dsha1 # COMDAT-deduplicated sections. Use --force-group-allocation to resolve th= ese # groups when linking modules. The option is available from ld.bfd 2.29 and # ld.lld 19.1.0. -KBUILD_LDFLAGS_MODULE +=3D $(call ld-option,--force-group-allocation) +KBUILD_LDFLAGS_MODULE +=3D $(if $(CONFIG_LD_HAS_FORCE_GROUP_ALLOCATION),--= force-group-allocation) =20 KBUILD_LDFLAGS +=3D -z noexecstack ifeq ($(CONFIG_LD_IS_BFD),y) -KBUILD_LDFLAGS +=3D $(call ld-option,--no-warn-rwx-segments) +KBUILD_LDFLAGS +=3D $(if $(CONFIG_LD_HAS_NO_WARN_RWX_SEGMENTS),--no-warn-r= wx-segments) endif =20 ifeq ($(CONFIG_STRIP_ASM_SYMS),y) diff --git a/arch/arm64/kernel/pi/Makefile b/arch/arm64/kernel/pi/Makefile index be92d73c25b2..5aa8dffe492b 100644 --- a/arch/arm64/kernel/pi/Makefile +++ b/arch/arm64/kernel/pi/Makefile @@ -9,7 +9,7 @@ KBUILD_CFLAGS :=3D $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFL= AGS)) -fpie \ -include $(srctree)/include/linux/hidden.h \ -D__DISABLE_EXPORTS -ffreestanding -D__NO_FORTIFY \ -fno-asynchronous-unwind-tables -fno-unwind-tables \ - $(call cc-option,-fno-addrsig) + $(if $(CONFIG_CC_HAS_NO_ADDRSIG),-fno-addrsig) =20 # this code may run with the MMU off so disable unaligned accesses CFLAGS_map_range.o +=3D -mstrict-align diff --git a/arch/riscv/kernel/pi/Makefile b/arch/riscv/kernel/pi/Makefile index bc098edac898..e0ed7ca7e347 100644 --- a/arch/riscv/kernel/pi/Makefile +++ b/arch/riscv/kernel/pi/Makefile @@ -8,7 +8,7 @@ KBUILD_CFLAGS :=3D $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFL= AGS)) -fpie \ -include $(srctree)/include/linux/hidden.h \ -D__DISABLE_EXPORTS -ffreestanding \ -fno-asynchronous-unwind-tables -fno-unwind-tables \ - $(call cc-option,-fno-addrsig) + $(if $(CONFIG_CC_HAS_NO_ADDRSIG),-fno-addrsig) =20 # Disable LTO KBUILD_CFLAGS :=3D $(filter-out $(CC_FLAGS_LTO), $(KBUILD_CFLAGS)) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 15fd9ec5ecac..be0624fee27a 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -2364,6 +2364,23 @@ config CC_HAS_KCFI_ARITY def_bool $(cc-option,-fsanitize=3Dkcfi -fsanitize-kcfi-arity) depends on CC_IS_CLANG && !RUST =20 +config CC_HAS_INDIRECT_BRANCH_CS_PREFIX + def_bool $(cc-option,-mindirect-branch-cs-prefix) + +config CC_HAS_CF_PROTECTION_NONE + def_bool $(cc-option,-fcf-protection=3Dnone) + +# with jump tables off: the compilers use NOTRACK for them, which kernel I= BT +# does not allow +config CC_HAS_CF_PROTECTION_BRANCH + def_bool $(cc-option,-fcf-protection=3Dbranch -fno-jump-tables) + +config CC_HAS_FALIGN_JUMPS + def_bool $(cc-option,-falign-jumps=3D1) + +config CC_HAS_FALIGN_LOOPS + def_bool $(cc-option,-falign-loops=3D1) + config FUNCTION_PADDING_CFI int default 59 if FUNCTION_ALIGNMENT_64B diff --git a/arch/x86/Makefile b/arch/x86/Makefile index 598f178102ee..29bb515df9f6 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -20,7 +20,7 @@ ifdef CONFIG_CC_IS_CLANG RETPOLINE_CFLAGS :=3D -mretpoline-external-thunk RETPOLINE_VDSO_CFLAGS :=3D -mretpoline endif -RETPOLINE_CFLAGS +=3D $(call cc-option,-mindirect-branch-cs-prefix) +RETPOLINE_CFLAGS +=3D $(if $(CONFIG_CC_HAS_INDIRECT_BRANCH_CS_PREFIX),-min= direct-branch-cs-prefix) =20 ifdef CONFIG_MITIGATION_RETHUNK RETHUNK_CFLAGS :=3D -mfunction-return=3Dthunk-extern @@ -52,7 +52,7 @@ REALMODE_CFLAGS :=3D $(CC_FLAGS_DIALECT) -m16 -g -Os \ -DDISABLE_BRANCH_PROFILING -D__DISABLE_EXPORTS \ -Wall -Wstrict-prototypes -march=3Di386 -mregparm=3D3 \ -fno-strict-aliasing -fomit-frame-pointer -fno-pic \ - -mno-mmx -mno-sse $(call cc-option,-fcf-protection=3Dnone) + -mno-mmx -mno-sse $(if $(CONFIG_CC_HAS_CF_PROTECTION_NONE),-fcf-prote= ction=3Dnone) =20 REALMODE_CFLAGS +=3D -ffreestanding REALMODE_CFLAGS +=3D -fno-stack-protector @@ -99,10 +99,10 @@ ifeq ($(CONFIG_X86_KERNEL_IBT),y) # # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D104816 # -KBUILD_CFLAGS +=3D $(call cc-option,-fcf-protection=3Dbranch -fno-jump-tab= les) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_CF_PROTECTION_BRANCH),-fcf-protect= ion=3Dbranch -fno-jump-tables) KBUILD_RUSTFLAGS +=3D -Zcf-protection=3Dbranch $(if $(call rustc-min-versi= on,109300),-Cjump-tables=3Dn,-Zno-jump-tables) else -KBUILD_CFLAGS +=3D $(call cc-option,-fcf-protection=3Dnone) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_CF_PROTECTION_NONE),-fcf-protectio= n=3Dnone) endif =20 ifeq ($(CONFIG_X86_32),y) @@ -138,10 +138,10 @@ else KBUILD_CFLAGS +=3D -m64 =20 # Align jump targets to 1 byte, not the default 16 bytes: - KBUILD_CFLAGS +=3D $(call cc-option,-falign-jumps=3D1) + KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_FALIGN_JUMPS),-falign-jump= s=3D1) =20 # Pack loops tightly as well: - KBUILD_CFLAGS +=3D $(call cc-option,-falign-loops=3D1) + KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_FALIGN_LOOPS),-falign-loop= s=3D1) =20 # Don't autogenerate traditional x87 instructions KBUILD_CFLAGS +=3D -mno-80387 diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/l= ibstub/Makefile index 77a2b2d74f3f..80058bbddaf5 100644 --- a/drivers/firmware/efi/libstub/Makefile +++ b/drivers/firmware/efi/libstub/Makefile @@ -40,7 +40,7 @@ KBUILD_CFLAGS :=3D $(subst $(CC_FLAGS_FTRACE),,$(cflags= -y)) \ -D__NO_FORTIFY \ -ffreestanding \ -fno-stack-protector \ - $(call cc-option,-fno-addrsig) \ + $(if $(CONFIG_CC_HAS_NO_ADDRSIG),-fno-addrsig) \ -D__DISABLE_EXPORTS =20 # diff --git a/init/Kconfig b/init/Kconfig index 8583d9f06c52..3c92c87254a3 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -144,6 +144,44 @@ config CC_HAS_ASSUME config CC_HAS_NO_PROFILE_FN_ATTR def_bool $(success,echo '__attribute__((no_profile_instrument_function)) = int x();' | $(CC) -x c - -c -o /dev/null -Werror) =20 +config CC_HAS_ZERO_INIT_PADDING_BITS + def_bool $(cc-option,-fzero-init-padding-bits=3Dall) + +config CC_HAS_NO_STACK_CLASH_PROTECTION + def_bool $(cc-option,-fno-stack-clash-protection) + +config CC_HAS_NO_ADDRSIG + def_bool $(cc-option,-fno-addrsig) + +config CC_HAS_DIAGNOSTICS_SHOW_CONTEXT + def_bool $(cc-option,-fdiagnostics-show-context=3D2) + +config CC_HAS_DIAGNOSTICS_SHOW_INLINING_CHAIN + def_bool $(cc-option,-fdiagnostics-show-inlining-chain) + +config CC_HAS_STRICT_FLEX_ARRAYS + def_bool $(cc-option,-fstrict-flex-arrays=3D3) + +# gcc-10 renamed --param=3Dallow-store-data-races=3D0 to -fno-allow-store-= data-races +config CC_HAS_ALLOW_STORE_DATA_RACES_PARAM + def_bool CC_IS_GCC && $(cc-option,--param=3Dallow-store-data-races=3D0) + +config CC_HAS_NO_ALLOW_STORE_DATA_RACES + def_bool CC_IS_GCC && $(cc-option,-fno-allow-store-data-races) + +config CC_HAS_MNOP_MCOUNT + def_bool $(cc-option,-mnop-mcount) + +# s390-linux-gnu-gcc did not support -mfentry until gcc-9. +config CC_HAS_MFENTRY + def_bool $(cc-option,-mfentry) + +config LD_HAS_FORCE_GROUP_ALLOCATION + def_bool $(ld-option,--force-group-allocation) + +config LD_HAS_NO_WARN_RWX_SEGMENTS + def_bool $(ld-option,--no-warn-rwx-segments) + config CC_HAS_COUNTED_BY bool # clang needs to be at least 20.1.0 to avoid potential crashes @@ -1006,6 +1044,45 @@ config CC_STRINGOP_OVERFLOW bool default y if CC_IS_GCC && !CC_NO_STRINGOP_OVERFLOW =20 +config CC_HAS_WNO_ADDRESS_OF_PACKED_MEMBER + def_bool $(cc-option,-Wno-address-of-packed-member) + +config CC_HAS_WNO_FORMAT_OVERFLOW_NON_KPRINTF + def_bool CC_IS_CLANG && $(cc-option,-Wno-format-overflow-non-kprintf) + +config CC_HAS_WNO_FORMAT_TRUNCATION_NON_KPRINTF + def_bool CC_IS_CLANG && $(cc-option,-Wno-format-truncation-non-kprintf) + +config CC_HAS_WNO_DEFAULT_CONST_INIT_UNSAFE + def_bool CC_IS_CLANG && $(cc-option,-Wno-default-const-init-unsafe) + +config CC_HAS_WNO_DANGLING_POINTER + def_bool $(cc-option,-Wno-dangling-pointer) + +config CC_HAS_WVLA_LARGER_THAN + def_bool $(cc-option,-Wvla-larger-than=3D1) + +config CC_HAS_WSTRINGOP_OVERFLOW + def_bool $(cc-option,-Wstringop-overflow) + +config CC_HAS_WNO_UNTERMINATED_STRING_INITIALIZATION + def_bool $(cc-option,-Wno-unterminated-string-initialization) + +config CC_HAS_WERROR_DESIGNATED_INIT + def_bool $(cc-option,-Werror=3Ddesignated-init) + +config CC_HAS_WENUM_CONVERSION + def_bool $(cc-option,-Wenum-conversion) + +config CC_HAS_WNO_PACKED_NOT_ALIGNED + def_bool $(cc-option,-Wno-packed-not-aligned) + +config CC_HAS_WNO_FORMAT_OVERFLOW + def_bool $(cc-option,-Wno-format-overflow) + +config CC_HAS_WNO_STRINGOP_TRUNCATION + def_bool $(cc-option,-Wno-stringop-truncation) + # # For architectures that know their GCC __int128 support is sound # diff --git a/scripts/Makefile.warn b/scripts/Makefile.warn index 35af7d6c6d18..83d274fcbfaf 100644 --- a/scripts/Makefile.warn +++ b/scripts/Makefile.warn @@ -17,7 +17,7 @@ KBUILD_CFLAGS +=3D -Werror=3Dstrict-prototypes KBUILD_CFLAGS +=3D -Wno-format-security KBUILD_CFLAGS +=3D -Wno-trigraphs KBUILD_CFLAGS +=3D -Wno-frame-address -KBUILD_CFLAGS +=3D $(call cc-option, -Wno-address-of-packed-member) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_WNO_ADDRESS_OF_PACKED_MEMBER),-Wno= -address-of-packed-member) KBUILD_CFLAGS +=3D -Wmissing-declarations KBUILD_CFLAGS +=3D -Wmissing-prototypes =20 @@ -30,8 +30,8 @@ KBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) +=3D -Wno-arra= y-bounds ifdef CONFIG_CC_IS_CLANG # Clang checks for overflow/truncation with '%p', while GCC does not: # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D111219 -KBUILD_CFLAGS +=3D $(call cc-option, -Wno-format-overflow-non-kprintf) -KBUILD_CFLAGS +=3D $(call cc-option, -Wno-format-truncation-non-kprintf) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_WNO_FORMAT_OVERFLOW_NON_KPRINTF),-= Wno-format-overflow-non-kprintf) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_WNO_FORMAT_TRUNCATION_NON_KPRINTF)= ,-Wno-format-truncation-non-kprintf) =20 # Clang may emit a warning when a const variable, such as the dummy variab= les # in typecheck(), or const member of an aggregate type are not initialized, @@ -43,7 +43,7 @@ KBUILD_CFLAGS +=3D $(call cc-option, -Wno-format-truncati= on-non-kprintf) # disabled with this same switch, there should not be too much coverage lo= st # because -Wuninitialized will still flag when an uninitialized const vari= able # is used. -KBUILD_CFLAGS +=3D $(call cc-option, -Wno-default-const-init-unsafe) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_WNO_DEFAULT_CONST_INIT_UNSAFE),-Wn= o-default-const-init-unsafe) else =20 # gcc inanely warns about local variables called 'main' @@ -54,7 +54,7 @@ endif KBUILD_CFLAGS +=3D -Wno-type-limits =20 # These result in bogus false positives -KBUILD_CFLAGS +=3D $(call cc-option, -Wno-dangling-pointer) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_WNO_DANGLING_POINTER),-Wno-danglin= g-pointer) =20 # Stack Variable Length Arrays (VLAs) must not be used in the kernel. # Function array parameters should, however, be usable, but -Wvla will @@ -62,7 +62,7 @@ KBUILD_CFLAGS +=3D $(call cc-option, -Wno-dangling-pointe= r) # types, so depend on GCC for now to keep stack VLAs out of the tree. # https://github.com/llvm/llvm-project/issues/57098 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D98217 -KBUILD_CFLAGS +=3D $(call cc-option,-Wvla-larger-than=3D1) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_WVLA_LARGER_THAN),-Wvla-larger-tha= n=3D1) =20 # disable pointer signed / unsigned warnings in gcc 4.0 KBUILD_CFLAGS +=3D -Wno-pointer-sign @@ -73,11 +73,11 @@ KBUILD_CFLAGS +=3D -Wno-pointer-sign KBUILD_CFLAGS +=3D -Wcast-function-type =20 # Currently, disable -Wstringop-overflow for GCC 11, globally. -KBUILD_CFLAGS-$(CONFIG_CC_NO_STRINGOP_OVERFLOW) +=3D $(call cc-option, -Wn= o-stringop-overflow) -KBUILD_CFLAGS-$(CONFIG_CC_STRINGOP_OVERFLOW) +=3D $(call cc-option, -Wstri= ngop-overflow) +KBUILD_CFLAGS-$(CONFIG_CC_NO_STRINGOP_OVERFLOW) +=3D $(if $(CONFIG_CC_HAS_= WSTRINGOP_OVERFLOW),-Wno-stringop-overflow) +KBUILD_CFLAGS-$(CONFIG_CC_STRINGOP_OVERFLOW) +=3D $(if $(CONFIG_CC_HAS_WST= RINGOP_OVERFLOW),-Wstringop-overflow) =20 # Currently, disable -Wunterminated-string-initialization as broken -KBUILD_CFLAGS +=3D $(call cc-option, -Wno-unterminated-string-initializati= on) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_WNO_UNTERMINATED_STRING_INITIALIZA= TION),-Wno-unterminated-string-initialization) =20 # The allocators already balk at large sizes, so silence the compiler # warnings for bounds checks involving those possible values. While @@ -100,10 +100,10 @@ KBUILD_CFLAGS +=3D -Werror=3Ddate-time KBUILD_CFLAGS +=3D -Werror=3Dincompatible-pointer-types =20 # Require designated initializers for all marked structures -KBUILD_CFLAGS +=3D $(call cc-option,-Werror=3Ddesignated-init) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_WERROR_DESIGNATED_INIT),-Werror=3D= designated-init) =20 # Warn if there is an enum types mismatch -KBUILD_CFLAGS +=3D $(call cc-option,-Wenum-conversion) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_WENUM_CONVERSION),-Wenum-conversio= n) =20 KBUILD_CFLAGS +=3D -Wunused =20 @@ -125,12 +125,12 @@ else # Suppress them by using -Wno... except for W=3D1. KBUILD_CFLAGS +=3D -Wno-unused-but-set-variable KBUILD_CFLAGS +=3D -Wno-unused-const-variable -KBUILD_CFLAGS +=3D $(call cc-option, -Wno-packed-not-aligned) -KBUILD_CFLAGS +=3D $(call cc-option, -Wno-format-overflow) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_WNO_PACKED_NOT_ALIGNED),-Wno-packe= d-not-aligned) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_WNO_FORMAT_OVERFLOW),-Wno-format-o= verflow) ifdef CONFIG_CC_IS_GCC KBUILD_CFLAGS +=3D -Wno-format-truncation endif -KBUILD_CFLAGS +=3D $(call cc-option, -Wno-stringop-truncation) +KBUILD_CFLAGS +=3D $(if $(CONFIG_CC_HAS_WNO_STRINGOP_TRUNCATION),-Wno-stri= ngop-truncation) =20 KBUILD_CFLAGS +=3D -Wno-override-init # alias for -Wno-initializer-overrid= es in clang =20 --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CA6593F327A; Tue, 8 Sep 2026 20:57:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901080; cv=none; b=d3C5ukxRbc6IqmLn9dFn195U/iHzaPfLUoJcXhACa5G300iESyUeqS7acNinLR/Jh4/Ynf5WTcGLgxdCRK9WkgYNfdAd7fsk2TguCndMRer+bKp6IuU/ySDL10mq3yD933U4U6E8ip45RgNneZaHABfyuIApELZ4QtGZYNBcAho= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901080; c=relaxed/simple; bh=Mu9/3L6JwuoO4qE0B5TB19zN1X+uYA2ATdK9i0ZQjsI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=KWytDAf2QKaf3SyDHbWfm2XuyJaEsXp4p1qu2ybQRYaHNNtC4E9QdNirJLl7V71IMEWvLT96Au852ga0ogwiVyfvUsy1rIiKDH/yHgoJPw6RFRkDquihQ7wKcl71sE7KT4CZTIq4zp34zMntXBG80rG57MNnnJv6croZ9tPkqjU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=H7eOezsG; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="H7eOezsG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 018651F00ACA; Tue, 8 Sep 2026 20:57:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788901078; bh=o9XBtkaVqNcdBkr0fOqwTDFvihvtpSpEfmr2OcpAjrM=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=H7eOezsG/k4L7RnezQlzDmyxYvIW6bywyEUwrvTlZzo4lKh0vtSAfck1wkSXTQ9ev VmZAJpvkEPwqDX3YHmC2BMJU5vL4+Jp8Qz22aWlTQoLIDTQ8d9YGnPv4v7SZKWJmD5 mTT1ODPotGOg6g/x0H87aTh061dDmL+8/iK+4eYyFoJK4js88YkUgoiBLn7V8GzuoJ y6i71GmBdFm8MSKvOsDdb2m+Z6GbI99drwRspDJqnv1ylIFlbuxjYAzmCi1oKczYfM P+rh2C9C8oqjl+1HOSJ9/d0i9kia2cmslDtIwrafGPzwwR1QJoYTXLQFpyVjQtdJ3Y OpWXnJrOCY06A== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:13 +0100 Subject: [PATCH 13/23] modpost: hash module source per-file, not per-byte Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-13-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=3622; i=ljs@kernel.org; h=from:subject:message-id; bh=Mu9/3L6JwuoO4qE0B5TB19zN1X+uYA2ATdK9i0ZQjsI=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlJmrS6scTz06IbNlhe9Px+M2gU853vLcZxboy2Ziz l/vw76so5SFQYyLQVZMkeX5F/H9QSJh8zov+LvBzGFlAhnCwMUpABN5tpbhv1eSsfzRGv+JKq57 9kz547UpXNTouVWchL6z5PO2L/VcTxj+J2n+Wl9knuH4X+q39dVyu90tolvqPb4dUZc0fis89dx TTgA= X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 modpost spends a long time md4 hashing module source at a per-byte granularity. Fix this by doing this hashing per-file instead by accumulating a per-file buffer in parse_file(). All 11,189 .mod.c files and Module.symvers were confirmed to be identical with this change applied. This is especially impactful for allmodconfig incremental builds (where CONFIG_MODULE_SRCVERSION_ALL is set). Whole build, 128-thread Threadripper 9980X, best of N runs: before after delta ------------------------------- x86 allmodconfig, touch mm/vma.c, gcc 40.6s 38.2s -2.4s (-6%) x86 allmodconfig, touch mm/vma.c, clang 36.2s 35.0s -1.2s (-3%) Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- scripts/mod/sumversion.c | 56 +++++++++++++++++++++++++++++++++++++-------= ---- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/scripts/mod/sumversion.c b/scripts/mod/sumversion.c index 3dd28b4d0099..2cbadd3cd97d 100644 --- a/scripts/mod/sumversion.c +++ b/scripts/mod/sumversion.c @@ -224,19 +224,11 @@ static void md4_final_ascii(struct md4_ctx *mctx, cha= r *out, unsigned int len) mctx->hash[0], mctx->hash[1], mctx->hash[2], mctx->hash[3]); } =20 -static inline void add_char(unsigned char c, struct md4_ctx *md) -{ - md4_update(md, &c, 1); -} - -static int parse_string(const char *file, unsigned long len, - struct md4_ctx *md) +static int parse_string(const char *file, unsigned long len) { unsigned long i; =20 - add_char(file[0], md); for (i =3D 1; i < len; i++) { - add_char(file[i], md); if (file[i] =3D=3D '"' && file[i-1] !=3D '\\') break; } @@ -255,15 +247,44 @@ static int parse_comment(const char *file, unsigned l= ong len) } =20 /* FIXME: Handle .s files differently (eg. # starts comments) --RR */ +static bool stop_char[256]; + +static void init_stop_chars(void) +{ + static bool done; + int chr; + + if (done) + return; + + for (chr =3D 0; chr < 256; chr++) + if (chr =3D=3D '\\' || chr =3D=3D '"' || chr =3D=3D '/' || isspace(chr)) + stop_char[chr] =3D true; + + done =3D true; +} + static int parse_file(const char *fname, struct md4_ctx *md) { + unsigned long i, len, n =3D 0; + unsigned char *buf; char *file; - unsigned long i, len; =20 file =3D read_text_file(fname); len =3D strlen(file); + if (!len) + goto out_file; + init_stop_chars(); + buf =3D xmalloc(len); /* File output buffer. */ =20 for (i =3D 0; i < len; i++) { + const unsigned char chr =3D file[i]; + + if (!stop_char[chr]) { + buf[n++] =3D file[i]; + continue; + } + /* Collapse and ignore \ and CR. */ if (file[i] =3D=3D '\\' && (i+1 < len) && file[i+1] =3D=3D '\n') { i++; @@ -276,7 +297,14 @@ static int parse_file(const char *fname, struct md4_ct= x *md) =20 /* Handle strings as whole units */ if (file[i] =3D=3D '"') { - i +=3D parse_string(file+i, len - i, md); + unsigned long slen =3D parse_string(file+i, len - i); + + /* Closing quote is included if there is one. */ + if (slen < len - i) + slen++; + memcpy(buf + n, file + i, slen); + n +=3D slen; + i +=3D slen - 1; continue; } =20 @@ -286,11 +314,15 @@ static int parse_file(const char *fname, struct md4_c= tx *md) continue; } =20 - add_char(file[i], md); + buf[n++] =3D file[i]; } + md4_update(md, buf, n); + free(buf); +out_file: free(file); return 1; } + /* Check whether the file is a static library or not */ static bool is_static_library(const char *objfile) { --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62CD43BFE5B; Tue, 8 Sep 2026 20:58:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901091; cv=none; b=ce5PA00EuyXGMduJHjdf5k2uejyWGp6kU7nBpqloGTZAeLWVBKVB5/An3gFJNpN9wuWG5jxVZrA9xG7r70E2wnt9kN0MOePDmz1CbAo3ihvI/hd+Z/2oTCmXYpaU8Ok/BLi9mJvxRzTFbLsOnQl0jY+ZqYVhfyjA5XNCs2lrc3c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901091; c=relaxed/simple; bh=BVONI6jQ5HybHDV5xL2pZB9mTVCT/1taKfTC6TZq7QU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VxqQV+U4Lz9RXEsO9QQywqWPD2GavjJk61dgcvBKBuulLb9TF290ULfaGZQq/a0g49UJugdDzAJCii2HINGFZFwuDIpGWpiOdQqd4NZ7/59n75j2N2P15IPvNo8KP4v2HAuTJSIu9thXYr//OWQ0lWtabltkK1dyUBQFFpvDsE0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=H6rggv9q; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="H6rggv9q" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 33E691F00A3A; Tue, 8 Sep 2026 20:57:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788901089; bh=xaxaPWmODXOjjlSFAfNKSXoU8O/l4eGEkzuqZMX6HWQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=H6rggv9q/tqtwuJX+Nwnp4lY2ozHj4VQ5i0Y16GL8w+yU1t76vD0CS6i5+Sr3AVWO p2ycfVE2VZLJaqXC3Ftc2E/BsDkT7Wtg3NVdblWIC9JoeEZ37amFQwS6p+m1nT+C/K H0KgY/1eKmvkFYZPZsrvneN68tJph23iQjjz4nk9KBYKbsldKEALwVxxfvRDLtNNJV kCfMs/aREKAUjAW8bryL2PpEHIsQvMZWmbMnKTo4ezQuAptgrLWsFfjX05vp/GDCc4 /zA6bG1vEOUPHQR2L8uvZ1QI+rAzBcnOSvRhcxhe2X3sr2ZMkE2h39tEpMKcHgfjEm LmBCKOMgymiLg== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:14 +0100 Subject: [PATCH 14/23] modpost: cache section relocation mismatch state Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-14-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=4714; i=ljs@kernel.org; h=from:subject:message-id; bh=BVONI6jQ5HybHDV5xL2pZB9mTVCT/1taKfTC6TZq7QU=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlJnfNprw8zHn24UTPjhnP3jU/kWvzGSNGNeaBclTs +8k52Z86ChlYRDjYpAVU2R5/kV8f5BI2LzOC/5uMHNYmUCGMHBxCsBEDAoZ/kpoL/yXFnp0nZ1w o/aSTce/NLTou57y6vzx10JSy0Vv7VSGn4zJfzZ6V19S/Zb2wsF+CveRr11reMp+tWm+4BJM8Z5 ZwQQA X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 For every relocation modpost invokes check_section_mismatch() to determine whether there is any kind of mismatch between the source and destination, and if so which classification applies. Each time it does this it invokes section_mismatch() which iterates through the sectioncheck[] array every time it's called. When walked a relocation section the source is fixed and there aren't many targets, so the same names are looked up over and over again. Therefore cache not only mismatch categorisation but also whether a mismatch even exists for a given section and look up the sections in the cache. Special indices (undefined, absolute, common) take the uncached path as before. This results in very significant speed ups for allmodconfig incremental builds. modpost is on the serial tail of every build that links vmlinux. Whole build, 128-thread Threadripper 9980X, best of N runs: before after delta ------------------------------- x86 defconfig, touch mm/vma.c, gcc 8.6s 8.4s -0.16s (-2%) x86 defconfig, touch mm/vma.c, clang 7.7s 7.5s -0.15s (-2%) x86 allmodconfig, touch mm/vma.c, gcc 38.2s 33.5s -4.6s (-12%) x86 allmodconfig, touch mm/vma.c, clang 35.0s 31.1s -4.0s (-11%) Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- scripts/mod/modpost.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++= ++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 75374c64b8cc..0fd43c8a89ea 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1155,12 +1155,67 @@ static void check_export_symbol(struct module *mod,= struct elf_info *elf, name); } =20 +/* + * mismatch_cache[section index] -> + * 0 - uncached. + * -1 - no mismatch. + * >0 - mismatch index + 1. + */ +static int *mismatch_cache; + +static void init_mismatch_cache(unsigned int num_sections) +{ + mismatch_cache =3D xcalloc(num_sections, sizeof(*mismatch_cache)); +} + +static void reset_mismatch_cache(unsigned int num_sections) +{ + memset(mismatch_cache, 0, num_sections * sizeof(*mismatch_cache)); +} + +static void free_mismatch_cache(void) +{ + free(mismatch_cache); + mismatch_cache =3D NULL; +} + +static const struct sectioncheck +*cache_mismatch(unsigned int secndx, const struct sectioncheck *mismatch) +{ + if (!mismatch) { + mismatch_cache[secndx] =3D -1; + return NULL; + } + + mismatch_cache[secndx] =3D (mismatch - sectioncheck) + 1; + return mismatch; +} + +static const struct sectioncheck *get_section_mismatch(const char *fromsec, + const struct elf_info *elf, unsigned int secndx) +{ + int cached; + + if (secndx >=3D elf->num_sections) + return section_mismatch(fromsec, sec_name(elf, secndx)); + + cached =3D mismatch_cache[secndx]; + if (cached < 0) + return NULL; + if (cached > 0) + return §ioncheck[cached - 1]; + + return cache_mismatch(secndx, + section_mismatch(fromsec, sec_name(elf, secndx))); +} + static void check_section_mismatch(struct module *mod, struct elf_info *el= f, Elf_Sym *sym, unsigned int fsecndx, const char *fromsec, Elf_Addr faddr, Elf_Addr taddr) { - const char *tosec =3D sec_name(elf, get_secindex(elf, sym)); + const unsigned int to_secndx =3D get_secindex(elf, sym); + const char *tosec =3D sec_name(elf, to_secndx); const struct sectioncheck *mismatch; =20 if (module_enabled && elf->export_symbol_secndx =3D=3D fsecndx) { @@ -1168,7 +1223,7 @@ static void check_section_mismatch(struct module *mod= , struct elf_info *elf, return; } =20 - mismatch =3D section_mismatch(fromsec, tosec); + mismatch =3D get_section_mismatch(fromsec, elf, to_secndx); if (!mismatch) return; =20 @@ -1445,6 +1500,8 @@ static void check_sec_ref(struct module *mod, struct = elf_info *elf) { int i; =20 + init_mismatch_cache(elf->num_sections); + /* Walk through all sections */ for (i =3D 0; i < elf->num_sections; i++) { Elf_Shdr *sechdr =3D &elf->sechdrs[i]; @@ -1461,6 +1518,9 @@ static void check_sec_ref(struct module *mod, struct = elf_info *elf) if (match(secname, section_white_list)) continue; =20 + /* Reset cache per-section. */ + reset_mismatch_cache(elf->num_sections); + start =3D sym_get_data_by_offset(elf, i, 0); stop =3D start + sechdr->sh_size; =20 @@ -1472,6 +1532,8 @@ static void check_sec_ref(struct module *mod, struct = elf_info *elf) start, stop); } } + + free_mismatch_cache(); } =20 static char *remove_dot(char *s) --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB1FB3CB2E7; Tue, 8 Sep 2026 20:58:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901104; cv=none; b=H6+F9U53pj86sWaRkrjGDbi+Wxu+syxtXIa8fqHW8+7EYdpufzuSxL3+qjF25xPD4YRXC++4blJtg0taPPQwc2tUWl/zxAbjIfYoTGvU9RxbARTbVHpCiQtSF5QHxgZy8ia/zOyyrjOYC6NWoPbSaytQ76F0S89ypRutubQE35E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901104; c=relaxed/simple; bh=AQu1KF1YNgQosHiZo0J2nPyyOBM3uoXaU4HMAqP1Ht4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=UjSde4g4DdtOlaZv7xMDp2sNfOfVO0Y3WUgh4QBhU6XMg0oKvofyJSj6pvij/7WDCwCZ26Nmc4rlkO2V1g5ZHVJa9hH6Cn8qZEy+088jlK6CQxrgOYwr2V3glKQGGx8r6AJh9Cy2LMHoGGvK8MEZD+tyeX+0fZZwe/3eX6ed8Jo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dL/sQW66; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="dL/sQW66" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6BE341F00A3D; Tue, 8 Sep 2026 20:58:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788901101; bh=gqE4ZzP3W9ruGfEjYRiiwb/nLgvEfrdDMaZ/ffIU/bs=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=dL/sQW66sciW50EuZaXUNRlES1QUKsVp55SiQ3utkOpSF0huTr5VM3UNXwAZ5R1kd KhM6ppRHPUkcvT4rP9HeCbMmZ1qOBC6fS3O2zoXYNiPkcIpbfsbbx5W/H5qkFGtUUI 1DJBN7S6CgeBt1Q4yAZ9OBzQHxJ8VIgRPUsF940qhmBwXx8BLQ83KIQv0v25nMXMQE Tt8mhmYzzqX+oDwSJU1Vk1QjJ03PsTFmzlZTLDxaNooSeNg7vbWFYOwDLroz4p5lbm aXgL8zLNAls8hfZ7nnSk+xY37wValzZuR9dPGVJeYnmsz9wuaVE6yk6U5s4n+s4Fpu eGEZVAeyP5NAA== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:15 +0100 Subject: [PATCH 15/23] modpost: emit module descriptors as assembly Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-15-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=30365; i=ljs@kernel.org; h=from:subject:message-id; bh=AQu1KF1YNgQosHiZo0J2nPyyOBM3uoXaU4HMAqP1Ht4=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlJlHXLLMTu+KnpeWq1gvuUn7eDFb0cV1G44XNRbP+ tUoP+l1RykLgxgXg6yYIsvzL+L7g0TC5nVe8HeDmcPKBDKEgYtTACZSsJCRYc6V08vfFFekTdnU 08SodeWFyiyrGb+kTllfMFp7rO4Q03eGv5LH8z5eKpu4+qvX0mtcr6vUzPUCAnf8CDphLdWnblW oxQ4A X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 modpost generates a descriptor for every module in the form of a .mod.c file with .modinfo strings, the __this_module descriptor, exported symbol tables and (with CONFIG_MODVERSIONS set), the CRC of imported symbols. These files are compiled like any other kernel C file with all of the -include preamble, as well as including linux/module.h, header dependencies generated by fixdep of a few hundred headers, an objtool run and if LTO is being performed, a link is performed to generate native code. On an x86-64 allmodconfig build 11,189 *.mod.c files are built, each taking ~0.24s of CPU time to compile, and module finalisation as a whole 6,300 CPU seconds, or 64 seconds of wall time when run over 128 threads. It also generates ~1.3 GiB of *.mod.o.cmd files that every subsequent build has to read back. Avoid all this by emitting the descriptors as assembly instead. The layout required (size and alignment of struct module, struct modversion_info, the module's name offsets, init, and exit fields and whether the architecture uses PREL32 ksymtab references) can all be derived from scripts/mod/module-offsets.h. An assembly file avoids all of the issues previously mentioned so this conversion results in a very significant performance win on kernel build. As a consequence of this change, since module-offsets.c includes linux/module.h, scripts/mod is now built after the generated headers in prepare0, rather than before. Also update .gitignore and make clean to handle .mod.S files, but keep .mod.c files there to ensure that users do not end up with untracked changes/dirty trees after the change takes effect. The sections were confirmed to be byte-for-byte identical to the C version produced - each of .modinfo, .gnu.linkonce.this_module, __ksymtab*, __ksymtab_strings, __kcrctab*, __kflagstab*, __versions, __version_ext_crcs, __version_ext_names and their relocations - for all 8,135 modules of a clang allmodconfig build with CONFIG_COMPILE_TEST off and CONFIG_MODVERSIONS, CONFIG_EXTENDED_MODVERSIONS and CONFIG_MODULE_SRCVERSION_ALL on, and for a sample built with gcc. What differs is what the compiler added around them: the __UNIQUE_ID_* locals, the KASAN constructor for the .mod.c globals, and on x86 a .note.gnu.property that the linker already drops from any module containing an assembly file and the loader never reads. None of these have any impact on the build, however. x86_64 kernels built with gcc and clang, with CONFIG_MODVERSIONS, CONFIG_EXTENDED_MODVERSIONS and CONFIG_MODULE_SRCVERSION_ALL, were booted, every module and an external one loaded and unloaded, and the srcversions checked against modinfo, i386 and arm64 defconfigs (gcc and clang) and a ThinLTO build were also built as part of testing. On the x86_64 allmodconfig with clang 22, "make modules" with every *.mod.o and *.ko deleted goes from 64.5s (6,306 CPU-s) to 28.5s (518 CPU-s). A consequence of this change is that the make jobs are now so small that make cannot dispatch them quick enough, however the next commit in the series addresses this issue. This impacts clean and no-op builds with a large number of modules most noticeably. Whole build, 128-thread Threadripper 9980X, best of N runs: before after delta ------------------------------- x86 allmodconfig, no-op make, gcc 11.0s 1.9s -9.1s (-82%) x86 allmodconfig, no-op make, clang 11.6s 2.4s -9.2s (-79%) x86 allmodconfig, clean, gcc 342.1s 304.5s -37.6s (-11%) x86 allmodconfig, clean, clang 338.5s 301.7s -36.8s (-11%) Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- .gitignore | 1 + Makefile | 4 +- include/linux/vermagic.h | 2 +- scripts/Makefile.modfinal | 10 +- scripts/Makefile.modpost | 2 +- scripts/mod/.gitignore | 1 + scripts/mod/Makefile | 8 + scripts/mod/modpost.c | 619 +++++++++++++++++++++++++++++++--------= ---- scripts/mod/module-offsets.c | 35 +++ scripts/tags.sh | 5 +- 10 files changed, 512 insertions(+), 175 deletions(-) diff --git a/.gitignore b/.gitignore index 9875120ea7bd..00fc262b894b 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,7 @@ *.lzo *.mod *.mod.c +*.mod.S *.o *.o.* *.patch diff --git a/Makefile b/Makefile index 088e901f0a72..997fc7ebe096 100644 --- a/Makefile +++ b/Makefile @@ -1422,8 +1422,8 @@ archprepare: outputmakefile archheaders archscripts s= cripts include/config/kerne include/generated/rustc_cfg remove-stale-files =20 prepare0: archprepare - $(Q)$(MAKE) $(build)=3Dscripts/mod $(Q)$(MAKE) $(build)=3D. prepare + $(Q)$(MAKE) $(build)=3Dscripts/mod =20 # All the preparing.. prepare: prepare0 @@ -2245,7 +2245,7 @@ clean: $(clean-dirs) -o -name '*.dt.yaml' -o -name 'dtbs-list' \ -o -name '*.dwo' -o -name '*.lst' \ -o -name '*.su' -o -name '*.mod' \ - -o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \ + -o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' -o -name '*.mod.S' \ -o -name '*.lex.c' -o -name '*.tab.[ch]' \ -o -name '*.asn1.[ch]' \ -o -name '*.symtypes' -o -name 'modules.order' \ diff --git a/include/linux/vermagic.h b/include/linux/vermagic.h index 335c360d4f9b..09f05d02664c 100644 --- a/include/linux/vermagic.h +++ b/include/linux/vermagic.h @@ -3,7 +3,7 @@ #define _LINUX_VERMAGIC_H =20 #ifndef INCLUDE_VERMAGIC -#error "This header can be included from kernel/module.c or *.mod.c only" +#error "This header can be included from kernel/module.c or scripts/module= -common.c only" #endif =20 #include diff --git a/scripts/Makefile.modfinal b/scripts/Makefile.modfinal index 01a37ec872b9..75e9effdf02c 100644 --- a/scripts/Makefile.modfinal +++ b/scripts/Makefile.modfinal @@ -20,10 +20,14 @@ __modfinal: $(modules:%.o=3D%.ko) modname =3D $(notdir $(@:.mod.o=3D)) part-of-module =3D y GCOV_PROFILE :=3D n -ccflags-remove-y :=3D $(CC_FLAGS_CFI) =20 -%.mod.o: %.mod.c FORCE - $(call if_changed_rule,cc_o_c) +# modpost lays the .mod.S out completely (write_mod_S_file()), so = it +# needs only the assembler and no dependency tracking. +quiet_cmd_as_mod_o =3D AS [M] $@ + cmd_as_mod_o =3D $(CC) $(_a_flags) $(modkern_aflags) -c -o $@ $< + +%.mod.o: %.mod.S FORCE + $(call if_changed,as_mod_o) =20 .module-common.o: $(srctree)/scripts/module-common.c FORCE $(call if_changed_rule,cc_o_c) diff --git a/scripts/Makefile.modpost b/scripts/Makefile.modpost index d7d45067d08b..eecf5f5c99b4 100644 --- a/scripts/Makefile.modpost +++ b/scripts/Makefile.modpost @@ -13,7 +13,7 @@ # Stage 2 is handled by this file and does the following # 1) Find all modules listed in modules.order # 2) modpost is then used to -# 3) create one .mod.c file per module +# 3) create one .mod.S file per module # 4) create one Module.symvers file with CRC for all exported symbols =20 # Step 3 is used to place certain information in the module's ELF diff --git a/scripts/mod/.gitignore b/scripts/mod/.gitignore index 0465ec33c9bf..620ab4362094 100644 --- a/scripts/mod/.gitignore +++ b/scripts/mod/.gitignore @@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only /devicetable-offsets.h +/module-offsets.h /elfconfig.h /mk_elfconfig /modpost diff --git a/scripts/mod/Makefile b/scripts/mod/Makefile index c729bc936bae..fbd5099e0441 100644 --- a/scripts/mod/Makefile +++ b/scripts/mod/Makefile @@ -13,10 +13,18 @@ $(obj)/$(devicetable-offsets-file): $(obj)/devicetable-= offsets.s FORCE =20 targets +=3D $(devicetable-offsets-file) devicetable-offsets.s =20 +module-offsets-file :=3D module-offsets.h + +$(obj)/$(module-offsets-file): $(obj)/module-offsets.s FORCE + $(call filechk,offsets,__MODULE_OFFSETS_H__) + +targets +=3D $(module-offsets-file) module-offsets.s + # dependencies on generated files need to be listed explicitly =20 $(obj)/modpost.o $(obj)/file2alias.o $(obj)/sumversion.o $(obj)/symsearch.= o: $(obj)/elfconfig.h $(obj)/file2alias.o: $(obj)/$(devicetable-offsets-file) +$(obj)/modpost.o: $(obj)/$(module-offsets-file) =20 quiet_cmd_elfconfig =3D MKELF $@ cmd_elfconfig =3D $(obj)/mk_elfconfig < $< > $@ diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 0fd43c8a89ea..550ccd753ed8 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -26,6 +26,7 @@ #include #include #include "modpost.h" +#include "module-offsets.h" #include "../../include/linux/license.h" =20 #define MODULE_NS_PREFIX "module:" @@ -1920,39 +1921,6 @@ static void check_modname_len(struct module *mod) mod_error(mod, "module name is too long\n"); } =20 -/** - * Header for the generated file - **/ -static void add_header(struct buffer *b, struct module *mod) -{ - buf_printf(b, "#include \n"); - buf_printf(b, "#include \n"); - buf_printf(b, "#include \n"); - buf_printf(b, "\n"); - buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n"); - buf_printf(b, "\n"); - buf_printf(b, "__visible struct module __this_module\n"); - buf_printf(b, "__section(\".gnu.linkonce.this_module\") =3D {\n"); - buf_printf(b, "\t.name =3D KBUILD_MODNAME,\n"); - if (mod->has_init) - buf_printf(b, "\t.init =3D init_module,\n"); - if (mod->has_cleanup) - buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n" - "\t.exit =3D cleanup_module,\n" - "#endif\n"); - buf_printf(b, "\t.arch =3D MODULE_ARCH_INIT,\n"); - buf_printf(b, "};\n"); - - if (!external_module) - buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n"); - - if (strstarts(mod->name, "drivers/staging")) - buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n"); - - if (strstarts(mod->name, "tools/testing")) - buf_printf(b, "\nMODULE_INFO(test, \"Y\");\n"); -} - static void add_exported_symbols(struct buffer *buf, struct module *mod) { struct symbol *sym; @@ -1990,123 +1958,6 @@ static void add_exported_symbols(struct buffer *buf= , struct module *mod) } } =20 -/** - * Record CRCs for unresolved symbols, supporting long names - */ -static void add_extended_versions(struct buffer *b, struct module *mod) -{ - struct symbol *s; - - if (!extended_modversions) - return; - - buf_printf(b, "\n"); - buf_printf(b, "static const u32 ____version_ext_crcs[]\n"); - buf_printf(b, "__used __section(\"__version_ext_crcs\") =3D {\n"); - list_for_each_entry(s, &mod->unresolved_symbols, list) { - if (!s->module) - continue; - if (!s->crc_valid) { - mod_warn(mod, "symbol '%s' has no CRC!\n", s->name); - continue; - } - buf_printf(b, "\t0x%08x,\n", s->crc); - } - buf_printf(b, "};\n"); - - buf_printf(b, "static const char ____version_ext_names[]\n"); - buf_printf(b, "__used __section(\"__version_ext_names\") =3D\n"); - list_for_each_entry(s, &mod->unresolved_symbols, list) { - if (!s->module) - continue; - if (!s->crc_valid) - /* - * We already warned on this when producing the crc - * table. - * We need to skip its name too, as the indexes in - * both tables need to align. - */ - continue; - buf_printf(b, "\t\"%s\\0\"\n", s->name); - } - buf_printf(b, ";\n"); -} - -/** - * Record CRCs for unresolved symbols - **/ -static void add_versions(struct buffer *b, struct module *mod) -{ - struct symbol *s; - - if (!basic_modversions) - return; - - buf_printf(b, "\n"); - buf_printf(b, "static const struct modversion_info ____versions[]\n"); - buf_printf(b, "__used __section(\"__versions\") =3D {\n"); - - list_for_each_entry(s, &mod->unresolved_symbols, list) { - if (!s->module) - continue; - if (!s->crc_valid) { - mod_warn(mod, "symbol '%s' has no CRC!\n", s->name); - continue; - } - if (strlen(s->name) >=3D MODULE_NAME_LEN) { - if (extended_modversions) { - /* this symbol will only be in the extended info */ - continue; - } else { - mod_error(mod, "too long symbol '%s'\n", s->name); - break; - } - } - buf_printf(b, "\t{ 0x%08x, \"%s\" },\n", - s->crc, s->name); - } - - buf_printf(b, "};\n"); -} - -static void add_depends(struct buffer *b, struct module *mod) -{ - struct symbol *s; - int first =3D 1; - - /* Clear ->seen flag of modules that own symbols needed by this. */ - list_for_each_entry(s, &mod->unresolved_symbols, list) { - if (s->module) - s->module->seen =3D s->module->is_vmlinux; - } - - buf_printf(b, "\n"); - buf_printf(b, "MODULE_INFO(depends, \""); - list_for_each_entry(s, &mod->unresolved_symbols, list) { - const char *p; - if (!s->module) - continue; - - if (s->module->seen) - continue; - - s->module->seen =3D true; - p =3D get_basename(s->module->name); - buf_printf(b, "%s%s", first ? "" : ",", p); - first =3D 0; - } - buf_printf(b, "\");\n"); -} - -static void add_srcversion(struct buffer *b, struct module *mod) -{ - if (mod->srcversion[0]) { - buf_printf(b, "\n"); - buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n", - mod->srcversion); - } -} - static void write_buf(struct buffer *b, const char *fname) { FILE *file; @@ -2191,30 +2042,465 @@ static void write_vmlinux_export_c_file(struct mod= ule *mod) free(buf.p); } =20 -/* do sanity checks, and generate *.mod.c file */ -static void write_mod_c_file(struct module *mod) +#if MOD_SIZEOF_LONG =3D=3D 8 +#define MOD_PTR_DIRECTIVE ".quad" +#else +#define MOD_PTR_DIRECTIVE ".long" +#endif + +/* See KSYM_FUNC() in include/linux/export-internal.h. */ +#if MOD_FUNC_PLABEL +#define MOD_FUNC_PREFIX "P%" +#else +#define MOD_FUNC_PREFIX "" +#endif + +/* See __KSYM_ALIGN in include/linux/export-internal.h. */ +#if MOD_PREL32_RELOCATIONS || MOD_SIZEOF_LONG =3D=3D 4 +#define KSYM_ALIGN 4 +#else +#define KSYM_ALIGN 8 +#endif + +/* Append the body of an assembler string literal, escaped as needed. */ +static void buf_escaped(struct buffer *buf, const char *str) { - struct buffer buf =3D { }; - struct module_alias *alias, *next; - char fname[PATH_MAX]; - int ret; + unsigned char chr; =20 - add_header(&buf, mod); - add_exported_symbols(&buf, mod); - add_versions(&buf, mod); - add_extended_versions(&buf, mod); - add_depends(&buf, mod); + while ((chr =3D *str++)) { + if (chr =3D=3D '"' || chr =3D=3D '\\') + buf_printf(buf, "\\%c", chr); + else if (isprint(chr)) + buf_printf(buf, "%c", chr); + else + buf_printf(buf, "\\%03o", chr); + } +} =20 - buf_printf(&buf, "\n"); +static void buf_asciz(struct buffer *buf, const char *str) +{ + buf_printf(buf, "\t.asciz \""); + buf_escaped(buf, str); + buf_printf(buf, "\"\n"); +} + +/* The equivalent of MODULE_INFO(tag, info). */ +static void add_asm_modinfo(struct buffer *buf, const char *tag, + const char *info) +{ + buf_printf(buf, "\t.section .modinfo,\"a\",%%progbits\n"); + buf_printf(buf, "\t.asciz \"%s=3D", tag); + buf_escaped(buf, info); + buf_printf(buf, "\"\n"); +} + +/* See __KSYM_REF() in include/linux/export-internal.h. */ +static void add_asm_ksym_ref(struct buffer *buf, const char *prefix, + const char *sym) +{ +#if MOD_PREL32_RELOCATIONS + buf_printf(buf, "\t.long %s%s - .\n", prefix, sym); +#else + buf_printf(buf, "\t" MOD_PTR_DIRECTIVE " %s%s\n", prefix, sym); +#endif +} + +/* The name and namespace strings a ksymtab entry refers to. */ +static void add_asm_kstrtab(struct buffer *buf, const struct symbol *sym) +{ + buf_printf(buf, "\t.section \"__ksymtab_strings\",\"aMS\",%%progbits,1\n"= ); + buf_printf(buf, "__kstrtab_%s:\n", sym->name); + buf_asciz(buf, sym->name); + buf_printf(buf, "__kstrtabns_%s:\n", sym->name); + buf_asciz(buf, sym->namespace); + buf_printf(buf, "\t.previous\n"); +} + +/* The equivalent of SYMBOL_FLAGS(). */ +static void add_asm_kflagstab(struct buffer *buf, const struct symbol *sym) +{ + buf_printf(buf, "\t.section \"___kflagstab+%s\", \"a\"\n", sym->name); + buf_printf(buf, "__flags_%s:\n", sym->name); + buf_printf(buf, "\t.byte 0x%02x\n", get_symbol_flags(sym)); + buf_printf(buf, "\t.previous\n"); +} + +/* The equivalent of KSYMTAB_FUNC()/KSYMTAB_DATA(). */ +static void add_asm_ksymtab(struct buffer *buf, const struct symbol *sym) +{ + const char *name =3D sym->name; + + add_asm_kstrtab(buf, sym); + + buf_printf(buf, "\t.section \"___ksymtab+%s\", \"a\"\n", name); + buf_printf(buf, "\t.balign %d\n", KSYM_ALIGN); + buf_printf(buf, "__ksymtab_%s:\n", name); + add_asm_ksym_ref(buf, sym->is_func ? MOD_FUNC_PREFIX : "", name); + add_asm_ksym_ref(buf, "__kstrtab_", name); + add_asm_ksym_ref(buf, "__kstrtabns_", name); + buf_printf(buf, "\t.previous\n"); + + add_asm_kflagstab(buf, sym); +} + +/* The equivalent of SYMBOL_CRC(). */ +static void add_asm_crc(struct buffer *buf, const struct symbol *sym) +{ + buf_printf(buf, "\t.section \"___kcrctab+%s\",\"a\"\n", sym->name); + buf_printf(buf, "\t.balign 4\n"); + buf_printf(buf, "__crc_%s:\n", sym->name); + buf_printf(buf, "\t.long 0x%08x\n", sym->crc); + buf_printf(buf, "\t.previous\n"); +} + +static bool export_is_kept(const struct symbol *sym) +{ + return !trim_unused_exports || sym->used; +} + +/* Record the CRCs of the exported symbols. */ +static void add_asm_crcs(struct buffer *buf, struct module *mod) +{ + struct symbol *sym; + + list_for_each_entry(sym, &mod->exported_symbols, list) { + if (!export_is_kept(sym)) + continue; + + if (!sym->crc_valid) + mod_warn(mod, "EXPORT symbol '%s' version generation failed, symbol wil= l not be versioned.\n" + "Is '%s' prototyped in ?\n", + sym->name, sym->name); + add_asm_crc(buf, sym); + } +} + +static void add_asm_exported_symbols(struct buffer *buf, struct module *mo= d) +{ + struct symbol *sym; + + list_for_each_entry(sym, &mod->exported_symbols, list) { + if (export_is_kept(sym)) + add_asm_ksymtab(buf, sym); + } + + if (modversions) + add_asm_crcs(buf, mod); +} + +/* Zero fill up to the offset. */ +static void asm_skip_to(struct buffer *buf, unsigned int *pos, + unsigned int offset) +{ + if (offset > *pos) + buf_printf(buf, "\t.skip %u\n", offset - *pos); + + *pos =3D offset; +} + +/* A pointer field of __this_module: its offset and an assembler expressio= n. */ +struct this_module_field { + unsigned int offset; + const char *value; +}; + +#define THIS_MODULE_MAX_FIELDS 4 + +static int compare_field_offsets(const void *ptr_a, const void *ptr_b) +{ + const struct this_module_field *field_a =3D ptr_a, *field_b =3D ptr_b; + + if (field_a->offset !=3D field_b->offset) + return field_a->offset < field_b->offset ? -1 : 1; + + return 0; +} + +/* The fields of __this_module which are not zero, in offset order. */ +static unsigned int get_this_module_fields(const struct module *mod, + struct this_module_field *fields) +{ + unsigned int nr_fields =3D 0; + + if (mod->has_init) + fields[nr_fields++] =3D (struct this_module_field) + { MOD_OFF_module_init, MOD_FUNC_PREFIX "init_module" }; +#ifdef MOD_OFF_module_exit + if (mod->has_cleanup) + fields[nr_fields++] =3D (struct this_module_field) + { MOD_OFF_module_exit, MOD_FUNC_PREFIX "cleanup_module" }; +#endif +#ifdef MOD_OFF_module_arch_fixup_start + fields[nr_fields++] =3D (struct this_module_field) + { MOD_OFF_module_arch_fixup_start, "__start_fixup" }; + fields[nr_fields++] =3D (struct this_module_field) + { MOD_OFF_module_arch_fixup_end, "__stop_fixup" }; +#endif + qsort(fields, nr_fields, sizeof(*fields), compare_field_offsets); + + return nr_fields; +} + +/* + * The equivalent of: + * + * __visible struct module __this_module __section(".gnu.linkonce.this_mod= ule") + * =3D { .name =3D KBUILD_MODNAME, .init =3D init_module, .exit =3D clean= up_module, + * .arch =3D MODULE_ARCH_INIT }; + * + * Everything not listed is zero, MODULE_ARCH_INIT included, except on m68= k. + */ +static void add_asm_this_module(struct buffer *buf, const struct module *m= od, + const char *modname) +{ + struct this_module_field fields[THIS_MODULE_MAX_FIELDS]; + const unsigned int nr_fields =3D get_this_module_fields(mod, fields); + unsigned int pos =3D 0, i; + + buf_printf(buf, "\n\t.section .gnu.linkonce.this_module,\"aw\",%%progbits= \n"); + buf_printf(buf, "\t.balign %d\n", MOD_ALIGNOF_struct_module); + buf_printf(buf, "\t.globl __this_module\n"); + buf_printf(buf, "\t.type __this_module, %%object\n"); + buf_printf(buf, "\t.size __this_module, %d\n", MOD_SIZEOF_struct_module); + buf_printf(buf, "__this_module:\n"); + + asm_skip_to(buf, &pos, MOD_OFF_module_name); + buf_printf(buf, "\t.ascii \"%s\"\n", modname); + pos +=3D strlen(modname); + + for (i =3D 0; i < nr_fields; i++) { + asm_skip_to(buf, &pos, fields[i].offset); + buf_printf(buf, "\t" MOD_PTR_DIRECTIVE " %s\n", fields[i].value); + pos +=3D MOD_SIZEOF_LONG; + } + + asm_skip_to(buf, &pos, MOD_SIZEOF_struct_module); +} + +/* + * An unresolved symbol without a module is not versioned; one without a C= RC + * cannot be, so warn about it. + */ +static bool skip_unversioned(struct module *mod, const struct symbol *sym) +{ + if (!sym->module) + return true; + if (sym->crc_valid) + return false; + + mod_warn(mod, "symbol '%s' has no CRC!\n", sym->name); + return true; +} + +/* One struct modversion_info: the CRC, then the name padded to the end. */ +static void add_asm_version(struct buffer *buf, const struct symbol *sym) +{ + buf_printf(buf, "\t" MOD_PTR_DIRECTIVE " 0x%08x\n", sym->crc); + buf_printf(buf, "\t.ascii \"%s\"\n", sym->name); + buf_printf(buf, "\t.skip %zu\n", MOD_SIZEOF_struct_modversion_info - + MOD_OFF_modversion_info_name - strlen(sym->name)); +} + +/* + * The equivalent of: + * + * static const struct modversion_info ____versions[] + * __used __section("__versions") =3D { { crc, "name" }, ... }; + * + * for unresolved symbols. + */ +static void add_asm_versions(struct buffer *buf, struct module *mod) +{ + struct symbol *sym; + + if (!basic_modversions) + return; + + buf_printf(buf, "\n\t.section __versions,\"a\",%%progbits\n"); + buf_printf(buf, "\t.balign %d\n", MOD_ALIGNOF_struct_modversion_info); + list_for_each_entry(sym, &mod->unresolved_symbols, list) { + if (skip_unversioned(mod, sym)) + continue; + + if (strlen(sym->name) >=3D MOD_NAME_LEN) { + /* Only the extended table can hold it. */ + if (extended_modversions) + continue; + + mod_error(mod, "too long symbol '%s'\n", sym->name); + break; + } + + add_asm_version(buf, sym); + } +} + +static void add_asm_version_ext_crcs(struct buffer *buf, struct module *mo= d) +{ + struct symbol *sym; + + buf_printf(buf, "\n\t.section __version_ext_crcs,\"a\",%%progbits\n"); + buf_printf(buf, "\t.balign 4\n"); + list_for_each_entry(sym, &mod->unresolved_symbols, list) { + if (skip_unversioned(mod, sym)) + continue; + + buf_printf(buf, "\t.long 0x%08x\n", sym->crc); + } +} + +/* + * A symbol without a CRC was warned about with the CRCs, and is skipped h= ere + * too so that the names line up with them. + */ +static void add_asm_version_ext_names(struct buffer *buf, struct module *m= od) +{ + struct symbol *sym; + + buf_printf(buf, "\t.section __version_ext_names,\"a\",%%progbits\n"); + list_for_each_entry(sym, &mod->unresolved_symbols, list) { + if (!sym->module || !sym->crc_valid) + continue; + + buf_asciz(buf, sym->name); + } + /* The terminator of the string literal this used to be. */ + buf_printf(buf, "\t.byte 0\n"); +} + +/* + * The equivalent of: + * static const u32 ____version_ext_crcs[] __section("__version_ext_crcs")= =3D { crc, ... }; + * static const char ____version_ext_names[] __section("__version_ext_name= s") =3D "name\0" ...; + * + * for unresolved symbols. + */ +static void add_asm_extended_versions(struct buffer *buf, struct module *m= od) +{ + if (!extended_modversions) + return; + + add_asm_version_ext_crcs(buf, mod); + add_asm_version_ext_names(buf, mod); +} + +/* Clear ->seen of the modules that own symbols this one needs. */ +static void clear_seen_dependencies(struct module *mod) +{ + struct symbol *sym; + + list_for_each_entry(sym, &mod->unresolved_symbols, list) { + if (sym->module) + sym->module->seen =3D sym->module->is_vmlinux; + } +} + +/* The modules this one depends on, each once, comma separated. */ +static void collect_dependencies(struct module *mod, struct buffer *deps) +{ + struct symbol *sym; + bool first =3D true; + + clear_seen_dependencies(mod); + + list_for_each_entry(sym, &mod->unresolved_symbols, list) { + struct module *owner =3D sym->module; + + if (!owner || owner->seen) + continue; + + owner->seen =3D true; + buf_printf(deps, "%s%s", first ? "" : ",", + get_basename(owner->name)); + first =3D false; + } + buf_write(deps, "", 1); +} + +static void add_asm_depends(struct buffer *buf, struct module *mod) +{ + struct buffer deps =3D { }; + + collect_dependencies(mod, &deps); + buf_printf(buf, "\n"); + add_asm_modinfo(buf, "depends", deps.p); + free(deps.p); +} + +/* + * KBUILD_MODNAME: the basename of the module with '-' and ',' replaced by + * '_' (see name-fix in scripts/Makefile.lib). + */ +static char *get_kbuild_modname(const struct module *mod) +{ + char *name =3D xstrdup(get_basename(mod->name)); + char *curr; + + for (curr =3D name; *curr; curr++) { + if (*curr =3D=3D '-' || *curr =3D=3D ',') + *curr =3D '_'; + } + + return name; +} + +/* The module's name, its descriptor, and where it comes from. */ +static void add_asm_header(struct buffer *buf, const struct module *mod, + const char *modname) +{ + buf_printf(buf, "/* Generated by modpost, see scripts/Makefile.modfinal *= /\n\n"); + + add_asm_modinfo(buf, "name", modname); + add_asm_this_module(buf, mod, modname); + buf_printf(buf, "\n"); + + if (!external_module) + add_asm_modinfo(buf, "intree", "Y"); + if (strstarts(mod->name, "drivers/staging")) + add_asm_modinfo(buf, "staging", "Y"); + if (strstarts(mod->name, "tools/testing")) + add_asm_modinfo(buf, "test", "Y"); +} + +static void add_asm_aliases(struct buffer *buf, struct module *mod) +{ + struct module_alias *alias, *next; + + buf_printf(buf, "\n"); list_for_each_entry_safe(alias, next, &mod->aliases, node) { - buf_printf(&buf, "MODULE_ALIAS(\"%s\");\n", alias->str); + add_asm_modinfo(buf, "alias", alias->str); list_del(&alias->node); free(alias); } +} =20 - add_srcversion(&buf, mod); +static void add_asm_srcversion(struct buffer *buf, const struct module *mo= d) +{ + if (!mod->srcversion[0]) + return; =20 - ret =3D snprintf(fname, sizeof(fname), "%s.mod.c", mod->name); + buf_printf(buf, "\n"); + add_asm_modinfo(buf, "srcversion", mod->srcversion); +} + +static void write_mod_S_file(struct module *mod) +{ + struct buffer buf =3D { }; + char fname[PATH_MAX]; + char *modname =3D get_kbuild_modname(mod); + int ret; + + add_asm_header(&buf, mod, modname); + buf_printf(&buf, "\n"); + add_asm_exported_symbols(&buf, mod); + add_asm_versions(&buf, mod); + add_asm_extended_versions(&buf, mod); + add_asm_depends(&buf, mod); + add_asm_aliases(&buf, mod); + add_asm_srcversion(&buf, mod); + buf_printf(&buf, "\n\t.section .note.GNU-stack,\"\",%%progbits\n"); + + ret =3D snprintf(fname, sizeof(fname), "%s.mod.S", mod->name); if (ret >=3D sizeof(fname)) { error("%s: too long path was truncated\n", fname); goto free; @@ -2223,6 +2509,7 @@ static void write_mod_c_file(struct module *mod) write_if_changed(&buf, fname); =20 free: + free(modname); free(buf.p); } =20 @@ -2462,7 +2749,7 @@ int main(int argc, char **argv) if (mod->is_vmlinux) write_vmlinux_export_c_file(mod); else - write_mod_c_file(mod); + write_mod_S_file(mod); } =20 if (missing_namespace_deps) diff --git a/scripts/mod/module-offsets.c b/scripts/mod/module-offsets.c new file mode 100644 index 000000000000..a336dd47aa33 --- /dev/null +++ b/scripts/mod/module-offsets.c @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Layout of the structures modpost emits into *.mod.S, extracted from the + * target headers as devicetable-offsets.c does for the device tables. + */ +#define COMPILE_OFFSETS +#include +#include + +int main(void) +{ + DEFINE(MOD_SIZEOF_LONG, sizeof(long)); + DEFINE(MOD_PREL32_RELOCATIONS, IS_ENABLED(CONFIG_HAVE_ARCH_PREL32_RELOCAT= IONS)); + DEFINE(MOD_FUNC_PLABEL, IS_ENABLED(CONFIG_PARISC) && IS_ENABLED(CONFIG_64= BIT)); + + DEFINE(MOD_SIZEOF_struct_module, sizeof(struct module)); + DEFINE(MOD_ALIGNOF_struct_module, __alignof__(struct module)); + OFFSET(MOD_OFF_module_name, module, name); + OFFSET(MOD_OFF_module_init, module, init); +#ifdef CONFIG_MODULE_UNLOAD + OFFSET(MOD_OFF_module_exit, module, exit); +#endif +#if defined(CONFIG_M68K) && defined(CONFIG_MMU) + /* MODULE_ARCH_INIT: the only architecture where it is not all zeroes. */ + OFFSET(MOD_OFF_module_arch_fixup_start, module, arch.fixup_start); + OFFSET(MOD_OFF_module_arch_fixup_end, module, arch.fixup_end); +#endif + DEFINE(MOD_NAME_LEN, MODULE_NAME_LEN); + + DEFINE(MOD_SIZEOF_struct_modversion_info, sizeof(struct modversion_info)); + DEFINE(MOD_ALIGNOF_struct_modversion_info, __alignof__(struct modversion_= info)); + OFFSET(MOD_OFF_modversion_info_name, modversion_info, name); + + return 0; +} diff --git a/scripts/tags.sh b/scripts/tags.sh index 41e38df96984..c33d0f58a9d4 100755 --- a/scripts/tags.sh +++ b/scripts/tags.sh @@ -14,8 +14,9 @@ fi =20 # RCS_FIND_IGNORE has escaped ()s -- remove them. ignore=3D"$(echo "$RCS_FIND_IGNORE" | sed 's|\\||g' )" -# tags and cscope files should also ignore MODVERSION *.mod.c files -ignore=3D"$ignore ( -name *.mod.c ) -prune -o" +# tags and cscope files should also ignore the modpost-generated *.mod.S f= iles +# and any *.mod.c left behind from before they were assembly +ignore=3D"$ignore ( -name *.mod.c -o -name *.mod.S ) -prune -o" =20 # ignore arbitrary directories if [ -n "${IGNORE_DIRS}" ]; then --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D141A406822; Tue, 8 Sep 2026 20:58:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901114; cv=none; b=J0GwMPyN5VJQhpggC8lWihr5Gag6ltV/t+7qLca88hjvYkeRjWiqOrDgrVMwuG7ebH7W/7XWj8rQxCSk0uFGbcoWdXgNYR+TzOu3gBcRxWp7mSkWsAynVKww//zyKCGde5xojQBgAiqu791+HT4mGYRceP5R5CekLFJJTywIff4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901114; c=relaxed/simple; bh=Gelur0DpysKwUODmcguGccgt6clYJhYW6IXRF54x23c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=H8aWYkfIlCbeW2aGWkYSDFAx+RstSZbJXo5Th6/hs6CrQfsghVRWe7iPxf4KXOWUZuu5csJ5Bm/gfYvPGGxGMaFaEpAjRPEPhtT5ZKyIciGZJpn/SIKYji85brBaz3DD6AqDtfWGeVusopUqwhoC+El9tiWEijVeryeQUQZYvQ0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UoHfTkok; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="UoHfTkok" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F9201F00A3A; Tue, 8 Sep 2026 20:58:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788901112; bh=pI6ihjInprs3EsyWvkPxTax/MQzOf/BnFtLX0/4DRHk=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=UoHfTkokxUxDvJUJNI0gY83ydEKYo9TV+HnOqobx9nDhcAVm/5ftXE/PQ7RqP+BMO ZyNOVUWijWGwyRLnZpuAPq5SxPKytoYXq7wgOdivVdtNjQfwe+ocBe2vDMAOD8d7V1 RsDmC4+N4D7PIO+TALKkFcNyfZZcRR07hBdcp/S94BKuCdvcNMEv/cI3X5AjYLxugm XKh/n+j+/McakEWmmtsT3cbxhZeE4kdsKTd/8NM6HGZ4SwRYBmtXXhTNkgZtSEUlQ3 ZCQgq76v3wYtOYUL+u8spfd7WcLC4Szxy4RN/QFJ/uA/4JUlNWXFcLSfwsen26eFNb njbLnnbezVWGw== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:16 +0100 Subject: [PATCH 16/23] kbuild: batch module finalisation Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-16-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=4146; i=ljs@kernel.org; h=from:subject:message-id; bh=Gelur0DpysKwUODmcguGccgt6clYJhYW6IXRF54x23c=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlFmYvV7lYBfmt0UvYf2hCVfScyq9LfxLQ2YW+h7eP fWgmm5YRykLgxgXg6yYIsvzL+L7g0TC5nVe8HeDmcPKBDKEgYtTACayjp2R4TZvs0gN81KrvwzJ L/SZsyo77jQfeuBvJ66+aH6oZd2CZIb/ocsudvxk8HW7K32qrMbi6ffWnO6Pp8IsA5t3/m9c966 TCQA= X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 Module finalisation on allmodconfig builds consists of a large number of very short-lived jobs, and the make job dispatcher cannot possibly dispatch jobs fast enough. For allmodconfig x86-64 this can be on the order of ~22,000 jobs of a few milliseconds in duration each. However per-job cost grows with the variables the instance holds, here the savedcmd_* of every .mod.o and .ko read back from the .cmd files, since it walks them all to build each child's environment. This makes module finalisation very inefficient when large numbers of modules are being built. Fix this by splitting modules.order into chunks of 128 at a time, run in parallel. Each instance holds only its own modules' variables and the top-level one reads no per-module .cmd files at all, the same rules serve both levels, and an instance is told its chunk with modfinal-first=3D. "make modules" with every *.mod.o and *.ko deleted goes from 28.9s to 15.9s with clang 22. No-op "make modules" goes from 5.6s to 4.8s, as checking the 22,000 targets is spread over the chunks too. Whole build, 128-thread Threadripper 9980X, best of N runs: before after delta ------------------------------- x86 allmodconfig, no-op make, gcc 1.9s 1.2s -0.74s (-39%) x86 allmodconfig, no-op make, clang 2.4s 1.6s -0.77s (-32%) x86 allmodconfig, clean, gcc 304.5s 291.4s -13.1s (-4%) x86 allmodconfig, clean, clang 301.7s 297.0s -4.7s (-2%) Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- scripts/Makefile.modfinal | 26 +++++++++++++++++++++++++- scripts/mod/sumversion.c | 4 ++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/scripts/Makefile.modfinal b/scripts/Makefile.modfinal index 75e9effdf02c..858fa798090b 100644 --- a/scripts/Makefile.modfinal +++ b/scripts/Makefile.modfinal @@ -13,9 +13,30 @@ include $(srctree)/scripts/Makefile.lib # find all modules listed in modules.order modules :=3D $(call read-file, modules.order) =20 +modfinal-chunk-size :=3D 128 + +ifdef modfinal-first + +# this instance handles the chunk of modules.order starting at $(modfinal-= first) +modules :=3D $(wordlist $(modfinal-first), $(words $(modules)), $(modules)) +modules :=3D $(wordlist 1, $(modfinal-chunk-size), $(modules)) + __modfinal: $(modules:%.o=3D%.ko) @: =20 +else + +modfinal-chunks :=3D $(addprefix chunk-, $(shell seq 1 $(modfinal-chunk-si= ze) $(words $(modules)))) + +PHONY +=3D $(modfinal-chunks) +$(modfinal-chunks): .module-common.o + $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modfinal modfinal-first=3D$(@:= chunk-%=3D%) + +__modfinal: $(modfinal-chunks) + @: + +endif + # modname and part-of-module are set to make c_flags define proper module = flags modname =3D $(notdir $(@:.mod.o=3D)) part-of-module =3D y @@ -58,7 +79,10 @@ ifdef CONFIG_DEBUG_INFO_BTF_MODULES endif +$(call cmd,check_tracepoint) =20 -targets +=3D $(modules:%.o=3D%.ko) $(modules:%.o=3D%.mod.o) .module-common= .o +targets +=3D .module-common.o +ifdef modfinal-first +targets +=3D $(modules:%.o=3D%.ko) $(modules:%.o=3D%.mod.o) +endif =20 # Add FORCE to the prerequisites of a target to force it to be always rebu= ilt. # ------------------------------------------------------------------------= --- diff --git a/scripts/mod/sumversion.c b/scripts/mod/sumversion.c index 2cbadd3cd97d..5501d6aa0bea 100644 --- a/scripts/mod/sumversion.c +++ b/scripts/mod/sumversion.c @@ -249,7 +249,7 @@ static int parse_comment(const char *file, unsigned lon= g len) /* FIXME: Handle .s files differently (eg. # starts comments) --RR */ static bool stop_char[256]; =20 -static void init_stop_chars(void) +static void sumversion_init(void) { static bool done; int chr; @@ -274,7 +274,7 @@ static int parse_file(const char *fname, struct md4_ctx= *md) len =3D strlen(file); if (!len) goto out_file; - init_stop_chars(); + sumversion_init(); buf =3D xmalloc(len); /* File output buffer. */ =20 for (i =3D 0; i < len; i++) { --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B7A2E3CD8C5; Tue, 8 Sep 2026 20:58:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901125; cv=none; b=mrhbw7rH3eI3kx3QSJMbrpyY9T7Ejty/fFP3ICUgPLHqoYHCzfJTEStBNzWErcNIpUVl+xPwHCyGH+A5ar3+19cYCrfySURlnxQMg6WXqbpRY7oH615V/i/Lga9bsGEvNABRuFTnHyBCeB1mLpsiHvrq/2OgYN7/e0hTM2xAHw4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901125; c=relaxed/simple; bh=WICQxA7wSs1OjQCOKkZs0RphLh6aY8TzrexzGYQ5YdM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Ke5BHrag9u1AkSd0gOFceicMTwuTBFlgN4L55RzbC25I48TCKzXrtGm9DvWMHqOJstsGewTF+MuKmdJST6S80Q/DXRlIBW81F/PRQUU05RqOJUkRy8dNIio1anyuQBr9LFl5J7Tr+aqBcNKRHsrgeO1Tn/ScPXvqkz2lE/PmSm8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=e6lx/od9; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="e6lx/od9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DABE81F00A3D; Tue, 8 Sep 2026 20:58:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788901123; bh=uqBq8a6PBmwcf0p2yuh6c4ut6ERzykBEHjxf3/hGkX8=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=e6lx/od9mLRaFbj6pD8WjdyUrpu387L5oUbxsS74POQpizvsu2X9B5alOW1uFj8y5 BogpoBtibbPrlFDz4Ec3/AdNz6xfPwZHUB5guwX01WhFo9UMbOB2aUsI9pZsrRRAP4 wLq+X3qQGbQ3EeoOOjm9JR1UCr2lqd+VX4HE/xOvgXCQToYgf4Uq0lu6WcIoU831NT 3Zr0ZWhdRXCFxU99AazeTkyNbDJRABmRLmC5/jK+DX1UXfCtMjxQZsFLerx/2qIlYU IYr/jQMlMujoHloJcSVIewZGRNZ7u1U02GBUq6x7y5XXIcE7+DXZzJcawr7XnyLaFC RprAR8YIz6BTg== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:17 +0100 Subject: [PATCH 17/23] modpost: perform srcversion hashing in parallel Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-17-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=5781; i=ljs@kernel.org; h=from:subject:message-id; bh=WICQxA7wSs1OjQCOKkZs0RphLh6aY8TzrexzGYQ5YdM=; b=kA0DAAoWz53NioHifxQByyZiAGqgdjiivqgxWMcAzwXiFMjxhapr5eptu4LqrpJHxWdzErhUN Yh1BAAWCgAdFiEE5/QXv1IUVp6J0E9Gz53NioHifxQFAmqgdjgACgkQz53NioHifxRJhQD+KkKD qLzGPYKtOKR3m+zxpGn6XPTCEmJRgn6E7DzMTooBAIO2t2bh9xq8Miz8ic/o7YF92YFvKDtRCsm kxE2EVJsK X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 modpost does a lot of single-threaded work hashing files from each object's .cmd file. This makes the build slower than it needs to be, so do this work in parallel. This is egregious for allmodconfig builds - for instance x86-64 can end up opening 200,000 files individually and hashing them all serially. Parallelise this operation by maintaining a thread pool for the hashing work. Combined with the per-file hashing commit this cuts modpost's run time nearly in half for an allmodconfig build. Module.symvers and every *.mod.S are byte for byte the same. modpost is on the serial tail of every allmodconfig build, however defconfig does not set CONFIG_MODULE_SRCVERSION_ALL and is unchanged. Whole build, 128-thread Threadripper 9980X, best of N runs: before after delta ------------------------------- x86 allmodconfig, touch mm/vma.c, gcc 33.4s 30.2s -3.2s (-10%) x86 allmodconfig, touch mm/vma.c, clang 31.1s 28.1s -3.0s (-10%) Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- scripts/mod/Makefile | 1 + scripts/mod/modpost.c | 72 ++++++++++++++++++++++++++++++++++++++++++++= ++-- scripts/mod/modpost.h | 2 ++ scripts/mod/sumversion.c | 3 +- 4 files changed, 74 insertions(+), 4 deletions(-) diff --git a/scripts/mod/Makefile b/scripts/mod/Makefile index fbd5099e0441..fdd486184f9c 100644 --- a/scripts/mod/Makefile +++ b/scripts/mod/Makefile @@ -5,6 +5,7 @@ hostprogs-always-y +=3D modpost mk_elfconfig always-y +=3D empty.o =20 modpost-objs :=3D modpost.o file2alias.o sumversion.o symsearch.o +HOSTLDLIBS_modpost :=3D -lpthread =20 devicetable-offsets-file :=3D devicetable-offsets.h =20 diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 550ccd753ed8..882169e51851 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -1717,8 +1718,7 @@ static void read_symbols(const char *modname) if (!mod->is_vmlinux) { version =3D get_modinfo(&info, "version"); if (version || all_versions) - get_src_version(mod->name, mod->srcversion, - sizeof(mod->srcversion) - 1); + mod->need_srcversion =3D true; } =20 parse_elf_finish(&info); @@ -1736,6 +1736,72 @@ static void read_symbols(const char *modname) } } =20 +static struct module **srcversion_mods; +static unsigned int nr_srcversion_mods, next_srcversion_mod; + +static bool get_next_src_version(void) +{ + struct module *mod; + unsigned int idx; + + idx =3D __sync_fetch_and_add(&next_srcversion_mod, 1); + if (idx >=3D nr_srcversion_mods) + return false; + mod =3D srcversion_mods[idx]; + + get_src_version(mod->name, mod->srcversion, + sizeof(mod->srcversion) - 1); + return true; +} + +static void *srcversion_worker(void *arg) +{ + while (get_next_src_version()) + ; + + return NULL; +} + +static void hash_srcversions(void) +{ + unsigned int i =3D 0; + struct module *mod; + pthread_t *threads; + long nr_threads; + + list_for_each_entry(mod, &modules, list) + if (mod->need_srcversion) + nr_srcversion_mods++; + + if (!nr_srcversion_mods) + return; + + srcversion_mods =3D xmalloc(nr_srcversion_mods * sizeof(*srcversion_mods)= ); + + list_for_each_entry(mod, &modules, list) + if (mod->need_srcversion) + srcversion_mods[i++] =3D mod; + + nr_threads =3D sysconf(_SC_NPROCESSORS_ONLN); + nr_threads =3D nr_threads < 1 ? 1 : nr_threads; /* On error assume 1. */ + if (nr_threads > nr_srcversion_mods) + nr_threads =3D nr_srcversion_mods; + + sumversion_init(); + threads =3D xmalloc(nr_threads * sizeof(*threads)); + for (i =3D 0; i < nr_threads; i++) { + if (pthread_create(&threads[i], NULL, srcversion_worker, NULL)) { + perror("pthread_create"); + exit(1); + } + } + for (i =3D 0; i < nr_threads; i++) + pthread_join(threads[i], NULL); + + free(threads); + free(srcversion_mods); +} + static void read_symbols_from_files(const char *filename) { FILE *in =3D stdin; @@ -2729,6 +2795,8 @@ int main(int argc, char **argv) if (files_source) read_symbols_from_files(files_source); =20 + hash_srcversions(); + list_for_each_entry(mod, &modules, list) { keep_no_trim_symbols(mod); =20 diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h index d5f6d82837d5..10d5f8f2f293 100644 --- a/scripts/mod/modpost.h +++ b/scripts/mod/modpost.h @@ -127,6 +127,7 @@ struct module { bool has_init; bool has_cleanup; char srcversion[25]; + bool need_srcversion; // Missing namespace dependencies struct list_head missing_namespaces; // Actual imported namespaces @@ -213,6 +214,7 @@ void handle_moddevtable(struct module *mod, struct elf_= info *info, Elf_Sym *sym, const char *symname); =20 /* sumversion.c */ +void sumversion_init(void); void get_src_version(const char *modname, char sum[], unsigned sumlen); =20 /* from modpost.c */ diff --git a/scripts/mod/sumversion.c b/scripts/mod/sumversion.c index 5501d6aa0bea..4521b92ef868 100644 --- a/scripts/mod/sumversion.c +++ b/scripts/mod/sumversion.c @@ -249,7 +249,7 @@ static int parse_comment(const char *file, unsigned lon= g len) /* FIXME: Handle .s files differently (eg. # starts comments) --RR */ static bool stop_char[256]; =20 -static void sumversion_init(void) +void sumversion_init(void) { static bool done; int chr; @@ -402,7 +402,6 @@ static int parse_source_files(const char *objfile, stru= ct md4_ctx *md) line, strerror(errno)); goto out_file; } - } =20 } --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 01CE93D0903; Tue, 8 Sep 2026 20:58:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901136; cv=none; b=Fbk3SjnwYh9TrNPzggAL0en7s0uI0v8ONfKnK9NEQFYnxfVIcUa6tNJnYkgLgQBi4jfO1QHwhxBuUzhN0NBAUcv0zwQ5QBtC514JZpaJTfUaRcgqyuziDmpa9NUJQ7X/Z02UEHPX/tPe6gPia4lHxFcTh/YYQEz5kjBpZgfXGNI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901136; c=relaxed/simple; bh=GECroYhAJRtbaOUcpsKGNrj8jn4Rk2wIoJhoCXYcSZk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NPsJRUtrgVpNjgM+0rN3ZkFAh9QHJ65lLKWQSGFmhFfQpLRG965Umh1Hrt1rdAzH0jTtiJzMinfFhvvBUgj0qo/hVeoWFQ3o3XMtadDx2s19h8rGTFrHt8qQIFXmE7g9HUXMFfVkH5QgMZyE4b41/NDzA4H9OxPsg1Ml0qcsGA8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=k8Aa7bte; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="k8Aa7bte" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 210C81F00A3E; Tue, 8 Sep 2026 20:58:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788901134; bh=ZQGcnADkM00DeAcZFfjjVHZiAQFuNA2hTkX5GoWoQQM=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=k8Aa7bteYxy8yNnT7VeUUCde2XDQEokJ337k5J8MEZlkCr1wOKQZsT2xgvltQXDcc nAdeplSUId+0I8KQ1uJFIoI7E5sCdn8TzNXJyZRIV1aq+b95O8Qf2Q3kR/TzSB0Sva zajxmwDMZA0FlLLKmIwn2Nr4LTeD2wL2lITFUW8axp+TnNRzzcDz9kI+MGNCzXy2sy bjFdNvBTNzIfGfgcBvV0sp9/MItu+53ZYlQ4JEIHo0/mOIx4veVaRfkRFzIBL4GhG3 jGvxuHZARKucauMaVVoDmx0ijpjCYolmuDJIiDk8mJ3eUCGF7wSfuFeztvJUXwpTzc sWK04ZyRIrKdw== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:18 +0100 Subject: [PATCH 18/23] objtool: cache relocations and function dead end state, do less work Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-18-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=14395; i=ljs@kernel.org; h=from:subject:message-id; bh=GECroYhAJRtbaOUcpsKGNrj8jn4Rk2wIoJhoCXYcSZk=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlFk8TE8Jle2MOLv2Q9HNi2ePeyxwvZe274Oz6zx3s eSpcq3VHaUsDGJcDLJiiizPv4jvDxIJm9d5wd8NZg4rE8gQBi5OAZjI1bkM/z3l02SLuj+lPgo7 7aUxY2354p39c+Ncn9mX5l8sLFny24yR4WvpnB9vmG7VbTDfFLhqlZ3g1oUBxz2uXKqc+lhiy5F bD3gB X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 Instruction relocations are looked up by destination in objtool via a hash which is keyed on a 16-byte (OFFSET_STRIDE) window within the section being walked. It iterates through each 16-byte window, looking up relocations over several passes, before moving on to the next 16-byte window, caching only when a relocation is not found saving further lookups in this case. Improve upon this by introducing a per-section relocation cache storing the first relocation at or after each 64-byte window of data (an empirically determined index range), indexed by chunk. The lookup is implemented an array lookup and touches no shared state, so can be used from multiple threads. This relies upon the entries within a section being sorted, which is the case for all sections supplied to objtool by the link step during the kernel build. For cases where sections are supplied out of order or grown one relocation at a time (e.g. livepatch), fall back to using the existing hash mechanism. DWARF sections are a special case - their relocations are never looked up by destination at all and only need to be on their symbol's list for elf_update_sym_relocs(). So do not index or hash DWARF relocations at all - however add a mechanism such that if one were ever looked up, a linear scan will be used. This is meaningful in practice as on an x86-64 kernel build with CONFIG_DEBUG_INFO set objtool processing of vmlinux.o is dominated by DWARF section processing. For an allmodconfig build ~9 million relocations were hashed, and ~8.2 million of those were DWARF sections, which added overhead on cache miss and pollution of the hash table. This is now eliminated. The hash, when used, is read-mostly (every jump, call and memory operand) and several relocations share a key due to the 16-byte stride, so keep the hash sparse by scaling the hash by the number of objects to be hashed. Another hotspot in objtool processing is determining 'dead end' functions, i.e. functions which never return. Implement a simple boolean per-function cache for this to avoid determining this more than once per function. The output of objtool before and after this change was confirmed to be byte-for-byte identical both for x86_64 defconfig and allmodconfig with gcc and clang. objtool on vmlinux.o is on the serial tail of every build that links vmlinux, no-op builds are unchanged. Whole build, 128-thread Threadripper 9980X, best of N runs: before after delta ------------------------------- x86 defconfig, touch mm/vma.c, gcc 8.4s 8.1s -0.28s (-3%) x86 defconfig, touch mm/vma.c, clang 7.5s 7.1s -0.41s (-5%) x86 defconfig, clean, gcc 27.1s 26.8s -0.34s (-1%) x86 defconfig, clean, clang 26.6s 26.2s -0.40s (-1%) x86 allmodconfig, touch mm/vma.c, gcc 30.2s 28.4s -1.8s (-6%) x86 allmodconfig, touch mm/vma.c, clang 28.1s 26.0s -2.1s (-7%) Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- tools/objtool/check.c | 10 +- tools/objtool/elf.c | 242 ++++++++++++++++++++++++++++++++= +--- tools/objtool/include/objtool/elf.h | 4 + 3 files changed, 238 insertions(+), 18 deletions(-) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 464f6c9d9ff0..2abd41cc3aaf 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -305,7 +305,15 @@ static bool __dead_end_function(struct objtool_file *f= ile, struct symbol *func, =20 static bool dead_end_function(struct objtool_file *file, struct symbol *fu= nc) { - return __dead_end_function(file, func, 0); + if (!func) + return false; + + if (!func->dead_end_known) { + func->dead_end =3D __dead_end_function(file, func, 0); + func->dead_end_known =3D 1; + } + + return func->dead_end; } =20 static void init_cfi_state(struct cfi_state *cfi) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index a791f4ea6ec1..13073dc72481 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -316,18 +316,117 @@ struct symbol *find_global_symbol_by_name(const stru= ct elf *elf, const char *nam return NULL; } =20 -/* If there are multiple matches, return the first one in the range */ -struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct secti= on *sec, +static bool is_dwarf_section(struct section *sec) +{ + return !strncmp(sec->name, ".debug_", 7); +} + +/* Cache relocations at a 64 byte granularity. */ +#define RELOC_CACHE_INDEX_SHIFT 6 + +static unsigned long reloc_cache_index(unsigned long offset) +{ + return offset >> RELOC_CACHE_INDEX_SHIFT; +} + +static unsigned int reloc_cache_nr_windows(const struct section *rsec) +{ + const unsigned long size =3D sec_size(rsec->base); + + return (size >> RELOC_CACHE_INDEX_SHIFT) + 1; +} + +static int init_reloc_cache(struct section *rsec) +{ + const unsigned int nr_relocs =3D sec_num_entries(rsec); + const unsigned int nr_windows =3D reloc_cache_nr_windows(rsec); + unsigned int reloc_idx, next_cache_idx =3D 0; + + rsec->reloc_cache =3D malloc(nr_windows * sizeof(unsigned int)); + if (!rsec->reloc_cache) { + ERROR_GLIBC("malloc"); + return -1; + } + + /* Populate relocation indexes reloc cache index -> reloc index. */ + for (reloc_idx =3D 0; reloc_idx < nr_relocs; reloc_idx++) { + struct reloc *reloc =3D &rsec->relocs[reloc_idx]; + const unsigned long offset =3D reloc_offset(reloc); + const unsigned long cache_idx =3D reloc_cache_index(offset); + + if (cache_idx >=3D nr_windows) + break; + + while (next_cache_idx <=3D cache_idx) + rsec->reloc_cache[next_cache_idx++] =3D reloc_idx; + } + + while (next_cache_idx < nr_windows) + rsec->reloc_cache[next_cache_idx++] =3D nr_relocs; + + rsec->sorted =3D true; + return 0; +} + +static void free_reloc_cache(struct section *rsec) +{ + free(rsec->reloc_cache); + rsec->reloc_cache =3D NULL; + rsec->sorted =3D false; +} + +static struct reloc *find_reloc_sorted(struct section *rsec, unsigned long offset, unsigned int len) { - struct reloc *reloc, *r =3D NULL; - struct section *rsec; - unsigned long o; + struct reloc *relocs =3D rsec->relocs; + const unsigned int nr_relocs =3D sec_num_entries(rsec); + const unsigned long cache_idx =3D reloc_cache_index(offset); + unsigned int reloc_idx, i; =20 - rsec =3D sec->rsec; - if (!rsec) + if (cache_idx >=3D reloc_cache_nr_windows(rsec)) + return NULL; + + reloc_idx =3D rsec->reloc_cache[cache_idx]; + + /* + * Scan through all relocations covered by cache entry to find the + * first at or after offset. Relocations are sorted by offset. + */ + for (i =3D reloc_idx; i < nr_relocs; i++) { + struct reloc *reloc =3D &relocs[i]; + const unsigned long curr_offset =3D reloc_offset(reloc); + + if (curr_offset >=3D offset) + break; + + reloc_idx++; + } + + /* Nothing found, or the first candidate lies beyond the range. */ + if (reloc_idx >=3D nr_relocs || + reloc_offset(&relocs[reloc_idx]) >=3D offset + len) return NULL; =20 + /* If there are duplicate entries, return the last. */ + for (i =3D reloc_idx; i < nr_relocs - 1; i++) { + struct reloc *reloc =3D &relocs[i]; + struct reloc *next_reloc =3D &relocs[i + 1]; + + if (reloc_offset(next_reloc) !=3D reloc_offset(reloc)) + break; + reloc_idx++; + } + + return &relocs[reloc_idx]; +} + +/* Not indexed, so look it up in the hash. */ +static struct reloc *find_reloc_hash(const struct elf *elf, struct section= *rsec, + unsigned long offset, unsigned int len) +{ + unsigned long o; + struct reloc *reloc, *r =3D NULL; + for_offset_range(o, offset, offset + len) { elf_hash_for_each_possible(elf, reloc, reloc, hash, sec_offset_hash(rsec, o)) { @@ -347,14 +446,48 @@ struct reloc *find_reloc_by_dest_range(const struct e= lf *elf, struct section *se return r; } =20 -struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *se= c, unsigned long offset) +/* Should never be invoked, provided as a backstop. */ +static struct reloc *find_reloc_linear(struct section *rsec, + unsigned long offset, unsigned int len) { - return find_reloc_by_dest_range(elf, sec, offset, 1); + struct reloc *reloc, *first =3D NULL; + + WARN("%s: linear scan for sec %s with %u relocs at offset %lu len %u", + __func__, rsec->name, sec_num_entries(rsec), offset, len); + + for_each_reloc(rsec, reloc) { + if (reloc_offset(reloc) < offset || + reloc_offset(reloc) >=3D offset + len) + continue; + + if (!first || reloc_offset(reloc) < reloc_offset(first)) + first =3D reloc; + } + + return first; } =20 -static bool is_dwarf_section(struct section *sec) +/* If there are multiple matches, return the first one in the range. */ +struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct secti= on *sec, + unsigned long offset, unsigned int len) { - return !strncmp(sec->name, ".debug_", 7); + struct section *rsec =3D sec->rsec; + + if (!rsec) + return NULL; + + if (rsec->sorted) + return find_reloc_sorted(rsec, offset, len); + + if (rsec->hashed) + return find_reloc_hash(elf, rsec, offset, len); + + return find_reloc_linear(rsec, offset, len); +} + +struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *se= c, unsigned long offset) +{ + return find_reloc_by_dest_range(elf, sec, offset, 1); } =20 static int read_sections(struct elf *elf) @@ -1071,7 +1204,8 @@ struct reloc *elf_init_reloc(struct elf *elf, struct = section *rsec, set_reloc_type(elf, reloc, type); set_reloc_addend(elf, reloc, addend); =20 - elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); + if (rsec->hashed) + elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); set_sym_next_reloc(reloc, sym->relocs); sym->relocs =3D reloc; =20 @@ -1123,18 +1257,44 @@ struct reloc *elf_init_reloc_data_sym(struct elf *e= lf, struct section *sec, elf_data_rela_type(elf)); } =20 +static u64 raw_reloc_offset(const struct section *rsec, unsigned int idx) +{ + const void *entry =3D rsec->data->d_buf + idx * rsec->sh.sh_entsize; + + if (rsec->sh.sh_entsize < sizeof(Elf64_Rel)) + return ((const Elf32_Rela *)entry)->r_offset; + + return ((const Elf64_Rela *)entry)->r_offset; +} + +static bool reloc_sec_in_order(struct section *rsec) +{ + const unsigned int nr_relocs =3D sec_num_entries(rsec); + u64 prev_offset =3D 0; + unsigned int i; + + for (i =3D 0; i < nr_relocs; i++) { + /* Called before relocs exist, so look at raw entry. */ + const u64 offset =3D raw_reloc_offset(rsec, i); + + if (offset < prev_offset) + return false; + prev_offset =3D offset; + } + + return true; +} + static int read_relocs(struct elf *elf) { - unsigned long nr_reloc, max_reloc =3D 0; + unsigned long nr_reloc, max_reloc =3D 0, nr_hashed =3D 0; struct section *rsec; struct reloc *reloc; unsigned int symndx; struct symbol *sym; + bool hashed; int i; =20 - if (!elf_alloc_hash(reloc, elf->num_relocs)) - return -1; - list_for_each_entry(rsec, &elf->sections, list) { if (!is_reloc_sec(rsec)) continue; @@ -1147,6 +1307,28 @@ static int read_relocs(struct elf *elf) =20 rsec->base->rsec =3D rsec; =20 + /* DWARF relocs are never looked up. */ + if (is_dwarf_section(rsec->base)) + continue; + if (reloc_sec_in_order(rsec)) { + rsec->sorted =3D true; + continue; + } + + rsec->hashed =3D true; + nr_hashed +=3D sec_num_entries(rsec); + } + + /* Read mostly, so avoid collisions and keep the hash sparse. */ + if (!elf_alloc_hash(reloc, nr_hashed * OFFSET_STRIDE)) + return -1; + + list_for_each_entry(rsec, &elf->sections, list) { + if (!is_reloc_sec(rsec)) + continue; + + hashed =3D rsec->hashed; + /* nr_alloc_relocs=3D0: libelf owns d_buf */ rsec->nr_alloc_relocs =3D 0; =20 @@ -1168,18 +1350,23 @@ static int read_relocs(struct elf *elf) return -1; } =20 - elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); + if (hashed) + elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); set_sym_next_reloc(reloc, sym->relocs); sym->relocs =3D reloc; =20 nr_reloc++; } max_reloc =3D max(max_reloc, nr_reloc); + + if (rsec->sorted && init_reloc_cache(rsec)) + return -1; } =20 if (opts.stats) { printf("max_reloc: %lu\n", max_reloc); printf("num_relocs: %lu\n", elf->num_relocs); + printf("num_relocs_hashed: %lu\n", nr_hashed); printf("reloc_bits: %d\n", elf->reloc_bits); } =20 @@ -1541,6 +1728,26 @@ struct section *elf_create_section(struct elf *elf, = const char *name, return sec; } =20 +/* A relocation was appended, abandon relocation cache and use hash instea= d. */ +static void copy_reloc_cache_to_hash(struct elf *elf, struct section *rsec, + unsigned int nr_relocs) +{ + unsigned int i; + + if (rsec->hashed) + return; + + if (rsec->sorted) + free_reloc_cache(rsec); + + for (i =3D 0; i < nr_relocs; i++) { + struct reloc *reloc =3D &rsec->relocs[i]; + + elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); + } + rsec->hashed =3D true; +} + static int elf_alloc_reloc(struct elf *elf, struct section *rsec) { struct reloc *old_relocs, *old_relocs_end, *new_relocs; @@ -1592,6 +1799,7 @@ static int elf_alloc_reloc(struct elf *elf, struct se= ction *rsec) } =20 rsec->nr_alloc_relocs =3D nr_alloc; + copy_reloc_cache_to_hash(elf, rsec, nr_relocs_old); =20 old_relocs =3D rsec->relocs; new_relocs =3D calloc(nr_alloc, sizeof(struct reloc)); diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index a82517a76a0f..0d61ddfec05f 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -59,6 +59,8 @@ struct section { const char *name; int idx; bool _changed, text, rodata, noinstr, init, truncate; + bool hashed, sorted; + unsigned int *reloc_cache; struct reloc *relocs; unsigned long nr_alloc_relocs; struct section *twin; @@ -98,6 +100,8 @@ struct symbol { u8 klp : 1; u8 dont_correlate : 1; u8 fake : 1; + u8 dead_end_known : 1; + u8 dead_end : 1; struct list_head pv_target; struct reloc *relocs; struct section *group_sec; --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 647E8426429; Tue, 8 Sep 2026 20:59:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901148; cv=none; b=UVFJWMsKirbLBN4IsVGPhG5uiPjNPS4d44ushHEbTUDQUZzkmV/ypdHe9cJxJqh4+0i0wkRNohOR6Vk13vdCaDn9Q4li+1j8btHyrGjC7z2vM0jLpZjRlq1GZDvaZsu/BAWOzSEkSf4JUCtcjJ8NKJ/WYXxL+p2Ekt73E+gCRS4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901148; c=relaxed/simple; bh=/v8LA4EYYewhFC4I2H+iwnOjDqUe/rJIsTneIDuzqV4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Cm/9Gy9XsZfEIZeijgLjxJOwU/tpyamRFrXROpajm6j7zyDzAxZ5d8LUe+MiLQOlSXFj4VWYkKoX00JsKvKgiMX+D5NbNXKgm77VbBp9AgShtNMS07rHIL1qB3Qo0Gwx+7pdaeOE9VEds2Wdu2bYHNEXvOTvO8fvsSIH8cBJWOY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hDxupDjC; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hDxupDjC" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 60D6C1F00A3A; Tue, 8 Sep 2026 20:58:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788901146; bh=difqveWtS9vdd8yi7vuhs8PygfdwBMTeA0uptS/s1Y0=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=hDxupDjCVXQnLjuIMnEbUQo9oIPx44EhH6AxsPzShtLmLjQPHwHLZLUZHL9iDKzdF /6GjR0Yd+boSVbS22KEuOOMJM/yw7Mpn092vwHzctbNJlB+TUi47K0fd00c+1YnDHt kon91tjJmCyrAPRofLKymZj71fBQxXdfhmb66HnEVMps8BSdrzNushuFUWK0dKgwWK hZOJjMwRpOFsF1FLQCwibuj3UWk1kPtsfBQyaRrN9U60X59P5PrDgGEDhCsWEscZa6 Yte6+fdIL/6DSMVNFMGg4nbvH9CnmiPjy2stlz/9rG1lMyu1y6KTrX56+H9v1dpEjz QtH9Ty4Jvq3dA== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:19 +0100 Subject: [PATCH 19/23] objtool: decode instructions and resolve branch targets in parallel Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-19-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=29639; i=ljs@kernel.org; h=from:subject:message-id; bh=/v8LA4EYYewhFC4I2H+iwnOjDqUe/rJIsTneIDuzqV4=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlFn48j+aukdgy+boC/9U+xv33xSI/7llQlK03ItLR 6f4LmI731HKwiDGxSArpsjy/Iv4/iCRsHmdF/zdYOawMoEMYeDiFICJ+HozMrRqL8up7ffs0ria dlo5inHhtEsTrDfVfXlQsDxcWfBi/jOGP/ybJ9ZZhgmnh95V437E+yhXOu7/8bLHP2adcz5vnhG 8nhUA X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 During a kernel build objtool is used to decode vmlinux.o's instructions and resolve every jump and call destination. This forms a large part of the work objtool does during the build process, and it is all done in serial. Decode these in parallel at a function granularity to speed things up. Only the instruction hash is shared between the threads and nothing is ever removed from it, so an insertion is a compare-and-swap on the bucket head. Also update the instruction hash to be more efficient - it was previously hardcoded to a size of 2^20 buckets. This is insufficient for an x86-64 allmodconfig kernel where an x86-64 build decodes ~16 million instructions and in practice find_insn() was observed working chains of four or more entries on lookup. Instead size it based on the amount of text to be decoded at roughly one bucket per instruction, identical to the relocation hash (set to OFFSET_STRIDE). This results in 2^20 buckets (~8 MiB memory usage) for a defconfig vmlinux.o and 2^22 (~32 MiB memory usage) for an allmodconfig kernel, so it is not an egregious use of memory. Threads are only created for objects with 8 MiB or more of text, meaning that runs involving smaller objects remain unaffected. Only decoding and the branch passes are threaded, so the gain flattens out at 16 threads and more only add overhead - on the clang allmodconfig vmlinux.o objtool takes 5.58s on 1 thread, 3.96s on 8, 3.91s on 16 and 4.03s on 128. Cap the thread count at 16, or the number of CPUs if fewer. The passes which resolve jump and call destinations and annotate call sites all run over the same regions. They only write to lists in struct objtool_file, so have each range work with their own copy of this data structure, which are then joined, in range order afterwards in a map-reduce fashion. The output of objtool before and after this change was confirmed to be byte-for-byte identical for x86_64 defconfig and allmodconfig with gcc and clang, and for a loongarch defconfig, where objtool runs on every object. On a 128-thread machine, objtool on the clang allmodconfig vmlinux.o goes from 5.9s to 3.7s (6.5s to 3.7s together with the previous patch), and on defconfig from 1.87s to 1.26s. objtool on vmlinux.o is on the serial tail of every build that links vmlinux, no-op builds are unchanged. Whole build, 128-thread Threadripper 9980X, best of N runs: before after delta ------------------------------- x86 defconfig, touch mm/vma.c, gcc 8.1s 7.5s -0.65s (-8%) x86 defconfig, touch mm/vma.c, clang 7.1s 6.6s -0.51s (-7%) x86 defconfig, clean, gcc 26.8s 26.3s -0.46s (-2%) x86 defconfig, clean, clang 26.2s 25.8s -0.45s (-2%) x86 allmodconfig, touch mm/vma.c, gcc 28.4s 23.7s -4.7s (-17%) x86 allmodconfig, touch mm/vma.c, clang 26.0s 22.1s -3.9s (-15%) Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- tools/objtool/Makefile | 2 +- tools/objtool/check.c | 757 ++++++++++++++++++++++++----= ---- tools/objtool/include/objtool/objtool.h | 3 +- tools/objtool/objtool.c | 1 - 4 files changed, 578 insertions(+), 185 deletions(-) diff --git a/tools/objtool/Makefile b/tools/objtool/Makefile index a4484fd22a96..2de50c3917ba 100644 --- a/tools/objtool/Makefile +++ b/tools/objtool/Makefile @@ -63,7 +63,7 @@ INCLUDES :=3D -I$(srctree)/tools/include \ OBJTOOL_CFLAGS :=3D -std=3Dgnu11 -fomit-frame-pointer -O2 -g $(WARNINGS) \ $(INCLUDES) $(LIBELF_FLAGS) $(LIBXXHASH_CFLAGS) $(HOSTCFLAGS) =20 -OBJTOOL_LDFLAGS :=3D $(LIBSUBCMD) $(LIBELF_LIBS) $(LIBXXHASH_LIBS) $(HOSTL= DFLAGS) +OBJTOOL_LDFLAGS :=3D $(LIBSUBCMD) $(LIBELF_LIBS) $(LIBXXHASH_LIBS) -lpthre= ad $(HOSTLDFLAGS) =20 # Allow old libelf to be used: elfshdr :=3D $(shell echo '$(pound)include ' | $(HOSTCC) $(OBJTO= OL_CFLAGS) -x c -E - 2>/dev/null | grep elf_getshdr) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 2abd41cc3aaf..b092cf8d582f 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -7,6 +7,9 @@ #include #include #include +#include +#include +#include #include #include =20 @@ -24,6 +27,7 @@ #include #include #include +#include #include #include =20 @@ -38,12 +42,22 @@ struct disas_context *objtool_disas_ctx; =20 size_t sym_name_max_len; =20 +static struct hlist_head *insn_hash_head(struct objtool_file *file, + struct section *sec, unsigned long offset) +{ + /* Determine instruction hash based on section index and offset. */ + const u32 sec_hash =3D sec_offset_hash(sec, offset); + const u32 hash =3D hash_min(sec_hash, file->insn_hash_bits); + + return &file->insn_hash[hash]; +} + struct instruction *find_insn(struct objtool_file *file, struct section *sec, unsigned long offset) { struct instruction *insn; =20 - hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, = offset)) { + hlist_for_each_entry(insn, insn_hash_head(file, sec, offset), hash) { if (insn->sec =3D=3D sec && insn->offset =3D=3D offset) return insn; } @@ -54,14 +68,13 @@ struct instruction *find_insn(struct objtool_file *file, struct instruction *next_insn_same_sec(struct objtool_file *file, struct instruction *insn) { - if (insn->idx =3D=3D INSN_CHUNK_MAX) - return find_insn(file, insn->sec, insn->offset + insn->len); + const unsigned long next_offset =3D insn->offset + insn->len; =20 - insn++; - if (!insn->len) - return NULL; + /* A chunk ends at its last slot or an empty one, so look the next up. */ + if (insn->idx =3D=3D INSN_CHUNK_MAX || !insn[1].len) + return find_insn(file, insn->sec, next_offset); =20 - return insn; + return insn + 1; } =20 struct instruction *next_insn_same_func(struct objtool_file *file, @@ -411,21 +424,391 @@ static void *cfi_hash_alloc(unsigned long size) static unsigned long nr_insns; static unsigned long nr_insns_visited; =20 +/* Only an object this large, e.g. vmlinux.o, is decoded on several thread= s. */ +#define DECODE_THREADED_MIN_TEXT SZ_8M +/* Only decoding and the branch passes are threaded, so more gains nothing= . */ +#define DECODE_MAX_THREADS 16 +#define DECODE_RANGES_PER_THREAD 4 + +/* + * sec_offset_hash() keys on OFFSET_STRIDE windows, so the instructions of= a + * window share a chain and buckets beyond one per window would sit empty. + */ +#define INSN_HASH_BYTES_PER_BUCKET OFFSET_STRIDE +#define INSN_HASH_MIN_BITS 10 + +static unsigned long total_text_size(struct objtool_file *file) +{ + unsigned long size =3D 0; + struct section *sec; + + for_each_sec(file->elf, sec) + if (is_text_sec(sec)) + size +=3D sec_size(sec); + + return size; +} + +static int alloc_insn_hash(struct objtool_file *file, unsigned long text_s= ize) +{ + const unsigned long nr_buckets =3D text_size / INSN_HASH_BYTES_PER_BUCKET; + const int bits =3D ilog2(nr_buckets); + + file->insn_hash_bits =3D max(INSN_HASH_MIN_BITS, bits); + file->insn_hash =3D calloc(1UL << file->insn_hash_bits, + sizeof(*file->insn_hash)); + if (!file->insn_hash) { + ERROR_GLIBC("calloc"); + return -1; + } + + if (opts.stats) + printf("insn_hash_bits: %d\n", file->insn_hash_bits); + + return 0; +} + +/* Per-thread state, only instruction hash is shared. */ +struct insn_range { + struct section *sec; + unsigned long start, end; + struct instruction *first, *last; + unsigned long nr_insns; + int ret; + + /* + * Each thread writes to its own copy of an objtool file, which are + * combined upon completion. + */ + struct objtool_file shadow; +}; + +#define range_for_each_insn(file, range, insn) \ + for (insn =3D (range)->first; \ + insn && insn->offset < (range)->end; \ + insn =3D next_insn_same_sec(file, insn)) + +typedef int (*range_fn_t)(struct objtool_file *file, struct insn_range *ra= nge); + +struct range_work { + range_fn_t fn; +}; + +static struct insn_range *decode_ranges; +static unsigned int nr_decode_ranges, next_decode_range, nr_decode_threads; + +/* The kernel's try_cmpxchg(); the tools' cmpxchg() is host-arch only. */ +static bool hlist_try_cmpxchg(struct hlist_node **ptr, struct hlist_node *= *old, + struct hlist_node *new) +{ + struct hlist_node *seen =3D __sync_val_compare_and_swap(ptr, *old, new); + + if (seen =3D=3D *old) + return true; + + *old =3D seen; + return false; +} + +/* Nothing is ever removed, so push onto the bucket as llist_add() does. */ +static void insn_hash_add(struct objtool_file *file, struct instruction *i= nsn) +{ + struct hlist_head *head =3D insn_hash_head(file, insn->sec, insn->offset); + struct hlist_node *first =3D head->first; + + insn->hash.pprev =3D &head->first; + do { + insn->hash.next =3D first; + } while (!hlist_try_cmpxchg(&head->first, &first, &insn->hash)); +} + +/* The slot after prev in its chunk, or the first of a new chunk. */ +static struct instruction *next_insn_slot(struct instruction *prev) +{ + struct instruction *insn; + + if (prev && prev->idx < INSN_CHUNK_MAX) { + insn =3D prev + 1; + insn->idx =3D prev->idx + 1; + return insn; + } + + insn =3D calloc(INSN_CHUNK_SIZE, sizeof(*insn)); + if (!insn) + ERROR_GLIBC("calloc"); + + return insn; +} + +static int decode_range(struct objtool_file *file, struct insn_range *rang= e) +{ + struct instruction *insn =3D NULL; + struct section *sec =3D range->sec; + unsigned long offset; + u8 prev_len =3D 0; + + for (offset =3D range->start; offset < range->end; offset +=3D insn->len)= { + const unsigned long remaining =3D sec_size(sec) - offset; + + insn =3D next_insn_slot(insn); + if (!insn) + return -1; + + INIT_LIST_HEAD(&insn->call_node); + insn->sec =3D sec; + insn->offset =3D offset; + insn->prev_len =3D prev_len; + + if (arch_decode_instruction(file, sec, offset, remaining, insn)) + return -1; + + prev_len =3D insn->len; + + if (insn->type =3D=3D INSN_BUG) + insn->dead_end =3D true; + + insn_hash_add(file, insn); + if (!range->first) + range->first =3D insn; + range->nr_insns++; + } + range->last =3D insn; + + /* The range ends at a function symbol, so decoding must land on it. */ + if (offset !=3D range->end) { + ERROR("%s: no instruction boundary at %s", sec->name, + offstr(sec, range->end)); + return -1; + } + + return 0; +} + +static int run_threads(void *(*fn)(void *), void *arg, unsigned int nr_thr= eads) +{ + unsigned int nr_started, i; + pthread_t *threads; + int ret =3D 0; + + if (nr_threads <=3D 1) { + fn(arg); + return 0; + } + + threads =3D calloc(nr_threads, sizeof(*threads)); + if (!threads) { + ERROR_GLIBC("calloc"); + return -1; + } + + for (nr_started =3D 0; nr_started < nr_threads; nr_started++) { + if (pthread_create(&threads[nr_started], NULL, fn, arg)) { + ERROR_GLIBC("pthread_create"); + ret =3D -1; + break; + } + } + + for (i =3D 0; i < nr_started; i++) + pthread_join(threads[i], NULL); + + free(threads); + return ret; +} + +/* Hand out the ranges one at a time, or NULL once they are all taken. */ +static struct insn_range *claim_decode_range(void) +{ + const unsigned int idx =3D __sync_fetch_and_add(&next_decode_range, 1); + + return idx < nr_decode_ranges ? &decode_ranges[idx] : NULL; +} + +static void *range_worker(void *arg) +{ + const struct range_work *work =3D arg; + struct insn_range *range; + + while ((range =3D claim_decode_range())) + range->ret =3D work->fn(&range->shadow, range); + + return NULL; +} + +/* The lists in the objtool_file that the passes add instructions to. */ +static const size_t shadow_list_offsets[] =3D { + offsetof(struct objtool_file, retpoline_call_list), + offsetof(struct objtool_file, return_thunk_list), + offsetof(struct objtool_file, static_call_list), + offsetof(struct objtool_file, mcount_loc_list), + offsetof(struct objtool_file, endbr_list), + offsetof(struct objtool_file, call_list), +}; + +static struct list_head *shadow_list(struct objtool_file *file, + unsigned int idx) +{ + return (void *)file + shadow_list_offsets[idx]; +} + +static void init_range_shadow(struct objtool_file *file, + struct insn_range *range) +{ + unsigned int i; + + range->shadow =3D *file; + range->ret =3D 0; + for (i =3D 0; i < ARRAY_SIZE(shadow_list_offsets); i++) + INIT_LIST_HEAD(shadow_list(&range->shadow, i)); +} + +/* Joined in range order, which is the order a single walk would produce. = */ +static int join_range_shadow(struct objtool_file *file, + struct insn_range *range) +{ + unsigned int i; + + for (i =3D 0; i < ARRAY_SIZE(shadow_list_offsets); i++) + list_splice_tail(shadow_list(&range->shadow, i), + shadow_list(file, i)); + + return range->ret; +} + +/* Run a pass over the instructions, one range per thread at a time. */ +static int run_insn_ranges(struct objtool_file *file, range_fn_t fn) +{ + struct range_work work =3D { .fn =3D fn }; + unsigned int i; + int ret =3D 0; + + for (i =3D 0; i < nr_decode_ranges; i++) + init_range_shadow(file, &decode_ranges[i]); + next_decode_range =3D 0; + + if (run_threads(range_worker, &work, nr_decode_threads)) + return -1; + + for (i =3D 0; i < nr_decode_ranges; i++) { + if (join_range_shadow(file, &decode_ranges[i])) + ret =3D -1; + } + + return ret; +} + +static int add_decode_range(struct section *sec, unsigned long start, + unsigned long end) +{ + const size_t size =3D (nr_decode_ranges + 1) * sizeof(*decode_ranges); + struct insn_range *range; + + decode_ranges =3D realloc(decode_ranges, size); + if (!decode_ranges) { + ERROR_GLIBC("realloc"); + return -1; + } + + range =3D &decode_ranges[nr_decode_ranges++]; + memset(range, 0, sizeof(*range)); + range->sec =3D sec; + range->start =3D start; + range->end =3D end; + + return 0; +} + +/* Split a section into ranges of roughly range_size, at function starts. = */ +static int add_decode_ranges(struct section *sec, unsigned long range_size) +{ + const unsigned long size =3D sec_size(sec); + unsigned long start =3D 0; + struct symbol *sym; + + if (!range_size) + return add_decode_range(sec, 0, size); + + sec_for_each_sym(sec, sym) { + if (!is_func_sym(sym) || sym->offset <=3D start || + sym->offset >=3D size) + continue; + if (sym->offset - start < range_size) + continue; + + if (add_decode_range(sec, start, sym->offset)) + return -1; + start =3D sym->offset; + } + + return add_decode_range(sec, start, size); +} + +static void free_decode_ranges(void) +{ + free(decode_ranges); + decode_ranges =3D NULL; + nr_decode_ranges =3D 0; + next_decode_range =3D 0; +} + +/* A range's first instruction follows the last of the range before it. */ +static void link_decode_ranges(void) +{ + unsigned int i; + + for (i =3D 1; i < nr_decode_ranges; i++) { + const struct insn_range *prev =3D &decode_ranges[i - 1]; + struct insn_range *range =3D &decode_ranges[i]; + + if (prev->sec !=3D range->sec || !prev->last || !range->first) + continue; + + range->first->prev_len =3D prev->last->len; + } +} + +static unsigned int decode_threads(unsigned long text_size) +{ + const long nr_cpus =3D sysconf(_SC_NPROCESSORS_ONLN); + + if (text_size < DECODE_THREADED_MIN_TEXT || nr_cpus < 2) + return 1; + + return min_t(unsigned int, nr_cpus, DECODE_MAX_THREADS); +} + +/* Several ranges per thread so uneven ones balance out; 0 means per secti= on. */ +static unsigned long decode_range_size(unsigned long text_size, + unsigned int nr_threads) +{ + const unsigned int nr_ranges =3D nr_threads * DECODE_RANGES_PER_THREAD; + + if (nr_threads <=3D 1) + return 0; + + return text_size / nr_ranges; +} + /* * Call the arch-specific instruction decoder for all the instructions and= add * them to the global instruction list. */ static int decode_instructions(struct objtool_file *file) { + const unsigned long text_size =3D total_text_size(file); + unsigned long range_size; + struct instruction *insn; struct section *sec; struct symbol *func; - unsigned long offset; - struct instruction *insn; + unsigned int i; + + if (alloc_insn_hash(file, text_size)) + return -1; + + nr_decode_threads =3D decode_threads(text_size); + range_size =3D decode_range_size(text_size, nr_decode_threads); =20 for_each_sec(file->elf, sec) { - struct instruction *insns =3D NULL; - u8 prev_len =3D 0; - u8 idx =3D 0; =20 if (!is_text_sec(sec)) continue; @@ -450,41 +833,20 @@ static int decode_instructions(struct objtool_file *f= ile) if (!strcmp(sec->name, ".init.text") && !opts.module) sec->init =3D true; =20 - for (offset =3D 0; offset < sec_size(sec); offset +=3D insn->len) { - if (!insns || idx =3D=3D INSN_CHUNK_MAX) { - insns =3D calloc(INSN_CHUNK_SIZE, sizeof(*insn)); - if (!insns) { - ERROR_GLIBC("calloc"); - return -1; - } - idx =3D 0; - } else { - idx++; - } - insn =3D &insns[idx]; - insn->idx =3D idx; - - INIT_LIST_HEAD(&insn->call_node); - insn->sec =3D sec; - insn->offset =3D offset; - insn->prev_len =3D prev_len; - - if (arch_decode_instruction(file, sec, offset, sec_size(sec) - offset, = insn)) - return -1; + if (add_decode_ranges(sec, range_size)) + return -1; + } =20 - prev_len =3D insn->len; + if (run_insn_ranges(file, decode_range)) + return -1; =20 - /* - * By default, "ud2" is a dead end unless otherwise - * annotated, because GCC 7 inserts it for certain - * divide-by-zero cases. - */ - if (insn->type =3D=3D INSN_BUG) - insn->dead_end =3D true; + for (i =3D 0; i < nr_decode_ranges; i++) + nr_insns +=3D decode_ranges[i].nr_insns; + link_decode_ranges(); =20 - hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offse= t)); - nr_insns++; - } + for_each_sec(file->elf, sec) { + if (!is_text_sec(sec)) + continue; =20 sec_for_each_sym(sec, func) { if (!is_notype_sym(func) && !is_func_sym(func)) @@ -1527,133 +1889,147 @@ static bool is_first_func_insn(struct objtool_fil= e *file, /* * Find the destination instructions for all jumps. */ -static int add_jump_destinations(struct objtool_file *file) +static int add_jump_destination(struct objtool_file *file, struct instruct= ion *insn) { - struct instruction *insn; struct reloc *reloc; + struct symbol *func =3D insn_func(insn); + struct instruction *dest_insn; + struct section *dest_sec; + struct symbol *dest_sym; + unsigned long dest_off; =20 - for_each_insn(file, insn) { - struct symbol *func =3D insn_func(insn); - struct instruction *dest_insn; - struct section *dest_sec; - struct symbol *dest_sym; - unsigned long dest_off; + if (!is_static_jump(insn)) + return 0; =20 - if (!is_static_jump(insn)) - continue; + if (insn->jump_dest) { + /* + * handle_group_alt() may have previously set + * 'jump_dest' for some alternatives. + */ + return 0; + } =20 - if (insn->jump_dest) { - /* - * handle_group_alt() may have previously set - * 'jump_dest' for some alternatives. - */ - continue; - } + reloc =3D insn_reloc(file, insn); + if (!reloc) { + dest_sec =3D insn->sec; + dest_off =3D arch_jump_destination(insn); + dest_sym =3D dest_sec->sym; + } else { + dest_sym =3D reloc->sym; + if (is_undef_sym(dest_sym)) { + if (dest_sym->retpoline_thunk) { + if (add_retpoline_call(file, insn)) + return -1; + return 0; + } =20 - reloc =3D insn_reloc(file, insn); - if (!reloc) { - dest_sec =3D insn->sec; - dest_off =3D arch_jump_destination(insn); - dest_sym =3D dest_sec->sym; - } else { - dest_sym =3D reloc->sym; - if (is_undef_sym(dest_sym)) { - if (dest_sym->retpoline_thunk) { - if (add_retpoline_call(file, insn)) - return -1; - continue; - } + if (dest_sym->return_thunk) { + add_return_call(file, insn, true); + return 0; + } =20 - if (dest_sym->return_thunk) { - add_return_call(file, insn, true); - continue; - } + /* External symbol */ + if (func) { + /* External sibling call */ + if (add_call_dest(file, insn, dest_sym, true)) + return -1; + return 0; + } =20 - /* External symbol */ - if (func) { - /* External sibling call */ - if (add_call_dest(file, insn, dest_sym, true)) - return -1; - continue; - } + /* Non-func asm code jumping to external symbol */ + return 0; + } =20 - /* Non-func asm code jumping to external symbol */ - continue; - } + dest_sec =3D dest_sym->sec; + dest_off =3D dest_sym->offset + arch_insn_adjusted_addend(insn, reloc); + } + + dest_insn =3D find_insn(file, dest_sec, dest_off); + if (!dest_insn) { + struct symbol *sym =3D find_symbol_by_offset(dest_sec, dest_off); =20 - dest_sec =3D dest_sym->sec; - dest_off =3D dest_sym->offset + arch_insn_adjusted_addend(insn, reloc); + /* + * retbleed_untrain_ret() jumps to + * __x86_return_thunk(), but objtool can't find + * the thunk's starting RET instruction, + * because the RET is also in the middle of + * another instruction. Objtool only knows + * about the outer instruction. + */ + if (sym && sym->embedded_insn) { + add_return_call(file, insn, false); + return 0; } =20 - dest_insn =3D find_insn(file, dest_sec, dest_off); - if (!dest_insn) { - struct symbol *sym =3D find_symbol_by_offset(dest_sec, dest_off); + /* + * GCOV/KCOV dead code can jump to the end of + * the function/section. + */ + if (file->ignore_unreachables && func && + dest_sec =3D=3D insn->sec && + dest_off =3D=3D func->offset + func->len) + return 0; =20 - /* - * retbleed_untrain_ret() jumps to - * __x86_return_thunk(), but objtool can't find - * the thunk's starting RET instruction, - * because the RET is also in the middle of - * another instruction. Objtool only knows - * about the outer instruction. - */ - if (sym && sym->embedded_insn) { - add_return_call(file, insn, false); - continue; - } + ERROR_INSN(insn, "can't find jump dest instruction at %s", + offstr(dest_sec, dest_off)); + return -1; + } =20 - /* - * GCOV/KCOV dead code can jump to the end of - * the function/section. - */ - if (file->ignore_unreachables && func && - dest_sec =3D=3D insn->sec && - dest_off =3D=3D func->offset + func->len) - continue; + if (!dest_sym || is_sec_sym(dest_sym)) { + dest_sym =3D insn_sym(dest_insn); + if (!dest_sym) + goto set_jump_dest; + } =20 - ERROR_INSN(insn, "can't find jump dest instruction at %s", - offstr(dest_sec, dest_off)); + if (dest_sym->retpoline_thunk && dest_insn->offset =3D=3D dest_sym->offse= t) { + if (add_retpoline_call(file, insn)) return -1; - } + return 0; + } =20 - if (!dest_sym || is_sec_sym(dest_sym)) { - dest_sym =3D insn_sym(dest_insn); - if (!dest_sym) - goto set_jump_dest; - } + if (dest_sym->return_thunk && dest_insn->offset =3D=3D dest_sym->offset) { + add_return_call(file, insn, true); + return 0; + } =20 - if (dest_sym->retpoline_thunk && dest_insn->offset =3D=3D dest_sym->offs= et) { - if (add_retpoline_call(file, insn)) - return -1; - continue; - } + if (!insn_sym(insn) || insn_sym(insn)->pfunc =3D=3D dest_sym->pfunc) + goto set_jump_dest; =20 - if (dest_sym->return_thunk && dest_insn->offset =3D=3D dest_sym->offset)= { - add_return_call(file, insn, true); - continue; - } + /* + * Internal cross-function jump. + */ =20 - if (!insn_sym(insn) || insn_sym(insn)->pfunc =3D=3D dest_sym->pfunc) - goto set_jump_dest; + if (is_first_func_insn(file, dest_insn)) { + /* Internal sibling call */ + if (add_call_dest(file, insn, dest_sym, true)) + return -1; + return 0; + } =20 - /* - * Internal cross-function jump. - */ +set_jump_dest: + insn->jump_dest =3D dest_insn; =20 - if (is_first_func_insn(file, dest_insn)) { - /* Internal sibling call */ - if (add_call_dest(file, insn, dest_sym, true)) - return -1; - continue; - } + return 0; +} =20 -set_jump_dest: - insn->jump_dest =3D dest_insn; +static int add_jump_destinations_range(struct objtool_file *file, + struct insn_range *range) +{ + struct instruction *insn; + + range_for_each_insn(file, range, insn) { + if (add_jump_destination(file, insn)) + return -1; } =20 return 0; } =20 +static int add_jump_destinations(struct objtool_file *file) +{ + return run_insn_ranges(file, add_jump_destinations_range); +} + static struct symbol *find_call_destination(struct section *sec, unsigned = long offset) { struct symbol *call_dest; @@ -1668,64 +2044,79 @@ static struct symbol *find_call_destination(struct = section *sec, unsigned long o /* * Find the destination instructions for all calls. */ -static int add_call_destinations(struct objtool_file *file) +static int add_call_destination(struct objtool_file *file, struct instruct= ion *insn) { - struct instruction *insn; unsigned long dest_off; struct symbol *dest; struct reloc *reloc; + struct symbol *func =3D insn_func(insn); =20 - for_each_insn(file, insn) { - struct symbol *func =3D insn_func(insn); - if (insn->type !=3D INSN_CALL) - continue; + if (insn->type !=3D INSN_CALL) + return 0; =20 - reloc =3D insn_reloc(file, insn); - if (!reloc) { - dest_off =3D arch_jump_destination(insn); - dest =3D find_call_destination(insn->sec, dest_off); + reloc =3D insn_reloc(file, insn); + if (!reloc) { + dest_off =3D arch_jump_destination(insn); + dest =3D find_call_destination(insn->sec, dest_off); =20 - if (add_call_dest(file, insn, dest, false)) - return -1; + if (add_call_dest(file, insn, dest, false)) + return -1; =20 - if (func && func->ignore) - continue; + if (func && func->ignore) + return 0; =20 - if (!insn_call_dest(insn)) { - ERROR_INSN(insn, "unannotated intra-function call"); - return -1; - } + if (!insn_call_dest(insn)) { + ERROR_INSN(insn, "unannotated intra-function call"); + return -1; + } =20 - if (func && !is_func_sym(insn_call_dest(insn))) { - ERROR_INSN(insn, "unsupported call to non-function"); - return -1; - } + if (func && !is_func_sym(insn_call_dest(insn))) { + ERROR_INSN(insn, "unsupported call to non-function"); + return -1; + } =20 - } else if (is_sec_sym(reloc->sym)) { - dest_off =3D arch_insn_adjusted_addend(insn, reloc); - dest =3D find_call_destination(reloc->sym->sec, dest_off); - if (!dest) { - ERROR_INSN(insn, "can't find call dest symbol at %s+0x%lx", - reloc->sym->sec->name, dest_off); - return -1; - } + } else if (is_sec_sym(reloc->sym)) { + dest_off =3D arch_insn_adjusted_addend(insn, reloc); + dest =3D find_call_destination(reloc->sym->sec, dest_off); + if (!dest) { + ERROR_INSN(insn, "can't find call dest symbol at %s+0x%lx", + reloc->sym->sec->name, dest_off); + return -1; + } =20 - if (add_call_dest(file, insn, dest, false)) - return -1; + if (add_call_dest(file, insn, dest, false)) + return -1; =20 - } else if (reloc->sym->retpoline_thunk) { - if (add_retpoline_call(file, insn)) - return -1; + } else if (reloc->sym->retpoline_thunk) { + if (add_retpoline_call(file, insn)) + return -1; =20 - } else { - if (add_call_dest(file, insn, reloc->sym, false)) - return -1; - } + } else { + if (add_call_dest(file, insn, reloc->sym, false)) + return -1; } =20 return 0; } =20 +static int add_call_destinations_range(struct objtool_file *file, + struct insn_range *range) +{ + struct instruction *insn; + + range_for_each_insn(file, range, insn) { + if (add_call_destination(file, insn)) + return -1; + } + + return 0; +} + +static int add_call_destinations(struct objtool_file *file) +{ + return run_insn_ranges(file, add_call_destinations_range); +} + /* * The .alternatives section requires some extra special care over and abo= ve * other special sections because alternatives are patched in place. @@ -2689,6 +3080,8 @@ int decode_file(struct objtool_file *file) if (read_annotate(file, __annotate_late)) return -1; =20 + free_decode_ranges(); + return 0; } =20 diff --git a/tools/objtool/include/objtool/objtool.h b/tools/objtool/includ= e/objtool/objtool.h index 6dc12a59ad00..79fe82b7397e 100644 --- a/tools/objtool/include/objtool/objtool.h +++ b/tools/objtool/include/objtool/objtool.h @@ -21,7 +21,8 @@ struct pv_state { =20 struct objtool_file { struct elf *elf; - DECLARE_HASHTABLE(insn_hash, 20); + struct hlist_head *insn_hash; + int insn_hash_bits; struct list_head retpoline_call_list; struct list_head return_thunk_list; struct list_head static_call_list; diff --git a/tools/objtool/objtool.c b/tools/objtool/objtool.c index a4e139dee7e9..71e048f8582a 100644 --- a/tools/objtool/objtool.c +++ b/tools/objtool/objtool.c @@ -29,7 +29,6 @@ struct objtool_file *objtool_open_read(const char *filena= me) if (!file.elf) return NULL; =20 - hash_init(file.insn_hash); INIT_LIST_HEAD(&file.retpoline_call_list); INIT_LIST_HEAD(&file.return_thunk_list); INIT_LIST_HEAD(&file.static_call_list); --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D63A83D47C0; Tue, 8 Sep 2026 20:59:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901159; cv=none; b=M/H3BK7XB0Y+D5ZCVSdmmJGaKlcSbdOXTPHWPw0LjMc5T7bHVFzRJV/8hvev/lUwLKXszEesLx5MLmVZ+90T+48bmFBwmwRmxOC1I7xs9w2pLKmdsTMMCmPYXvPo/YPnT5U5HASWcZuf4NzfLtWvYaxNs58/qKsPnjX27erIya0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901159; c=relaxed/simple; bh=LB9T7y9hXziw5kYzoWlATIyn+dzyDLeA+KV1Dtb+tR0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bZj/C47roIp9DkTGBUzLDYcfCddNpVCA6I5N9O12GXopXwp3uvP0B9eRSDD5zwY5fO0gjV6EZSltQbs2s+wH3ny5W8jAZTaNU7B/28Dm9YMim3+O5w7zbL/a4pXwIJqFWcChik+nyHkaY070g758Jr/WhwUtdCmcIM0g2HrlPRY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=l2H8oEfy; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="l2H8oEfy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C1D881F00A3D; Tue, 8 Sep 2026 20:59:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788901157; bh=KP4DPjtGqjYlZDY/C6oiw5PNNFH4xgYFq5gb3vP/9DM=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=l2H8oEfyiisTrkmWPXFPkrb9zX+PnNqQLPTWl+vHf3NsuP+fCsXMWBsX7BjXJYlWZ nqqgTPf7uY55PDIAWtwo6hXNTbMnaLlGNx4ryMRpe/nOzVqpEzc5iWLw2JLhb8Co05 9s1SasDqfvP91H8sdwcoy4A7YTh8DB0wv+Bi/OoXlL2MblT9cgvSRSaEexFHujfFy6 8P4N8pmqaNnNKGCxCVliu0/Kec45kp7oMDRUSzS6XwAjYJU0gH5oVqdZOxDseQyQO1 Jh1owZbgK0zD6cmR+j9o1TGHMmkFGwpKic4yhyualkluEnQUYWO/YKOT5YBaGeN1wf Oue7w0xvvFlNw== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:20 +0100 Subject: [PATCH 20/23] kbuild: rust: parallelise rustc front end Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-20-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=3115; i=ljs@kernel.org; h=from:subject:message-id; bh=LB9T7y9hXziw5kYzoWlATIyn+dzyDLeA+KV1Dtb+tR0=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlFnEMd3Ve9ZQVLbpj4/Z9s0fLu8KN58+tafn1S/fR l1FmWdTO0pZGMS4GGTFFFmefxHfHyQSNq/zgr8bzBxWJpAhDFycAjCR0CuMDFOjrd+XSqqK1Lpq xpUur5gszfvmzYl5k5X2XJzc+WaJ1SNGhteOwtIX8jW6gl0FqjNPhzDoas99cHsCh5ZbR+ar/1f 9uAA= X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 Rust crates are compiled in a serial chain on the critical path - core, bindings, kernel crates and then the drivers - each a rustc invocation. With CONFIG_RUST set and every rust driver enabled, the entire chain takes around 25 seconds on a 128-thread machine, with the associated C code taking 24 seconds. Each time a change is made to any of the bindings, it triggers a rebuild. Use the -Zthreads option to have rustc perform builds in parallel. This has been available since rust 1.84 (rust-lang/rust#132282), and the kernel requires rust 1.85 or above. A future -j/--jobs option is planned for rustc:(rust-lang/compiler-team#1005), so check to see if this available and if so use it. If the user's rustc supports neither, then it falls back gracefully and neither are used. The threads are taken from make's jobserver, so a parallel build is not oversubscribed. It was found that benefits level off at 8 threads (16 was found to be around the same, and 32 slower). Generated output was confirmed byte-for-byte identical. Observed build time changes (using rustc 1.98, clang): before after core.o 7.7s 4.9s bindings.o 4.3s 3.1s kernel.o 2.0s 1.4s clean build 39.3s 34.6s touch rust/kernel/lib.rs 12.9s 12.1s touch rust/bindings/bindings_helper.h 19.5s 17.5s Whole build, 128-thread Threadripper 9980X, best of N runs: before after delta ------------------------------- x86 defconfig+RUST, clean 41.0s 36.4s -4.6s (-11%) x86 defconfig+RUST, touch lib.rs 8.3s 7.7s -0.56s (-7%) Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- Makefile | 4 ++++ init/Kconfig | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/Makefile b/Makefile index 997fc7ebe096..7d23d7b64be3 100644 --- a/Makefile +++ b/Makefile @@ -1204,6 +1204,10 @@ KBUILD_RUSTFLAGS +=3D --remap-path-prefix=3D$(srcroo= t)/=3D --remap-path-scope=3Dmacro endif endif =20 +ifdef CONFIG_RUST +KBUILD_RUSTFLAGS +=3D $(if $(CONFIG_RUSTC_HAS_JOBS),-j8,$(if $(CONFIG_RUST= C_HAS_ZTHREADS),-Zthreads=3D8)) +endif + # include additional Makefiles when needed include-y :=3D scripts/Makefile.warn include-$(CONFIG_DEBUG_INFO) +=3D scripts/Makefile.debug diff --git a/init/Kconfig b/init/Kconfig index 3c92c87254a3..4096a306e2ab 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -231,6 +231,13 @@ config RUSTC_HAS_FILE_AS_C_STR config RUSTC_HAS_SUSPICIOUS_RUNTIME_SYMBOL_DEFINITIONS def_bool RUSTC_VERSION >=3D 109800 =20 +# rustc's parallel front end: -j is the coming spelling, -Zthreads the cur= rent one +config RUSTC_HAS_JOBS + def_bool $(rustc-option,-j8) + +config RUSTC_HAS_ZTHREADS + def_bool $(rustc-option,-Zthreads=3D8) + config PAHOLE_VERSION int default "$(PAHOLE_VERSION)" --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DF1733D3D05; Tue, 8 Sep 2026 20:59:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901171; cv=none; b=M/j49EEdG8viO/5aAa9S4CwU0wfXvxN0TpYzJ+0uRA7g+WJhGezn6zKT17EVInva+QBX4FHfj+l7KhkbzewGrhGPeN0srmURPyidKzPX2f7gpdYlk0QvxqtlDMsG0pfPIyYNW8KNZcVqePe3ISEvxb30IH0CmOyHUB62220c/OI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901171; c=relaxed/simple; bh=kx145CbsbOZNlrJo3YNnQ6X8zzclemP0tqvdTYAGrNw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IlOB2Yh5P2A7mTgi46aiydOfYNgyLzB9R6P5xs0kA0jTL/nAxeE7zjaiKu4UX4EjUJs0klh/M2MN5Xz+cPIB6HpkbGLOk9TBvD7HpU3C16bIrVSif2xKYFovppEHfX2Up+FA4PjaajzUkZ5aNVT0XnqanUhX+Zrl3S0tnCzLgOo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OIpDOBSg; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="OIpDOBSg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 046F01F00A3A; Tue, 8 Sep 2026 20:59:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788901168; bh=RpyglNkLvgtO+g1zDJNpkY3JopuChXGi3ttcKAuST+8=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=OIpDOBSgJrH76v4qVZGsW50WfuCBkHgvg4YvO+ZN3uJi1EdpWYnqx9/rxijC1G8Y2 VmSITaucbZMwSNTrXt+SWMY3umH2RYhUY9lg8jXa+zDPf6/z26mQyBH6CgVrjNi3NQ B2IlUKR/DopfAfvqCauFgd0ycxSvLywvK9BKj3UvpoAkW+0BzLMFCXb/P5rNbzhqK/ JomapY4q5gpgznQ6qVwqt0ZH9qq4WWdA8YAfsA0whir9iAxDmqsIfvPFDUxhSBP6v+ MDdFlVH88zzAe38RNflu9HCao15bxZR9oE8mh13QjCKeM/zrvUOJfBkBoNAveTh4ui c227LJShut05w== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:21 +0100 Subject: [PATCH 21/23] rust: make exports.o depend on the headers generated for it Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-21-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=1286; i=ljs@kernel.org; h=from:subject:message-id; bh=kx145CbsbOZNlrJo3YNnQ6X8zzclemP0tqvdTYAGrNw=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlFmyftt0/rHJ4/XeJ5NNZMp8m66+kNfu9lsnecLy8 QEXFo2SjlIWBjEuBlkxRZbnX8T3B4mEzeu84O8GM4eVCWQIAxenAEwkPJGRoT+U9y0318zvJ0XM dT6t5PNlvlHHN0H8/Wup7seP/9x/r8bI8Hra9b3v2RsWHzq2frJy1QIuIS+b/yHHakRzt3woe77 sJicA X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 rust/exports.c includes the exports_*_generated.h listing the symbols of the core, bindings, kernel and helpers objects, but this is not expressed in its Makefile. This worked previously because the headers are always-y targets, and make prepare built all of the rust crates first, which is an implicit dependency. Fix this as it is a dependency of the subsequent commit. Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- rust/Makefile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rust/Makefile b/rust/Makefile index da1a7409d984..74d21ae6f383 100644 --- a/rust/Makefile +++ b/rust/Makefile @@ -728,6 +728,11 @@ endef $(obj)/helpers/helpers.o: $(src)/helpers/helpers.c $(recordmcount_source) = FORCE +$(call if_changed_rule,rust_cc_library) =20 +# The exported symbol lists are generated from the crates' objects. +$(obj)/exports.o: $(obj)/exports_core_generated.h \ + $(obj)/exports_bindings_generated.h $(obj)/exports_kernel_generated.h \ + $(if $(CONFIG_RUST_INLINE_HELPERS),,$(obj)/exports_helpers_generated.h) + # Disable symbol versioning for exports.o to avoid conflicts with the actu= al # symbol versions generated from Rust objects. $(obj)/exports.o: private skip_gendwarfksyms =3D 1 --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 296F23D811C; Tue, 8 Sep 2026 20:59:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901181; cv=none; b=ZW4d5gyQ/TCMoIGSL/TFQenYl+oeH21s9mjGKhKSRGvR8l11YFedrZF4WR95/p9SDE5MY3jgz5e2uijQ/xoZKBDeQBvWE5wu7/Qv5giwAPBPE0nxn+S+sHpxv5l79JsntG7s6Bw++iYkCzvKgASdrD6dHs9aXVrVaD8vsE7QKOk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901181; c=relaxed/simple; bh=XCvCrcK+AhcCVOOFTIEWP4r+MqSjPsV0qg7Bzqs7ge4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dqBBt7DlXpdBDMKkD4SalWUaqxlAz0iIeeUUmL5k9mW64hMfYE/kCwh+tetcpEtw33KODM7zSAXMcW7qggwizUM2E5kB1DPfoFea73I1r+My/8dTsuI+ZRnpdYex5ayiUMDimDdS6RpVjbI7H2RsUqqS9ivzTT4Qxtl6ObYjjJQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=cee9VYiY; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="cee9VYiY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 53A351F00A3D; Tue, 8 Sep 2026 20:59:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788901180; bh=RQEjHODHdWtfRLaYljXTCOiq1fIwDiQ3ymNC15jt1/U=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=cee9VYiY+WdzxYhQHHcFgW9cEwCd2qP/qR+XgTI3WUEJ0rbaGMAwBQrVwh6V/pNQN CpIE2yLM+2TfFktdZCqWhVBBUKNzujLyuCVWJCWVfEBlbUqbm8n9lWw6oPCj5dFDZU M41dPGXGQ0mGZSLm33uRXCzUEFoO6y/dwB7tkN5ybxYgrObbeLwY5+JKUz/GxN4vgW UcOlkNW7puN3P38kETgBuAC0BPsD68GMf6ZjU+mY2WDR9zV1NH1UEGbdkKlP9cdoO6 Sl9KTeFOpgl77TNi5rzqQBkJpHbm3c6JghAn0h+pyaui8GDgz6V7T0MnUM0AWIHiwb zc5K3c0DyX7bw== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:22 +0100 Subject: [PATCH 22/23] kbuild: build rust crates in parallel with the rest of the build Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-22-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=3754; i=ljs@kernel.org; h=from:subject:message-id; bh=XCvCrcK+AhcCVOOFTIEWP4r+MqSjPsV0qg7Bzqs7ge4=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlFlmvzlQkJt/tI7n6rKpT/ZyLvslHZtgUDXlzhmLG QbK1/LyOkpZGMS4GGTFFFmefxHfHyQSNq/zgr8bzBxWJpAhDFycAjARPj1Ghv+RslWHyrfcvtHy t8lna87BP5svKogr5rrNNdNolvaaZsrwPyHOTVVg1cNbW6XLa+b/z3I8sNfvVs+U3T+agq1m+l7 LYgQA X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 When CONFIG_RUST is specified make prepare builds all of the rust components before descending into the tree. This means nothing can be done until all of these are compiled, resulting in a stall at the start of every clean CONFIG_RUST build. This also implicitly slows down every rust-capable LLVM allmodconfig build as this enables the CONFIG_RUST option. Fix this by allowing rust crates to be built alongside the C code. Remove the rust build from the make prepare step, then establish a dependency between rust code located elsewhere in the tree upon rust components contained in rust/. Do this by establishing a top-level list of directories containing rust code, KBUILD_RUST_DIRS, upon which the dependency is expressed. Finally, ensure that no rust code is located elsewhere and fail the build if any is found to ensure that nothing is missed in future. Care is taken to ensure the rust/ dependency is also established for any module being built to account for external rust modules which rely upon it. Every Rust and C object was confirmed to be byte-for-byte identical after this change. Whole build, 128-thread Threadripper 9980X, best of N runs: before after delta ------------------------------- x86 defconfig+RUST, clean 36.5s 32.6s -3.9s (-11%) x86 allmodconfig, clean 297.0s 278.3s -18.7s (-6%) Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- Kbuild | 5 +++++ Makefile | 11 ++++++++++- scripts/Makefile.build | 5 ++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Kbuild b/Kbuild index a6a0192dea08..9670556059dd 100644 --- a/Kbuild +++ b/Kbuild @@ -115,3 +115,8 @@ obj-$(CONFIG_NET) +=3D net/ obj-y +=3D virt/ obj-y +=3D $(ARCH_DRIVERS) obj-$(CONFIG_DRM_HEADER_TEST) +=3D include/ + +# Rust code elsewhere in the tree depends upon rust/. +ifdef CONFIG_RUST +$(KBUILD_RUST_DIRS): | rust +endif diff --git a/Makefile b/Makefile index 7d23d7b64be3..3805f72428cb 100644 --- a/Makefile +++ b/Makefile @@ -1429,11 +1429,14 @@ prepare0: archprepare $(Q)$(MAKE) $(build)=3D. prepare $(Q)$(MAKE) $(build)=3Dscripts/mod =20 +ifdef CONFIG_RUST +export KBUILD_RUST_DIRS :=3D drivers lib mm samples +endif + # All the preparing.. prepare: prepare0 ifdef CONFIG_RUST +$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh - $(Q)$(MAKE) $(build)=3Drust endif =20 PHONY +=3D remove-stale-files @@ -1760,6 +1763,12 @@ modules: modules_prepare # Target to prepare building external modules modules_prepare: prepare $(Q)$(MAKE) $(build)=3Dscripts scripts/module.lds +ifdef CONFIG_RUST +# Ensure rust/ is build before any external rust module which will rely up= on it. +ifeq ($(MAKECMDGOALS),modules_prepare) + $(Q)$(MAKE) $(build)=3Drust +endif +endif =20 endif # CONFIG_MODULES =20 diff --git a/scripts/Makefile.build b/scripts/Makefile.build index b9093b39cc2f..77351d6006fa 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -360,8 +360,11 @@ quiet_cmd_rustc_o_rs =3D $(RUSTC_OR_CLIPPY_QUIET) $(qu= iet_modtag) $@ $(cmd_ld_single)) \ $(cmd_objtool) =20 +rust-dir-ok =3D $(or $(KBUILD_EXTMOD),$(filter rust $(KBUILD_RUST_DIRS),$(= firstword $(subst /, ,$@)))) + +# Ensure that any rust code located elsewhere from rust/ is listed in KBUI= LD_RUST_DIRS. define rule_rustc_o_rs - $(call cmd_and_fixdep,rustc_o_rs) + $(if $(rust-dir-ok),,$(error $@: Rust code in a directory not listed in K= BUILD_RUST_DIRS, see the top Makefile))$(call cmd_and_fixdep,rustc_o_rs) $(call cmd,gen_objtooldep) endef =20 --=20 2.55.0 From nobody Tue Sep 15 23:49:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5BE503C4B81; Tue, 8 Sep 2026 20:59:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901192; cv=none; b=ObBupEnVVERFZVfxYjlOlR7Bz0Guhsn9Mw4rwIJVAnOzupRlY66/+R69XmMlIKoDNNWO9BNex+1ZGlv7hEPQWNmpZMwdWPgC2NIWlvqTNQBPIfdC8i8yF2i6fyenmyCByzhGTh1tN8Ix/vU95FcKZXenVCJ9FjCgULbpvEJVvyg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788901192; c=relaxed/simple; bh=/cGfZoMmr7KpIZ+1dXwGv9v2Wc3LWJWwGs5e7Tz+VNE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=d5vsW7b5PpgZyy/q6vQOQmWPRPId1EeCNnpmq+5rJ9+OG+9cE7IDMjx2omfaDATU3N8s+w9hsPyZqaszU8Wp/o4BsunjqU5q9O1QF0CpG2oWOgrpg+2/aSuGJ7GkWLthGNzGJiMszm6zf5stDa2ni+Y0I7GdiU+6AjqZG/8z7Qg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MdO7GGCn; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MdO7GGCn" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8613E1F00A3A; Tue, 8 Sep 2026 20:59:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788901191; bh=8qApsahOXbBIqOceZevDiK3r7godzpGwnSgF9lGt9lA=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=MdO7GGCn0+KpMxsISFASdD9dBeQiWmdJV6zH6t4gzZ5v/IkbEVIfcTsiIALXNtNLj dFKVRsqLpM4VxR9/cgaCaaIM8If6JAXGEv7nzSsfCRev6+enJY4RHrDFyU7NHQ5Qvp xOoik+kf2au/vKN9QZt/hbZQRAsBSK1fRwamdkGXQIrTgIHSfo5GeIVS91y7sTeNAs RMryxLxJ9s2KHaRhVjS7PJ7AQyWgKEtnuhencnKFV7WGH+LmQdpFNF0YkmKpDJWnO7 38B+TFNamLP1eHAQtxCYJp3TkHMPKvdowdf0jA/KR6oeDesYCBg+6wnzjzvIKo4VyJ t9x2EB5VjgNtA== From: "Lorenzo Stoakes (ARM)" Date: Tue, 08 Sep 2026 21:55:23 +0100 Subject: [PATCH 23/23] kbuild: use pigz for gzip compression if available Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260908-build-speedup-v1-23-5dc1ac01672d@kernel.org> References: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org> To: Linus Torvalds , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Masahiro Yamada , Alexey Gladkov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Ilias Apalodimas , Josh Poimboeuf , Peter Zijlstra , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Jonathan Corbet , Randy Dunlap Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, Jens Axboe , "Lorenzo Stoakes (ARM)" X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=4672; i=ljs@kernel.org; h=from:subject:message-id; bh=/cGfZoMmr7KpIZ+1dXwGv9v2Wc3LWJWwGs5e7Tz+VNE=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIWlFnOe8Tg5bZ6llWidBaDf//HKzIea68pJ4U1fnOvK UmZvHdqRykLgxgXg6yYIsvzL+L7g0TC5nVe8HeDmcPKBDKEgYtTACZykJuR4eic7LZFB/flfLzG vKvj/Y0bez9vyL3NYrPz78pFPrkRJjoM/x2cp17LlWRaKbL+v5h5m22pf93yr542HzPtHxn9vL9 kHisA X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 The gzip step of a kernel build is very lengthily, especially for larger builds such as allmodconfig. gzip itself cannot be run in parallel, however an alternative tool exists that can, providing the same feature set as gzip itself - pigz - which works as a drop-in replacement. On a 128-core Threadripper, gzip -9 of a 36 MiB x86-64 vmlinux.bin takes 1.6s, and with pigz it takes 0.09s, so the performance increase is significant. It is already possible to specify the KGZIP environmental variable to make use of pigz, however it seems sensible to make use of pigz if it is available. Therefore default to using pigz if it is available on the system upon which the kernel is being built, otherwise fall back to gzip. The output is byte-for-byte identical between pigz/gzip invocations, but gzip and pigz do not produce the same stream as one another. Therefore, for reproducible builds, the same set of tools should be used. This seems to already be an implicit requirement in any case, but update the reproducible build documentation to make this clear. Also update the kbuild documentation to reflect the change. Every x86 build ends with the compression of vmlinux.bin, 36MB for defconfig and over 200MB for allmodconfig, no-op builds are unchanged. Whole build, 128-thread Threadripper 9980X, best of N runs: before after delta ------------------------------- x86 defconfig, touch mm/vma.c, gcc 7.4s 5.4s -2.0s (-27%) x86 defconfig, touch mm/vma.c, clang 6.6s 4.9s -1.7s (-26%) x86 defconfig, clean, gcc 26.2s 24.4s -1.8s (-7%) x86 defconfig, clean, clang 25.8s 24.3s -1.5s (-6%) x86 allmodconfig, touch mm/vma.c, gcc 23.7s 15.3s -8.4s (-35%) x86 allmodconfig, touch mm/vma.c, clang 22.1s 15.2s -6.9s (-31%) x86 allmodconfig, clean, gcc 291.4s 275.2s -16.2s (-6%) x86 allmodconfig, clean, clang 278.3s 265.7s -12.6s (-5%) Link: https://zlib.net/pigz/ Assisted-by: LLM Signed-off-by: Lorenzo Stoakes (ARM) --- Documentation/kbuild/kbuild.rst | 13 +++++++++++++ Documentation/kbuild/reproducible-builds.rst | 16 ++++++++++++++++ Makefile | 2 +- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Documentation/kbuild/kbuild.rst b/Documentation/kbuild/kbuild.= rst index 5a9013bacfb7..ebfc2319d6da 100644 --- a/Documentation/kbuild/kbuild.rst +++ b/Documentation/kbuild/kbuild.rst @@ -110,6 +110,19 @@ HOSTLDLIBS ---------- Additional libraries to link against when building host programs. =20 +KGZIP +----- +The gzip compressor used for the compressed kernel image, compressed +modules and packaging. + +If unset, it defaults to pigz (a parallel implementation of gzip) if avail= able, +otherwise gzip. + +KBZIP2, KLZOP, LZMA, LZ4, XZ, ZSTD +---------------------------------- +The compressor programs for the other formats. Each defaults to the program +of the same name. + .. _userkbuildflags: =20 USERCFLAGS diff --git a/Documentation/kbuild/reproducible-builds.rst b/Documentation/k= build/reproducible-builds.rst index bc1eb82211df..b95586df2cb5 100644 --- a/Documentation/kbuild/reproducible-builds.rst +++ b/Documentation/kbuild/reproducible-builds.rst @@ -76,6 +76,22 @@ include generated files. You should ensure the source t= ree is pristine by running ``make mrproper`` or ``git clean -d -f -x`` before building a source package. =20 +Compression tools +----------------- + +The compressed kernel image, compressed modules and packages are produced +using the binary specified by the environment variable ``KGZIP`` (described +in Documentation/kbuild/kbuild.rst). + +This variable defaults to ``pigz`` if installed (a parallel implementation +of gzip), or ``gzip`` otherwise. + +The generated output between two invocations of identical builds with +either of the default tools will be byte-for-byte equivalent. + +However, for reproducible builds, ensure the same tool is used on all build +hosts, as different tools may generate different output from one another. + Module signing -------------- =20 diff --git a/Makefile b/Makefile index 3805f72428cb..5cb819034372 100644 --- a/Makefile +++ b/Makefile @@ -561,7 +561,7 @@ PERL =3D perl PYTHON3 =3D python3 CHECK =3D sparse BASH =3D bash -KGZIP =3D gzip +KGZIP :=3D $(if $(shell command -v pigz 2>/dev/null),pigz,gzip) KBZIP2 =3D bzip2 KLZOP =3D lzop LZMA =3D lzma --=20 2.55.0