Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH v2 1/1] x86/fred: Fix the FRED RSP0 MSR out of sync with its per-CPU cache
@ 2025-01-10 17:46 Xin Li (Intel)
  2025-01-10 18:09 ` Dave Hansen
  0 siblings, 1 reply; 3+ messages in thread
From: Xin Li (Intel) @ 2025-01-10 17:46 UTC (permalink / raw)
  To: linux-kernel, stable
  Cc: tglx, mingo, bp, dave.hansen, x86, hpa, andrew.cooper3

The FRED RSP0 MSR is only used for delivering events when running
userspace.  Linux leverages this property to reduce expensive MSR
writes and optimize context switches.  The kernel only writes the
MSR when about to run userspace *and* when the MSR has actually
changed since the last time userspace ran.

This optimization is implemented by maintaining a per-CPU cache of
FRED RSP0 and then checking that against the value for the top of
current task stack before running userspace.

However cpu_init_fred_exceptions() writes the MSR without updating
the per-CPU cache.  This means that the kernel might return to
userspace with MSR_IA32_FRED_RSP0==0 when it needed to point to the
top of current task stack.  This would induce a double fault (#DF),
which is bad.

A context switch after cpu_init_fred_exceptions() can paper over
the issue since it updates the cached value.  That evidently
happens most of the time explaining how this bug got through.

Fix the bug through resynchronizing the FRED RSP0 MSR with its
per-CPU cache in cpu_init_fred_exceptions().

Fixes: fe85ee391966 ("x86/entry: Set FRED RSP0 on return to userspace instead of context switch")
Signed-off-by: Xin Li (Intel) <xin@zytor.com>
Cc: stable@vger.kernel.org
---
 arch/x86/kernel/fred.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/fred.c b/arch/x86/kernel/fred.c
index 8d32c3f48abc..5e2cd1004980 100644
--- a/arch/x86/kernel/fred.c
+++ b/arch/x86/kernel/fred.c
@@ -50,7 +50,13 @@ void cpu_init_fred_exceptions(void)
 	       FRED_CONFIG_ENTRYPOINT(asm_fred_entrypoint_user));
 
 	wrmsrl(MSR_IA32_FRED_STKLVLS, 0);
-	wrmsrl(MSR_IA32_FRED_RSP0, 0);
+
+	/*
+	 * Ater a CPU offline/online cycle, the FRED RSP0 MSR should be
+	 * resynchronized with its per-CPU cache.
+	 */
+	wrmsrl(MSR_IA32_FRED_RSP0, __this_cpu_read(fred_rsp0));
+
 	wrmsrl(MSR_IA32_FRED_RSP1, 0);
 	wrmsrl(MSR_IA32_FRED_RSP2, 0);
 	wrmsrl(MSR_IA32_FRED_RSP3, 0);

base-commit: 59011effc84d7b167f4b6542bd05c7aff1b7574a
-- 
2.47.1


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

* Re: [PATCH v2 1/1] x86/fred: Fix the FRED RSP0 MSR out of sync with its per-CPU cache
  2025-01-10 17:46 [PATCH v2 1/1] x86/fred: Fix the FRED RSP0 MSR out of sync with its per-CPU cache Xin Li (Intel)
@ 2025-01-10 18:09 ` Dave Hansen
  2025-01-10 18:16   ` Xin Li
  0 siblings, 1 reply; 3+ messages in thread
From: Dave Hansen @ 2025-01-10 18:09 UTC (permalink / raw)
  To: Xin Li (Intel), linux-kernel, stable
  Cc: tglx, mingo, bp, dave.hansen, x86, hpa, andrew.cooper3

On 1/10/25 09:46, Xin Li (Intel) wrote:
...> Fix the bug through resynchronizing the FRED RSP0 MSR with its
> per-CPU cache in cpu_init_fred_exceptions().
> 
> Fixes: fe85ee391966 ("x86/entry: Set FRED RSP0 on return to userspace instead of context switch")
> Signed-off-by: Xin Li (Intel) <xin@zytor.com>
> Cc: stable@vger.kernel.org

Thanks for the update:

Acked-by: Dave Hansen <dave.hansen@linux.intel.com>

I'll probably do a wee bit of testing today and apply to the urgent pile
early next week.

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

* Re: [PATCH v2 1/1] x86/fred: Fix the FRED RSP0 MSR out of sync with its per-CPU cache
  2025-01-10 18:09 ` Dave Hansen
@ 2025-01-10 18:16   ` Xin Li
  0 siblings, 0 replies; 3+ messages in thread
From: Xin Li @ 2025-01-10 18:16 UTC (permalink / raw)
  To: Dave Hansen, linux-kernel, stable
  Cc: tglx, mingo, bp, dave.hansen, x86, hpa, andrew.cooper3

On 1/10/2025 10:09 AM, Dave Hansen wrote:
> On 1/10/25 09:46, Xin Li (Intel) wrote:
> ...> Fix the bug through resynchronizing the FRED RSP0 MSR with its
>> per-CPU cache in cpu_init_fred_exceptions().
>>
>> Fixes: fe85ee391966 ("x86/entry: Set FRED RSP0 on return to userspace instead of context switch")
>> Signed-off-by: Xin Li (Intel) <xin@zytor.com>
>> Cc: stable@vger.kernel.org
> 
> Thanks for the update:
> 
> Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
> 
> I'll probably do a wee bit of testing today and apply to the urgent pile
> early next week.
> 

Sure, thanks a lot!

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

end of thread, other threads:[~2025-01-10 18:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-10 17:46 [PATCH v2 1/1] x86/fred: Fix the FRED RSP0 MSR out of sync with its per-CPU cache Xin Li (Intel)
2025-01-10 18:09 ` Dave Hansen
2025-01-10 18:16   ` Xin Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox