Skip to contentSkip to conversation

arm64: tlbflush: Reset active_cpu on ASID rollover

PATCH In review [email protected] 5 participants 11 messages Jun 9 – Jun 25

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi all,

This series is based on arm64: tlbflush: Don't broadcast if mm was only active on local cpu, specifically
on commit(s) starting from https://lore.kernel.org/linux-arm-kernel/[email protected]/.

Changes since the previous posting:
* Reset active_cpu to ACTIVE_CPU_NONE when a new ASID is assigned after rollover, so we don’t remain stuck in ACTIVE_CPU_MULTIPLE when the workload later settles back to one CPU.
* Rely on the fact that flush_context() already issues a global TLB flush at ASID assignment after rollover, ensuring there are no stale TLB entries on any CPU.
* This restores a fresh chance for processes to take the local-only flush fast path after each ASID generation rollover.

Series overview:
* Patch 1/2: arm64: tlbflush: Reset active_cpu on ASID rollover
* Patch 2/2: arm64: tlbflush: Don't broadcast if mm was only active on local cpu

Thanks,
Sayali Kulkarni
[email protected] (Ampere)

#1 lore raw

PATCH 1/2 arm64: tlbflush: Don't broadcast if mm was only active on local cpu

From: Ryan Roberts <[email protected]>

There are 3 variants of tlb flush that invalidate user mappings:
flush_tlb_mm(), flush_tlb_page() and __flush_tlb_range(). All of these would previously unconditionally broadcast their tlbis to all cpus in the inner shareable domain.

But this is a waste of effort if we can prove that the mm for which we are flushing the mappings has only ever been active on the local cpu. In that case, it is safe to avoid the broadcast and simply invalidate the current cpu.

So let's track in mm_context_t::active_cpu either the mm has never been active on any cpu, has been active on more than 1 cpu, or has been active on precisely 1 cpu - and in that case, which one. We update this when switching context, being careful to ensure that it gets updated *before* installing the mm's pgtables. On the reader side, we ensure we read *after* the previous write(s) to the pgtable(s) that necessitated the tlb flush have completed. This guarrantees that if a cpu that is doing a tlb flush sees it's own id in active_cpu, then the old pgtable entry cannot have been seen by any other cpu and we can flush only the local cpu.

---
 arch/arm64/include/asm/mmu.h         |  12 +++
 arch/arm64/include/asm/mmu_context.h |   2 +
 arch/arm64/include/asm/tlbflush.h    | 127 +++++++++++++++++++++------
 arch/arm64/mm/context.c              |  30 ++++++-
 4 files changed, 141 insertions(+), 30 deletions(-)
arch/arm64/include/asm/mmu.h +12 -0
index 5e1211c540ab..0002101c1f21 100644
@@ -16,6 +16,17 @@
 #include <linux/refcount.h>
 #include <asm/cpufeature.h>
 
+/*
+ * Sentinal values for mm_context_t::active_cpu. ACTIVE_CPU_NONE indicates the
+ * mm has never been active on any CPU. ACTIVE_CPU_MULTIPLE indicates the mm
+ * has been active on multiple CPUs. Any other value is the ID of the single
+ * CPU that the mm has been active on.
+ */
+enum active_cpu {
+	ACTIVE_CPU_NONE = UINT_MAX,
+	ACTIVE_CPU_MULTIPLE = UINT_MAX - 1,
+};
+
 typedef struct {
 	atomic64_t	id;
 #ifdef CONFIG_COMPAT
@@ -25,6 +36,7 @@ typedef struct {
 	void		*vdso;
 	unsigned long	flags;
 	u8		pkey_allocation_map;
+	unsigned int	active_cpu;
 } mm_context_t;
 
 /*
arch/arm64/include/asm/mmu_context.h +2 -0
index 803b68758152..101cae0c7262 100644
@@ -172,6 +172,8 @@ init_new_context(struct task_struct *tsk, struct mm_struct *mm)
 	/* pkey 0 is the default, so always reserve it. */
 	mm->context.pkey_allocation_map = BIT(0);
 
+	WRITE_ONCE(mm->context.active_cpu, ACTIVE_CPU_NONE);
+
 	return 0;
 }
 
