Skip to content

Commit 80fc7c7

Browse files
hygonigregkh
authored andcommitted
mm: introduce and use {pgd,p4d}_populate_kernel()
commit f2d2f95 upstream. Introduce and use {pgd,p4d}_populate_kernel() in core MM code when populating PGD and P4D entries for the kernel address space. These helpers ensure proper synchronization of page tables when updating the kernel portion of top-level page tables. Until now, the kernel has relied on each architecture to handle synchronization of top-level page tables in an ad-hoc manner. For example, see commit 9b86152 ("x86-64, mem: Update all PGDs for direct mapping and vmemmap mapping changes"). However, this approach has proven fragile for following reasons: 1) It is easy to forget to perform the necessary page table synchronization when introducing new changes. For instance, commit 4917f55 ("mm/sparse-vmemmap: improve memory savings for compound devmaps") overlooked the need to synchronize page tables for the vmemmap area. 2) It is also easy to overlook that the vmemmap and direct mapping areas must not be accessed before explicit page table synchronization. For example, commit 8d40091 ("x86/vmemmap: handle unpopulated sub-pmd ranges")) caused crashes by accessing the vmemmap area before calling sync_global_pgds(). To address this, as suggested by Dave Hansen, introduce _kernel() variants of the page table population helpers, which invoke architecture-specific hooks to properly synchronize page tables. These are introduced in a new header file, include/linux/pgalloc.h, so they can be called from common code. They reuse existing infrastructure for vmalloc and ioremap. Synchronization requirements are determined by ARCH_PAGE_TABLE_SYNC_MASK, and the actual synchronization is performed by arch_sync_kernel_mappings(). This change currently targets only x86_64, so only PGD and P4D level helpers are introduced. Currently, these helpers are no-ops since no architecture sets PGTBL_{PGD,P4D}_MODIFIED in ARCH_PAGE_TABLE_SYNC_MASK. In theory, PUD and PMD level helpers can be added later if needed by other architectures. For now, 32-bit architectures (x86-32 and arm) only handle PGTBL_PMD_MODIFIED, so p*d_populate_kernel() will never affect them unless we introduce a PMD level helper. [harry.yoo@oracle.com: fix KASAN build error due to p*d_populate_kernel()] Link: https://lkml.kernel.org/r/20250822020727.202749-1-harry.yoo@oracle.com Link: https://lkml.kernel.org/r/20250818020206.4517-3-harry.yoo@oracle.com Fixes: 8d40091 ("x86/vmemmap: handle unpopulated sub-pmd ranges") Signed-off-by: Harry Yoo <harry.yoo@oracle.com> Suggested-by: Dave Hansen <dave.hansen@linux.intel.com> Acked-by: Kiryl Shutsemau <kas@kernel.org> Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org> Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Acked-by: David Hildenbrand <david@redhat.com> Cc: Alexander Potapenko <glider@google.com> Cc: Alistair Popple <apopple@nvidia.com> Cc: Andrey Konovalov <andreyknvl@gmail.com> Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com> Cc: Andy Lutomirski <luto@kernel.org> Cc: "Aneesh Kumar K.V" <aneesh.kumar@linux.ibm.com> Cc: Anshuman Khandual <anshuman.khandual@arm.com> Cc: Ard Biesheuvel <ardb@kernel.org> Cc: Arnd Bergmann <arnd@arndb.de> Cc: bibo mao <maobibo@loongson.cn> Cc: Borislav Betkov <bp@alien8.de> Cc: Christoph Lameter (Ampere) <cl@gentwo.org> Cc: Dennis Zhou <dennis@kernel.org> Cc: Dev Jain <dev.jain@arm.com> Cc: Dmitriy Vyukov <dvyukov@google.com> Cc: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Jane Chu <jane.chu@oracle.com> Cc: Joao Martins <joao.m.martins@oracle.com> Cc: Joerg Roedel <joro@8bytes.org> Cc: John Hubbard <jhubbard@nvidia.com> Cc: Kevin Brodsky <kevin.brodsky@arm.com> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Oscar Salvador <osalvador@suse.de> Cc: Peter Xu <peterx@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Qi Zheng <zhengqi.arch@bytedance.com> Cc: Ryan Roberts <ryan.roberts@arm.com> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Tejun Heo <tj@kernel.org> Cc: Thomas Gleinxer <tglx@linutronix.de> Cc: Thomas Huth <thuth@redhat.com> Cc: "Uladzislau Rezki (Sony)" <urezki@gmail.com> Cc: Vincenzo Frascino <vincenzo.frascino@arm.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> [ Adjust context ] Signed-off-by: Harry Yoo <harry.yoo@oracle.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent cee8913 commit 80fc7c7

