__kernel_physical_mapping_init() will soon need to work on multiple
PGDs, so factor out something similar to phys_p4d_init() and friends,
which takes the base of the PGD as an argument.
Signed-off-by: Brendan Jackman <jackmanb@google.com>
---
arch/x86/mm/init_64.c | 33 +++++++++++++++++++++++----------
1 file changed, 23 insertions(+), 10 deletions(-)
diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c
index 0e4270e20fadb578c7fd6bf5c5e4762027c36c45..e98e85cf15f42db669696ba8195d8fc633351b26 100644
--- a/arch/x86/mm/init_64.c
+++ b/arch/x86/mm/init_64.c
@@ -741,21 +741,20 @@ phys_p4d_init(p4d_t *p4d_page, unsigned long paddr, unsigned long paddr_end,
}
static unsigned long __meminit
-__kernel_physical_mapping_init(unsigned long paddr_start,
- unsigned long paddr_end,
- unsigned long page_size_mask,
- pgprot_t prot, bool init)
+phys_pgd_init(pgd_t *pgd_page, unsigned long paddr_start, unsigned long paddr_end,
+ unsigned long page_size_mask, pgprot_t prot, bool init, bool *pgd_changed)
{
- bool pgd_changed = false;
unsigned long vaddr, vaddr_start, vaddr_end, vaddr_next, paddr_last;
+ *pgd_changed = false;
+
paddr_last = paddr_end;
vaddr = (unsigned long)__va(paddr_start);
vaddr_end = (unsigned long)__va(paddr_end);
vaddr_start = vaddr;
for (; vaddr < vaddr_end; vaddr = vaddr_next) {
- pgd_t *pgd = pgd_offset_k(vaddr);
+ pgd_t *pgd = pgd_offset_pgd(pgd_page, vaddr);
p4d_t *p4d;
vaddr_next = (vaddr & PGDIR_MASK) + PGDIR_SIZE;
@@ -781,15 +780,29 @@ __kernel_physical_mapping_init(unsigned long paddr_start,
(pud_t *) p4d, init);
spin_unlock(&init_mm.page_table_lock);
- pgd_changed = true;
+ *pgd_changed = true;
}
- if (pgd_changed)
- sync_global_pgds(vaddr_start, vaddr_end - 1);
-
return paddr_last;
}
+static unsigned long __meminit
+__kernel_physical_mapping_init(unsigned long paddr_start,
+ unsigned long paddr_end,
+ unsigned long page_size_mask,
+ pgprot_t prot, bool init)
+{
+ bool pgd_changed;
+ unsigned long paddr_last;
+
+ paddr_last = phys_pgd_init(init_mm.pgd, paddr_start, paddr_end, page_size_mask,
+ prot, init, &pgd_changed);
+ if (pgd_changed)
+ sync_global_pgds((unsigned long)__va(paddr_start),
+ (unsigned long)__va(paddr_end) - 1);
+
+ return paddr_last;
+}
/*
* Create page table mapping for the physical memory for specific physical
--
2.50.1
On Wed, Sep 24, 2025 at 02:59:38PM +0000, Brendan Jackman wrote:
> +static unsigned long __meminit
> +__kernel_physical_mapping_init(unsigned long paddr_start,
> + unsigned long paddr_end,
> + unsigned long page_size_mask,
> + pgprot_t prot, bool init)
> +{
> + bool pgd_changed;
I have to say, that pgd_changed is yuck but I don't have a better idea and
this has happened a long time ago anyway.
How about you have the caller pass in false:
bool pgd_changed = false;
and then callee sets it to true when it does so?
> + unsigned long paddr_last;
The tip-tree preferred ordering of variable declarations at the
beginning of a function is reverse fir tree order::
struct long_struct_name *descriptive_name;
unsigned long foo, bar;
unsigned int tmp;
int ret;
The above is faster to parse than the reverse ordering::
int ret;
unsigned int tmp;
unsigned long foo, bar;
struct long_struct_name *descriptive_name;
And even more so than random ordering::
unsigned long foo, bar;
int ret;
struct long_struct_name *descriptive_name;
unsigned int tmp;
> +
> + paddr_last = phys_pgd_init(init_mm.pgd, paddr_start, paddr_end, page_size_mask,
> + prot, init, &pgd_changed);
> + if (pgd_changed)
> + sync_global_pgds((unsigned long)__va(paddr_start),
> + (unsigned long)__va(paddr_end) - 1);
> +
> + return paddr_last;
> +}
>
> /*
> * Create page table mapping for the physical memory for specific physical
>
> --
> 2.50.1
>
--
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette
On Sat Oct 25, 2025 at 11:48 AM UTC, Borislav Petkov wrote:
> On Wed, Sep 24, 2025 at 02:59:38PM +0000, Brendan Jackman wrote:
>> +static unsigned long __meminit
>> +__kernel_physical_mapping_init(unsigned long paddr_start,
>> + unsigned long paddr_end,
>> + unsigned long page_size_mask,
>> + pgprot_t prot, bool init)
>> +{
>> + bool pgd_changed;
>
> I have to say, that pgd_changed is yuck but I don't have a better idea and
> this has happened a long time ago anyway.
>
> How about you have the caller pass in false:
>
> bool pgd_changed = false;
>
> and then callee sets it to true when it does so?
Sure.
Per Dave's feedback I am still slightly hopeful I can find a way to
come in and refactor this code so that it's gets cleaner for you guys
and then ASI becomes a natural addition. So far I don't come up with
anything in init_64.c but I'm still planning to stare at set_memory.c a
while longer and see if anything comes to mind. So maybe we'll be able
to reduce the yuck factor a bit.
>> + unsigned long paddr_last;
>
> The tip-tree preferred ordering of variable declarations at the
> beginning of a function is reverse fir tree order::
>
> struct long_struct_name *descriptive_name;
> unsigned long foo, bar;
> unsigned int tmp;
> int ret;
>
> The above is faster to parse than the reverse ordering::
>
> int ret;
> unsigned int tmp;
> unsigned long foo, bar;
> struct long_struct_name *descriptive_name;
>
> And even more so than random ordering::
>
> unsigned long foo, bar;
> int ret;
> struct long_struct_name *descriptive_name;
> unsigned int tmp;
Ack
>> +
>> + paddr_last = phys_pgd_init(init_mm.pgd, paddr_start, paddr_end, page_size_mask,
>> + prot, init, &pgd_changed);
>> + if (pgd_changed)
>> + sync_global_pgds((unsigned long)__va(paddr_start),
>> + (unsigned long)__va(paddr_end) - 1);
>> +
>> + return paddr_last;
>> +}
>>
>> /*
>> * Create page table mapping for the physical memory for specific physical
>>
>> --
>> 2.50.1
>>
On Sun, Oct 26, 2025 at 10:29:23PM +0000, Brendan Jackman wrote:
> Per Dave's feedback I am still slightly hopeful I can find a way to
> come in and refactor this code so that it's gets cleaner for you guys
> and then ASI becomes a natural addition. So far I don't come up with
> anything in init_64.c but I'm still planning to stare at set_memory.c a
> while longer and see if anything comes to mind. So maybe we'll be able
> to reduce the yuck factor a bit.
Cleanups like that are always more than welcome!
:-)
--
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette
On Mon Nov 10, 2025 at 11:38 AM UTC, Borislav Petkov wrote: > On Sun, Oct 26, 2025 at 10:29:23PM +0000, Brendan Jackman wrote: >> Per Dave's feedback I am still slightly hopeful I can find a way to >> come in and refactor this code so that it's gets cleaner for you guys >> and then ASI becomes a natural addition. So far I don't come up with >> anything in init_64.c but I'm still planning to stare at set_memory.c a >> while longer and see if anything comes to mind. So maybe we'll be able >> to reduce the yuck factor a bit. > > Cleanups like that are always more than welcome! > > :-) In that case, I will advertise this (less ambitious) cleanup which is awaiting review: https://lore.kernel.org/all/20251003-x86-init-cleanup-v1-4-f2b7994c2ad6@google.com/
Hi Brendan,
kernel test robot noticed the following build warnings:
[auto build test WARNING on bf2602a3cb2381fb1a04bf1c39a290518d2538d1]
url: https://github.com/intel-lab-lkp/linux/commits/Brendan-Jackman/x86-mm-asi-Add-CONFIG_MITIGATION_ADDRESS_SPACE_ISOLATION/20250924-230633
base: bf2602a3cb2381fb1a04bf1c39a290518d2538d1
patch link: https://lore.kernel.org/r/20250924-b4-asi-page-alloc-v1-3-2d861768041f%40google.com
patch subject: [PATCH 03/21] x86/mm: factor out phys_pgd_init()
config: x86_64-kexec (https://download.01.org/0day-ci/archive/20250927/202509272136.N4ELb64u-lkp@intel.com/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250927/202509272136.N4ELb64u-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202509272136.N4ELb64u-lkp@intel.com/
All warnings (new ones prefixed by >>):
>> arch/x86/mm/init_64.c:747:23: warning: variable 'vaddr_start' set but not used [-Wunused-but-set-variable]
747 | unsigned long vaddr, vaddr_start, vaddr_end, vaddr_next, paddr_last;
| ^
1 warning generated.
vim +/vaddr_start +747 arch/x86/mm/init_64.c
7e82ea946ae4d0 arch/x86/mm/init_64.c Kirill A. Shutemov 2017-06-06 742
eccd906484d1cd arch/x86/mm/init_64.c Brijesh Singh 2019-04-17 743 static unsigned long __meminit
46b7f8ebabd0a2 arch/x86/mm/init_64.c Brendan Jackman 2025-09-24 744 phys_pgd_init(pgd_t *pgd_page, unsigned long paddr_start, unsigned long paddr_end,
46b7f8ebabd0a2 arch/x86/mm/init_64.c Brendan Jackman 2025-09-24 745 unsigned long page_size_mask, pgprot_t prot, bool init, bool *pgd_changed)
^1da177e4c3f41 arch/x86_64/mm/init.c Linus Torvalds 2005-04-16 746 {
59b3d0206d74a7 arch/x86/mm/init_64.c Thomas Garnier 2016-06-21 @747 unsigned long vaddr, vaddr_start, vaddr_end, vaddr_next, paddr_last;
^1da177e4c3f41 arch/x86_64/mm/init.c Linus Torvalds 2005-04-16 748
46b7f8ebabd0a2 arch/x86/mm/init_64.c Brendan Jackman 2025-09-24 749 *pgd_changed = false;
46b7f8ebabd0a2 arch/x86/mm/init_64.c Brendan Jackman 2025-09-24 750
59b3d0206d74a7 arch/x86/mm/init_64.c Thomas Garnier 2016-06-21 751 paddr_last = paddr_end;
59b3d0206d74a7 arch/x86/mm/init_64.c Thomas Garnier 2016-06-21 752 vaddr = (unsigned long)__va(paddr_start);
59b3d0206d74a7 arch/x86/mm/init_64.c Thomas Garnier 2016-06-21 753 vaddr_end = (unsigned long)__va(paddr_end);
59b3d0206d74a7 arch/x86/mm/init_64.c Thomas Garnier 2016-06-21 754 vaddr_start = vaddr;
^1da177e4c3f41 arch/x86_64/mm/init.c Linus Torvalds 2005-04-16 755
59b3d0206d74a7 arch/x86/mm/init_64.c Thomas Garnier 2016-06-21 756 for (; vaddr < vaddr_end; vaddr = vaddr_next) {
46b7f8ebabd0a2 arch/x86/mm/init_64.c Brendan Jackman 2025-09-24 757 pgd_t *pgd = pgd_offset_pgd(pgd_page, vaddr);
f2a6a7050109e0 arch/x86/mm/init_64.c Kirill A. Shutemov 2017-03-17 758 p4d_t *p4d;
44df75e629106e arch/x86_64/mm/init.c Matt Tolentino 2006-01-17 759
59b3d0206d74a7 arch/x86/mm/init_64.c Thomas Garnier 2016-06-21 760 vaddr_next = (vaddr & PGDIR_MASK) + PGDIR_SIZE;
4f9c11dd49fb73 arch/x86/mm/init_64.c Jeremy Fitzhardinge 2008-06-25 761
7e82ea946ae4d0 arch/x86/mm/init_64.c Kirill A. Shutemov 2017-06-06 762 if (pgd_val(*pgd)) {
7e82ea946ae4d0 arch/x86/mm/init_64.c Kirill A. Shutemov 2017-06-06 763 p4d = (p4d_t *)pgd_page_vaddr(*pgd);
7e82ea946ae4d0 arch/x86/mm/init_64.c Kirill A. Shutemov 2017-06-06 764 paddr_last = phys_p4d_init(p4d, __pa(vaddr),
59b3d0206d74a7 arch/x86/mm/init_64.c Thomas Garnier 2016-06-21 765 __pa(vaddr_end),
eccd906484d1cd arch/x86/mm/init_64.c Brijesh Singh 2019-04-17 766 page_size_mask,
c164fbb40c43f8 arch/x86/mm/init_64.c Logan Gunthorpe 2020-04-10 767 prot, init);
4f9c11dd49fb73 arch/x86/mm/init_64.c Jeremy Fitzhardinge 2008-06-25 768 continue;
4f9c11dd49fb73 arch/x86/mm/init_64.c Jeremy Fitzhardinge 2008-06-25 769 }
4f9c11dd49fb73 arch/x86/mm/init_64.c Jeremy Fitzhardinge 2008-06-25 770
7e82ea946ae4d0 arch/x86/mm/init_64.c Kirill A. Shutemov 2017-06-06 771 p4d = alloc_low_page();
7e82ea946ae4d0 arch/x86/mm/init_64.c Kirill A. Shutemov 2017-06-06 772 paddr_last = phys_p4d_init(p4d, __pa(vaddr), __pa(vaddr_end),
c164fbb40c43f8 arch/x86/mm/init_64.c Logan Gunthorpe 2020-04-10 773 page_size_mask, prot, init);
8ae3a5a8dff2c9 arch/x86/mm/init_64.c Jan Beulich 2008-08-21 774
8ae3a5a8dff2c9 arch/x86/mm/init_64.c Jan Beulich 2008-08-21 775 spin_lock(&init_mm.page_table_lock);
ed7588d5dc6f5e arch/x86/mm/init_64.c Kirill A. Shutemov 2018-05-18 776 if (pgtable_l5_enabled())
eccd906484d1cd arch/x86/mm/init_64.c Brijesh Singh 2019-04-17 777 pgd_populate_init(&init_mm, pgd, p4d, init);
7e82ea946ae4d0 arch/x86/mm/init_64.c Kirill A. Shutemov 2017-06-06 778 else
eccd906484d1cd arch/x86/mm/init_64.c Brijesh Singh 2019-04-17 779 p4d_populate_init(&init_mm, p4d_offset(pgd, vaddr),
eccd906484d1cd arch/x86/mm/init_64.c Brijesh Singh 2019-04-17 780 (pud_t *) p4d, init);
eccd906484d1cd arch/x86/mm/init_64.c Brijesh Singh 2019-04-17 781
8ae3a5a8dff2c9 arch/x86/mm/init_64.c Jan Beulich 2008-08-21 782 spin_unlock(&init_mm.page_table_lock);
46b7f8ebabd0a2 arch/x86/mm/init_64.c Brendan Jackman 2025-09-24 783 *pgd_changed = true;
46b7f8ebabd0a2 arch/x86/mm/init_64.c Brendan Jackman 2025-09-24 784 }
46b7f8ebabd0a2 arch/x86/mm/init_64.c Brendan Jackman 2025-09-24 785
46b7f8ebabd0a2 arch/x86/mm/init_64.c Brendan Jackman 2025-09-24 786 return paddr_last;
^1da177e4c3f41 arch/x86_64/mm/init.c Linus Torvalds 2005-04-16 787 }
9b861528a8012e arch/x86/mm/init_64.c Haicheng Li 2010-08-20 788
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
On Sat Sep 27, 2025 at 7:29 PM UTC, kernel test robot wrote: > Hi Brendan, > > kernel test robot noticed the following build warnings: > > [auto build test WARNING on bf2602a3cb2381fb1a04bf1c39a290518d2538d1] I've fixed this and the others in my WIP branch but I will wait a bit longer before sending a v2.. They're all real issues - one of them confirms I have not exercised the CMA with this code (by demonstrating that I did not even compile with CONFIG_CMA=y). This one is just a benign bug that only shows up with W=1.
© 2016 - 2026 Red Hat, Inc.