From patchwork Fri Jul 21 22:43:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 124155 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp538736vqg; Fri, 21 Jul 2023 17:17:06 -0700 (PDT) X-Google-Smtp-Source: APBJJlGPc/IPMdlYUdkw3h5dD2veJGKiXaeWzkMm7iO+GsIQdPha9RMVvV00+kYCqyUpz3EVxmjX X-Received: by 2002:a17:906:dc:b0:992:1233:9c45 with SMTP id 28-20020a17090600dc00b0099212339c45mr2921465eji.69.1689985026227; Fri, 21 Jul 2023 17:17:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689985026; cv=none; d=google.com; s=arc-20160816; b=ITQ7P++4BCK/8ITYMTyJPIxo9rxbZ/g6743tP1Idnqi4ekHpxlo2kVBwb646AJK1zV 7xvJVoYkl3xIbk4gkAT9EO7oA4YvgEGhCGEnGIEUiomvt6skYU4NH0ykXh38G9dLZsSJ OiTM6eX06lOnOuBjMQujo7BZobQFTxs2ccR74rfku/dR7FfCXkAqrwBd/iD9Rk5uIBo3 w4d1J/y5z9HXSyHkBWYTxKH6EYANorfIQeUKhwHD0kWTqmRAUfwVdHQA1kbc+Q/W60Kv clqAXAMfCpRuTMb3tZK+s6aBLN7FzptQQMnpzBX+ioAe/E2fWvxEF/GCZ2CBOkJC0ztG 4K4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=EqN+mkVwiz0JYePPsvbJE9Of00MhnNzfwNJUrTtqx34=; fh=T75m8JrM20jHSdKNPoe0+m/2mWMXPKQUUqmw3wpFotY=; b=SfWu09XAqOEE/iyOfkkJ7dbBjXNCAkj2uw5Wl/704ecIHsuvl5UCXlFYgPiHXS4M2+ yo4VtDTtyTf1qxH9mQZ49+mny0nGd+lkN9zedtpxeI2ebUJYNUvAJUbUiiKcfBq/khYt 6UPSUZ9eUz/jUQ1e5zhAOwEV7frol0m9xJ8mQfavwVl5dzN40TQWQInN8BO/sydNM4YW aV+1LmB/qqPzdfnNdyWMOjLuYNCsv9HnFqn6dmrreMTJWn1iCa/Ap/mjBssyy6FHXjtw UWQwNe+veXRpjEzWFzS/KBnIB4txE0m/1f80lkPaSg8IV2xQvW1zrtZ7blKYi0UY21Wd 7ebg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=uPbc6qjT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lh14-20020a170906f8ce00b00992be03096dsi2535077ejb.846.2023.07.21.17.16.42; Fri, 21 Jul 2023 17:17:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=uPbc6qjT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230476AbjGUWnp (ORCPT + 99 others); Fri, 21 Jul 2023 18:43:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230454AbjGUWnn (ORCPT ); Fri, 21 Jul 2023 18:43:43 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 184BC3A86 for ; Fri, 21 Jul 2023 15:43:42 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1b89e3715acso14187345ad.3 for ; Fri, 21 Jul 2023 15:43:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689979421; x=1690584221; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=EqN+mkVwiz0JYePPsvbJE9Of00MhnNzfwNJUrTtqx34=; b=uPbc6qjTF+37bLOVeLN5oBf/3QUeWpvQQj3Os3hzKIn+9w+stgDfD/wRvHH6vvS1vz Jdb1/CGaRKexplycNPtMGXN+Tg/RyJbt630Xs8IeS3SMFNv5PCsghbo+kYJFygNjCFpF sVNhgSsW83NY1COiSao1pWUPh1AtIrckRNYwIAGc+050XIyJvUElwzdbPqwTGeusV0wO MmoVQyd6hZXWLdmSuWxhFzejY1yhd7tNiH5rpZy3iuO0rqNdjsPMj90Dn4Tac+hSKGnn 8AHG8NIUVhg6+9rueuzf1vrfPuAWZzW4D3IejKxilXlQAwYf9OjIQqNCdXkMDdc9uB2Q IB8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689979421; x=1690584221; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=EqN+mkVwiz0JYePPsvbJE9Of00MhnNzfwNJUrTtqx34=; b=dW69igaIjmICSUFNCKQ+z5NFWJs4uG9eRy1nrwqbkjomzP+ibc1aIsGdjyDQXKZOeg 6PMpCXyq0h9Z7sR9aSALBnK/3YL0tSiZz/QAo8gzRDBEYQL3AaBFD3l2hSJ27UVDy0LE otQdEZG9dWhnDeuGv7MAY9jlUl6Vysk0+O2jlqdD22ZJmiBUEb0sqGox13i/Kq9bNafH SFgibSHTPTxjM9q8T9MR0Qrot2+3AIaKkKrrq6oHbkbjUR80YmcQBMZqP5bhk/K7mcmp x9+R+fmqOrTAy3ILmc9twWB6PGHWvF/DShnstM7Joftrn2VLlz6kC57YRWuxjc4wTOQB BMeQ== X-Gm-Message-State: ABy/qLZTuGL7qc3P9K0vuAdonxv+vaHxE4FJDkM6bDDnPNhCkaI/zwOw QAWnUyvwXwwMrnpIq84Kz6CKTDje33Q= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:32cd:b0:1a6:4ce8:3ed5 with SMTP id i13-20020a17090332cd00b001a64ce83ed5mr12524plr.4.1689979421568; Fri, 21 Jul 2023 15:43:41 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 21 Jul 2023 15:43:36 -0700 In-Reply-To: <20230721224337.2335137-1-seanjc@google.com> Mime-Version: 1.0 References: <20230721224337.2335137-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Message-ID: <20230721224337.2335137-2-seanjc@google.com> Subject: [PATCH 1/2] KVM: x86: Acquire SRCU read lock when handling fastpath MSR writes From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Greg Thelen , Aaron Lewis X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772077738794650496 X-GMAIL-MSGID: 1772077738794650496 Temporarily acquire kvm->srcu for read when potentially emulating WRMSR in the VM-Exit fastpath handler, as several of the common helpers used during emulation expect the caller to provide SRCU protection. E.g. if the guest is counting instructions retired, KVM will query the PMU event filter when stepping over the WRMSR. dump_stack+0x85/0xdf lockdep_rcu_suspicious+0x109/0x120 pmc_event_is_allowed+0x165/0x170 kvm_pmu_trigger_event+0xa5/0x190 handle_fastpath_set_msr_irqoff+0xca/0x1e0 svm_vcpu_run+0x5c3/0x7b0 [kvm_amd] vcpu_enter_guest+0x2108/0x2580 Alternatively, check_pmu_event_filter() could acquire kvm->srcu, but this isn't the first bug of this nature, e.g. see commit 5c30e8101e8d ("KVM: SVM: Skip WRMSR fastpath on VM-Exit if next RIP isn't valid"). Providing protection for the entirety of WRMSR emulation will allow reverting the aforementioned commit, and will avoid having to play whack-a-mole when new uses of SRCU-protected structures are inevitably added in common emulation helpers. Fixes: dfdeda67ea2d ("KVM: x86/pmu: Prevent the PMU from counting disallowed events") Reported-by: Greg Thelen Reported-by: Aaron Lewis Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a6b9bea62fb8..8c073a4af484 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2172,6 +2172,8 @@ fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu) u64 data; fastpath_t ret = EXIT_FASTPATH_NONE; + kvm_vcpu_srcu_read_lock(vcpu); + switch (msr) { case APIC_BASE_MSR + (APIC_ICR >> 4): data = kvm_read_edx_eax(vcpu); @@ -2194,6 +2196,8 @@ fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu) if (ret != EXIT_FASTPATH_NONE) trace_kvm_msr_write(msr, data); + kvm_vcpu_srcu_read_unlock(vcpu); + return ret; } EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff); From patchwork Fri Jul 21 22:43:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 124146 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp515634vqg; Fri, 21 Jul 2023 16:15:11 -0700 (PDT) X-Google-Smtp-Source: APBJJlEVmz5QN6jq/LqWLzC8Xxhop3l3tuAYOVBGth9S4H1w/AzZc6epSjk+X7+jJ95v9fGDYovJ X-Received: by 2002:a17:90b:4a51:b0:263:c247:6d1b with SMTP id lb17-20020a17090b4a5100b00263c2476d1bmr2849319pjb.40.1689981311113; Fri, 21 Jul 2023 16:15:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689981311; cv=none; d=google.com; s=arc-20160816; b=HC6f2QbXt2ef1xSgl4zy+ONZe5G8PP4KSmVYiX6vl2InOWKNQLE1YAW651j4n1iJ+v A1/2QyOyTnt/aj+88IyU83Wzxx7Nd3CihjlG0ZME7h7P+DCcZSK8snqG+wkIn9IAZID6 WQ0tcZpIBTqp8zRhUMyGm9yvDpMehh6MntUcaO07XRJoC0APUmyHj/kJfhm7yAlvBDCS 4V1mppue+SVK8LqjxqKUJSpzU/jAE8bp0NBDvtti/8nJoD/AgUkSj6yyIwblUqgRmlpX SKUQpauU/UHTZgbrS1+9f/j9K8CA7+nj8PhVyAw8aUJnKfU6GkG1EKsFVdA3dcnzXf7n yEEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=QLdjoAuH6ny9qcAXLDVVZFI8shq7PUiJbkoQWuRMVVg=; fh=T75m8JrM20jHSdKNPoe0+m/2mWMXPKQUUqmw3wpFotY=; b=dC3/Aa+rjKfwGRWNmdVrZi0NXfR4bWiVOWxyJOKQDBLE3BhZ9ksmFTQ7qCp1V0Y2WN GDwXpve87YR86E8pOKMbe9NXCCIaTN/2RGeJAYNSJVD2R0119hrk4n1auAtOhZTfoQ9i mSeouoige86EOVRE3THdMtASt+XFlVNTe6E6IH0GPlO9VYsXDdrPAoBS8e3Iq+qZPnhy htw92rY3RIqsn9+hHwQZngD0gEEWTXk7XtPVm4dEC46dBrOxC+lQhpllKDtYjWvY4ew0 3u0Vt2adAY3Ni8mB7qd7wufzsevN4RtSiZ2DH7lvN65ifux0YfrqEqmnAFg1DdZYoYWr rA1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=r7DayOcA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ng16-20020a17090b1a9000b002613720fcd5si7217157pjb.35.2023.07.21.16.14.58; Fri, 21 Jul 2023 16:15:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=r7DayOcA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230497AbjGUWns (ORCPT + 99 others); Fri, 21 Jul 2023 18:43:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230444AbjGUWno (ORCPT ); Fri, 21 Jul 2023 18:43:44 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04AD13A86 for ; Fri, 21 Jul 2023 15:43:44 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-2631231fed0so1289603a91.3 for ; Fri, 21 Jul 2023 15:43:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689979423; x=1690584223; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=QLdjoAuH6ny9qcAXLDVVZFI8shq7PUiJbkoQWuRMVVg=; b=r7DayOcA+ltTh6qjlaW24skIpW7xZ/eeZRZhOUL6eNDr49X0gtEPZCrp9ecJuvEyXa iyUjReMGUqms9AQmBsMakzpo5wZxxM68a5j76iaYZttCeKC4bgdGFfLa/BnQr3+jt8S+ +wXPqZcgfTEEk3m9HhS0BLqP24tj0/TPPTZGgXkBIKGL56mERUjJVbULQtda6tVhY5wQ eqhWSNgI0nNgQGqXEIaQ8wWdHVrZ1LWyxmvpoDZcT8PSdbhwYfJDwFHV3UfEtrr/WgNh /BQ+a0EeMxOH3Ukt17t90axDe3hzA33+A6yMeH5hU5EXAVwuIAeIrC8vgkArp/6Z5hks ynzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689979423; x=1690584223; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=QLdjoAuH6ny9qcAXLDVVZFI8shq7PUiJbkoQWuRMVVg=; b=fGejo3Ou5OiSxu9EHwkLxXSInj9NZp1gafnIgZPVjwldneBAz7rmlD+tb0MoCA7FTG CERcKNWKUl+bxLTTgvxpxSBl5EOtT2PbmWwrvywjVW/rtLyBRxsk4/z+GjQlXeuYhQL1 MzNIQb/Xkd/EcdwC6lHqQNgwyykOxyXjKSKozS8gIzm74S62Y9yzx0bcHHedCDJpThP3 qxXwcx1nUj+IzYkHO/ZeK6rcKoH8x0hsepMeeRfdOutfkOt6r5Bg1HucU3jHWnqZCflI ++Tak0gZhFO4L6IKVThBfJ9kf4rAHUIvylKJqvkvEHTt3Aqs+nxLlErsCA0uPCOSJ96g Ze4g== X-Gm-Message-State: ABy/qLaV1t4uLsuyvbkBRlnlsg2G4Ygd0g7CmjziIGpwAv28E8DO7NJh jDXzape6+oIS2opPtXcrmJnM1svS0CY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:f691:b0:1b8:a555:385d with SMTP id l17-20020a170902f69100b001b8a555385dmr13248plg.9.1689979423484; Fri, 21 Jul 2023 15:43:43 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 21 Jul 2023 15:43:37 -0700 In-Reply-To: <20230721224337.2335137-1-seanjc@google.com> Mime-Version: 1.0 References: <20230721224337.2335137-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Message-ID: <20230721224337.2335137-3-seanjc@google.com> Subject: [PATCH 2/2] Revert "KVM: SVM: Skip WRMSR fastpath on VM-Exit if next RIP isn't valid" From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Greg Thelen , Aaron Lewis X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772073843141630536 X-GMAIL-MSGID: 1772073843141630536 Now that handle_fastpath_set_msr_irqoff() acquires kvm->srcu, i.e. allows dereferencing memslots during WRMSR emulation, drop the requirement that "next RIP" is valid. In hindsight, acquiring kvm->srcu would have been a better fix than avoiding the pastpath, but at the time it was thought that accessing SRCU-protected data in the fastpath was a one-off edge case. This reverts commit 5c30e8101e8d5d020b1d7119117889756a6ed713. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index d381ad424554..cea08e5fa69b 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3986,14 +3986,8 @@ static int svm_vcpu_pre_run(struct kvm_vcpu *vcpu) static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu) { - struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control; - - /* - * Note, the next RIP must be provided as SRCU isn't held, i.e. KVM - * can't read guest memory (dereference memslots) to decode the WRMSR. - */ - if (control->exit_code == SVM_EXIT_MSR && control->exit_info_1 && - nrips && control->next_rip) + if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_MSR && + to_svm(vcpu)->vmcb->control.exit_info_1) return handle_fastpath_set_msr_irqoff(vcpu); return EXIT_FASTPATH_NONE;