From patchwork Thu Sep 14 08:49:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 139463 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp220616vqi; Thu, 14 Sep 2023 02:27:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH0fsG7a/jBkeCqXr5vUKrT+rUtKy1jtY/GoENp1Ln4OEqc90ni7ckfGA8DoY7IqQffJUCf X-Received: by 2002:a67:fb4a:0:b0:44d:4c28:55ca with SMTP id e10-20020a67fb4a000000b0044d4c2855camr4628306vsr.16.1694683648872; Thu, 14 Sep 2023 02:27:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694683648; cv=none; d=google.com; s=arc-20160816; b=Lj+D8vIwwiH51zzrpSfSeUi6GPu0KDnobmAR7DKxJN6cwnKQK34jx71jfW60LjCDxE 9JrrmVj1cfTJT/+IMO4TlDieotQ7uG1IJfyatuFv6lc3hn6OaOAAL3ri68/i4vzehkmw Iqsa+LfG9bDPULFOGTX0qseWJBDCANkcEJQEgG7o2ctZ0mR2ygkDXH0jEp+f54V6r8jg Pp8rhVAPmB+0l0zjvwW8HqkrDYdEK+75P7yidYnbfZ8CKil61Y5PiiqrkekYYcosvLIp +TvEgw3B/LlsIJpBvtP2/FHjdrvk6hcZBbiEEPBu8dTevp1qm9FNL0B/73qW6vbqqMpq FOPg== 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=Vg4nybFPifYv4mCdEydLFQh8wTvl7sWSN8mffLcAAaU=; fh=garbXx6xThcytiY3nDlsqHTysvuL2ydFqtzT1pbUU/Y=; b=ZcBY+apvIRa3FS0oxQbOKURgBCzZq0M20du8G4iGCLJXZW45xvMyptbvYf9FqYmrfy FcgQUGydnteBeNOTKlt6BkQ3iZvZo9ZS6c9ucaI79vDLErwEgExHW2ybU/iE1KQXaYg0 YIXj3xxGNMbzHjX91TQTNJkvzVVwb+5Y+u7wdJdExfdSMmuyBG0VKospsNGF5b0j2xj/ 74slMO3q59i7uirCWphdUJ+uRAXBChpYSYaC4To78uW8M9/LKwiqlo5FNzzg/PBO6Hqf fTj4xRIx1KINJJk2fERtYFalk7uedkjrqri7CZDCTldWvyPJ2Gjw+giRBR9BdFsh+sb7 ibTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b="Q/8QIj5b"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id 140-20020a630192000000b0056554a8edb1si1151705pgb.219.2023.09.14.02.27.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 02:27:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b="Q/8QIj5b"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (Postfix) with ESMTP id E57C981C524C; Thu, 14 Sep 2023 02:07:34 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236998AbjINJHX (ORCPT + 35 others); Thu, 14 Sep 2023 05:07:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236840AbjINJHP (ORCPT ); Thu, 14 Sep 2023 05:07:15 -0400 X-Greylist: delayed 1013 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Thu, 14 Sep 2023 02:07:10 PDT Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05C7A1FEC; Thu, 14 Sep 2023 02:07:09 -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=Vg4nybFPifYv4mCdEydLFQh8wTvl7sWSN8mffLcAAaU=; b=Q/8QIj5be1SceDQi1cPfTcBAyx 3H4yVzw6rXOO6n4wDzHHnH+RRR7RzZYd7hXqLqssTdhW6JwDEv9T0pqHP6Y4znpK7OpohiOW/41Db L+30vbJxYAJkyusxE4GvnXaNEQhbbYZNEcxWd2WejYZermQUKYIlbVLVzol+EhF0Rx9I=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qgi3F-0001iU-DL; Thu, 14 Sep 2023 08:50:17 +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 1qgi3F-0002T9-58; Thu, 14 Sep 2023 08:50:17 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , Paolo Bonzini , David Woodhouse Subject: [PATCH 1/8] KVM: pfncache: add a map helper function Date: Thu, 14 Sep 2023 08:49:39 +0000 Message-Id: <20230914084946.200043-2-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230914084946.200043-1-paul@xen.org> References: <20230914084946.200043-1-paul@xen.org> MIME-Version: 1.0 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 (lipwig.vger.email [0.0.0.0]); Thu, 14 Sep 2023 02:07:35 -0700 (PDT) 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 lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777004601605518878 X-GMAIL-MSGID: 1777004601605518878 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. Signed-off-by: Paul Durrant Reviewed-by: David Woodhouse --- Cc: Paolo Bonzini Cc: David Woodhouse --- 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);