Skip to contentSkip to conversation

mm/debug: use valid physical memory for pmd/pud tests

PATCH Superseded Frank van der Linden 2 participants 5 messages Jan 5, 2023 – Jan 9, 2023 Memory management

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/debug: use valid physical memory for pmd/pud tests

The page table debug tests need a physical address to validate low-level page table manipulation with. The memory at this address is not actually touched, it just encoded in the page table entries at various levels during the tests only.

Since the memory is not used, the code just picks the physical address of the start_kernel symbol. This value is then truncated to get a properly aligned address that is to be used for various tests. Because of the truncation, the address might not actually exist, or might not describe a complete huge page. That's not a problem for most tests, but the arch-specific code may check for attribute validity and consistency. The x86 version of {pud,pmd}_set_huge actually validates the MTRRs for the PMD/PUD range. This may fail with an address derived from start_kernel, depending on where the kernel was loaded and what the physical memory layout of the system is. This then leads to false negatives for the {pud,pmd}_set_huge tests.

Avoid this by finding a properly aligned memory range that exists and is usable. If such a range is not found, skip the tests that needed it.

Fixes 399145f9eb6c ("mm/debug: add tests validating architecture page table helpers")
Cc Anshuman Khandual <[email protected]>
---
 mm/debug_vm_pgtable.c | 70 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 61 insertions(+), 9 deletions(-)
mm/debug_vm_pgtable.c +61 -9
index c631ade3f1d2..e9b52600904a 100644
@@ -15,6 +15,7 @@
 #include <linux/hugetlb.h>
 #include <linux/kernel.h>
 #include <linux/kconfig.h>
+#include <linux/memblock.h>
 #include <linux/mm.h>
 #include <linux/mman.h>
 #include <linux/mm_types.h>
@@ -80,6 +81,8 @@ struct pgtable_debug_args {
 	unsigned long		pmd_pfn;
 	unsigned long		pte_pfn;
 
+	phys_addr_t		fixed_alignment;
+
 	unsigned long		fixed_pgd_pfn;
 	unsigned long		fixed_p4d_pfn;
 	unsigned long		fixed_pud_pfn;
@@ -430,7 +433,8 @@ static void __init pmd_huge_tests(struct pgtable_debug_args *args)
 {
 	pmd_t pmd;
 
-	if (!arch_vmap_pmd_supported(args->page_prot))
+	if (!arch_vmap_pmd_supported(args->page_prot) ||
+	    args->fixed_alignment < PMD_SIZE)
 		return;
 
 	pr_debug("Validating PMD huge\n");
@@ -449,7 +453,8 @@ static void __init pud_huge_tests(struct pgtable_debug_args *args)
 {
 	pud_t pud;
 
-	if (!arch_vmap_pud_supported(args->page_prot))
+	if (!arch_vmap_pud_supported(args->page_prot) ||
+	    args->fixed_alignment < PUD_SIZE)
 		return;
 
 	pr_debug("Validating PUD huge\n");
@@ -1077,11 +1082,41 @@ debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order)
 	return page;
 }
 
+/*
+ * Check if a physical memory range described by <pstart, pend> contains
+ * an area that is of size psize, and aligned to the same.
+ *
+ * Don't use address 0, and check for overflow.
+ */
+static int __init phys_align_check(phys_addr_t pstart,
+	phys_addr_t pend, phys_addr_t psize, phys_addr_t *physp,
+	phys_addr_t *alignp)
+{
+	phys_addr_t aligned_start, aligned_end;
+
+	if (pstart == 0)
+		pstart = PAGE_SIZE;
+
+	aligned_start = ALIGN(pstart, psize);
+	aligned_end = aligned_start + psize;
+
+	if (aligned_end > aligned_start && aligned_end <= pend) {
+		*alignp = psize;
+		*physp = aligned_start;
+		return 1;
+	}
+
+	return 0;
+}
+
+
 static int __init init_args(struct pgtable_debug_args *args)
 {
 	struct page *page = NULL;
 	phys_addr_t phys;
 	int ret = 0;
+	u64 idx;
+	phys_addr_t pstart, pend;
 
 	/*
 	 * Initialize the debugging data.
@@ -1161,15 +1196,32 @@ static int __init init_args(struct pgtable_debug_args *args)
 	WARN_ON(!args->start_ptep);
 
 	/*
-	 * PFN for mapping at PTE level is determined from a standard kernel
-	 * text symbol. But pfns for higher page table levels are derived by
-	 * masking lower bits of this real pfn. These derived pfns might not
-	 * exist on the platform but that does not really matter as pfn_pxx()
-	 * helpers will still create appropriate entries for the test. This
-	 * helps avoid large memory block allocations to be used for mapping
-	 * at higher page table levels in some of the tests.
+	 * Find a valid physical range, preferably aligned to PUD_SIZE.
+	 * Return the address and the alignment. It doesn't need to be
+	 * allocated, it just needs to exist as usable memory. The memory
+	 * won't be touched.
+	 *
+	 * The alignment is recorded, and can be checked to see if we
+	 * can run the tests that require and actual valid physical
+	 * address range on some architectures ({pmd,pud}_huge_test
+	 * on x86).
 	 */
+
 	phys = __pa_symbol(&start_kernel);
+	args->fixed_alignment = PAGE_SIZE;
+
+	for_each_mem_range(idx, &pstart, &pend) {
+		if (phys_align_check(pstart, pend, PUD_SIZE, &phys,
+				&args->fixed_alignment))
+			break;
+
+		if (args->fixed_alignment >= PMD_SIZE)
+			continue;
+
+		(void)phys_align_check(pstart, pend, PMD_SIZE, &phys,
+				&args->fixed_alignment);
+	}
+
 	args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK);
 	args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK);
 	args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK);
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
#1 lore raw

