Received: by 2002:ab2:3319:0:b0:1ef:7a0f:c32d with SMTP id i25csp895389lqc; Fri, 8 Mar 2024 15:28:18 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVdBvnGdOaEcH8P4cgCNRdibCgpfyEKN/IApljvJRkUVZBFzdNAx3wXl+8saiuGgpotVrlKR1KobNPT4je59ro4byAVrgF9+gBMqMSwRw== X-Google-Smtp-Source: AGHT+IFghUlzTL8BxueCK4nPGJK/EYUOMKkLXu0MJ+uwjvGX7PuXUgzj0Ck5xhts2skYJ5EYBe68 X-Received: by 2002:a17:906:89a:b0:a44:2563:c5dd with SMTP id n26-20020a170906089a00b00a442563c5ddmr72429eje.45.1709940498795; Fri, 08 Mar 2024 15:28:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709940498; cv=pass; d=google.com; s=arc-20160816; b=Je9s/we4BUBWPZ0EBKhxN8hIIMdLj8MbAjDQaN8JUs/JEiax7PS7ZawzIkOriOlepx PKR6fa8PO7cESVTkE9AB4VtuX0CSP5PThg7a52FZK9OKS/76SBNXKAn/S8KF/q2guEzS 7J38vVJyb85fLbvkUvaTcaQXMG/pT5ZK3k2iIIPIFvuz+7DKZWWBXeuIC/r8aWSy+Hee gIM2xmR/q3WGvBtKsM+dSToVDR5Cls8b28+PqAougNS1cvzIEw5vc1XCnS/WttyArAB0 RFWziRykvBU0l7jwvsJGri3XCxmNHXX52TlggS6CUjM9vB9wD4OQfYjb/IxzfCPEfkry CyQg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=N25dHtdFBFZm2u6EyvcAjsvLHySdv26qbRgS6+X49N0=; fh=bjoccWhpyINfeSnZKpSPA/bm3puIjnw76gN64+atFa8=; b=qeeM68UJdX4tXSxALaCI9rnLkjY8icj9ikp4N43HDXI5bWKhASh3yb6JiwId9lfS2e WL8g572M4Vjvqaq9aCTDZU08yyriBJ2rcx4txp0lRv6KKD0dqzSgya0nfDdtAw+81Iwl J++KO3JkSp6ZAjQSjZGppJcZqzr53PMgOlqvEIkAWUcUH/aPGxw4U+ifL4UtKvFJIB5u 8QIOLBHzo8i2C40QwaB10VsYMrOckM72+TRO//F+KQgh5xY6epjIcurb4X5UKn3XE+mI h3XMIjasWuZRikoRy3TaF31OAjaH6ONHP2r0az3/waHJJdSAqi0hDYqohAAKOEHU+yRX g2pA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=WRAxvtG5; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-97691-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97691-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 o2-20020a1709061b0200b00a440d812e25si238789ejg.85.2024.03.08.15.28.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Mar 2024 15:28:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-97691-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=@google.com header.s=20230601 header.b=WRAxvtG5; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-97691-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97691-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 86DD61F2189E for ; Fri, 8 Mar 2024 23:28:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6DC7B3BBED; Fri, 8 Mar 2024 23:28:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="WRAxvtG5" Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 269223BBC3 for ; Fri, 8 Mar 2024 23:28:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709940491; cv=none; b=VtajWtM1yRTquyMWY+18CRpn31xI0LitqxNNSEL9eVZRxkk9zi59vjKWRJFqjBnZ3HG8cwkeb9fxSUVpPH46tcuFbqYXY2O6Mckb6KY5g6mxQST8QsUKCuaRJSVqiTyGj1wkIbkRMwi5VP56FXDsegkq86Img897KnjAjsJpssE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709940491; c=relaxed/simple; bh=BpN4iQW4PFrq3WVtA7bWTxphTlwS/fKO8mFIPGOtpZc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Buf6KesE8K8fF1dyjozZqoHrJ5Kso/cYHfRjGcaj0l/8HvngFtkFH7JQE5O6EGKNAa7gOWf6H5NXQVTbtXOQCiV7s6BSauXYHGAtzXukb4XdXgluosIv91dgKiVD8GE1po4HiBwqSyEY0LaqXHQlWSIpK1bX/zi7WXFjTH1gYto= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=WRAxvtG5; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-6e62136860eso1216144b3a.3 for ; Fri, 08 Mar 2024 15:28:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709940489; x=1710545289; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=N25dHtdFBFZm2u6EyvcAjsvLHySdv26qbRgS6+X49N0=; b=WRAxvtG5vRvZT1YNgSI3niXFlBqKbkMll4cCmp0t2El7oTO7DAnbQPbVBh32wrJUSe cv9yJIe7ue5azDN59RnFZjVgj5USUN66eJgB+2HNRyjl1nbVaqMWAEHjJH4AiSKxBvXW EneuvUgimHyFAO4GERAM4m9qku9haemxByAN/taS6tgkzVX+sscMoTIzkxZO/7ax7C02 CclO62pZPcPddmFvlJdaCtqZ+PVmVCZTMEszjPwwj++mZ1ABTQ98C8oPezyAE+BpxAXN uRvN8UxwpmpqSZQo4PqOPTRwd8ramGkQdzmS234re6X+XsxGK+6yIGYjwtj5ap17wOXk UuFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709940489; x=1710545289; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=N25dHtdFBFZm2u6EyvcAjsvLHySdv26qbRgS6+X49N0=; b=pXgK+koaaJNX26JBgqMTUzcREISNeWOHqcZ0oTHdYStMFaVGfJCQA9Jhuh8v8WOqqa KQL3ruLHlmthi0vG3Pu7FWrV65ZWtchu/wQZEnwcpPOV4f+H1AiYieAN5iUz4x5K7bM1 XvY5fS4D/jIsYp7RWqcEYUAtOC0Fqb5HMMB0DgE7wmjik4/CiY7doTLThZsCn0XIl8pO FZ0TiYGdaVk2EXha8aXxa0ePfi14N06c5vH+PlNGVcYk9d/Rlxo7ZBvO9L9E7W5rucMP 8oIIfJbaD1lB3G4iZWs76YPgk9dHsjjcj1k4SQqEykx8ZyHRinohR6+ZbhHrp8Thql05 DbBw== X-Forwarded-Encrypted: i=1; AJvYcCWCmZS0Wjq/wjVPlOC+VrhGQf/KLX65bJRZlLchglaGiqYoi/0BIwG8mbBT7q5X2YGS1JsxauGO8T2Sa9kmXhQPMjIhP/j9UGUrtijz X-Gm-Message-State: AOJu0Yz/vEX7EvvjXbXKyE0U4xsNq1jCM4BzRfkFjYaKtsBqCSMVrkHN H/+c6VefIu3YyujtGcc5/jKbXv2wVr4PtNOlI7KV72vcHnZVO4CrgsK65SlV4oKlEMnMBDfM8NZ gmQ== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:1817:b0:6e6:508e:395c with SMTP id y23-20020a056a00181700b006e6508e395cmr23698pfa.3.1709940489438; Fri, 08 Mar 2024 15:28:09 -0800 (PST) Date: Fri, 8 Mar 2024 15:28:08 -0800 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240228024147.41573-1-seanjc@google.com> <20240228024147.41573-10-seanjc@google.com> Message-ID: Subject: Re: [PATCH 09/16] KVM: x86/mmu: Move private vs. shared check above slot validity checks From: Sean Christopherson To: Xu Yilun 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 Content-Type: text/plain; charset="us-ascii" 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.