[PATCH RFC v2 11/54] x86/mm/pat: drop the init_mm read lock from change_page_attr_set_clr()

From: Mike Rapoport (Microsoft)

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


change_page_attr_set_clr() holds the init_mm read lock so that a collapse
of large pages, which holds the write lock, cannot free a page table under
a CPA lookup.

Since commit 1aac65f3e651 ("x86/mm/pat: Take cpa_lock around large-page
collapse") the collapse holds cpa_lock as well, and with debug_pagealloc no
longer using CPA, cpa_lock is enough to synchronize all CPA operations.

The only thing mmap_read_lock() adds is keeping out a concurrent ptdump.

A ptdump racing with any CPA operation except collapse is safe: only a
collapse frees page tables, and a split replaces a leaf with a complete
page table. The only difference without mmap_read_lock() in CPA is that
ptdump may show some entries before and some after the change.

Collapse, though, must keep the mmap_write_lock() to ensure ptdump and text
poking are not accessing page tables the collapse frees.

Remove mmap_read_lock() and CPA_ST_INIT_MM_LOCKED from CPA operations to
simplify its locking model.

Update the comments of cpa_lock and of the mmap_write_lock() around the
collapse to clearly state what they serialize.

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

diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index c3af82788879..e1abc0d1d143 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -61,9 +61,10 @@ enum cpa_warn {
static const int cpa_warn_level = CPA_PROTECT;

/*
- * Serialize cpa() using cpa_lock so that we don't allow any other cpu, with
- * stale large tlb entries, to change the page attribute in parallel to some
- * other cpu splitting a large page entry along with changing the attribute.
+ * Serialize CPA using cpa_lock so that:
+ * - concurrent requests don't split or change the same large mapping at the
+ * same time
+ * - collapses of large mappings don't free page tables under a CPA lookup
*/
static DEFINE_SPINLOCK(cpa_lock);

@@ -78,7 +79,6 @@ static DEFINE_SPINLOCK(cpa_lock);
#define CPA_ST_FLUSH_ALL 0x02 /* flush the entire TLB */
#define CPA_ST_ALIAS 0x04 /* the request is for an alias */
#define CPA_ST_STATIC_PROT 0x08 /* enforce static protections in a split */
-#define CPA_ST_INIT_MM_LOCKED 0x10 /* init_mm's mmap lock is held */

static inline pgprot_t cachemode2pgprot(enum page_cache_mode pcm)
{
@@ -1238,11 +1238,7 @@ static int split_large_page(struct cpa_data *cpa, pte_t *kpte,
int ret;

spin_unlock(&cpa_lock);
- if (cpa->state & CPA_ST_INIT_MM_LOCKED)
- mmap_read_unlock(&init_mm);
pte = pte_alloc_one_kernel(&init_mm);
- if (cpa->state & CPA_ST_INIT_MM_LOCKED)
- mmap_read_lock(&init_mm);
spin_lock(&cpa_lock);
if (!pte)
return -ENOMEM;
@@ -2110,11 +2106,7 @@ static int change_page_attr_set_clr(unsigned long addr, int numpages,
cpa.flags = in_flag;
cpa.curpage = 0;

- /* Avoid race with concurrent CPA collapse. */
- cpa.state = CPA_ST_INIT_MM_LOCKED;
- scoped_guard(mmap_read_lock, &init_mm)
- ret = __change_page_attr_set_clr(&cpa);
- cpa.state &= ~CPA_ST_INIT_MM_LOCKED;
+ ret = __change_page_attr_set_clr(&cpa);

/*
* Check whether we really changed something:
@@ -2143,10 +2135,10 @@ static int change_page_attr_set_clr(unsigned long addr, int numpages,
goto out;

/*
- * Take the mmap write lock on init_mm to:
- * - Avoid a use-after-free if raced by ptdump (which takes its own
- * write lock on init_mm).
- * - Serialise concurrent CPA walkers.
+ * Take the mmap write lock on init_mm to avoid a use-after-free if
+ * there is a race with
+ * - ptdump (which takes its own write lock on init_mm)
+ * - text poking (which takes the read lock around vmalloc_to_page())
*/
scoped_guard(mmap_write_lock, &init_mm) {
guard(spinlock)(&cpa_lock);

--
2.53.0