Hi Frank,

Thanks for the patch, in principle this LGTM. Did a quick run on arm64, did not find anything problematic. Although I have some comments below.

Quoted 42 lines · Frank van der Linden

The page table debug tests need a physical address to validate low-level page table manipulation with. The memory at this address is not actually touched, it just encoded in the page table entries at various levels during the tests only.

Since the memory is not used, the code just picks the physical address of the start_kernel symbol. This value is then truncated to get a properly aligned address that is to be used for various tests. Because of the truncation, the address might not actually exist, or might not describe a complete huge page. That's not a problem for most tests, but the arch-specific code may check for attribute validity and consistency. The x86 version of {pud,pmd}_set_huge actually validates the MTRRs for the PMD/PUD range. This may fail with an address derived from start_kernel, depending on where the kernel was loaded and what the physical memory layout of the system is. This then leads to false negatives for the {pud,pmd}_set_huge tests.

Avoid this by finding a properly aligned memory range that exists and is usable. If such a range is not found, skip the tests that needed it.

Fixes 399145f9eb6c ("mm/debug: add tests validating architecture page table helpers")
Cc Anshuman Khandual <[email protected]>
---
 mm/debug_vm_pgtable.c | 70 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 61 insertions(+), 9 deletions(-)
mm/debug_vm_pgtable.c +1 -0
index c631ade3f1d2..e9b52600904a 100644
@@ -15,6 +15,7 @@
 #include <linux/hugetlb.h>
 #include <linux/kernel.h>
 #include <linux/kconfig.h>
+#include <linux/memblock.h>
 #include <linux/mm.h>
 #include <linux/mman.h>
 #include <linux/mm_types.h>

mm/debug_vm_pgtable.c struct pgtable_debug_args { · line 85

Quoted 5 lines
mm/debug_vm_pgtable.c +2 -0
@@ -80,6 +81,8 @@ struct pgtable_debug_args {
 	unsigned long		pmd_pfn;
 	unsigned long		pte_pfn;
 
+	phys_addr_t		fixed_alignment;
+

This should not be a 'phys_addr_t', as it does not really contain a physical address. Alignment value can be captured in 'unsigned long' like other elements.

mm/debug_vm_pgtable.c static void __init pmd_huge_tests(struct pgtable_debug_args *args) · line 438

Quoted 11 lines
mm/debug_vm_pgtable.c +2 -1
 	unsigned long		fixed_pgd_pfn;
 	unsigned long		fixed_p4d_pfn;
 	unsigned long		fixed_pud_pfn;
@@ -430,7 +433,8 @@ static void __init pmd_huge_tests(struct pgtable_debug_args *args)
 {
 	pmd_t pmd;
 
-	if (!arch_vmap_pmd_supported(args->page_prot))
+	if (!arch_vmap_pmd_supported(args->page_prot) ||
+	    args->fixed_alignment < PMD_SIZE)
 		return;

Small nit. Additional line not need for the conditional statement.

mm/debug_vm_pgtable.c static void __init pud_huge_tests(struct pgtable_debug_args *args) · line 458

Quoted 10 lines
mm/debug_vm_pgtable.c +2 -1
 	pr_debug("Validating PMD huge\n");
@@ -449,7 +453,8 @@ static void __init pud_huge_tests(struct pgtable_debug_args *args)
 {
 	pud_t pud;
 
-	if (!arch_vmap_pud_supported(args->page_prot))
+	if (!arch_vmap_pud_supported(args->page_prot) ||
+	    args->fixed_alignment < PUD_SIZE)
 		return;

Small nit. Additional line not needed for the conditional statement.

Quoted 10 lines
mm/debug_vm_pgtable.c +4 -0
 	pr_debug("Validating PUD huge\n");
@@ -1077,11 +1082,41 @@ debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order)
 	return page;
 }
 
+/*
+ * Check if a physical memory range described by <pstart, pend> contains
+ * an area that is of size psize, and aligned to the same.
+ *

mm/debug_vm_pgtable.c debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order) · line 1098

Quoted 10 lines
mm/debug_vm_pgtable.c +10 -0
+ * Don't use address 0, and check for overflow.
+ */
+static int __init phys_align_check(phys_addr_t pstart,
+	phys_addr_t pend, phys_addr_t psize, phys_addr_t *physp,
+	phys_addr_t *alignp)
+{
+	phys_addr_t aligned_start, aligned_end;
+
+	if (pstart == 0)
+		pstart = PAGE_SIZE;

Why ?

mm/debug_vm_pgtable.c debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order) · line 1110

Quoted 12 lines
mm/debug_vm_pgtable.c +12 -0
+
+	aligned_start = ALIGN(pstart, psize);
+	aligned_end = aligned_start + psize;
+
+	if (aligned_end > aligned_start && aligned_end <= pend) {
+		*alignp = psize;
+		*physp = aligned_start;
+		return 1;
+	}
+
+	return 0;
+}

To be more clear, this function should return a 'bool' instead

mm/debug_vm_pgtable.c debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order) · line 1119