File tree

5 files changed

+48
-18
lines changed

5 files changed

+48
-18
lines changed

include/linux/pgalloc.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _LINUX_PGALLOC_H
3+
#define _LINUX_PGALLOC_H
4+
5+
#include <linux/pgtable.h>
6+
#include <asm/pgalloc.h>
7+
8+
/*
9+
* {pgd,p4d}_populate_kernel() are defined as macros to allow
10+
* compile-time optimization based on the configured page table levels.
11+
* Without this, linking may fail because callers (e.g., KASAN) may rely
12+
* on calls to these functions being optimized away when passing symbols
13+
* that exist only for certain page table levels.
14+
*/
15+
#define pgd_populate_kernel(addr, pgd, p4d) \
16+
do { \
17+
pgd_populate(&init_mm, pgd, p4d); \
18+
if (ARCH_PAGE_TABLE_SYNC_MASK & PGTBL_PGD_MODIFIED) \
19+
arch_sync_kernel_mappings(addr, addr); \
20+
} while (0)
21+
22+
#define p4d_populate_kernel(addr, p4d, pud) \
23+
do { \
24+
p4d_populate(&init_mm, p4d, pud); \
25+
if (ARCH_PAGE_TABLE_SYNC_MASK & PGTBL_P4D_MODIFIED) \
26+
arch_sync_kernel_mappings(addr, addr); \
27+
} while (0)
28+
29+
#endif /* _LINUX_PGALLOC_H */

include/linux/pgtable.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,8 +1474,8 @@ static inline int pmd_protnone(pmd_t pmd)
14741474

14751475
/*
14761476
* Architectures can set this mask to a combination of PGTBL_P?D_MODIFIED values
1477-
* and let generic vmalloc and ioremap code know when arch_sync_kernel_mappings()
1478-
* needs to be called.
1477+
* and let generic vmalloc, ioremap and page table update code know when
1478+
* arch_sync_kernel_mappings() needs to be called.
14791479
*/
14801480
#ifndef ARCH_PAGE_TABLE_SYNC_MASK
14811481
#define ARCH_PAGE_TABLE_SYNC_MASK 0
@@ -1608,10 +1608,11 @@ static inline bool arch_has_pfn_modify_check(void)
16081608
/*
16091609
* Page Table Modification bits for pgtbl_mod_mask.
16101610
*
1611-
* These are used by the p?d_alloc_track*() set of functions an in the generic
1612-
* vmalloc/ioremap code to track at which page-table levels entries have been
1613-
* modified. Based on that the code can better decide when vmalloc and ioremap
1614-
* mapping changes need to be synchronized to other page-tables in the system.
1611+
* These are used by the p?d_alloc_track*() and p*d_populate_kernel()
1612+
* functions in the generic vmalloc, ioremap and page table update code
1613+
* to track at which page-table levels entries have been modified.
1614+
* Based on that the code can better decide when page table changes need
1615+
* to be synchronized to other page-tables in the system.
16151616
*/
16161617
#define __PGTBL_PGD_MODIFIED 0
16171618
#define __PGTBL_P4D_MODIFIED 1

mm/kasan/init.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
#include <linux/mm.h>
1414
#include <linux/pfn.h>
1515
#include <linux/slab.h>
16+
#include <linux/pgalloc.h>
1617

1718
#include <asm/page.h>
18-
#include <asm/pgalloc.h>
1919

2020
#include "kasan.h"
2121

