linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] x86/vdso: Fix output operand size of RDPID insn
@ 2025-06-16  9:52 Uros Bizjak
  2025-06-16  9:52 ` [PATCH 2/2] x86/vdso: Use RDPID mnemonic in vdso_red_cpunode() Uros Bizjak
  2025-08-27  6:45 ` [tip: x86/asm] x86/vdso: Fix output operand size of RDPID tip-bot2 for Uros Bizjak
  0 siblings, 2 replies; 6+ messages in thread
From: Uros Bizjak @ 2025-06-16  9:52 UTC (permalink / raw)
  To: x86, linux-kernel
  Cc: Uros Bizjak, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, H. Peter Anvin

RDPID instruction outputs to a word-sized register (64-bit on
x86_64 and 32-bit on x86_32). Use unsigned long variable to use
the correct size.

LSL outputs to 32-bit register, use %k operand prefix
to always print 32-bit name of the register.

Fixes: ffebbaedc861 ("x86/vdso: Introduce helper functions for CPU and node number")
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/segment.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/segment.h b/arch/x86/include/asm/segment.h
index 77d8f49b92bd..93fcf0daba01 100644
--- a/arch/x86/include/asm/segment.h
+++ b/arch/x86/include/asm/segment.h
@@ -244,7 +244,7 @@ static inline unsigned long vdso_encode_cpunode(int cpu, unsigned long node)
 
 static inline void vdso_read_cpunode(unsigned *cpu, unsigned *node)
 {
-	unsigned int p;
+	unsigned long p;
 
 	/*
 	 * Load CPU and node number from the GDT.  LSL is faster than RDTSCP
@@ -254,7 +254,7 @@ static inline void vdso_read_cpunode(unsigned *cpu, unsigned *node)
 	 *
 	 * If RDPID is available, use it.
 	 */
-	alternative_io ("lsl %[seg],%[p]",
+	alternative_io ("lsl %[seg],%k[p]",
 			".byte 0xf3,0x0f,0xc7,0xf8", /* RDPID %eax/rax */
 			X86_FEATURE_RDPID,
 			[p] "=a" (p), [seg] "r" (__CPUNODE_SEG));
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/2] x86/vdso: Use RDPID mnemonic in vdso_red_cpunode()
  2025-06-16  9:52 [PATCH 1/2] x86/vdso: Fix output operand size of RDPID insn Uros Bizjak
@ 2025-06-16  9:52 ` Uros Bizjak
  2025-08-27  6:45 ` [tip: x86/asm] x86/vdso: Fix output operand size of RDPID tip-bot2 for Uros Bizjak
  1 sibling, 0 replies; 6+ messages in thread
From: Uros Bizjak @ 2025-06-16  9:52 UTC (permalink / raw)
  To: x86, linux-kernel
  Cc: Uros Bizjak, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, H. Peter Anvin

Current minimum required version of binutils is 2.30,
which supports RDPID instruction mnemonic.

Replace the byte-wise specification of RDPID with
this proper mnemonic.

No functional change intended.

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/segment.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/segment.h b/arch/x86/include/asm/segment.h
index 93fcf0daba01..f59ae7186940 100644
--- a/arch/x86/include/asm/segment.h
+++ b/arch/x86/include/asm/segment.h
@@ -255,9 +255,9 @@ static inline void vdso_read_cpunode(unsigned *cpu, unsigned *node)
 	 * If RDPID is available, use it.
 	 */
 	alternative_io ("lsl %[seg],%k[p]",
-			".byte 0xf3,0x0f,0xc7,0xf8", /* RDPID %eax/rax */
+			"rdpid %[p]",
 			X86_FEATURE_RDPID,
-			[p] "=a" (p), [seg] "r" (__CPUNODE_SEG));
+			[p] "=r" (p), [seg] "r" (__CPUNODE_SEG));
 
 	if (cpu)
 		*cpu = (p & VDSO_CPUNODE_MASK);
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [tip: x86/asm] x86/vdso: Fix output operand size of RDPID
  2025-06-16  9:52 [PATCH 1/2] x86/vdso: Fix output operand size of RDPID insn Uros Bizjak
  2025-06-16  9:52 ` [PATCH 2/2] x86/vdso: Use RDPID mnemonic in vdso_red_cpunode() Uros Bizjak
@ 2025-08-27  6:45 ` tip-bot2 for Uros Bizjak
  2025-08-27  7:53   ` Uros Bizjak
  1 sibling, 1 reply; 6+ messages in thread
From: tip-bot2 for Uros Bizjak @ 2025-08-27  6:45 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: Uros Bizjak, Borislav Petkov (AMD), x86, linux-kernel

The following commit has been merged into the x86/asm branch of tip:

Commit-ID:     ac9c408ed19d535289ca59200dd6a44a6a2d6036
Gitweb:        https://git.kernel.org/tip/ac9c408ed19d535289ca59200dd6a44a6a2d6036
Author:        Uros Bizjak <ubizjak@gmail.com>
AuthorDate:    Mon, 16 Jun 2025 11:52:57 +02:00
Committer:     Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Tue, 26 Aug 2025 19:33:19 +02:00

x86/vdso: Fix output operand size of RDPID

RDPID instruction outputs to a word-sized register (64-bit on x86_64 and
32-bit on x86_32). Use an unsigned long variable to store the correct size.

LSL outputs to 32-bit register, use %k operand prefix to always print the
32-bit name of the register.

Use RDPID insn mnemonic while at it as the minimum binutils version of
2.30 supports it.

  [ bp: Merge two patches touching the same function into a single one. ]

Fixes: ffebbaedc861 ("x86/vdso: Introduce helper functions for CPU and node number")
Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Link: https://lore.kernel.org/20250616095315.230620-1-ubizjak@gmail.com
---
 arch/x86/include/asm/segment.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/arch/x86/include/asm/segment.h b/arch/x86/include/asm/segment.h
index 77d8f49..f59ae71 100644
--- a/arch/x86/include/asm/segment.h
+++ b/arch/x86/include/asm/segment.h
@@ -244,7 +244,7 @@ static inline unsigned long vdso_encode_cpunode(int cpu, unsigned long node)
 
 static inline void vdso_read_cpunode(unsigned *cpu, unsigned *node)
 {
-	unsigned int p;
+	unsigned long p;
 
 	/*
 	 * Load CPU and node number from the GDT.  LSL is faster than RDTSCP
@@ -254,10 +254,10 @@ static inline void vdso_read_cpunode(unsigned *cpu, unsigned *node)
 	 *
 	 * If RDPID is available, use it.
 	 */
-	alternative_io ("lsl %[seg],%[p]",
-			".byte 0xf3,0x0f,0xc7,0xf8", /* RDPID %eax/rax */
+	alternative_io ("lsl %[seg],%k[p]",
+			"rdpid %[p]",
 			X86_FEATURE_RDPID,
-			[p] "=a" (p), [seg] "r" (__CPUNODE_SEG));
+			[p] "=r" (p), [seg] "r" (__CPUNODE_SEG));
 
 	if (cpu)
 		*cpu = (p & VDSO_CPUNODE_MASK);

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [tip: x86/asm] x86/vdso: Fix output operand size of RDPID
  2025-08-27  6:45 ` [tip: x86/asm] x86/vdso: Fix output operand size of RDPID tip-bot2 for Uros Bizjak
