From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=96.67.55.147 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796185269; b=CltJILyKvyrV5GatXIJoj4WdKPprOqnv+87IX3f/iVzxIG0TRZmS/7k7brte483HkWf0 SbQwbvNeXOJrw6bcU3H/yg4MrTbZDyKWy4rZ0V+NKwv6BlBE+CLJsb+j27XSOSbSdlBq1 NO9M80iFt0d2GbqljiMOfDnSeDZuzsdtW4fyGoomzStxzjEkvzTjwSTca7qDvL6rVXtCk nzEs3628LKiRMjYp/gA0hPyQHThYww4jSUNtFFIx39RWaLr34WM1m+W+LFST6/DK3CE/c T1IREbS3lS+jYXQUBke8ythNRsX5DdQ/Xgt1W4R/DzlX1LjEYubPkWjCtWkoOTxyWNw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796185269; h=DMARC-Filter:DKIM-Signature:Received:Message-ID:Subject:From:To:Cc: Date:In-Reply-To:References:Autocrypt:Content-Type: Content-Transfer-Encoding:User-Agent:MIME-Version; bh=5I2lYOtucWf5RwDkqjkiWrUeOJYkfO/PZOyqJB/flkw=; b=vc9Ug/RIISVkAuS6ChOyUrRGusPr1WNDYE/hKWrtyZdMeJ9HMWBB3cVVrw9PZvYl5pZ6 AJ8iDtkRBG3QFWeJAg9FCdNHwtFHhFFW5tyDQtk7sN1mlwYeQ5c1z6aNHtH22FQ1wk4LT +nu7YLWYZq+QnvieT9wIgz2i45dSuT2iAuJdmSjcysGVcnBdHFsg3UOErJZZaPMlio6c6 VrAfrMKasFDm3ne3X9Jb+3x52lMKGnVn7D1pckQSLrppYJviRKd+xlYaOkhgk6pRe8Q2k 3PiRjsqWlcuwXesNBppOF2NPjT7Km8S4S2e4651hzwEuBcoB9tsRNBL/SJtLsG+Uv8g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=surriel.com; spf=pass smtp.mailfrom=surriel.com; dkim=pass header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b=PtfjkPY7; arc=none smtp.remote-ip=96.67.55.147 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 88AB2606A31A for ; Wed, 2 Dec 2026 04:21:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 88AB2606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=surriel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=surriel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b=PtfjkPY7 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=surriel.com ; s=mail; h=MIME-Version:Content-Transfer-Encoding:Content-Type:References: In-Reply-To:Date:Cc:To:From:Subject:Message-ID:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID; bh=5I2lYOtucWf5RwDkqjkiWrUeOJYkfO/PZOyqJB/flkw=; b=PtfjkP Y75Wz7MKEApnPmVfyA0a17H3b+07PQnjFEgwRK84JcsUuWxxcZ6+r/kbg1UK2mMeJ+8AumOytCqk7 jAZdnk5ekEUqxn3BoZi4oEON0bpWas0vY3pKAU6+DsNXb8bX+ZEtvzkNVMHAVQQI7JLz1tFehHIGX j9R6nVE/lFkTqv5AGOJaUdsf91ychDF+FAWiAYT2upRYNEdzhGE26O+aiwxa6Su4yB3poDnNTxKxi ofwTq8/qNcUTVtxes3BjYcUtU8wjwXU2/N1UHeG00vqZgAjWZUfrX4JcvaMkCx0c9Fl8841HGzJa8 CCAsVMPynyaL8KLvB2HxeW+vHyig==; Received: from [2601:18c:8100:a0e0:5a47:caff:fe78:8708] by shelob.surriel.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.5) (envelope-from ) id 1xYbob-00000001kmV-3HpT; Wed, 02 Dec 2026 04:19:33 +0000 Message-ID: Subject: Re: [RFC PATCH 2/2] x86/mm/tlb: Skip stale and lazy CPUs in arch_tlbbatch_flush() From: Rik van Riel To: Bing Jiao , x86@kernel.org, linux-mm@kvack.org, Dave Hansen Cc: Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Andrew Morton , Huang Ying , Sebastian Andrzej Siewior , Juergen Gross , Hou Wenlong , Chuyi Zhou , Xie Yuanbin , linux-kernel@vger.kernel.org Date: Tue, 01 Dec 2026 23:19:33 -0500 In-Reply-To: <20261103012626.2699661-3-bingjiao@google.com> References: <20261103012626.2699661-1-bingjiao@google.com> <20261103012626.2699661-3-bingjiao@google.com> Autocrypt: addr=riel@surriel.com; prefer-encrypt=mutual; keydata=mQENBFIt3aUBCADCK0LicyCYyMa0E1lodCDUBf6G+6C5UXKG1jEYwQu49cc/gUBTTk33A eo2hjn4JinVaPF3zfZprnKMEGGv4dHvEOCPWiNhlz5RtqH3SKJllq2dpeMS9RqbMvDA36rlJIIo47 Z/nl6IA8MDhSqyqdnTY8z7LnQHqq16jAqwo7Ll9qALXz4yG1ZdSCmo80VPetBZZPw7WMjo+1hByv/ lvdFnLfiQ52tayuuC1r9x2qZ/SYWd2M4p/f5CLmvG9UcnkbYFsKWz8bwOBWKg1PQcaYHLx06sHGdY dIDaeVvkIfMFwAprSo5EFU+aes2VB2ZjugOTbkkW2aPSWTRsBhPHhV6dABEBAAG0HlJpayB2YW4gU mllbCA8cmllbEByZWRoYXQuY29tPokBHwQwAQIACQUCW5LcVgIdIAAKCRDOed6ShMTeg05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2aHDwErhI omYQ0Xluo1WBtH/UmHgirHvclsou1Ks9jyTxiPyUKRfae7GNOFiX99+ZlB27P3t8CjtSO831Ij0Ip QrfooZ21YVlUKw0Wy6Ll8EyefyrEYSh8KTm8dQj4O7xxvdg865TLeLpho5PwDRF+/mR3qi8CdGbkE c4pYZQO8UDXUN4S+pe0aTeTqlYw8rRHWF9TnvtpcNzZw== Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.60.2 (3.60.2-3.fc44) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 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... >=20 > +++ b/arch/x86/include/asm/tlbbatch.h > @@ -4,12 +4,26 @@ >=20 > =C2=A0struct arch_tlbflush_unmap_batch { > =C2=A0 /* > =C2=A0 * Each bit set is a CPU that potentially has a TLB entry > for one of > =C2=A0 * the PFNs being flushed.. > =C2=A0 */ > =C2=A0 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; > + > =C2=A0 /* > =C2=A0 * Set if pages were unmapped from any MM, even one that > does not > =C2=A0 * 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 > =C2=A0 struct mm_struct *mm, unsigned long start, unsigned > long end) > =C2=A0{ > =C2=A0 inc_mm_tlb_gen(mm); > - if (!cpu_feature_enabled(X86_FEATURE_INVLPGB)) > + if (!cpu_feature_enabled(X86_FEATURE_INVLPGB)) { > + unsigned short i, nr =3D batch->nr_mms; > + > =C2=A0 cpumask_or(&batch->cpumask, &batch->cpumask, > mm_cpumask(mm)); > + > + if (likely(nr <=3D ARCH_TLBBATCH_MAX_MMS)) { > + for (i =3D nr; i > 0; i--) { > + if (batch->mms[i - 1] =3D=3D mm) > + goto out; > + } > + if (likely(nr < ARCH_TLBBATCH_MAX_MMS)) > + batch->mms[nr] =3D mm; > + batch->nr_mms =3D nr + 1; > + } This block could use a one-line comment explaining why it's there. > + } > +out: > =C2=A0 batch->unmapped_pages =3D true; > =C2=A0 mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL); > =C2=A0} > 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) > =C2=A0} > =C2=A0EXPORT_SYMBOL_FOR_KVM(__flush_tlb_all); >=20 > +static bool should_flush_tlb_batch(int cpu, > + =C2=A0=C2=A0=C2=A0 const struct > arch_tlbflush_unmap_batch *batch) > +{ > + struct mm_struct *loaded_mm =3D > READ_ONCE(per_cpu(cpu_tlbstate.loaded_mm, cpu)); > + unsigned short i; > + > + if (loaded_mm !=3D LOADED_MM_SWITCHING) { > + for (i =3D 0; i < batch->nr_mms; i++) { > + if (loaded_mm =3D=3D 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)); > +} > + > =C2=A0void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) > =C2=A0{ > =C2=A0 struct flush_tlb_info info; > =C2=A0 bool remote_flush =3D false; > =C2=A0 int cpu =3D get_cpu(); >=20 > + if (likely(!cpu_feature_enabled(X86_FEATURE_INVLPGB) && > + =C2=A0=C2=A0 batch->nr_mms && batch->nr_mms <=3D > 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 =3D 0; > + > =C2=A0 init_flush_tlb_info(&info, NULL, 0, TLB_FLUSH_ALL, 0, false, > =C2=A0 =C2=A0=C2=A0=C2=A0 TLB_GENERATION_INVALID); > =C2=A0 /* > -- > 2.56.0.385.gac671c819a-goog >=20 >=20 --=20 All Rights Reversed.