Message ID | 20221102193020.1091939-1-bgardon@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp106179wru; Wed, 2 Nov 2022 12:48:47 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4ujd5ln9QrYCJe1ENLgFRqNpaL1rIdMS2FhL247glPJvnzm2WIpgNBfXpcX2mNQ/Ocdsn4 X-Received: by 2002:a17:907:3f13:b0:799:6aef:9837 with SMTP id hq19-20020a1709073f1300b007996aef9837mr25711069ejc.296.1667418527729; Wed, 02 Nov 2022 12:48:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667418527; cv=none; d=google.com; s=arc-20160816; b=crGJR1ouhntUE35cdgnpE+J82Fzurb2O/50uzxNaNwWndLK4/v4xvrylBAOv9Bu8cA fkVTiggcLm3MM7Yi70Lju7r62d8OD4GKfgZYsdLRGLgS/QJ82+/gGmfzd5PSgvdBz1ix ssfboy4XUKxclvw6PX2yYsu+jsChoVD/eu0irpjunvI4iRDxmFgPkZCmhs3ERu9EzQ4W 2Wvbs/ihRDvqQC4yYwKdPls9boDnJR2dxuqQ6qSkn/YPEbfqio6fkw7yy8xmPn8Jc2fS fPoR7N/AJ0OSRM64Bwg40L6v3BlMikao+edtR1mr/4z3K2OaX0Wvds2GFV9xNt2qCmug NHJQ== 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:mime-version:date :dkim-signature; bh=4mfNKilwFmWrqa2spYtO5Tj25NqUdCGG8fx/ELXfW3g=; b=ZoyfkRUQuSilJLqmkWOL86tavs29SXVerOEDexk3k/oTaOpqw02rVIQknu5G+OR5N3 v57BYU0QYRuaMb72sXA4C7MCOt8DY4hVVZCv+LUZe2cqsdNfCVn1eZvlosJG92qJgpFW MGiF9YOCWHwn3bFDI1OJLsUyNT1fbiOvK/WOhssG5dh8I/8AKvurBXXjSrmAcSM1EFwm u/dvdgHi+Jp4jePkPJF7wzz0BNguiCG9kJf7vPSWCEuYdkTICb8VxrPCd7dltr3XItLN AE0sk9bXRfzRKUqpvic46G5HNwz93RQcbrxkfnrSapqr8hF/eLwiy6jcY5+ZOAnqta0z AOjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=hhK6C71g; 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 y5-20020a056402358500b004614db9083csi20262913edc.313.2022.11.02.12.48.23; Wed, 02 Nov 2022 12:48:47 -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=20210112 header.b=hhK6C71g; 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 S231289AbiKBTa3 (ORCPT <rfc822;yves.mi.zy@gmail.com> + 99 others); Wed, 2 Nov 2022 15:30:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229640AbiKBTaZ (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 2 Nov 2022 15:30:25 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B42565CA for <linux-kernel@vger.kernel.org>; Wed, 2 Nov 2022 12:30:25 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id v18-20020a637a12000000b0046ed84b94efso9853178pgc.6 for <linux-kernel@vger.kernel.org>; Wed, 02 Nov 2022 12:30:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=4mfNKilwFmWrqa2spYtO5Tj25NqUdCGG8fx/ELXfW3g=; b=hhK6C71gMmgCuhyKE25DVYTKUHSUUpT5xEYkm1oWFozn/GMGCDvp3A4t4RmXNVxaaV YWZdMzV2pCTe90Zn/na6/OD8hPoDn73YxV4ek+H8NFrId8pLGs9gB15H/I13eQBbfx03 Zq6aRH94hVD2SytAxjwfY+Ri/baAKSJ3od+LHlYlOX9tmS6OfREqelFSknh3W5pxgFBQ qh6aafaql1y7CMtxFYjFwYw+YXOq7TNxfstUffyK70SfuaA5n68E6m6wAQ3ojftVQnMk EbYETIW/CyYkeJyEUoUKENpYhHAVRBVMKCmzAJZQIkUpXXbjG7IGT+BspHKzZoRUyKbw QGWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=4mfNKilwFmWrqa2spYtO5Tj25NqUdCGG8fx/ELXfW3g=; b=FHCTqy5Y423x4/Bs1TpYn4Qvm5VH1day5Ann8VFKVCFIppWXGJIvlGZ+14jynWmhJM wTtIHFo9GjQD372v/J73iL9Sgz+0IqmFz7kfDTOZAf6z1EOTtFyKXSSTukMZhNiTMhEm zXIBjrPHysMrbe+0a5YFH4l0h7Q3Pa/mQw1Czpa0i4zWhn8+r/ElV7mg9mblbXvjvfDk P60rRe1m0xTRLFTFpLWAv7DjzWXVAQ/lVcaum7QOPSmaV7nGtk+lISmsls8eaJRJYj26 fTWSQKE7ui/krXsUGww2A3TtfZJs+3stQQ4igeeaJujxeSHFnC+fwKnTDdrgaq6zsSKt AlJg== X-Gm-Message-State: ACrzQf0cJFwwaBfEegudgG7mhai1DEs+5CXXnZjsATEvfoRdaQJ0J0Jx u0AFMCIEXug14mniuaxpeml1kfEvRB4WhcGjamV2M2U58IQFZ931QshxgzVggq6+vPBS1rI3on+ 53IcimlOE+TmKuumCNFje25egulWR+ypOR4MybkY86r7jMbJfcHwFoEdzI2952aE9xEA6dEqW X-Received: from sweer.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:e45]) (user=bgardon job=sendgmr) by 2002:aa7:9099:0:b0:56a:d3f0:a013 with SMTP id i25-20020aa79099000000b0056ad3f0a013mr26991991pfa.26.1667417424554; Wed, 02 Nov 2022 12:30:24 -0700 (PDT) Date: Wed, 2 Nov 2022 19:30:20 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.38.1.431.g37b22c650d-goog Message-ID: <20221102193020.1091939-1-bgardon@google.com> Subject: [PATCH] KVM: x86: Use SRCU to protect zap in __kvm_set_or_clear_apicv_inhibit From: Ben Gardon <bgardon@google.com> To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini <pbonzini@redhat.com>, Sean Christopherson <seanjc@google.com>, David Matlack <dmatlack@google.com>, Anish Ghulati <aghulati@google.com>, Ben Gardon <bgardon@google.com> Content-Type: text/plain; charset="UTF-8" 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_NONE, SPF_HELO_NONE,SPF_PASS,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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748415049897664897?= X-GMAIL-MSGID: =?utf-8?q?1748415049897664897?= |
Series |
KVM: x86: Use SRCU to protect zap in __kvm_set_or_clear_apicv_inhibit
|
|
Commit Message
Ben Gardon
Nov. 2, 2022, 7:30 p.m. UTC
kvm_zap_gfn_range must be called in an SRCU read-critical section, but
there is no SRCU annotation in __kvm_set_or_clear_apicv_inhibit.
Add the needed SRCU annotation.
Tested: ran tools/testing/selftests/kvm/x86_64/debug_regs on a DBG
build. This patch causes the suspicious RCU warning to disappear.
Note that the warning is hit in __kvm_zap_rmaps, so
kvm_memslots_have_rmaps must return true in order for this to
repro (i.e. the TDP MMU must be off or nesting in use.)
Fixes: 36222b117e36 ("KVM: x86: don't disable APICv memslot when inhibited")
Signed-off-by: Ben Gardon <bgardon@google.com>
---
arch/x86/kvm/x86.c | 3 +++
1 file changed, 3 insertions(+)
Comments
On Wed, Nov 02, 2022, Ben Gardon wrote: > kvm_zap_gfn_range must be called in an SRCU read-critical section, but Please add parantheses when referencing functions, i.e. kvm_zap_gfn_range(). > there is no SRCU annotation in __kvm_set_or_clear_apicv_inhibit. __kvm_set_or_clear_apicv_inhibit() > Add the needed SRCU annotation. It's not an annotation, acquiring SRCU is very much functional code. > Tested: ran tools/testing/selftests/kvm/x86_64/debug_regs on a DBG > build. This patch causes the suspicious RCU warning to disappear. > Note that the warning is hit in __kvm_zap_rmaps, so > kvm_memslots_have_rmaps must return true in order for this to > repro (i.e. the TDP MMU must be off or nesting in use.) Please provide the stack trace or at least a verbal description of what paths can reach __kvm_set_or_clear_apicv_inhibit() without holding SRCU, i.e. explain why this bug isn't being hit left and right. E.g. Unconditionally take KVM's SRCU lock in __kvm_set_or_clear_apicv_inhibit() when zapping virtual APIC SPTEs. SRCU must be held when zapping SPTEs in shadow MMUs to protect the gfn=>memslot translation (the TDP MMU walks all roots and so doesn't dereference memslots). In most cases, the inhibits are updated during KVM_RUN and so SRCU is already held, but other ioctls() can also modify inhibits and don't acquire SRCU, e.g. KVM_SET_GUEST_DEBUG and KVM_SET_LAPIC. Acquire SRCU unconditionally to avoid playing whack-a-mole, as nesting SRCU locks is safe and this is not a hot path. > Fixes: 36222b117e36 ("KVM: x86: don't disable APICv memslot when inhibited") Reported-by? IIRC this originated in a syzkaller report?
On Wed, Nov 2, 2022 at 12:47 PM Sean Christopherson <seanjc@google.com> wrote: > > On Wed, Nov 02, 2022, Ben Gardon wrote: > > kvm_zap_gfn_range must be called in an SRCU read-critical section, but > > Please add parantheses when referencing functions, i.e. kvm_zap_gfn_range(). > > > there is no SRCU annotation in __kvm_set_or_clear_apicv_inhibit. > > __kvm_set_or_clear_apicv_inhibit() > > > Add the needed SRCU annotation. > > It's not an annotation, acquiring SRCU is very much functional code. Right, totally true. Will correct. > > > Tested: ran tools/testing/selftests/kvm/x86_64/debug_regs on a DBG > > build. This patch causes the suspicious RCU warning to disappear. > > Note that the warning is hit in __kvm_zap_rmaps, so > > kvm_memslots_have_rmaps must return true in order for this to > > repro (i.e. the TDP MMU must be off or nesting in use.) > > Please provide the stack trace or at least a verbal description of what paths > can reach __kvm_set_or_clear_apicv_inhibit() without holding SRCU, i.e. explain > why this bug isn't being hit left and right. > > E.g. > > Unconditionally take KVM's SRCU lock in __kvm_set_or_clear_apicv_inhibit() > when zapping virtual APIC SPTEs. SRCU must be held when zapping SPTEs in > shadow MMUs to protect the gfn=>memslot translation (the TDP MMU walks all > roots and so doesn't dereference memslots). > > In most cases, the inhibits are updated during KVM_RUN and so SRCU is > already held, but other ioctls() can also modify inhibits and don't > acquire SRCU, e.g. KVM_SET_GUEST_DEBUG and KVM_SET_LAPIC. Acquire SRCU > unconditionally to avoid playing whack-a-mole, as nesting SRCU locks is > safe and this is not a hot path. > > > Fixes: 36222b117e36 ("KVM: x86: don't disable APICv memslot when inhibited") > > Reported-by? IIRC this originated in a syzkaller report? This was found on an non-upstream Google kernel by Greg Thelen, but a great point. I'll credit him in v2.
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index cd9eb13e2ed7..6d853e5f683d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10091,7 +10091,10 @@ void __kvm_set_or_clear_apicv_inhibit(struct kvm *kvm, kvm->arch.apicv_inhibit_reasons = new; if (new) { unsigned long gfn = gpa_to_gfn(APIC_DEFAULT_PHYS_BASE); + int idx = srcu_read_lock(&kvm->srcu); + kvm_zap_gfn_range(kvm, gfn, gfn+1); + srcu_read_unlock(&kvm->srcu, idx); } } else { kvm->arch.apicv_inhibit_reasons = new;