arch/arm64/include/asm/tlbflush.h +100 -27

183 lines not on this page · raw message

arch/arm64/mm/context.c +27 -3
index 0f4a28b87469..f34ed78393e0 100644
@@ -214,9 +214,10 @@ static u64 new_context(struct mm_struct *mm)
 
 void check_and_switch_context(struct mm_struct *mm)
 {
-	unsigned long flags;
-	unsigned int cpu;
+	unsigned int cpu = smp_processor_id();
 	u64 asid, old_active_asid;
+	unsigned int active;
+	unsigned long flags;
 
 	if (system_supports_cnp())
 		cpu_set_reserved_ttbr0();
@@ -251,7 +252,6 @@ void check_and_switch_context(struct mm_struct *mm)
 		atomic64_set(&mm->context.id, asid);
 	}
 
-	cpu = smp_processor_id();
 	if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending))
 		local_flush_tlb_all();
 
@@ -262,6 +262,30 @@ void check_and_switch_context(struct mm_struct *mm)
 
 	arm64_apply_bp_hardening();
 
+	/*
+	 * Update mm->context.active_cpu in such a manner that we avoid cmpxchg
+	 * and dsb unless we definitely need it. If initially ACTIVE_CPU_NONE
+	 * then we are the first cpu to run so set it to our id. If initially
+	 * any id other than ours, we are the second cpu to run so set it to
+	 * ACTIVE_CPU_MULTIPLE. If we update the value then we must issue
+	 * dsb(ishst) to ensure stores to mm->context.active_cpu are ordered
+	 * against the TTBR0 write in cpu_switch_mm()/uaccess_enable(); the
+	 * store must be visible to another cpu before this cpu could have
+	 * populated any TLB entries based on the pgtables that will be
+	 * installed.
+	 */
+	active = READ_ONCE(mm->context.active_cpu);
+	if (active != cpu && active != ACTIVE_CPU_MULTIPLE) {
+		if (active == ACTIVE_CPU_NONE)
+			active = cmpxchg_relaxed(&mm->context.active_cpu,
+						 ACTIVE_CPU_NONE, cpu);
+
+		if (active != ACTIVE_CPU_NONE)
+			WRITE_ONCE(mm->context.active_cpu, ACTIVE_CPU_MULTIPLE);
+
+		dsb(ishst);
+	}
+
 	/*
 	 * Defer TTBR0_EL1 setting for user threads to uaccess_enable() when
 	 * emulating PAN.
Signature · 1 line
2.47.3
maintainer
#2 lore raw

Re: [PATCH 1/2] arm64: tlbflush: Don't broadcast if mm was only active on local cpu

Quoted 23 lines · On Tue, Jun 09, 2026 at 02:34:32PM -0700, [email protected] wrote:

From: Ryan Roberts <[email protected]>

There are 3 variants of tlb flush that invalidate user mappings:
flush_tlb_mm(), flush_tlb_page() and __flush_tlb_range(). All of these would previously unconditionally broadcast their tlbis to all cpus in the inner shareable domain.

But this is a waste of effort if we can prove that the mm for which we are flushing the mappings has only ever been active on the local cpu. In that case, it is safe to avoid the broadcast and simply invalidate the current cpu.

So let's track in mm_context_t::active_cpu either the mm has never been active on any cpu, has been active on more than 1 cpu, or has been active on precisely 1 cpu - and in that case, which one. We update this when switching context, being careful to ensure that it gets updated *before* installing the mm's pgtables. On the reader side, we ensure we read *after* the previous write(s) to the pgtable(s) that necessitated the tlb flush have completed. This guarrantees that if a cpu that is doing a tlb flush sees it's own id in active_cpu, then the old pgtable entry cannot have been seen by any other cpu and we can flush only the local cpu.

Quoted 5 lines

Nit: if you repost someone's patch, please add your signed-off-by.

Signature · 1 line
Catalin
maintainer
#3 lore raw

Re: [PATCH 1/2] arm64: tlbflush: Don't broadcast if mm was only active on local cpu

Quoted 24 lines · Catalin Marinas
Quoted 23 lines · On Tue, Jun 09, 2026 at 02:34:32PM -0700, [email protected] wrote:

From: Ryan Roberts <[email protected]>

There are 3 variants of tlb flush that invalidate user mappings:
flush_tlb_mm(), flush_tlb_page() and __flush_tlb_range(). All of these would previously unconditionally broadcast their tlbis to all cpus in the inner shareable domain.

But this is a waste of effort if we can prove that the mm for which we are flushing the mappings has only ever been active on the local cpu. In that case, it is safe to avoid the broadcast and simply invalidate the current cpu.

So let's track in mm_context_t::active_cpu either the mm has never been active on any cpu, has been active on more than 1 cpu, or has been active on precisely 1 cpu - and in that case, which one. We update this when switching context, being careful to ensure that it gets updated *before* installing the mm's pgtables. On the reader side, we ensure we read *after* the previous write(s) to the pgtable(s) that necessitated the tlb flush have completed. This guarrantees that if a cpu that is doing a tlb flush sees it's own id in active_cpu, then the old pgtable entry cannot have been seen by any other cpu and we can flush only the local cpu.

Quoted 7 lines
Quoted 5 lines

Nit: if you repost someone's patch, please add your signed-off-by.

I have a feeling this patch is horribly broken, so I'll reply on the original.

Will

#4 lore raw

PATCH 2/2 arm64: tlbflush: Reset active_cpu on ASID rollover

In reply to [email protected]

From: Sayali Kulkarni <[email protected]>

Once active_cpu flips to ACTIVE_CPU_MULTIPLE, it never resets, even if the process settles back to one CPU. Reset it to ACTIVE_CPU_NONE when a new ASID is assigned after rollover, since flush_context() already issued a global TLB flush at that point meaning no stale TLB entries exist on any CPU.

This gives processes a fresh chance at the local-only flush fast path after each ASID generation rollover.

---
 arch/arm64/mm/context.c | 2 ++
 1 file changed, 2 insertions(+)
arch/arm64/mm/context.c +2 -0
index f34ed78393e0..0c92cc8fb4cd 100644
@@ -250,6 +250,7 @@ void check_and_switch_context(struct mm_struct *mm)
 	if (!asid_gen_match(asid)) {
 		asid = new_context(mm);
 		atomic64_set(&mm->context.id, asid);
+		WRITE_ONCE(mm->context.active_cpu, ACTIVE_CPU_NONE);
 	}
 
 	if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending))
@@ -321,6 +322,7 @@ unsigned long arm64_mm_context_get(struct mm_struct *mm)
 		 */
 		asid = new_context(mm);
 		atomic64_set(&mm->context.id, asid);
