Skip to content

Commit 2e25aa5

Browse files
paulusmackavikivity
authored andcommitted
KVM: PPC: Book3S HV: Make virtual processor area registration more robust
The PAPR API allows three sorts of per-virtual-processor areas to be registered (VPA, SLB shadow buffer, and dispatch trace log), and furthermore, these can be registered and unregistered for another virtual CPU. Currently we just update the vcpu fields pointing to these areas at the time of registration or unregistration. If this is done on another vcpu, there is the possibility that the target vcpu is using those fields at the time and could end up using a bogus pointer and corrupting memory. This fixes the race by making the target cpu itself do the update, so we can be sure that the update happens at a time when the fields aren't being used. Each area now has a struct kvmppc_vpa which is used to manage these updates. There is also a spinlock which protects access to all of the kvmppc_vpa structs, other than to the pinned_addr fields. (We could have just taken the spinlock when using the vpa, slb_shadow or dtl fields, but that would mean taking the spinlock on every guest entry and exit.) This also changes 'struct dtl' (which was undefined) to 'struct dtl_entry', which is what the rest of the kernel uses. Thanks to Michael Ellerman <michael@ellerman.id.au> for pointing out the need to initialize vcpu->arch.vpa_update_lock. Signed-off-by: Paul Mackerras <paulus@samba.org> Signed-off-by: Alexander Graf <agraf@suse.de> Signed-off-by: Avi Kivity <avi@redhat.com>
1 parent f0888f7 commit 2e25aa5

4 files changed

Lines changed: 190 additions & 76 deletions

File tree

‎arch/powerpc/include/asm/hvcall.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@
114114
#define H_PP1 (1UL<<(63-62))
115115
#define H_PP2 (1UL<<(63-63))
116116

117+
/* Flags for H_REGISTER_VPA subfunction field */
118+
#define H_VPA_FUNC_SHIFT (63-18) /* Bit posn of subfunction code */
119+
#define H_VPA_FUNC_MASK 7UL
120+
#define H_VPA_REG_VPA 1UL /* Register Virtual Processor Area */
121+
#define H_VPA_REG_DTL 2UL /* Register Dispatch Trace Log */
122+
#define H_VPA_REG_SLB 3UL /* Register SLB shadow buffer */
123+
#define H_VPA_DEREG_VPA 5UL /* Deregister Virtual Processor Area */
124+
#define H_VPA_DEREG_DTL 6UL /* Deregister Dispatch Trace Log */
125+
#define H_VPA_DEREG_SLB 7UL /* Deregister SLB shadow buffer */
126+
117127
/* VASI States */
118128
#define H_VASI_INVALID 0
119129
#define H_VASI_ENABLED 1

‎arch/powerpc/include/asm/kvm_host.h‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ struct kvm_vcpu;
8282

8383
struct lppaca;
8484
struct slb_shadow;
85-
struct dtl;
85+
struct dtl_entry;
8686

