futex: Use runtime constants for futex_hash computation
v6 cover futex: Use runtime constants for futex_hash computation
tl;dr
This series introduces runtime_const_mask_32() and uses runtime constants for __ro_after_init data in futex_hash() hot path. More information can be found on v2 at
https://lore.kernel.org/lkml/[email protected]/
Notable changes in v5
=====================
Intel test robot flagged a case of compilers re-ordering the runtime_const usage before their initialization in futex_init() at
https://lore.kernel.org/lkml/[email protected]/.
This was fixed by adding a barrier() after the runtime_const_init() bits in futex_init() to prevent this re-ordering.
Sashiko flagged that __fls(val), when val is 0, can be optimized by the compiler to consider val as non-zero and silently optimize the BUG_ON(!val && ...) to only consider the latter bits, skipping the !val check. Fix this by explicitly checking val before __fls().
Testing
=======
Apart from x86, which was build and boot tested on baremetal, all the other architectures have been build and boot tested with cross-compile + QEMU with some light sanity testing on each.
Patches are based on:
git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git master
at commit 8d0b0ef04514 ("Merge branch into tip/master: 'x86/tdx'") (27-07-2026)
Diffstat from v5 is essentially 5+++++-- with effectively 3 lines of code change and 2 newlines; Resending for convenience since adding parital diffs to v5 was getting too finnicky and this is easier for pickup.
Charlie's optimization for RISC-V bits to patch slli + srli with andi + nop if mask fits the immediate encoding is left to a later series once this is merged.
(https://lore.kernel.org/lkml/178366995930.1208691.2993932866462893112.b4-review@b4/)
Few comments from checkpatch.pl have been ignored to adhere to the style of the particular file. If something needs addressing, please let me know and I'll address it with a v6.X fixups unless there is a larger change that will require a re-spin
Everyone has been Cc'd on the cover-letter and the futex bits for the context. Respective arch maintainers, reviewers, and whoever got lucky with get_maintainer.pl have been Cc'd on their respective arch specific changes. Futex maintainers and the lists will be receiving the whole series (sorry in advance!)
---
changelog v5..v6:
o Added a barrier after runtime_const_init() to prevent compilers from re-ordering their access before the initialization in futex_init(). (Sashiko, Intel test robot)
o Added a zero check before __fls() to prevent smart compiler optimizations of a later BUG_ON(). (Sashiko)
o Picked up Peter's S-o-b from his tree.
o Collected tags from Charlie. (Thanks a ton!)
o Rebased on latest tip.
v5: https://lore.kernel.org/lkml/[email protected]/
---
K Prateek Nayak (5):
arm64/runtime-const: Use aarch64_insn_patch_text_nosync() for patching arm64/runtime-const: Introduce runtime_const_mask_32() riscv/runtime-const: Replace open-coded placeholder with RUNTIME_MAGIC riscv/runtime-const: Introduce runtime_const_mask_32() s390/runtime-const: Introduce runtime_const_mask_32()
Peter Zijlstra (3):
x86/runtime-const: Introduce runtime_const_mask_32() asm-generic/runtime-const: Add dummy runtime_const_mask_32()
futex: Use runtime constants for __futex_hash() hot path
arch/arm64/include/asm/runtime-const.h | 63 ++++++++++++++++---- arch/riscv/include/asm/asm.h | 1 + arch/riscv/include/asm/runtime-const.h | 82 ++++++++++++++++++++------ arch/s390/include/asm/runtime-const.h | 22 ++++++- arch/x86/include/asm/runtime-const.h | 14 +++++ include/asm-generic/runtime-const.h | 1 + include/asm-generic/vmlinux.lds.h | 5 +- kernel/futex/core.c | 44 ++++++++------ 8 files changed, 181 insertions(+), 51 deletions(-)
base-commit: 8d0b0ef045143e7ebf2b4c3ad5347bc1e0e45a80
Signature · 1 line
2.34.1
PATCH v6 1/8 x86/runtime-const: Introduce runtime_const_mask_32()
From: Peter Zijlstra <[email protected]>
Futex hash computation requires a mask operation with read-only after init data that will be converted to a runtime constant in the subsequent commit.
Introduce runtime_const_mask_32 to further optimize the mask operation in the futex hash computation hot path.
[ prateek: Broke off the x86 chunk, commit message. ]
---
changelog v5..v6:
o Picked up Peter's S-o-b from his tree.
o No changes to the diff since v4.
--- arch/x86/include/asm/runtime-const.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
arch/x86/include/asm/runtime-const.h +14 -0
@@ -41,6 +41,15 @@ :"+r" (__ret)); \ __ret; }) +#define runtime_const_mask_32 (val, sym) ({ \ + typeof(0u+(val)) __ret = (val); \ + asm_inline("and $0x12345678, %k0\n1:\n" \ + ".pushsection runtime_mask_" #sym ",\"a\"\n\t"\ + ".long 1b - 4 - .\n" \ + ".popsection" \ + : "+r" (__ret)); \ + __ret; }) +#define runtime_const_init (type, sym) do { \ extern s32 __start_runtime_##type##_##sym[]; \ extern s32 __stop_runtime_##type##_##sym[]; \
@@ -65,6 +74,11 @@ static inline void __runtime_fixup_shift(void *where, unsigned long val) *(unsigned char *)where = val; } +static inline void __runtime_fixup_mask (void *where,unsigned long val) +{ + *(unsigned int *)where = val; +} +static inline void runtime_const_fixup (void (*fn)(void *,unsigned long ),unsigned long val,s32 *start,s32 *end) {
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v6 1/8] x86/runtime-const: Introduce runtime_const_mask_32()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The commit introduces `runtime_const_mask_32` for x86 but omits a generic fallback in…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The commit introduces runtime_const_mask_32 for x86 but omits a generic fallback in include/asm-generic/runtime-const.h. --
--- Patch [1]: [PATCH v6 1/8] x86/runtime-const: Introduce runtime_const_mask_32() --- commit 664c491c817106934caf3b46117ff57570227576
Author: Peter Zijlstra <[email protected]>
x86/runtime-const: Introduce runtime_const_mask_32()
This commit introduces the runtime_const_mask_32() macro for the x86 architecture to optimize the mask operation in the futex hash computation hot path. It also adds the __runtime_fixup_mask() helper to support this functionality.
Quoted 4 lines
arch/x86/include/asm/runtime-const.h +0
-0
[ ... ]
arch/x86/include/asm/runtime-const.h line 52
Quoted 13 lines
arch/x86/include/asm/runtime-const.h +9
-0@@ -41,6 +41,15 @@ :"+r" (__ret)); \ __ret; }) +#define runtime_const_mask_32 (val, sym) ({ \ + typeof(0u+(val)) __ret = (val); \ + asm_inline("and $0x12345678, %k0\n1:\n" \ + ".pushsection runtime_mask_" #sym ",\"a\"\n\t"\ + ".long 1b - 4 - .\n" \ + ".popsection" \ + : "+r" (__ret)); \ + __ret; }) +
[Severity: Medium]
Will this cause a build regression on non-x86 architectures when the generic code starts using runtime_const_mask_32()?
The commit message mentions this will be used by the futex hash computation in a subsequent commit. However, there doesn't appear to be a generic fallback for this macro in include/asm-generic/runtime-const.h.
Without a generic fallback, it seems architectures that don't support or haven't yet implemented runtime constants would fail to compile once the generic futex code uses this macro.
Should a fallback like the following be introduced in include/asm-generic/runtime-const.h alongside this x86 implementation to maintain cross-architecture compatibility and strict bisectability?
#define runtime_const_mask_32(val, sym) ((val) & (sym))
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v6 1/8] x86/runtime-const: Introduce runtime_const_mask_32()
Hello,
Quoted 9 lines · On 7/28/2026 11:07 AM, [email protected] wrote:
Quoted 4 lines
arch/x86/include/asm/runtime-const.h +0
-0[ ... ]
Quoted 4 lines
arch/x86/include/asm/runtime-const.h +0
-0@@ -41,6 +41,15 @@ :"+r" (__ret)); \ __ret; })
Quoted 13 lines
arch/x86/include/asm/runtime-const.h line 52
Quoted 9 lines
arch/x86/include/asm/runtime-const.h +9
-0+#define runtime_const_mask_32 (val, sym) ({ \ + typeof(0u+(val)) __ret = (val); \ + asm_inline("and $0x12345678, %k0\n1:\n" \ + ".pushsection runtime_mask_" #sym ",\"a\"\n\t"\ + ".long 1b - 4 - .\n" \ + ".popsection" \ + : "+r" (__ret)); \ + __ret; }) +[Severity: Medium]
Will this cause a build regression on non-x86 architectures when the generic code starts usingruntime_const_mask_32()?
That only happens at Patch 8 and by Patch 7, we have the generic fallback. Everything is aligned before the first user is introduced.
Quoted 9 lines
The commit message mentions this will be used by the futex hash computation in a subsequent commit. However, there doesn't appear to be a generic fallback for this macro in
include/asm-generic/runtime-const.h.Without a generic fallback, it seems architectures that don't support or haven't yet implemented runtime constants would fail to compile once the generic futex code uses this macro.
Quoted 5 lines
Should a fallback like the following be introduced in
include/asm-generic/runtime-const.halongside this x86 implementation to maintain cross-architecture compatibility and strict bisectability?#define
runtime_const_mask_32(val, sym)((val) & (sym))
Done in Patch 7. Bisectibility is preserved by having the plumbing in place (in some order) before first user is introduced in Patch 8.
Signature · 2 lines
Thanks and Regards, Prateek
[tip: locking/core] x86/runtime-const: Introduce runtime_const_mask_32()
In reply to K Prateek NayakThe following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 775e0f2284554e24855c4372faeb957a5e515337 Gitweb: https://git.kernel.org/tip/775e0f2284554e24855c4372faeb957a5e515337 Author: Peter Zijlstra <[email protected]> AuthorDate: Tue, 28 Jul 2026 05:25:33 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 28 Jul 2026 13:07:14 +02:00
x86/runtime-const: Introduce runtime_const_mask_32()
Futex hash computation requires a mask operation with read-only after init data that will be converted to a runtime constant in the subsequent commit.
Introduce runtime_const_mask_32 to further optimize the mask operation in the futex hash computation hot path.
[ prateek: Broke off the x86 chunk, commit message. ]
--- arch/x86/include/asm/runtime-const.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
arch/x86/include/asm/runtime-const.h +14 -0
@@ -41,6 +41,15 @@ :"+r" (__ret)); \ __ret; }) +#define runtime_const_mask_32 (val, sym) ({ \ + typeof(0u+(val)) __ret = (val); \ + asm_inline("and $0x12345678, %k0\n1:\n" \ + ".pushsection runtime_mask_" #sym ",\"a\"\n\t"\ + ".long 1b - 4 - .\n" \ + ".popsection" \ + : "+r" (__ret)); \ + __ret; }) +#define runtime_const_init (type, sym) do { \ extern s32 __start_runtime_##type##_##sym[]; \ extern s32 __stop_runtime_##type##_##sym[]; \
@@ -65,6 +74,11 @@ static inline void __runtime_fixup_shift(void *where, unsigned long val) *(unsigned char *)where = val; } +static inline void __runtime_fixup_mask (void *where,unsigned long val) +{ + *(unsigned int *)where = val; +} +static inline void runtime_const_fixup (void (*fn)(void *,unsigned long ),unsigned long val,s32 *start,s32 *end) {
PATCH v6 2/8 arm64/runtime-const: Use aarch64_insn_patch_text_nosync() for patching
In reply to K Prateek NayakThe current scheme to directly patch the kernel text for runtime constants runs into the following issue with futex adapted to using runtime constants on arm64:
Unable to handle kernel write to read-only memory at virtual address ...
The pc points to the *p assignment in the following call chain:
futex_init()
runtime_const_init(shift, __futex_shift)
__runtime_fixup_shift()
*p = cpu_to_le32(insn);which suggests that core_initcall() is too late to patch the kernel text directly unlike the "d_hash_shift" which is initialized during vfs_caches_init_early() before the protections are in place.
Use aarch64_insn_patch_text_nosync() to patch the runtime constants instead of doing it directly to allow runtime_const_init() slightly later into the boot.
Since aarch64_insn_patch_text_nosync() calls caches_clean_inval_pou() internally, __runtime_fixup_caches() ends up being redundant. runtime_const_init() are rare and the overheads of multiple calls to caches_clean_inval_pou() instead of batching them together should be negligible in practice.
The cpu_to_le32() conversion of instruction isn't necessary since it is handled later in the aarch64_insn_patch_text_nosync() call-chain:
aarch64_insn_patch_text_nosync(addr, insn)
aarch64_insn_write(addr, insn)
__aarch64_insn_write(addr, cpu_to_le32(insn))Sashiko noted that aarch64_insn_patch_text_nosync() does not expect a lm_alias() address and Catalin suggested it is safe to drop the lm_alias() for runtime patching since the kernel text is readable. The address passed to fixup function is interpreted as a __le32 and dereferenced as is to read the opcode at the patch site.
No functional changes are intended.
---
changelog v5..v6:
o Picked up tags from Charlie. (Thanks a ton!)
o No changes to the diff.
--- arch/arm64/include/asm/runtime-const.h | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-)
arch/arm64/include/asm/runtime-const.h +5 -12
@@ -7,6 +7,7 @@#endif #include <asm/cacheflush.h> +#include <asm/text-patching.h> /* Sigh. You can still run arm64 in BE mode */ #include <asm/byteorder.h>
@@ -50,34 +51,26 @@ static inline void __runtime_fixup_16(__le32 *p, unsigned int val)u32 insn =le32_to_cpu (*p); insn &=0xffe0001f ; insn |= (val &0xffff ) <<5 ; - *p =cpu_to_le32 (insn); -} - -static inline void __runtime_fixup_caches (void *where,unsigned int insns) -{ -unsigned long va = (unsigned long )where; -caches_clean_inval_pou (va, va +4 *insns); +aarch64_insn_patch_text_nosync (p, insn); }static inline void __runtime_fixup_ptr (void *where,unsigned long val) { -__le32 *p =lm_alias (where); +__le32 *p = where;__runtime_fixup_16 (p, val);__runtime_fixup_16 (p+1 , val >>16 );__runtime_fixup_16 (p+2 , val >>32 );__runtime_fixup_16 (p+3 , val >>48 ); -__runtime_fixup_caches (where,4 ); }/* Immediate value is 6 bits starting at bit #16 */ static inline void __runtime_fixup_shift (void *where,unsigned long val) { -__le32 *p =lm_alias (where); +__le32 *p = where;u32 insn =le32_to_cpu (*p); insn &=0xffc0ffff ; insn |= (val &63 ) <<16 ; - *p =cpu_to_le32 (insn); -__runtime_fixup_caches (where,1 ); +aarch64_insn_patch_text_nosync (p, insn); } static inline void runtime_const_fixup(void (*fn)(void *, unsigned long),
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v6 2/8] arm64/runtime-const: Use aarch64_insn_patch_text_nosync() for patching
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
The current scheme to directly patch the kernel text for runtime constants runs into the following issue with futex adapted to using runtime constants on arm64:
Unable to handle kernel write to read-only memory at virtual address ...The pc points to the *p assignment in the following call chain:
futex_init() runtime_const_init(shift, __futex_shift) __runtime_fixup_shift() *p = cpu_to_le32(insn);
Quoted 8 lines
which suggests that
core_initcall()is too late to patch the kernel text directly unlike the "d_hash_shift" which is initialized during [ ... ]Reviewed-by Catalin Marinas <[email protected]>Reviewed-by Charlie Jenkins <[email protected]>Tested-by Charlie Jenkins <[email protected]>Signed-off-by K Prateek Nayak <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[tip: locking/core] arm64/runtime-const: Use aarch64_insn_patch_text_nosync() for patching
In reply to K Prateek NayakThe following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: a6690350a4ae7558b39d15fcc776c4d1ab5bbf39 Gitweb: https://git.kernel.org/tip/a6690350a4ae7558b39d15fcc776c4d1ab5bbf39 Author: K Prateek Nayak <[email protected]> AuthorDate: Tue, 28 Jul 2026 05:25:34 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 28 Jul 2026 13:07:14 +02:00
arm64/runtime-const: Use aarch64_insn_patch_text_nosync() for patching
The current scheme to directly patch the kernel text for runtime constants runs into the following issue with futex adapted to using runtime constants on arm64:
Unable to handle kernel write to read-only memory at virtual address ...
The pc points to the *p assignment in the following call chain:
futex_init()
runtime_const_init(shift, __futex_shift)
__runtime_fixup_shift()
*p = cpu_to_le32(insn);which suggests that core_initcall() is too late to patch the kernel text directly unlike the "d_hash_shift" which is initialized during vfs_caches_init_early() before the protections are in place.
Use aarch64_insn_patch_text_nosync() to patch the runtime constants instead of doing it directly to allow runtime_const_init() slightly later into the boot.
Since aarch64_insn_patch_text_nosync() calls caches_clean_inval_pou() internally, __runtime_fixup_caches() ends up being redundant. runtime_const_init() are rare and the overheads of multiple calls to caches_clean_inval_pou() instead of batching them together should be negligible in practice.
The cpu_to_le32() conversion of instruction isn't necessary since it is handled later in the aarch64_insn_patch_text_nosync() call-chain:
aarch64_insn_patch_text_nosync(addr, insn)
aarch64_insn_write(addr, insn)
__aarch64_insn_write(addr, cpu_to_le32(insn))Sashiko noted that aarch64_insn_patch_text_nosync() does not expect a lm_alias() address and Catalin suggested it is safe to drop the lm_alias() for runtime patching since the kernel text is readable. The address passed to fixup function is interpreted as a __le32 and dereferenced as is to read the opcode at the patch site.
No functional changes are intended.
--- arch/arm64/include/asm/runtime-const.h | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-)
arch/arm64/include/asm/runtime-const.h +5 -12
@@ -7,6 +7,7 @@#endif #include <asm/cacheflush.h> +#include <asm/text-patching.h> /* Sigh. You can still run arm64 in BE mode */ #include <asm/byteorder.h>
@@ -50,34 +51,26 @@ static inline void __runtime_fixup_16(__le32 *p, unsigned int val)u32 insn =le32_to_cpu (*p); insn &=0xffe0001f ; insn |= (val &0xffff ) <<5 ; - *p =cpu_to_le32 (insn); -} - -static inline void __runtime_fixup_caches (void *where,unsigned int insns) -{ -unsigned long va = (unsigned long )where; -caches_clean_inval_pou (va, va +4 *insns); +aarch64_insn_patch_text_nosync (p, insn); }static inline void __runtime_fixup_ptr (void *where,unsigned long val) { -__le32 *p =lm_alias (where); +__le32 *p = where;__runtime_fixup_16 (p, val);__runtime_fixup_16 (p+1 , val >>16 );__runtime_fixup_16 (p+2 , val >>32 );__runtime_fixup_16 (p+3 , val >>48 ); -__runtime_fixup_caches (where,4 ); }/* Immediate value is 6 bits starting at bit #16 */ static inline void __runtime_fixup_shift (void *where,unsigned long val) { -__le32 *p =lm_alias (where); +__le32 *p = where;u32 insn =le32_to_cpu (*p); insn &=0xffc0ffff ; insn |= (val &63 ) <<16 ; - *p =cpu_to_le32 (insn); -__runtime_fixup_caches (where,1 ); +aarch64_insn_patch_text_nosync (p, insn); } static inline void runtime_const_fixup(void (*fn)(void *, unsigned long),
PATCH v6 3/8 arm64/runtime-const: Introduce runtime_const_mask_32()
In reply to K Prateek NayakFutex hash computation requires a mask operation with read-only after init data that will be converted to a runtime constant in the subsequent commit.
Introduce runtime_const_mask_32 to further optimize the mask operation in the futex hash computation hot path. Since all the current use-cases are of the form GENMASK(n, 0), with n > 0, a single:
ubfx w0, w0, #0, #widthm1 // w0 = w0 [widthm1:0]
instruction is used for amd64 to improve instruction dinsity and performance.
"Arm A-profile A64 Instruction Set Architecture" manual, Sec. "A64 -- Base Instructions" [1] for UBFX instruction highlights the immediate "width" is encoded as width minus 1 in imms (Bits [15:10]) which is patched by __runtime_fixup_mask() once the mask is known.
If a future use case arises that needs to tackle arbitrary mask, consider using:
movz w1, #lo16, lsl #0
movk w1, #hi16, lsl #16
to patch the 32-bit mask in the asm block and return "__ret & (val)" from runtime_const_mask_32() which allows compiler to further optimize the logical and operation. __runtime_fixup_ptr() already patches a "movz, + movk lsl #16" sequence which can be reused when the need arises.
A possible implementation for this alternate scheme can be found at [2].
---
changelog v5..v6:
o Picked up tags from Charlie. (Thanks a ton!)
o Check the val before using __fls(). Flagged by Sashiko for potential compiler optimizations that can consider val !=0 after an __fls() call and then silently pass the BUG_ON().
--- arch/arm64/include/asm/runtime-const.h | 46 ++++++++++++++++++++++++++ 1 file changed, 46 insertions(+)
arch/arm64/include/asm/runtime-const.h +46 -0
@@ -36,6 +36,17 @@ :"r" (0u+(val))); \ __ret; }) +#define runtime_const_mask_32 (val, sym) ({ \ + unsigned long __ret; \ + asm_inline("1:\t" \ + "ubfx %w0, %w1, #0, #32\n\t" \ + ".pushsection runtime_mask_" #sym ",\"a\"\n\t" \ + ".long 1b - .\n\t" \ + ".popsection" \ + :"=r" (__ret) \ + :"r" (0u+(val))); \ + __ret; }) +#define runtime_const_init (type, sym) do { \ extern s32 __start_runtime_##type##_##sym[]; \ extern s32 __stop_runtime_##type##_##sym[]; \
@@ -73,6 +84,41 @@ static inline void __runtime_fixup_shift(void *where, unsigned long val)aarch64_insn_patch_text_nosync (p, insn); } +static inline void __runtime_fixup_mask (void *where,unsigned long val) +{ +unsigned int width = (val) ?__fls (val) +1 :0 ; +__le32 *p = where; +u32 insn; + +/* +* XXX: Current implementation only supports patching masks of +* form GENMASK(n, 0) (n >= 0) using a single UBFX instruction +* to improve performance, density, and covers all the current +* use-cases. +* +* When the need arises to support any generic mask, and this +* BUG_ON() is tripped, consider using a: +* +* movz %w0, #imm16 +* movk %w0, #imm16, lsl #16 +* +* sequence to load the 32bit const mask, and perform a logical +* and outside the asm block before returning the result. Fixup +* can simply reuse the existing __runtime_fixup_16() to patch +* the individual mov instructions. +*/ +BUG_ON (!val || width >32 || (GENMASK (width -1 ,0 ) != val)); + +/* +* The width of the mask is encoded as (width - 1) in imms +* which is 6 bits starting at bit #10. +*/ + insn =le32_to_cpu (*p); + insn &=0xffff03ff ; + insn |= ((width -1 ) &0x1f ) <<10 ; +aarch64_insn_patch_text_nosync (p, insn); +} +static inline void runtime_const_fixup (void (*fn)(void *,unsigned long ),unsigned long val,s32 *start,s32 *end) {
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v6 3/8] arm64/runtime-const: Introduce runtime_const_mask_32()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The commit message incorrectly states the optimization is for 'amd64' instead of 'arm64' and…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The commit message incorrectly states the optimization is for 'amd64' instead of 'arm64' and misspells 'density'. --
--- Patch [3]: [PATCH v6 3/8] arm64/runtime-const: Introduce runtime_const_mask_32() --- commit 42eccec62a5249e8cefa9cbda5e62f01751903e8
Author: K Prateek Nayak <[email protected]>
arm64/runtime-const: Introduce runtime_const_mask_32()
This commit introduces runtime_const_mask_32() to optimize futex hash computation mask operations on arm64. It utilizes a single UBFX instruction for masks of the form GENMASK(n, 0) and implements runtime patching for the width field.
Quoted 2 lines
instruction is used for amd64 to improve instruction dinsity and performance.
[Severity: Low]
This isn't a bug, but there are a couple of typos here: "amd64" instead of "arm64", and "dinsity" instead of "density".
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[tip: locking/core] arm64/runtime-const: Introduce runtime_const_mask_32()
In reply to K Prateek NayakThe following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 7eccf137dc044c802f11d3d60bc7fc29066fdf92 Gitweb: https://git.kernel.org/tip/7eccf137dc044c802f11d3d60bc7fc29066fdf92 Author: K Prateek Nayak <[email protected]> AuthorDate: Tue, 28 Jul 2026 05:25:35 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 28 Jul 2026 13:07:14 +02:00
arm64/runtime-const: Introduce runtime_const_mask_32()
Futex hash computation requires a mask operation with read-only after init data that will be converted to a runtime constant in the subsequent commit.
Introduce runtime_const_mask_32 to further optimize the mask operation in the futex hash computation hot path. Since all the current use-cases are of the form GENMASK(n, 0), with n > 0, a single:
ubfx w0, w0, #0, #widthm1 // w0 = w0 [widthm1:0]
instruction is used for amd64 to improve instruction dinsity and performance.
"Arm A-profile A64 Instruction Set Architecture" manual, Sec. "A64 -- Base Instructions" [1] for UBFX instruction highlights the immediate "width" is encoded as width minus 1 in imms (Bits [15:10]) which is patched by __runtime_fixup_mask() once the mask is known.
If a future use case arises that needs to tackle arbitrary mask, consider using:
movz w1, #lo16, lsl #0
movk w1, #hi16, lsl #16
to patch the 32-bit mask in the asm block and return "__ret & (val)" from runtime_const_mask_32() which allows compiler to further optimize the logical and operation. __runtime_fixup_ptr() already patches a "movz, + movk lsl #16" sequence which can be reused when the need arises.
A possible implementation for this alternate scheme can be found at [2].
--- arch/arm64/include/asm/runtime-const.h | 46 +++++++++++++++++++++++++- 1 file changed, 46 insertions(+)
arch/arm64/include/asm/runtime-const.h +46 -0
@@ -36,6 +36,17 @@ :"r" (0u+(val))); \ __ret; }) +#define runtime_const_mask_32 (val, sym) ({ \ + unsigned long __ret; \ + asm_inline("1:\t" \ + "ubfx %w0, %w1, #0, #32\n\t" \ + ".pushsection runtime_mask_" #sym ",\"a\"\n\t" \ + ".long 1b - .\n\t" \ + ".popsection" \ + :"=r" (__ret) \ + :"r" (0u+(val))); \ + __ret; }) +#define runtime_const_init (type, sym) do { \ extern s32 __start_runtime_##type##_##sym[]; \ extern s32 __stop_runtime_##type##_##sym[]; \
@@ -73,6 +84,41 @@ static inline void __runtime_fixup_shift(void *where, unsigned long val)aarch64_insn_patch_text_nosync (p, insn); } +static inline void __runtime_fixup_mask (void *where,unsigned long val) +{ +unsigned int width = (val) ?__fls (val) +1 :0 ; +__le32 *p = where; +u32 insn; + +/* +* XXX: Current implementation only supports patching masks of +* form GENMASK(n, 0) (n >= 0) using a single UBFX instruction +* to improve performance, density, and covers all the current +* use-cases. +* +* When the need arises to support any generic mask, and this +* BUG_ON() is tripped, consider using a: +* +* movz %w0, #imm16 +* movk %w0, #imm16, lsl #16 +* +* sequence to load the 32bit const mask, and perform a logical +* and outside the asm block before returning the result. Fixup +* can simply reuse the existing __runtime_fixup_16() to patch +* the individual mov instructions. +*/ +BUG_ON (!val || width >32 || (GENMASK (width -1 ,0 ) != val)); + +/* +* The width of the mask is encoded as (width - 1) in imms +* which is 6 bits starting at bit #10. +*/ + insn =le32_to_cpu (*p); + insn &=0xffff03ff ; + insn |= ((width -1 ) &0x1f ) <<10 ; +aarch64_insn_patch_text_nosync (p, insn); +} +static inline void runtime_const_fixup (void (*fn)(void *,unsigned long ),unsigned long val,s32 *start,s32 *end) {
PATCH v6 4/8 riscv/runtime-const: Replace open-coded placeholder with RUNTIME_MAGIC
In reply to K Prateek NayakDefine the placeholder used for lui + addi[w] patching sequence as RUNTIME_MAGIC and use that instead of open coding the constants in the inline assembly.
No functional changes intended.
---
changelog v5..v6:
o Picked up tags from Charlie. (Thanks a ton!)
--- arch/riscv/include/asm/runtime-const.h | 38 ++++++++++++++------------ 1 file changed, 20 insertions(+), 18 deletions(-)
arch/riscv/include/asm/runtime-const.h +20 -18
@@ -15,21 +15,23 @@#include <linux/uaccess.h> +#define RUNTIME_MAGIC __ASM_STR(0x89ABCDEF) +#ifdef CONFIG_32BIT -#define runtime_const_ptr (sym) \ -({ \ - typeof(sym) __ret; \ - asm_inline(".option push\n\t" \ - ".option norvc\n\t" \ - "1:\t" \ - "lui %[__ret],0x89abd\n\t" \ - "addi %[__ret],%[__ret],-0x211\n\t" \ - ".option pop\n\t" \ - ".pushsection runtime_ptr_" #sym ",\"a\"\n\t" \ - ".long 1b - .\n\t" \ - ".popsection" \ - : [__ret] "=r" (__ret)); \ - __ret; \ +#define runtime_const_ptr (sym) \ +({ \ + typeof(sym) __ret; \ + asm_inline(".option push\n\t" \ + ".option norvc\n\t" \ + "1:\t" \ + "lui %[__ret], %%hi(" RUNTIME_MAGIC ")\n\t" \ + "addi %[__ret],%[__ret], %%lo(" RUNTIME_MAGIC ")\n\t" \ + ".option pop\n\t" \ + ".pushsection runtime_ptr_" #sym ",\"a\"\n\t" \ + ".long 1b - .\n\t" \ + ".popsection" \ + : [__ret] "=r" (__ret)); \ + __ret; \ })#else /*
@@ -46,10 +48,10 @@".option push\n\t" \".option norvc\n\t" \"1:\t" \ -"lui %[__ret],0x89abd\n\t" \ -"lui %[__tmp],0x1234\n\t" \ -"addiw %[__ret],%[__ret],-0x211\n\t" \ -"addiw %[__tmp],%[__tmp],0x567\n\t" \ +"lui %[__ret], %%hi(" RUNTIME_MAGIC ")\n\t" \ +"lui %[__tmp], %%hi(" RUNTIME_MAGIC ")\n\t" \ +"addiw %[__ret],%[__ret], %%lo(" RUNTIME_MAGIC ")\n\t" \ +"addiw %[__tmp],%[__tmp], %%lo(" RUNTIME_MAGIC ")\n\t" \ #define RISCV_RUNTIME_CONST_64_BASE \"slli %[__tmp],%[__tmp],32\n\t" \
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v6 4/8] riscv/runtime-const: Replace open-coded placeholder with RUNTIME_MAGIC
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
Define the placeholder used for lui + addi[w] patching sequence as
RUNTIME_MAGICand use that instead of open coding the constants in the inline assembly.No functional changes intended.
Suggested-by Guo Ren <[email protected]>Reviewed-by Charlie Jenkins <[email protected]>Tested-by Charlie Jenkins <[email protected]>Reviewed-by Guo Ren <[email protected]>Signed-off-by K Prateek Nayak <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[tip: locking/core] riscv/runtime-const: Replace open-coded placeholder with RUNTIME_MAGIC
In reply to K Prateek NayakThe following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: ee10b1028129a73857593801614d5bf75a15b9c7 Gitweb: https://git.kernel.org/tip/ee10b1028129a73857593801614d5bf75a15b9c7 Author: K Prateek Nayak <[email protected]> AuthorDate: Tue, 28 Jul 2026 05:25:36 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 28 Jul 2026 13:07:15 +02:00
riscv/runtime-const: Replace open-coded placeholder with RUNTIME_MAGIC
Define the placeholder used for lui + addi[w] patching sequence as RUNTIME_MAGIC and use that instead of open coding the constants in the inline assembly.
No functional changes intended.
--- arch/riscv/include/asm/runtime-const.h | 38 +++++++++++++------------ 1 file changed, 20 insertions(+), 18 deletions(-)
arch/riscv/include/asm/runtime-const.h +20 -18
@@ -15,21 +15,23 @@#include <linux/uaccess.h> +#define RUNTIME_MAGIC __ASM_STR(0x89ABCDEF) +#ifdef CONFIG_32BIT -#define runtime_const_ptr (sym) \ -({ \ - typeof(sym) __ret; \ - asm_inline(".option push\n\t" \ - ".option norvc\n\t" \ - "1:\t" \ - "lui %[__ret],0x89abd\n\t" \ - "addi %[__ret],%[__ret],-0x211\n\t" \ - ".option pop\n\t" \ - ".pushsection runtime_ptr_" #sym ",\"a\"\n\t" \ - ".long 1b - .\n\t" \ - ".popsection" \ - : [__ret] "=r" (__ret)); \ - __ret; \ +#define runtime_const_ptr (sym) \ +({ \ + typeof(sym) __ret; \ + asm_inline(".option push\n\t" \ + ".option norvc\n\t" \ + "1:\t" \ + "lui %[__ret], %%hi(" RUNTIME_MAGIC ")\n\t" \ + "addi %[__ret],%[__ret], %%lo(" RUNTIME_MAGIC ")\n\t" \ + ".option pop\n\t" \ + ".pushsection runtime_ptr_" #sym ",\"a\"\n\t" \ + ".long 1b - .\n\t" \ + ".popsection" \ + : [__ret] "=r" (__ret)); \ + __ret; \ })#else /*
@@ -46,10 +48,10 @@".option push\n\t" \".option norvc\n\t" \"1:\t" \ -"lui %[__ret],0x89abd\n\t" \ -"lui %[__tmp],0x1234\n\t" \ -"addiw %[__ret],%[__ret],-0x211\n\t" \ -"addiw %[__tmp],%[__tmp],0x567\n\t" \ +"lui %[__ret], %%hi(" RUNTIME_MAGIC ")\n\t" \ +"lui %[__tmp], %%hi(" RUNTIME_MAGIC ")\n\t" \ +"addiw %[__ret],%[__ret], %%lo(" RUNTIME_MAGIC ")\n\t" \ +"addiw %[__tmp],%[__tmp], %%lo(" RUNTIME_MAGIC ")\n\t" \ #define RISCV_RUNTIME_CONST_64_BASE \"slli %[__tmp],%[__tmp],32\n\t" \
PATCH v6 5/8 riscv/runtime-const: Introduce runtime_const_mask_32()
In reply to K Prateek NayakFutex hash computation requires a mask operation with read-only after init data that will be converted to a runtime constant in the subsequent commit.
Introduce runtime_const_mask_32 to further optimize the mask operation in the futex hash computation hot path. Since all the current use-cases are of the form GENMASK(n, 0), with n > 0, following sequence:
srli a0, a1, imm
slli a0, a0, imm
is used for RISC-V where imm = (31 - width) to improve instruction density and performance.
"The RISC-V Instruction Set Manual, Volume I - Unprivileged Architecture" [1] Sec. 2.4.1 "Integer Register-Immediate Instructions" notes the immediate shift for SRLI and SLLI are 5 bits wide starting at bit #10. __runtime_fixup_shift() is reused to patch the immediate shifts for the two instructions.
If a future use case arises that needs to tackle arbitrary mask, consider using:
lui a0, 0x12346 # upper; +0x800 then >>12 for correct rounding addi a0, a0, 0x678 # lower 12 bits
to patch the 32-bit mask in the asm block and return "__ret & (val)" from runtime_const_mask_32() which allows compiler to further optimize the logical and operation. __runtime_fixup_ptr() already patches a lui + addi sequence which can be reused when the need arises.
A possible implementation for this alternate scheme can be found at [2].
---
changelog v5..v6:
o Picked up tags from charlie. (Thanks a ton!)
o Check the val before using __fls(). Flagged by Sashiko for potential compiler optimizations that can consider val !=0 after an __fls() call and then silently pass the BUG_ON().
--- arch/riscv/include/asm/asm.h | 1 + arch/riscv/include/asm/runtime-const.h | 44 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+)
arch/riscv/include/asm/asm.h +1 -0
@@ -34,6 +34,7 @@#define SZREG __REG_SEL(8, 4)#define LGREG __REG_SEL(3, 2)#define SRLI __REG_SEL(srliw, srli) +#define SLLI __REG_SEL(slliw, slli)#if __SIZEOF_POINTER__ ==8 #ifdef __ASSEMBLER__
arch/riscv/include/asm/runtime-const.h +44 -0
56 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v6 5/8] riscv/runtime-const: Introduce runtime_const_mask_32()
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The BUG_ON bounds check in `__runtime_fixup_mask` incorrectly panics the kernel on a valid…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The BUG_ON bounds check in __runtime_fixup_mask incorrectly panics the kernel on a valid 32-bit all-ones mask (0xFFFFFFFF) due to an off-by-one error caused by confusing bit index with bit width.
- [Medium] The commit message and inline comments incorrectly describe the instruction sequence and immediate shift calculations, contradicting the correct code implementation. --
--- Patch [5]: [PATCH v6 5/8] riscv/runtime-const: Introduce runtime_const_mask_32() --- commit f8085a85ab8321e958c65177cacf4c654afce3c8
Author: K Prateek Nayak <[email protected]>
Subject: riscv/runtime-const: Introduce runtime_const_mask_32()
This commit introduces runtime_const_mask_32() to optimize mask operations in the futex hash computation hot path on RISC-V. It implements this using left and right shifts, patching the immediate values at runtime.
Quoted 9 lines
Introduce
runtime_const_mask_32to further optimize the mask operation in the futex hash computation hot path. Since all the current use-cases are of the formGENMASK(n, 0), with n > 0, following sequence:srli a0, a1, imm
slli a0, a0, immis used for RISC-V where imm = (31 - width) to improve instruction density and performance.
[Severity: Medium]
Is this description in the commit message accurate?
The commit message describes using an srli + slli sequence and calculates the shift as imm = 31 - width. However, the actual macro implementation correctly uses an slli + srli sequence (shifting left then right) to clear the upper bits, and __runtime_fixup_mask() uses imm = 32 - width.
Could the commit message be updated to match the implementation?
Quoted 4 lines
arch/riscv/include/asm/runtime-const.h +0
-0
[ ... ]
arch/riscv/include/asm/runtime-const.h static inline void __runtime_fixup_shift(void *where, unsigned long val) · line 288
Quoted 11 lines
arch/riscv/include/asm/runtime-const.h +7
-0@@ -262,6 +279,33 @@ static inline void __runtime_fixup_shift(void *where, unsigned long val)mutex_unlock (&text_mutex); } +static inline void __runtime_fixup_mask (void *where,unsigned long val) +{ +unsigned int width = (val) ?__fls (val) +1 :0 ; + +/* +* XXX: Current implementation only supports patching masks of +* form GENMASK(width, 0) (width >= 0) using a SRLI + SLLI
[Severity: Medium]
Is the inline documentation here slightly out of sync with the code?
Similar to the commit message, this mentions SRLI + SLLI and GENMASK(width, 0), but the code below uses GENMASK(width - 1, 0) and the macro actually implements SLLI + SRLI.
Quoted 1 line
arch/riscv/include/asm/runtime-const.h +1
-0+* sequence instead of LUI + ADDI + AND sequence to improve
[ ... ]
arch/riscv/include/asm/runtime-const.h static inline void __runtime_fixup_shift(void *where, unsigned long val) · line 303
Quoted 5 lines
arch/riscv/include/asm/runtime-const.h +5
-0+* and outside the asm block before returning the result. Fixup +* can simply reuse the existing __runtime_fixup_32() to patch +* the LUI + ADDI sequence. +*/ +BUG_ON (!val || width >31 || (GENMASK (width -1 ,0 ) != val));
[Severity: Medium]
Does this BUG_ON() condition reject a valid 32-bit mask?
When the mask is all ones (0xFFFFFFFF), __fls(val) + 1 evaluates to a width of 32. The width > 31 check would trip the BUG_ON() and panic the kernel.
A shift of 32 - 32 = 0 is mathematically valid for RISC-V, as using slliw and srliw with a 0 shift correctly truncates and zero-extends the 32-bit value to 64 bits.
Should this check allow a full 32-bit mask instead?
Quoted 4 lines
arch/riscv/include/asm/runtime-const.h +4
-0+ +__runtime_fixup_shift (where,32 - width); +__runtime_fixup_shift (where +4 ,32 - width); +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[tip: locking/core] riscv/runtime-const: Introduce runtime_const_mask_32()
In reply to K Prateek NayakThe following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 3f0d06613ae58ae09c48915638692d093168abc1 Gitweb: https://git.kernel.org/tip/3f0d06613ae58ae09c48915638692d093168abc1 Author: K Prateek Nayak <[email protected]> AuthorDate: Tue, 28 Jul 2026 05:25:37 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 28 Jul 2026 13:07:15 +02:00
riscv/runtime-const: Introduce runtime_const_mask_32()
Futex hash computation requires a mask operation with read-only after init data that will be converted to a runtime constant in the subsequent commit.
Introduce runtime_const_mask_32 to further optimize the mask operation in the futex hash computation hot path. Since all the current use-cases are of the form GENMASK(n, 0), with n > 0, following sequence:
srli a0, a1, imm
slli a0, a0, imm
is used for RISC-V where imm = (31 - width) to improve instruction density and performance.
"The RISC-V Instruction Set Manual, Volume I - Unprivileged Architecture" [1] Sec. 2.4.1 "Integer Register-Immediate Instructions" notes the immediate shift for SRLI and SLLI are 5 bits wide starting at bit #10. __runtime_fixup_shift() is reused to patch the immediate shifts for the two instructions.
If a future use case arises that needs to tackle arbitrary mask, consider using:
lui a0, 0x12346 # upper; +0x800 then >>12 for correct rounding addi a0, a0, 0x678 # lower 12 bits
to patch the 32-bit mask in the asm block and return "__ret & (val)" from runtime_const_mask_32() which allows compiler to further optimize the logical and operation. __runtime_fixup_ptr() already patches a lui + addi sequence which can be reused when the need arises.
A possible implementation for this alternate scheme can be found at [2].
--- arch/riscv/include/asm/asm.h | 1 +- arch/riscv/include/asm/runtime-const.h | 44 +++++++++++++++++++++++++- 2 files changed, 45 insertions(+)
arch/riscv/include/asm/asm.h +1 -0
@@ -34,6 +34,7 @@#define SZREG __REG_SEL(8, 4)#define LGREG __REG_SEL(3, 2)#define SRLI __REG_SEL(srliw, srli) +#define SLLI __REG_SEL(slliw, slli)#if __SIZEOF_POINTER__ ==8 #ifdef __ASSEMBLER__
arch/riscv/include/asm/runtime-const.h +44 -0
56 lines not on this page · raw message
Re: [tip: locking/core] riscv/runtime-const: Introduce runtime_const_mask_32()
Quoted 9 lines · * tip-bot2 for K Prateek Nayak <[email protected]> wrote:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 3f0d06613ae58ae09c48915638692d093168abc1 Gitweb: https://git.kernel.org/tip/3f0d06613ae58ae09c48915638692d093168abc1 Author: K Prateek Nayak <[email protected]> AuthorDate: Tue, 28 Jul 2026 05:25:37 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 28 Jul 2026 13:07:15 +02:00
Quoted 8 lines
riscv/runtime-const: Introduce
runtime_const_mask_32()Suggested-by Samuel Holland <[email protected]>Suggested-by Charlie Jenkins <[email protected]>Assisted-by Claude:claude-sonnet-4-5Signed-off-by Charlie Jenkins <[email protected]>Signed-off-by K Prateek Nayak <[email protected]>Signed-off-by Peter Zijlstra (Intel) <[email protected]>
So author is Prateek, but the first SOB is Chalie Jenkins's? That's not a valid SOB chain.
Is a 'From: Charlie' line missing, or was it Co-developed-by?
Thanks,
Ingo
Re: [tip: locking/core] riscv/runtime-const: Introduce runtime_const_mask_32()
Hello Ingo, Peter,
Quoted 21 lines · Ingo Molnar
Quoted 9 lines · * tip-bot2 for K Prateek Nayak <[email protected]> wrote:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 3f0d06613ae58ae09c48915638692d093168abc1 Gitweb: https://git.kernel.org/tip/3f0d06613ae58ae09c48915638692d093168abc1 Author: K Prateek Nayak <[email protected]> AuthorDate: Tue, 28 Jul 2026 05:25:37 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 28 Jul 2026 13:07:15 +02:00Quoted 8 lines
riscv/runtime-const: Introduce
runtime_const_mask_32()Suggested-by Samuel Holland <[email protected]>Suggested-by Charlie Jenkins <[email protected]>Assisted-by Claude:claude-sonnet-4-5Signed-off-by Charlie Jenkins <[email protected]>Signed-off-by K Prateek Nayak <[email protected]>Signed-off-by Peter Zijlstra (Intel) <[email protected]>
Quoted 4 lines
So author is Prateek, but the first SOB is Chalie Jenkins's? That's not a valid SOB chain.
Is a 'From: Charlie' line missing, or was it Co-developed-by?
That was b4 picking up a S-o-b for a parallel suggestion here
https://lore.kernel.org/lkml/alXQY4hgZYLmr8x7@blinky/
Sorry I didn't catch this earlier. Could you please drop that tag.
Signature · 2 lines
Thanks and Regards, Prateek
Peter Zijlstra
reviewerRe: [tip: locking/core] riscv/runtime-const: Introduce runtime_const_mask_32()
Quoted 27 lines · K Prateek Nayak
Hello Ingo, Peter,
Quoted 24 lines · Ingo Molnar
Quoted 9 lines · * tip-bot2 for K Prateek Nayak <[email protected]> wrote:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 3f0d06613ae58ae09c48915638692d093168abc1 Gitweb: https://git.kernel.org/tip/3f0d06613ae58ae09c48915638692d093168abc1 Author: K Prateek Nayak <[email protected]> AuthorDate: Tue, 28 Jul 2026 05:25:37 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 28 Jul 2026 13:07:15 +02:00Quoted 8 lines
riscv/runtime-const: Introduce
runtime_const_mask_32()Suggested-by Samuel Holland <[email protected]>Suggested-by Charlie Jenkins <[email protected]>Assisted-by Claude:claude-sonnet-4-5Signed-off-by Charlie Jenkins <[email protected]>Signed-off-by K Prateek Nayak <[email protected]>Signed-off-by Peter Zijlstra (Intel) <[email protected]>So author is Prateek, but the first SOB is Chalie Jenkins's? That's not a valid SOB chain.
Quoted 6 lines
Quoted 1 line
Is a 'From: Charlie' line missing, or was it Co-developed-by?
That was b4 picking up a S-o-b for a parallel suggestion here
https://lore.kernel.org/lkml/alXQY4hgZYLmr8x7@blinky/Sorry I didn't catch this earlier. Could you please drop that tag.
No problem, should be fixed now.
[tip: locking/core] riscv/runtime-const: Introduce runtime_const_mask_32()
In reply to K Prateek NayakThe following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: e412c77541c7926ff61baec712616837a2c6887f Gitweb: https://git.kernel.org/tip/e412c77541c7926ff61baec712616837a2c6887f Author: K Prateek Nayak <[email protected]> AuthorDate: Tue, 28 Jul 2026 05:25:37 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 29 Jul 2026 13:17:45 +02:00
riscv/runtime-const: Introduce runtime_const_mask_32()
Futex hash computation requires a mask operation with read-only after init data that will be converted to a runtime constant in the subsequent commit.
Introduce runtime_const_mask_32 to further optimize the mask operation in the futex hash computation hot path. Since all the current use-cases are of the form GENMASK(n, 0), with n > 0, following sequence:
srli a0, a1, imm
slli a0, a0, imm
is used for RISC-V where imm = (31 - width) to improve instruction density and performance.
"The RISC-V Instruction Set Manual, Volume I - Unprivileged Architecture" [1] Sec. 2.4.1 "Integer Register-Immediate Instructions" notes the immediate shift for SRLI and SLLI are 5 bits wide starting at bit #10. __runtime_fixup_shift() is reused to patch the immediate shifts for the two instructions.
If a future use case arises that needs to tackle arbitrary mask, consider using:
lui a0, 0x12346 # upper; +0x800 then >>12 for correct rounding addi a0, a0, 0x678 # lower 12 bits
to patch the 32-bit mask in the asm block and return "__ret & (val)" from runtime_const_mask_32() which allows compiler to further optimize the logical and operation. __runtime_fixup_ptr() already patches a lui + addi sequence which can be reused when the need arises.
A possible implementation for this alternate scheme can be found at [2].
--- arch/riscv/include/asm/asm.h | 1 +- arch/riscv/include/asm/runtime-const.h | 44 +++++++++++++++++++++++++- 2 files changed, 45 insertions(+)
arch/riscv/include/asm/asm.h +1 -0
@@ -34,6 +34,7 @@#define SZREG __REG_SEL(8, 4)#define LGREG __REG_SEL(3, 2)#define SRLI __REG_SEL(srliw, srli) +#define SLLI __REG_SEL(slliw, slli)#if __SIZEOF_POINTER__ ==8 #ifdef __ASSEMBLER__
arch/riscv/include/asm/runtime-const.h +44 -0
56 lines not on this page · raw message
PATCH v6 6/8 s390/runtime-const: Introduce runtime_const_mask_32()
In reply to K Prateek NayakFutex hash computation requires a mask operation with read-only after init data that will be converted to a runtime constant in the subsequent commit.
Introduce runtime_const_mask_32 to further optimize the mask operation in the futex hash computation hot path.
GCC generates a:
nilf %r1,<imm32>
to tackle arbitrary 32-bit masks and the same is implemented here. Immediate patching pattern for __runtime_fixup_mask() has been adopted from __runtime_fixup_ptr().
---
changelog v5..v6:
o No changes.
--- arch/s390/include/asm/runtime-const.h | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
arch/s390/include/asm/runtime-const.h +21 -1
@@ -33,6 +33,20 @@ __ret; \ }) +#define runtime_const_mask_32 (val, sym) \ +({ \ + unsigned int __ret = (val); \ + \ + asm_inline( \ + "0: nilf %[__ret],12\n" \ + ".pushsection runtime_mask_" #sym ",\"a\"\n" \ + ".long 0b - .\n" \ + ".popsection" \ + : [__ret] "+d" (__ret) \ + : : "cc"); \ + __ret; \ +}) +#define runtime_const_init (type, sym) do { \ extern s32 __start_runtime_##type##_##sym[]; \ extern s32 __stop_runtime_##type##_##sym[]; \
@@ -43,12 +57,12 @@ __stop_runtime_##type##_##sym); \ } while (0) -/* 32-bit immediate for iihf and iilf in bits in I2 field */ static inline void __runtime_fixup_32 (u32 *p,unsigned int val) {s390_kernel_write (p, &val,sizeof (val)); } +/* 32-bit immediate for iihf and iilf in bits in I2 field */ static inline void __runtime_fixup_ptr (void *where,unsigned long val) {__runtime_fixup_32 (where +2 , val >>32 );
@@ -65,6 +79,12 @@ static inline void __runtime_fixup_shift(void *where, unsigned long val)s390_kernel_write (where, &insn,sizeof (insn)); } +/* 32-bit immediate for nilf in bits in I2 field */ +static inline void __runtime_fixup_mask (void *where,unsigned long val) +{ +__runtime_fixup_32 (where +2 , val); +} +static inline void runtime_const_fixup (void (*fn)(void *,unsigned long ),unsigned long val,s32 *start,s32 *end) {
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v6 6/8] s390/runtime-const: Introduce runtime_const_mask_32()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
Futex hash computation requires a mask operation with read-only after init data that will be converted to a runtime constant in the subsequent commit.
Introduce
runtime_const_mask_32to further optimize the mask operation in the futex hash computation hot path.GCC generates a:
Quoted 8 lines
nilf %r1,<imm32>
to tackle arbitrary 32-bit masks and the same is implemented here. Immediate patching pattern for
__runtime_fixup_mask()has been adopted from__runtime_fixup_ptr().Acked-by Heiko Carstens <[email protected]>Signed-off-by K Prateek Nayak <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
[tip: locking/core] s390/runtime-const: Introduce runtime_const_mask_32()
In reply to K Prateek NayakThe following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 2593da6abcabe0b9d93d1bb9affff5769e16e844 Gitweb: https://git.kernel.org/tip/2593da6abcabe0b9d93d1bb9affff5769e16e844 Author: K Prateek Nayak <[email protected]> AuthorDate: Tue, 28 Jul 2026 05:25:38 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 28 Jul 2026 13:07:15 +02:00
s390/runtime-const: Introduce runtime_const_mask_32()
Futex hash computation requires a mask operation with read-only after init data that will be converted to a runtime constant in the subsequent commit.
Introduce runtime_const_mask_32 to further optimize the mask operation in the futex hash computation hot path.
GCC generates a:
nilf %r1,<imm32>
to tackle arbitrary 32-bit masks and the same is implemented here. Immediate patching pattern for __runtime_fixup_mask() has been adopted from __runtime_fixup_ptr().
--- arch/s390/include/asm/runtime-const.h | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
arch/s390/include/asm/runtime-const.h +21 -1
@@ -33,6 +33,20 @@ __ret; \ }) +#define runtime_const_mask_32 (val, sym) \ +({ \ + unsigned int __ret = (val); \ + \ + asm_inline( \ + "0: nilf %[__ret],12\n" \ + ".pushsection runtime_mask_" #sym ",\"a\"\n" \ + ".long 0b - .\n" \ + ".popsection" \ + : [__ret] "+d" (__ret) \ + : : "cc"); \ + __ret; \ +}) +#define runtime_const_init (type, sym) do { \ extern s32 __start_runtime_##type##_##sym[]; \ extern s32 __stop_runtime_##type##_##sym[]; \
@@ -43,12 +57,12 @@ __stop_runtime_##type##_##sym); \ } while (0) -/* 32-bit immediate for iihf and iilf in bits in I2 field */ static inline void __runtime_fixup_32 (u32 *p,unsigned int val) {s390_kernel_write (p, &val,sizeof (val)); } +/* 32-bit immediate for iihf and iilf in bits in I2 field */ static inline void __runtime_fixup_ptr (void *where,unsigned long val) {__runtime_fixup_32 (where +2 , val >>32 );
@@ -65,6 +79,12 @@ static inline void __runtime_fixup_shift(void *where, unsigned long val)s390_kernel_write (where, &insn,sizeof (insn)); } +/* 32-bit immediate for nilf in bits in I2 field */ +static inline void __runtime_fixup_mask (void *where,unsigned long val) +{ +__runtime_fixup_32 (where +2 , val); +} +static inline void runtime_const_fixup (void (*fn)(void *,unsigned long ),unsigned long val,s32 *start,s32 *end) {
[tip: locking/core] s390/runtime-const: Introduce runtime_const_mask_32()
In reply to K Prateek NayakThe following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: cf0de88094a7b4df18008b6efb1290f4efed731f Gitweb: https://git.kernel.org/tip/cf0de88094a7b4df18008b6efb1290f4efed731f Author: K Prateek Nayak <[email protected]> AuthorDate: Tue, 28 Jul 2026 05:25:38 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 29 Jul 2026 13:17:52 +02:00
s390/runtime-const: Introduce runtime_const_mask_32()
Futex hash computation requires a mask operation with read-only after init data that will be converted to a runtime constant in the subsequent commit.
Introduce runtime_const_mask_32 to further optimize the mask operation in the futex hash computation hot path.
GCC generates a:
nilf %r1,<imm32>
to tackle arbitrary 32-bit masks and the same is implemented here. Immediate patching pattern for __runtime_fixup_mask() has been adopted from __runtime_fixup_ptr().
--- arch/s390/include/asm/runtime-const.h | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
arch/s390/include/asm/runtime-const.h +21 -1
@@ -33,6 +33,20 @@ __ret; \ }) +#define runtime_const_mask_32 (val, sym) \ +({ \ + unsigned int __ret = (val); \ + \ + asm_inline( \ + "0: nilf %[__ret],12\n" \ + ".pushsection runtime_mask_" #sym ",\"a\"\n" \ + ".long 0b - .\n" \ + ".popsection" \ + : [__ret] "+d" (__ret) \ + : : "cc"); \ + __ret; \ +}) +#define runtime_const_init (type, sym) do { \ extern s32 __start_runtime_##type##_##sym[]; \ extern s32 __stop_runtime_##type##_##sym[]; \
@@ -43,12 +57,12 @@ __stop_runtime_##type##_##sym); \ } while (0) -/* 32-bit immediate for iihf and iilf in bits in I2 field */ static inline void __runtime_fixup_32 (u32 *p,unsigned int val) {s390_kernel_write (p, &val,sizeof (val)); } +/* 32-bit immediate for iihf and iilf in bits in I2 field */ static inline void __runtime_fixup_ptr (void *where,unsigned long val) {__runtime_fixup_32 (where +2 , val >>32 );
@@ -65,6 +79,12 @@ static inline void __runtime_fixup_shift(void *where, unsigned long val)s390_kernel_write (where, &insn,sizeof (insn)); } +/* 32-bit immediate for nilf in bits in I2 field */ +static inline void __runtime_fixup_mask (void *where,unsigned long val) +{ +__runtime_fixup_32 (where +2 , val); +} +static inline void runtime_const_fixup (void (*fn)(void *,unsigned long ),unsigned long val,s32 *start,s32 *end) {
PATCH v6 7/8 asm-generic/runtime-const: Add dummy runtime_const_mask_32()
In reply to K Prateek NayakFrom: Peter Zijlstra <[email protected]>
Add a dummy runtime_const_mask_32() for all the architectures that do not support runtime-const.
---
changelog v5..v6:
o Picked up tags from Charlie. (Thanks a ton!)
o Picked up Peter's S-o-b from his tree.
--- include/asm-generic/runtime-const.h | 1 + 1 file changed, 1 insertion(+)
include/asm-generic/runtime-const.h +1 -0
@@ -10,6 +10,7 @@*/ #define runtime_const_ptr (sym) (sym)#define runtime_const_shift_right_32 (val, sym) ((u32)(val)>>(sym)) +#define runtime_const_mask_32 (val, sym) ((u32)(val)&(sym))#define runtime_const_init (type,sym) do { } while (0)#endif
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v6 7/8] asm-generic/runtime-const: Add dummy runtime_const_mask_32()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
From: Peter Zijlstra <[email protected]>
Add a dummy
runtime_const_mask_32()for all the architectures that do not support runtime-const.Signed-off-by Peter Zijlstra <[email protected]>Reviewed-by Charlie Jenkins <[email protected]>Tested-by Charlie Jenkins <[email protected]>Signed-off-by K Prateek Nayak <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
[tip: locking/core] asm-generic/runtime-const: Add dummy runtime_const_mask_32()
In reply to K Prateek NayakThe following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: c24195555982b292efa05762aed9d98dfebb6585 Gitweb: https://git.kernel.org/tip/c24195555982b292efa05762aed9d98dfebb6585 Author: Peter Zijlstra <[email protected]> AuthorDate: Tue, 28 Jul 2026 05:25:39 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 28 Jul 2026 13:07:16 +02:00
asm-generic/runtime-const: Add dummy runtime_const_mask_32()
Add a dummy runtime_const_mask_32() for all the architectures that do not support runtime-const.
--- include/asm-generic/runtime-const.h | 1 + 1 file changed, 1 insertion(+)
include/asm-generic/runtime-const.h +1 -0
@@ -10,6 +10,7 @@*/ #define runtime_const_ptr (sym) (sym)#define runtime_const_shift_right_32 (val, sym) ((u32)(val)>>(sym)) +#define runtime_const_mask_32 (val, sym) ((u32)(val)&(sym))#define runtime_const_init (type,sym) do { } while (0)#endif
[tip: locking/core] asm-generic/runtime-const: Add dummy runtime_const_mask_32()
In reply to K Prateek NayakThe following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: cb9362dddcd167662ad7c6347ce96fd47890a27f Gitweb: https://git.kernel.org/tip/cb9362dddcd167662ad7c6347ce96fd47890a27f Author: Peter Zijlstra <[email protected]> AuthorDate: Tue, 28 Jul 2026 05:25:39 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 29 Jul 2026 13:17:52 +02:00
asm-generic/runtime-const: Add dummy runtime_const_mask_32()
Add a dummy runtime_const_mask_32() for all the architectures that do not support runtime-const.
--- include/asm-generic/runtime-const.h | 1 + 1 file changed, 1 insertion(+)
include/asm-generic/runtime-const.h +1 -0
@@ -10,6 +10,7 @@*/ #define runtime_const_ptr (sym) (sym)#define runtime_const_shift_right_32 (val, sym) ((u32)(val)>>(sym)) +#define runtime_const_mask_32 (val, sym) ((u32)(val)&(sym))#define runtime_const_init (type,sym) do { } while (0)#endif
PATCH v6 8/8 futex: Use runtime constants for __futex_hash() hot path
In reply to K Prateek NayakFrom: Peter Zijlstra <[email protected]>
Runtime constify the read-only after init data __futex_shift(shift_32), __futex_mask(mask_32), and __futex_queues(ptr) used in __futex_hash() hot path to avoid referencing global variable.
This also allows __futex_queues to be allocated dynamically to "nr_node_ids" slots instead of reserving config dependent MAX_NUMNODES (1 << CONFIG_NODES_SHIFT) worth of slots upfront.
Runtime constants are initialized before their first access and runtime_const_init() provides necessary barrier to ensure subsequent accesses are not reordered against their initialization.
No functional changes intended.
[ prateek: Dynamically allocate __futex_queues, mark the global data
__ro_after_init since they are constified after futex_init(). ]
---
changelog v5..v6:
o Picked up tags from Charlie. (Thanks a ton!)
o Picked up Peter's S-o-b from his tree.
o Added a barrier after runtime_const_init() after to prevent compilers from re-ordering a later runtime const usage in the init function with their initialization. (Sashiko; Reported by Intel test robot with a GCC14 build).
(https://lore.kernel.org/lkml/alNgNvhIkBfkGo1Y@xsang-OptiPlex-9020/)
--- include/asm-generic/vmlinux.lds.h | 5 +++- kernel/futex/core.c | 44 ++++++++++++++++++------------- 2 files changed, 29 insertions(+), 20 deletions(-)
include/asm-generic/vmlinux.lds.h +4 -1
@@ -970,7 +970,10 @@ RUNTIME_CONST(ptr, __dentry_cache) \ RUNTIME_CONST(ptr, __names_cache) \ RUNTIME_CONST(ptr, __filp_cache) \ -RUNTIME_CONST (ptr, __bfilp_cache) + RUNTIME_CONST(ptr, __bfilp_cache) \ + RUNTIME_CONST(shift, __futex_shift) \ + RUNTIME_CONST(mask, __futex_mask) \ +RUNTIME_CONST (ptr, __futex_queues) /* Alignment must be consistent with (kunit_suite *) in include/kunit/test.h */#define KUNIT_TABLE () \
kernel/futex/core.c +25 -19
88 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v6 8/8] futex: Use runtime constants for __futex_hash() hot path
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines
From: Peter Zijlstra <[email protected]>
Runtime constify the read-only after init data
__futex_shift(shift_32),__futex_mask(mask_32), and__futex_queues(ptr)used in__futex_hash()hot path to avoid referencing global variable.This also allows
__futex_queuesto be allocated dynamically to "nr_node_ids" slots instead of reserving config dependentMAX_NUMNODES(1 <<CONFIG_NODES_SHIFT) worth of slots upfront.Runtime constants are initialized before their first access and
runtime_const_init()provides necessary barrier to ensure subsequent accesses are not reordered against their initialization.No functional changes intended.
[ prateek: Dynamically allocate
__futex_queues, mark the global data
__ro_after_initsince they are constified afterfutex_init(). ]
Quoted 6 lines
Signed-off-by Peter Zijlstra <[email protected]>Reviewed-by Charlie Jenkins <[email protected]>Tested-by Charlie Jenkins <[email protected]>Signed-off-by K Prateek Nayak <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Peter Zijlstra
reviewerRe: [PATCH v6 8/8] futex: Use runtime constants for __futex_hash() hot path
In reply to K Prateek NayakQuoted 25 lines · K Prateek Nayak
From: Peter Zijlstra <[email protected]>
Runtime constify the read-only after init data
__futex_shift(shift_32),__futex_mask(mask_32), and__futex_queues(ptr)used in__futex_hash()hot path to avoid referencing global variable.This also allows
__futex_queuesto be allocated dynamically to "nr_node_ids" slots instead of reserving config dependentMAX_NUMNODES(1 <<CONFIG_NODES_SHIFT) worth of slots upfront.Runtime constants are initialized before their first access and
runtime_const_init()provides necessary barrier to ensure subsequent accesses are not reordered against their initialization.No functional changes intended.
[ prateek: Dynamically allocate
__futex_queues, mark the global data
__ro_after_initsince they are constified afterfutex_init(). ]Signed-off-by Peter Zijlstra <[email protected]>Reviewed-by Charlie Jenkins <[email protected]>Tested-by Charlie Jenkins <[email protected]>Signed-off-by K Prateek Nayak <[email protected]>
Quoted 6 lines
---
changelog v5..v6:o Picked up tags from Charlie. (Thanks a ton!)
o Picked up Peter's S-o-b from his tree.
Right, so I also munged the Changelog in my tree to include those benchmark results. I failed to actually push out those patches before disappearing on holidays, but I recon I should refresh them patches and re-munge the changelog?
Quoted 5 lines
o Added a barrier after
runtime_const_init()after to prevent compilers from re-ordering a later runtime const usage in the init function with their initialization. (Sashiko; Reported by Intel test robot with a GCC14 build).
(https://lore.kernel.org/lkml/alNgNvhIkBfkGo1Y@xsang-OptiPlex-9020/)
Peter Zijlstra
reviewerRe: [PATCH v6 8/8] futex: Use runtime constants for __futex_hash() hot path
Quoted 29 lines · Peter Zijlstra
Quoted 28 lines · K Prateek Nayak
From: Peter Zijlstra <[email protected]>
Runtime constify the read-only after init data
__futex_shift(shift_32),__futex_mask(mask_32), and__futex_queues(ptr)used in__futex_hash()hot path to avoid referencing global variable.This also allows
__futex_queuesto be allocated dynamically to "nr_node_ids" slots instead of reserving config dependentMAX_NUMNODES(1 <<CONFIG_NODES_SHIFT) worth of slots upfront.Runtime constants are initialized before their first access and
runtime_const_init()provides necessary barrier to ensure subsequent accesses are not reordered against their initialization.No functional changes intended.
[ prateek: Dynamically allocate
__futex_queues, mark the global data
__ro_after_initsince they are constified afterfutex_init(). ]Signed-off-by Peter Zijlstra <[email protected]>Reviewed-by Charlie Jenkins <[email protected]>Tested-by Charlie Jenkins <[email protected]>Signed-off-by K Prateek Nayak <[email protected]>---
changelog v5..v6:
Quoted 8 lines
Quoted 3 lines
o Picked up tags from Charlie. (Thanks a ton!)
o Picked up Peter's S-o-b from his tree.
Right, so I also munged the Changelog in my tree to include those benchmark results. I failed to actually push out those patches before disappearing on holidays, but I recon I should refresh them patches and re-munge the changelog?
With a bit of luck I didn't wreck things and this new version should now be available in queue/locking/core.
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH v6 8/8] futex: Use runtime constants for __futex_hash() hot path
Hello Peter,
Quoted 7 lines · Peter Zijlstra
Quoted 4 lines
Right, so I also munged the Changelog in my tree to include those benchmark results. I failed to actually push out those patches before disappearing on holidays, but I recon I should refresh them patches and re-munge the changelog?
With a bit of luck I didn't wreck things and this new version should now be available in queue/locking/core.
Thank you for fixing the changelog and refreshing the tree! I gave it a spin like everything looks alright - x86 on baremetal, and others in QEMU.
Signature · 2 lines
Thanks and Regards, Prateek
[tip: locking/core] futex: Use runtime constants for __futex_hash() hot path
In reply to K Prateek NayakThe following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 45ac711e2f14833534a57f785f17e20873b1ba21 Gitweb: https://git.kernel.org/tip/45ac711e2f14833534a57f785f17e20873b1ba21 Author: Peter Zijlstra <[email protected]> AuthorDate: Tue, 28 Jul 2026 05:25:40 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 28 Jul 2026 13:07:16 +02:00
futex: Use runtime constants for __futex_hash() hot path
Runtime constify the read-only after init data __futex_shift(shift_32), __futex_mask(mask_32), and __futex_queues(ptr) used in __futex_hash() hot path to avoid referencing global variable.
This also allows __futex_queues to be allocated dynamically to "nr_node_ids" slots instead of reserving config dependent MAX_NUMNODES (1 << CONFIG_NODES_SHIFT) worth of slots upfront.
Runtime constants are initialized before their first access and runtime_const_init() provides necessary barrier to ensure subsequent accesses are not reordered against their initialization.
No functional changes intended.
perf bench futex on a 3rd Gen EPYC (2 x 64C/128T):
+----------------+-----------+-----------+-----------+--------------+ | Benchmark | Kernel 1 | Kernel 2 | Unit | % Improvement| | | (avg/5) | (avg/5) | | (K2 vs K1) | +----------------+-----------+-----------+-----------+--------------+ | Wake-parallel | 0.01614 | 0.00456 | ms | +71.75% | | Requeue | 0.26394 | 0.24644 | ms | +6.63% | | Lock-pi | 34.0 | 57.2 | ops/sec | +68.24% | +----------------+-----------+-----------+-----------+--------------+
Performance testing on a 144-thread Intel(R) Xeon(R) CPU E7-8890 v3 (4 NUMA nodes):
+-------------------------------------------------------------+ | perf bench futex hash -b 0 | +----------------------+------------+------------+------------+ | Configuration | As-is | Patched | Delta | +----------------------+------------+------------+------------+ | 1 thread, 1 futex | 6,449,632 | 6,532,004 | +1.28% | | 144 threads, 1024 fx | 2,111,486 | 2,139,685 | +1.34% | +----------------------+------------+------------+------------+i
[ prateek: Dynamically allocate __futex_queues, mark the global data
__ro_after_init since they are constified after futex_init(). ]
--- include/asm-generic/vmlinux.lds.h | 5 ++- kernel/futex/core.c | 44 +++++++++++++++++------------- 2 files changed, 29 insertions(+), 20 deletions(-)
include/asm-generic/vmlinux.lds.h +4 -1
@@ -970,7 +970,10 @@ RUNTIME_CONST(ptr, __dentry_cache) \ RUNTIME_CONST(ptr, __names_cache) \ RUNTIME_CONST(ptr, __filp_cache) \ -RUNTIME_CONST (ptr, __bfilp_cache) + RUNTIME_CONST(ptr, __bfilp_cache) \ + RUNTIME_CONST(shift, __futex_shift) \ + RUNTIME_CONST(mask, __futex_mask) \ +RUNTIME_CONST (ptr, __futex_queues) /* Alignment must be consistent with (kunit_suite *) in include/kunit/test.h */#define KUNIT_TABLE () \
kernel/futex/core.c +25 -19
88 lines not on this page · raw message
[tip: locking/core] futex: Use runtime constants for __futex_hash() hot path
In reply to K Prateek NayakThe following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: b78b0b65825275f58336a43611a700de174be8c3 Gitweb: https://git.kernel.org/tip/b78b0b65825275f58336a43611a700de174be8c3 Author: Peter Zijlstra <[email protected]> AuthorDate: Tue, 28 Jul 2026 05:25:40 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 29 Jul 2026 13:17:52 +02:00
futex: Use runtime constants for __futex_hash() hot path
Runtime constify the read-only after init data __futex_shift(shift_32), __futex_mask(mask_32), and __futex_queues(ptr) used in __futex_hash() hot path to avoid referencing global variable.
This also allows __futex_queues to be allocated dynamically to "nr_node_ids" slots instead of reserving config dependent MAX_NUMNODES (1 << CONFIG_NODES_SHIFT) worth of slots upfront.
Runtime constants are initialized before their first access and runtime_const_init() provides necessary barrier to ensure subsequent accesses are not reordered against their initialization.
No functional changes intended.
perf bench futex on a 3rd Gen EPYC (2 x 64C/128T):
+----------------+-----------+-----------+-----------+--------------+ | Benchmark | Kernel 1 | Kernel 2 | Unit | % Improvement| | | (avg/5) | (avg/5) | | (K2 vs K1) | +----------------+-----------+-----------+-----------+--------------+ | Wake-parallel | 0.01614 | 0.00456 | ms | +71.75% | | Requeue | 0.26394 | 0.24644 | ms | +6.63% | | Lock-pi | 34.0 | 57.2 | ops/sec | +68.24% | +----------------+-----------+-----------+-----------+--------------+
Performance testing on a 144-thread Intel(R) Xeon(R) CPU E7-8890 v3 (4 NUMA nodes):
+-------------------------------------------------------------+ | perf bench futex hash -b 0 | +----------------------+------------+------------+------------+ | Configuration | As-is | Patched | Delta | +----------------------+------------+------------+------------+ | 1 thread, 1 futex | 6,449,632 | 6,532,004 | +1.28% | | 144 threads, 1024 fx | 2,111,486 | 2,139,685 | +1.34% | +----------------------+------------+------------+------------+i
[ prateek: Dynamically allocate __futex_queues, mark the global data
__ro_after_init since they are constified after futex_init(). ]
--- include/asm-generic/vmlinux.lds.h | 5 ++- kernel/futex/core.c | 44 +++++++++++++++++------------- 2 files changed, 29 insertions(+), 20 deletions(-)
include/asm-generic/vmlinux.lds.h +4 -1
@@ -970,7 +970,10 @@ RUNTIME_CONST(ptr, __dentry_cache) \ RUNTIME_CONST(ptr, __names_cache) \ RUNTIME_CONST(ptr, __filp_cache) \ -RUNTIME_CONST (ptr, __bfilp_cache) + RUNTIME_CONST(ptr, __bfilp_cache) \ + RUNTIME_CONST(shift, __futex_shift) \ + RUNTIME_CONST(mask, __futex_mask) \ +RUNTIME_CONST (ptr, __futex_queues) /* Alignment must be consistent with (kunit_suite *) in include/kunit/test.h */#define KUNIT_TABLE () \
kernel/futex/core.c +25 -19
88 lines not on this page · raw message