@@ -188,7 +188,7 @@ static int __ref zero_p4d_populate(pgd_t *pgd, unsigned long addr,
188188
pud_t *pud;
189189
pmd_t *pmd;
190190

191-
p4d_populate(&init_mm, p4d,
191+
p4d_populate_kernel(addr, p4d,
192192
lm_alias(kasan_early_shadow_pud));
193193
pud = pud_offset(p4d, addr);
194194
pud_populate(&init_mm, pud,
@@ -207,7 +207,7 @@ static int __ref zero_p4d_populate(pgd_t *pgd, unsigned long addr,
207207
if (!p)
208208
return -ENOMEM;
209209
} else {
210-
p4d_populate(&init_mm, p4d,
210+
p4d_populate_kernel(addr, p4d,
211211
early_alloc(PAGE_SIZE, NUMA_NO_NODE));
212212
}
213213
}
@@ -247,10 +247,10 @@ int __ref kasan_populate_early_shadow(const void *shadow_start,
247247
* puds,pmds, so pgd_populate(), pud_populate()
248248
* is noops.
249249
*/
250-
pgd_populate(&init_mm, pgd,
250+
pgd_populate_kernel(addr, pgd,
251251
lm_alias(kasan_early_shadow_p4d));
252252
p4d = p4d_offset(pgd, addr);
253-
p4d_populate(&init_mm, p4d,
253+
p4d_populate_kernel(addr, p4d,
254254
lm_alias(kasan_early_shadow_pud));
255255
pud = pud_offset(p4d, addr);
256256
pud_populate(&init_mm, pud,
@@ -269,7 +269,7 @@ int __ref kasan_populate_early_shadow(const void *shadow_start,
269269
if (!p)
270270
return -ENOMEM;
271271
} else {
272-
pgd_populate(&init_mm, pgd,
272+
pgd_populate_kernel(addr, pgd,
273273
early_alloc(PAGE_SIZE, NUMA_NO_NODE));
274274
}
275275
}

mm/percpu.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3172,7 +3172,7 @@ int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
31723172
#endif /* BUILD_EMBED_FIRST_CHUNK */
31733173

31743174
#ifdef BUILD_PAGE_FIRST_CHUNK
3175-
#include <asm/pgalloc.h>
3175+
#include <linux/pgalloc.h>
31763176

31773177
#ifndef P4D_TABLE_SIZE
31783178
#define P4D_TABLE_SIZE PAGE_SIZE
@@ -3202,7 +3202,7 @@ void __init __weak pcpu_populate_pte(unsigned long addr)
32023202
new = memblock_alloc(P4D_TABLE_SIZE, P4D_TABLE_SIZE);
32033203
if (!new)
32043204
goto err_alloc;
3205-
pgd_populate(&init_mm, pgd, new);
3205+
pgd_populate_kernel(addr, pgd, new);
32063206
}
32073207

32083208
p4d = p4d_offset(pgd, addr);
@@ -3212,7 +3212,7 @@ void __init __weak pcpu_populate_pte(unsigned long addr)
32123212
new = memblock_alloc(PUD_TABLE_SIZE, PUD_TABLE_SIZE);
32133213
if (!new)
32143214
goto err_alloc;
3215-
p4d_populate(&init_mm, p4d, new);
3215+
p4d_populate_kernel(addr, p4d, new);
32163216
}
32173217

32183218
pud = pud_offset(p4d, addr);

mm/sparse-vmemmap.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
#include <linux/spinlock.h>
2828
#include <linux/vmalloc.h>
2929
#include <linux/sched.h>
30+
#include <linux/pgalloc.h>
3031

3132
#include <asm/dma.h>
32-
#include <asm/pgalloc.h>
3333

3434
/*
3535
* Allocate a block of memory to be used to back the virtual memory map
@@ -215,7 +215,7 @@ p4d_t * __meminit vmemmap_p4d_populate(pgd_t *pgd, unsigned long addr, int node)
215215
void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node);
216216
if (!p)
217217
return NULL;
218-
p4d_populate(&init_mm, p4d, p);
218+
p4d_populate_kernel(addr, p4d, p);
219219
}
220220
return p4d;
221221
}
@@ -227,7 +227,7 @@ pgd_t * __meminit vmemmap_pgd_populate(unsigned long addr, int node)
227227
void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node);
228228
if (!p)
229229
return NULL;
230-
pgd_populate(&init_mm, pgd, p);
230+
pgd_populate_kernel(addr, pgd, p);
231231
}
232232
return pgd;
233233
}

0 commit comments

Comments
 (0)