+		WRITE_ONCE(mm->context.active_cpu, ACTIVE_CPU_NONE);
 	}
 
 	nr_pinned_asids++;
Signature · 1 line
2.47.3
#5 lore raw
kernel test robot noticed the following build warnings:

Hi Ryan,

kernel test robot noticed the following build warnings:

[auto build test WARNING on arm64/for-next/core]
[also build test WARNING on kvmarm/next soc/for-next linus/master v7.1-rc7 next-20260609] [cannot apply to arm/for-next arm/fixes]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/sk-gentwo-org/arm64-tlbflush-Reset-active_cpu-on-ASID-rollover/20260610-063444
base:   https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-next/core
patch link:    https://lore.kernel.org/r/20260609213615.2788698-3-sk%40gentwo.org
patch subject: [PATCH 2/2] arm64: tlbflush: Reset active_cpu on ASID rollover
config: arm64-randconfig-r132-20260610 (https://download.01.org/0day-ci/archive/20260611/[email protected]/config)
compiler: aarch64-linux-gcc (GCC) 12.5.0
sparse: v0.6.5-rc1
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260611/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)
arch/arm64/mm/context.c: note: in included file (through arch/arm64/include/asm/atomic.h, include/linux/atomic.h, include/asm-generic/bitops/atomic.h, ...):

