futex: Use runtime constants for futex_hash computation
v3 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 [1].
Comments that have *not* been addressed in this version
=======================================================
Samuel had an observation on v2 that __futex_mask is always of the form
((1 << bits) - 1) /* Only lower bits set; bits > 1. */
and ARM64 and RISC-V can use a single ubfx (ARM64), or slli+srli pattern (RISC-V) for the mask operation respectively but this had the main limitation of runtime_const_mask_32() only working with masks of such form and others would fail runtime_const_init() at boot.
RISC-V does generated a "addi + slli" pattern with CONFIG_BASE_SMALL=y where the futex_hash_mask can be computed at compile time.
The old scheme is retained for now since it is equivalent to the generated asm for !CONFIG_BASE_SMALL and can handle any arbitrary masks allowing for all future use cases.
If there is enough interest, please let me know, and I can look into further optimization to runtime_const_mask_32() based on the current use case for __futex_hash.
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 1086b33a3f64 ("Merge branch into tip/master: 'x86/vdso'") (2026-04-02)
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 rfc v2..v3:
o Collected Ack from Heiko for s390 bits after folding in their suggested changes (Thanks a ton!)
o Reordered Patch 2 and Patch 3 to allow for runtime_const_init() at late_initcall() first before introducing runtime_const_mask_32() on ARM64. (David)
o Moved the "&" operation outside the inline asm block on ARM64 and RISC-V which allows the compiler to optimize it further if possible. (David)
o Dropped the RFC tag.
v2: https://lore.kernel.org/lkml/[email protected]/ [1]
changelog rfc v1..rfc v2:
o Use runtime constants to avoid the dereference overheads for dynamically allocated futex_queues.
o arch/ side plumbings for runtime_const_mask_32()
v1: https://lore.kernel.org/all/[email protected]/
---
K Prateek Nayak (4):
arm64/runtime-const: Use aarch64_insn_patch_text_nosync() for patching arm64/runtime-const: Introduce runtime_const_mask_32() 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 | 32 ++++++++++++++------ arch/riscv/include/asm/runtime-const.h | 22 ++++++++++++++ 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 | 42 ++++++++++++++------------ 7 files changed, 107 insertions(+), 31 deletions(-)
base-commit: 1086b33a3f644c3bc37abefd699defc45accced1
Signature · 1 line
2.34.1
PATCH v3 1/7 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 v2..v3:
o No changes.
--- 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
PATCH v3 2/7 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))---
Changelog v2..v3:
o Reordered this to come before the introduction of runtime_const_mask_32(). (David)
o Trimmed down the commit message to be more precise.
--- arch/arm64/include/asm/runtime-const.h | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-)
arch/arm64/include/asm/runtime-const.h +3 -10
@@ -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,13 +51,7 @@ 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)
@@ -66,7 +61,6 @@ static inline void __runtime_fixup_ptr(void *where, unsigned long 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 */
@@ -76,8 +70,7 @@ static inline void __runtime_fixup_shift(void *where, unsigned long val)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
Catalin Marinas
maintainerRe: [PATCH v3 2/7] arm64/runtime-const: Use aarch64_insn_patch_text_nosync() for patching
Quoted 15 lines · K Prateek Nayak
arch/arm64/include/asm/runtime-const.h +1
-0@@ -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,13 +51,7 @@ static inline void __runtime_fixup_16(__le32 *p, unsigned int val)u32 insn =le32_to_cpu (*p); insn &=0xffe0001f ;
arch/arm64/include/asm/runtime-const.h static inline void __runtime_fixup_16(__le32 *p, unsigned int val) · line 55
Quoted 10 lines
arch/arm64/include/asm/runtime-const.h +1
-7insn |= (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); }
Sashiko has some good points here:
https://sashiko.dev/#/patchset/[email protected]
In short, aarch64_insn_patch_text_nosync() does not expect a linear map address but rather a kernel text one (or vmalloc/modules). The other valid point is on aliasing I-caches.
I think dropping the lm_alias() and just use 'where' directly would do but I haven't tried.
Signature · 1 line
Catalin
Re: [PATCH v3 2/7] arm64/runtime-const: Use aarch64_insn_patch_text_nosync() for patching
Hello Catalin,
Thank you for taking a look at the series and pointing to the Shashiko review.
Quoted 8 lines · Catalin Marinas
Review
Quoted 6 lines
(quoted diff) +1
-4-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); }Sashiko has some good points here:
Ack! I did check the Sashiko review a few days after posting. I think I'll probably start replying to Shahsiko's inline review on future threads on LKML to keep the record like some folks are doing.
Quoted 9 lines
https://sashiko.dev/#/patchset/[email protected]
In short,
aarch64_insn_patch_text_nosync()does not expect a linear map address but rather a kernel text one (or vmalloc/modules). The other valid point is on aliasing I-caches.I think dropping the
lm_alias()and just use 'where' directly would do but I haven't tried.
Ack! I completely missed that subtlety of passing "where" to caches_clean_inval_pou(). I'm still surprised that it didn't blow up in my testing.
Anyhow, following diff, on top of the full series builds and tests fine and has been blessed by review-prompts:
arch/arm64/include/asm/runtime-const.h +10 -11
@@ -57,21 +57,21 @@ }while (0 )/* 16-bit immediate for wide move (movz and movk) in bits 5..20 */ -static inline void __runtime_fixup_16(__le32 *p, unsigned int val) +static inline void __runtime_fixup_16(void *where, unsigned int val) { +__le32 *p =lm_alias (where);u32 insn =le32_to_cpu (*p); insn &=0xffe0001f ; insn |= (val &0xffff ) <<5 ; -aarch64_insn_patch_text_nosync (p, insn); +aarch64_insn_patch_text_nosync (where, insn); }static inline void __runtime_fixup_ptr (void *where,unsigned long val) { -__le32 *p =lm_alias (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_16 (where, val); +__runtime_fixup_16 (where +4 , val >>16 ); +__runtime_fixup_16 (where +8 , val >>32 ); +__runtime_fixup_16 (where +12 , val >>48 ); }/* Immediate value is 6 bits starting at bit #16 */
@@ -81,15 +81,14 @@ static inline void __runtime_fixup_shift(void *where, unsigned long val)u32 insn =le32_to_cpu (*p); insn &=0xffc0ffff ; insn |= (val &63 ) <<16 ; -aarch64_insn_patch_text_nosync (p, insn); +aarch64_insn_patch_text_nosync (where, insn); }/* Immediate value is 6 bits starting at bit #16 */ static inline void __runtime_fixup_mask (void *where,unsigned long val) { -__le32 *p =lm_alias (where); -__runtime_fixup_16 (p, val); -__runtime_fixup_16 (p+1 , val >>16 ); +__runtime_fixup_16 (where, val); +__runtime_fixup_16 (where +4 , val >>16 ); } static inline void runtime_const_fixup(void (*fn)(void *, unsigned long),
---
I'll do some more sanity checks and address the rest of the comments before posting out v4 soon after the merge window. Thank you again for your feedback. Much appreciated.
Signature · 2 lines
Thanks and Regards, Prateek
Catalin Marinas
maintainerRe: [PATCH v3 2/7] arm64/runtime-const: Use aarch64_insn_patch_text_nosync() for patching
Quoted 7 lines · K Prateek Nayak
Quoted 6 lines · Catalin Marinas
https://sashiko.dev/#/patchset/[email protected]
In short,
aarch64_insn_patch_text_nosync()does not expect a linear map address but rather a kernel text one (or vmalloc/modules). The other valid point is on aliasing I-caches.
Quoted 6 lines
Quoted 2 lines
I think dropping the
lm_alias()and just use 'where' directly would do but I haven't tried.Ack! I completely missed that subtlety of passing "where" to
caches_clean_inval_pou(). I'm still surprised that it didn't blow up in my testing.
For the first part (passing a linear map address), I suspect we get away with this in vmalloc_to_page() as it just walks the page tables and VIRTUAL_BUG_ON() is a no-op with defconfig.
For the I-cache aliasing, you may not have the right hardware but even if you did, it's harder to hit.
Quoted 11 lines
Anyhow, following diff, on top of the full series builds and tests fine and has been blessed by review-prompts:
arch/arm64/include/asm/runtime-const.h +0
-0@@ -57,21 +57,21 @@ }while (0 )/* 16-bit immediate for wide move (movz and movk) in bits 5..20 */
arch/arm64/include/asm/runtime-const.h line 67
Quoted 10 lines
arch/arm64/include/asm/runtime-const.h +3
-2-static inline void __runtime_fixup_16 (__le32 *p,unsigned int val) +static inline void __runtime_fixup_16 (void *where,unsigned int val) { +__le32 *p =lm_alias (where);u32 insn =le32_to_cpu (*p); insn &=0xffe0001f ; insn |= (val &0xffff ) <<5 ; -aarch64_insn_patch_text_nosync (p, insn); +aarch64_insn_patch_text_nosync (where, insn); }
You might as well keep the __le32 *p argument and just dereference it directly, no need for lm_alias(). The kernel text is readable and you avoid having to change all the other functions.
arch/arm64/include/asm/runtime-const.h line 75
Quoted 12 lines
arch/arm64/include/asm/runtime-const.h +4
-5static inline void __runtime_fixup_ptr (void *where,unsigned long val) { -__le32 *p =lm_alias (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_16 (where, val); +__runtime_fixup_16 (where +4 , val >>16 ); +__runtime_fixup_16 (where +8 , val >>32 ); +__runtime_fixup_16 (where +12 , val >>48 ); }
And here change the argument to '__le32 *p' (and in other places where you changed p to where + 4 etc.).
Signature · 1 line
Catalin
PATCH v3 3/7 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. GCC generates a:
movz w1, #lo16, lsl #0 // w1 = bits [15:0] movk w1, #hi16, lsl #16 // w1 = full 32-bit value and w0, w0, w1 // w0 = w0 & w1
pattern to tackle arbitrary 32-bit masks and the same was also suggested by Claude which is implemented here. The (__mask & value) operation is intentiaonally placed outside of asm block to allow compilers to further optimize it if possible.
__runtime_fixup_ptr() already patches a "movz, + movk lsl #16" sequence which has been reused to patch the same sequence for __runtime_fixup_mask().
---
Changelog v2..v3:
o Reordered this to come after the text patching fixes for ARM64. (David)
o Moved the "&" operation outside the inline asm block to allow for compilers to further optimize it if possible. (David)
--- arch/arm64/include/asm/runtime-const.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
arch/arm64/include/asm/runtime-const.h +19 -0
@@ -36,6 +36,17 @@ :"r" (0u+(val))); \ __ret; }) +#define runtime_const_mask_32 (val, sym) ({ \ + unsigned long __mask; \ + asm_inline("1:\t" \ + "movz %w0, #0xcdef\n\t" \ + "movk %w0, #0x89ab, lsl #16\n\t" \ + ".pushsection runtime_mask_" #sym ",\"a\"\n\t" \ + ".long 1b - .\n\t" \ + ".popsection" \ + :"=r" (__mask)); \ + (__mask & val); }) +#define runtime_const_init (type, sym) do { \ extern s32 __start_runtime_##type##_##sym[]; \ extern s32 __stop_runtime_##type##_##sym[]; \
@@ -73,6 +84,14 @@ static inline void __runtime_fixup_shift(void *where, unsigned long val)aarch64_insn_patch_text_nosync (p, insn); } +/* Immediate value is 6 bits starting at bit #16 */ +static inline void __runtime_fixup_mask (void *where,unsigned long val) +{ +__le32 *p =lm_alias (where); +__runtime_fixup_16 (p, val); +__runtime_fixup_16 (p+1 , val >>16 ); +} +static inline void runtime_const_fixup (void (*fn)(void *,unsigned long ),unsigned long val,s32 *start,s32 *end) {
Signature · 1 line
2.34.1
PATCH v3 4/7 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. GCC generates a:
lui a0, 0x12346 # upper; +0x800 then >>12 for correct rounding addi a0, a0, 0x678 # lower 12 bits and a1, a1, a0 # a1 = a1 & a0
pattern to tackle arbitrary 32-bit masks and the same was also suggested by Claude which is implemented here. The (__mask & val) operation is intentionally placed outside of asm block to allow compilers to further optimize it if possible.
__runtime_fixup_ptr() already patches a "lui + addi" sequence which has been reused to patch the same sequence for __runtime_fixup_mask().
---
Changelog v2..v3:
o Moved the "&" operation outside the inline asm block to allow for compilers to further optimize it if possible. (Based on David's comment on ARM64 bits).
--- arch/riscv/include/asm/runtime-const.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
arch/riscv/include/asm/runtime-const.h +22 -0
@@ -153,6 +153,22 @@ __ret; \ }) +#define runtime_const_mask_32 (val, sym) \ +({ \ + u32 __mask; \ + asm_inline(".option push\n\t" \ + ".option norvc\n\t" \ + "1:\t" \ + "lui %[__mask],0x89abd\n\t" \ + "addi %[__mask],%[__mask],-0x211\n\t" \ + ".option pop\n\t" \ + ".pushsection runtime_mask_" #sym ",\"a\"\n\t" \ + ".long 1b - .\n\t" \ + ".popsection" \ + : [__mask] "=r" (__mask)); \ + (__mask & val); \ +}) +#define runtime_const_init (type, sym) do { \ extern s32 __start_runtime_##type##_##sym[]; \ extern s32 __stop_runtime_##type##_##sym[]; \
@@ -256,6 +272,12 @@ 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) +{ +__runtime_fixup_32 (where, where +4 , val); +__runtime_fixup_caches (where,2 ); +} +static inline void runtime_const_fixup (void (*fn)(void *,unsigned long ),unsigned long val,s32 *start,s32 *end) {
Signature · 1 line
2.34.1
Re: [PATCH v3 4/7] riscv/runtime-const: Introduce runtime_const_mask_32()
Quoted 38 lines · K Prateek Nayak
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:lui a0, 0x12346 # upper; +0x800 then >>12 for correct rounding addi a0, a0, 0x678 # lower 12 bits and a1, a1, a0 # a1 = a1 & a0pattern to tackle arbitrary 32-bit masks and the same was also suggested by Claude which is implemented here. The (
__mask& val) operation is intentionally placed outside of asm block to allow compilers to further optimize it if possible.
__runtime_fixup_ptr()already patches a "lui + addi" sequence which has been reused to patch the same sequence for__runtime_fixup_mask().Assisted-by Claude:claude-sonnet-4-5Signed-off-by K Prateek Nayak <[email protected]>---
Changelog v2..v3:o Moved the "&" operation outside the inline asm block to allow for compilers to further optimize it if possible. (Based on David's comment on ARM64 bits).
--- arch/riscv/include/asm/runtime-const.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)arch/riscv/include/asm/runtime-const.h +0
-0@@ -153,6 +153,22 @@ __ret; \
arch/riscv/include/asm/runtime-const.h line 163
Quoted 10 lines
arch/riscv/include/asm/runtime-const.h +8
-0}) +#define runtime_const_mask_32 (val, sym) \ +({ \ + u32 __mask; \ + asm_inline(".option push\n\t" \ + ".option norvc\n\t" \ + "1:\t" \ + "lui %[__mask],0x89abd\n\t" \ + "addi %[__mask],%[__mask],-0x211\n\t" \
Ref include/uapi/linux/reboot.h:
#define LINUX_REBOOT_CMD_CAD_ON 0x89ABCDEF
#define RUNTIME_MAGIC 0x89ABCDEF
"lui %[__mask], %%hi(RUNTIME_MAGIC)\n\t" "addi %[__mask], %[__mask], %%lo(RUNTIME_MAGIC)\n\t"
Quoted 28 lines
arch/riscv/include/asm/runtime-const.h +14
-1+".option pop\n\t" \ + ".pushsection runtime_mask_" #sym ",\"a\"\n\t" \ + ".long 1b - .\n\t" \ + ".popsection" \ + : [__mask] "=r" (__mask)); \ + (__mask & val); \ +}) +#define runtime_const_init (type, sym) do { \ extern s32 __start_runtime_##type##_##sym[]; \ extern s32 __stop_runtime_##type##_##sym[]; \@@ -256,6 +272,12 @@ 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) +{ +__runtime_fixup_32 (where, where +4 , val); +__runtime_fixup_caches (where,2 ); +} +static inline void runtime_const_fixup (void (*fn)(void *,unsigned long ),unsigned long val,s32 *start,s32 *end) { --2.34.1
Signature · 2 lines
Best Regards Guo Ren
Re: [PATCH v3 4/7] riscv/runtime-const: Introduce runtime_const_mask_32()
Hello Guo,
Quoted 19 lines · Guo Ren
Quoted 6 lines
arch/riscv/include/asm/runtime-const.h +0
-0@@ -153,6 +153,22 @@ __ret; \arch/riscv/include/asm/runtime-const.h line 163
Quoted 10 lines
arch/riscv/include/asm/runtime-const.h +8
-0}) +#define runtime_const_mask_32 (val, sym) \ +({ \ + u32 __mask; \ + asm_inline(".option push\n\t" \ + ".option norvc\n\t" \ + "1:\t" \ + "lui %[__mask],0x89abd\n\t" \ + "addi %[__mask],%[__mask],-0x211\n\t" \Ref
include/uapi/linux/reboot.h:
#defineLINUX_REBOOT_CMD_CAD_ON0x89ABCDEF
Quoted 4 lines
#define
RUNTIME_MAGIC0x89ABCDEF"lui %[__mask], %%hi(RUNTIME_MAGIC)\n\t" "addi %[__mask], %[__mask], %%lo(RUNTIME_MAGIC)\n\t"
Ack! I'll clean it up in the next version while also fixing the stuff that Sashiko reported.
Thanks a ton for taking a look at the series.
Quoted 9 lines
Quoted 9 lines
arch/riscv/include/asm/runtime-const.h +7
-0+".option pop\n\t" \ + ".pushsection runtime_mask_" #sym ",\"a\"\n\t" \ + ".long 1b - .\n\t" \ + ".popsection" \ + : [__mask] "=r" (__mask)); \ + (__mask & val); \ +})
Signature · 2 lines
Thanks and Regards, Prateek
PATCH v3 5/7 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 v2..v3:
o Collected Ack from Heiko after folding in the suggested diff. (Thanks a ton!)
--- 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
PATCH v3 6/7 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 v2..v3:
o No changes.
--- 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
PATCH v3 7/7 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.
No functional chages intended.
[ prateek: Dynamically allocate __futex_queues, mark the global data
__ro_after_init since they are constified after futex_init(). ]
---
Changelog v2..v3:
o No changes.
--- include/asm-generic/vmlinux.lds.h | 5 +++- kernel/futex/core.c | 42 +++++++++++++++++-------------- 2 files changed, 27 insertions(+), 20 deletions(-)
include/asm-generic/vmlinux.lds.h +4 -1
@@ -975,7 +975,10 @@ RUNTIME_CONST(shift, d_hash_shift) \ RUNTIME_CONST(ptr, dentry_hashtable) \ RUNTIME_CONST(ptr, __dentry_cache) \ -RUNTIME_CONST (ptr, __names_cache) + RUNTIME_CONST(ptr, __names_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 +23 -19
87 lines not on this page · raw message
Signature · 1 line
2.34.1