From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D594FC27C6E for ; Fri, 14 Jun 2024 16:23:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B93B6B00BA; Fri, 14 Jun 2024 12:17:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 64D836B00C3; Fri, 14 Jun 2024 12:17:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3D0166B00D6; Fri, 14 Jun 2024 12:17:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C7BAE6B015E for ; Fri, 14 Jun 2024 12:13:04 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id D92B0A080F for ; Fri, 14 Jun 2024 16:13:03 +0000 (UTC) X-FDA: 82229988246.27.BE05CE7 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by imf29.hostedemail.com (Postfix) with ESMTP id 062E012001C for ; Fri, 14 Jun 2024 16:13:01 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="bL/hO+/b"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf29.hostedemail.com: domain of 3DGxsZgYKCM8Dzv84x19916z.x97638FI-775Gvx5.9C1@flex--seanjc.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3DGxsZgYKCM8Dzv84x19916z.x97638FI-775Gvx5.9C1@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718381579; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=7gUwZpSkwFU+FWUT8qGAdEg7M+9bZN02qOvPWnijOmA=; b=jBoDMzIATPk2e9i3ewm5015XzrviT3ZdjV7EJsm3cEvmd7dpZ9Zn6OJr6zquMd7GxhBwzA TmMF8eg4/gTiQQ+5Re8yFU2yjkO1pCRW7FH5vIpKadwxypXlTQdst8+IuHJwBWJXRiNn2b +Xvy11GHGUowew2p1nEgEKWIxkCGAwU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718381579; a=rsa-sha256; cv=none; b=otsmwKj7PTZoHKcK2vK9zKkAaoBcm88B9DxxDnnipS9W8yWAVcJ6c+5ejEa/QrwXJ/7euQ 5MQ4SYpnoufqNqiH6+EwCB5heEYxxV2NB6CMxMV6oS6v+DrKZYeoZEJNQYcZv+3FOHxS1B pKZvovT+mkGSGGM5Ho6OG3u9rbymaDg= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="bL/hO+/b"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf29.hostedemail.com: domain of 3DGxsZgYKCM8Dzv84x19916z.x97638FI-775Gvx5.9C1@flex--seanjc.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3DGxsZgYKCM8Dzv84x19916z.x97638FI-775Gvx5.9C1@flex--seanjc.bounces.google.com Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2c24109ad3fso2183933a91.0 for ; Fri, 14 Jun 2024 09:13:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718381581; x=1718986381; darn=kvack.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=7gUwZpSkwFU+FWUT8qGAdEg7M+9bZN02qOvPWnijOmA=; b=bL/hO+/bayInIQin4+nSIiyDuZRjNU04+s1R4NIhCSuXVCdANncJT2qB76hsCpe33i WlhnD/M8dZazXmLKKdPyKSsDYpj14lgMTpeTVexF9jnBWRmkMqt5PCXzvrM5tCJ/oY4f oFEuYOVeuZpRhH9EDntufCAuGDg67AeXV/VZhYBlnW0LCQzfhZt+5PQRyCLSO+z1PHZr iB5VFpGscwRhqGM047qozG0TlRShPIlz2q5bBD5unuhO7Y0yPG/vTnfvUWBeignPyGV5 uUb8OUvdPqHiHgE0sB5wgwAzNusZP5Ju9GD/SGZg6VMwwxCiv3bYS7BMdFcgIabD5KyM iAUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718381581; x=1718986381; 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=7gUwZpSkwFU+FWUT8qGAdEg7M+9bZN02qOvPWnijOmA=; b=OXyQAwynDkph6rM+Ic9XWCG55ZhqAisKwOocBmcbvPh2m04Gos5KOCjXohIIxhDqM1 yWGx10bcDZAClvZHHkWuOq3gGEWiseWzF+R49PJ7VpWtJoEnf5jG1UQGuG2kq2fSFUnW ye3D/Ne3Cc9CftkRV4dLxb45Y8MSohL6Jaxlw+tD9g2CzQoOsh+elDVNzrJu1V1/Q/sj YSAc3ODT3uhY53M490lcjRha0KhTaJEg0CxeO5VIkVcJGPNUFyE6pGdEcc7iBNxIYgJy kJkX/IrfCT3mqj3sl7ZRvR42FY6xEJqAVhrF8fTPbcoaMFbK5R46YNIA9ELzq7/yOK1o Cg8A== X-Forwarded-Encrypted: i=1; AJvYcCU83RmMtpETtR/K45z5Jv3vMkGiWh/Wpo4Rnd/oU8CU0yfIdoyulYB1DjD8FqIfBhl80y7rNVifM6jPP/jgoqC1QTQ= X-Gm-Message-State: AOJu0Yw0xVR2vCZwPxafAJkfEKnUOOyrAQBtz+MVHX8N7xd4Gqct4bgY i9ycpqJHZ7m5zD6lFY1bjIhrfswZxpnDLfyM+TfNPBUUanQNLsXvOMU8WMXvMXnRsqh2zxPQ8qu JaA== X-Google-Smtp-Source: AGHT+IGlVfs98G7fCfwtsxB7s1pHGkpT0a32/JmIPXufrERUYhHEg26qK2yxEIeC4poHP/k7VOCg4R/uWRE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:4294:b0:2c2:c6fa:c05d with SMTP id 98e67ed59e1d1-2c4dbf336famr123419a91.9.1718381580584; Fri, 14 Jun 2024 09:13:00 -0700 (PDT) Date: Fri, 14 Jun 2024 09:12:59 -0700 In-Reply-To: Mime-Version: 1.0 References: <20240611002145.2078921-1-jthoughton@google.com> <20240611002145.2078921-5-jthoughton@google.com> Message-ID: Subject: Re: [PATCH v5 4/9] mm: Add test_clear_young_fast_only MMU notifier From: Sean Christopherson To: James Houghton Cc: Yu Zhao , Andrew Morton , Paolo Bonzini , Ankit Agrawal , Axel Rasmussen , Catalin Marinas , David Matlack , David Rientjes , James Morse , Jonathan Corbet , Marc Zyngier , Oliver Upton , Raghavendra Rao Ananta , Ryan Roberts , Shaoqin Huang , Suzuki K Poulose , Wei Xu , Will Deacon , Zenghui Yu , kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 062E012001C X-Stat-Signature: 1w3z57u41qjqks5yp71h7yedp6ffxnuq X-Rspam-User: X-HE-Tag: 1718381581-938434 X-HE-Meta: U2FsdGVkX19UrEMhHhVE5HOue6kJyUwv9QaftTz8CPlsLqf02XpYqgVyLTe6220fPk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nYOANOaQoeocuVcPWTvhm6U4jMRV1IRGSpWV5O7zUvMCS1A0DCuUPnnRtje69Ds2HAdHstF0Bum/PcC1EG9peVdRvOjxK2aCfsF9cwipSsDgh61xMkc3AAwp8zOCxrdUDtVcYB+t2DbQ8uV7UwJ4WAS/i4DH7SvCTqHT+aEhkgsGR403qj+yUnX9B4PaKwgtkOjdiYJbiPrVyYGvHuMtCQbCVHiFvjfzPV8FU6Ua7j2iHeJpKfJqrwNLjumV/hnvx3K/45hDOCv5lPUoHqp0AmIMwjukhwvZXoV2QBn+JbpZYbYvekEir0dXoq9VuHHMeX12ovTKaN7u4R6+yOJGVwgdeg4NQKrNnY0rUgVVExsD9tlRondNHgMXB8PK5X4yTPqQAHAbYB8tV/GSqCEmbfm6Mv//ZXNGzvxrJJOpW+5KECkzgtciWZXFMC22Yqi9yTvvGmuxnPVfcQF X-Bogosity: Ham, tests=bogofilter, spamicity=0.000014, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Thu, Jun 13, 2024, James Houghton wrote: > On Tue, Jun 11, 2024 at 5:34=E2=80=AFPM Sean Christopherson wrote: > > A flag would also avoid an indirect call and thus a RETPOLINE when CONF= IG_RETPOLINE=3Dy, > > i.e. would be a minor optimization when KVM doesn't suppport fast aging= . But that's > > probably a pretty unlikely combination, so it's probably not a valid ar= gument. > > > > So, I guess I don't have a strong opinion? >=20 > (Sorry for the somewhat delayed response... spent some time actually > writing what this would look like.) >=20 > I see what you mean, thanks! So has_fast_aging might be set by KVM if > the architecture sets a Kconfig saying that it understands the concept > of fast aging, basically what the presence of this v5's > test_clear_young_fast_only() indicates. It would need to be a runtime setting, because KVM x86-64 with tdp_mmu_enab= led=3Dfalse doesn't support fast aging (uses the shadow MMU even for TDP). > > I don't understand where the "must check shadow MMU" in #4 comes from. = I also > > don't think it's necessary; see below. >=20 > I just meant `kvm_has_shadow_mmu_sptes()` or > `kvm_memslots_have_rmaps()`. I like the logic you suggest below. :) >=20 > > > Some of this reordering (and maybe a change from > > > kvm_shadow_root_allocated() to checking indirect_shadow_pages or > > > something else) can be done in its own patch. >=20 > So just to be clear, for test_young(), I intend to have a patch in v6 > to elide the shadow MMU check if the TDP MMU indicates Accessed. Seems > like a pure win; no reason not to include it if we're making logic > changes here anyway. I don't think that's correct. The initial fast_only=3Dfalse aging should p= rocess shadow MMUs (nested TDP) and TDP MMUs, otherwise a future fast_only=3Dfalse= would get a false positive on young due to failing to clear the Accessed bit in t= he shadow MMU. E.g. if page X is accessed by both L1 and L2, then aged, and n= ever accessed again, the Accessed bit would still be set in the page tables for = L2. My thought for MMU_NOTIFY_WAS_FAST below (which again is a bad name) is to communicate to MGLRU that the page was found to be young in an MMU that sup= ports fast aging, i.e. that looking around at other SPTEs is worth doing. > > > > So rather than failing the fast aging, I think what we want is to k= now if an > > > > mmu_notifier found a young SPTE during a fast lookup. E.g. somethi= ng like this > > > > in KVM, where using kvm_has_shadow_mmu_sptes() instead of kvm_memsl= ots_have_rmaps() > > > > is an optional optimization to avoid taking mmu_lock for write in p= aths where a > > > > (very rare) false negative is acceptable. > > > > > > > > static bool kvm_has_shadow_mmu_sptes(struct kvm *kvm) > > > > { > > > > return !tdp_mmu_enabled || READ_ONCE(kvm->arch.indirect_sha= dow_pages); > > > > } > > > > > > > > static int __kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *r= ange, > > > > bool fast_only) > > > > { > > > > int young =3D 0; > > > > > > > > if (!fast_only && kvm_has_shadow_mmu_sptes(kvm)) { > > > > write_lock(&kvm->mmu_lock); > > > > young =3D kvm_handle_gfn_range(kvm, range, kvm_age_= rmap); > > > > write_unlock(&kvm->mmu_lock); > > > > } > > > > > > > > if (tdp_mmu_enabled && kvm_tdp_mmu_age_gfn_range(kvm, range= )) > > > > young =3D 1 | MMU_NOTIFY_WAS_FAST; >=20 > The most straightforward way (IMHO) to return something like `1 | > MMU_NOTIFY_WAS_FAST` up to the MMU notifier itself is to make > gfn_handler_t return int instead of bool. Hrm, all the options are unpleasant. Modifying gfn_handler_t to return an = int will require an absurd amount of churn (all implementations in all archictu= res), and I don't love that the APIs that return true/false to indicate "flush" w= ould lose their boolean-ness. One idea would be to add kvm_mmu_notifier_arg.aging_was_fast or so, and the= n refactor kvm_handle_hva_range_no_flush() into a dedicated aging helper, and= have it morph the KVM-internal flag into an MMU_NOTIFIER flag. It's not perect = either, but it requires far less churn and keeps some of the KVM<=3D>mmu_notifer de= tails in common KVM code. diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 7b9d2633a931..c11a359b6ff5 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -258,6 +258,7 @@ int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu); #ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER union kvm_mmu_notifier_arg { unsigned long attributes; + bool aging_was_fast; }; =20 struct kvm_gfn_range { diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 436ca41f61e5..a936f6bedd97 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -685,10 +685,10 @@ static __always_inline int kvm_handle_hva_range(struc= t mmu_notifier *mn, return __kvm_handle_hva_range(kvm, &range).ret; } =20 -static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifi= er *mn, - unsigned long star= t, - unsigned long end, - gfn_handler_t hand= ler) +static __always_inline int kvm_age_hva_range(struct mmu_notifier *mn, + unsigned long start, + unsigned long end, + bool flush_if_young) { struct kvm *kvm =3D mmu_notifier_to_kvm(mn); const struct kvm_mmu_notifier_range range =3D { @@ -696,11 +696,14 @@ static __always_inline int kvm_handle_hva_range_no_fl= ush(struct mmu_notifier *mn .end =3D end, .handler =3D handler, .on_lock =3D (void *)kvm_null_fn, - .flush_on_ret =3D false, + .flush_on_ret =3D flush_if_young, .may_block =3D false, + .aging_was_fast =3D false, }; =20 - return __kvm_handle_hva_range(kvm, &range).ret; + bool young =3D __kvm_handle_hva_range(kvm, &range).ret; + + return (int)young | (range.aging_was_fast ? MMU_NOTIFIER_FAST_AGING= : 0); } =20 void kvm_mmu_invalidate_begin(struct kvm *kvm) @@ -865,7 +868,7 @@ static int kvm_mmu_notifier_clear_flush_young(struct mm= u_notifier *mn, { trace_kvm_age_hva(start, end); =20 - return kvm_handle_hva_range(mn, start, end, kvm_age_gfn); + return kvm_age_hva_range(mn, start, end, true); } =20 static int kvm_mmu_notifier_clear_young(struct mmu_notifier *mn, @@ -875,20 +878,7 @@ static int kvm_mmu_notifier_clear_young(struct mmu_not= ifier *mn, { trace_kvm_age_hva(start, end); =20 - /* - * Even though we do not flush TLB, this will still adversely - * affect performance on pre-Haswell Intel EPT, where there is - * no EPT Access Bit to clear so that we have to tear down EPT - * tables instead. If we find this unacceptable, we can always - * add a parameter to kvm_age_hva so that it effectively doesn't - * do anything on clear_young. - * - * Also note that currently we never issue secondary TLB flushes - * from clear_young, leaving this job up to the regular system - * cadence. If we find this inaccurate, we might come up with a - * more sophisticated heuristic later. - */ - return kvm_handle_hva_range_no_flush(mn, start, end, kvm_age_gfn); + return kvm_age_hva_range(mn, start, end, false); } =20 static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn, @@ -897,8 +887,7 @@ static int kvm_mmu_notifier_test_young(struct mmu_notif= ier *mn, { trace_kvm_test_age_hva(address); =20 - return kvm_handle_hva_range_no_flush(mn, address, address + 1, - kvm_test_age_gfn); + return kvm_age_hva_range(mn, address, address + 1, false); } =20 static void kvm_mmu_notifier_release(struct mmu_notifier *mn, > > The change, relative to v5, that I am proposing is that MGLRU looks aro= und if > > the page was young in _a_ "fast" secondary MMU, whereas v5 looks around= if and > > only if _all_ secondary MMUs are fast. > > > > In other words, if a fast MMU had a young SPTE, look around _that_ MMU,= via the > > fast_only flag. >=20 > Oh, yeah, that's a lot more intelligent than what I had. I think I > fully understand your suggestion; I guess we'll see in v6. :) >=20 > I wonder if this still makes sense if whether or not an MMU is "fast" > is determined by how contended some lock(s) are at the time. No. Just because a lock wasn't contended on the initial aging doesn't mean= it won't be contended on the next round. E.g. when using KVM x86's shadow MMU= , which takes mmu_lock for write for all operations, an aging operation could get l= ucky and sneak in while mmu_lock happened to be free, but then get stuck behind = a large queue of operations. The fast-ness needs to be predictable and all but guaranteed, i.e. lockless= or in an MMU that takes mmu_lock for read in all but the most rare paths.