mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
To: linux-mm@kvack.org
Cc: Albert Ou <aou@eecs.berkeley.edu>,
	 Andrew Morton <akpm@linux-foundation.org>,
	 Andy Lutomirski <luto@kernel.org>,
	Borislav Petkov <bp@alien8.de>,
	 Brendan Jackman <jackmanb@google.com>,
	 Catalin Marinas <catalin.marinas@arm.com>,
	 Dave Hansen <dave.hansen@linux.intel.com>,
	 David Hildenbrand <david@kernel.org>,
	Huacai Chen <chenhuacai@kernel.org>,
	 Ingo Molnar <mingo@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	 Kevin Brodsky <kevin.brodsky@arm.com>,
	 "Liam R. Howlett" <liam@infradead.org>,
	Lorenzo Stoakes <ljs@kernel.org>,  Michal Hocko <mhocko@suse.com>,
	Mike Rapoport <rppt@kernel.org>,
	 Palmer Dabbelt <palmer@dabbelt.com>,
	Paul Walmsley <pjw@kernel.org>,
	 Peter Zijlstra <peterz@infradead.org>,
	"H. Peter Anvin" <hpa@zytor.com>,
	 Suren Baghdasaryan <surenb@google.com>,
	Thomas Gleixner <tglx@kernel.org>,
	 Vlastimil Babka <vbabka@kernel.org>,
	Will Deacon <will@kernel.org>,
	 linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org,  linux-riscv@lists.infradead.org,
	loongarch@lists.linux.dev, x86@kernel.org
Subject: [PATCH RFC 06/18] x86/mm/pat: introduce an inline helper to check if alias needs update
Date: Tue, 21 Jul 2026 19:23:29 +0300	[thread overview]
Message-ID: <20260721-generic-set-memory-v0-1-v1-6-2c1fc62306b3@kernel.org> (raw)
In-Reply-To: <20260721-generic-set-memory-v0-1-v1-0-2c1fc62306b3@kernel.org>

Once core parts of change_page_attr() would be moved to common code,
the processing of alias mappings would be out of line and would require
a function call for each modified PTE even if the primary target of
change_page_attr() does not have alias mappings.

Calling a function per PTE just to find out it has nothing to do is
expensive and resulted in ~30% regression in instrumented cpa-test.

Although cpa-test is a microbenchmark, ~30% regression is still a lot
and can be easily avoided by inlining the checks that gates processing
of the alias mapping.

Introduce cpa_should_update_alias() inline helper that checks if the
alias update is required.

Since on 64-bit an alias could be in the high kernel mapping, pull
__cpa_pfn_in_highmap() along and rename it to pfn_is_kernel().

No functional change intended.

Assisted-by: Copilot:claude-opus-4.8
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
 arch/x86/include/asm/set_memory.h | 50 +++++++++++++++++++++++++++++++
 arch/x86/mm/pat/set_memory.c      | 62 +++------------------------------------
 2 files changed, 54 insertions(+), 58 deletions(-)

diff --git a/arch/x86/include/asm/set_memory.h b/arch/x86/include/asm/set_memory.h
index 4362c26aa992..332ec300c2ed 100644
--- a/arch/x86/include/asm/set_memory.h
+++ b/arch/x86/include/asm/set_memory.h
@@ -3,12 +3,62 @@
 #define _ASM_X86_SET_MEMORY_H
 
 #include <asm/page.h>
+#include <asm/sections.h>
 #include <asm-generic/set_memory.h>
 #include <asm/pgtable.h>
 
 #define set_memory_rox set_memory_rox
 int set_memory_rox(unsigned long addr, int numpages);
 
+#ifdef CONFIG_X86_64
+/*
+ * The kernel image is mapped into two places in the virtual address space
+ * (addresses without KASLR, of course):
+ *
+ * 1. The kernel direct map (0xffff880000000000)
+ * 2. The "high kernel map" (0xffffffff81000000)
+ *
+ * We actually execute out of #2. If we get the address of a kernel symbol, it
+ * points to #2, but almost all physical-to-virtual translations point to #1.
+ *
+ * This is so that we can have both a directmap of all physical memory *and*
+ * take full advantage of the limited (s32) immediate addressing range (2G)
+ * of x86_64.
+ *
+ * See Documentation/arch/x86/x86_64/mm.rst for more detail.
+ */
+static inline bool pfn_is_kernel(unsigned long pfn)
+{
+	unsigned long spfn = __pa_symbol(_text) >> PAGE_SHIFT;
+	/* Do not reference a physical address outside the kernel. */
+	unsigned long epfn = __pa_symbol(roundup(_brk_end, PMD_SIZE) - 1) >> PAGE_SHIFT;
+
+	return pfn >= spfn && pfn <= epfn;
+}
+#else
+static inline bool pfn_is_kernel(unsigned long pfn)
+{
+	/* There is no highmap on 32-bit */
+	return false;
+}
+#endif
+
+static inline bool cpa_should_update_alias(unsigned long vaddr,
+					   unsigned long pfn)
+{
+	/* Primary is not in the direct map, its direct map alias needs update */
+	if (vaddr < PAGE_OFFSET ||
+	    vaddr >= PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))
+		return true;
+
+	/* direct map page that is also part of the kernel highmap */
+	if ((vaddr < (unsigned long)_text || vaddr >= _brk_end) &&
+	    pfn_is_kernel(pfn))
+		return true;
+
+	return false;
+}
+
 /*
  * The set_memory_* API can be used to change various attributes of a virtual
  * address range. The attributes include:
diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index ee40962d4ee9..2a087dcedf84 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -224,61 +224,6 @@ within(unsigned long addr, unsigned long start, unsigned long end)
 	return addr >= start && addr < end;
 }
 
-#ifdef CONFIG_X86_64
-
-static inline int
-within_inclusive(unsigned long addr, unsigned long start, unsigned long end)
-{
-	return addr >= start && addr <= end;
-}
-
-/*
- * The kernel image is mapped into two places in the virtual address space
- * (addresses without KASLR, of course):
- *
- * 1. The kernel direct map (0xffff880000000000)
- * 2. The "high kernel map" (0xffffffff81000000)
- *
- * We actually execute out of #2. If we get the address of a kernel symbol, it
- * points to #2, but almost all physical-to-virtual translations point to #1.
- *
- * This is so that we can have both a directmap of all physical memory *and*
- * take full advantage of the limited (s32) immediate addressing range (2G)
- * of x86_64.
- *
- * See Documentation/arch/x86/x86_64/mm.rst for more detail.
- */
-
-static inline unsigned long highmap_start_pfn(void)
-{
-	return __pa_symbol(_text) >> PAGE_SHIFT;
-}
-
-static inline unsigned long highmap_end_pfn(void)
-{
-	/* Do not reference physical address outside the kernel. */
-	return __pa_symbol(roundup(_brk_end, PMD_SIZE) - 1) >> PAGE_SHIFT;
-}
-
-static bool __cpa_pfn_in_highmap(unsigned long pfn)
-{
-	/*
-	 * Kernel text has an alias mapping at a high address, known
-	 * here as "highmap".
-	 */
-	return within_inclusive(pfn, highmap_start_pfn(), highmap_end_pfn());
-}
-
-#else
-
-static bool __cpa_pfn_in_highmap(unsigned long pfn)
-{
-	/* There is no highmap on 32-bit */
-	return false;
-}
-
-#endif
-
 /*
  * See set_mce_nospec().
  *
@@ -1830,7 +1775,7 @@ static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
 		cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
 		return 0;
 
-	} else if (__cpa_pfn_in_highmap(cpa->pfn)) {
+	} else if (pfn_is_kernel(cpa->pfn)) {
 		/* Faults in the highmap are OK, so do not warn: */
 		return -EFAULT;
 	} else {
@@ -1968,7 +1913,7 @@ static int cpa_process_alias(struct cpa_data *cpa)
 	 * to touch the high mapped kernel as well:
 	 */
 	if (!within(vaddr, (unsigned long)_text, _brk_end) &&
-	    __cpa_pfn_in_highmap(cpa->pfn)) {
+	    pfn_is_kernel(cpa->pfn)) {
 		unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
 					       __START_KERNEL_map - phys_base;
 		alias_cpa = *cpa;
@@ -2026,7 +1971,8 @@ static int __change_page_attr_set_clr(struct cpa_data *cpa, int primary)
 		if (ret)
 			goto out;
 
-		if (primary && !(cpa->flags & CPA_NO_CHECK_ALIAS)) {
+		if (primary && !(cpa->flags & CPA_NO_CHECK_ALIAS) &&
+		    cpa_should_update_alias(__cpa_addr(cpa, cpa->curpage), cpa->pfn)) {
 			ret = cpa_process_alias(cpa);
 			if (ret)
 				goto out;

-- 
2.53.0


  parent reply	other threads:[~2026-07-21 16:24 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-21 16:23 [PATCH RFC 00/18] arch, mm: create a generic set_memory/change_page_attr core Mike Rapoport (Microsoft)
2026-07-21 16:23 ` [PATCH RFC 01/18] x86/mm/pat: Don't gate cpa_lock on debug_pagealloc_enabled() Mike Rapoport (Microsoft)
2026-07-21 17:16   ` Lorenzo Stoakes (ARM)
2026-07-21 18:30     ` Mike Rapoport
2026-07-22  8:55       ` Lorenzo Stoakes (ARM)
2026-07-21 16:23 ` [PATCH RFC 02/18] x86/mm/pat: fix effective RW computation in lookup_address_in_pgd_attr() Mike Rapoport (Microsoft)
2026-07-21 16:23 ` [PATCH RFC 03/18] x86/mm/pat: replace x86-speficic pg_level with generic pgtable_level Mike Rapoport (Microsoft)
2026-07-21 16:23 ` [PATCH RFC 04/18] x86/mm/pat: use page table accessors for effective RW/NX bits Mike Rapoport (Microsoft)
2026-07-21 16:23 ` [PATCH RFC 05/18] x86/mm/pat: use a generic helper for the large page size Mike Rapoport (Microsoft)
2026-07-21 16:23 ` Mike Rapoport (Microsoft) [this message]
2026-07-21 16:23 ` [PATCH RFC 07/18] x86/mm/pat: introduce cpa_handle_large_page() Mike Rapoport (Microsoft)
2026-07-21 16:23 ` [PATCH RFC 08/18] x86/mm/pat: use cpa_split_data structure to pass split parameters Mike Rapoport (Microsoft)
2026-07-21 16:23 ` [PATCH RFC 09/18] x86/mm/pat: use a single locked lookup for PTE verification Mike Rapoport (Microsoft)
2026-07-21 16:23 ` [PATCH RFC 10/18] x86/mm/pat: move the large page size calculation to should_split_large_page Mike Rapoport (Microsoft)
2026-07-21 16:23 ` [PATCH RFC 11/18] x86/mm/pat, mm: set_memory: pull change_page_attr() core to mm/ Mike Rapoport (Microsoft)
2026-07-21 16:23 ` [PATCH RFC 12/18] x86/pat/mm, mm/set_memory: abstract the CPA page table lock Mike Rapoport (Microsoft)
2026-07-21 16:23 ` [PATCH RFC 13/18] mm/set_memory: read page table entries with pXdp_get() Mike Rapoport (Microsoft)
2026-07-21 16:23 ` [PATCH RFC 14/18] x86/mm/pat, mm: move cpa-test to mm Mike Rapoport (Microsoft)
2026-07-21 16:23 ` [PATCH RFC 15/18] riscv: convert set_memory to the generic CPA implementation Mike Rapoport (Microsoft)
2026-07-21 16:23 ` [PATCH RFC 16/18] riscv: make set_direct_map_*_noflush actually noflush Mike Rapoport (Microsoft)
2026-07-21 16:23 ` [PATCH RFC 17/18] loongarch: convert set_memory to the generic CPA implementation Mike Rapoport (Microsoft)
2026-07-21 16:23 ` [PATCH RFC 18/18] loongarch: make set_direct_map_*_noflush actually noflush Mike Rapoport (Microsoft)
2026-07-21 17:19 ` [PATCH RFC 00/18] arch, mm: create a generic set_memory/change_page_attr core Borislav Petkov
2026-07-21 18:28   ` Mike Rapoport
2026-07-21 19:54     ` Borislav Petkov
2026-07-22  6:50       ` Mike Rapoport
2026-07-22 17:03   ` Dave Hansen
2026-07-27 10:22     ` Mike Rapoport
2026-07-22 13:20 ` [syzbot ci] " syzbot ci
2026-07-22 15:17 ` [PATCH RFC 00/18] " Adrian Barnaś
2026-07-27 10:16   ` Mike Rapoport
2026-11-01 22:16 ` patchwork-bot+linux-riscv

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=20260721-generic-set-memory-v0-1-v1-6-2c1fc62306b3@kernel.org \
    --to=rppt@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=bp@alien8.de \
    --cc=catalin.marinas@arm.com \
    --cc=chenhuacai@kernel.org \
    --cc=dave.hansen@linux.intel.com \
    --cc=david@kernel.org \
    --cc=hpa@zytor.com \
    --cc=jackmanb@google.com \
    --cc=kevin.brodsky@arm.com \
    --cc=liam@infradead.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=ljs@kernel.org \
    --cc=loongarch@lists.linux.dev \
    --cc=luto@kernel.org \
    --cc=mhocko@suse.com \
    --cc=mingo@kernel.org \
    --cc=mingo@redhat.com \
    --cc=palmer@dabbelt.com \
    --cc=peterz@infradead.org \
    --cc=pjw@kernel.org \
    --cc=surenb@google.com \
    --cc=tglx@kernel.org \
    --cc=vbabka@kernel.org \
    --cc=will@kernel.org \
    --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®