@ 2025-08-27  7:53   ` Uros Bizjak
  2025-08-27  8:19     ` Borislav Petkov
  0 siblings, 1 reply; 6+ messages in thread
From: Uros Bizjak @ 2025-08-27  7:53 UTC (permalink / raw)
  To: linux-kernel; +Cc: linux-tip-commits, Borislav Petkov (AMD), x86

On Wed, Aug 27, 2025 at 8:45 AM tip-bot2 for Uros Bizjak
<tip-bot2@linutronix.de> wrote:
>
> The following commit has been merged into the x86/asm branch of tip:
>
> Commit-ID:     ac9c408ed19d535289ca59200dd6a44a6a2d6036
> Gitweb:        https://git.kernel.org/tip/ac9c408ed19d535289ca59200dd6a44a6a2d6036
> Author:        Uros Bizjak <ubizjak@gmail.com>
> AuthorDate:    Mon, 16 Jun 2025 11:52:57 +02:00
> Committer:     Borislav Petkov (AMD) <bp@alien8.de>
> CommitterDate: Tue, 26 Aug 2025 19:33:19 +02:00
>
> x86/vdso: Fix output operand size of RDPID
>
> RDPID instruction outputs to a word-sized register (64-bit on x86_64 and
> 32-bit on x86_32). Use an unsigned long variable to store the correct size.
>
> LSL outputs to 32-bit register, use %k operand prefix to always print the
> 32-bit name of the register.
>
> Use RDPID insn mnemonic while at it as the minimum binutils version of
> 2.30 supports it.
>
>   [ bp: Merge two patches touching the same function into a single one. ]

