Message ID | 20231121180223.12484-8-paul@xen.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2b07:b0:403:3b70:6f57 with SMTP id io7csp827815vqb; Tue, 21 Nov 2023 10:23:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IFQJ69N3f3inXUE7FFwS/znNE/lcZ7ba7kx2pVY8l/Hd5+SmTOZ19N7zsyyROGmKTqbhbZn X-Received: by 2002:a9d:6185:0:b0:6b9:b600:589 with SMTP id g5-20020a9d6185000000b006b9b6000589mr99694otk.15.1700590995048; Tue, 21 Nov 2023 10:23:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700590995; cv=none; d=google.com; s=arc-20160816; b=N2ceoqx3QlRcCuquU2QyeIKTPHymjyciL0+21t5a2/wBXE+hXwyxv5sWVvCaWJz1b1 qMqsblZYkXWpEKIkuxzxTX1Mb/NVpXJSR5f6jQ7PFJmSCZfEnGT5HCEEn4D2V8dtRsqu 1r1ArVnfvI/hgBPK6APBQPW3Qlu8f7Vil588/2Vtn/7lgOFJhBQimsnJnKZ10qOzNdwT Fi8TOuOGBJOhgrLfA11UwrQTbxZNPrR4vw/Q9RhNniQwklTFaJXkKJWXiGKjkLsV8RoE qA7ZbgrBlZ2Qfam0Pbz2s2feECHbhi7ikBgZ+O2rEDuLvsIcduPPz/zAPa8TQpxNwoCQ DLDA== 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:to:from :dkim-signature; bh=ul4KEFASJpTyKeeWEaoEGIEJkAp76tc14RGcJ05EwiA=; fh=Cum1qOF0YrAzH/5yQjcCBfU63B+V0l01YtzsDBRUOAc=; b=NB+kTcAwDfgDq/FjQEMfzcv6XcUIXPHTS1PJ0sMzyzhQjaeHs4Sx6Vvf5e6PLRPBlv g6O8s9F4GPu+TF4U/12AAaXVkPSR6MOxZNqtwDNKM9upRS834yxRLRkL3zjnKiCMnQNv A6HlL8G9IKiU6FrQ2EQaWU8Ho8n+A7Y7VG/pbEPSSnAIRBb66SgjMtab1WCiKiMvVQ61 lCtE1VlnlSmdRXO2XxbOnx5i5wxBBcxFlchgJdSgcBAMeKZ4eBLb46qN7LLH/0GHRkm/ 2i7Rgxcf9tGx5Dd4xrw8Ci2PH/e3rR14W/f4fruuLkfq0ZWxNk2hB5Wn50SNNkzvCoch plpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=erG+F38a; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id bw32-20020a056a0204a000b005bddb4be6fcsi11779755pgb.520.2023.11.21.10.23.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Nov 2023 10:23:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=erG+F38a; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id 9A070801B6D7; Tue, 21 Nov 2023 10:03:57 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234495AbjKUSDq (ORCPT <rfc822;ouuuleilei@gmail.com> + 99 others); Tue, 21 Nov 2023 13:03:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234426AbjKUSDh (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 21 Nov 2023 13:03:37 -0500 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DEA3219A; Tue, 21 Nov 2023 10:03:30 -0800 (PST) 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:To:From; bh=ul4KEFASJpTyKeeWEaoEGIEJkAp76tc14RGcJ05EwiA=; b=erG+F38airXnNflww1t2so14R0 digkEX2GGR3Oi798xyp5sw6vC2tZbX4ylcCt7x4nGGkjR4bBmgYvyfqau4e86DRvrXx27nu+XFmBY JMwv2gRcUEJxXpJqzao6rE77ceGI0+DXOQAcUnN/D/NLKJPIPRG3SxK2qyUPYAWywauw=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from <paul@xen.org>) id 1r5V5f-00085K-UE; Tue, 21 Nov 2023 18:03:15 +0000 Received: from 54-240-197-231.amazon.com ([54.240.197.231] 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 <paul@xen.org>) id 1r5V5f-0004Z3-Le; Tue, 21 Nov 2023 18:03:15 +0000 From: Paul Durrant <paul@xen.org> To: David Woodhouse <dwmw2@infradead.org>, Paul Durrant <paul@xen.org>, Sean Christopherson <seanjc@google.com>, Paolo Bonzini <pbonzini@redhat.com>, Thomas Gleixner <tglx@linutronix.de>, Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>, Dave Hansen <dave.hansen@linux.intel.com>, x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v8 07/15] KVM: pfncache: include page offset in uhva and use it consistently Date: Tue, 21 Nov 2023 18:02:15 +0000 Message-Id: <20231121180223.12484-8-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231121180223.12484-1-paul@xen.org> References: <20231121180223.12484-1-paul@xen.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 21 Nov 2023 10:03:57 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783198903579005813 X-GMAIL-MSGID: 1783198903579005813 |
Series |
KVM: xen: update shared_info and vcpu_info handling
|
|
Commit Message
Paul Durrant
Nov. 21, 2023, 6:02 p.m. UTC
From: Paul Durrant <pdurrant@amazon.com> Currently the pfncache page offset is sometimes determined using the gpa and sometimes the khva, whilst the uhva is always page-aligned. After a subsequent patch is applied the gpa will not always be valid so adjust the code to include the page offset in the uhva and use it consistently as the source of truth. Also, where a page-aligned address is required, use PAGE_ALIGN_DOWN() for clarity. Signed-off-by: Paul Durrant <pdurrant@amazon.com> --- Cc: Sean Christopherson <seanjc@google.com> Cc: Paolo Bonzini <pbonzini@redhat.com> Cc: David Woodhouse <dwmw2@infradead.org> v8: - New in this version. --- virt/kvm/pfncache.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-)
Comments
On Tue, 2023-11-21 at 18:02 +0000, Paul Durrant wrote: > @@ -242,8 +242,7 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, > } > > old_pfn = gpc->pfn; > - old_khva = gpc->khva - offset_in_page(gpc->khva); > - old_uhva = gpc->uhva; > + old_khva = (void *)PAGE_ALIGN_DOWN((uintptr_t)gpc->khva); > > /* If the userspace HVA is invalid, refresh that first */ > if (gpc->gpa != gpa || gpc->generation != slots->generation || > @@ -259,13 +258,25 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, > ret = -EFAULT; > goto out; > } There's a subtle behaviour change here, isn't there? I'd *really* like you do say 'No functional change intended' where that is true, and then the absence of that sentence in this one would be meaningful. You are now calling hva_to_pfn_retry() even when the uhva page hasn't changed. Which is harmless and probably not important, but IIUC fixable by the addition of: + if (gpc->uhva != PAGE_ALIGN_DOWN(old_uhva)) > + hva_change = true; > + } else { > + /* > + * No need to do any re-mapping if the only thing that has > + * changed is the page offset. Just page align it to allow the > + * new offset to be added in. > + */ > + gpc->uhva = PAGE_ALIGN_DOWN(gpc->uhva); > } > > + /* Note: the offset must be correct before calling hva_to_pfn_retry() */ > + gpc->uhva += page_offset; > + > /* > * If the userspace HVA changed or the PFN was already invalid, > * drop the lock and do the HVA to PFN lookup again. > */ > - if (!gpc->valid || old_uhva != gpc->uhva) { > + if (!gpc->valid || hva_change) { > ret = hva_to_pfn_retry(gpc); > } else { > /* > -- But I don't really think it's that important if you can come up with a coherent justification for the change and note it in the commit message. So either way: Reviewed-by: David Woodhouse <dwmw@amazon.co.uk>
On Tue, Nov 21, 2023 at 06:02:15PM +0000, Paul Durrant wrote: > From: Paul Durrant <pdurrant@amazon.com> > > Currently the pfncache page offset is sometimes determined using the gpa > and sometimes the khva, whilst the uhva is always page-aligned. After a > subsequent patch is applied the gpa will not always be valid so adjust > the code to include the page offset in the uhva and use it consistently > as the source of truth. > > Also, where a page-aligned address is required, use PAGE_ALIGN_DOWN() > for clarity. > > Signed-off-by: Paul Durrant <pdurrant@amazon.com> > --- > Cc: Sean Christopherson <seanjc@google.com> > Cc: Paolo Bonzini <pbonzini@redhat.com> > Cc: David Woodhouse <dwmw2@infradead.org> > > v8: > - New in this version. > --- > virt/kvm/pfncache.c | 27 +++++++++++++++++++-------- > 1 file changed, 19 insertions(+), 8 deletions(-) > > diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c > index 0eeb034d0674..c545f6246501 100644 > --- a/virt/kvm/pfncache.c > +++ b/virt/kvm/pfncache.c > @@ -48,10 +48,10 @@ bool kvm_gpc_check(struct gfn_to_pfn_cache *gpc, unsigned long len) > if (!gpc->active) > return false; > > - if (offset_in_page(gpc->gpa) + len > PAGE_SIZE) > + if (gpc->generation != slots->generation || kvm_is_error_hva(gpc->uhva)) > return false; > > - if (gpc->generation != slots->generation || kvm_is_error_hva(gpc->uhva)) > + if (offset_in_page(gpc->uhva) + len > PAGE_SIZE) > return false; > > if (!gpc->valid) > @@ -119,7 +119,7 @@ static inline bool mmu_notifier_retry_cache(struct kvm *kvm, unsigned long mmu_s > static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) > { > /* Note, the new page offset may be different than the old! */ > - void *old_khva = gpc->khva - offset_in_page(gpc->khva); > + void *old_khva = (void *)PAGE_ALIGN_DOWN((uintptr_t)gpc->khva); > kvm_pfn_t new_pfn = KVM_PFN_ERR_FAULT; > void *new_khva = NULL; > unsigned long mmu_seq; > @@ -192,7 +192,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) > > gpc->valid = true; > gpc->pfn = new_pfn; > - gpc->khva = new_khva + offset_in_page(gpc->gpa); > + gpc->khva = new_khva + offset_in_page(gpc->uhva); > > /* > * Put the reference to the _new_ pfn. The pfn is now tracked by the > @@ -215,8 +215,8 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, > struct kvm_memslots *slots = kvm_memslots(gpc->kvm); > unsigned long page_offset = offset_in_page(gpa); > bool unmap_old = false; > - unsigned long old_uhva; > kvm_pfn_t old_pfn; > + bool hva_change = false; > void *old_khva; > int ret; > > @@ -242,8 +242,7 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, > } > > old_pfn = gpc->pfn; > - old_khva = gpc->khva - offset_in_page(gpc->khva); > - old_uhva = gpc->uhva; > + old_khva = (void *)PAGE_ALIGN_DOWN((uintptr_t)gpc->khva); > > /* If the userspace HVA is invalid, refresh that first */ > if (gpc->gpa != gpa || gpc->generation != slots->generation || > @@ -259,13 +258,25 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, > ret = -EFAULT; > goto out; > } > + > + hva_change = true; > + } else { > + /* > + * No need to do any re-mapping if the only thing that has > + * changed is the page offset. Just page align it to allow the > + * new offset to be added in. I don't understand how the uhva('s offset) could be changed when both gpa and slot are not changed. Maybe I have no knowledge of xen, but in later patch you said your uhva would never change... Thanks, Yilun > + */ > + gpc->uhva = PAGE_ALIGN_DOWN(gpc->uhva); > } > > + /* Note: the offset must be correct before calling hva_to_pfn_retry() */ > + gpc->uhva += page_offset; > + > /* > * If the userspace HVA changed or the PFN was already invalid, > * drop the lock and do the HVA to PFN lookup again. > */ > - if (!gpc->valid || old_uhva != gpc->uhva) { > + if (!gpc->valid || hva_change) { > ret = hva_to_pfn_retry(gpc); > } else { > /* > -- > 2.39.2 > >
On Wed, 2023-11-22 at 16:54 +0800, Xu Yilun wrote: > > > @@ -259,13 +258,25 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, > > ret = -EFAULT; > > goto out; > > } > > + > > + hva_change = true; > > + } else { > > + /* > > + * No need to do any re-mapping if the only thing that has > > + * changed is the page offset. Just page align it to allow the > > + * new offset to be added in. > > I don't understand how the uhva('s offset) could be changed when both gpa and > slot are not changed. Maybe I have no knowledge of xen, but in later > patch you said your uhva would never change... It doesn't change on a normal refresh with kvm_gpc_refresh(), which is just for revalidation after memslot changes or MMU invalidation. But it can change if the gpc is being reinitialized with a new address (perhaps because the guest has made another hypercall to set the address, etc.) That new address could happen to be in the *same* page as the previous one. In fact the xen_shinfo_test explicitly tests that case, IIRC. And kvm_gpc_activate() also happens to use __kvm_gpc_refresh() internally.
On 21/11/2023 22:35, David Woodhouse wrote: > On Tue, 2023-11-21 at 18:02 +0000, Paul Durrant wrote: >> @@ -242,8 +242,7 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, >> } >> >> old_pfn = gpc->pfn; >> - old_khva = gpc->khva - offset_in_page(gpc->khva); >> - old_uhva = gpc->uhva; >> + old_khva = (void *)PAGE_ALIGN_DOWN((uintptr_t)gpc->khva); >> >> /* If the userspace HVA is invalid, refresh that first */ >> if (gpc->gpa != gpa || gpc->generation != slots->generation || >> @@ -259,13 +258,25 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, >> ret = -EFAULT; >> goto out; >> } > > > There's a subtle behaviour change here, isn't there? I'd *really* like > you do say 'No functional change intended' where that is true, and then > the absence of that sentence in this one would be meaningful. > > You are now calling hva_to_pfn_retry() even when the uhva page hasn't > changed. Which is harmless and probably not important, but IIUC fixable > by the addition of: > > + if (gpc->uhva != PAGE_ALIGN_DOWN(old_uhva)) True; I can keep that optimization and then I will indeed add 'no functional change'... Didn't seem worth it at the time, but no harm. >> + hva_change = true; >> + } else { >> + /* >> + * No need to do any re-mapping if the only thing that has >> + * changed is the page offset. Just page align it to allow the >> + * new offset to be added in. >> + */ >> + gpc->uhva = PAGE_ALIGN_DOWN(gpc->uhva); >> } >> >> + /* Note: the offset must be correct before calling hva_to_pfn_retry() */ >> + gpc->uhva += page_offset; >> + >> /* >> * If the userspace HVA changed or the PFN was already invalid, >> * drop the lock and do the HVA to PFN lookup again. >> */ >> - if (!gpc->valid || old_uhva != gpc->uhva) { >> + if (!gpc->valid || hva_change) { >> ret = hva_to_pfn_retry(gpc); >> } else { >> /* >> -- > > But I don't really think it's that important if you can come up with a > coherent justification for the change and note it in the commit > message. So either way: > > Reviewed-by: David Woodhouse <dwmw@amazon.co.uk> Thanks, Paul
On Wed, Nov 22, 2023 at 09:12:18AM +0000, David Woodhouse wrote: > On Wed, 2023-11-22 at 16:54 +0800, Xu Yilun wrote: > > > > > @@ -259,13 +258,25 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, > > > ret = -EFAULT; > > > goto out; > > > } > > > + > > > + hva_change = true; > > > + } else { > > > + /* > > > + * No need to do any re-mapping if the only thing that has > > > + * changed is the page offset. Just page align it to allow the > > > + * new offset to be added in. > > > > I don't understand how the uhva('s offset) could be changed when both gpa and > > slot are not changed. Maybe I have no knowledge of xen, but in later > > patch you said your uhva would never change... > > It doesn't change on a normal refresh with kvm_gpc_refresh(), which is > just for revalidation after memslot changes or MMU invalidation. > > But it can change if the gpc is being reinitialized with a new address > (perhaps because the guest has made another hypercall to set the > address, etc.) > > That new address could happen to be in the *same* page as the previous In this case, the lower bits of new gpa should be different to gpc->gpa, so will hit "if (gpc->gpa != gpa ...)" branch. And I see this comment is deleted in v9, which makes sense to me. Thanks, Yilun > one. In fact the xen_shinfo_test explicitly tests that case, IIRC. > > And kvm_gpc_activate() also happens to use __kvm_gpc_refresh() > internally.
On Wed, 2023-11-22 at 22:27 +0800, Xu Yilun wrote: > On Wed, Nov 22, 2023 at 09:12:18AM +0000, David Woodhouse wrote: > > On Wed, 2023-11-22 at 16:54 +0800, Xu Yilun wrote: > > > > > > > @@ -259,13 +258,25 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, > > > > ret = -EFAULT; > > > > goto out; > > > > } > > > > + > > > > + hva_change = true; > > > > + } else { > > > > + /* > > > > + * No need to do any re-mapping if the only thing that has > > > > + * changed is the page offset. Just page align it to allow the > > > > + * new offset to be added in. > > > > > > I don't understand how the uhva('s offset) could be changed when both gpa and > > > slot are not changed. Maybe I have no knowledge of xen, but in later > > > patch you said your uhva would never change... > > > > It doesn't change on a normal refresh with kvm_gpc_refresh(), which is > > just for revalidation after memslot changes or MMU invalidation. > > > > But it can change if the gpc is being reinitialized with a new address > > (perhaps because the guest has made another hypercall to set the > > address, etc.) > > > > That new address could happen to be in the *same* page as the previous > > In this case, the lower bits of new gpa should be different to gpc->gpa, > so will hit "if (gpc->gpa != gpa ...)" branch. I think that 'if (gpc->gpa != gpa); branch is also gratuitously refreshing when it doesn't need to; it only needs to refresh if the *aligned* gpas don't match. But it was like that already, so I won't heckle Paul any further :)
On 22/11/2023 15:42, David Woodhouse wrote: > On Wed, 2023-11-22 at 22:27 +0800, Xu Yilun wrote: >> On Wed, Nov 22, 2023 at 09:12:18AM +0000, David Woodhouse wrote: >>> On Wed, 2023-11-22 at 16:54 +0800, Xu Yilun wrote: >>>> >>>>> @@ -259,13 +258,25 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, >>>>> ret = -EFAULT; >>>>> goto out; >>>>> } >>>>> + >>>>> + hva_change = true; >>>>> + } else { >>>>> + /* >>>>> + * No need to do any re-mapping if the only thing that has >>>>> + * changed is the page offset. Just page align it to allow the >>>>> + * new offset to be added in. >>>> >>>> I don't understand how the uhva('s offset) could be changed when both gpa and >>>> slot are not changed. Maybe I have no knowledge of xen, but in later >>>> patch you said your uhva would never change... >>> >>> It doesn't change on a normal refresh with kvm_gpc_refresh(), which is >>> just for revalidation after memslot changes or MMU invalidation. >>> >>> But it can change if the gpc is being reinitialized with a new address >>> (perhaps because the guest has made another hypercall to set the >>> address, etc.) >>> >>> That new address could happen to be in the *same* page as the previous >> >> In this case, the lower bits of new gpa should be different to gpc->gpa, >> so will hit "if (gpc->gpa != gpa ...)" branch. > > I think that 'if (gpc->gpa != gpa); branch is also gratuitously > refreshing when it doesn't need to; it only needs to refresh if the > *aligned* gpas don't match. > I did look at that but decided that gfn_to_hva_memslot() was sufficiently lightweight that it was not really worth optimising. > But it was like that already, so I won't heckle Paul any further :) I appreciate it! :-) Paul
diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 0eeb034d0674..c545f6246501 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -48,10 +48,10 @@ bool kvm_gpc_check(struct gfn_to_pfn_cache *gpc, unsigned long len) if (!gpc->active) return false; - if (offset_in_page(gpc->gpa) + len > PAGE_SIZE) + if (gpc->generation != slots->generation || kvm_is_error_hva(gpc->uhva)) return false; - if (gpc->generation != slots->generation || kvm_is_error_hva(gpc->uhva)) + if (offset_in_page(gpc->uhva) + len > PAGE_SIZE) return false; if (!gpc->valid) @@ -119,7 +119,7 @@ static inline bool mmu_notifier_retry_cache(struct kvm *kvm, unsigned long mmu_s static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) { /* Note, the new page offset may be different than the old! */ - void *old_khva = gpc->khva - offset_in_page(gpc->khva); + void *old_khva = (void *)PAGE_ALIGN_DOWN((uintptr_t)gpc->khva); kvm_pfn_t new_pfn = KVM_PFN_ERR_FAULT; void *new_khva = NULL; unsigned long mmu_seq; @@ -192,7 +192,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) gpc->valid = true; gpc->pfn = new_pfn; - gpc->khva = new_khva + offset_in_page(gpc->gpa); + gpc->khva = new_khva + offset_in_page(gpc->uhva); /* * Put the reference to the _new_ pfn. The pfn is now tracked by the @@ -215,8 +215,8 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, struct kvm_memslots *slots = kvm_memslots(gpc->kvm); unsigned long page_offset = offset_in_page(gpa); bool unmap_old = false; - unsigned long old_uhva; kvm_pfn_t old_pfn; + bool hva_change = false; void *old_khva; int ret; @@ -242,8 +242,7 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, } old_pfn = gpc->pfn; - old_khva = gpc->khva - offset_in_page(gpc->khva); - old_uhva = gpc->uhva; + old_khva = (void *)PAGE_ALIGN_DOWN((uintptr_t)gpc->khva); /* If the userspace HVA is invalid, refresh that first */ if (gpc->gpa != gpa || gpc->generation != slots->generation || @@ -259,13 +258,25 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, ret = -EFAULT; goto out; } + + hva_change = true; + } else { + /* + * No need to do any re-mapping if the only thing that has + * changed is the page offset. Just page align it to allow the + * new offset to be added in. + */ + gpc->uhva = PAGE_ALIGN_DOWN(gpc->uhva); } + /* Note: the offset must be correct before calling hva_to_pfn_retry() */ + gpc->uhva += page_offset; + /* * If the userspace HVA changed or the PFN was already invalid, * drop the lock and do the HVA to PFN lookup again. */ - if (!gpc->valid || old_uhva != gpc->uhva) { + if (!gpc->valid || hva_change) { ret = hva_to_pfn_retry(gpc); } else { /*