From patchwork Tue Sep 26 12:20:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 144943 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp2009057vqu; Tue, 26 Sep 2023 08:40:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGmVAht7fwXfz0I6aI2MZtn0AxP3eOWxzAN1RxyOc9dcOrvty8gnPvDdVZRSh+dhdtpxDOS X-Received: by 2002:a05:6a20:ce94:b0:154:d3ac:2063 with SMTP id if20-20020a056a20ce9400b00154d3ac2063mr7426629pzb.27.1695742847973; Tue, 26 Sep 2023 08:40:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695742847; cv=none; d=google.com; s=arc-20160816; b=NEwbI9YQtOCLHDZRb/p48Dc7nc2Ga8EBlTgQ7w5mZzPMcjWRteVWKtiljCB9H5PHGu Ger9i4B188dSpMKy8ew9jOhXqA77s2JAEYBiTue60AVmIkluaxKHUetZ9wpV+SOv+Buc NEk3O1a1UByS7GZ61Ht+66rYigivdL+m9bBYALtemu7tblZV0SMI0KQ/63a5RU5ktb/i Bhzr++U8IMTbLRWOr0Utfzv+tTctXQEX9kZZ4LCSO/GO19nLoaw7tx+p52bJGaOWch1S v8wb3pXkN5Z19WXM5gx+Rq6kCZYqiirLOFt7m28ApBp7Q1QWtr4jVIy++fXv9Nix7cTT iGlQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=cQ02ZWi3OXJZ36oKzuEnX4ff/ZDsG5TQqSf2+fKM4a0=; fh=eFj9v25W9Wrh6fN+JLpeVaL0rwE8+rsOVorNY/OMc1Y=; b=D+i7KQ71IRRB+RV4iG3jxsy5XbZOrqsrsyEn2HlEJDQ8kt0K6RrJSNmpmJ2yZK6QBO jKEWETrtbBJ/5LVh3d3tCzPLUAjxnQVQzigFwIoJVJlbB3jj/QV/46myeB5RthHh0leN iyRchqRcNWMZMR7cOCRieb44uiH7ucZ9WWiIsL+g46H9xAWxE1YxY2T/9OAPzQMQC4+m 5Qa9u9mfTP2IUypQ/BGjoyDjzrQRtIqz2hi6TY5fbDVF/pXnvpI/K5utb3vx+qxhe/BV PTCOVIo7arPhFMzfrV/rF2c13dY6fiQUs+f9L8q6FEjfHJsMiGZMklstKOOlJfUfLPe6 X+uA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=yp0xE+8U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id 67-20020a17090a09c900b0026b42961a4asi12498558pjo.38.2023.09.26.08.40.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 08:40:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=yp0xE+8U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 283F480BCBB4; Tue, 26 Sep 2023 05:20:51 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234592AbjIZMUh (ORCPT + 28 others); Tue, 26 Sep 2023 08:20:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230231AbjIZMUc (ORCPT ); Tue, 26 Sep 2023 08:20:32 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F283EEB; Tue, 26 Sep 2023 05:20:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=cQ02ZWi3OXJZ36oKzuEnX4ff/ZDsG5TQqSf2+fKM4a0=; b=yp0xE+8U3XTGejS7Zwm5Mk8YxQ In1Ppxv0ffaaTWzgyYhb6JYemU6ltvY4A2sbSRmqz2mPtxKd3GydXpT8G86Dk9UGI9tfKoljeZmx6 Em8CLsiChKexK2MPxAmPXru8/U4g7t/LIURbSmOHFtZOcc5uhCY2ieyj/SJ1R1bacY3s=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ql737-0000Gc-R4; Tue, 26 Sep 2023 12:20:21 +0000 Received: from ec2-63-33-11-17.eu-west-1.compute.amazonaws.com ([63.33.11.17] helo=REM-PW02S00X.ant.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ql737-0001mF-F2; Tue, 26 Sep 2023 12:20:21 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , David Woodhouse , Sean Christopherson , David Woodhouse , Paolo Bonzini Subject: [PATCH v6 01/11] KVM: pfncache: add a map helper function Date: Tue, 26 Sep 2023 12:20:03 +0000 Message-Id: <20230926122013.867391-2-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230926122013.867391-1-paul@xen.org> References: <20230926122013.867391-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 26 Sep 2023 05:20:51 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778115252551406581 X-GMAIL-MSGID: 1778115252551406581 From: Paul Durrant We have an unmap helper but mapping is open-coded. Arguably this is fine because mapping is done in only one place, hva_to_pfn_retry(), but adding the helper does make that function more readable. No functional change intended. Signed-off-by: Paul Durrant Reviewed-by: David Woodhouse --- Cc: Sean Christopherson Cc: David Woodhouse Cc: Paolo Bonzini --- virt/kvm/pfncache.c | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 2d6aba677830..0f36acdf577f 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -96,17 +96,28 @@ bool kvm_gpc_check(struct gfn_to_pfn_cache *gpc, unsigned long len) } EXPORT_SYMBOL_GPL(kvm_gpc_check); -static void gpc_unmap_khva(kvm_pfn_t pfn, void *khva) +static void *gpc_map(kvm_pfn_t pfn) +{ + if (pfn_valid(pfn)) + return kmap(pfn_to_page(pfn)); +#ifdef CONFIG_HAS_IOMEM + else + return memremap(pfn_to_hpa(pfn), PAGE_SIZE, MEMREMAP_WB); +#endif +} + +static void gpc_unmap(kvm_pfn_t pfn, void *khva) { /* Unmap the old pfn/page if it was mapped before. */ - if (!is_error_noslot_pfn(pfn) && khva) { - if (pfn_valid(pfn)) - kunmap(pfn_to_page(pfn)); + if (is_error_noslot_pfn(pfn) || !khva) + return; + + if (pfn_valid(pfn)) + kunmap(pfn_to_page(pfn)); #ifdef CONFIG_HAS_IOMEM - else - memunmap(khva); + else + memunmap(khva); #endif - } } static inline bool mmu_notifier_retry_cache(struct kvm *kvm, unsigned long mmu_seq) @@ -175,7 +186,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) * the existing mapping and didn't create a new one. */ if (new_khva != old_khva) - gpc_unmap_khva(new_pfn, new_khva); + gpc_unmap(new_pfn, new_khva); kvm_release_pfn_clean(new_pfn); @@ -193,15 +204,11 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) * too must be done outside of gpc->lock! */ if (gpc->usage & KVM_HOST_USES_PFN) { - if (new_pfn == gpc->pfn) { + if (new_pfn == gpc->pfn) new_khva = old_khva; - } else if (pfn_valid(new_pfn)) { - new_khva = kmap(pfn_to_page(new_pfn)); -#ifdef CONFIG_HAS_IOMEM - } else { - new_khva = memremap(pfn_to_hpa(new_pfn), PAGE_SIZE, MEMREMAP_WB); -#endif - } + else + new_khva = gpc_map(new_pfn); + if (!new_khva) { kvm_release_pfn_clean(new_pfn); goto out_error; @@ -326,7 +333,7 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, mutex_unlock(&gpc->refresh_lock); if (unmap_old) - gpc_unmap_khva(old_pfn, old_khva); + gpc_unmap(old_pfn, old_khva); return ret; } @@ -412,7 +419,7 @@ void kvm_gpc_deactivate(struct gfn_to_pfn_cache *gpc) list_del(&gpc->list); spin_unlock(&kvm->gpc_lock); - gpc_unmap_khva(old_pfn, old_khva); + gpc_unmap(old_pfn, old_khva); } } EXPORT_SYMBOL_GPL(kvm_gpc_deactivate);