From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.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 D708518D65C for ; Tue, 6 Aug 2024 14:03:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722952998; cv=none; b=PVhtkrx6u5VaSzn18NXcG4RdNYFcdWK4QvIU0qjkK1EN+QjA1W0mYqREPY1auWSVbhMBnBtvoCXpnDsqv0T5HCWIoNZVanoTx1iENg7Zzz3p8sYto6EQA0vcKqEkXhbw2VKBXBOHL7VzWPAWCRmlngnQoKCUes3ehLTISApfJ9M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722952998; c=relaxed/simple; bh=p9ZVmTdp+MSHPZCDiYNhr72+FrIxO5hIK6X0gOSKUBw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mAv1P77XEh5pj60FOEq/L/Z/bO1A4MmBDcrvPJ1ZELxex8K6IaIcqtmZfMo5ABvuwrQ1fHZNtSbw7uj2uzBQLkEzLQuGNJUk6WNLlFh5RL2PhF6haVpVqY/3dRDVSp93TbdNx7R8zVN3Vzk6A1LbGiMjR0uSqj9cMB3Na00DJIE= 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=JDzTsCTa; arc=none smtp.client-ip=209.85.219.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="JDzTsCTa" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e0e78047c98so767504276.3 for ; Tue, 06 Aug 2024 07:03:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722952996; x=1723557796; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=uqGT2mTXRSzZVlXU0nXvRQaIKYNQT+/mO2u06f0vChA=; b=JDzTsCTapuq6R60wlzcPS5LVQ50+8zyNc+Y/A0rQlgY8AYEirfrcZqJQjFGMG5ITXm oJXq+3adOlh0wEur39vdzVG5FFHAkrpjaEDvkAv2PO3xwVy4xNHfMXzLVyb/5r+UmPGP jK2xajxBQECE8QPWTvTN2tzml+3qtZ8FOCM1lFt7dGaS2/fu+nIYeiDXn5sPTKM7N/mD 3z+EzMhc+CyEP8krdl24J1anMSNX6DY3bCIKrQBCmBLgIIgaOh78XyHSe5o3e2un+LaP 7bKbAzaWU1xz6Xg+eZU1BbkBhvaFWoq8YREpSgwU6dhgj+8qyX7RF8+VbXakkGDFSuGx a8pQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722952996; x=1723557796; h=content-transfer-encoding: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=uqGT2mTXRSzZVlXU0nXvRQaIKYNQT+/mO2u06f0vChA=; b=uFaI5n3jvdvtpGEcGBVDDNTksRG2O07+KWty1uzfZ0IfkcG6V7IMzXgenTwRpXj1CN zQTRciYtpakvEw0xyHeFm28x+Lq1CxWTau4yz5z9Szqy7yC/TRicZpO+Yu1dteXyILjv VTqo+L36xZT4MjipQG7HbqrbPL7zeBthgERgIQ0jeZS36gzvLu7ZIt4sGgt3YQn/g6g9 hUGIlEkwv+OFDVt+56Xj6xln9FiPRGrx0DtdogETrVI8wv0UOf5bHLkXfjh/UWKZghfw zFbxDNJBknOmSaIcXvHDaWfEqbbR+4vCKh8BKAZ2s5thYrfNTHd99qs/P1v8WzkWm0K0 rXxg== X-Forwarded-Encrypted: i=1; AJvYcCVNbyTDyK2BQi3dButWeslXDiVmJKpMW0/+rpPn0wzA23m7z3qTiYV/0LY+qwoLr2aCTsJsv3SsJl4SOo9h/fuzz75IR9L9pTUtRxkB X-Gm-Message-State: AOJu0YyDyYSk9qKuNdfvG391vz7IsxxtBQxKb8gXsoHCBGMRe9uQk9Cp O0Mev9xOQmh3G5+ApT3vhluKhZq61vF6qOqroEzM4tLrXlRBZkrwNFsynPMPVJa6LRZI/cOx9fR Ujw== X-Google-Smtp-Source: AGHT+IHr0cjXP2WYBxQNM2DFAMasCde6kX0ZJgK/KZ2REvTSlG5RrUyWL0rGH3moNZ4O8mUg1dBZlOQRgtI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:150a:b0:e0b:edb:143c with SMTP id 3f1490d57ef6-e0bdde81b6cmr199586276.0.1722952995798; Tue, 06 Aug 2024 07:03:15 -0700 (PDT) Date: Tue, 6 Aug 2024 07:03:09 -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: <20220427014004.1992589-1-seanjc@google.com> <20220427014004.1992589-7-seanjc@google.com> <294c8c437c2e48b318b8c27eb7467430dfcba92b.camel@infradead.org> Message-ID: Subject: Re: [PATCH] KVM: Move gfn_to_pfn_cache invalidation to invalidate_range_end hook From: Sean Christopherson To: David Woodhouse Cc: Paolo Bonzini , Mushahid Hussain , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Mingwei Zhang , Maxim Levitsky Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On Tue, Aug 06, 2024, David Woodhouse wrote: > On Mon, 2024-08-05 at 17:45 -0700, Sean Christopherson wrote: > > On Mon, Aug 05, 2024, David Woodhouse wrote: > > > From: David Woodhouse > > Servicing guest pages faults has the same problem, which is why > > mmu_invalidate_retry_gfn() was added.=C2=A0 Supporting hva-only GPCs ma= de our lives a > > little harder, but not horrifically so (there are ordering differences = regardless). > >=20 > > Woefully incomplete, but I think this is the gist of what you want: >=20 > Hm, maybe. It does mean that migration occurring all through memory > (indeed, just one at top and bottom of guest memory space) would > perturb GPCs which remain present. If that happens with a real world VMM, and it's not a blatant VMM goof, the= n we can fix KVM. The stage-2 page fault path hammers the mmu_notifier retry lo= gic far more than GPCs, so if a range-based check is inadequate for some use ca= se, then we definitely need to fix both. In short, I don't see any reason to invent something different for GPCs. > > > @@ -849,6 +837,8 @@ static void kvm_mmu_notifier_invalidate_range_end= (struct mmu_notifier *mn, > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0wake =3D !kvm->mn_act= ive_invalidate_count; > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0spin_unlock(&kvm->mn_= invalidate_lock); > > > =C2=A0 > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0gfn_to_pfn_cache_invalidat= e(kvm, range->start, range->end); > >=20 > > We can't do this.=C2=A0 The contract with mmu_notifiers is that seconda= ry MMUs must > > unmap the hva before returning from invalidate_range_start(), and must = not create > > new mappings until invalidate_range_end(). >=20 > But in the context of the GPC, it is only "mapped" when the ->valid bit i= s set.=20 >=20 > Even the invalidation callback just clears the valid bit, and that > means nobody is allowed to dereference the ->khva any more. It doesn't > matter that the underlying (stale) PFN is still kmapped. >=20 > Can we not apply the same logic to the hva_to_pfn_retry() loop? Yes, it > might kmap a page that gets removed, but it's not actually created a > new mapping if it hasn't set the ->valid bit. >=20 > I don't think this version quite meets the constraints, and I might > need to hook *both* the start and end notifiers, and might not like it > once I get there. But I'll have a go... I'm pretty sure you're going to need the range-based retry logic. KVM can'= t safely set gpc->valid until mn_active_invalidate_count reaches zero, so if = a GPC refresh comes along after mn_active_invalidate_count has been elevated, it = won't be able to set gpc->valid until the MADV_DONTNEED storm goes away. Without range-based tracking, there's no way to know if a previous invalidation was relevant to the GPC.