From nobody Mon Dec 29 00:44:46 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A8280C4167B for ; Mon, 4 Dec 2023 14:44:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346107AbjLDOok (ORCPT ); Mon, 4 Dec 2023 09:44:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345712AbjLDOoS (ORCPT ); Mon, 4 Dec 2023 09:44:18 -0500 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A97C2C1; Mon, 4 Dec 2023 06:44:24 -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=TQCkPB1sujZxJovh7KF/VJS4UU1Zw/7OzvD4MgUIPi4=; b=dHyr0Copuw/HSjEl7CnzplFOVr vlR36iCrHPk8oWK+PTJ1X1Sf1ECs7aGW+Km7774N1heGRcESAbfNX56WBjEDScWIeEaZ3II4Mx++B sWwZvCSrixcwp39SCOwxYJ5jrZkp7d7oznk5PpfVsl1+saARviPdp5pEnGlYwepFhx1M=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rAAB9-0003fH-1k; Mon, 04 Dec 2023 14:44:11 +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 ) id 1rAAB8-00088g-Oh; Mon, 04 Dec 2023 14:44:11 +0000 From: Paul Durrant To: Paolo Bonzini , Jonathan Corbet , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , David Woodhouse , Paul Durrant , Shuah Khan , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v10 07/19] KVM: pfncache: include page offset in uhva and use it consistently Date: Mon, 4 Dec 2023 14:43:22 +0000 Message-Id: <20231204144334.910-8-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231204144334.910-1-paul@xen.org> References: <20231204144334.910-1-paul@xen.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Paul Durrant 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. No functional change intended. Signed-off-by: Paul Durrant Reviewed-by: David Woodhouse --- Cc: Sean Christopherson Cc: Paolo Bonzini Cc: David Woodhouse v8: - New in this version. --- virt/kvm/pfncache.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 0eeb034d0674..97eec8ee3449 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, unsign= ed long len) if (!gpc->active) return false; =20 - if (offset_in_page(gpc->gpa) + len > PAGE_SIZE) + if (gpc->generation !=3D slots->generation || kvm_is_error_hva(gpc->uhva)) return false; =20 - if (gpc->generation !=3D slots->generation || kvm_is_error_hva(gpc->uhva)) + if (offset_in_page(gpc->uhva) + len > PAGE_SIZE) return false; =20 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 =3D gpc->khva - offset_in_page(gpc->khva); + void *old_khva =3D (void *)PAGE_ALIGN_DOWN((uintptr_t)gpc->khva); kvm_pfn_t new_pfn =3D KVM_PFN_ERR_FAULT; void *new_khva =3D NULL; unsigned long mmu_seq; @@ -192,7 +192,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cac= he *gpc) =20 gpc->valid =3D true; gpc->pfn =3D new_pfn; - gpc->khva =3D new_khva + offset_in_page(gpc->gpa); + gpc->khva =3D new_khva + offset_in_page(gpc->uhva); =20 /* * Put the reference to the _new_ pfn. The pfn is now tracked by the @@ -217,6 +217,7 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *g= pc, gpa_t gpa, bool unmap_old =3D false; unsigned long old_uhva; kvm_pfn_t old_pfn; + bool hva_change =3D false; void *old_khva; int ret; =20 @@ -242,10 +243,10 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache = *gpc, gpa_t gpa, } =20 old_pfn =3D gpc->pfn; - old_khva =3D gpc->khva - offset_in_page(gpc->khva); - old_uhva =3D gpc->uhva; + old_khva =3D (void *)PAGE_ALIGN_DOWN((uintptr_t)gpc->khva); + old_uhva =3D PAGE_ALIGN_DOWN(gpc->uhva); =20 - /* If the userspace HVA is invalid, refresh that first */ + /* Refresh the userspace HVA if necessary */ if (gpc->gpa !=3D gpa || gpc->generation !=3D slots->generation || kvm_is_error_hva(gpc->uhva)) { gfn_t gfn =3D gpa_to_gfn(gpa); @@ -259,13 +260,25 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache = *gpc, gpa_t gpa, ret =3D -EFAULT; goto out; } + + /* + * Even if the GPA and/or the memslot generation changed, the + * HVA may still be the same. + */ + if (gpc->uhva !=3D old_uhva) + hva_change =3D true; + } else { + gpc->uhva =3D old_uhva; } =20 + /* Note: the offset must be correct before calling hva_to_pfn_retry() */ + gpc->uhva +=3D 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 !=3D gpc->uhva) { + if (!gpc->valid || hva_change) { ret =3D hva_to_pfn_retry(gpc); } else { /* --=20 2.39.2