The 1/2 patch is intended to be backportable (only this part carries
"Fixes" designation). The 2/2 patch introduces RDPID mnemonic that
requires binutils-2.30, so the second part is not backportable to
branches that still require binutils-2.25.

Uros.

>
> Fixes: ffebbaedc861 ("x86/vdso: Introduce helper functions for CPU and node number")
> Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
> Link: https://lore.kernel.org/20250616095315.230620-1-ubizjak@gmail.com
> ---
>  arch/x86/include/asm/segment.h | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/arch/x86/include/asm/segment.h b/arch/x86/include/asm/segment.h
> index 77d8f49..f59ae71 100644
> --- a/arch/x86/include/asm/segment.h
> +++ b/arch/x86/include/asm/segment.h
> @@ -244,7 +244,7 @@ static inline unsigned long vdso_encode_cpunode(int cpu, unsigned long node)
>
>  static inline void vdso_read_cpunode(unsigned *cpu, unsigned *node)
>  {
> -       unsigned int p;
> +       unsigned long p;
>
>         /*
>          * Load CPU and node number from the GDT.  LSL is faster than RDTSCP
> @@ -254,10 +254,10 @@ static inline void vdso_read_cpunode(unsigned *cpu, unsigned *node)
>          *
>          * If RDPID is available, use it.
>          */
> -       alternative_io ("lsl %[seg],%[p]",
> -                       ".byte 0xf3,0x0f,0xc7,0xf8", /* RDPID %eax/rax */
> +       alternative_io ("lsl %[seg],%k[p]",
> +                       "rdpid %[p]",
>                         X86_FEATURE_RDPID,
> -                       [p] "=a" (p), [seg] "r" (__CPUNODE_SEG));
> +                       [p] "=r" (p), [seg] "r" (__CPUNODE_SEG));
>
>         if (cpu)
>                 *cpu = (p & VDSO_CPUNODE_MASK);

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [tip: x86/asm] x86/vdso: Fix output operand size of RDPID
  2025-08-27  7:53   ` Uros Bizjak
@ 2025-08-27  8:19     ` Borislav Petkov
  2025-08-27  8:28       ` Uros Bizjak
  0 siblings, 1 reply; 6+ messages in thread
From: Borislav Petkov @ 2025-08-27  8:19 UTC (permalink / raw)
  To: Uros Bizjak; +Cc: linux-kernel, linux-tip-commits, x86

On Wed, Aug 27, 2025 at 09:53:17AM +0200, Uros Bizjak wrote:
> The 1/2 patch is intended to be backportable

Is it fixing an actual, real life bug?

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [tip: x86/asm] x86/vdso: Fix output operand size of RDPID
  2025-08-27  8:19     ` Borislav Petkov
@ 2025-08-27  8:28       ` Uros Bizjak
  0 siblings, 0 replies; 6+ messages in thread
From: Uros Bizjak @ 2025-08-27  8:28 UTC (permalink / raw)
  To: Borislav Petkov; +Cc: linux-kernel, linux-tip-commits, x86

On Wed, Aug 27, 2025 at 10:19 AM Borislav Petkov <bp@alien8.de> wrote:
>
> On Wed, Aug 27, 2025 at 09:53:17AM +0200, Uros Bizjak wrote:
> > The 1/2 patch is intended to be backportable
>
> Is it fixing an actual, real life bug?

No, because ".byte 0xf3,0x0f,0xc7,0xf8" encodes "RDPID %rax" on 64-bit
targets and we are only interested in the lower 32-bits anyway.

So, the change only matters on the 64-bit target when mnemonic is used
(there is no RDPID %eax" instruction), but the patch should not be
backported for the reasons outlined in my previous message. OTOH, "LSL
%eax,%eax" zero-extends to the full 64-bit register width on 64-bit
targets even when 32-bit register form is used.

Thanks,
Uros.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-08-27  8:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-16  9:52 [PATCH 1/2] x86/vdso: Fix output operand size of RDPID insn Uros Bizjak
2025-06-16  9:52 ` [PATCH 2/2] x86/vdso: Use RDPID mnemonic in vdso_red_cpunode() Uros Bizjak
2025-08-27  6:45 ` [tip: x86/asm] x86/vdso: Fix output operand size of RDPID tip-bot2 for Uros Bizjak
2025-08-27  7:53   ` Uros Bizjak
2025-08-27  8:19     ` Borislav Petkov
2025-08-27  8:28       ` Uros Bizjak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).