Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp750735lql; Mon, 11 Mar 2024 17:08:17 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVgEybw5Voa0MLk64IX+zN1DFlCcwfos7IdYu0WIspOnL9B/TDDeHp6svvQWO8GcBU7FAb6m79ED3TSxEeOzk7Ky0es3WdLjgPCcOeyLw== X-Google-Smtp-Source: AGHT+IFildvBl0xkhfcmeg0lXvjGmtCz2VRiAOg6JCSPul9b/DjIlMnEVQqhS1/+DId9Si+o4AE+ X-Received: by 2002:a9d:560e:0:b0:6e5:25a3:8e04 with SMTP id e14-20020a9d560e000000b006e525a38e04mr4905720oti.17.1710202097068; Mon, 11 Mar 2024 17:08:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710202097; cv=pass; d=google.com; s=arc-20160816; b=cO5ZsZimf8dzIasoSnrxdtWsTYgd30SDQGCh73APXUjijPT77W27F/CMv9ZTJfLsoi 3/lgkHxqLn0zbHQwEpLjMMGXMz91nb1DQ9DwckAQnoTpHA+nNPcpJ9aK8bCo9XtkAFuU CJaxqbvUtNrqR8/b45DMBAORIEd9S19WRv1a5lde9TDuWK7nCGhwX6OzbYDfmM+rfZoZ QHX4PL9OTyLQHh7Uxf5FiVIycqqVZCwM1XJz/BqW9ZEeCMKdKxVDfNWp1SZO/NQJZ+xd XGpzKuDDt1D+HocFP1IVHhE5D3Xu2GzX1uqP3q73+BSJ/6zFqOJXs1G1L5GqGvSD8apW 1UuQ== 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=Pum+V0ZuZEgl9/jYMpYphQnY8pqiA+YW9T5jbGt8uRw=; fh=rh0AbsRPzO0utclYg2wk9trpOXUdZNwQ3R/SHeXxgpA=; b=G9NENuqrGjRzmNPHmOEgdGj1TL5VRGBJAeZen7PabVeGobxej6l3v96ZKVPRnMauTu t5SLIYWOvK52AOAqbVN0D9SBm9oR1FFlC6THgQC5mgpcsb3OhL536E7yuXX8JYmb1b2y 1CeKKMMgSzKo6HJm2IWxyOgSU8pqPHFRyUPSQmmy/Up8XbiiYuKfu6dXa4uDoDISea5s UkKTvJhal5gZ7iFBLQie2dquwL2pZ8YQPddzqXi68IGnqBYywcQEBpZWbxO0VgiELeXj vS2dV1rPcEGuEHQHr5EZXhhl4KVa39zaYQmSlA7BzLtYhS1qTA8GnNy18iEZIOd16JPb AwwA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NBcMzKgL; 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-99687-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99687-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id c17-20020a63d511000000b005dc1bb540cesi5785849pgg.76.2024.03.11.17.08.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Mar 2024 17:08:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-99687-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NBcMzKgL; 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-99687-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99687-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id B8623281B5F for ; Tue, 12 Mar 2024 00:08:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1937DEC3; Tue, 12 Mar 2024 00:08:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="NBcMzKgL" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 CE000370 for ; Tue, 12 Mar 2024 00:08:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710202088; cv=none; b=raCrz/mt57KD79U0gfLLgECRxEgFzNo1k3CnoUHecC7Ctbvce1TMD47qZBX6TQN9Y/pYUYhcnihnvuMSKWVu4RNSM83hARQ8FRlLc2wbr1SafFBKJ7C4lSgK/FuVaiv6FWZesyT24HPCGqGuLKCHHv18mp2/hNlvv7U1A4tyHIg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710202088; c=relaxed/simple; bh=l6rVoLL7b5GYnY86FxsMiWPpqEw2YPCNBuuoZ8f4T38=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kUt9jiKPv7Gonkb+dTp62fv8rZ21HUILfyWKH40pipC72sh05ORBkV5eGaco9stmlT2VbIDyA3IdpGgunbBCzn/1b7XLFsYwlzFal4TrEuSP8XrqyGQGz7eakOxjUz/4kdit2hf9xbOVTOAH4+D8iWiQpKAKoZpVbWCC1DbxYQI= 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=NBcMzKgL; arc=none smtp.client-ip=209.85.219.202 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-yb1-f202.google.com with SMTP id 3f1490d57ef6-dced704f17cso8520221276.1 for ; Mon, 11 Mar 2024 17:08:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710202086; x=1710806886; 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=Pum+V0ZuZEgl9/jYMpYphQnY8pqiA+YW9T5jbGt8uRw=; b=NBcMzKgLEfN0BungWxGhN6WGxnrtYOf4wVN2zMaDYZ8HpR7p/53fLBVvhCOF4FbBOC VuvfO4G+uE077eqJISDKMnTOwZHJKeYthhbFBSKnZLNtZSWh9NiEcNwQ20NkKdPocfDy N99HKEIyCNzU54XTJ0pd7Bx9vwH73VsnHfJsD+tfGoCxDne4YLKFSD9uLmm9ziu2y5pY KIDBSESjACFonf4khiHuNgsxh+sPE4ZBdYqkiMxR6rzMlfxeSyTh1ZDGH5MmoghD79pJ a/mKoFNWXYOH+kTpu8LZbUjkGeCUDFG/6hyFfQHAjXRq2t/14WTQh71dwJvqFj7woLF0 XaiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710202086; x=1710806886; 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=Pum+V0ZuZEgl9/jYMpYphQnY8pqiA+YW9T5jbGt8uRw=; b=wqMfHMMrbNllt6G/RG9Uh86kVHEg0H3wqeHaVGWv67mkVewKWRAUTedLTzoBUPcQhk N54WppqlZKjH8/ouRVV9xTm7VyP82Z5Ai14GQmMYk8VYC6TuKORJqW7F4BuwJfAWwur+ nTBv55Q3BicQP9tijRZamNe5J0IcEqkWYePgQsgRx7XLNp7DWXiXEvL3YIWiQS5rUCm6 +d90DY/nIPqbL6LIt9p1iuLCMKCqakEZ3rqTnkyjNuQkULt9Nye2yh+xbchZGVH4Mxe+ xAzJuaFaccfhwJqb4/9IeFU9o/zmAau0SxOxmtf3FBIhz/d78MY/Twsq5R7J1v8/EirA ZV7w== X-Forwarded-Encrypted: i=1; AJvYcCX/LG694rtVBfX31jYsFj3U9pTcfaEojCwLAwl69WkT6ZSmkNxItKRnwXmmtgEa1scvvEo4jlXOXHWzMnH7nY5DeOC7os+VQwEXS8z+ X-Gm-Message-State: AOJu0Yz0OrZiWEVGSxQgh+xZlX83qz434bJnDjqbnM8UpRLUvTw+NKth WXT6O5Q8tvZXQSvSvrw5ATxgeI1WSk1rwK4tO2m89/HaGTyuWT/NR618iIgmcsZF3discMZ77tD tvQ== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:4ad:b0:dcc:94b7:a7a3 with SMTP id r13-20020a05690204ad00b00dcc94b7a7a3mr418633ybs.12.1710202085872; Mon, 11 Mar 2024 17:08:05 -0700 (PDT) Date: Mon, 11 Mar 2024 17:08:04 -0700 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 Mon, Mar 11, 2024, Xu Yilun wrote: > 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: > > > > @@ -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. Yeah, I'll add a comment. The only reason not to add a comment is that, ideally, the comment/documentation would live in common KVM code, not x86. But this code already has a few big comments about the mmu_notifier retry logic, one more definitely won't hurt.