Quoted 9 lines
mm/debug_vm_pgtable.c +4 -0
+
+
 static int __init init_args(struct pgtable_debug_args *args)
 {
 	struct page *page = NULL;
 	phys_addr_t phys;
 	int ret = 0;
+	u64 idx;
+	phys_addr_t pstart, pend;

This declaration can be merged into the previous line containing 'phys'.

Quoted 11 lines
mm/debug_vm_pgtable.c +0 -4
 	/*
 	 * Initialize the debugging data.
@@ -1161,15 +1196,32 @@ static int __init init_args(struct pgtable_debug_args *args)
 	WARN_ON(!args->start_ptep);
 
 	/*
-	 * PFN for mapping at PTE level is determined from a standard kernel
-	 * text symbol. But pfns for higher page table levels are derived by
-	 * masking lower bits of this real pfn. These derived pfns might not
-	 * exist on the platform but that does not really matter as pfn_pxx()

mm/debug_vm_pgtable.c static int __init init_args(struct pgtable_debug_args *args) · line 1205

Quoted 10 lines
mm/debug_vm_pgtable.c +7 -3
-	 * helpers will still create appropriate entries for the test. This
-	 * helps avoid large memory block allocations to be used for mapping
-	 * at higher page table levels in some of the tests.
+	 * Find a valid physical range, preferably aligned to PUD_SIZE.
+	 * Return the address and the alignment. It doesn't need to be
+	 * allocated, it just needs to exist as usable memory. The memory
+	 * won't be touched.
+	 *
+	 * The alignment is recorded, and can be checked to see if we
+	 * can run the tests that require and actual valid physical

s/and/an ?

mm/debug_vm_pgtable.c static int __init init_args(struct pgtable_debug_args *args) · line 1210

Quoted 5 lines
mm/debug_vm_pgtable.c +3 -0
+	 * address range on some architectures ({pmd,pud}_huge_test
+	 * on x86).
 	 */
+
 	phys = __pa_symbol(&start_kernel);

This original 'phys' will still be used as fallback, in case the below attempt does not find a physical address with required alignments i.e [PUD|PMD]_SIZE ?

mm/debug_vm_pgtable.c static int __init init_args(struct pgtable_debug_args *args) · line 1222

Quoted 12 lines
mm/debug_vm_pgtable.c +12 -0
+	args->fixed_alignment = PAGE_SIZE;
+
+	for_each_mem_range(idx, &pstart, &pend) {
+		if (phys_align_check(pstart, pend, PUD_SIZE, &phys,
+				&args->fixed_alignment))
+			break;
+
+		if (args->fixed_alignment >= PMD_SIZE)
+			continue;
+
+		(void)phys_align_check(pstart, pend, PMD_SIZE, &phys,
+				&args->fixed_alignment);

(void) ? Why not check the return value here ?

mm/debug_vm_pgtable.c static int __init init_args(struct pgtable_debug_args *args) · line 1227

Quoted 5 lines
mm/debug_vm_pgtable.c +2 -0
+	}
+
 	args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK);
 	args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK);
 	args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK);

This loops attempts to find a PUD_SIZE aligned address but breaks out in case it atleast finds a PMD_SIZE aligned address, while looping through available memory ranges. The entire process of finding 'phys' and 'args->fixed_alignment' should be encapsulated inside a helper that also updates 'args->fixed_pxx_pfn' elements.

- Anshuman

#2 lore raw

Hi Anshuman, thanks for looking at this.

Quoted 6 lines · Anshuman Khandual

Hi Frank,

Thanks for the patch, in principle this LGTM. Did a quick run on arm64, did not find anything problematic. Although I have some comments below.

[...]

Quoted 16 lines
Quoted 16 lines
mm/debug_vm_pgtable.c +1 -0
index c631ade3f1d2..e9b52600904a 100644
@@ -15,6 +15,7 @@
 #include <linux/hugetlb.h>
 #include <linux/kernel.h>
 #include <linux/kconfig.h>
+#include <linux/memblock.h>
 #include <linux/mm.h>
 #include <linux/mman.h>
 #include <linux/mm_types.h>
