[PATCH RFC v2 10/54] x86/mm/pat: stop using CPA for debug_pagealloc

From: Mike Rapoport (Microsoft)

Date: Thu Nov 26 2026 - 06:16:16 EST


With debug_pagealloc, the page allocator unmaps pages from the direct map
when it frees them and maps them back when it allocates them, in whatever
context it runs. It does that with CPA, in a mode of its own.

That mode is the one exception in the locking of CPA: it skips cpa_lock,
and every request checks for it. The exception is easy to get wrong:
commit 5fce67641a3e ("x86/mm/pat: Don't gate cpa_lock on
debug_pagealloc_enabled()") dropped it, and commit 7da514d819a0 ("x86/mm:
Fix and document DEBUG_PAGEALLOC") had to bring it back.

With debug_pagealloc enabled the direct map is mapped with 4K pages, so
mapping or unmapping a page always updates a single existing PTE.

Update the PTEs with apply_to_existing_page_range(), like powerpc and riscv
do, and drop the lockless mode from CPA.

Every request now takes cpa_lock, so cpa_inc_4k_install() does not need
data_race() any more.

Signed-off-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>
---
arch/x86/mm/pat/set_memory.c | 77 +++++++++++++++++++-------------------------
1 file changed, 33 insertions(+), 44 deletions(-)

diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index 87c7c7934b42..c3af82788879 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -72,7 +72,6 @@ static DEFINE_SPINLOCK(cpa_lock);
#define CPA_REQ_COLLAPSE 0x02 /* try to collapse large pages */
#define CPA_REQ_SPLIT 0x04 /* split large pages */
#define CPA_REQ_PAGES_ARRAY 0x08
-#define CPA_REQ_DEBUG_PAGEALLOC 0x10

/* cpa_data::state */
#define CPA_ST_FLUSHTLB 0x01 /* the TLB needs a flush */
@@ -165,7 +164,7 @@ static inline void cpa_inc_2m_checked(void)

static inline void cpa_inc_4k_install(void)
{
- data_race(cpa_4k_install++);
+ cpa_4k_install++;
}

static inline void cpa_inc_lp_sameprot(int level)
@@ -2014,7 +2013,6 @@ static int __change_page_attr_set_clr(struct cpa_data *cpa)
{
unsigned long numpages = cpa->numpages;
unsigned long rempages = numpages;
- bool lock = true;
int ret = 0;

/*
@@ -2024,29 +2022,6 @@ static int __change_page_attr_set_clr(struct cpa_data *cpa)
!(cpa->flags & CPA_REQ_SPLIT))
return ret;

- /*
- * DEBUG_PAGEALLOC is special; it is called from any context the
- * page-allocator is, which violates the normal cpa_lock locking
- * rules.
- *
- * However, since it is part of the page-allocator, things are still
- * properly serialized by the page-allocator locking and the fact that
- * when a page is owned by the page-allocator, it isn't owned by
- * anybody else. That is, you *SHOULD NOT* be calling cpa() on memory
- * that isn't allocated.
- *
- * Additionally, DEBUG_PAGEALLOC ensures (per probe_page_size_mask())
- * that the kernel mapping is 4k pages, therefore there are no large
- * pages to split/collapse.
- *
- * Furthermore, the page-allocator strictly manages pages that
- * *exist*, avoiding pgd_lock.
- *
- * Therefore, it is safe to not take cpa_lock.
- */
- if (debug_pagealloc_enabled() && (cpa->flags & CPA_REQ_DEBUG_PAGEALLOC))
- lock = false;
-
while (rempages) {
/*
* Store the remaining nr of pages for the large page
@@ -2057,12 +2032,8 @@ static int __change_page_attr_set_clr(struct cpa_data *cpa)
if (cpa->flags & CPA_REQ_PAGES_ARRAY)
cpa->numpages = 1;

- if (lock) {
- guard(spinlock)(&cpa_lock);
- ret = __change_page_attr(cpa);
- } else {
+ scoped_guard(spinlock, &cpa_lock)
ret = __change_page_attr(cpa);
- }
if (ret)
goto out;

@@ -2651,14 +2622,14 @@ int set_pages_rw(struct page *page, int numpages)
return set_memory_rw(addr, numpages);
}

-static int __set_pages_p(struct page *page, int numpages, unsigned int cpa_flags)
+static int __set_pages_p(struct page *page, int numpages)
{
struct cpa_data cpa = { .addr = (unsigned long)page_address(page),
.pgd = NULL,
.numpages = numpages,
.mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
.mask_clr = __pgprot(0),
- .flags = CPA_REQ_NO_CHECK_ALIAS | cpa_flags };
+ .flags = CPA_REQ_NO_CHECK_ALIAS };

/*
* No alias checking needed for setting present flag. otherwise,
@@ -2669,14 +2640,14 @@ static int __set_pages_p(struct page *page, int numpages, unsigned int cpa_flags
return __change_page_attr_set_clr(&cpa);
}

-static int __set_pages_np(struct page *page, int numpages, unsigned int cpa_flags)
+static int __set_pages_np(struct page *page, int numpages)
{
struct cpa_data cpa = { .addr = (unsigned long)page_address(page),
.pgd = NULL,
.numpages = numpages,
.mask_set = __pgprot(0),
.mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY),
- .flags = CPA_REQ_NO_CHECK_ALIAS | cpa_flags };
+ .flags = CPA_REQ_NO_CHECK_ALIAS };

/*
* No alias checking needed for setting not present flag. otherwise,
@@ -2689,17 +2660,35 @@ static int __set_pages_np(struct page *page, int numpages, unsigned int cpa_flag

int set_direct_map_invalid_noflush(struct page *page, unsigned int nr)
{
- return __set_pages_np(page, nr, 0);
+ return __set_pages_np(page, nr);
}

int set_direct_map_default_noflush(struct page *page, unsigned int nr)
{
- return __set_pages_p(page, nr, 0);
+ return __set_pages_p(page, nr);
}

#ifdef CONFIG_DEBUG_PAGEALLOC
+static int kernel_map_pte(pte_t *ptep, unsigned long addr, void *data)
+{
+ int *enable = data;
+ pte_t pte = ptep_get(ptep);
+ pgprot_t prot = pte_pgprot(pte);
+
+ if (*enable)
+ pgprot_val(prot) |= _PAGE_PRESENT | _PAGE_RW;
+ else
+ pgprot_val(prot) &= ~(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY);
+ prot = pgprot_clear_protnone_bits(prot);
+ set_pte_atomic(ptep, pfn_pte(pte_pfn(pte), prot));
+
+ return 0;
+}
+
void __kernel_map_pages(struct page *page, int numpages, int enable)
{
+ unsigned long addr = (unsigned long)page_address(page);
+
if (PageHighMem(page))
return;
if (!enable) {
@@ -2708,14 +2697,14 @@ void __kernel_map_pages(struct page *page, int numpages, int enable)
}

/*
- * The return value is ignored as the calls cannot fail.
- * Large pages for identity mappings are not used at boot time
- * and hence no memory allocations during large page split.
+ * This runs in any context the page allocator does, without locks.
+ * The allocator owns these pages, so nothing else changes their PTEs,
+ * and with debug_pagealloc the direct map has only 4K pages (see
+ * probe_page_size_mask()), so their page tables are never split or
+ * collapsed.
*/
- if (enable)
- __set_pages_p(page, numpages, CPA_REQ_DEBUG_PAGEALLOC);
- else
- __set_pages_np(page, numpages, CPA_REQ_DEBUG_PAGEALLOC);
+ apply_to_existing_page_range(&init_mm, addr, numpages * PAGE_SIZE,
+ kernel_map_pte, &enable);

/*
* We should perform an IPI and flush all tlbs, but that can

--
2.53.0