Skip to content

Commit 60223a3

Browse files
Jeremy Fitzhardingejsgf
authored andcommitted
xen: Place vcpu_info structure into per-cpu memory
An experimental patch for Xen allows guests to place their vcpu_info structs anywhere. We try to use this to place the vcpu_info into the PDA, which allows direct access. If this works, then switch to using direct access operations for irq_enable, disable, save_fl and restore_fl. Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com> Cc: Chris Wright <chrisw@sous-sol.org> Cc: Keir Fraser <keir@xensource.com>
1 parent 3e2b8fb commit 60223a3

5 files changed

Lines changed: 164 additions & 16 deletions

File tree

‎arch/i386/xen/enlighten.c‎

Lines changed: 148 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,63 @@ DEFINE_PER_CPU(unsigned long, xen_cr3);
6161
struct start_info *xen_start_info;
6262
EXPORT_SYMBOL_GPL(xen_start_info);
6363

64-
void xen_vcpu_setup(int cpu)
64+
static /* __initdata */ struct shared_info dummy_shared_info;
65+
66+
/*
67+
* Point at some empty memory to start with. We map the real shared_info
68+
* page as soon as fixmap is up and running.
69+
*/
70+
struct shared_info *HYPERVISOR_shared_info = (void *)&dummy_shared_info;
71+
72+
/*
73+
* Flag to determine whether vcpu info placement is available on all
74+
* VCPUs. We assume it is to start with, and then set it to zero on
75+
* the first failure. This is because it can succeed on some VCPUs
76+
* and not others, since it can involve hypervisor memory allocation,
77+
* or because the guest failed to guarantee all the appropriate
78+
* constraints on all VCPUs (ie buffer can't cross a page boundary).
79+
*
80+
* Note that any particular CPU may be using a placed vcpu structure,
81+
* but we can only optimise if the all are.
82+
*
83+
* 0: not available, 1: available
84+
*/
85+
static int have_vcpu_info_placement = 1;
86+
87+
static void __init xen_vcpu_setup(int cpu)
6588
{
89+
struct vcpu_register_vcpu_info info;
90+
int err;
91+
struct vcpu_info *vcpup;
92+
6693
per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
94+
95+
if (!have_vcpu_info_placement)
96+
return; /* already tested, not available */
97+
98+
vcpup = &per_cpu(xen_vcpu_info, cpu);
99+
100+
info.mfn = virt_to_mfn(vcpup);
101+
info.offset = offset_in_page(vcpup);
102+
103+
printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %x, offset %d\n",
104+
cpu, vcpup, info.mfn, info.offset);
105+
106+
/* Check to see if the hypervisor will put the vcpu_info
107+
structure where we want it, which allows direct access via
108+
a percpu-variable. */
109+
err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
110+
111+
if (err) {
112+
printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
113+
have_vcpu_info_placement = 0;
114+
} else {
115+
/* This cpu is using the registered vcpu info, even if
116+
later ones fail to. */
117+
per_cpu(xen_vcpu, cpu) = vcpup;
118+
printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
119+
cpu, vcpup);
120+
}
67121
}
68122

69123
static void __init xen_banner(void)
@@ -123,6 +177,20 @@ static unsigned long xen_save_fl(void)
123177
return (-flags) & X86_EFLAGS_IF;
124178
}
125179

180+
static unsigned long xen_save_fl_direct(void)
181+
{
182+
unsigned long flags;
183+
184+
/* flag has opposite sense of mask */
185+
flags = !x86_read_percpu(xen_vcpu_info.evtchn_upcall_mask);
186+
187+
/* convert to IF type flag
188+
-0 -> 0x00000000
189+
-1 -> 0xffffffff
190+
*/
191+
return (-flags) & X86_EFLAGS_IF;
192+
}
193+
126194
static void xen_restore_fl(unsigned long flags)
127195
{
128196
struct vcpu_info *vcpu;
@@ -149,6 +217,25 @@ static void xen_restore_fl(unsigned long flags)
149217
}
150218
}
151219

