From: Andrew Cooper <andrew.cooper3@citrix.com>
To: "H. Peter Anvin" <hpa@zytor.com>, Borislav Petkov <bp@alien8.de>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>,
x86-ML <x86@kernel.org>, LKML <linux-kernel@vger.kernel.org>
Subject: Re: Save a WRMSR GS.base?
Date: Fri, 5 Jun 2026 10:13:07 +0100 [thread overview]
Message-ID: <fa21fb3f-b8a3-4b8a-82a9-01bb6dcc1333@citrix.com> (raw)
In-Reply-To: <0F905129-3447-4173-A88B-98EAC8C18E4C@zytor.com>
On 05/06/2026 6:05 am, H. Peter Anvin wrote:
> On June 4, 2026 9:38:46 PM PDT, Borislav Petkov <bp@alien8.de> wrote:
>> On Thu, Jun 04, 2026 at 09:30:33PM -0700, H. Peter Anvin wrote:
>>> On June 4, 2026 9:26:52 PM PDT, Borislav Petkov <bp@alien8.de> wrote:
>>>> On Thu, Jun 04, 2026 at 08:20:57PM -0700, H. Peter Anvin wrote:
>>>>> I guess the question is why there is a "first" one.
>>>> That happens when we do:
>>>>
>>>> x86_fsgsbase_load()
>>>>
>>>> loadseg(GS) -> load_gs_index() -> native_load_gs_index() ->
>>>> if (cpu_feature_enabled(X86_FEATURE_LKGS))
>>>> native_lkgs(selector);
>>>>
>>>> then back in x86_fsgsbase_load() we do:
>>>>
>>>> __wrgsbase_inactive(next->gsbase);
>>>>
>>>> which does
>>>>
>>>> wrmsrq(MSR_KERNEL_GS_BASE, gsbase);
>>>>
>>>> on FRED.
>>>>
>>>> But LKGS already wrote MSR_KERNEL_GS_BASE...
>>>>
>>>>> Logically the sequence should be LKGS first, if needed; then WRMSR(NS). LKGS
>>>>> can be replaced with swapgs/mov gs/swapgs on legacy.
>>>> Right.
>>>>
>>>> I think avoiding that second WRMSR(MSR_KERNEL_GS_BASE) should give some perf
>>>> back...
>>>>
>>>> Although, I need to think how to make it pretty...
>>>>
>>> Should be doing wrmsrns...
>> No, I think that second WRMSR* should not happen at all if we have executed
>> LKGS which has already written MSR_KERNEL_GS_BASE, right?
>>
>>
> You can't do that (at least not without further checks) if user space has WRGSBASE enabled, since you have no guarantee that the active GS.base is consistent with GS.selector.
>
> Since GS > 3 is pretty rare in 64-bit code at least, it doesn't seem to be a code path that needs to be that heavily optimized.
I think you're slightly talking past each other, and I also made a
mistake on the original reply, so lets try rephrasing it.
LGKS only writes a zero-extended 32bit value into KERN_GS_BASE. This is
because there's only 32 bits of information in the GDT/LDT.
So the real write into KERN_GS_BASE is still needed. Sorry - you can't
optimise this away. Also, I'm pretty sure amluto did some x86 selftests
covering this last time the logic was rewritten.
As to WRMSR vs WRMSRNS, yes Intel CPUs want this to be WRMSRNS. AMD
don't have WRMSRNS but this particular MSR index is architecturally not
architecturally serialising anyway.
~Andrew
next prev parent reply other threads:[~2026-06-05 9:13 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-04 1:53 Save a WRMSR GS.base? Borislav Petkov
2026-06-04 9:17 ` Andrew Cooper
2026-06-05 2:24 ` Borislav Petkov
2026-06-05 2:36 ` H. Peter Anvin
2026-06-05 2:54 ` Borislav Petkov
2026-06-05 3:20 ` H. Peter Anvin
2026-06-05 4:26 ` Borislav Petkov
2026-06-05 4:30 ` H. Peter Anvin
2026-06-05 4:38 ` Borislav Petkov
2026-06-05 5:05 ` H. Peter Anvin
2026-06-05 9:13 ` Andrew Cooper [this message]
2026-06-05 15:13 ` H. Peter Anvin
2026-06-05 15:16 ` Andrew Cooper
2026-06-05 15:51 ` H. Peter Anvin
2026-06-05 17:17 ` Borislav Petkov
2026-06-08 6:46 ` H. Peter Anvin
2026-06-08 14:38 ` Borislav Petkov
2026-06-08 17:30 ` H. Peter Anvin
2026-06-08 20:05 ` Borislav Petkov
2026-06-08 21:21 ` Borislav Petkov
2026-06-08 21:52 ` H. Peter Anvin
2026-06-08 22:58 ` Andrew Cooper
2026-06-18 1:09 ` Borislav Petkov
2026-06-18 10:22 ` David Laight
2026-06-18 18:17 ` H. Peter Anvin
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=fa21fb3f-b8a3-4b8a-82a9-01bb6dcc1333@citrix.com \
--to=andrew.cooper3@citrix.com \
--cc=bp@alien8.de \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.