Skip to contentSkip to conversation

xen/x86: Change stub page allocation/free

PATCH In review Roger Pau Monne 3 participants 5 messages Jun 12

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 xen/x86: Change stub page allocation/free

From: Jason Andryuk <[email protected]>

Today the inline tracking of the stub page is problematic. 0xcc is used to indicate unused, but it is also a "clear value." A !CONFIG_PV build or when running with FRED support will not populate the LSTAR/CSTAR stubs at CPU bringup. If a CPU is then offlined, the stubs page will be freed as its content will be all 0xcc, regardless of the stubs page still begin referenced by other CPUs.

The new approach uses a global, CPU-indexed dynamically allocated array of stub addresses. However, to handle NUMA aware allocations, we cannot allocate all the memory in advance because of the NUMA dependency. Take advantage of the fact that Xen will attempt to contiguously pack CPUs on the same NUMA node (see normalise_cpu_order()), and on CPU bringup use the same stubs page the previous CPU did if suitable. Note the code would still function properly even if CPUs from NUMA nodes are not contiguously packed, it just consumes more memory.

stub pages are no longer freed. They remain referenced in the global CPU-indexed array and are re-used if the CPU is re-onlined.

The stubs array doesn't have an explicit lock. During boot it's accessed single threaded. During runtime, &cpu_add_remove_lock serializes access.

Fixes 7a66ac8d1633 ("x86: move syscall trampolines off the stack")

---
Changes since v2:
- Adjust commit message.
- Handle the case where stubs[cpu - 1] == PADDR_INVALID.

---
 xen/arch/x86/include/asm/stubs.h |  2 +-
 xen/arch/x86/setup.c             |  4 +-
 xen/arch/x86/smpboot.c           | 94 +++++++++++++++++---------------
 3 files changed, 52 insertions(+), 48 deletions(-)
xen/arch/x86/include/asm/stubs.h +1 -1
index a520928e9a50..467551136a2a 100644
@@ -32,6 +32,6 @@ struct stubs {
 };
 
 DECLARE_PER_CPU(struct stubs, stubs);
-unsigned long alloc_stub_page(unsigned int cpu, unsigned long *mfn);
+void init_stub(void);
 
 #endif /* X86_ASM_STUBS_H */
xen/arch/x86/setup.c +1 -3
index 4192edf635b6..0253d22c349d 100644
@@ -2089,9 +2089,7 @@ void asmlinkage __init noreturn __start_xen(void)
 
     init_idle_domain();
 
-    this_cpu(stubs.addr) = alloc_stub_page(smp_processor_id(),
-                                           &this_cpu(stubs).mfn);
-    BUG_ON(!this_cpu(stubs.addr));
+    init_stub();
 
     bsp_traps_reinit(); /* Needs stubs allocated, must be before presmp_initcalls. */
 
xen/arch/x86/smpboot.c +50 -44

144 lines not on this page · raw message

Signature · 1 line
2.53.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 18 lines · Roger Pau Monne

From: Jason Andryuk <[email protected]>

Today the inline tracking of the stub page is problematic. 0xcc is used to indicate unused, but it is also a "clear value." A !CONFIG_PV build or when running with FRED support will not populate the LSTAR/CSTAR stubs at CPU bringup. If a CPU is then offlined, the stubs page will be freed as its content will be all 0xcc, regardless of the stubs page still begin referenced by other CPUs.

The new approach uses a global, CPU-indexed dynamically allocated array of stub addresses. However, to handle NUMA aware allocations, we cannot allocate all the memory in advance because of the NUMA dependency. Take advantage of the fact that Xen will attempt to contiguously pack CPUs on the same NUMA node (see normalise_cpu_order()), and on CPU bringup use the same stubs page the previous CPU did if suitable. Note the code would still function properly even if CPUs from NUMA nodes are not contiguously packed, it just consumes more memory.

Quoted 1 line

stub pages are no longer freed. They remain referenced in the global

Nit: Didn't you mean to s/stub/Stub/ as per Andrew's comment?

Quoted 10 lines

CPU-indexed array and are re-used if the CPU is re-onlined.

The stubs array doesn't have an explicit lock. During boot it's accessed single threaded. During runtime, &cpu_add_remove_lock serializes access.

Fixes 7a66ac8d1633 ("x86: move syscall trampolines off the stack")

Jan

#2 lore raw
Quoted 19 lines · Jan Beulich
Quoted 18 lines · Roger Pau Monne

From: Jason Andryuk <[email protected]>

