Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp783104ybl; Wed, 11 Dec 2019 07:27:26 -0800 (PST) X-Google-Smtp-Source: APXvYqyeac3Thpz0XcpzWKy851+RaWCqtkIeLwrEBwvFajvjIYIKDcp29A5/bco1LlEw73kNfO+3 X-Received: by 2002:a54:4485:: with SMTP id v5mr3056016oiv.144.1576078045916; Wed, 11 Dec 2019 07:27:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576078045; cv=none; d=google.com; s=arc-20160816; b=iVDOKrBsXTce9KzDwTu6A0R7qV8HPBdu8yKPVLa8tGh9dyRxX4TjJbUX8A0xV+XMWB lUS3R500Bm/P9rNu4gmeMX8IcFvSqVSvv1Fx6S0MmKDHGM1XuxddZDk8ODO90JZblClL jB8363TSIzlgDjBSXeCEwvilXgf7JzDsUbxUZlWsu4Zxo4pJUSdkMzhoeFBSMLn0iOl5 TtDaL8mamCbLpZss5fPDL1vdFRmqonceOO2OECR906M8h/qcRljB0o50NYzNy1JhfJ/Q yuFeEuXYUgkYBrFINENeJ8vZvcj9fwV+leuj/fMkLvDa9NdOQkCj7wbMtVM7CMNAbsnp Xu8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=9y9bpQu3ZPPE6hOZRg5WzsFTgUTQf/KDG3S68KrQAsU=; b=qFdvvZ6A5REYJmGvyZR/zWiVbBCyBWDzIia393P4mpwZmh0OOYwJ3jc7gMazKIj7+d LlXDNoWjfvCbG2j2qf8LyI3vKgaswE+Xc/chwEMLsUTTKOl5kOYi8qWM+4QtT9UQTJ/5 PfbwQEo42/PJZrsi3n7pblvZCDEAL9a4IOT11vV9mwUKpOqYf3lAnB5Gl6EXdILElYtx MjIlDbWsVrlqUVi7y9vGk6E/jnkvzI2VmINJc9r1SvyP0MqHlq8zGWEiWNK/JsaNWTrf xx2Nu5tyvaaKgNWl3tyyLF11kSRNmz+YXTCDi8XkwmsEFHUv0Wjpbw4kruP0YdnUJQGq iIyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=SRioxFq2; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w131si1355619oif.240.2019.12.11.07.27.14; Wed, 11 Dec 2019 07:27:25 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=SRioxFq2; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733166AbfLKP0m (ORCPT + 99 others); Wed, 11 Dec 2019 10:26:42 -0500 Received: from mail.kernel.org ([198.145.29.99]:60052 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733151AbfLKP0k (ORCPT ); Wed, 11 Dec 2019 10:26:40 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5839F2173E; Wed, 11 Dec 2019 15:26:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1576077999; bh=7HNuNUXEtkvELhgCo62JxHZPL/MBx2gnOhva4AopJsw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SRioxFq2XilQDqXdV/H6SPVTIWY82w+dFaftfI6OSRCfOV9BsRagma0gP3ywz946u uMCVS/blaHxyQwGdUeTcBdAKHrM9LCOuZ/p+qiG8Ns8gxQzFwpUW5Da6DP8PnEIHSY vKhZemfcsADdK1a6u7A4FEwIpN8J2jJoO8M12f4c= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sean Christopherson , Paolo Bonzini Subject: [PATCH 4.19 228/243] KVM: x86: Grab KVMs srcu lock when setting nested state Date: Wed, 11 Dec 2019 16:06:30 +0100 Message-Id: <20191211150354.726820634@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191211150339.185439726@linuxfoundation.org> References: <20191211150339.185439726@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson commit ad5996d9a0e8019c3ae5151e687939369acfe044 upstream. Acquire kvm->srcu for the duration of ->set_nested_state() to fix a bug where nVMX derefences ->memslots without holding ->srcu or ->slots_lock. The other half of nested migration, ->get_nested_state(), does not need to acquire ->srcu as it is a purely a dump of internal KVM (and CPU) state to userspace. Detected as an RCU lockdep splat that is 100% reproducible by running KVM's state_test selftest with CONFIG_PROVE_LOCKING=y. Note that the failing function, kvm_is_visible_gfn(), is only checking the validity of a gfn, it's not actually accessing guest memory (which is more or less unsupported during vmx_set_nested_state() due to incorrect MMU state), i.e. vmx_set_nested_state() itself isn't fundamentally broken. In any case, setting nested state isn't a fast path so there's no reason to go out of our way to avoid taking ->srcu. ============================= WARNING: suspicious RCU usage 5.4.0-rc7+ #94 Not tainted ----------------------------- include/linux/kvm_host.h:626 suspicious rcu_dereference_check() usage! other info that might help us debug this: rcu_scheduler_active = 2, debug_locks = 1 1 lock held by evmcs_test/10939: #0: ffff88826ffcb800 (&vcpu->mutex){+.+.}, at: kvm_vcpu_ioctl+0x85/0x630 [kvm] stack backtrace: CPU: 1 PID: 10939 Comm: evmcs_test Not tainted 5.4.0-rc7+ #94 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 Call Trace: dump_stack+0x68/0x9b kvm_is_visible_gfn+0x179/0x180 [kvm] mmu_check_root+0x11/0x30 [kvm] fast_cr3_switch+0x40/0x120 [kvm] kvm_mmu_new_cr3+0x34/0x60 [kvm] nested_vmx_load_cr3+0xbd/0x1f0 [kvm_intel] nested_vmx_enter_non_root_mode+0xab8/0x1d60 [kvm_intel] vmx_set_nested_state+0x256/0x340 [kvm_intel] kvm_arch_vcpu_ioctl+0x491/0x11a0 [kvm] kvm_vcpu_ioctl+0xde/0x630 [kvm] do_vfs_ioctl+0xa2/0x6c0 ksys_ioctl+0x66/0x70 __x64_sys_ioctl+0x16/0x20 do_syscall_64+0x54/0x200 entry_SYSCALL_64_after_hwframe+0x49/0xbe RIP: 0033:0x7f59a2b95f47 Fixes: 8fcc4b5923af5 ("kvm: nVMX: Introduce KVM_CAP_NESTED_STATE") Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/x86.c | 3 +++ 1 file changed, 3 insertions(+) --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4117,6 +4117,7 @@ long kvm_arch_vcpu_ioctl(struct file *fi case KVM_SET_NESTED_STATE: { struct kvm_nested_state __user *user_kvm_nested_state = argp; struct kvm_nested_state kvm_state; + int idx; r = -EINVAL; if (!kvm_x86_ops->set_nested_state) @@ -4138,7 +4139,9 @@ long kvm_arch_vcpu_ioctl(struct file *fi if (kvm_state.flags == KVM_STATE_NESTED_RUN_PENDING) break; + idx = srcu_read_lock(&vcpu->kvm->srcu); r = kvm_x86_ops->set_nested_state(vcpu, user_kvm_nested_state, &kvm_state); + srcu_read_unlock(&vcpu->kvm->srcu, idx); break; } default: