From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
To: Andy Lutomirski <luto@kernel.org>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
Ingo Molnar <mingo@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
Thomas Gleixner <tglx@kernel.org>,
x86@kernel.org
Cc: Mike Rapoport <rppt@kernel.org>, "H. Peter Anvin" <hpa@zytor.com>,
Rick Edgecombe <rick.p.edgecombe@intel.com>,
Tom Lendacky <thomas.lendacky@amd.com>,
linux-kernel@vger.kernel.org
Subject: [PATCH 1/9] x86/mm/pat: flush the TLB when only an alias mapping changed
Date: Thu, 12 Nov 2026 18:31:06 +0200 [thread overview]
Message-ID: <20261112-fixes-x86-v1-1-791adb8d6670@kernel.org> (raw)
In-Reply-To: <20261112-fixes-x86-v1-0-791adb8d6670@kernel.org>
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@kernel.org>
---
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
next prev parent reply other threads:[~2026-11-12 16:31 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 16:31 [PATCH 0/9] x86/mm: CPA robustness fixes and cleanups Mike Rapoport (Microsoft)
2026-11-12 16:31 ` Mike Rapoport (Microsoft) [this message]
2026-11-12 16:31 ` [PATCH 2/9] x86/mm: don't set kernel_set_to_readonly when write protecting fails Mike Rapoport (Microsoft)
2026-11-12 16:31 ` [PATCH 3/9] x86/mm/pat: don't populate page tables in set_memory_{en,de}crypted() Mike Rapoport (Microsoft)
2026-11-12 16:31 ` [PATCH 4/9] x86/mm/pat: don't BUG() on inconsistent page count Mike Rapoport (Microsoft)
2026-11-12 16:31 ` [PATCH 5/9] x86/mm/pat: don't warn about W^X violations before mark_rodata_ro() Mike Rapoport (Microsoft)
2026-11-12 16:31 ` [PATCH 6/9] x86/mm/pat: remove CPA_ARRAY Mike Rapoport (Microsoft)
2026-11-12 16:31 ` [PATCH 7/9] x86/mm/pat: pass the address to CPA by value Mike Rapoport (Microsoft)
2026-11-12 16:31 ` [PATCH 8/9] x86/mm/pat: move the collapse of large pages out of cpa_flush() Mike Rapoport (Microsoft)
2026-11-12 16:31 ` [PATCH 9/9] x86/mm/pat: use cpa_flush() on CPA errors Mike Rapoport (Microsoft)
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=20261112-fixes-x86-v1-1-791adb8d6670@kernel.org \
--to=rppt@kernel.org \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=rick.p.edgecombe@intel.com \
--cc=tglx@kernel.org \
--cc=thomas.lendacky@amd.com \
--cc=x86@kernel.org \
/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®