>> arch/arm64/include/asm/cmpxchg.h:169:1: sparse: sparse: cast truncates bits from constant value (ffffffff becomes ff)
>> arch/arm64/include/asm/cmpxchg.h:169:1: sparse: sparse: cast truncates bits from constant value (ffffffff becomes ffff)

vim +169 arch/arm64/include/asm/cmpxchg.h

10b663aef1c2479 Catalin Marinas 2012-03-05 168

305d454aaa292be Will Deacon     2015-10-08 @169  __CMPXCHG_GEN()
305d454aaa292be Will Deacon     2015-10-08  170  __CMPXCHG_GEN(_acq)
305d454aaa292be Will Deacon     2015-10-08  171  __CMPXCHG_GEN(_rel)
305d454aaa292be Will Deacon     2015-10-08  172  __CMPXCHG_GEN(_mb)

10b663aef1c2479 Catalin Marinas 2012-03-05 173

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

maintainer
#6 lore raw
In reply to [email protected]
Quoted 10 lines · On Tue, Jun 09, 2026 at 02:34:33PM -0700, [email protected] wrote:

From: Sayali Kulkarni <[email protected]>

Once active_cpu flips to ACTIVE_CPU_MULTIPLE, it never resets, even if the process settles back to one CPU. Reset it to ACTIVE_CPU_NONE when a new ASID is assigned after rollover, since flush_context() already issued a global TLB flush at that point meaning no stale TLB entries exist on any CPU.

This gives processes a fresh chance at the local-only flush fast path after each ASID generation rollover.

flush_context() does not invalidate any TLBs, just marks the in a bitmap which CPUs need to flush, locally, on the next context switch.

Check Sashiko's comments, it has some good points (you can ignore the comments on the first patch as we no longer rely on DVM for SVA.

https://sashiko.dev/#/patchset/[email protected]

Quoted 10 lines
---
 arch/arm64/mm/context.c | 2 ++
 1 file changed, 2 insertions(+)
arch/arm64/mm/context.c +0 -0
index f34ed78393e0..0c92cc8fb4cd 100644

arch/arm64/mm/context.c void check_and_switch_context(struct mm_struct *mm) · line 254

Quoted 5 lines
arch/arm64/mm/context.c +1 -0
@@ -250,6 +250,7 @@ void check_and_switch_context(struct mm_struct *mm)
 	if (!asid_gen_match(asid)) {
 		asid = new_context(mm);
 		atomic64_set(&mm->context.id, asid);
+		WRITE_ONCE(mm->context.active_cpu, ACTIVE_CPU_NONE);
 	}

This breaks the case where you have another thread of the current process running on a different CPU. new_context(mm) will reuse the current ASID, just bump the generation, so setting active_cpu to ACTIVE_CPU_NONE is incorrect.

A better place for this would be in new_context() after the "set_asid" label.

arch/arm64/mm/context.c unsigned long arm64_mm_context_get(struct mm_struct *mm) · line 326

Quoted 7 lines
arch/arm64/mm/context.c +1 -0
 	if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending))
@@ -321,6 +322,7 @@ unsigned long arm64_mm_context_get(struct mm_struct *mm)
 		 */
 		asid = new_context(mm);
 		atomic64_set(&mm->context.id, asid);
+		WRITE_ONCE(mm->context.active_cpu, ACTIVE_CPU_NONE);
 	}

And it would cover this path as well (not that this function is used currently).

Signature · 1 line
Catalin
#7 lore raw

PATCH v2 1/1 arm64: tlbflush: Reset active_cpu on ASID rollover

From: Sayali Kulkarni <[email protected]>

Hi Catalin,

Thank you for the review. I’ve addressed your feedback in v2:

- Moved WRITE_ONCE(mm->context.active_cpu, ACTIVE_CPU_NONE) from check_and_switch_context() to new_context() after the set_asid label. At this point, a brand new ASID has been allocated that no CPU has ever used, so the reset is safe even for multi-threaded processes where other CPUs may still be running with the old ASID via reserved_asids.
- Updated the commit message to correct the safety reasoning: flush_context() only sets tlb_flush_pending; it does not issue a global TLB flush.

Thanks,
Sayali

Once active_cpu flips to ACTIVE_CPU_MULTIPLE it never resets, even if the process settles back to one CPU. Reset it to ACTIVE_CPU_NONE in new_context() after a new ASID is allocated at the set_asid label.

At this point a brand new ASID has been assigned that no CPU has ever used, so ACTIVE_CPU_NONE accurately reflects reality. Any other threads of the same process continue running with the old ASID via reserved_asids and are unaffected.

This gives processes a fresh chance at the local-only flush fast path after each ASID generation rollover.

---
 arch/arm64/mm/context.c | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/mm/context.c +1 -0
index f34ed78393e0..46c7fd07b9bf 100644
@@ -209,6 +209,7 @@ static u64 new_context(struct mm_struct *mm)
 set_asid:
 	__set_bit(asid, asid_map);
 	cur_idx = asid;
+	WRITE_ONCE(mm->context.active_cpu, ACTIVE_CPU_NONE);
 	return asid2ctxid(asid, generation);
 }
 
Signature · 1 line
2.47.3
#8 lore raw

Hi,

Quoted 35 lines · Sayali Kulkarni

From: Sayali Kulkarni <[email protected]>

Hi Catalin,

Thank you for the review. I’ve addressed your feedback in v2:

- Moved WRITE_ONCE(mm->context.active_cpu, ACTIVE_CPU_NONE) from check_and_switch_context() to new_context() after the set_asid label. At this point, a brand new ASID has been allocated that no CPU has ever used, so the reset is safe even for multi-threaded processes where other CPUs may still be running with the old ASID via reserved_asids.
- Updated the commit message to correct the safety reasoning: flush_context() only sets tlb_flush_pending; it does not issue a global TLB flush.

Thanks,
Sayali

Once active_cpu flips to ACTIVE_CPU_MULTIPLE it never resets, even if the process settles back to one CPU. Reset it to ACTIVE_CPU_NONE in new_context() after a new ASID is allocated at the set_asid label.

At this point a brand new ASID has been assigned that no CPU has ever used, so ACTIVE_CPU_NONE accurately reflects reality. Any other threads of the same process continue running with the old ASID via reserved_asids and are unaffected.

This gives processes a fresh chance at the local-only flush fast path after each ASID generation rollover.

---
 arch/arm64/mm/context.c | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/mm/context.c +0 -0
index f34ed78393e0..46c7fd07b9bf 100644

arch/arm64/mm/context.c static u64 new_context(struct mm_struct *mm) · line 212

Quoted 4 lines
arch/arm64/mm/context.c +1 -0
@@ -209,6 +209,7 @@ static u64 new_context(struct mm_struct *mm)
 set_asid:
 	__set_bit(asid, asid_map);
 	cur_idx = asid;
+	WRITE_ONCE(mm->context.active_cpu, ACTIVE_CPU_NONE);

Can the above store race with the store to active_cpu in another thread, that updates it to ACTIVE_CPU_MULTIPLE ?

Lets say we have two threads both initially running in CPU 0,

Thread 1: Runs in CPU 0

Encounters a rollover, updates mm->context.active_cpu to ACTIVE_CPU_NONE and updates mm->context.id to new asid.

Thread 2: Scheduled to run on CPU 1 for the first time

Observes the updated mm->context.id that belongs to the current generation(after the rollover) and hence proceeds to switch_mm_fastpath and ends up updating the active_cpu to ACTIVE_CPU_MULTIPLE.

If Thread 1 and Thread 2 races, then active_cpu can get corrupted ?

The reason this could be possible is that, write to active_cpu and mm->context.id can get reordered and we need to enforce ordering for correctness ?

