Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp1487389rwi; Thu, 13 Oct 2022 14:35:28 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6LIDtIMNq0132ib0Z/og4ANXPy97/Q4YT2Y5N0E/e5UacBd1UPrm+wOng5v1zvYWnEob+Y X-Received: by 2002:a17:907:2c74:b0:78d:845a:1cf9 with SMTP id ib20-20020a1709072c7400b0078d845a1cf9mr1222083ejc.359.1665696928095; Thu, 13 Oct 2022 14:35:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665696928; cv=none; d=google.com; s=arc-20160816; b=a2v7LmR6zp/07PdLA1y1zxMMCmx5Ty+EMh3HqLAZfggomjbD4TfMU2tGyxIO85k3As LjcReq7TDKeSjiRluzvyLvdEPV/tbKqbCME8dxwjiijSYxK+klZHTQxIvvB/CYdm9wrU hslAMkCiMUs40rnc5fBFeQ0cA0FkebLQrqm+PC1C2MyJDU7PbTWa6Ojtd8egOIajhynM VJnOQ8LMXpKvZVSbG/VEYIFm6CA6+bgfv7O9WMgSTzPi9Y3ZMWeWnpq4p9MvxZSKYjyx /xQqK2BanIKGs3Py4GfnasRPseal7HlHETZ44f6GDIPvE+vLhKXAXbdSBSSbryQpKy6u xlgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date:reply-to :dkim-signature; bh=N+ZLsWQWrbTeNoSHVahwYLBX7L/YFHf5HDS9U7VFCCo=; b=sgjqX7lvf1JAoqPcO5NTEDLsVng9lhJb5+SyThlYYfA1YyergrVOkHSWPytmOTDxJq dy5HYJythBpWI8xI77KA2iHfzT7dKFN4nZjiGAnCkqwPGkKpGhiFV7/vPldHO8LWdP3+ SJ1oblqNKessooztGAQrcHZErnjh51Fp+3L+anTGdiEnnPKfc6pSC4esT464dsI3RYOy 8AG/Qp9NiceXGgzp7Eap7UkGoZHiSrJTk4iC5Cdu83/d0ygjDUwqGAieRCCD2TRe2+pa numKM+oBdlOwapOWN2g0jdmBPlbzPNpsfsZRpD0rGkpoxPIGxjoVMfMH4lMUbmQDMJjc rI6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=FsxHbjOH; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o25-20020a170906289900b0078d4dcceb5dsi567570ejd.717.2022.10.13.14.34.40; Thu, 13 Oct 2022 14:35:28 -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=FsxHbjOH; 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 S230241AbiJMVOc (ORCPT + 99 others); Thu, 13 Oct 2022 17:14:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230179AbiJMVNq (ORCPT ); Thu, 13 Oct 2022 17:13:46 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BBB0419375C for ; Thu, 13 Oct 2022 14:13:35 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-3562f0fb5a7so28156457b3.1 for ; Thu, 13 Oct 2022 14:13:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=content-transfer-encoding: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=N+ZLsWQWrbTeNoSHVahwYLBX7L/YFHf5HDS9U7VFCCo=; b=FsxHbjOHYqiWQWKvKcoWJWpwm1TWzvwZwM7umrP8lrQyx512BCB/aj9APV57S1zRKv M3W57j19MhZJuBdQgrGaDEbFXEI0b50MpiTMzR8NLjjzCIOQALgZrDczMsSpRlULzv9X piO072IEBzh3t59bPz1frM+5kgdt7pNZCO4/PenZkNJ4BFmJssUs+ksH47Qq5hTKhhnO 0xQKj3lupz+pNlcziUvEtCWa97qMpPZXn/BWiqQbftslhGY9+YKuUbNoGp9dcsq3iDGL MJaUwH9n0jjDAttouYARLnARPc7rxM55KeHAEkG6JjJ60M4n7eO+ZlBv9dyPh1DaSjWS VrAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding: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=N+ZLsWQWrbTeNoSHVahwYLBX7L/YFHf5HDS9U7VFCCo=; b=DlsQOV3MAy3AqPpgM1ba3BLyxqzEUjVaFNMMGbb6/k84ZuKDmFRY90iqMXds5JIkrp C+O0/ZPmVtD3Ar9LGrqaB0K7Z7blTSI3TtMmfwmYG3r1z4JHGCQ6KbM9OdIe8t1xHXmx 8O4KFZT84q8TpH30guhXCHsSv5/d1iWez7ISb4YKBBpJ7kHAymPnwvmxNMKpsKpgzdF8 WeB2qcd4ahIxvA0/lmprXlMlqfVGKIBTJoKEYnwR44NTOqsV6afVXe1TcxD2W9NYRoxO MPHLgQsr2t1mWCG83C+50iTokHcs8V+3VRsCGy1yN5KSOGTIkF/d4CxRb9K9eKoiDwIp rtzg== X-Gm-Message-State: ACrzQf1+adEyk9P5Pzm009zptWX8TeRIvIvXung7KNowHKijQCLRf4Kl i/TDqByj6Vq3+tOCS23F08M48MQsgYk= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:99cf:0:b0:360:b7bd:64de with SMTP id q198-20020a8199cf000000b00360b7bd64demr1839403ywg.91.1665695577390; Thu, 13 Oct 2022 14:12:57 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 13 Oct 2022 21:12:29 +0000 In-Reply-To: <20221013211234.1318131-1-seanjc@google.com> Mime-Version: 1.0 References: <20221013211234.1318131-1-seanjc@google.com> X-Mailer: git-send-email 2.38.0.413.g74048e4d9e-goog Message-ID: <20221013211234.1318131-12-seanjc@google.com> Subject: [PATCH v2 11/16] KVM: Drop KVM's API to allow temprorarily unmapping gfn=>pfn cache From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Michal Luczaj , David Woodhouse Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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=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 Drop kvm_gpc_unmap() as it has no users and unclear requirements. The API was added as part of the original gfn_to_pfn_cache support, but its sole usage[*] was never merged. Fold the guts of kvm_gpc_unmap() into the deactivate path and drop the API. Omit acquiring refresh_lock as as concurrent calls to kvm_gpc_deactivate() are not allowed (this is not enforced, e.g. via lockdep. due to it being called during vCPU destruction). If/when temporary unmapping makes a comeback, the desirable behavior is likely to restrict temporary unmapping to vCPU-exclusive mappings and require the vcpu->mutex be held to serialize unmap. Use of the refresh_lock to protect unmapping was somewhat specuatively added by commit 93984f19e7bc ("KVM: Fully serialize gfn=3D>pfn cache refresh via mutex") to guard against concurrent unmaps, but the primary use case of the temporary unmap, nested virtualization[*], doesn't actually need or want concurrent unmaps. [*] https://lore.kernel.org/all/20211210163625.2886-7-dwmw2@infradead.org Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 12 ----------- virt/kvm/pfncache.c | 44 +++++++++++++++------------------------- 2 files changed, 16 insertions(+), 40 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index b63d2abbef56..22cf43389954 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1315,18 +1315,6 @@ bool kvm_gpc_check(struct gfn_to_pfn_cache *gpc, gpa= _t gpa); */ int kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa); =20 -/** - * kvm_gpc_unmap - temporarily unmap a gfn_to_pfn_cache. - * - * @kvm: pointer to kvm instance. - * @gpc: struct gfn_to_pfn_cache object. - * - * This unmaps the referenced page. The cache is left in the invalid state - * but at least the mapping from GPA to userspace HVA will remain cached - * and can be reused on a subsequent refresh. - */ -void kvm_gpc_unmap(struct kvm *kvm, struct gfn_to_pfn_cache *gpc); - /** * kvm_gpc_deactivate - deactivate and unlink a gfn_to_pfn_cache. * diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 432b150bd9f1..62b47feed36c 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -328,33 +328,6 @@ int kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_= t gpa) } EXPORT_SYMBOL_GPL(kvm_gpc_refresh); =20 -void kvm_gpc_unmap(struct kvm *kvm, struct gfn_to_pfn_cache *gpc) -{ - void *old_khva; - kvm_pfn_t old_pfn; - - mutex_lock(&gpc->refresh_lock); - write_lock_irq(&gpc->lock); - - gpc->valid =3D false; - - old_khva =3D gpc->khva - offset_in_page(gpc->khva); - old_pfn =3D gpc->pfn; - - /* - * We can leave the GPA =E2=86=92 uHVA map cache intact but the PFN - * lookup will need to be redone even for the same page. - */ - gpc->khva =3D NULL; - gpc->pfn =3D KVM_PFN_ERR_FAULT; - - write_unlock_irq(&gpc->lock); - mutex_unlock(&gpc->refresh_lock); - - gpc_unmap_khva(old_pfn, old_khva); -} -EXPORT_SYMBOL_GPL(kvm_gpc_unmap); - void kvm_gpc_init(struct gfn_to_pfn_cache *gpc, struct kvm *kvm, struct kvm_vcpu *vcpu, enum pfn_cache_usage usage, unsigned long len) @@ -402,6 +375,8 @@ EXPORT_SYMBOL_GPL(kvm_gpc_activate); void kvm_gpc_deactivate(struct gfn_to_pfn_cache *gpc) { struct kvm *kvm =3D gpc->kvm; + kvm_pfn_t old_pfn; + void *old_khva; =20 if (gpc->active) { /* @@ -411,13 +386,26 @@ void kvm_gpc_deactivate(struct gfn_to_pfn_cache *gpc) */ write_lock_irq(&gpc->lock); gpc->active =3D false; + gpc->valid =3D false; + + /* + * Leave the GPA =3D> uHVA cache intact, it's protected by the + * memslot generation. The PFN lookup needs to be redone every + * time as mmu_notifier protection is lost when the cache is + * removed from the VM's gpc_list. + */ + old_khva =3D gpc->khva - offset_in_page(gpc->khva); + gpc->khva =3D NULL; + + old_pfn =3D gpc->pfn; + gpc->pfn =3D KVM_PFN_ERR_FAULT; write_unlock_irq(&gpc->lock); =20 spin_lock(&kvm->gpc_lock); list_del(&gpc->list); spin_unlock(&kvm->gpc_lock); =20 - kvm_gpc_unmap(kvm, gpc); + gpc_unmap_khva(old_pfn, old_khva); } } EXPORT_SYMBOL_GPL(kvm_gpc_deactivate); --=20 2.38.0.413.g74048e4d9e-goog