From: Rik van Riel <riel@surriel.com>
To: Bing Jiao <bingjiao@google.com>,
x86@kernel.org, linux-mm@kvack.org,
Dave Hansen <dave.hansen@linux.intel.com>
Cc: Andy Lutomirski <luto@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Thomas Gleixner <tglx@kernel.org>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
"H. Peter Anvin" <hpa@zytor.com>,
Andrew Morton <akpm@linux-foundation.org>,
Huang Ying <ying.huang@linux.alibaba.com>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Juergen Gross <jgross@suse.com>,
Hou Wenlong <houwenlong.hwl@antgroup.com>,
Chuyi Zhou <zhouchuyi@bytedance.com>,
Xie Yuanbin <qq570070308@gmail.com>,
linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH 2/2] x86/mm/tlb: Skip stale and lazy CPUs in arch_tlbbatch_flush()
Date: Tue, 01 Dec 2026 23:19:33 -0500 [thread overview]
Message-ID: <c9b513908e0629312892d39bfce66c65687dba1b.camel@surriel.com> (raw)
In-Reply-To: <20261103012626.2699661-3-bingjiao@google.com>
On Tue, 2026-11-03 at 01:26 +0000, Bing Jiao wrote:
> Keeping
> the array at 8 entries fits within a single 64-byte cache line in
> task_struct->tlb_ubc, and batches spanning more than 8 mms fall back
> to
> the unfiltered cpumask.
Nit: nothing seems to actually cache line align this
thing, but also it probably doesn't matter, so...
>
> +++ b/arch/x86/include/asm/tlbbatch.h
> @@ -4,12 +4,26 @@
>
> struct arch_tlbflush_unmap_batch {
> /*
> * Each bit set is a CPU that potentially has a TLB entry
> for one of
> * the PFNs being flushed..
> */
> struct cpumask cpumask;
> +
> + /*
> + * Unique mm_struct pointers unmapped in this batch. Used
> only for
> + * pointer equality against per-CPU loaded_mm in
> arch_tlbbatch_flush()
> + * and never dereferenced there. nr_mms >
> ARCH_TLBBATCH_MAX_MMS
> + * indicates overflow.
> + */
> + struct mm_struct *mms[ARCH_TLBBATCH_MAX_MMS];
This comment tells us what the code does, but not
why. Maybe something like this?
/*
* Track which mms actually need flushing.
* Used in arch_tlbbatch_flush() to skip flushing
* CPUs that do not have any of these mms loaded.
* On overflow, flush all the CPUs in the cpumask.
*/
> + unsigned short nr_mms;
> +
> /*
> * Set if pages were unmapped from any MM, even one that
> does not
> * have active CPUs in its cpumask.
> diff --git a/arch/x86/include/asm/tlbflush.h
> b/arch/x86/include/asm/tlbflush.h
> index 4ec71ea0943f..0e86ade83ebc 100644
> --- a/arch/x86/include/asm/tlbflush.h
> +++ b/arch/x86/include/asm/tlbflush.h
> @@ -379,8 +379,22 @@ static inline void
> arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *b
> struct mm_struct *mm, unsigned long start, unsigned
> long end)
> {
> inc_mm_tlb_gen(mm);
> - if (!cpu_feature_enabled(X86_FEATURE_INVLPGB))
> + if (!cpu_feature_enabled(X86_FEATURE_INVLPGB)) {
> + unsigned short i, nr = batch->nr_mms;
> +
> cpumask_or(&batch->cpumask, &batch->cpumask,
> mm_cpumask(mm));
> +
> + if (likely(nr <= ARCH_TLBBATCH_MAX_MMS)) {
> + for (i = nr; i > 0; i--) {
> + if (batch->mms[i - 1] == mm)
> + goto out;
> + }
> + if (likely(nr < ARCH_TLBBATCH_MAX_MMS))
> + batch->mms[nr] = mm;
> + batch->nr_mms = nr + 1;
> + }
This block could use a one-line comment explaining
why it's there.
> + }
> +out:
> batch->unmapped_pages = true;
> mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL);
> }
> diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c
> index 0c4da320831c..10b8e839e964 100644
> --- a/arch/x86/mm/tlb.c
> +++ b/arch/x86/mm/tlb.c
> @@ -1679,12 +1679,48 @@ void __flush_tlb_all(void)
> }
> EXPORT_SYMBOL_FOR_KVM(__flush_tlb_all);
>
> +static bool should_flush_tlb_batch(int cpu,
> + const struct
> arch_tlbflush_unmap_batch *batch)
> +{
> + struct mm_struct *loaded_mm =
> READ_ONCE(per_cpu(cpu_tlbstate.loaded_mm, cpu));
> + unsigned short i;
> +
> + if (loaded_mm != LOADED_MM_SWITCHING) {
> + for (i = 0; i < batch->nr_mms; i++) {
> + if (loaded_mm == batch->mms[i])
> + goto check_lazy;
> + }
> + return false;
> + }
This block could also use some comment.
/* Skip the flush if the CPU is running an unaffected mm */
> +
> +check_lazy:
> + /*
> + * Order 'loaded_mm' and 'is_lazy' against their write
> ordering in
> + * switch_mm_irqs_off(). Ensure 'is_lazy' is at least as new
> as
> + * 'loaded_mm'.
> + */
> + smp_rmb();
> +
> + return !READ_ONCE(per_cpu(cpu_tlbstate_shared.is_lazy,
> cpu));
> +}
> +
> void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
> {
> struct flush_tlb_info info;
> bool remote_flush = false;
> int cpu = get_cpu();
>
> + if (likely(!cpu_feature_enabled(X86_FEATURE_INVLPGB) &&
> + batch->nr_mms && batch->nr_mms <=
> ARCH_TLBBATCH_MAX_MMS)) {
Should the overflow test just be moved into
should_flush_tlb_batch() for readability?
Or could this entire block be moved into
a helper function that explains what the code
is for?
Maybe adjust_tlbbatch_flush_cpumask() ?
> + int target_cpu;
> +
> + for_each_cpu(target_cpu, &batch->cpumask) {
> + if (!should_flush_tlb_batch(target_cpu,
> batch))
> + __cpumask_clear_cpu(target_cpu,
> &batch->cpumask);
> + }
> + }
> + batch->nr_mms = 0;
> +
> init_flush_tlb_info(&info, NULL, 0, TLB_FLUSH_ALL, 0, false,
> TLB_GENERATION_INVALID);
> /*
> --
> 2.56.0.385.gac671c819a-goog
>
>
--
All Rights Reversed.
prev parent reply other threads:[~2026-12-02 4:21 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-03 1:26 [RFC PATCH 0/2] x86/mm/tlb: Skip stale and lazy CPUs in batched TLB flushes Bing Jiao
2026-11-03 1:26 ` [RFC PATCH 1/2] x86/mm/tlb: Skip batch->cpumask updates when X86_FEATURE_INVLPGB is enabled Bing Jiao
2026-12-02 3:35 ` Rik van Riel
2026-11-03 1:26 ` [RFC PATCH 2/2] x86/mm/tlb: Skip stale and lazy CPUs in arch_tlbbatch_flush() Bing Jiao
2026-12-02 4:19 ` Rik van Riel [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c9b513908e0629312892d39bfce66c65687dba1b.camel@surriel.com \
--to=riel@surriel.com \
--cc=akpm@linux-foundation.org \
--cc=bigeasy@linutronix.de \
--cc=bingjiao@google.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=houwenlong.hwl@antgroup.com \
--cc=hpa@zytor.com \
--cc=jgross@suse.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=luto@kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=qq570070308@gmail.com \
--cc=tglx@kernel.org \
--cc=x86@kernel.org \
--cc=ying.huang@linux.alibaba.com \
--cc=zhouchuyi@bytedance.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®