8787
struct kvm_vm_stat {
8888
u32 remote_tlb_flush;
@@ -279,6 +279,19 @@ struct kvmppc_vcore {
279279
#define VCORE_EXITING 2
280280
#define VCORE_SLEEPING 3
281281

282+
/*
283+
* Struct used to manage memory for a virtual processor area
284+
* registered by a PAPR guest. There are three types of area
285+
* that a guest can register.
286+
*/
287+
struct kvmppc_vpa {
288+
void *pinned_addr; /* Address in kernel linear mapping */
289+
void *pinned_end; /* End of region */
290+
unsigned long next_gpa; /* Guest phys addr for update */
291+
unsigned long len; /* Number of bytes required */
292+
u8 update_pending; /* 1 => update pinned_addr from next_gpa */
293+
};
294+
282295
struct kvmppc_pte {
283296
ulong eaddr;
284297
u64 vpage;
@@ -473,11 +486,6 @@ struct kvm_vcpu_arch {
473486
u8 prodded;
474487
u32 last_inst;
475488

476-
struct lppaca *vpa;
477-
struct slb_shadow *slb_shadow;
478-
struct dtl *dtl;
479-
struct dtl *dtl_end;
480-
481489
wait_queue_head_t *wqp;
482490
struct kvmppc_vcore *vcore;
483491
int ret;
@@ -502,6 +510,13 @@ struct kvm_vcpu_arch {
502510
struct task_struct *run_task;
503511
struct kvm_run *kvm_run;
504512
pgd_t *pgdir;
513+
514+
spinlock_t vpa_update_lock;
515+
struct kvmppc_vpa vpa;
516+
struct kvmppc_vpa dtl;
517+
struct dtl_entry *dtl_ptr;
518+
unsigned long dtl_index;
519+
struct kvmppc_vpa slb_shadow;
505520
#endif
506521
};
507522

‎arch/powerpc/kernel/asm-offsets.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ int main(void)
466466
DEFINE(VCPU_PENDING_EXC, offsetof(struct kvm_vcpu, arch.pending_exceptions));
467467
DEFINE(VCPU_CEDED, offsetof(struct kvm_vcpu, arch.ceded));
468468
DEFINE(VCPU_PRODDED, offsetof(struct kvm_vcpu, arch.prodded));
469-
DEFINE(VCPU_VPA, offsetof(struct kvm_vcpu, arch.vpa));
469+
DEFINE(VCPU_VPA, offsetof(struct kvm_vcpu, arch.vpa.pinned_addr));
470470
DEFINE(VCPU_MMCR, offsetof(struct kvm_vcpu, arch.mmcr));
471471
DEFINE(VCPU_PMC, offsetof(struct kvm_vcpu, arch.pmc));
472472
DEFINE(VCPU_SLB, offsetof(struct kvm_vcpu, arch.slb));

‎arch/powerpc/kvm/book3s_hv.c‎

Lines changed: 158 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,22 @@ static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
134134
vpa->yield_count = 1;
135135
}
136136

137+
/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
138+
struct reg_vpa {
139+
u32 dummy;
140+
union {
141+
u16 hword;
142+
u32 word;
143+
} length;
144+
};
145+
146+
static int vpa_is_registered(struct kvmppc_vpa *vpap)
147+
{
148+
if (vpap->update_pending)
149+
return vpap->next_gpa != 0;
150+
return vpap->pinned_addr != NULL;
151+
}
152+
137153
static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
138154
unsigned long flags,
139155
unsigned long vcpuid, unsigned long vpa)
@@ -142,88 +158,153 @@ static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
142158
unsigned long len, nb;
143159
void *va;
144160
struct kvm_vcpu *tvcpu;
145-
int err = H_PARAMETER;
161+
int err;
162+
int subfunc;
163+
struct kvmppc_vpa *vpap;
146164

147165
tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
148166
if (!tvcpu)
149167
return H_PARAMETER;
150168

151-
flags >>= 63 - 18;
152-
flags &= 7;
153-
if (flags == 0 || flags == 4)
154-
return H_PARAMETER;
155-
if (flags < 4) {
156-
if (vpa & 0x7f)
169+
subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
170+
if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
171+
subfunc == H_VPA_REG_SLB) {
172+
/* Registering new area - address must be cache-line aligned */
173+
if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
157174
return H_PARAMETER;
158-
if (flags >= 2 && !tvcpu->arch.vpa)
159-
return H_RESOURCE;
160-
/* registering new area; convert logical addr to real */
175+
176+
/* convert logical addr to kernel addr and read length */
161177
va = kvmppc_pin_guest_page(kvm, vpa, &nb);
162178
if (va == NULL)
163179
return H_PARAMETER;
164-
if (flags <= 1)
165-
len = *(unsigned short *)(va + 4);
180+
if (subfunc == H_VPA_REG_VPA)
181+
len = ((struct reg_vpa *)va)->length.hword;
166182
else
167-
len = *(unsigned int *)(va + 4);
168-
if (len > nb)
169-
goto out_unpin;
170-
switch (flags) {
171-
case 1: /* register VPA */
172-
if (len < 640)
173-
goto out_unpin;
174-
if (tvcpu->arch.vpa)
175-
kvmppc_unpin_guest_page(kvm, vcpu->arch.vpa);
176-
tvcpu->arch.vpa = va;
177-
init_vpa(vcpu, va);
178-
break;
179-
case 2: /* register DTL */
180-
if (len < 48)
181-
goto out_unpin;
182-
len -= len % 48;
183-
if (tvcpu->arch.dtl)
184-
kvmppc_unpin_guest_page(kvm, vcpu->arch.dtl);
185-
tvcpu->arch.dtl = va;
186-
tvcpu->arch.dtl_end = va + len;
183+
len = ((struct reg_vpa *)va)->length.word;
184+
kvmppc_unpin_guest_page(kvm, va);
185+
186+
/* Check length */
187+
if (len > nb || len < sizeof(struct reg_vpa))
188+
return H_PARAMETER;
189+
} else {
190+
vpa = 0;
191+
len = 0;
192+
}
193+
194+
err = H_PARAMETER;
195+
vpap = NULL;
196+
spin_lock(&tvcpu->arch.vpa_update_lock);
197+
198+
switch (subfunc) {
199+
case H_VPA_REG_VPA: /* register VPA */
200+
if (len < sizeof(struct lppaca))
187201
break;
188-
case 3: /* register SLB shadow buffer */
189-
if (len < 16)
190-
goto out_unpin;
191-
if (tvcpu->arch.slb_shadow)
192-
kvmppc_unpin_guest_page(kvm, vcpu->arch.slb_shadow);
193-
tvcpu->arch.slb_shadow = va;
202+
vpap = &tvcpu->arch.vpa;
203+
err = 0;
204+
break;
205+
206+
case H_VPA_REG_DTL: /* register DTL */
207+
if (len < sizeof(struct dtl_entry))
194208
break;
195-
}
196-
} else {
197-
switch (flags) {
198-
case 5: /* unregister VPA */
199-
if (tvcpu->arch.slb_shadow || tvcpu->arch.dtl)
200-
return H_RESOURCE;
201-
if (!tvcpu->arch.vpa)
202-
break;
203-
kvmppc_unpin_guest_page(kvm, tvcpu->arch.vpa);
204-
tvcpu->arch.vpa = NULL;
209+
len -= len % sizeof(struct dtl_entry);
210+
211+
/* Check that they have previously registered a VPA */
212+
err = H_RESOURCE;
213+
if (!vpa_is_registered(&tvcpu->arch.vpa))
205214
break;
206-
case 6: /* unregister DTL */
207-
if (!tvcpu->arch.dtl)
208-
break;
209-
kvmppc_unpin_guest_page(kvm, tvcpu->arch.dtl);
210-
tvcpu->arch.dtl = NULL;
215+
216+
vpap = &tvcpu->arch.dtl;
217+
err = 0;
218+
break;
219+
220+
case H_VPA_REG_SLB: /* register SLB shadow buffer */
221+
/* Check that they have previously registered a VPA */
222+
err = H_RESOURCE;
223+
if (!vpa_is_registered(&tvcpu->arch.vpa))
211224
break;
212-
case 7: /* unregister SLB shadow buffer */
213-
if (!tvcpu->arch.slb_shadow)
214-
break;
215-
kvmppc_unpin_guest_page(kvm, tvcpu->arch.slb_shadow);
216-
tvcpu->arch.slb_shadow = NULL;
225+
226+
vpap = &tvcpu->arch.slb_shadow;
227+
err = 0;
228+
break;
229+
230+
case H_VPA_DEREG_VPA: /* deregister VPA */
231+
/* Check they don't still have a DTL or SLB buf registered */
232+
err = H_RESOURCE;
233+
if (vpa_is_registered(&tvcpu->arch.dtl) ||
234+
vpa_is_registered(&tvcpu->arch.slb_shadow))
217235
break;
218-
}
236+
237+
vpap = &tvcpu->arch.vpa;
238+
err = 0;
239+
break;
240+
241+
case H_VPA_DEREG_DTL: /* deregister DTL */
242+
vpap = &tvcpu->arch.dtl;
243+
err = 0;
244+
break;
245+
246+
case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
247+
vpap = &tvcpu->arch.slb_shadow;
248+
err = 0;
249+
break;
219250
}
220-
return H_SUCCESS;
221251

222-
out_unpin:
223-
kvmppc_unpin_guest_page(kvm, va);
252+
if (vpap) {
253+
vpap->next_gpa = vpa;
254+
vpap->len = len;
255+
vpap->update_pending = 1;
256+
}
257+
258+
spin_unlock(&tvcpu->arch.vpa_update_lock);
259+
224260
return err;
225261
}
226262

263+
static void kvmppc_update_vpa(struct kvm *kvm, struct kvmppc_vpa *vpap)
264+
{
265+
void *va;
266+
unsigned long nb;
267+
268+
vpap->update_pending = 0;
269+
va = NULL;
270+
if (vpap->next_gpa) {
271+
va = kvmppc_pin_guest_page(kvm, vpap->next_gpa, &nb);
272+
if (nb < vpap->len) {
273+
/*
274+
* If it's now too short, it must be that userspace
275+
* has changed the mappings underlying guest memory,
276+
* so unregister the region.
277+
*/
278+
kvmppc_unpin_guest_page(kvm, va);
279+
va = NULL;
280+
}
281+
}
282+
if (vpap->pinned_addr)
283+
kvmppc_unpin_guest_page(kvm, vpap->pinned_addr);
284+
vpap->pinned_addr = va;
285+
if (va)
286+
vpap->pinned_end = va + vpap->len;
287+
}
288+
289+
static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
290+
{
291+
struct kvm *kvm = vcpu->kvm;
292+
293+
spin_lock(&vcpu->arch.vpa_update_lock);
294+
if (vcpu->arch.vpa.update_pending) {
295+
kvmppc_update_vpa(kvm, &vcpu->arch.vpa);
296+
init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
297+
}
298+
if (vcpu->arch.dtl.update_pending) {
299+
kvmppc_update_vpa(kvm, &vcpu->arch.dtl);
300+
vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
301+
vcpu->arch.dtl_index = 0;
302+
}
303+
if (vcpu->arch.slb_shadow.update_pending)
304+
kvmppc_update_vpa(kvm, &vcpu->arch.slb_shadow);
305+
spin_unlock(&vcpu->arch.vpa_update_lock);
306+
}
307+
227308
int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
228309
{
229310
unsigned long req = kvmppc_get_gpr(vcpu, 3);
@@ -468,6 +549,7 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
468549
/* default to host PVR, since we can't spoof it */
469550
vcpu->arch.pvr = mfspr(SPRN_PVR);
470551
kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
552+
spin_lock_init(&vcpu->arch.vpa_update_lock);
471553

472554
kvmppc_mmu_book3s_hv_init(vcpu);
473555

@@ -512,12 +594,14 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
512594

513595
void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
514596
{
515-
if (vcpu->arch.dtl)
516-
kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.dtl);
517-
if (vcpu->arch.slb_shadow)
518-
kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.slb_shadow);
519-
if (vcpu->arch.vpa)
520-
kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.vpa);
597+
spin_lock(&vcpu->arch.vpa_update_lock);
598+
if (vcpu->arch.dtl.pinned_addr)
599+
kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.dtl.pinned_addr);
600+
if (vcpu->arch.slb_shadow.pinned_addr)
601+
kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.slb_shadow.pinned_addr);
602+
if (vcpu->arch.vpa.pinned_addr)
603+
kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.vpa.pinned_addr);
604+
spin_unlock(&vcpu->arch.vpa_update_lock);
521605
kvm_vcpu_uninit(vcpu);
522606
kmem_cache_free(kvm_vcpu_cache, vcpu);
523607
}
@@ -722,8 +806,13 @@ static int kvmppc_run_core(struct kvmppc_vcore *vc)
722806
vc->in_guest = 0;
723807
vc->pcpu = smp_processor_id();
724808
vc->napping_threads = 0;
725-
list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
809+
list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
726810
kvmppc_start_thread(vcpu);
811+
if (vcpu->arch.vpa.update_pending ||
812+
vcpu->arch.slb_shadow.update_pending ||
813+
vcpu->arch.dtl.update_pending)
814+
kvmppc_update_vpas(vcpu);
815+
}
727816
/* Grab any remaining hw threads so they can't go into the kernel */
728817
for (i = ptid; i < threads_per_core; ++i)
729818
kvmppc_grab_hwthread(vc->pcpu + i);

0 commit comments

Comments
 (0)