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 729B5C27C6E for ; Fri, 14 Jun 2024 18:27:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 042936B0159; Fri, 14 Jun 2024 14:27:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EF2E16B00DB; Fri, 14 Jun 2024 14:27:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A6F636B00EA; Fri, 14 Jun 2024 14:27:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 05A366B0159 for ; Fri, 14 Jun 2024 14:24:01 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id BD011A1F19 for ; Fri, 14 Jun 2024 18:24:01 +0000 (UTC) X-FDA: 82230318282.23.CF71EFD Received: from mail-qt1-f174.google.com (mail-qt1-f174.google.com [209.85.160.174]) by imf20.hostedemail.com (Postfix) with ESMTP id EAD291C0006 for ; Fri, 14 Jun 2024 18:23:59 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=27o1q3C+; spf=pass (imf20.hostedemail.com: domain of jthoughton@google.com designates 209.85.160.174 as permitted sender) smtp.mailfrom=jthoughton@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718389438; 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=+e1S7VxRmA5wGFSxD1T8KbdKKkr/h73427zkqEMDfd4=; b=d2TpFs2wD6OlcH2lps/7HnMrvD6TtpIRXelFMljWu/hZb7mNT7+9xolIwcrsgoaua622pn 49+Puv1ZQmNBMZ8MzVGx8zdR09SUTiwYGcJ66IctFlMTGm7xePkNGqaUZiZ5ItWAWKQKZh ymlAcXPrKKpPjFzSJK6uq6Sax1L0re0= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=27o1q3C+; spf=pass (imf20.hostedemail.com: domain of jthoughton@google.com designates 209.85.160.174 as permitted sender) smtp.mailfrom=jthoughton@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718389438; a=rsa-sha256; cv=none; b=oTi9gjIJ6ZF4rB/DtrMRlnsbtbtLbQSGl2MrILLPYs2Soh1fceJEx2NSSqRUgGOWTnWypV 3tppfll6EiunieP2c5oXa87tcAq0sutkf1ZfoMD2tSu64mRs2EiIm12O2phkHkNxYQ9vQH qsAgLnniHvAOyWlLMtdZO2m3O+QiUK0= Received: by mail-qt1-f174.google.com with SMTP id d75a77b69052e-44056f72257so45131cf.0 for ; Fri, 14 Jun 2024 11:23:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718389439; x=1718994239; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=+e1S7VxRmA5wGFSxD1T8KbdKKkr/h73427zkqEMDfd4=; b=27o1q3C+S6Y7mt/jWlFkqQSsDFqtZk+pYzk0SBUogG+YaowHvErNJVmZI/MJmnJ/Z1 +04ypbVgzi196HMK5kVgEmQWTVfG9SxSPxMplWQDP6xh5yXSdoxbiOLBLa2m/lwWfxul 2GpdKH+jCipS96avsUcLe/5VFiMXtZAUhbcNVEysDzlbKJ4VOzOJZ2W6ZX9FjkVQALiq p3t8IaieuK3e0IKuQWVzEgRXCoQTKlxbcQm7dgmLPgL49g6JDt0aJ9p9nzfPLSkmlo9Q VPoIz843aaCBKewyo8x0Nn77NTbtQkn/teRYa9VlfDeV0pvSBOVMYYX4EtHwIkuGA/56 q4uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718389439; x=1718994239; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+e1S7VxRmA5wGFSxD1T8KbdKKkr/h73427zkqEMDfd4=; b=W31grpWOxCY+L020IPkLDCxO/r5j6iLeiRerL5kb2wT24seE0svS3G/s/HpmtWURom d3lCz6QACZRYpTeDeerBhZUON1fhlERQyUq/cAdLHrfN0gkp1/FGDLSqWDzMLlNQ4vmI LNjsSvG/EkCgZ4/VQ6juUXhIaEnW3cZ2ng2JI07o2AQeLGHDkSQkxWbLiwc/W7aRtwmW n+l9uRbic63VD1BDkj43QigKGkBkdGcMyg+KvA1m1gl8JbcjKQefB4ivLA/T3qCTRlv9 4qlmTnfA9QME2PtzaST2XZ1YK6XSw49s6Ks0gAIUwa9UDS5njw3PSsw8ynuiaZ/Plp45 SBKg== X-Forwarded-Encrypted: i=1; AJvYcCWazyiK4mIsY8hYcfDBp1C3SsCvGoGe4VuERfGXTWxiPlXjZkTViOiX/db8TJHPwgVWNf/leEsixcbvtAroEUKB5ds= X-Gm-Message-State: AOJu0Yy2SiSt18x1jKMrjNjnNNjZm6f4tcWYbc6WyxwBCU0gwdxYUCdu tangEssCtKqnFB1qYq170mQ2CaT1BiLRqYrlee9rSCTvn5qFfJafN5sBqrG+FMdq3a3R4eLk87U ISFDGhaGBvvB40jrtcmuO9Ot0SzZXLGhMhHKA X-Google-Smtp-Source: AGHT+IH+9subwK96UF5eNA7C1xIBVfzLuBaLXzzEgBSMa1vnBDab4AZtlky3wQKAROgOb6aooh80p+OgpnovU+m6mmo= X-Received: by 2002:ac8:5a08:0:b0:441:630e:110a with SMTP id d75a77b69052e-4434f82a2e6mr155091cf.17.1718389438198; Fri, 14 Jun 2024 11:23:58 -0700 (PDT) MIME-Version: 1.0 References: <20240611002145.2078921-1-jthoughton@google.com> <20240611002145.2078921-5-jthoughton@google.com> In-Reply-To: From: James Houghton Date: Fri, 14 Jun 2024 11:23:21 -0700 Message-ID: Subject: Re: [PATCH v5 4/9] mm: Add test_clear_young_fast_only MMU notifier To: Sean Christopherson 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-Stat-Signature: cwyox784a5isps336r9kinm1fqnobwo1 X-Rspam-User: X-Rspamd-Queue-Id: EAD291C0006 X-Rspamd-Server: rspam02 X-HE-Tag: 1718389439-300968 X-HE-Meta: U2FsdGVkX1+L0/ok12yYS74Dr6xJwMtNCfbvv+yTP0rjsbOk0MuvZW6yQqWyk+b+uX67rsLywjrdz325QDCuB2itfUIz27yqXU/YFmNh6L6FCkEYu6HotSVY4nDzQOnizFljFwjoLXtUNddN/y3TjuggZW80/6oiEe37Ugk/pjyjTVadtV+PqyFEvYuFo0maWBEzFv5qDwbvrF7Nm4+Jam5Lz2ajrKkIV815UcQNBlOk1VOnXZ5czSR5tSh3A940j2aeOHDCaWcNFtSd2UwrL39/R66Qv4wXCxg7ZvavDF4NVU5zhaQ0U7DbKWyeLNzQHltUeT5WFIutXWHyaJ2dQL5x6V/k7BtwMe0s3TyD4ofThcxxfvN3MRcxHmVBgYzWGskm5SYH0aGQbTv9sBJQvbqh5W33U64NSjzUNqBDdELri7d2mCsBrqPy3z/AIvAOGCVNbj1Tvs44h2TaBC2eMpN5+8K2RkcyAtNEzqgJRuUobcuKz1Fl6WJuO0w9Rav6j0Qjftem5dVp7qC15Naee52ae9m9h2A1r1KUVHQ7pP8MVS+SEOkZBB2gZOQEAmepl5a5SnJjVhpGEVwbrC7uWI5++av0Tc/2W24tBcGODPwtuthJz6U+Y1+6+Rm/nYKGptcaZQcPpU2spzrA7qayI7Pz3apIt2+4HY3GhKuRSdC2bQK/hzzB04913wwzgOy7mgnvU9nTJfyC3O/IYO1Mn3qZHeTFbVFEZ0NGq61kJLoLmdU1az7VAZ7SzsGIybKI6hnoL58HR31ui0Svce+2R2XS3qAnN+1M1/Vqbtst2zBI86TrIOEWGOhDFLvBYra7aCJUtJyVT8HZ8u8k0AKRAwEFyEUv5XnVl/yul4YgXq0R9PyHX3KpeWO5pdSGkmIXo9yl7JTsknFlIeYVl950j10GNzGXsBASajdiyApKUjDySn8CzW1lK/Kn4oi2EckPDXdgqgz/lN9d9M8/kJK aRdrstAH kH+FMXLNPSMbRidNp0KDkeayuDKPfdUhhp7VWOrWM7CcN3ZwH/kRnrnYRN/YTBEwgwZPMseGB6UiRSJudI3W+dGk4Q14rG8FvxHmq6EYaMaVRd2feXot7M1ES2HIKP1YJkhhIjsDgoaf2hb3ByzzCTKSvTKYbVNyi8wI1SULgRI3q8oA243viURsfOFX9T5Wpoh6mXEwtkXSdb4Ax7vv7iDBGK2+TsYd1mZceidgLl5ZElu0m/to1qd0/Zg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, 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 Fri, Jun 14, 2024 at 9:13=E2=80=AFAM Sean Christopherson wrote: > > 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 CO= NFIG_RETPOLINE=3Dy, > > > i.e. would be a minor optimization when KVM doesn't suppport fast agi= ng. But that's > > > probably a pretty unlikely combination, so it's probably not a valid = argument. > > > > > > So, I guess I don't have a strong opinion? > > > > (Sorry for the somewhat delayed response... spent some time actually > > writing what this would look like.) > > > > 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_en= abled=3Dfalse > doesn't support fast aging (uses the shadow MMU even for TDP). I see. I'm not sure if it makes sense to put this in `ops` as you originally had it then (it seems like a bit of a pain anyway). I could just make it a member of `struct mmu_notifier` itself. > > 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= process > shadow MMUs (nested TDP) and TDP MMUs, otherwise a future fast_only=3Dfal= se would > get a false positive on young due to failing to clear the Accessed bit in= the > shadow MMU. E.g. if page X is accessed by both L1 and L2, then aged, and= never > accessed again, the Accessed bit would still be set in the page tables fo= r L2. For clear_young(fast_only=3Dfalse), yeah we need to check and clear Accessed for both MMUs. But for test_young(fast_only=3Dfalse), I don't see why we couldn't just return early if the TDP MMU reports young. > My thought for MMU_NOTIFY_WAS_FAST below (which again is a bad name) is t= o > communicate to MGLRU that the page was found to be young in an MMU that s= upports > fast aging, i.e. that looking around at other SPTEs is worth doing. That makes sense; I don't think this little test_young() optimization affects that. > > > > > So rather than failing the fast aging, I think what we want is to= know if an > > > > > mmu_notifier found a young SPTE during a fast lookup. E.g. somet= hing like this > > > > > in KVM, where using kvm_has_shadow_mmu_sptes() instead of kvm_mem= slots_have_rmaps() > > > > > is an optional optimization to avoid taking mmu_lock for write in= paths 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_s= hadow_pages); > > > > > } > > > > > > > > > > static int __kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range = *range, > > > > > 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_ag= e_rmap); > > > > > write_unlock(&kvm->mmu_lock); > > > > > } > > > > > > > > > > if (tdp_mmu_enabled && kvm_tdp_mmu_age_gfn_range(kvm, ran= ge)) > > > > > young =3D 1 | MMU_NOTIFY_WAS_FAST; > > > > 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 a= n int > will require an absurd amount of churn (all implementations in all archic= tures), > and I don't love that the APIs that return true/false to indicate "flush"= would > lose their boolean-ness. > > One idea would be to add kvm_mmu_notifier_arg.aging_was_fast or so, and t= hen > refactor kvm_handle_hva_range_no_flush() into a dedicated aging helper, a= nd have > it morph the KVM-internal flag into an MMU_NOTIFIER flag. It's not perec= t either, > but it requires far less churn and keeps some of the KVM<=3D>mmu_notifer = details in > common KVM code. SGTM. I think this will work. Thanks! > 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; > }; > > 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(str= uct mmu_notifier *mn, > return __kvm_handle_hva_range(kvm, &range).ret; > } > > -static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_noti= fier *mn, > - unsigned long st= art, > - unsigned long en= d, > - gfn_handler_t ha= ndler) > +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_= flush(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, > }; > > - 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_AGI= NG : 0); > } > > void kvm_mmu_invalidate_begin(struct kvm *kvm) > @@ -865,7 +868,7 @@ static int kvm_mmu_notifier_clear_flush_young(struct = mmu_notifier *mn, > { > trace_kvm_age_hva(start, end); > > - return kvm_handle_hva_range(mn, start, end, kvm_age_gfn); > + return kvm_age_hva_range(mn, start, end, true); > } > > static int kvm_mmu_notifier_clear_young(struct mmu_notifier *mn, > @@ -875,20 +878,7 @@ static int kvm_mmu_notifier_clear_young(struct mmu_n= otifier *mn, > { > trace_kvm_age_hva(start, end); > > - /* > - * 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); > } > > static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn, > @@ -897,8 +887,7 @@ static int kvm_mmu_notifier_test_young(struct mmu_not= ifier *mn, > { > trace_kvm_test_age_hva(address); > > - 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); > } > > static void kvm_mmu_notifier_release(struct mmu_notifier *mn, > > > > > The change, relative to v5, that I am proposing is that MGLRU looks a= round if > > > the page was young in _a_ "fast" secondary MMU, whereas v5 looks arou= nd if and > > > only if _all_ secondary MMUs are fast. > > > > > > In other words, if a fast MMU had a young SPTE, look around _that_ MM= U, via the > > > fast_only flag. > > > > 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. :) > > > > 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 me= an it > won't be contended on the next round. E.g. when using KVM x86's shadow M= MU, which > takes mmu_lock for write for all operations, an aging operation could get= lucky > and sneak in while mmu_lock happened to be free, but then get stuck behin= d a large > queue of operations. > > The fast-ness needs to be predictable and all but guaranteed, i.e. lockle= ss or in > an MMU that takes mmu_lock for read in all but the most rare paths. Aging and look-around themselves only use the fast-only notifiers, so they won't ever wait on a lock (well... provided KVM is written like that, which I think is a given). should_look_around() will use the slow notifier because it (despite its name) is responsible for accurately determining if a page is young lest we evict a young page. So in this case where "fast" means "lock not contended for now", I don't think it's necessarily wrong for MGLRU to attempt to find young pages, even if sometimes it will bail out because a lock is contended/held for a few or even a majority of the pages. Not doing look-around is the same as doing look-around and finding that no pages are young. Anyway, I don't think this bit is really all that important unless we can demonstrate that KVM participating like this actually results in a measurable win.