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

[PATCH v7 50/50] xen/smp: assert that by-pointer callfunc payloads are not on the stack



All in-tree callers of on_selected_cpus() / smp_call_function() /
stop_machine_run() that used to pass a pointer to a stack local have
been converted: rendezvous-style payloads moved to statics, message-
and result-style payloads are passed by value.  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;
stack payloads are not reachable from a CPU in such a context, so the
conversion must stay complete.  Enforce the resulting contract with a
debug-build assert: a payload passed by pointer must not point into
the stack the initiator is running on.

The check is deliberately unconditional (not tied to any Address Space
Isolation option): with per-CPU mapped stacks a violating caller
faults on the remote CPU, typically in a path that boot testing never
exercises (MTRR/PSR/cpufreq/MCE inject).  Asserting on every debug
build turns a future reintroduction of the pattern into an immediate,
attributable failure at the call site, regardless of configuration.
For the same uniformity the check applies even to a self-only target
mask (where a stack payload would happen to work): the API contract is
one rule, not a per-mask case analysis.

The helper derives the current stack block from the address of a local
variable, relying on stacks being STACK_SIZE-aligned blocks, as they
are on x86, Arm and PPC, whose get_cpu_info() depends on it.  RISC-V's
vCPU stacks come from vzalloc() and are only page-aligned, but nothing
runs on them yet.  A
payload referencing a different mapping of the same stack pages (such
as a directmap alias) is not caught - inherent to any VA-comparison
scheme; a payload taken from a local variable's address names the
stack being run on, not an alias of it.

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/common/smp.c          |  6 ++++++
 xen/common/stop_machine.c |  6 ++++++
 xen/include/xen/smp.h     | 16 ++++++++++++++++
 3 files changed, 28 insertions(+)

diff --git a/xen/common/smp.c b/xen/common/smp.c
index eb419a3aeb..ef068c014e 100644
--- a/xen/common/smp.c
+++ b/xen/common/smp.c
@@ -67,6 +67,12 @@ static void _on_selected_cpus(
     ASSERT(cpumask_subset(selected, &cpu_online_map));
     BUG_ON(size > CALLFUNC_DATA_MAX);
     BUG_ON(size && !info);
+    /*
+     * By-pointer payloads must be reachable from the target CPUs, which a
+     * CPU's stack need not be: pass such payloads by value (@size != 0), or
+     * move the datum off the stack.
+     */
+    ASSERT(size || !info || !ptr_on_current_stack(info));
 
     spin_lock(&call_lock);
 
diff --git a/xen/common/stop_machine.c b/xen/common/stop_machine.c
index 398cfd507c..e7cdc4ce5a 100644
--- a/xen/common/stop_machine.c
+++ b/xen/common/stop_machine.c
@@ -81,6 +81,12 @@ int stop_machine_run(int (*fn)(void *data), void *data, 
unsigned int cpu)
 
     BUG_ON(!local_irq_is_enabled());
     BUG_ON(!is_idle_vcpu(current));
+    /*
+     * @data is dereferenced from other CPUs' stop-machine contexts, so it
+     * must not point into a CPU's stack, which other CPUs need not be able
+     * to reach.
+     */
+    ASSERT(!data || !ptr_on_current_stack(data));
 
     /* cpu_online_map must not change. */
     if ( !get_cpu_maps() )
diff --git a/xen/include/xen/smp.h b/xen/include/xen/smp.h
index 73bf7075d6..4b9afa18d9 100644
--- a/xen/include/xen/smp.h
+++ b/xen/include/xen/smp.h
@@ -126,6 +126,22 @@ static inline void on_each_cpu(
     on_selected_cpus(&cpu_online_map, func, info, wait);
 }
 
+/*
+ * Does @p point into the stack we are currently running on?  Stacks
+ * are STACK_SIZE-aligned blocks (on x86, Arm and PPC; RISC-V's vCPU
+ * stacks, which nothing runs on yet, are only page-aligned), so the
+ * containing block can be derived from any live stack address, such
+ * as that of a local variable.  Note that other mappings of the same
+ * stack (e.g. a directmap alias of the underlying pages) compare as
+ * not-on-stack by design.
+ */
+static inline bool ptr_on_current_stack(const void *p)
+{
+    unsigned long here = (unsigned long)&here;
+
+    return !(((unsigned long)p ^ here) & ~(STACK_SIZE - 1));
+}
+
 /*
  * Call a function on the current CPU
  */
-- 
2.56.0




 


Rackspace

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