Skip to content

Commit 1508c22

Browse files
aikpaulusmack
authored andcommitted
KVM: PPC: Protect kvm_vcpu_read_guest with srcu locks
The kvm_vcpu_read_guest/kvm_vcpu_write_guest used for nested guests eventually call srcu_dereference_check to dereference a memslot and lockdep produces a warning as neither kvm->slots_lock nor kvm->srcu lock is held and kvm->users_count is above zero (>100 in fact). This wraps mentioned VCPU read/write helpers in srcu read lock/unlock as it is done in other places. This uses vcpu->srcu_idx when possible. These helpers are only used for nested KVM so this may explain why we did not see these before. Here is an example of a warning: ============================= WARNING: suspicious RCU usage 5.7.0-rc3-le_dma-bypass.3.2_a+fstn1 #897 Not tainted ----------------------------- include/linux/kvm_host.h:633 suspicious rcu_dereference_check() usage! other info that might help us debug this: rcu_scheduler_active = 2, debug_locks = 1 1 lock held by qemu-system-ppc/2752: #0: c000200359016be0 (&vcpu->mutex){+.+.}-{3:3}, at: kvm_vcpu_ioctl+0x144/0xd80 [kvm] stack backtrace: CPU: 80 PID: 2752 Comm: qemu-system-ppc Not tainted 5.7.0-rc3-le_dma-bypass.3.2_a+fstn1 #897 Call Trace: [c0002003591ab240] [c000000000b23ab4] dump_stack+0x190/0x25c (unreliable) [c0002003591ab2b0] [c00000000023f954] lockdep_rcu_suspicious+0x140/0x164 [c0002003591ab330] [c008000004a445f8] kvm_vcpu_gfn_to_memslot+0x4c0/0x510 [kvm] [c0002003591ab3a0] [c008000004a44c18] kvm_vcpu_read_guest+0xa0/0x180 [kvm] [c0002003591ab410] [c008000004ff9bd8] kvmhv_enter_nested_guest+0x90/0xb80 [kvm_hv] [c0002003591ab980] [c008000004fe07bc] kvmppc_pseries_do_hcall+0x7b4/0x1c30 [kvm_hv] [c0002003591aba10] [c008000004fe5d30] kvmppc_vcpu_run_hv+0x10a8/0x1a30 [kvm_hv] [c0002003591abae0] [c008000004a5d954] kvmppc_vcpu_run+0x4c/0x70 [kvm] [c0002003591abb10] [c008000004a56e54] kvm_arch_vcpu_ioctl_run+0x56c/0x7c0 [kvm] [c0002003591abba0] [c008000004a3ddc4] kvm_vcpu_ioctl+0x4ac/0xd80 [kvm] [c0002003591abd20] [c0000000006ebb58] ksys_ioctl+0x188/0x210 [c0002003591abd70] [c0000000006ebc28] sys_ioctl+0x48/0xb0 [c0002003591abdb0] [c000000000042764] system_call_exception+0x1d4/0x2e0 [c0002003591abe20] [c00000000000cce8] system_call_common+0xe8/0x214 Signed-off-by: Alexey Kardashevskiy <[email protected]> Signed-off-by: Paul Mackerras <[email protected]>
1 parent e55f4d5 commit 1508c22

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