Do you see this as a valid scenario ?

--
Thanks,
Linu Cherian.

#9 lore raw

Thank you for catching this. After going through the patch again, I realize that the race is real. Resetting active_cpu to ACTIVE_CPU_NONE on rollover can leave it in an inconsistent state when stale entries still live. Note that the rollover state entries themselves aren’t the issue. Those are handled by the existing reserved_asids/tlb_flush_pending mechanism. It’s specifically active_cpu’s own record going inconsistent.

For v3, I am exploring handling active_cpu together with the generation so that the reset can’t open a NONE window. Doing it in the slow path after local flush and updating to the current CPU rather than NONE. Does that direction seem reasonable or is there a cleaner way to avoid the under-claim window? Will follow up with a patch once it’s figured out.

Thanks,
Sayali

Quoted 80 lines · Linu Cherian

Hi,

Quoted 35 lines · Sayali Kulkarni

From: Sayali Kulkarni <[email protected]>

Hi Catalin,

Thank you for the review. I’ve addressed your feedback in v2:

- Moved WRITE_ONCE(mm->context.active_cpu, ACTIVE_CPU_NONE) from check_and_switch_context() to new_context() after the set_asid label. At this point, a brand new ASID has been allocated that no CPU has ever used, so the reset is safe even for multi-threaded processes where other CPUs may still be running with the old ASID via reserved_asids.
- Updated the commit message to correct the safety reasoning: flush_context() only sets tlb_flush_pending; it does not issue a global TLB flush.

Thanks,
Sayali

Once active_cpu flips to ACTIVE_CPU_MULTIPLE it never resets, even if the process settles back to one CPU. Reset it to ACTIVE_CPU_NONE in new_context() after a new ASID is allocated at the set_asid label.

At this point a brand new ASID has been assigned that no CPU has ever used, so ACTIVE_CPU_NONE accurately reflects reality. Any other threads of the same process continue running with the old ASID via reserved_asids and are unaffected.

This gives processes a fresh chance at the local-only flush fast path after each ASID generation rollover.

---
 arch/arm64/mm/context.c | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/mm/context.c +0 -0
index f34ed78393e0..46c7fd07b9bf 100644

arch/arm64/mm/context.c static u64 new_context(struct mm_struct *mm) · line 212

Quoted 4 lines
arch/arm64/mm/context.c +1 -0
@@ -209,6 +209,7 @@ static u64 new_context(struct mm_struct *mm)
 set_asid:
 	__set_bit(asid, asid_map);
 	cur_idx = asid;
+	WRITE_ONCE(mm->context.active_cpu, ACTIVE_CPU_NONE);

Can the above store race with the store to active_cpu in another thread, that updates it to ACTIVE_CPU_MULTIPLE ?

Lets say we have two threads both initially running in CPU 0,

Thread 1: Runs in CPU 0

Encounters a rollover, updates mm->context.active_cpu to ACTIVE_CPU_NONE and updates mm->context.id to new asid.

Thread 2: Scheduled to run on CPU 1 for the first time

Observes the updated mm->context.id that belongs to the current generation(after the rollover) and hence proceeds to switch_mm_fastpath and ends up updating the active_cpu to ACTIVE_CPU_MULTIPLE.

If Thread 1 and Thread 2 races, then active_cpu can get corrupted ?

The reason this could be possible is that, write to active_cpu and mm->context.id can get reordered and we need to enforce ordering for correctness ?

Do you see this as a valid scenario ?

--
Thanks,
Linu Cherian.

maintainer
#10 lore raw
Quoted 5 lines · Sayali Kulkarni

For v3, I am exploring handling active_cpu together with the generation so that the reset can’t open a NONE window. Doing it in the slow path after local flush and updating to the current CPU rather than NONE. Does that direction seem reasonable or is there a cleaner way to avoid the under-claim window? Will follow up with a patch once it’s figured out.

Please don't.

I already reviewed Ryan's v2 and there was some good discussion over there. Having you send a random other version of it isn't helpful in the slightest and I won't be applying it.

Will