From nobody Mon Feb 9 04:42:27 2026 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.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 981351A4AB4 for ; Thu, 26 Sep 2024 01:35:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727314533; cv=none; b=Fz3tt7XCbNKA6Pi3gWptYFkSWKfLRpl6kQqj27ziL967X1bCvDiiUxpOiTbhfGUbqvahz8ClfgfoFYkhbn/L8wUJPlr+b0CuKsoWHSdVB/4b26VcayaM2PJc4q624vS14tkWsFpXsTdoUfj/TyjVXDyTXRaMGJcpDuex5WslZQA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727314533; c=relaxed/simple; bh=iynxZ4Ry9WaN4XO7OQhzo2JcvREd4yVbmFHSIphWh40=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=n8HQbBb6WpSEqZXH+ODsmUqpad/R5G/whHhCj59E34vLLo7RIHkk5zcst0IzyzS74H/D3Pv+3DdWvlbM19HsfEBcBxrJC1sdGj5JwymHhnjvVKWH6q9yY5QLR+XBjsLvwovG8nIOeTZA1x/xEq6OkEcvSi0cp3xi34b34g4ZDZ0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=VWyL8IMW; arc=none smtp.client-ip=209.85.128.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--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="VWyL8IMW" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e230808455so5355257b3.0 for ; Wed, 25 Sep 2024 18:35:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727314531; x=1727919331; 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=ogisB+p0t7E+MEmwUL2EdeyMfss1r+UivrCLQXciZ0o=; b=VWyL8IMWaAQGG04phvq0Z82R4n3aqoS8tnjLNrvV31bkzDKYLafBHC6bvUn+CxBWhA wgUjKspczotuLzqQkZA08e3t2qzXpHzyFxXGXL3oAqCpuveC4EQXmyq9ndvserDhpQCU YxO64grDH4l7l7CgnhizfmPdsfkHeV/O3t746cdXyk4q9froEqnH4gGhox54CF69qst/ JGnKQ3s8K78MpE8Vo/EggETvPbj44jRIF4iuqglsTJXRS1dzVVnILFkWJ4GtouowoPqD Ez6nRSiklK1H9zgRKk0QeNTmf2xNHpMc9zpK4F3u+4TahGNcWTUoYo+W4sQIRWsRJlFw srRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727314531; x=1727919331; 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=ogisB+p0t7E+MEmwUL2EdeyMfss1r+UivrCLQXciZ0o=; b=pBgb7T6jrlzU2t+1/8MzqxRva+QSnlpVLWe/1+av3GHyUFmDAYWlIH0ReVZWOXeA5/ eaVRPgLbGudJVJ2sHSCiJpJUPfQsbkouFt7r5crE8lfYNH8hpel5HF/gfkTw3YbmLUmF EDVIs3SmEd2ObeU6T2n4IeAZ+xoIuBOnIJOZf56eg+6k5tRW5/VXUGA7fn2ZI4w5FFmr xqHD5rjR2/afIZpuEL/6J2sXm8oMRx/EUhUfsWpic87t9ucICt+ybJwTUdOxMlVMPMp2 jikea9W7n27MPX+rBZ8EZMLrvrp41iT08ouYk32CmpUSWtGktoWN6PW5p00tgI8ilJph aUlw== X-Forwarded-Encrypted: i=1; AJvYcCVo4VSnmoP76IHTbjaj0C8icXSzmEKwJJF38OjpCIa/P6kgiyujQ6F2SHpaHijRgB4Gkj+CBHGDXUM9vHo=@vger.kernel.org X-Gm-Message-State: AOJu0Yy9P8iyeCDEPhiQWwoeqfLdSSUhoEL/gyOtJWpw61heKX8By95Z YY7BomEfG9hOr+XMqh2ZtU8U0jN5/IeRsOc4ZW3ZC8kERAPYwEjcFhsFb9xLDoH+MXG4YAVxdI6 XNQZS+gXXX01yr5yFXg== X-Google-Smtp-Source: AGHT+IEFdzw0O48kZ1pdx1YoauWRvV0zu2R4nMdxsgoP5v/WIUZXOoAYIDJzZnEXzkuO0wTdBSaqUczWpwalNBYg X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:13d:fb22:ac12:a84b]) (user=jthoughton job=sendgmr) by 2002:a05:690c:5292:b0:62c:f976:a763 with SMTP id 00721157ae682-6e21d6c27c9mr136077b3.1.1727314530709; Wed, 25 Sep 2024 18:35:30 -0700 (PDT) Date: Thu, 26 Sep 2024 01:35:01 +0000 In-Reply-To: <20240926013506.860253-1-jthoughton@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240926013506.860253-1-jthoughton@google.com> X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240926013506.860253-14-jthoughton@google.com> Subject: [PATCH v7 13/18] mm: Add fast_only bool to test_young and clear_young MMU notifiers From: James Houghton To: Sean Christopherson , Paolo Bonzini Cc: Andrew Morton , David Matlack , David Rientjes , James Houghton , Jason Gunthorpe , Jonathan Corbet , Marc Zyngier , Oliver Upton , Wei Xu , Yu Zhao , Axel Rasmussen , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" For implementers, the fast_only bool indicates that the age information needs to be harvested such that we do not slow down other MMU operations, and ideally that we are not ourselves slowed down by other MMU operations. Usually this means that the implementation should be lockless. Also add mmu_notifier_test_young_fast_only() and mmu_notifier_clear_young_fast_only() helpers to set fast_only for these notifiers. Signed-off-by: James Houghton --- include/linux/mmu_notifier.h | 61 ++++++++++++++++++++++++++++++++---- include/trace/events/kvm.h | 19 ++++++----- mm/mmu_notifier.c | 18 ++++++++--- virt/kvm/kvm_main.c | 12 ++++--- 4 files changed, 88 insertions(+), 22 deletions(-) diff --git a/include/linux/mmu_notifier.h b/include/linux/mmu_notifier.h index 37643fa43687..7c17e2871c66 100644 --- a/include/linux/mmu_notifier.h +++ b/include/linux/mmu_notifier.h @@ -106,21 +106,38 @@ struct mmu_notifier_ops { * clear_young is a lightweight version of clear_flush_young. Like the * latter, it is supposed to test-and-clear the young/accessed bitflag * in the secondary pte, but it may omit flushing the secondary tlb. + * + * The fast_only parameter indicates that this call should not block, + * and this function should not cause other MMU notifier calls to + * block. Usually this means that the implementation should be + * lockless. + * + * When called with fast_only, this notifier will be a no-op (and + * return that the range is NOT young), unless has_fast_aging is set + * on the struct mmu_notifier. + * + * When fast_only is true, if the implementer cannot determine that a + * range is young without blocking, it should return 0 (i.e., that + * the range is NOT young). */ int (*clear_young)(struct mmu_notifier *subscription, struct mm_struct *mm, unsigned long start, - unsigned long end); + unsigned long end, + bool fast_only); =20 /* * test_young is called to check the young/accessed bitflag in * the secondary pte. This is used to know if the page is * frequently used without actually clearing the flag or tearing * down the secondary mapping on the page. + * + * The fast_only parameter has the same meaning as with clear_young. */ int (*test_young)(struct mmu_notifier *subscription, struct mm_struct *mm, - unsigned long address); + unsigned long address, + bool fast_only); =20 /* * invalidate_range_start() and invalidate_range_end() must be @@ -381,9 +398,11 @@ extern int __mmu_notifier_clear_flush_young(struct mm_= struct *mm, unsigned long end); extern int __mmu_notifier_clear_young(struct mm_struct *mm, unsigned long start, - unsigned long end); + unsigned long end, + bool fast_only); extern int __mmu_notifier_test_young(struct mm_struct *mm, - unsigned long address); + unsigned long address, + bool fast_only); extern bool __mm_has_fast_young_notifiers(struct mm_struct *mm); extern int __mmu_notifier_invalidate_range_start(struct mmu_notifier_range= *r); extern void __mmu_notifier_invalidate_range_end(struct mmu_notifier_range = *r); @@ -418,7 +437,16 @@ static inline int mmu_notifier_clear_young(struct mm_s= truct *mm, unsigned long end) { if (mm_has_notifiers(mm)) - return __mmu_notifier_clear_young(mm, start, end); + return __mmu_notifier_clear_young(mm, start, end, false); + return 0; +} + +static inline int mmu_notifier_clear_young_fast_only(struct mm_struct *mm, + unsigned long start, + unsigned long end) +{ + if (mm_has_notifiers(mm)) + return __mmu_notifier_clear_young(mm, start, end, true); return 0; } =20 @@ -426,7 +454,15 @@ static inline int mmu_notifier_test_young(struct mm_st= ruct *mm, unsigned long address) { if (mm_has_notifiers(mm)) - return __mmu_notifier_test_young(mm, address); + return __mmu_notifier_test_young(mm, address, false); + return 0; +} + +static inline int mmu_notifier_test_young_fast_only(struct mm_struct *mm, + unsigned long address) +{ + if (mm_has_notifiers(mm)) + return __mmu_notifier_test_young(mm, address, true); return 0; } =20 @@ -622,12 +658,25 @@ static inline int mmu_notifier_clear_young(struct mm_= struct *mm, return 0; } =20 +static inline int mmu_notifier_clear_young_fast_only(struct mm_struct *mm, + unsigned long start, + unsigned long end) +{ + return 0; +} + static inline int mmu_notifier_test_young(struct mm_struct *mm, unsigned long address) { return 0; } =20 +static inline int mmu_notifier_test_young_fast_only(struct mm_struct *mm, + unsigned long address) +{ + return 0; +} + static inline bool mm_has_fast_young_notifiers(struct mm_struct *mm) { return 0; diff --git a/include/trace/events/kvm.h b/include/trace/events/kvm.h index 74e40d5d4af4..6d9485cf3e51 100644 --- a/include/trace/events/kvm.h +++ b/include/trace/events/kvm.h @@ -457,36 +457,41 @@ TRACE_EVENT(kvm_unmap_hva_range, ); =20 TRACE_EVENT(kvm_age_hva, - TP_PROTO(unsigned long start, unsigned long end), - TP_ARGS(start, end), + TP_PROTO(unsigned long start, unsigned long end, bool fast_only), + TP_ARGS(start, end, fast_only), =20 TP_STRUCT__entry( __field( unsigned long, start ) __field( unsigned long, end ) + __field( bool, fast_only ) ), =20 TP_fast_assign( __entry->start =3D start; __entry->end =3D end; + __entry->fast_only =3D fast_only; ), =20 - TP_printk("mmu notifier age hva: %#016lx -- %#016lx", - __entry->start, __entry->end) + TP_printk("mmu notifier age hva: %#016lx -- %#016lx fast_only: %d", + __entry->start, __entry->end, __entry->fast_only) ); =20 TRACE_EVENT(kvm_test_age_hva, - TP_PROTO(unsigned long hva), - TP_ARGS(hva), + TP_PROTO(unsigned long hva, bool fast_only), + TP_ARGS(hva, fast_only), =20 TP_STRUCT__entry( __field( unsigned long, hva ) + __field( bool, fast_only ) ), =20 TP_fast_assign( __entry->hva =3D hva; + __entry->fast_only =3D fast_only; ), =20 - TP_printk("mmu notifier test age hva: %#016lx", __entry->hva) + TP_printk("mmu notifier test age hva: %#016lx fast_only: %d", + __entry->hva, __entry->fast_only) ); =20 #endif /* _TRACE_KVM_MAIN_H */ diff --git a/mm/mmu_notifier.c b/mm/mmu_notifier.c index c405e5b072cf..f9ec810c8a1b 100644 --- a/mm/mmu_notifier.c +++ b/mm/mmu_notifier.c @@ -404,7 +404,8 @@ bool __mm_has_fast_young_notifiers(struct mm_struct *mm) =20 int __mmu_notifier_clear_young(struct mm_struct *mm, unsigned long start, - unsigned long end) + unsigned long end, + bool fast_only) { struct mmu_notifier *subscription; int young =3D 0, id; @@ -413,9 +414,13 @@ int __mmu_notifier_clear_young(struct mm_struct *mm, hlist_for_each_entry_rcu(subscription, &mm->notifier_subscriptions->list, hlist, srcu_read_lock_held(&srcu)) { + if (fast_only && !subscription->has_fast_aging) + continue; + if (subscription->ops->clear_young) young |=3D subscription->ops->clear_young(subscription, - mm, start, end); + mm, start, end, + fast_only); } srcu_read_unlock(&srcu, id); =20 @@ -423,7 +428,8 @@ int __mmu_notifier_clear_young(struct mm_struct *mm, } =20 int __mmu_notifier_test_young(struct mm_struct *mm, - unsigned long address) + unsigned long address, + bool fast_only) { struct mmu_notifier *subscription; int young =3D 0, id; @@ -432,9 +438,13 @@ int __mmu_notifier_test_young(struct mm_struct *mm, hlist_for_each_entry_rcu(subscription, &mm->notifier_subscriptions->list, hlist, srcu_read_lock_held(&srcu)) { + if (fast_only && !subscription->has_fast_aging) + continue; + if (subscription->ops->test_young) { young =3D subscription->ops->test_young(subscription, mm, - address); + address, + fast_only); if (young) break; } diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index f6c369eccd2a..ec07caaed6b6 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -846,7 +846,7 @@ static int kvm_mmu_notifier_clear_flush_young(struct mm= u_notifier *mn, IS_ENABLED(CONFIG_KVM_MMU_NOTIFIER_YOUNG_LOCKLESS), }; =20 - trace_kvm_age_hva(start, end); + trace_kvm_age_hva(start, end, false); =20 return kvm_handle_hva_range(kvm, &range).ret; } @@ -854,7 +854,8 @@ static int kvm_mmu_notifier_clear_flush_young(struct mm= u_notifier *mn, static int kvm_mmu_notifier_clear_young(struct mmu_notifier *mn, struct mm_struct *mm, unsigned long start, - unsigned long end) + unsigned long end, + bool fast_only) { struct kvm *kvm =3D mmu_notifier_to_kvm(mn); const struct kvm_mmu_notifier_range range =3D { @@ -868,7 +869,7 @@ static int kvm_mmu_notifier_clear_young(struct mmu_noti= fier *mn, IS_ENABLED(CONFIG_KVM_MMU_NOTIFIER_YOUNG_LOCKLESS), }; =20 - trace_kvm_age_hva(start, end); + trace_kvm_age_hva(start, end, fast_only); =20 /* * Even though we do not flush TLB, this will still adversely @@ -888,7 +889,8 @@ static int kvm_mmu_notifier_clear_young(struct mmu_noti= fier *mn, =20 static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn, struct mm_struct *mm, - unsigned long address) + unsigned long address, + bool fast_only) { struct kvm *kvm =3D mmu_notifier_to_kvm(mn); const struct kvm_mmu_notifier_range range =3D { @@ -902,7 +904,7 @@ static int kvm_mmu_notifier_test_young(struct mmu_notif= ier *mn, IS_ENABLED(CONFIG_KVM_MMU_NOTIFIER_YOUNG_LOCKLESS), }; =20 - trace_kvm_test_age_hva(address); + trace_kvm_test_age_hva(address, fast_only); =20 return kvm_handle_hva_range(kvm, &range).ret; } --=20 2.46.0.792.g87dc391469-goog