arch/powerpc/kvm/book3s_64_mmu_radix.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ int kvmppc_mmu_walk_radix_tree(struct kvm_vcpu *vcpu, gva_t eaddr,
160160
return -EINVAL;
161161
/* Read the entry from guest memory */
162162
addr = base + (index * sizeof(rpte));
163+
vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
163164
ret = kvm_read_guest(kvm, addr, &rpte, sizeof(rpte));
165+
srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
164166
if (ret) {
165167
if (pte_ret_p)
166168
*pte_ret_p = addr;
@@ -236,7 +238,9 @@ int kvmppc_mmu_radix_translate_table(struct kvm_vcpu *vcpu, gva_t eaddr,
236238

237239
/* Read the table to find the root of the radix tree */
238240
ptbl = (table & PRTB_MASK) + (table_index * sizeof(entry));
241+
vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
239242
ret = kvm_read_guest(kvm, ptbl, &entry, sizeof(entry));
243+
srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
240244
if (ret)
241245
return ret;
242246

arch/powerpc/kvm/book3s_hv_nested.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -233,20 +233,21 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)
233233

234234
/* copy parameters in */
235235
hv_ptr = kvmppc_get_gpr(vcpu, 4);
236+
regs_ptr = kvmppc_get_gpr(vcpu, 5);
237+
vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
236238
err = kvm_vcpu_read_guest(vcpu, hv_ptr, &l2_hv,
237-
sizeof(struct hv_guest_state));
239+
sizeof(struct hv_guest_state)) ||
240+
kvm_vcpu_read_guest(vcpu, regs_ptr, &l2_regs,
241+
sizeof(struct pt_regs));
242+
srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
238243
if (err)
239244
return H_PARAMETER;
245+
240246
if (kvmppc_need_byteswap(vcpu))
241247
byteswap_hv_regs(&l2_hv);
242248
if (l2_hv.version != HV_GUEST_STATE_VERSION)
243249
return H_P2;
244250

245-
regs_ptr = kvmppc_get_gpr(vcpu, 5);
246-
err = kvm_vcpu_read_guest(vcpu, regs_ptr, &l2_regs,
247-
sizeof(struct pt_regs));
248-
if (err)
249-
return H_PARAMETER;
250251
if (kvmppc_need_byteswap(vcpu))
251252
byteswap_pt_regs(&l2_regs);
252253
if (l2_hv.vcpu_token >= NR_CPUS)
@@ -323,12 +324,12 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)
323324
byteswap_hv_regs(&l2_hv);
324325
byteswap_pt_regs(&l2_regs);
325326
}
327+
vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
326328
err = kvm_vcpu_write_guest(vcpu, hv_ptr, &l2_hv,
327-
sizeof(struct hv_guest_state));
328-
if (err)
329-
return H_AUTHORITY;
330-
err = kvm_vcpu_write_guest(vcpu, regs_ptr, &l2_regs,
329+
sizeof(struct hv_guest_state)) ||
330+
kvm_vcpu_write_guest(vcpu, regs_ptr, &l2_regs,
331331
sizeof(struct pt_regs));
332+
srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
332333
if (err)
333334
return H_AUTHORITY;
334335

@@ -508,12 +509,16 @@ long kvmhv_copy_tofrom_guest_nested(struct kvm_vcpu *vcpu)
508509
goto not_found;
509510

510511
/* Write what was loaded into our buffer back to the L1 guest */
512+
vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
511513
rc = kvm_vcpu_write_guest(vcpu, gp_to, buf, n);
514+
srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
512515
if (rc)
513516
goto not_found;
514517
} else {
515518
/* Load the data to be stored from the L1 guest into our buf */
519+
vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
516520
rc = kvm_vcpu_read_guest(vcpu, gp_from, buf, n);
521+
srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
517522
if (rc)
518523
goto not_found;
519524

@@ -548,9 +553,12 @@ static void kvmhv_update_ptbl_cache(struct kvm_nested_guest *gp)
548553

549554
ret = -EFAULT;
550555
ptbl_addr = (kvm->arch.l1_ptcr & PRTB_MASK) + (gp->l1_lpid << 4);
551-
if (gp->l1_lpid < (1ul << ((kvm->arch.l1_ptcr & PRTS_MASK) + 8)))
556+
if (gp->l1_lpid < (1ul << ((kvm->arch.l1_ptcr & PRTS_MASK) + 8))) {
557+
int srcu_idx = srcu_read_lock(&kvm->srcu);
552558
ret = kvm_read_guest(kvm, ptbl_addr,
553559
&ptbl_entry, sizeof(ptbl_entry));
560+
srcu_read_unlock(&kvm->srcu, srcu_idx);
561+
}
554562
if (ret) {
555563
gp->l1_gr_to_hr = 0;
556564
gp->process_table = 0;

arch/powerpc/kvm/book3s_rtas.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu)
229229
*/
230230
args_phys = kvmppc_get_gpr(vcpu, 4) & KVM_PAM;
231231

232+
vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
232233
rc = kvm_read_guest(vcpu->kvm, args_phys, &args, sizeof(args));
234+
srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
233235
if (rc)
234236
goto fail;
235237

arch/powerpc/kvm/powerpc.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,10 @@ int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
403403
return EMULATE_DONE;
404404
}
405405

406-
if (kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size))
406+
vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
407+
rc = kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size);
408+
srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
409+
if (rc)
407410
return EMULATE_DO_MMIO;
408411

409412
return EMULATE_DONE;

0 commit comments

Comments
 (0)