220+
static void xen_restore_fl_direct(unsigned long flags)
221+
{
222+
/* convert from IF type flag */
223+
flags = !(flags & X86_EFLAGS_IF);
224+
225+
/* This is an atomic update, so no need to worry about
226+
preemption. */
227+
x86_write_percpu(xen_vcpu_info.evtchn_upcall_mask, flags);
228+
229+
/* If we get preempted here, then any pending event will be
230+
handled anyway. */
231+
232+
if (flags == 0) {
233+
barrier(); /* unmask then check (avoid races) */
234+
if (unlikely(x86_read_percpu(xen_vcpu_info.evtchn_upcall_pending)))
235+
force_evtchn_callback();
236+
}
237+
}
238+
152239
static void xen_irq_disable(void)
153240
{
154241
/* There's a one instruction preempt window here. We need to
@@ -159,6 +246,12 @@ static void xen_irq_disable(void)
159246
preempt_enable_no_resched();
160247
}
161248

249+
static void xen_irq_disable_direct(void)
250+
{
251+
/* Atomic update, so preemption not a concern. */
252+
x86_write_percpu(xen_vcpu_info.evtchn_upcall_mask, 1);
253+
}
254+
162255
static void xen_irq_enable(void)
163256
{
164257
struct vcpu_info *vcpu;
@@ -179,6 +272,19 @@ static void xen_irq_enable(void)
179272
force_evtchn_callback();
180273
}
181274

275+
static void xen_irq_enable_direct(void)
276+
{
277+
/* Atomic update, so preemption not a concern. */
278+
x86_write_percpu(xen_vcpu_info.evtchn_upcall_mask, 0);
279+
280+
/* Doesn't matter if we get preempted here, because any
281+
pending event will get dealt with anyway. */
282+
283+
barrier(); /* unmask then check (avoid races) */
284+
if (unlikely(x86_read_percpu(xen_vcpu_info.evtchn_upcall_pending)))
285+
force_evtchn_callback();
286+
}
287+
182288
static void xen_safe_halt(void)
183289
{
184290
/* Blocking includes an implicit local_irq_enable(). */
@@ -551,11 +657,21 @@ static void xen_flush_tlb_others(const cpumask_t *cpus, struct mm_struct *mm,
551657
xen_mc_issue(PARAVIRT_LAZY_MMU);
552658
}
553659

660+
static void xen_write_cr2(unsigned long cr2)
661+
{
662+
x86_read_percpu(xen_vcpu)->arch.cr2 = cr2;
663+
}
664+
554665
static unsigned long xen_read_cr2(void)
555666
{
556667
return x86_read_percpu(xen_vcpu)->arch.cr2;
557668
}
558669

670+
static unsigned long xen_read_cr2_direct(void)
671+
{
672+
return x86_read_percpu(xen_vcpu_info.arch.cr2);
673+
}
674+
559675
static void xen_write_cr4(unsigned long cr4)
560676
{
561677
/* never allow TSC to be disabled */
@@ -753,8 +869,27 @@ static __init void xen_pagetable_setup_done(pgd_t *base)
753869
if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
754870
BUG();
755871
}
872+
}
756873

757-
xen_vcpu_setup(smp_processor_id());
874+
/* This is called once we have the cpu_possible_map */
875+
void __init xen_setup_vcpu_info_placement(void)
876+
{
877+
int cpu;
878+
879+
for_each_possible_cpu(cpu)
880+
xen_vcpu_setup(cpu);
881+
882+
/* xen_vcpu_setup managed to place the vcpu_info within the
883+
percpu area for all cpus, so make use of it */
884+
if (have_vcpu_info_placement) {
885+
printk(KERN_INFO "Xen: using vcpu_info placement\n");
886+
887+
paravirt_ops.save_fl = xen_save_fl_direct;
888+
paravirt_ops.restore_fl = xen_restore_fl_direct;
889+
paravirt_ops.irq_disable = xen_irq_disable_direct;
890+
paravirt_ops.irq_enable = xen_irq_enable_direct;
891+
paravirt_ops.read_cr2 = xen_read_cr2_direct;
892+
}
758893
}
759894