Today the inline tracking of the stub page is problematic. 0xcc is used to indicate unused, but it is also a "clear value." A !CONFIG_PV build or when running with FRED support will not populate the LSTAR/CSTAR stubs at CPU bringup. If a CPU is then offlined, the stubs page will be freed as its content will be all 0xcc, regardless of the stubs page still begin referenced by other CPUs.

The new approach uses a global, CPU-indexed dynamically allocated array of stub addresses. However, to handle NUMA aware allocations, we cannot allocate all the memory in advance because of the NUMA dependency. Take advantage of the fact that Xen will attempt to contiguously pack CPUs on the same NUMA node (see normalise_cpu_order()), and on CPU bringup use the same stubs page the previous CPU did if suitable. Note the code would still function properly even if CPUs from NUMA nodes are not contiguously packed, it just consumes more memory.

Quoted 3 lines
Quoted 1 line

stub pages are no longer freed. They remain referenced in the global

Nit: Didn't you mean to s/stub/Stub/ as per Andrew's comment?

Hm, yes, I've fixed the instance below, but not the one here.

Quoted 5 lines
Quoted 5 lines

CPU-indexed array and are re-used if the CPU is re-onlined.

The stubs array doesn't have an explicit lock. During boot it's accessed single threaded. During runtime, &cpu_add_remove_lock serializes access.

Quoted 7 lines
Quoted 5 lines
Fixes 7a66ac8d1633 ("x86: move syscall trampolines off the stack")

Thanks.

I will run a CI pre-commit loop and push it today if it's all fine.

Roger.

Reviewed-by on v3 1/1
#3 lore raw
In reply to Roger Pau Monne

xen/arch/x86/setup.c void asmlinkage __init noreturn __start_xen(void) · line 2092

Quoted 12 lines · Roger Pau Monne
xen/arch/x86/setup.c +1 -3
index 4192edf635b6..0253d22c349d 100644
@@ -2089,9 +2089,7 @@ void asmlinkage __init noreturn __start_xen(void)
 
     init_idle_domain();
 
-    this_cpu(stubs.addr) = alloc_stub_page(smp_processor_id(),
-                                           &this_cpu(stubs).mfn);
-    BUG_ON(!this_cpu(stubs.addr));
+    init_stub();

While I hate to nitpick further, "stubs".  There are 4 per CPU, and I'm reasonably sure we're consistently plural elsewhere.

Otherwise (and with Jan's grammar note in the commit message too),

One further thought, which would be for a followup patch if we decide to do it.

We do now use UDB for bugframes.  It's also marginally better than INT3 because if we do ever end up finding ourselves executing it, because we'll #UD on the first one, rather than try to brute force our way through the #BPs printing a log message per instruction.

One thing I didn't check and I really need to is whether UDB has the same speculation-halting properties that UD2 has.  It's not a guarantee, given that the encoding is valid in other modes.

~Andrew

#4 lore raw
Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Roger Pau Monne
xen/arch/x86/setup.c +0 -0
index 4192edf635b6..0253d22c349d 100644
@@ -2089,9 +2089,7 @@ void asmlinkage __init noreturn __start_xen(void)
 
     init_idle_domain();
 
Quoted 7 lines

xen/arch/x86/setup.c void asmlinkage __init noreturn __start_xen(void) · line 2092

Quoted 4 lines
xen/arch/x86/setup.c +1 -3
-    this_cpu(stubs.addr) = alloc_stub_page(smp_processor_id(),
-                                           &this_cpu(stubs).mfn);
-    BUG_ON(!this_cpu(stubs.addr));
+    init_stub();

While I hate to nitpick further, "stubs".  There are 4 per CPU, and I'm reasonably sure we're consistently plural elsewhere.

Funny you ask - I had it as "stubs" initially, but then realized the existing function is named alloc_stub_page(), and hence switched back to use "stub" as that seemed more inline with the existing usage.

I will adjust on commit.

Quoted 2 lines

Otherwise (and with Jan's grammar note in the commit message too),

Thanks, yes, that's been adjusted in the commit message.

Quoted 11 lines

One further thought, which would be for a followup patch if we decide to do it.

We do now use UDB for bugframes.  It's also marginally better than INT3 because if we do ever end up finding ourselves executing it, because we'll #UD on the first one, rather than try to brute force our way through the #BPs printing a log message per instruction.

One thing I didn't check and I really need to is whether UDB has the same speculation-halting properties that UD2 has.  It's not a guarantee, given that the encoding is valid in other modes.

Hm, yes, let's see if I find some time to change it, or whether someone else beats me to it :).

Roger.