@@ -80,6 +81,8 @@ struct pgtable_debug_args {
      unsigned long           pmd_pfn;
      unsigned long           pte_pfn;
 
Quoted 6 lines

mm/debug_vm_pgtable.c struct pgtable_debug_args { · line 85

Quoted 2 lines
mm/debug_vm_pgtable.c +2 -0
+     phys_addr_t             fixed_alignment;
+

This should not be a 'phys_addr_t', as it does not really contain a physical address. Alignment value can be captured in 'unsigned long' like other elements.

True, yep.

Quoted 8 lines
Quoted 8 lines
mm/debug_vm_pgtable.c +0 -0
      unsigned long           fixed_pgd_pfn;
      unsigned long           fixed_p4d_pfn;
      unsigned long           fixed_pud_pfn;
@@ -430,7 +433,8 @@ static void __init pmd_huge_tests(struct pgtable_debug_args *args)
 {
      pmd_t pmd;
 
Quoted 7 lines

mm/debug_vm_pgtable.c static void __init pmd_huge_tests(struct pgtable_debug_args *args) · line 438

Quoted 4 lines
mm/debug_vm_pgtable.c +2 -1
-     if (!arch_vmap_pmd_supported(args->page_prot))
+     if (!arch_vmap_pmd_supported(args->page_prot) ||
+         args->fixed_alignment < PMD_SIZE)
              return;

Small nit. Additional line not need for the conditional statement.

You mean the line break in the condition? Not breaking it would push it to 90 characters (if tab=8).

Most of this file, except for a few lines, does stick to 80. I don't feel particularly strongly about this either way, though :)

Quoted 7 lines
Quoted 7 lines
mm/debug_vm_pgtable.c +0 -0
      pr_debug("Validating PMD huge\n");
@@ -449,7 +453,8 @@ static void __init pud_huge_tests(struct pgtable_debug_args *args)
 {
      pud_t pud;
 
Quoted 5 lines

mm/debug_vm_pgtable.c static void __init pud_huge_tests(struct pgtable_debug_args *args) · line 458

Quoted 4 lines
mm/debug_vm_pgtable.c +2 -1
-     if (!arch_vmap_pud_supported(args->page_prot))
+     if (!arch_vmap_pud_supported(args->page_prot) ||
+         args->fixed_alignment < PUD_SIZE)
              return;

Small nit. Additional line not needed for the conditional statement.

See above.

Quoted 7 lines
Quoted 7 lines
mm/debug_vm_pgtable.c +0 -0
      pr_debug("Validating PUD huge\n");
@@ -1077,11 +1082,41 @@ debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order)
      return page;
 }
 
Quoted 16 lines
Quoted 4 lines
mm/debug_vm_pgtable.c +4 -0
+/*
+ * Check if a physical memory range described by <pstart, pend> contains
+ * an area that is of size psize, and aligned to the same.
+ *

mm/debug_vm_pgtable.c debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order) · line 1098

Quoted 10 lines
mm/debug_vm_pgtable.c +10 -0
+ * Don't use address 0, and check for overflow.
+ */
+static int __init phys_align_check(phys_addr_t pstart,
+     phys_addr_t pend, phys_addr_t psize, phys_addr_t *physp,
+     phys_addr_t *alignp)
+{
+     phys_addr_t aligned_start, aligned_end;
+
+     if (pstart == 0)
+             pstart = PAGE_SIZE;

Why ?

Since the physical address will be used for page table tests, I think that avoiding 0 is probably a good idea. If e.g. a masking mistake crept into the code somewhere, using physical address 0 might not find it. Also, physical address 0 isn't used on x86.

Quoted 15 lines

mm/debug_vm_pgtable.c debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order) · line 1110

Quoted 13 lines
mm/debug_vm_pgtable.c +12 -0
+
+     aligned_start = ALIGN(pstart, psize);
+     aligned_end = aligned_start + psize;
+
+     if (aligned_end > aligned_start && aligned_end <= pend) {
+             *alignp = psize;
+             *physp = aligned_start;
+             return 1;
+     }
+
+     return 0;
+}

To be more clear, this function should return a 'bool' instead

That would be better, yes.

Quoted 12 lines

mm/debug_vm_pgtable.c debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order) · line 1119

Quoted 10 lines
mm/debug_vm_pgtable.c +4 -0
+
+
 static int __init init_args(struct pgtable_debug_args *args)
 {
      struct page *page = NULL;
      phys_addr_t phys;
      int ret = 0;
+     u64 idx;
+     phys_addr_t pstart, pend;

This declaration can be merged into the previous line containing 'phys'.

Sure, yes.

Quoted 16 lines
Quoted 16 lines
mm/debug_vm_pgtable.c +1 -7
      /*
       * Initialize the debugging data.
@@ -1161,15 +1196,32 @@ static int __init init_args(struct pgtable_debug_args *args)
      WARN_ON(!args->start_ptep);
 
      /*
-      * PFN for mapping at PTE level is determined from a standard kernel
-      * text symbol. But pfns for higher page table levels are derived by
-      * masking lower bits of this real pfn. These derived pfns might not
-      * exist on the platform but that does not really matter as pfn_pxx()
-      * helpers will still create appropriate entries for the test. This
-      * helps avoid large memory block allocations to be used for mapping
-      * at higher page table levels in some of the tests.
+      * Find a valid physical range, preferably aligned to PUD_SIZE.
Quoted 8 lines

mm/debug_vm_pgtable.c static int __init init_args(struct pgtable_debug_args *args) · line 1205

Quoted 6 lines
mm/debug_vm_pgtable.c +6 -0
+      * Return the address and the alignment. It doesn't need to be
+      * allocated, it just needs to exist as usable memory. The memory
+      * won't be touched.
+      *
+      * The alignment is recorded, and can be checked to see if we
+      * can run the tests that require and actual valid physical

s/and/an ?

Indeed, that's a typo.

Quoted 9 lines

mm/debug_vm_pgtable.c static int __init init_args(struct pgtable_debug_args *args) · line 1210

Quoted 6 lines
mm/debug_vm_pgtable.c +3 -0
+      * address range on some architectures ({pmd,pud}_huge_test
+      * on x86).
       */
+
      phys = __pa_symbol(&start_kernel);

This original 'phys' will still be used as fallback, in case the below attempt does not find a physical address with required alignments i.e [PUD|PMD]_SIZE ?

Right, the original value (as it is done now) is there as a fallback.

Quoted 15 lines

mm/debug_vm_pgtable.c static int __init init_args(struct pgtable_debug_args *args) · line 1222

Quoted 13 lines
mm/debug_vm_pgtable.c +12 -0
+     args->fixed_alignment = PAGE_SIZE;
+
+     for_each_mem_range(idx, &pstart, &pend) {
+             if (phys_align_check(pstart, pend, PUD_SIZE, &phys,
+                             &args->fixed_alignment))
+                     break;
+
+             if (args->fixed_alignment >= PMD_SIZE)
+                     continue;
+
+             (void)phys_align_check(pstart, pend, PMD_SIZE, &phys,
+                             &args->fixed_alignment);

(void) ? Why not check the return value here ?

If you get to that function call, you know that no aligned area has been found so far, so checking the return value won't change what you're going to do: you're going to keep going, since even if you get a PMD_SIZE aligned area, you still want to try to get a PUD_SIZE aligned area. So there's no point in checking it.

Quoted 11 lines

mm/debug_vm_pgtable.c static int __init init_args(struct pgtable_debug_args *args) · line 1227

Quoted 6 lines
mm/debug_vm_pgtable.c +2 -0
+     }
+
      args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK);
      args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK);
      args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK);

This loops attempts to find a PUD_SIZE aligned address but breaks out in case it atleast finds a PMD_SIZE aligned address, while looping through available memory ranges. The entire process of finding 'phys' and 'args->fixed_alignment' should be encapsulated inside a helper that also updates 'args->fixed_pxx_pfn' elements.

The loop keeps going until it either runs out of physical memory ranges to check, or until it finds a PUD_SIZE-aligned area. It won't break out for a PMD_SIZE-aligned area.

It could be made in to a separate function, yes, that might look a little cleaner.

Quoted 2 lines

- Anshuman

Thanks again for the comments. I see that this was added to mm-unstable by now. I can send an mm-unstable follow-up patch (though there won't be any functional changes).

- Frank

#3 lore raw
Quoted 54 lines · Frank van der Linden

Hi Anshuman, thanks for looking at this.

Quoted 6 lines · Anshuman Khandual

Hi Frank,

Thanks for the patch, in principle this LGTM. Did a quick run on arm64, did not find anything problematic. Although I have some comments below.

[...]

Quoted 16 lines
Quoted 16 lines
mm/debug_vm_pgtable.c +1 -0
index c631ade3f1d2..e9b52600904a 100644
@@ -15,6 +15,7 @@
 #include <linux/hugetlb.h>
 #include <linux/kernel.h>
 #include <linux/kconfig.h>
+#include <linux/memblock.h>
 #include <linux/mm.h>
 #include <linux/mman.h>
 #include <linux/mm_types.h>
@@ -80,6 +81,8 @@ struct pgtable_debug_args {
      unsigned long           pmd_pfn;
      unsigned long           pte_pfn;
 
Quoted 6 lines

mm/debug_vm_pgtable.c struct pgtable_debug_args { · line 85

Quoted 2 lines
mm/debug_vm_pgtable.c +2 -0
+     phys_addr_t             fixed_alignment;
+

This should not be a 'phys_addr_t', as it does not really contain a physical address. Alignment value can be captured in 'unsigned long' like other elements.

True, yep.

Quoted 8 lines
Quoted 8 lines
mm/debug_vm_pgtable.c +0 -0
      unsigned long           fixed_pgd_pfn;
      unsigned long           fixed_p4d_pfn;
      unsigned long           fixed_pud_pfn;
@@ -430,7 +433,8 @@ static void __init pmd_huge_tests(struct pgtable_debug_args *args)
 {
      pmd_t pmd;
 
Quoted 7 lines

mm/debug_vm_pgtable.c static void __init pmd_huge_tests(struct pgtable_debug_args *args) · line 438

Quoted 4 lines
mm/debug_vm_pgtable.c +2 -1
-     if (!arch_vmap_pmd_supported(args->page_prot))
+     if (!arch_vmap_pmd_supported(args->page_prot) ||
+         args->fixed_alignment < PMD_SIZE)
              return;

Small nit. Additional line not need for the conditional statement.

Quoted 5 lines

You mean the line break in the condition? Not breaking it would push it to 90 characters (if tab=8).

Most of this file, except for a few lines, does stick to 80. I don't feel particularly strongly about this either way, though :)

I guess currently the lines could extend up to 100 instead.

Quoted 40 lines
Quoted 8 lines
Quoted 8 lines
mm/debug_vm_pgtable.c +0 -0
      pr_debug("Validating PMD huge\n");
@@ -449,7 +453,8 @@ static void __init pud_huge_tests(struct pgtable_debug_args *args)
 {
      pud_t pud;
 
Quoted 5 lines

mm/debug_vm_pgtable.c static void __init pud_huge_tests(struct pgtable_debug_args *args) · line 458

Quoted 4 lines
mm/debug_vm_pgtable.c +2 -1
-     if (!arch_vmap_pud_supported(args->page_prot))
+     if (!arch_vmap_pud_supported(args->page_prot) ||
+         args->fixed_alignment < PUD_SIZE)
              return;

Small nit. Additional line not needed for the conditional statement.

See above.

Quoted 7 lines
Quoted 7 lines
mm/debug_vm_pgtable.c +0 -0
      pr_debug("Validating PUD huge\n");
@@ -1077,11 +1082,41 @@ debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order)
      return page;
 }
 
Quoted 16 lines
Quoted 4 lines
mm/debug_vm_pgtable.c +4 -0
+/*
+ * Check if a physical memory range described by <pstart, pend> contains
+ * an area that is of size psize, and aligned to the same.
+ *

mm/debug_vm_pgtable.c debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order) · line 1098

Quoted 10 lines
mm/debug_vm_pgtable.c +10 -0
+ * Don't use address 0, and check for overflow.
+ */
+static int __init phys_align_check(phys_addr_t pstart,
+     phys_addr_t pend, phys_addr_t psize, phys_addr_t *physp,
+     phys_addr_t *alignp)
+{
+     phys_addr_t aligned_start, aligned_end;
+
+     if (pstart == 0)
+             pstart = PAGE_SIZE;

Why ?

Quoted 4 lines

Since the physical address will be used for page table tests, I think that avoiding 0 is probably a good idea. If e.g. a masking mistake crept into the code somewhere, using physical address 0 might not find it. Also, physical address 0 isn't used on x86.

Make sense, but will need a small comment explaining the same.

Quoted 87 lines
Quoted 15 lines

mm/debug_vm_pgtable.c debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order) · line 1110

Quoted 13 lines
mm/debug_vm_pgtable.c +12 -0
+
+     aligned_start = ALIGN(pstart, psize);
+     aligned_end = aligned_start + psize;
+
+     if (aligned_end > aligned_start && aligned_end <= pend) {
+             *alignp = psize;
+             *physp = aligned_start;
+             return 1;
+     }
+
+     return 0;
+}

To be more clear, this function should return a 'bool' instead

That would be better, yes.

Quoted 12 lines

mm/debug_vm_pgtable.c debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order) · line 1119

Quoted 10 lines
mm/debug_vm_pgtable.c +4 -0
+
+
 static int __init init_args(struct pgtable_debug_args *args)
 {
      struct page *page = NULL;
      phys_addr_t phys;
      int ret = 0;
+     u64 idx;
+     phys_addr_t pstart, pend;

This declaration can be merged into the previous line containing 'phys'.

Sure, yes.

Quoted 16 lines
Quoted 16 lines
mm/debug_vm_pgtable.c +1 -7
      /*
       * Initialize the debugging data.
@@ -1161,15 +1196,32 @@ static int __init init_args(struct pgtable_debug_args *args)
      WARN_ON(!args->start_ptep);
 
      /*
-      * PFN for mapping at PTE level is determined from a standard kernel
-      * text symbol. But pfns for higher page table levels are derived by
-      * masking lower bits of this real pfn. These derived pfns might not
-      * exist on the platform but that does not really matter as pfn_pxx()
-      * helpers will still create appropriate entries for the test. This
-      * helps avoid large memory block allocations to be used for mapping
-      * at higher page table levels in some of the tests.
+      * Find a valid physical range, preferably aligned to PUD_SIZE.
Quoted 8 lines

mm/debug_vm_pgtable.c static int __init init_args(struct pgtable_debug_args *args) · line 1205

Quoted 6 lines
mm/debug_vm_pgtable.c +6 -0
+      * Return the address and the alignment. It doesn't need to be
+      * allocated, it just needs to exist as usable memory. The memory
+      * won't be touched.
+      *
+      * The alignment is recorded, and can be checked to see if we
+      * can run the tests that require and actual valid physical

s/and/an ?

Indeed, that's a typo.

Quoted 9 lines

mm/debug_vm_pgtable.c static int __init init_args(struct pgtable_debug_args *args) · line 1210

Quoted 6 lines
mm/debug_vm_pgtable.c +3 -0
+      * address range on some architectures ({pmd,pud}_huge_test
+      * on x86).
       */
+
      phys = __pa_symbol(&start_kernel);

This original 'phys' will still be used as fallback, in case the below attempt does not find a physical address with required alignments i.e [PUD|PMD]_SIZE ?

Right, the original value (as it is done now) is there as a fallback.

Quoted 15 lines

mm/debug_vm_pgtable.c static int __init init_args(struct pgtable_debug_args *args) · line 1222

Quoted 13 lines
mm/debug_vm_pgtable.c +12 -0
+     args->fixed_alignment = PAGE_SIZE;
+
+     for_each_mem_range(idx, &pstart, &pend) {
+             if (phys_align_check(pstart, pend, PUD_SIZE, &phys,
+                             &args->fixed_alignment))
+                     break;
+
+             if (args->fixed_alignment >= PMD_SIZE)
+                     continue;
+
+             (void)phys_align_check(pstart, pend, PMD_SIZE, &phys,
+                             &args->fixed_alignment);

(void) ? Why not check the return value here ?

Quoted 5 lines

If you get to that function call, you know that no aligned area has been found so far, so checking the return value won't change what you're going to do: you're going to keep going, since even if you get a PMD_SIZE aligned area, you still want to try to get a PUD_SIZE aligned area. So there's no point in checking it.

Okay but does a void is really necessary here even if the return value is not checked ?

Quoted 13 lines
Quoted 8 lines

mm/debug_vm_pgtable.c static int __init init_args(struct pgtable_debug_args *args) · line 1227

Quoted 7 lines
mm/debug_vm_pgtable.c +2 -0
+     }
+
      args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK);
      args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK);
      args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK);
