Skip to contentSkip to conversation

x86/mm: some cleanups for pagetable setup code

PATCH Superseded Brendan Jackman 4 participants 14 messages May 3 – Jun 24

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover x86/mm: some cleanups for pagetable setup code

Per discussion in [0] I'm looking for ways to refactor this code to make ASI easier to deal with. But, while looking, I found some little things that seem like just straightforward cleanups without any real refactoring needed. So let's start there.

This applies to tip/master.

I'm having some infra issues so this hasn't been through Sashiko review yet. I've tested it on QEMU.

[0] https://lore.kernel.org/all/[email protected]/T/#t

---
Changes in v2:
- Simplified patchset, instead of trying to fix confusing code only to
delete it in a subsequent patch, just delete it in the first place.
- Fixed add_pfn_range_mapped() args. (This bug causes a KASAN build to
crash during boot).
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Brendan Jackman (3):
x86/mm: drop unused return from init_memory_mapping() x86/mm: simplify calculation of max_pfn_mapped x86/mm: drop unused returns from direct map setup functions

 arch/x86/include/asm/pgtable.h |  3 +-
 arch/x86/mm/init.c             | 19 ++++-----
 arch/x86/mm/init_32.c          |  5 +--
 arch/x86/mm/init_64.c          | 96 +++++++++++++++---------------------------
 arch/x86/mm/mm_internal.h      | 11 ++---
 5 files changed, 48 insertions(+), 86 deletions(-)

---
base-commit: 32b8f4c4b8650a879d15ca10f2462d1072e49381
change-id: 20251003-x86-init-cleanup-0ad754910bac

Best regards,

Signature · 1 line
Brendan Jackman <[email protected]>
#1 lore raw

PATCH v2 1/3 x86/mm: drop unused return from init_memory_mapping()

None of the callers look at the return value.

---
 arch/x86/include/asm/pgtable.h |  3 +--
 arch/x86/mm/init.c             | 16 +++++++---------
 2 files changed, 8 insertions(+), 11 deletions(-)
arch/x86/include/asm/pgtable.h +1 -2
index 2187e9cfcefa1..eb09fa7840b49 100644
@@ -1170,8 +1170,7 @@ extern int direct_gbpages;
 void init_mem_mapping(void);
 void early_alloc_pgt_buf(void);
 void __init poking_init(void);
-unsigned long init_memory_mapping(unsigned long start,
-				  unsigned long end, pgprot_t prot);
+void init_memory_mapping(unsigned long start, unsigned long end, pgprot_t prot);
 
 #ifdef CONFIG_X86_64
 extern pgd_t trampoline_pgd_entry;
arch/x86/mm/init.c +7 -9
index fb67217fddcd3..ae3e9e0820153 100644
@@ -531,11 +531,11 @@ bool pfn_range_is_mapped(unsigned long start_pfn, unsigned long end_pfn)
  * This runs before bootmem is initialized and gets pages directly from
  * the physical memory. To access them they are temporarily mapped.
  */
-unsigned long __ref init_memory_mapping(unsigned long start,
-					unsigned long end, pgprot_t prot)
+void __ref init_memory_mapping(unsigned long start,
+			       unsigned long end, pgprot_t prot)
 {
 	struct map_range mr[NR_RANGE_MR];
-	unsigned long ret = 0;
+	unsigned long paddr_last = 0;
 	int nr_range, i;
 
 	pr_debug("init_memory_mapping: [mem %#010lx-%#010lx]\n",
@@ -545,13 +545,11 @@ unsigned long __ref init_memory_mapping(unsigned long start,
 	nr_range = split_mem_range(mr, 0, start, end);
 
 	for (i = 0; i < nr_range; i++)
-		ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
-						   mr[i].page_size_mask,
-						   prot);
+		paddr_last = kernel_physical_mapping_init(mr[i].start, mr[i].end,
+							  mr[i].page_size_mask,
+							  prot);
 
-	add_pfn_range_mapped(start >> PAGE_SHIFT, ret >> PAGE_SHIFT);
-
-	return ret >> PAGE_SHIFT;
+	add_pfn_range_mapped(start >> PAGE_SHIFT, paddr_last >> PAGE_SHIFT);
 }
 
 /*
Signature · 1 line
2.51.2
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] x86/mm: drop unused return from init_memory_mapping()

Quoted 1 line · Brendan Jackman

None of the callers look at the return value.

Ahh, yes. Looks like once upon a time it was used to set max_pfn_mapped. But, at some point add_pfn_range_mapped() started doing it instead.

#4 lore raw

PATCH v2 2/3 x86/mm: simplify calculation of max_pfn_mapped

In reply to Brendan Jackman

The phys_*_init()s return the "last physical address mapped". The exact definition of this is pretty fiddly, but only in these conditions:

1. There is a mismatch between the alignment of the requested range and the page sizes allowed by page_size_mask

2. The range ends in a region that is not mapped according to e820.

3. The range ends in a region that was already mapped (note this case is particularly fiddly because the return value depends on what level the existing mapping is at. This is probably a bug, see [0] for discussion).

Luckily, init_memory_mapping() avoids all these conditions. In that case, the return value is just paddr_end. And that value is already present, no need to depend on the confusing return value.

[0]: https://lore.kernel.org/all/[email protected]/

---
 arch/x86/mm/init.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/mm/init.c +5 -4
index ae3e9e0820153..1a6a6fc700bb5 100644
@@ -544,10 +544,11 @@ void __ref init_memory_mapping(unsigned long start,
 	memset(mr, 0, sizeof(mr));
 	nr_range = split_mem_range(mr, 0, start, end);
 
-	for (i = 0; i < nr_range; i++)
-		paddr_last = kernel_physical_mapping_init(mr[i].start, mr[i].end,
-							  mr[i].page_size_mask,
-							  prot);
+	for (i = 0; i < nr_range; i++) {
+		kernel_physical_mapping_init(mr[i].start, mr[i].end,
+					     mr[i].page_size_mask, prot);
+		paddr_last = mr[i].end;
+	}
 
 	add_pfn_range_mapped(start >> PAGE_SHIFT, paddr_last >> PAGE_SHIFT);
 }
Signature · 1 line
2.51.2
#5 lore raw

Re: [PATCH v2 2/3] x86/mm: simplify calculation of max_pfn_mapped

On 5/3/26 06:04, Brendan Jackman wrote:
...

Quoted 3 lines

Luckily, init_memory_mapping() avoids all these conditions. In that case, the return value is just paddr_end. And that value is already present, no need to depend on the confusing return value.

It feels like we should say something about split_mem_range() here. All of the guaranteed non-fiddly behavior originates in there, right?

Quoted 7 lines
arch/x86/mm/init.c +0 -0
index ae3e9e0820153..1a6a6fc700bb5 100644
@@ -544,10 +544,11 @@ void __ref init_memory_mapping(unsigned long start,
 	memset(mr, 0, sizeof(mr));
 	nr_range = split_mem_range(mr, 0, start, end);

arch/x86/mm/init.c void __ref init_memory_mapping(unsigned long start, · line 551

Quoted 10 lines
arch/x86/mm/init.c +5 -4
 
-	for (i = 0; i < nr_range; i++)
-		paddr_last = kernel_physical_mapping_init(mr[i].start, mr[i].end,
-							  mr[i].page_size_mask,
-							  prot);
+	for (i = 0; i < nr_range; i++) {
+		kernel_physical_mapping_init(mr[i].start, mr[i].end,
+					     mr[i].page_size_mask, prot);
+		paddr_last = mr[i].end;
+	}

I guess this is actually:

	for (i = 0; i < nr_range; i++)
		kernel_physical_mapping_init(...);

	paddr_last = mr[nr_range-1].end;

Right? But what you have is probably just as compact.

#6 lore raw

Re: [PATCH v2 2/3] x86/mm: simplify calculation of max_pfn_mapped

Quoted 8 lines · Dave Hansen

On 5/3/26 06:04, Brendan Jackman wrote:
...

Quoted 3 lines

Luckily, init_memory_mapping() avoids all these conditions. In that case, the return value is just paddr_end. And that value is already present, no need to depend on the confusing return value.

It feels like we should say something about split_mem_range() here. All of the guaranteed non-fiddly behavior originates in there, right?

[pasting back the conditions from the commit message for context]

Quoted 5 lines
Quoted 5 lines

but only in these conditions:

1. There is a mismatch between the alignment of the requested range and the page sizes allowed by page_size_mask

Quoted 7 lines
Quoted 7 lines

2. The range ends in a region that is not mapped according to

      e820.

3. The range ends in a region that was already mapped (note this case is particularly fiddly because the return value depends on what level the existing mapping is at. This is probably a bug, see [0] for discussion).

split_mem_range() is responsible for excluding point 1, since it returns the correct page_size_mask. The other two are actually down to the callers, right?

So how about for point 1 I mention that in the commit message, then for points 2 and 3 maybe they should actually be code comments, i.e. documented as preconditions for calling init_memory_mapping()?

Quoted 20 lines
Quoted 7 lines
arch/x86/mm/init.c +0 -0
index ae3e9e0820153..1a6a6fc700bb5 100644
@@ -544,10 +544,11 @@ void __ref init_memory_mapping(unsigned long start,
 	memset(mr, 0, sizeof(mr));
 	nr_range = split_mem_range(mr, 0, start, end);

arch/x86/mm/init.c void __ref init_memory_mapping(unsigned long start, · line 551

Quoted 10 lines
arch/x86/mm/init.c +5 -4
 
-	for (i = 0; i < nr_range; i++)
-		paddr_last = kernel_physical_mapping_init(mr[i].start, mr[i].end,
-							  mr[i].page_size_mask,
-							  prot);
+	for (i = 0; i < nr_range; i++) {
+		kernel_physical_mapping_init(mr[i].start, mr[i].end,
+					     mr[i].page_size_mask, prot);
+		paddr_last = mr[i].end;
+	}

I guess this is actually:

Quoted 6 lines
	for (i = 0; i < nr_range; i++)
		kernel_physical_mapping_init(...);

	paddr_last = mr[nr_range-1].end;

Right? But what you have is probably just as compact.

Oh, weird. My code might be just as compact but it's confusing, it should be written your way for sure.

#7 lore raw

Re: [PATCH v2 2/3] x86/mm: simplify calculation of max_pfn_mapped

Quoted 24 lines · Brendan Jackman
Quoted 8 lines · Dave Hansen

On 5/3/26 06:04, Brendan Jackman wrote:
...

Quoted 3 lines

Luckily, init_memory_mapping() avoids all these conditions. In that case, the return value is just paddr_end. And that value is already present, no need to depend on the confusing return value.

It feels like we should say something about split_mem_range() here. All of the guaranteed non-fiddly behavior originates in there, right?

[pasting back the conditions from the commit message for context]

Quoted 5 lines
Quoted 5 lines

but only in these conditions:

1. There is a mismatch between the alignment of the requested range and the page sizes allowed by page_size_mask

Quoted 7 lines
Quoted 7 lines

2. The range ends in a region that is not mapped according to

      e820.

3. The range ends in a region that was already mapped (note this case is particularly fiddly because the return value depends on what level the existing mapping is at. This is probably a bug, see [0] for discussion).

Quoted 7 lines

split_mem_range() is responsible for excluding point 1, since it returns the correct page_size_mask. The other two are actually down to the callers, right?

So how about for point 1 I mention that in the commit message, then for points 2 and 3 maybe they should actually be code comments, i.e. documented as preconditions for calling init_memory_mapping()?

For posterity: I realised that treating point 2 as a separate case is bogus here. I was looking at the e820__mapped_any() blocks in phys_*_init() and noting that they don't update the local paddr_last. But actualy, those blocks only run for paddr>=paddr_end, which can already only happen in case 1 i.e. when the range is misaligned wrt page_size_mask.

So I'm just gonna drop that bit.

This realisation is really reinforcing that removing this return value is the right thing to do.

Quoted 29 lines
Quoted 20 lines
Quoted 7 lines
arch/x86/mm/init.c +0 -0
index ae3e9e0820153..1a6a6fc700bb5 100644
@@ -544,10 +544,11 @@ void __ref init_memory_mapping(unsigned long start,
 	memset(mr, 0, sizeof(mr));
 	nr_range = split_mem_range(mr, 0, start, end);

arch/x86/mm/init.c void __ref init_memory_mapping(unsigned long start, · line 551

Quoted 10 lines
arch/x86/mm/init.c +5 -4
 
-	for (i = 0; i < nr_range; i++)
-		paddr_last = kernel_physical_mapping_init(mr[i].start, mr[i].end,
-							  mr[i].page_size_mask,
-							  prot);
+	for (i = 0; i < nr_range; i++) {
+		kernel_physical_mapping_init(mr[i].start, mr[i].end,
+					     mr[i].page_size_mask, prot);
+		paddr_last = mr[i].end;
+	}

I guess this is actually:

Quoted 6 lines
	for (i = 0; i < nr_range; i++)
		kernel_physical_mapping_init(...);

	paddr_last = mr[nr_range-1].end;

Right? But what you have is probably just as compact.

Oh, weird. My code might be just as compact but it's confusing, it should be written your way for sure.

#9 lore raw

PATCH v2 3/3 x86/mm: drop unused returns from direct map setup functions

In reply to Brendan Jackman

Nothing looks at these return values. Furthermore, as discussed in [0], it seems like in the case of a pre-existing 4K mapping, the return value of kernel_physical_mapping_init() is wrong anyway. So, just stop returning a value.

---
 arch/x86/mm/init_32.c     |  5 +--
 arch/x86/mm/init_64.c     | 96 ++++++++++++++++-------------------------------
 arch/x86/mm/mm_internal.h | 11 ++----
 3 files changed, 38 insertions(+), 74 deletions(-)
arch/x86/mm/init_32.c +1 -4
index 0908c44d51e6f..05c456dc9855f 100644
@@ -245,14 +245,13 @@ static inline int is_x86_32_kernel_text(unsigned long addr)
  * of max_low_pfn pages, by creating page tables starting from address
  * PAGE_OFFSET:
  */
-unsigned long __init
+void __init
 kernel_physical_mapping_init(unsigned long start,
 			     unsigned long end,
 			     unsigned long page_size_mask,
 			     pgprot_t prot)
 {
 	int use_pse = page_size_mask == (1<<PG_LEVEL_2M);
-	unsigned long last_map_addr = end;
 	unsigned long start_pfn, end_pfn;
 	pgd_t *pgd_base = swapper_pg_dir;
 	int pgd_idx, pmd_idx, pte_ofs;
@@ -356,7 +355,6 @@ kernel_physical_mapping_init(unsigned long start,
 				pages_4k++;
 				if (mapping_iter == 1) {
 					set_pte(pte, pfn_pte(pfn, init_prot));
-					last_map_addr = (pfn << PAGE_SHIFT) + PAGE_SIZE;
 				} else
 					set_pte(pte, pfn_pte(pfn, prot));
 			}
@@ -382,7 +380,6 @@ kernel_physical_mapping_init(unsigned long start,
 		mapping_iter = 2;
 		goto repeat;
 	}
-	return last_map_addr;
 }
 
 #ifdef CONFIG_HIGHMEM
arch/x86/mm/init_64.c +33 -63

262 lines not on this page · raw message

arch/x86/mm/mm_internal.h +4 -7
index 7c4a41235323b..dad8abe65ed03 100644
@@ -10,13 +10,10 @@ static inline void *alloc_low_page(void)
 
 void early_ioremap_page_table_range_init(void);
 
-unsigned long kernel_physical_mapping_init(unsigned long start,
-					     unsigned long end,
-					     unsigned long page_size_mask,
-					     pgprot_t prot);
-unsigned long kernel_physical_mapping_change(unsigned long start,
-					     unsigned long end,
-					     unsigned long page_size_mask);
+void kernel_physical_mapping_init(unsigned long start, unsigned long end,
+				  unsigned long page_size_mask, pgprot_t prot);
+void kernel_physical_mapping_change(unsigned long start, unsigned long end,
+				    unsigned long page_size_mask);
 
 extern int after_bootmem;
 
Signature · 1 line
2.51.2
Reviewed-by on v2 3/3
#10 lore raw

Re: [PATCH v2 3/3] x86/mm: drop unused returns from direct map setup functions

Quoted 4 lines · Brendan Jackman

Nothing looks at these return values. Furthermore, as discussed in [0], it seems like in the case of a pre-existing 4K mapping, the return value of kernel_physical_mapping_init() is wrong anyway. So, just stop returning a value.

Oh, this is a really nice simplification. That code is not fun. Thanks for figuring it out!

Assuming we can get the changelog worked out for 2/3:

#12 lore raw
In reply to Brendan Jackman

Hi folks,

Can someone take a look at this?

Sashiko claims to have pointed out some pre-existing issues [0]:

- No synchronisation for pfn_mapped. Maybe this could be fixed by spraying some get_online_mems() or something? Fixing this individual bug in isolation seems a bit pointless though, whenever I look into data structures that get modified during hotplug I get the feeling the synchronisation needs a pretty wide overhaul. Maybe someone else feels differently...

- Points out that phys_p4d_init() operates on a single P4D table but doesn't seem to check that the addresses it's operating on are within a single PGD.

I'm pretty sure I noticed this before but assumed it was impossible for the range to span multiple PGDs here. But now I look more carefully I see that's not true and I think Sashiko is right here.

It also points out that the paddr >= paddr_end check looks unreachable, which sounds plausible but I haven't thought it through properly.

Happy to fix the latter as an additional patch but I think the rest of this is ready for review regardless.

[0]: https://sashiko.dev/#/patchset/20260503-x86-init-cleanup-v2-0-bb690bd2477c%40google.com

Cheers,
Brendan

#13 lore raw
In reply to Brendan Jackman

Oh, and these seems low-ish risk. But let's plan on it being material for the next -rc1. I'd also appreciate as always if you could dig up another person or two to review them. Both of our employers pay a kernel developer or two and I'm sure at least one or two of them want to be helpful.