Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp349780lql; Mon, 11 Mar 2024 04:56:52 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUHPFMwZWSB4rGWBFO1/OybbsoydM183lqQoBVs8gCjrKDOc/aSbwwZQV9dnQxe1O2eDewkehtArw93GnQB1oRGF9SOKcX5tTz8gWTSPg== X-Google-Smtp-Source: AGHT+IH/CTa7VgGUx575o9A1L62SciOoAtyuynMQ/pN9hqa6DX8VGvxKoa6J2IdwM1pNlliSJRwD X-Received: by 2002:a17:906:acf:b0:a3e:4d7c:9ce1 with SMTP id z15-20020a1709060acf00b00a3e4d7c9ce1mr5125653ejf.21.1710158212000; Mon, 11 Mar 2024 04:56:52 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710158211; cv=pass; d=google.com; s=arc-20160816; b=SZ53sWK3WblCMasa3XojbrUqQUHy3J47jISKEuaODf9IpFYRikEKTSh3jJc5GTtkdn Krsw0Mlek41173aoiTTU4V/v+6eDSbL9iU1qVW6IqtSgRGYoJfShZDv6WKMaGIxJ+GJo oDKkvekk01y054GvsgeyWDNnHK2XyS9FWBPuOYWlgimGf5N+W95Crg5vTwlesEXAl2E0 scDj+Y30oFrSwwGM1m2HgFg2L2kz8vNkg9jpAkCPMGf5+E9yQyMNinV7rYReYmKsePy/ m9zzc3Ode7qJWqsSNib+A7u4xfF2K8lBXbscBpghNwZ+OF5v9hUkc1m9FHMgLH9bE6FG 2Vsg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=m0UwdpfC2N5uR3F2+OXZfwKFfBJ2ndNK1n77nJkIwec=; fh=A4iw5QxNVdA3riROn5Qb3bx4lYbSO9D5/HiDyRZmbBM=; b=jCjj31fl6fpjYBMKDFwjpLLRvvohU5JJLrxZncM6cyAo+7U3X7vi5NAAd3bKMP3WXi fuoIU+4USSAyYYkLc84E8SEg8/3jCWCLIomZzug7QBOZuIp/j1YGYBp9zEPyGQZud4bV Yx0CFfdjFpo/P+wzhDsAiJYiNEU9K1pbd8fL+zj6JXXNtchRToGFBUVVhXM0YXv3cTju +qWDzBaD/eElkmzINxSe2ZaaoN8Uw9H0xzsnuyF+i5A49kH6yNaDpVFIYTmzRzDXiIGX pmju9KSBtVwZVCXM6RIRZuAxI6KeriV9pvMwKigLT10unMe2xGnRRKQz2ZCRZh3IH0OU kOrA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=kn5dAt4V; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-98460-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-98460-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id k22-20020a170906055600b00a442eb054fasi2339791eja.497.2024.03.11.04.56.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Mar 2024 04:56:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-98460-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=kn5dAt4V; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-98460-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-98460-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id C08DC1F21D93 for ; Mon, 11 Mar 2024 04:48:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 609EE8495; Mon, 11 Mar 2024 04:48:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="kn5dAt4V" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CA6691878; Mon, 11 Mar 2024 04:48:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710132486; cv=none; b=Se0Ff4dL+A2D8rfPx9YHKH/swzVGmclByXRQ1/63Yr8LMUcEn4VWsO/n+RQPr+0lmyRa5nyyGR7Z49GrAnwYAio4fw8qtMjRG8TrodxxACFhetczef5QdDenhVpVX6vwO3LynORYKjsbzwpuqGLsg6D0pSJa751zc6eu80bcTps= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710132486; c=relaxed/simple; bh=+xQ984v3y25MBDNQk67yJ/vJQapd1ncJ6iyho2QJ5+w=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=u5Xe2S6qHDR6qvgUdVbzIkcj/mlhRGlokf9UQ5+eGL4S+VOzDFh/Smsru9KmzSoc+l8yGGskMSCeHY33bIfgULYwbsxE8uPJEdPMJsnxz99Kb+DZMYsbSQ/AbUcoXBtkPjKoW0v7mwnRrML5ZvjGNqk1JMpbKffNW+eM9uiUpuM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=kn5dAt4V; arc=none smtp.client-ip=192.198.163.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710132485; x=1741668485; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=+xQ984v3y25MBDNQk67yJ/vJQapd1ncJ6iyho2QJ5+w=; b=kn5dAt4V8P2HSwOZkqIgtsDMnxKX+l8+HaNkBtvtgF+4cdCLYCHVENkD iw3GBG6EDLjqkLs2BWFvhlgePLmcwDcvSKOwjVohk7fJ32UTq09Sk+skl CZX3BBR/Idjl6lYvsbsC0fYv6nUpIR5QI1dHSotHaP2mDU48v3v4DyNH0 390p4vVhB5c3OT4WNJvfLdfnpeFInjmITCHjJaO9aX4hyyDuG+iD4cHKy EsDFx4YBktc8Y3kzXro15BStJ20x0nwg3zHkEQpVchx/1lDhUQV6p+JfL sQaV3W4oTGWKyrwwBhzWxDP3BbbisJmR1SChxC9oQ5FF28XayP5MI/4kp Q==; X-IronPort-AV: E=McAfee;i="6600,9927,11009"; a="4621577" X-IronPort-AV: E=Sophos;i="6.07,115,1708416000"; d="scan'208";a="4621577" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Mar 2024 21:48:04 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,115,1708416000"; d="scan'208";a="42030439" Received: from yilunxu-optiplex-7050.sh.intel.com (HELO localhost) ([10.239.159.165]) by fmviesa001.fm.intel.com with ESMTP; 10 Mar 2024 21:48:01 -0700 Date: Mon, 11 Mar 2024 12:43:41 +0800 From: Xu Yilun To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yan Zhao , Isaku Yamahata , Michael Roth , Yu Zhang , Chao Peng , Fuad Tabba , David Matlack Subject: Re: [PATCH 09/16] KVM: x86/mmu: Move private vs. shared check above slot validity checks Message-ID: References: <20240228024147.41573-1-seanjc@google.com> <20240228024147.41573-10-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Fri, Mar 08, 2024 at 03:28:08PM -0800, Sean Christopherson wrote: > On Fri, Mar 08, 2024, Xu Yilun wrote: > > On Tue, Feb 27, 2024 at 06:41:40PM -0800, Sean Christopherson wrote: > > > Prioritize private vs. shared gfn attribute checks above slot validity > > > checks to ensure a consistent userspace ABI. E.g. as is, KVM will exit to > > > userspace if there is no memslot, but emulate accesses to the APIC access > > > page even if the attributes mismatch. > > > > > > Fixes: 8dd2eee9d526 ("KVM: x86/mmu: Handle page fault for private memory") > > > Cc: Yu Zhang > > > Cc: Chao Peng > > > Cc: Fuad Tabba > > > Cc: Michael Roth > > > Cc: Isaku Yamahata > > > Signed-off-by: Sean Christopherson > > > --- > > > arch/x86/kvm/mmu/mmu.c | 15 ++++++++++----- > > > 1 file changed, 10 insertions(+), 5 deletions(-) > > > > > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > > > index 9206cfa58feb..58c5ae8be66c 100644 > > > --- a/arch/x86/kvm/mmu/mmu.c > > > +++ b/arch/x86/kvm/mmu/mmu.c > > > @@ -4365,11 +4365,6 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault > > > return RET_PF_EMULATE; > > > } > > > > > > - if (fault->is_private != kvm_mem_is_private(vcpu->kvm, fault->gfn)) { > > > - kvm_mmu_prepare_memory_fault_exit(vcpu, fault); > > > - return -EFAULT; > > > - } > > > - > > > if (fault->is_private) > > > return kvm_faultin_pfn_private(vcpu, fault); > > > > > > @@ -4410,6 +4405,16 @@ static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault, > > > fault->mmu_seq = vcpu->kvm->mmu_invalidate_seq; > > > smp_rmb(); > > > > > > + /* > > > + * Check for a private vs. shared mismatch *after* taking a snapshot of > > > + * mmu_invalidate_seq, as changes to gfn attributes are guarded by the > > > + * invalidation notifier. > > > > I didn't see how mmu_invalidate_seq influences gfn attribute judgement. > > And there is no synchronization between the below check and > > kvm_vm_set_mem_attributes(), the gfn attribute could still be changing > > after the snapshot. > > There is synchronization. If kvm_vm_set_mem_attributes() changes the attributes, > and thus bumps mmu_invalidate_seq, after kvm_faultin_pfn() takes its snapshot, > then is_page_fault_stale() will detect that an invalidation related to the gfn > occured and resume the guest *without* installing a mapping in KVM's page tables. > > I.e. KVM may read the old, stale gfn attributes, but it will never actually > expose the stale attirubtes to the guest. That makes sense! I was just thinking of the racing for below few lines, if (fault->is_private != kvm_mem_is_private(vcpu->kvm, fault->gfn)) { kvm_mmu_prepare_memory_fault_exit(vcpu, fault); return -EFAULT; } But the guarding is actually for the whole kvm_faultin_pfn(). It is the the same mechanism between getting old gfn attributes and getting old pfn. I wonder if we could instead add some general comments at fault->mmu_seq = vcpu->kvm->mmu_invalidate_seq; about the snapshot and is_page_fault_stale() thing. Thanks, Yilun >