Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp947082rdf; Wed, 22 Nov 2023 00:58:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IHhSqg/8zLIm/+QrrtxHfVCX5KSPp9WcYSMf9AnnpqLR9UHFIwvoX8I8oNY9i97c3L6aoiD X-Received: by 2002:a05:6808:8c3:b0:3a8:1c07:17d3 with SMTP id k3-20020a05680808c300b003a81c0717d3mr2077568oij.19.1700643502778; Wed, 22 Nov 2023 00:58:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700643502; cv=none; d=google.com; s=arc-20160816; b=iAWOOFKOnwSBdWY3zMCwN/tK93XnilyCiS/QpimNMhLon9dQCGKcE7q7HuL1o+RgDZ jeFr0pb4nbbl9YhJl/4iT8MdSF0aK9xDG6XcJ/bPf/W1A/zEEA+4upmicfGi7bE3PtQx TZdeG65SfU25qajlRWIk15MKBB1I9pqZaRnvm/1yJtwnohu97csgxiErUAu4wc5zcEKk 447iTR8XuFuGzGBRH5ulST2z1o+fqAM58lix6weFhtfjlWA2Masqe9OicbEYvvr4EENm QyP5KKMAh7WBxzDzwP9gcEblPr30gfv5Iz/lg8kKn0Sea1DUjw6BlCqWwMzhaQsMKL/r kfMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=mK0nG0bqm6jARpTaZfGmfUZ4KSaeWrzR711EIbCRtYY=; fh=f4h+eG60D9wtQIsZtw3bq6XBS1W/6NJC4O92N6N8Ovc=; b=wolZU0yMzeFUFvBkMXj8SIdkJsQQvQTY6UIJt1qvyeMJm2+1lLmIIbgVI/NANMdIcr Wx+oLHKyjrN+MAwYX58/wJZCUmjfQfMY5wPA/CTiGwoFKGW6DIiDTk81Q+n6Gxub7qmC tqqMEqM2cbzBjHy2Dd2mI2K+ac/pwUkx6+Vm0kWBQAVbE+ekKpfDCobu/R6zd39kzwZf h2fy9dYT1TT+tPeFzxfoUOPMGBDUmVsUAECq3iebUlwgklMYUdNJLNYme3OGhutMbiDP 4npLggMXZidmSw3oPeiSiNAPfRPOQ7F6TYcJB1rFFonT0123O+7hDVSITx+5uxO/PYe5 AmxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=JUlVi7MP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id t6-20020a056a0021c600b006910a45a234si12339728pfj.202.2023.11.22.00.58.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 00:58:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=JUlVi7MP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 6F48C80C2553; Wed, 22 Nov 2023 00:56:35 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230267AbjKVI41 (ORCPT + 99 others); Wed, 22 Nov 2023 03:56:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229477AbjKVI40 (ORCPT ); Wed, 22 Nov 2023 03:56:26 -0500 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B316AE7; Wed, 22 Nov 2023 00:56:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700643383; x=1732179383; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=579B6Vn86UXddgf59Bx7trqMdHFc5/puvhZkPkUaV4A=; b=JUlVi7MPbR//by4Thh7ciwxViQLuX3LQvwvLS8Ri7e3qi6GBLnZPXnHs oMzy7cLvLTw5hpHFQsyYYWutNKkPJ3x1eQ4+SkdSTA5uKlEx+Bais/C2p bDrFifPgYrHryGLJqJPXJXxNw6w5BkSf2P1xNEBWYBQychdvCkvY06LKy cMtXk69hF1BsIo4n7qsUfGfP5vnpYIyGX2dKZwAt+StXtenznfqE6ntBG ZDGdvpD35/sxstU5Llj1J0+cuaqelhwuAJVaoZyko4l8QaVwegMf5NygV C7EcDjNl9dmaTnjsRWRsBxpugL6JiH3p29aTHm2fU7++NatvoJfEIvRTe A==; X-IronPort-AV: E=McAfee;i="6600,9927,10901"; a="5154735" X-IronPort-AV: E=Sophos;i="6.04,218,1695711600"; d="scan'208";a="5154735" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Nov 2023 00:56:22 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10901"; a="770520489" X-IronPort-AV: E=Sophos;i="6.04,218,1695711600"; d="scan'208";a="770520489" Received: from yilunxu-optiplex-7050.sh.intel.com (HELO localhost) ([10.239.159.165]) by fmsmga007.fm.intel.com with ESMTP; 22 Nov 2023 00:56:19 -0800 Date: Wed, 22 Nov 2023 16:54:26 +0800 From: Xu Yilun To: Paul Durrant Cc: David Woodhouse , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v8 07/15] KVM: pfncache: include page offset in uhva and use it consistently Message-ID: References: <20231121180223.12484-1-paul@xen.org> <20231121180223.12484-8-paul@xen.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231121180223.12484-8-paul@xen.org> X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.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 (agentk.vger.email [0.0.0.0]); Wed, 22 Nov 2023 00:56:35 -0800 (PST) On Tue, Nov 21, 2023 at 06:02:15PM +0000, Paul Durrant wrote: > 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. > > Signed-off-by: Paul Durrant > --- > Cc: Sean Christopherson > Cc: Paolo Bonzini > Cc: David Woodhouse > > 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 > >