[PATCH 1/9] x86/mm/pat: flush the TLB when only an alias mapping changed
From: Mike Rapoport (Microsoft)
Date: Thu Nov 12 2026 - 11:41:42 EST
CPA flushes the TLB only when CPA_FLUSHTLB is set. cpa_process_alias()
sets the flag only in alias_cpa and never in the original request.
When the requested range already has the new protection and only its
alias changes, the alias keeps stale TLB entries. For example:
/* the vmalloc mapping is created read-only */
p = vmap(pages, nr, VM_MAP, PAGE_KERNEL_RO);
/* only the direct map changes and nothing is flushed */
set_memory_ro((unsigned long)p, nr);
Setting ->force_flush_all in cpa_process_alias() does not help: it only
chooses how the TLB is flushed, and cpa_flush() is skipped entirely
without CPA_FLUSHTLB.
Worse, when the alias wasn't changed at all, for example when the primary
mapping changes from ROX to RO or vice versa, ->force_flush_all in
cpa_process_alias() causes an unnecessary flush of all TLBs.
Propagate CPA_FLUSHTLB from the alias update to the original request, and
flush the entire TLB only when an alias actually changed.
Fixes: ab5130186d74 ("x86/mm/cpa: Flush direct map alias during cpa")
Signed-off-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>
---
arch/x86/mm/pat/set_memory.c | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)
diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index b1d5c5a0ffb41..fb2413cb8d314 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -1975,7 +1975,8 @@ static int cpa_process_alias(struct cpa_data *cpa)
alias_cpa = *cpa;
alias_cpa.vaddr = &laddr;
- alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
+ alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY |
+ CPA_FLUSHTLB);
alias_cpa.curpage = 0;
/* Directmap always has NX set, do not modify. */
@@ -1984,9 +1985,11 @@ static int cpa_process_alias(struct cpa_data *cpa)
alias_cpa.mask_set.pgprot &= ~_PAGE_NX;
}
- cpa->force_flush_all = 1;
-
ret = __change_page_attr_set_clr(&alias_cpa, 0);
+ if (alias_cpa.flags & CPA_FLUSHTLB) {
+ cpa->flags |= CPA_FLUSHTLB;
+ cpa->force_flush_all = 1;
+ }
if (ret)
return ret;
}
@@ -2003,7 +2006,8 @@ static int cpa_process_alias(struct cpa_data *cpa)
__START_KERNEL_map - phys_base;
alias_cpa = *cpa;
alias_cpa.vaddr = &temp_cpa_vaddr;
- alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
+ alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY |
+ CPA_FLUSHTLB);
alias_cpa.curpage = 0;
/*
@@ -2015,12 +2019,15 @@ static int cpa_process_alias(struct cpa_data *cpa)
alias_cpa.mask_set.pgprot &= ~_PAGE_NX;
}
- cpa->force_flush_all = 1;
/*
* The high mapping range is imprecise, so ignore the
* return value.
*/
__change_page_attr_set_clr(&alias_cpa, 0);
+ if (alias_cpa.flags & CPA_FLUSHTLB) {
+ cpa->flags |= CPA_FLUSHTLB;
+ cpa->force_flush_all = 1;
+ }
}
#endif
--
2.53.0