[PATCH RFC v2 06/54] x86/mm/pat: don't count kernel image splits as direct map splits

From: Mike Rapoport (Microsoft)

Date: Thu Nov 26 2026 - 06:15:39 EST


The DirectMap lines of /proc/meminfo count only the direct map. The kernel
image mapping is never added to them.

CPA counts a split if virt_addr_valid() accepts the address and its pfn is
mapped in the direct map. On 64-bit virt_addr_valid() accepts the kernel
image mapping too, and its pfns are in the direct map, so the splits of the
kernel image mapping are counted as well.

Such splits can happen when mark_rodata_ro() updates the protections of the
kernel image.

Each split of the kernel image mapping moves 2M from DirectMap2M to
DirectMap4k, although the direct map does not change.

Count splits and collapses only for addresses in the direct map.

Fixes: 8eb5779f6b9c ("x86, mm: use pfn_range_is_mapped() with CPA")
Signed-off-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>
---
arch/x86/mm/pat/set_memory.c | 15 +++++++--------
1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index 2ff37ff1cecd..42f27d60adf3 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -1190,12 +1190,9 @@ __split_large_page(struct cpa_data *cpa, pte_t *kpte, unsigned long address,
for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc, lpaddr += lpinc)
split_set_pte(cpa, pbase + i, pfn, ref_prot, lpaddr, lpinc);

- if (virt_addr_valid(address)) {
- unsigned long pfn = PFN_DOWN(__pa(address));
-
- if (pfn_range_is_mapped(pfn, pfn + 1))
- split_page_count(level);
- }
+ if (within(address, PAGE_OFFSET,
+ PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))
+ split_page_count(level);

/*
* Install the new, split up pagetable.
@@ -1314,7 +1311,8 @@ static int collapse_pmd_page(pmd_t *pmd, unsigned long addr,
}
}

- if (virt_addr_valid(addr) && pfn_range_is_mapped(pfn, pfn + 1))
+ if (within(addr, PAGE_OFFSET,
+ PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))
collapse_page_count(PG_LEVEL_2M);

return 1;
@@ -1361,7 +1359,8 @@ static int collapse_pud_page(pud_t *pud, unsigned long addr,
list_add(&page_ptdesc(pud_page(*pud))->pt_list, pgtables);
set_pud(pud, pfn_pud(pfn, pmd_pgprot(first)));

- if (virt_addr_valid(addr) && pfn_range_is_mapped(pfn, pfn + 1))
+ if (within(addr, PAGE_OFFSET,
+ PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))
collapse_page_count(PG_LEVEL_1G);

return 1;

--
2.53.0