public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: Uros Bizjak <ubizjak@gmail.com>
Cc: Brian Gerst <brgerst@gmail.com>, "H. Peter Anvin" <hpa@zytor.com>,
	linux-kernel@vger.kernel.org,
	tip-bot2 for Uros Bizjak <tip-bot2@linutronix.de>,
	linux-tip-commits@vger.kernel.org,
	"Borislav Petkov (AMD)" <bp@alien8.de>,
	Michael Kelley <mhklinux@outlook.com>,
	x86@kernel.org
Subject: Re: [tip: x86/cleanups] x86/segment: Use MOVL when reading segment registers
Date: Wed, 21 Jan 2026 18:55:40 +0000	[thread overview]
Message-ID: <20260121185540.020f7b72@pumpkin> (raw)
In-Reply-To: <CAFULd4b8hJpY-gAy4MPug1PjK4ME0M_jeBZ68XLjYuSERr7RKA@mail.gmail.com>

On Wed, 21 Jan 2026 17:16:53 +0100
Uros Bizjak <ubizjak@gmail.com> wrote:

> On Wed, Jan 21, 2026 at 5:06 PM David Laight
> <david.laight.linux@gmail.com> wrote:
> >
> > On Wed, 21 Jan 2026 06:49:16 -0500
> > Brian Gerst <brgerst@gmail.com> wrote:
> >  
> > > On Wed, Jan 21, 2026 at 2:29 AM H. Peter Anvin <hpa@zytor.com> wrote:  
> > > >
> > > > On January 20, 2026 4:08:01 AM PST, tip-bot2 for Uros Bizjak <tip-bot2@linutronix.de> wrote:  
> > > > >The following commit has been merged into the x86/cleanups branch of tip:
> > > > >
> > > > >Commit-ID:     53ed3d91a141f5c8b3bce45b0004fbbfefe77956
> > > > >Gitweb:        https://git.kernel.org/tip/53ed3d91a141f5c8b3bce45b0004fbbfefe77956
> > > > >Author:        Uros Bizjak <ubizjak@gmail.com>
> > > > >AuthorDate:    Mon, 05 Jan 2026 10:02:32 +01:00
> > > > >Committer:     Borislav Petkov (AMD) <bp@alien8.de>
> > > > >CommitterDate: Tue, 20 Jan 2026 12:34:58 +01:00
> > > > >
> > > > >x86/segment: Use MOVL when reading segment registers
> > > > >
> > > > >Use MOVL when reading segment registers to avoid 0x66 operand-size override
> > > > >insn prefix. The segment value is always 16-bit and gets zero-extended to the
> > > > >full 32-bit size.
> > > > >
> > > > >Example:
> > > > >
> > > > >  4e4:       66 8c c0                mov    %es,%ax
> > > > >  4e7:       66 89 83 80 0b 00 00    mov    %ax,0xb80(%rbx)
> > > > >
> > > > >  4e4:       8c c0                   mov    %es,%eax
> > > > >  4e6:       66 89 83 80 0b 00 00    mov    %ax,0xb80(%rbx)
> > > > >
> > > > >Also, use the %k0 modifier which generates the SImode (signed integer)
> > > > >register name for the target register.
> > > > >
> > > > >  [ bp: Extend and clarify commit message. ]
> > > > >
> > > > >Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
> > > > >Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
> > > > >Reviewed-by: H. Peter Anvin (Intel) <hpa@zytor.com>
> > > > >Tested-by: Michael Kelley <mhklinux@outlook.com>
> > > > >Link: https://patch.msgid.link/20260105090422.6243-1-ubizjak@gmail.com
> > > > >---
> > > > > arch/x86/include/asm/segment.h | 2 +-
> > > > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > > > >
> > > > >diff --git a/arch/x86/include/asm/segment.h b/arch/x86/include/asm/segment.h
> > > > >index f59ae71..9f5be2b 100644
> > > > >--- a/arch/x86/include/asm/segment.h
> > > > >+++ b/arch/x86/include/asm/segment.h
> > > > >@@ -348,7 +348,7 @@ static inline void __loadsegment_fs(unsigned short value)
> > > > >  * Save a segment register away:
> > > > >  */
> > > > > #define savesegment(seg, value)                               \
> > > > >-      asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
> > > > >+      asm("movl %%" #seg ",%k0" : "=r" (value) : : "memory")
> > > > >
> > > > > #endif /* !__ASSEMBLER__ */
> > > > > #endif /* __KERNEL__ */
> > > > >  
> > > >
> > > > Incidentally, why aren't we using =rm here? Segment moves support memory operands.  
> > >
> > > You would have to be really careful to only use short (16-bit)
> > > variables, because it will not zero-extend with a memory operand.
> > >  
> >
> > It would be much safer to have something that returned the value
> > of the segment register (zero extended to 32 bits).  
> 
> movl from %seg to 32-bit register (as proposed in the patch)
> zero-extends the value all the way to word size (64-bits on x86_64).
> The proposed solution also handles memory operands, so:
> 
> --cut here--
> unsigned int m;
> 
> void foo(void)
> {
>   asm("mov %%gs,%k0" : "=r"(m));
> }
> --cut here--
> 
> compiles to optimal code:
> 
> 0000000000000000 <foo>:
>   0:   8c e8                   mov    %gs,%eax
>   2:   89 05 00 00 00 00       mov    %eax,0x0(%rip)        # 8 <foo+0x8>
>                        4: R_X86_64_PC32        m-0x4
>   8:   c3                      ret
> 
> Uros.

As does this version:

unsigned int m;

#define get_seg(seg) ({ \
    unsigned int _seg_val; \
    asm("mov %%" #seg ",%k0" : "=r"(_seg_val)); \
    _seg_val; \
})