Quoted 4 lines

This loops attempts to find a PUD_SIZE aligned address but breaks out in case it atleast finds a PMD_SIZE aligned address, while looping through available memory ranges. The entire process of finding 'phys' and 'args->fixed_alignment' should be encapsulated inside a helper that also updates 'args->fixed_pxx_pfn' elements.

Quoted 6 lines

The loop keeps going until it either runs out of physical memory ranges to check, or until it finds a PUD_SIZE-aligned area. It won't break out for a PMD_SIZE-aligned area.

It could be made in to a separate function, yes, that might look a little cleaner.

Indeed.

Quoted 6 lines
Quoted 2 lines

- Anshuman

Thanks again for the comments. I see that this was added to mm-unstable by now. I can send an mm-unstable follow-up patch (though there won't be any functional changes).

I think you could still send an updated version with the suggested changes, which can be pulled again for mm-unstable. These changes should be part of a single commit being merged, for future clarity while reading these code.

#4 lore raw

Sure, v2 sent, addressing your comments. I got rid of the return value of phys_align_check() entirely, instead just checking the recorded alignment value. It's more consistent. Added more comments, made types consistent, split off things into a function.

Thanks,

- Frank

Quoted 240 lines · Anshuman Khandual
Quoted 54 lines · Frank van der Linden

Hi Anshuman, thanks for looking at this.

Quoted 6 lines · Anshuman Khandual

Hi Frank,

Thanks for the patch, in principle this LGTM. Did a quick run on arm64, did not find anything problematic. Although I have some comments below.

[...]

Quoted 16 lines
Quoted 16 lines
mm/debug_vm_pgtable.c +1 -0
index c631ade3f1d2..e9b52600904a 100644
@@ -15,6 +15,7 @@
 #include <linux/hugetlb.h>
 #include <linux/kernel.h>
 #include <linux/kconfig.h>
+#include <linux/memblock.h>
 #include <linux/mm.h>
 #include <linux/mman.h>
 #include <linux/mm_types.h>
@@ -80,6 +81,8 @@ struct pgtable_debug_args {
      unsigned long           pmd_pfn;
      unsigned long           pte_pfn;
 
Quoted 6 lines

mm/debug_vm_pgtable.c struct pgtable_debug_args { · line 85

Quoted 2 lines
mm/debug_vm_pgtable.c +2 -0
+     phys_addr_t             fixed_alignment;
+

This should not be a 'phys_addr_t', as it does not really contain a physical address. Alignment value can be captured in 'unsigned long' like other elements.

True, yep.

Quoted 8 lines
Quoted 8 lines
mm/debug_vm_pgtable.c +0 -0
      unsigned long           fixed_pgd_pfn;
      unsigned long           fixed_p4d_pfn;
      unsigned long           fixed_pud_pfn;
@@ -430,7 +433,8 @@ static void __init pmd_huge_tests(struct pgtable_debug_args *args)
 {
      pmd_t pmd;
 
Quoted 7 lines

mm/debug_vm_pgtable.c static void __init pmd_huge_tests(struct pgtable_debug_args *args) · line 438

Quoted 4 lines
mm/debug_vm_pgtable.c +2 -1
-     if (!arch_vmap_pmd_supported(args->page_prot))
+     if (!arch_vmap_pmd_supported(args->page_prot) ||
+         args->fixed_alignment < PMD_SIZE)
              return;

Small nit. Additional line not need for the conditional statement.

Quoted 5 lines

You mean the line break in the condition? Not breaking it would push it to 90 characters (if tab=8).

Most of this file, except for a few lines, does stick to 80. I don't feel particularly strongly about this either way, though :)

I guess currently the lines could extend up to 100 instead.

Quoted 40 lines
Quoted 8 lines
Quoted 8 lines
mm/debug_vm_pgtable.c +0 -0
      pr_debug("Validating PMD huge\n");
@@ -449,7 +453,8 @@ static void __init pud_huge_tests(struct pgtable_debug_args *args)
 {
      pud_t pud;
 
Quoted 5 lines

mm/debug_vm_pgtable.c static void __init pud_huge_tests(struct pgtable_debug_args *args) · line 458

Quoted 4 lines
mm/debug_vm_pgtable.c +2 -1
-     if (!arch_vmap_pud_supported(args->page_prot))
+     if (!arch_vmap_pud_supported(args->page_prot) ||
+         args->fixed_alignment < PUD_SIZE)
              return;

Small nit. Additional line not needed for the conditional statement.

See above.

Quoted 7 lines
Quoted 7 lines
mm/debug_vm_pgtable.c +0 -0
      pr_debug("Validating PUD huge\n");
@@ -1077,11 +1082,41 @@ debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order)
      return page;
 }
 
Quoted 16 lines
Quoted 4 lines
mm/debug_vm_pgtable.c +4 -0
+/*
+ * Check if a physical memory range described by <pstart, pend> contains
+ * an area that is of size psize, and aligned to the same.
+ *

mm/debug_vm_pgtable.c debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order) · line 1098

Quoted 10 lines
mm/debug_vm_pgtable.c +10 -0
+ * Don't use address 0, and check for overflow.
+ */
+static int __init phys_align_check(phys_addr_t pstart,
+     phys_addr_t pend, phys_addr_t psize, phys_addr_t *physp,
+     phys_addr_t *alignp)
+{
+     phys_addr_t aligned_start, aligned_end;
+
+     if (pstart == 0)
+             pstart = PAGE_SIZE;

Why ?

Quoted 4 lines

Since the physical address will be used for page table tests, I think that avoiding 0 is probably a good idea. If e.g. a masking mistake crept into the code somewhere, using physical address 0 might not find it. Also, physical address 0 isn't used on x86.

Make sense, but will need a small comment explaining the same.

Quoted 87 lines
Quoted 15 lines

mm/debug_vm_pgtable.c debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order) · line 1110

Quoted 13 lines
mm/debug_vm_pgtable.c +12 -0
+
+     aligned_start = ALIGN(pstart, psize);
+     aligned_end = aligned_start + psize;
+
+     if (aligned_end > aligned_start && aligned_end <= pend) {
+             *alignp = psize;
+             *physp = aligned_start;
+             return 1;
+     }
+
+     return 0;
+}

To be more clear, this function should return a 'bool' instead

That would be better, yes.

Quoted 12 lines

mm/debug_vm_pgtable.c debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order) · line 1119

Quoted 10 lines
mm/debug_vm_pgtable.c +4 -0
+
+
 static int __init init_args(struct pgtable_debug_args *args)
 {
      struct page *page = NULL;
      phys_addr_t phys;
      int ret = 0;
+     u64 idx;
+     phys_addr_t pstart, pend;

This declaration can be merged into the previous line containing 'phys'.

Sure, yes.

Quoted 16 lines
Quoted 16 lines
mm/debug_vm_pgtable.c +1 -7
      /*
       * Initialize the debugging data.
@@ -1161,15 +1196,32 @@ static int __init init_args(struct pgtable_debug_args *args)
      WARN_ON(!args->start_ptep);
 
      /*
-      * PFN for mapping at PTE level is determined from a standard kernel
-      * text symbol. But pfns for higher page table levels are derived by
-      * masking lower bits of this real pfn. These derived pfns might not
-      * exist on the platform but that does not really matter as pfn_pxx()
-      * helpers will still create appropriate entries for the test. This
-      * helps avoid large memory block allocations to be used for mapping
-      * at higher page table levels in some of the tests.
+      * Find a valid physical range, preferably aligned to PUD_SIZE.
Quoted 8 lines

mm/debug_vm_pgtable.c static int __init init_args(struct pgtable_debug_args *args) · line 1205

Quoted 6 lines
mm/debug_vm_pgtable.c +6 -0
+      * Return the address and the alignment. It doesn't need to be
+      * allocated, it just needs to exist as usable memory. The memory
+      * won't be touched.
+      *
+      * The alignment is recorded, and can be checked to see if we
+      * can run the tests that require and actual valid physical

s/and/an ?

Indeed, that's a typo.

Quoted 9 lines

mm/debug_vm_pgtable.c static int __init init_args(struct pgtable_debug_args *args) · line 1210

Quoted 6 lines
mm/debug_vm_pgtable.c +3 -0
+      * address range on some architectures ({pmd,pud}_huge_test
+      * on x86).
       */
+
      phys = __pa_symbol(&start_kernel);

This original 'phys' will still be used as fallback, in case the below attempt does not find a physical address with required alignments i.e [PUD|PMD]_SIZE ?

Right, the original value (as it is done now) is there as a fallback.

Quoted 15 lines

mm/debug_vm_pgtable.c static int __init init_args(struct pgtable_debug_args *args) · line 1222

Quoted 13 lines
mm/debug_vm_pgtable.c +12 -0
+     args->fixed_alignment = PAGE_SIZE;
+
+     for_each_mem_range(idx, &pstart, &pend) {
+             if (phys_align_check(pstart, pend, PUD_SIZE, &phys,
+                             &args->fixed_alignment))
+                     break;
+
+             if (args->fixed_alignment >= PMD_SIZE)
+                     continue;
+
+             (void)phys_align_check(pstart, pend, PMD_SIZE, &phys,
+                             &args->fixed_alignment);

(void) ? Why not check the return value here ?

Quoted 5 lines

If you get to that function call, you know that no aligned area has been found so far, so checking the return value won't change what you're going to do: you're going to keep going, since even if you get a PMD_SIZE aligned area, you still want to try to get a PUD_SIZE aligned area. So there's no point in checking it.

Okay but does a void is really necessary here even if the return value is not checked ?

Quoted 13 lines
Quoted 8 lines

mm/debug_vm_pgtable.c static int __init init_args(struct pgtable_debug_args *args) · line 1227

Quoted 7 lines
mm/debug_vm_pgtable.c +2 -0
+     }
+
      args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK);
      args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK);
      args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK);
Quoted 4 lines

This loops attempts to find a PUD_SIZE aligned address but breaks out in case it atleast finds a PMD_SIZE aligned address, while looping through available memory ranges. The entire process of finding 'phys' and 'args->fixed_alignment' should be encapsulated inside a helper that also updates 'args->fixed_pxx_pfn' elements.

Quoted 6 lines

The loop keeps going until it either runs out of physical memory ranges to check, or until it finds a PUD_SIZE-aligned area. It won't break out for a PMD_SIZE-aligned area.

It could be made in to a separate function, yes, that might look a little cleaner.

Indeed.

Quoted 6 lines
Quoted 2 lines

- Anshuman

Thanks again for the comments. I see that this was added to mm-unstable by now. I can send an mm-unstable follow-up patch (though there won't be any functional changes).

I think you could still send an updated version with the suggested changes, which can be pulled again for mm-unstable. These changes should be part of a single commit being merged, for future clarity while reading these code.