mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "H. Peter Anvin" <hpa@zytor.com>
To: Uros Bizjak <ubizjak@gmail.com>,
	x86@kernel.org, linux-kernel@vger.kernel.org
Cc: Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@kernel.org>, Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>
Subject: Re: [PATCH 2/2] x86/bitops: Fix false output register dependency of TZCNT insn
Date: Tue, 25 Mar 2025 11:29:47 -0700	[thread overview]
Message-ID: <75C56CF0-A414-4AB8-BCD9-29CE5A344166@zytor.com> (raw)
In-Reply-To: <20250325175215.330659-2-ubizjak@gmail.com>

On March 25, 2025 10:52:02 AM PDT, Uros Bizjak <ubizjak@gmail.com> wrote:
>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
>
>Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
>Cc: Thomas Gleixner <tglx@linutronix.de>
>Cc: Ingo Molnar <mingo@kernel.org>
>Cc: Borislav Petkov <bp@alien8.de>
>Cc: Dave Hansen <dave.hansen@linux.intel.com>
>Cc: "H. Peter Anvin" <hpa@zytor.com>
>---
> arch/x86/include/asm/bitops.h | 10 ++++++----
> 1 file changed, 6 insertions(+), 4 deletions(-)
>
>diff --git a/arch/x86/include/asm/bitops.h b/arch/x86/include/asm/bitops.h
>index bbaf75ea6703..7e3d1cc97c5a 100644
>--- a/arch/x86/include/asm/bitops.h
>+++ b/arch/x86/include/asm/bitops.h
>@@ -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;
> }

Is xor better there than a mov (making it a copy of the source)? It might be more frequently fusable.

  parent reply	other threads:[~2025-03-25 18:30 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-25 17:52 [PATCH 1/2] x86/bitops: Use TZCNT mnemonic in <asm/bitops.h> Uros Bizjak
2025-03-25 17:52 ` [PATCH 2/2] x86/bitops: Fix false output register dependency of TZCNT insn Uros Bizjak
2025-03-25 17:59   ` Borislav Petkov
2025-03-25 18:29   ` H. Peter Anvin [this message]
2025-03-25 21:43   ` Ingo Molnar
2025-03-26  8:46     ` Uros Bizjak
2025-03-28 22:27       ` Ingo Molnar
2025-03-29  8:48         ` Uros Bizjak
2025-03-25 21:48 ` [tip: x86/asm] x86/bitops: Use TZCNT mnemonic in <asm/bitops.h> tip-bot2 for Uros Bizjak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=75C56CF0-A414-4AB8-BCD9-29CE5A344166@zytor.com \
    --to=hpa@zytor.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=ubizjak@gmail.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®