x86/bitops: Use TZCNT mnemonic in <asm/bitops.h>
Subsystems (2)
PATCH 1/2 x86/bitops: Use TZCNT mnemonic in <asm/bitops.h>
Current minimum required version of binutils is 2.25, which supports TZCNT instruction mnemonic.
Replace "REP; BSF" in variable__{ffs,ffz}() function with this proper mnemonic.
No functional change intended.
--- arch/x86/include/asm/bitops.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/bitops.h +2 -2
@@ -248,7 +248,7 @@ arch_test_bit_acquire(unsigned long nr, const volatile unsigned long *addr)static __always_inline unsigned long variable__ffs (unsigned long word) { - asm("rep; bsf %1,%0" + asm("tzcnt %1,%0" :"=r" (word) : ASM_INPUT_RM (word));return word;
@@ -267,7 +267,7 @@ static __always_inline unsigned long variable__ffs(unsigned long word)static __always_inline unsigned long variable_ffz (unsigned long word) { - asm("rep; bsf %1,%0" + asm("tzcnt %1,%0" :"=r" (word) :"r" (~word));return word;
Signature · 1 line
2.42.0
PATCH 2/2 x86/bitops: Fix false output register dependency of TZCNT insn
On Haswell and later Intel processors, the TZCNT instruction appears to have a false dependency on the destination register. Even though the instruction only writes to it, the instruction will wait until destination is ready before executing. This false dependency was fixed for Skylake (and later) processors.
Fix false dependency by clearing the destination register first.
The x86_64 defconfig object size increases by 4215 bytes:
text data bss dec hex filename 27342396 4642999 814852 32800247 1f47df7 vmlinux-old.o 27346611 4643015 814852 32804478 1f48e7e vmlinux-new.o
--- arch/x86/include/asm/bitops.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
arch/x86/include/asm/bitops.h +6 -4
@@ -248,8 +248,9 @@ arch_test_bit_acquire(unsigned long nr, const volatile unsigned long *addr)static __always_inline unsigned long variable__ffs (unsigned long word) { - asm("tzcnt %1,%0" - :"=r" (word) + asm("xor %k0,%k0\n\t" /* avoid false dependency on dest register */ +"tzcnt %1,%0" + :"=&r" (word) : ASM_INPUT_RM (word));return word; }
@@ -267,8 +268,9 @@ static __always_inline unsigned long variable__ffs(unsigned long word)static __always_inline unsigned long variable_ffz (unsigned long word) { - asm("tzcnt %1,%0" - :"=r" (word) + asm("xor %k0,%k0\n\t" /* avoid false dependency on dest register */ +"tzcnt %1,%0" + :"=&r" (word) :"r" (~word));return word; }
Signature · 1 line
2.42.0
Borislav Petkov
maintainerRe: [PATCH 2/2] x86/bitops: Fix false output register dependency of TZCNT insn
Quoted 7 lines · Uros Bizjak
On Haswell and later Intel processors, the TZCNT instruction appears to have a false dependency on the destination register. Even though the instruction only writes to it, the instruction will wait until destination is ready before executing. This false dependency was fixed for Skylake (and later) processors.
Fix false dependency by clearing the destination register first.
Same questions as about the POPCNT patch.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteH. Peter Anvin
reviewerRe: [PATCH 2/2] x86/bitops: Fix false output register dependency of TZCNT insn
In reply to Uros BizjakQuoted 42 lines · Uros Bizjak
On Haswell and later Intel processors, the TZCNT instruction appears to have a false dependency on the destination register. Even though the instruction only writes to it, the instruction will wait until destination is ready before executing. This false dependency was fixed for Skylake (and later) processors.
Fix false dependency by clearing the destination register first.
The
x86_64defconfig object size increases by 4215 bytes:text data bss dec hex filename 27342396 4642999 814852 32800247 1f47df7 vmlinux-old.o 27346611 4643015 814852 32804478 1f48e7e vmlinux-new.oSigned-off-by Uros Bizjak <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc Borislav Petkov <[email protected]>Cc Dave Hansen <[email protected]>Cc "H. Peter Anvin" <[email protected]>---
arch/x86/include/asm/bitops.h| 10 ++++++----1 file changed, 6 insertions(+), 4 deletions(-)arch/x86/include/asm/bitops.h +3
-2@@ -248,8 +248,9 @@ arch_test_bit_acquire(unsigned long nr, const volatile unsigned long *addr)static __always_inline unsigned long variable__ffs (unsigned long word) { - asm("tzcnt %1,%0" - :"=r" (word) + asm("xor %k0,%k0\n\t" /* avoid false dependency on dest register */ +"tzcnt %1,%0" + :"=&r" (word) : ASM_INPUT_RM (word));return word; }@@ -267,8 +268,9 @@ static __always_inline unsigned long variable__ffs(unsigned long word)
arch/x86/include/asm/bitops.h static __always_inline unsigned long variable__ffs(unsigned long word) · line 276
Quoted 10 lines
arch/x86/include/asm/bitops.h +3
-2static __always_inline unsigned long variable_ffz (unsigned long word) { - asm("tzcnt %1,%0" - :"=r" (word) + asm("xor %k0,%k0\n\t" /* avoid false dependency on dest register */ +"tzcnt %1,%0" + :"=&r" (word) :"r" (~word));return word; }
Is xor better there than a mov (making it a copy of the source)? It might be more frequently fusable.
Re: [PATCH 2/2] x86/bitops: Fix false output register dependency of TZCNT insn
In reply to Uros BizjakQuoted 6 lines · Uros Bizjak
On Haswell and later Intel processors, the TZCNT instruction appears to have a false dependency on the destination register. Even though the instruction only writes to it, the instruction will wait until destination is ready before executing. This false dependency was fixed for Skylake (and later) processors.
Quoted 7 lines
Fix false dependency by clearing the destination register first.
The
x86_64defconfig object size increases by 4215 bytes:text data bss dec hex filename 27342396 4642999 814852 32800247 1f47df7 vmlinux-old.o 27346611 4643015 814852 32804478 1f48e7e vmlinux-new.o
Yeah, so Skylake was released in 2015, about a decade ago.
So we'd be making the kernel larger for an unquantified micro-optimization for CPUs that almost nobody uses anymore. That's a bad trade-off.
Thanks,
Ingo
Re: [PATCH 2/2] x86/bitops: Fix false output register dependency of TZCNT insn
Quoted 18 lines · Ingo Molnar
Quoted 6 lines · Uros Bizjak
On Haswell and later Intel processors, the TZCNT instruction appears to have a false dependency on the destination register. Even though the instruction only writes to it, the instruction will wait until destination is ready before executing. This false dependency was fixed for Skylake (and later) processors.
Quoted 7 lines
Fix false dependency by clearing the destination register first.
The
x86_64defconfig object size increases by 4215 bytes:text data bss dec hex filename 27342396 4642999 814852 32800247 1f47df7 vmlinux-old.o 27346611 4643015 814852 32804478 1f48e7e vmlinux-new.o
Quoted 5 lines
Yeah, so Skylake was released in 2015, about a decade ago.
So we'd be making the kernel larger for an unquantified micro-optimization for CPUs that almost nobody uses anymore. That's a bad trade-off.
Yes, 4.2k seems a bit excessive. OTOH, I'd not say that the issue is a micro-optimization, it is bordering on the hardware bug.
An alternative (also answering Peter's question) would be to make instruction dependent only on the input (so, matching input and output operand, making insn destructive), requiring MOV if the input operand is to be reused. A quick test (x86_64 defconfig) with the attached patch shows much less impact:
text data bss dec hex filename
27564120 4642423 814852 33021395 1f7ddd3 vmlinux-old.o 27564679 4642423 814852 33021954 1f7e002 vmlinux-new.o
The increase is now 559 bytes, which IMHO is an acceptable trade-off. Please also note that:
asm("tzcnt %1,%0"
: "=r" (word)
: "r" (~word));
return word;emits NOT insn that is also destructive and already requires copying of the input operand if the operand is to be reused.
Thanks,
Uros.
arch/x86/include/asm/bitops.h +2 -5
@@ -250,7 +250,7 @@ static __always_inline unsigned long variable__ffs(unsigned long word) { asm("tzcnt %1,%0" :"=r" (word) - : ASM_INPUT_RM (word)); + :"0" (word));/* avoid false output dependency */ return word; }
@@ -267,10 +267,7 @@ static __always_inline unsigned long variable__ffs(unsigned long word)static __always_inline unsigned long variable_ffz (unsigned long word) { - asm("tzcnt %1,%0" - :"=r" (word) - :"r" (~word)); -return word; +return variable__ffs (~word); }/**
Re: [PATCH 2/2] x86/bitops: Fix false output register dependency of TZCNT insn
Quoted 19 lines · Uros Bizjak
Quoted 18 lines · Ingo Molnar
Quoted 13 lines · Uros Bizjak
On Haswell and later Intel processors, the TZCNT instruction appears to have a false dependency on the destination register. Even though the instruction only writes to it, the instruction will wait until destination is ready before executing. This false dependency was fixed for Skylake (and later) processors.
Fix false dependency by clearing the destination register first.
The
x86_64defconfig object size increases by 4215 bytes:text data bss dec hex filename 27342396 4642999 814852 32800247 1f47df7 vmlinux-old.o 27346611 4643015 814852 32804478 1f48e7e vmlinux-new.o
Quoted 8 lines
Quoted 5 lines
Yeah, so Skylake was released in 2015, about a decade ago.
So we'd be making the kernel larger for an unquantified micro-optimization for CPUs that almost nobody uses anymore. That's a bad trade-off.
Yes, 4.2k seems a bit excessive. OTOH, I'd not say that the issue is a micro-optimization, it is bordering on the hardware bug.
Has this been quantified, and do we really care about the micro-performance of ~10-year old CPUs, especially at the expense of modern CPUs?
Thanks,
Ingo
Re: [PATCH 2/2] x86/bitops: Fix false output register dependency of TZCNT insn
Quoted 29 lines · Ingo Molnar
Quoted 25 lines · Uros Bizjak
Quoted 23 lines · Ingo Molnar
Quoted 6 lines · Uros Bizjak
On Haswell and later Intel processors, the TZCNT instruction appears to have a false dependency on the destination register. Even though the instruction only writes to it, the instruction will wait until destination is ready before executing. This false dependency was fixed for Skylake (and later) processors.
Quoted 7 lines
Fix false dependency by clearing the destination register first.
The
x86_64defconfig object size increases by 4215 bytes:text data bss dec hex filename 27342396 4642999 814852 32800247 1f47df7 vmlinux-old.o 27346611 4643015 814852 32804478 1f48e7e vmlinux-new.oYeah, so Skylake was released in 2015, about a decade ago.
So we'd be making the kernel larger for an unquantified micro-optimization for CPUs that almost nobody uses anymore. That's a bad trade-off.
Quoted 6 lines
Quoted 2 lines
Yes, 4.2k seems a bit excessive. OTOH, I'd not say that the issue is a micro-optimization, it is bordering on the hardware bug.
Has this been quantified, and do we really care about the micro-performance of ~10-year old CPUs, especially at the expense of modern CPUs?
No, although the change would be a one liner now. Without specially crafted benchmark loops the impact is not noticeable and typical kernel usage of these instructions is not that sensitive on destination.
Thanks,
Uros.
The following commit has been merged into the x86/asm branch of tip:
The following commit has been merged into the x86/asm branch of tip:
Commit-ID: 0717b1392dc7e3f350e5a5d25ea794aa92210684 Gitweb: https://git.kernel.org/tip/0717b1392dc7e3f350e5a5d25ea794aa92210684 Author: Uros Bizjak <[email protected]> AuthorDate: Tue, 25 Mar 2025 18:52:01 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Tue, 25 Mar 2025 22:38:29 +01:00
x86/bitops: Use TZCNT mnemonic in <asm/bitops.h>
Current minimum required version of binutils is 2.25, which supports TZCNT instruction mnemonic.
Replace "REP; BSF" in variable__{ffs,ffz}() function with this proper mnemonic.
No functional change intended.
--- arch/x86/include/asm/bitops.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/bitops.h +2 -2
@@ -248,7 +248,7 @@ arch_test_bit_acquire(unsigned long nr, const volatile unsigned long *addr)static __always_inline unsigned long variable__ffs (unsigned long word) { - asm("rep; bsf %1,%0" + asm("tzcnt %1,%0" :"=r" (word) : ASM_INPUT_RM (word));return word;
@@ -267,7 +267,7 @@ static __always_inline unsigned long variable__ffs(unsigned long word)static __always_inline unsigned long variable_ffz (unsigned long word) { - asm("rep; bsf %1,%0" + asm("tzcnt %1,%0" :"=r" (word) :"r" (~word));return word;