void bar(void)
{
    m = get_seg(gs);
}

bar:
        movl    %gs, %eax
        movl    %eax, m(%rip)
        retq

Without the hidden lvalue.

	David


  reply	other threads:[~2026-01-21 18:55 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-05  9:02 [PATCH RESEND v2 1/3] x86: Use MOVL when reading segment registers Uros Bizjak
2026-01-05  9:02 ` [PATCH RESEND v2 2/3] x86/hyperv: Use savesegment() instead of inline asm() to save " Uros Bizjak
2026-01-19 16:22   ` H. Peter Anvin
2026-01-05  9:02 ` [PATCH RESEND v2 3/3] x86/hyperv: Remove ASM_CALL_CONSTRAINT with VMMCALL insn Uros Bizjak
2026-01-19 16:23   ` H. Peter Anvin
2026-01-19 16:22 ` [PATCH RESEND v2 1/3] x86: Use MOVL when reading segment registers H. Peter Anvin
2026-01-20 12:08 ` [tip: x86/cleanups] x86/segment: " tip-bot2 for Uros Bizjak
2026-01-20 12:38   ` H. Peter Anvin
2026-01-20 13:47     ` Uros Bizjak
2026-01-20 14:02       ` Uros Bizjak
2026-01-21 11:49     ` Brian Gerst
2026-01-21 14:08       ` David Laight
2026-01-21 16:16         ` Uros Bizjak
2026-01-21 18:55           ` David Laight [this message]
2026-03-12  9:30     ` Uros Bizjak
2026-03-12 18:55       ` H. Peter Anvin
2026-03-12 19:38         ` Uros Bizjak
2026-03-12 18:56       ` H. Peter Anvin
2026-03-12 19:11       ` H. Peter Anvin
2026-03-12 19:50         ` Uros Bizjak
2026-03-12 22:01           ` 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=20260121185540.020f7b72@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=bp@alien8.de \
    --cc=brgerst@gmail.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=mhklinux@outlook.com \
    --cc=tip-bot2@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