[PATCH 12/22] x86/mm: use mm_is_kernel() for kernel mm checks
From: Kevin Brodsky
Date: Tue Jul 14 2026 - 10:23:57 EST
The new MMF_KERNEL flag identifies kernel-owned mm's. Checking the
flag with mm_is_kernel() is preferred over comparing directly
against &init_mm.
No functional change, as only init_mm has MMF_KERNEL set for now.
Assisted-by: Codex:GPT-5.5
Signed-off-by: Kevin Brodsky <kevin.brodsky@xxxxxxx>
---
arch/x86/include/asm/tlbflush.h | 8 ++++----
arch/x86/mm/pgtable.c | 2 +-
arch/x86/mm/tlb.c | 9 +++++----
3 files changed, 10 insertions(+), 9 deletions(-)
diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h
index 0545fe75c3fa..4af1cf711a60 100644
--- a/arch/x86/include/asm/tlbflush.h
+++ b/arch/x86/include/asm/tlbflush.h
@@ -159,10 +159,10 @@ struct tlb_state_shared {
* - Actively using an mm. Our CPU's bit will be set in
* mm_cpumask(loaded_mm) and is_lazy == false;
*
- * - Not using a real mm. loaded_mm == &init_mm. Our CPU's bit
- * will not be set in mm_cpumask(&init_mm) and is_lazy == false.
+ * - Not using a real mm. loaded_mm is a kernel mm. Our CPU's bit
+ * will not be set in mm_cpumask(loaded_mm) and is_lazy == false.
*
- * - Lazily using a real mm. loaded_mm != &init_mm, our bit
+ * - Lazily using a real mm. loaded_mm is not a kernel mm, our bit
* is set in mm_cpumask(loaded_mm), but is_lazy == true.
* We're heuristically guessing that the CR3 load we
* skipped more than makes up for the overhead added by
@@ -188,7 +188,7 @@ DECLARE_PER_CPU_SHARED_ALIGNED(struct tlb_state_shared, cpu_tlbstate_shared);
#define enter_lazy_tlb enter_lazy_tlb
static __always_inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
{
- if (this_cpu_read(cpu_tlbstate.loaded_mm) == &init_mm)
+ if (mm_is_kernel(this_cpu_read(cpu_tlbstate.loaded_mm)))
return;
this_cpu_write(cpu_tlbstate_shared.is_lazy, true);
diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c
index f32facdb3035..91d2a6f2ac5a 100644
--- a/arch/x86/mm/pgtable.c
+++ b/arch/x86/mm/pgtable.c
@@ -168,7 +168,7 @@ static int preallocate_pmds(struct mm_struct *mm, pmd_t *pmds[], int count)
bool failed = false;
gfp_t gfp = GFP_PGTABLE_USER;
- if (mm == &init_mm)
+ if (mm_is_kernel(mm))
gfp &= ~__GFP_ACCOUNT;
gfp &= ~__GFP_HIGHMEM;
diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c
index 1023acadd8f8..5517cab8ad00 100644
--- a/arch/x86/mm/tlb.c
+++ b/arch/x86/mm/tlb.c
@@ -594,7 +594,7 @@ void leave_mm(void)
* This needs to happen before any other sanity checks due to
* intel_idle's shenanigans.
*/
- if (loaded_mm == &init_mm)
+ if (mm_is_kernel(loaded_mm))
return;
/* Warn if we're not lazy. */
@@ -855,7 +855,7 @@ void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next,
* cpu_tlbstate_shared.is_lazy whether or not to send an IPI.
*/
if (IS_ENABLED(CONFIG_DEBUG_VM) &&
- WARN_ON_ONCE(prev != &init_mm && !is_notrack_mm(prev) &&
+ WARN_ON_ONCE(!mm_is_kernel(prev) && !is_notrack_mm(prev) &&
!cpumask_test_cpu(cpu, mm_cpumask(next))))
cpumask_set_cpu(cpu, mm_cpumask(next));
@@ -932,7 +932,8 @@ void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next,
* This way switch_mm() must see the new tlb_gen or
* flush_tlb_mm_range() must see the new loaded_mm, or both.
*/
- if (next != &init_mm && !cpumask_test_cpu(cpu, mm_cpumask(next)))
+ if (!mm_is_kernel(next) &&
+ !cpumask_test_cpu(cpu, mm_cpumask(next)))
cpumask_set_cpu(cpu, mm_cpumask(next));
else
smp_mb();
@@ -1134,7 +1135,7 @@ static void flush_tlb_func(void *info)
return;
}
- if (unlikely(loaded_mm == &init_mm))
+ if (unlikely(mm_is_kernel(loaded_mm)))
return;
/* Reload the ASID if transitioning into or out of a global ASID */
--
2.51.2