760895
static const struct paravirt_ops xen_paravirt_ops __initdata = {
@@ -788,7 +923,7 @@ static const struct paravirt_ops xen_paravirt_ops __initdata = {
788923
.write_cr0 = native_write_cr0,
789924

790925
.read_cr2 = xen_read_cr2,
791-
.write_cr2 = native_write_cr2,
926+
.write_cr2 = xen_write_cr2,
792927

793928
.read_cr3 = xen_read_cr3,
794929
.write_cr3 = xen_write_cr3,
@@ -974,7 +1109,16 @@ asmlinkage void __init xen_start_kernel(void)
9741109
/* keep using Xen gdt for now; no urgent need to change it */
9751110

9761111
x86_write_percpu(xen_cr3, __pa(pgd));
977-
xen_vcpu_setup(0);
1112+
1113+
#ifdef CONFIG_SMP
1114+
/* Don't do the full vcpu_info placement stuff until we have a
1115+
possible map. */
1116+
per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
1117+
#else
1118+
/* May as well do it now, since there's no good time to call
1119+
it later on UP. */
1120+
xen_setup_vcpu_info_placement();
1121+
#endif
9781122

9791123
paravirt_ops.kernel_rpl = 1;
9801124
if (xen_feature(XENFEAT_supervisor_mode_kernel))

‎arch/i386/xen/setup.c‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@
2424
extern const char xen_hypervisor_callback[];
2525
extern const char xen_failsafe_callback[];
2626

27-
static __initdata struct shared_info init_shared;
28-
29-
/*
30-
* Point at some empty memory to start with. We map the real shared_info
31-
* page as soon as fixmap is up and running.
32-
*/
33-
struct shared_info *HYPERVISOR_shared_info = &init_shared;
34-
3527
unsigned long *phys_to_machine_mapping;
3628
EXPORT_SYMBOL(phys_to_machine_mapping);
3729

‎arch/i386/xen/smp.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,6 @@ void __init xen_smp_prepare_boot_cpu(void)
142142
BUG_ON(smp_processor_id() != 0);
143143
native_smp_prepare_boot_cpu();
144144

145-
xen_vcpu_setup(0);
146-
147145
/* We've switched to the "real" per-cpu gdt, so make sure the
148146
old memory can be recycled */
149147
make_lowmem_page_readwrite(&per_cpu__gdt_page);
@@ -152,6 +150,8 @@ void __init xen_smp_prepare_boot_cpu(void)
152150
cpus_clear(cpu_sibling_map[cpu]);
153151
cpus_clear(cpu_core_map[cpu]);
154152
}
153+
154+
xen_setup_vcpu_info_placement();
155155
}
156156

157157
void __init xen_smp_prepare_cpus(unsigned int max_cpus)
@@ -262,7 +262,6 @@ int __cpuinit xen_cpu_up(unsigned int cpu)
262262

263263
init_gdt(cpu);
264264
per_cpu(current_task, cpu) = idle;
265-
xen_vcpu_setup(cpu);
266265
irq_ctx_init(cpu);
267266
xen_setup_timer(cpu);
268267

‎arch/i386/xen/xen-ops.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static inline unsigned xen_get_lazy_mode(void)
3838

3939
void __init xen_fill_possible_map(void);
4040

41-
void xen_vcpu_setup(int cpu);
41+
void __init xen_setup_vcpu_info_placement(void);
4242
void xen_smp_prepare_boot_cpu(void);
4343
void xen_smp_prepare_cpus(unsigned int max_cpus);
4444
int xen_cpu_up(unsigned int cpu);

‎include/xen/interface/vcpu.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,17 @@ struct vcpu_set_singleshot_timer {
151151
#define _VCPU_SSHOTTMR_future (0)
152152
#define VCPU_SSHOTTMR_future (1U << _VCPU_SSHOTTMR_future)
153153

154+
/*
155+
* Register a memory location in the guest address space for the
156+
* vcpu_info structure. This allows the guest to place the vcpu_info
157+
* structure in a convenient place, such as in a per-cpu data area.
158+
* The pointer need not be page aligned, but the structure must not
159+
* cross a page boundary.
160+
*/
161+
#define VCPUOP_register_vcpu_info 10 /* arg == struct vcpu_info */
162+
struct vcpu_register_vcpu_info {
163+
uint32_t mfn; /* mfn of page to place vcpu_info */
164+
uint32_t offset; /* offset within page */
165+
};
166+
154167
#endif /* __XEN_PUBLIC_VCPU_H__ */

0 commit comments

Comments
 (0)