|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [PATCH v8 53/65] x86: keep a per-CPU mirror of current for cross-CPU readers
get_cpu_current(cpu) reads another CPU's current vCPU by dereferencing
that CPU's stack (the cpu_info block at its top) through stack_base[].
It is the only runtime cross-CPU consumer of a stack address: the core
scheduling rendezvous (sched_switch_units()) and the runqueue dump use
it. With per-CPU mapped stacks (Address Space Isolation), and even
more so once the stack pages have no alias in the sparse directmap
view, another CPU's stack is not reachable from every context, so the
value must live somewhere that is.
Keep a per-CPU mirror of current, written by set_current() alongside
the stack field (one extra store on context switch), plus the one
open-coded set_current() equivalent in cpu_smpboot_alloc(), and make
get_cpu_current() read the mirror.
Note that the pre-existing per_cpu(curr_vcpu) is not a substitute: it
tracks whose register state is loaded on the hardware, which lags
behind current across lazy idle switches, whereas the scheduler wants
the scheduling notion of current. The stack-embedded field remains
authoritative for entry code, which only ever reads it on the local
CPU.
Groundwork for per-CPU stack mappings: cross-CPU readers of current
must not depend on reaching another CPU's stack once stacks are mapped
per-CPU.
Assisted-by: Claude Code:claude-opus-4-8, Claude Code:claude-opus-5-5
Signed-off-by: George Dunlap <gwd@xxxxxxxxxxxxxx>
---
Changes in v8:
- New in this version.
Changes since previously posted version ("x86: adventures in Address Space
Isolation", v2):
- New: v2 left get_cpu_current() reading the other CPU's stack
through stack_base[], which the full directmap kept reachable.
---
xen/arch/x86/domain.c | 1 +
xen/arch/x86/include/asm/current.h | 15 ++++++++++++++-
xen/arch/x86/include/asm/smp.h | 7 +++----
xen/arch/x86/smpboot.c | 1 +
4 files changed, 19 insertions(+), 5 deletions(-)
diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c
index f3e393088f..1df44f6216 100644
--- a/xen/arch/x86/domain.c
+++ b/xen/arch/x86/domain.c
@@ -79,6 +79,7 @@
#endif
DEFINE_PER_CPU(struct vcpu *, curr_vcpu);
+DEFINE_PER_CPU(struct vcpu *, current_vcpu);
static void cf_check default_idle(void);
void (*pm_idle) (void) __read_mostly = default_idle;
diff --git a/xen/arch/x86/include/asm/current.h
b/xen/arch/x86/include/asm/current.h
index 875fe41d92..0e2218c0c4 100644
--- a/xen/arch/x86/include/asm/current.h
+++ b/xen/arch/x86/include/asm/current.h
@@ -101,8 +101,21 @@ static inline attr_const struct cpu_info
*get_cpu_info(void)
return get_cpu_info_from_stack(sp);
}
+/*
+ * Mirror of the stack-embedded ->current_vcpu, for readers on other CPUs,
+ * which need not be able to reach this CPU's stack. Entry code reads the
+ * stack copy. Unlike per_cpu(curr_vcpu), which tracks whose state is loaded
+ * and lags behind current across lazy idle switches, this follows current.
+ */
+DECLARE_PER_CPU(struct vcpu *, current_vcpu);
+
#define get_current() (get_cpu_info()->current_vcpu)
-#define set_current(vcpu) (get_cpu_info()->current_vcpu = (vcpu))
+#define set_current(v) do { \
+ struct vcpu *set_current__v = (v); \
+ \
+ this_cpu(current_vcpu) = set_current__v; \
+ get_cpu_info()->current_vcpu = set_current__v; \
+} while ( 0 )
#define current (get_current())
#define smp_processor_id() (get_cpu_info()->processor_id)
diff --git a/xen/arch/x86/include/asm/smp.h b/xen/arch/x86/include/asm/smp.h
index 8278d0b225..77c489cfe5 100644
--- a/xen/arch/x86/include/asm/smp.h
+++ b/xen/arch/x86/include/asm/smp.h
@@ -63,11 +63,10 @@ void set_nr_sockets(void);
extern cpumask_t **socket_cpumask;
/*
- * To be used only while no context switch can occur on the cpu, i.e.
- * by certain scheduling code only.
+ * To be used only for an online CPU, and while no context switch can
+ * occur on it, i.e. by certain scheduling code only.
*/
-#define get_cpu_current(cpu) \
- (get_cpu_info_from_stack((unsigned long)stack_base[cpu])->current_vcpu)
+#define get_cpu_current(cpu) per_cpu(current_vcpu, cpu)
extern unsigned int disabled_cpus;
extern bool unaccounted_cpus;
diff --git a/xen/arch/x86/smpboot.c b/xen/arch/x86/smpboot.c
index d03c19f4c0..4bd37e3c99 100644
--- a/xen/arch/x86/smpboot.c
+++ b/xen/arch/x86/smpboot.c
@@ -1088,6 +1088,7 @@ static int cpu_smpboot_alloc(unsigned int cpu)
init_shadow_spec_ctrl_state(info);
info->current_vcpu = idle_vcpu[cpu]; /* set_current() */
+ per_cpu(current_vcpu, cpu) = idle_vcpu[cpu];
per_cpu(curr_vcpu, cpu) = idle_vcpu[cpu];
per_cpu(pgtable_vcpu, cpu) = idle_vcpu[cpu];
--
2.56.0
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |