[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v7 40/50] x86/mtrr: move the set_mtrr() rendezvous data off the stack



Per-CPU stack mappings, to come as part of Address Space Isolation,
give some contexts page-tables which map only the stack of the CPU
they run on.  A CPU in such a context cannot reach another CPU's
stack, so an IPI handler cannot dereference a pointer into the
initiating CPU's stack.  Payloads handed to other CPUs via
on_selected_cpus() must live somewhere globally reachable.

set_mtrr() passes a pointer to a stack-allocated struct set_mtrr_data
to all other CPUs.  The payload is a live rendezvous object: the IPI
handler decrements data->count and spins on data->gate in three
phases, while the initiating CPU re-arms count and toggles gate.  The
dispatch is asynchronous (@wait == 0), but set_mtrr() supplies its own
barrier - it spins on data->count through the entire protocol and does
not return until the rendezvous has completed - so the frame stays
alive for as long as the callees use the data.  The current code is
correct; it just relies on cross-CPU stack reachability, which is
going away.

Make the rendezvous data a function-scope static.  All fields are
(re)initialised on every invocation before the IPI is sent, so the one
new requirement is that set_mtrr() invocations be serialised: a static
shares a single object across invocations where each call previously
had its own private stack copy.  mtrr_add_page() and mtrr_del_page()
already call set_mtrr() with mtrr_mutex held; take the lock in
mtrr_aps_sync_end() as well, and assert in set_mtrr() that it is held.

Groundwork for per-CPU stack mappings, which take away a CPU's access
to other CPUs' stacks: besides get_cpu_current(), cross-CPU rendezvous
and call-function payloads are what reads another CPU's stack at run
time.

Assisted-by: Claude Code:claude-opus-4-8, Claude Code:claude-opus-5-5
Signed-off-by: George Dunlap <gwd@xxxxxxxxxxxxxx>
---
Changes in v7:
- New in this version.
---
 xen/arch/x86/cpu/mtrr/main.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/xen/arch/x86/cpu/mtrr/main.c b/xen/arch/x86/cpu/mtrr/main.c
index 432d6f6761..2a7b544bb1 100644
--- a/xen/arch/x86/cpu/mtrr/main.c
+++ b/xen/arch/x86/cpu/mtrr/main.c
@@ -202,9 +202,16 @@ static void set_mtrr(unsigned int reg, unsigned long base,
 {
        cpumask_t allbutself;
        unsigned int nr_cpus;
-       struct set_mtrr_data data;
+       /*
+        * Shared with the IPI handlers on the other CPUs, so not on the
+        * stack, which other CPUs need not be able to reach.  All callers
+        * hold mtrr_mutex, which serialises access to the shared object.
+        */
+       static struct set_mtrr_data data;
        unsigned long flags;
 
+       ASSERT(spin_is_locked(&mtrr_mutex));
+
        cpumask_andnot(&allbutself, &cpu_online_map,
                       cpumask_of(smp_processor_id()));
        nr_cpus = cpumask_weight(&allbutself);
@@ -606,9 +613,11 @@ void mtrr_aps_sync_begin(void)
 
 void mtrr_aps_sync_end(void)
 {
+       mutex_lock(&mtrr_mutex);
        if (mtrr_if)
                set_mtrr(~0U, 0, 0, 0);
        hold_mtrr_updates_on_aps = false;
+       mutex_unlock(&mtrr_mutex);
 }
 
 void asmlinkage mtrr_bp_restore(void)
-- 
2.56.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.