mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Nikunj A. Dadhania" <nikunj@amd.com>
To: Sohil Mehta <sohil.mehta@intel.com>,
	Dave Hansen <dave.hansen@intel.com>,
	Tom Lendacky <thomas.lendacky@amd.com>,
	Borislav Petkov <bp@alien8.de>
Cc: <linux-kernel@vger.kernel.org>, <kvm@vger.kernel.org>,
	<tglx@kernel.org>, <mingo@redhat.com>,
	<dave.hansen@linux.intel.com>, <hpa@zytor.com>, <xin@zytor.com>,
	<seanjc@google.com>, <pbonzini@redhat.com>, <x86@kernel.org>,
	<jon.grimm@amd.com>
Subject: Re: [PATCH v2 1/2] x86/cpu: Disable CR pinning during CPU bringup
Date: Fri, 13 Mar 2026 14:05:16 +0530	[thread overview]
Message-ID: <1439b4f4-ff0c-4b6a-ac86-5c0da2d26cf5@amd.com> (raw)
In-Reply-To: <13f67190-b51c-4719-a409-13d813549d7c@intel.com>

On 3/12/2026 11:39 PM, Sohil Mehta wrote:
> On 3/12/2026 7:08 AM, Nikunj A. Dadhania wrote:
> 
>>   1) Universally set X86_CR4_FSGSBASE in cr4_init() and call cr4_init()
>>      from trap_init() on the boot CPU
> 
> 
> cr4_init() seems like the wrong place to do this. I don't think this a
> primarily a CR4 issue. Deferring CR4 pinning maybe have uncovered the
> FSGSBASE issue.
> 
> But, essentially the difference lies in when we enable exception
> handling related features on the BSP vs APs. It involves setting a few
> other things than CR4 programming.
> 
> See:
> 
> /*
>  * Setup everything needed to handle exceptions from the IDT, including
> the IST
>  * exceptions which use paranoid_entry().
>  */
> void cpu_init_exception_handling(bool boot_cpu)
> 
> IIUC, anything that is needed to handle exceptions should be initialized
> here. As FSGSBASE is used in the paranoid_entry() code, should its
> enabling be moved to cpu_init_exception_handling()?

Good idea, thanks!

For the boot CPU, FRED is enabled via cpu_init_replace_early_idt(), and
cpu_init_exception_handling() is called later (before alternative patching).
FSGSBASE can be safely enabled in cpu_init_exception_handling():

start_kernel()
  setup_arch()
   cpu_init_replace_early_idt()
     cpu_init_fred_exceptions()             <-- FRED enabled here
  ...
  trap_init()
    cpu_init_exception_handling(true)
      cr4_set_bits(X86_CR4_FSGSBASE);       <-- Enable FSGSBASE here
  ...
  arch_cpu_finalize_init()
    ...
    alternative_instructions()              <- Patches code to use RDGSBASE/WRGSBASE

For secondary CPUs, FSGSBASE can be safely enabled before any exceptions
occur, and FRED is enabled immediately after:

  start_secondary()
    cr4_init()                              <- Code already patched, CR4.FSGSBASE=0
    cpu_init_exception_handling(false)
      cr4_set_bits(X86_CR4_FSGSBASE);       <-- Enable FSGSBASE here
      if (!boot_cpu)
        cpu_init_fred_exceptions();         <-- FRED enabled here for secondary CPU
    ...
    cpu_init()


diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 1c3261cae40c..bd35e98d648d 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -2047,12 +2047,6 @@ static void identify_cpu(struct cpuinfo_x86 *c)
 	setup_umip(c);
 	setup_lass(c);
 
-	/* Enable FSGSBASE instructions if available. */
-	if (cpu_has(c, X86_FEATURE_FSGSBASE)) {
-		cr4_set_bits(X86_CR4_FSGSBASE);
-		elf_hwcap2 |= HWCAP2_FSGSBASE;
-	}
-
 	/*
 	 * The vendor-specific functions might have changed features.
 	 * Now we do "generic changes."
@@ -2413,6 +2407,16 @@ void cpu_init_exception_handling(bool boot_cpu)
 	/* GHCB needs to be setup to handle #VC. */
 	setup_ghcb();
 
+	/*
+	 * CPUs that support FSGSBASE may use RDGSBASE/WRGSBASE in
+	 * paranoid_entry(). Enable the feature before any exceptions
+	 * occur.
+	 */
+	if (cpu_feature_enabled(X86_FEATURE_FSGSBASE)) {
+		cr4_set_bits(X86_CR4_FSGSBASE);
+		elf_hwcap2 |= HWCAP2_FSGSBASE;
+	}
+
 	if (cpu_feature_enabled(X86_FEATURE_FRED)) {
 		/* The boot CPU has enabled FRED during early boot */
 		if (!boot_cpu)

Regards,
Nikunj



  reply	other threads:[~2026-03-13  8:35 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-26  9:23 [PATCH v2 0/2] x86/fred: Fix SEV-ES/SNP guest boot failures Nikunj A Dadhania
2026-02-26  9:23 ` [PATCH v2 1/2] x86/cpu: Disable CR pinning during CPU bringup Nikunj A Dadhania
2026-03-09 13:46   ` Borislav Petkov
2026-03-09 15:38     ` Dave Hansen
2026-03-09 16:15       ` Borislav Petkov
2026-03-09 18:03         ` Dave Hansen
2026-03-09 18:40           ` Tom Lendacky
2026-03-09 19:27             ` Dave Hansen
2026-03-11 10:41               ` Nikunj A. Dadhania
2026-03-11 14:07                 ` Dave Hansen
2026-03-11 15:42                   ` Nikunj A. Dadhania
2026-03-11 17:28                     ` Dave Hansen
2026-03-12  7:21                       ` Nikunj A. Dadhania
2026-03-12  7:26                         ` Nikunj A. Dadhania
2026-03-12 14:08                       ` Nikunj A. Dadhania
2026-03-12 14:20                         ` Dave Hansen
2026-03-12 14:53                           ` Nikunj A. Dadhania
2026-03-12 15:02                             ` Dave Hansen
2026-03-12 19:06                               ` David Laight
2026-03-16 20:27                           ` Chang S. Bae
2026-03-16 21:43                             ` Dave Hansen
2026-03-17  4:12                               ` Nikunj A. Dadhania
2026-03-17 14:26                                 ` Borislav Petkov
2026-03-17 15:31                                 ` Dave Hansen
2026-03-17 16:54                                   ` Borislav Petkov
2026-03-18  8:19                                     ` Nikunj A. Dadhania
2026-03-17 17:04                               ` Chang S. Bae
2026-03-17 17:51                                 ` Chang S. Bae
2026-03-12 18:09                         ` Sohil Mehta
2026-03-13  8:35                           ` Nikunj A. Dadhania [this message]
2026-03-13 18:05                             ` Sohil Mehta
2026-03-13 19:10                               ` Borislav Petkov
2026-03-17 17:06                             ` Chang S. Bae
2026-02-26  9:23 ` [PATCH v2 2/2] x86/fred: Fix early boot failures on SEV-ES/SNP guests Nikunj A Dadhania
2026-02-26 14:14   ` Tom Lendacky
2026-02-27  4:14     ` Nikunj A. Dadhania

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=1439b4f4-ff0c-4b6a-ac86-5c0da2d26cf5@amd.com \
    --to=nikunj@amd.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=jon.grimm@amd.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=sohil.mehta@intel.com \
    --cc=tglx@kernel.org \
    --cc=thomas.lendacky@amd.com \
    --cc=x86@kernel.org \
    --cc=xin@zytor.com \
    /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®