Skip to contentSkip to conversation

fix vmalloc_to_page for huge vmap mappings

PATCH Superseded Nicholas Piggin 4 participants 12 messages Jun 23, 2019 – Jul 2, 2019

Subsystems (5)

v1 of 13 Superseded Latest is v13 →

lore
Share & tools
lore

Nicholas Piggin

author reviewer
#0 lore raw

cover fix vmalloc_to_page for huge vmap mappings

This is a change broken out from the huge vmap vmalloc series as requested. There is a little bit of dependency juggling across trees, but patches are pretty trivial. Ideally if Andrew accepts this patch and queues it up for next, then the arch patches would be merged through those trees then patch 3 gets sent by Andrew.

I've tested this with other powerpc and vmalloc patches, with code that explicitly tests vmalloc_to_page on vmalloced memory and results look fine.

Thanks,
Nick

Nicholas Piggin (3):
arm64: mm: Add p?d_large() definitions powerpc/64s: Add p?d_large definitions mm/vmalloc: fix vmalloc_to_page for huge vmap mappings

 arch/arm64/include/asm/pgtable.h             |  2 ++
 arch/powerpc/include/asm/book3s/64/pgtable.h | 24 ++++++++-----
 include/asm-generic/4level-fixup.h           |  1 +
 include/asm-generic/5level-fixup.h           |  1 +
 mm/vmalloc.c                                 | 37 +++++++++++++-------
 5 files changed, 43 insertions(+), 22 deletions(-)
Signature · 1 line
2.20.1
author reviewer
#1 lore raw

PATCH 1/3 arm64: mm: Add p?d_large() definitions

walk_page_range() is going to be allowed to walk page tables other than those of user space. For this it needs to know when it has reached a 'leaf' entry in the page tables. This information will be provided by the p?d_large() functions/macros.

For arm64, we already have p?d_sect() macros which we can reuse for p?d_large().

pud_sect() is defined as a dummy function when CONFIG_PGTABLE_LEVELS < 3 or CONFIG_ARM64_64K_PAGES is defined. However when the kernel is configured this way then architecturally it isn't allowed to have a large page that this level, and any code using these page walking macros is implicitly relying on the page size/number of levels being the same as the kernel. So it is safe to reuse this for p?d_large() as it is an architectural restriction.

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>

---
This patch is taken from arm64 but is required if this series is not build together with arm64 tree.

 arch/arm64/include/asm/pgtable.h | 2 ++
 1 file changed, 2 insertions(+)
arch/arm64/include/asm/pgtable.h +2 -0
index fca26759081a..0e973201bc16 100644
@@ -417,6 +417,7 @@ extern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
 				 PMD_TYPE_TABLE)
 #define pmd_sect(pmd)		((pmd_val(pmd) & PMD_TYPE_MASK) == \
 				 PMD_TYPE_SECT)
+#define pmd_large(pmd)		pmd_sect(pmd)
 
 #if defined(CONFIG_ARM64_64K_PAGES) || CONFIG_PGTABLE_LEVELS < 3
 #define pud_sect(pud)		(0)
@@ -499,6 +500,7 @@ static inline void pte_unmap(pte_t *pte) { }
 #define pud_none(pud)		(!pud_val(pud))
 #define pud_bad(pud)		(!(pud_val(pud) & PUD_TABLE_BIT))
 #define pud_present(pud)	pte_present(pud_pte(pud))
+#define pud_large(pud)		pud_sect(pud)
 #define pud_valid(pud)		pte_valid(pud_pte(pud))
 
 static inline void set_pud(pud_t *pudp, pud_t pud)
Signature · 1 line
2.20.1
maintainer
#2 lore raw

Re: [PATCH 1/3] arm64: mm: Add p?d_large() definitions

Hi Nick,

Quoted 4 lines · Nicholas Piggin

walk_page_range() is going to be allowed to walk page tables other than those of user space. For this it needs to know when it has reached a 'leaf' entry in the page tables. This information will be provided by the p?d_large() functions/macros.

I can't remember whether or not I asked this before, but why not call this macro p?d_leaf() if that's what it's identifying? "Large" and "huge" are usually synonymous, so I find this naming needlessly confusing based on this patch in isolation.

Will

#3 lore raw

Re: Re: [PATCH 1/3] arm64: mm: Add p?d_large() definitions

Quoted 8 lines · Will Deacon

Hi Nick,

Quoted 4 lines · Nicholas Piggin

walk_page_range() is going to be allowed to walk page tables other than those of user space. For this it needs to know when it has reached a 'leaf' entry in the page tables. This information will be provided by the p?d_large() functions/macros.

Quoted 4 lines

I can't remember whether or not I asked this before, but why not call this macro p?d_leaf() if that's what it's identifying? "Large" and "huge" are usually synonymous, so I find this naming needlessly confusing based on this patch in isolation.

Hi Will,

Quoted 2 lines · to which you

I've have thought p?d_leaf() might match better with your description above, but I'm not going to quibble on naming.

Have you changed your mind about quibbling? ;)

Steve

[1]
https://lore.kernel.org/lkml/[email protected]/

maintainer
#4 lore raw

Re: Re: [PATCH 1/3] arm64: mm: Add p?d_large() definitions

Quoted 12 lines · Steven Price
Quoted 10 lines · Will Deacon
Quoted 4 lines · Nicholas Piggin

walk_page_range() is going to be allowed to walk page tables other than those of user space. For this it needs to know when it has reached a 'leaf' entry in the page tables. This information will be provided by the p?d_large() functions/macros.

I can't remember whether or not I asked this before, but why not call this macro p?d_leaf() if that's what it's identifying? "Large" and "huge" are usually synonymous, so I find this naming needlessly confusing based on this patch in isolation.

Quoted 4 lines
Quoted 2 lines · to which you

I've have thought p?d_leaf() might match better with your description above, but I'm not going to quibble on naming.

That explains the sense of deja vu.

Quoted 1 line

Have you changed your mind about quibbling? ;)

Ha, I suppose I have! If it's not loads of effort to use p?d_leaf() instead of p?d_large, then I'd certainly prefer that.

Will

author reviewer
#5 lore raw

Re: Re: [PATCH 1/3] arm64: mm: Add p?d_large() definitions

Will Deacon's on July 1, 2019 8:15 pm:

Quoted 8 lines · Steven Price
Quoted 7 lines · Will Deacon
Quoted 6 lines · Nicholas Piggin
Quoted 4 lines

walk_page_range() is going to be allowed to walk page tables other than those of user space. For this it needs to know when it has reached a 'leaf' entry in the page tables. This information will be provided by the p?d_large() functions/macros.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

I can't remember whether or not I asked this before, but why not call this macro p?d_leaf() if that's what it's identifying? "Large" and "huge" are usually synonymous, so I find this naming needlessly confusing based on this patch in isolation.

Those page table macro names are horrible. Large, huge, leaf, wtf? They could do with a sensible renaming. But this series just follows naming that's alreay there on x86.

Thanks,
Nick

maintainer
#6 lore raw

Re: Re: [PATCH 1/3] arm64: mm: Add p?d_large() definitions

Quoted 9 lines · Nicholas Piggin

Will Deacon's on July 1, 2019 8:15 pm:

Quoted 8 lines · Steven Price
Quoted 7 lines · Will Deacon
Quoted 6 lines · Nicholas Piggin
Quoted 4 lines

walk_page_range() is going to be allowed to walk page tables other than those of user space. For this it needs to know when it has reached a 'leaf' entry in the page tables. This information will be provided by the p?d_large() functions/macros.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

I can't remember whether or not I asked this before, but why not call this macro p?d_leaf() if that's what it's identifying? "Large" and "huge" are usually synonymous, so I find this naming needlessly confusing based on this patch in isolation.

Those page table macro names are horrible. Large, huge, leaf, wtf? They could do with a sensible renaming. But this series just follows naming that's alreay there on x86.

I realise that, and I wasn't meaning to have a go at you. Just wanted to make my opinion clear by having a moan :)

Will

author reviewer
#7 lore raw

PATCH 2/3 powerpc/64s: Add p?d_large definitions

In reply to Nicholas Piggin

The subsequent patch to fix vmalloc_to_page with huge vmap requires HUGE_VMAP archs to provide p?d_large definitions for the non-pgd page table levels they support.

---
Upstream powerpc code does not enable HUGE_VMAP, but the powerpc next tree has patches, so this patch is required to fix dependency between this series and powerpc tree in linux-next.

 arch/powerpc/include/asm/book3s/64/pgtable.h | 24 ++++++++++++--------
 1 file changed, 15 insertions(+), 9 deletions(-)
arch/powerpc/include/asm/book3s/64/pgtable.h +15 -9
index ccf00a8b98c6..c19c8396a1bd 100644
@@ -915,6 +915,11 @@ static inline int pud_present(pud_t pud)
 	return !!(pud_raw(pud) & cpu_to_be64(_PAGE_PRESENT));
 }
 
+static inline int pud_large(pud_t pud)
+{
+	return !!(pud_raw(pud) & cpu_to_be64(_PAGE_PTE));
+}
+
 extern struct page *pud_page(pud_t pud);
 extern struct page *pmd_page(pmd_t pmd);
 static inline pte_t pud_pte(pud_t pud)
@@ -958,6 +963,11 @@ static inline int pgd_present(pgd_t pgd)
 	return !!(pgd_raw(pgd) & cpu_to_be64(_PAGE_PRESENT));
 }
 
+static inline int pgd_large(pgd_t pgd)
+{
+	return !!(pgd_raw(pgd) & cpu_to_be64(_PAGE_PTE));
+}
+
 static inline pte_t pgd_pte(pgd_t pgd)
 {
 	return __pte_raw(pgd_raw(pgd));
@@ -1083,6 +1093,11 @@ static inline pte_t *pmdp_ptep(pmd_t *pmd)
 #define pmd_mk_savedwrite(pmd)	pte_pmd(pte_mk_savedwrite(pmd_pte(pmd)))
 #define pmd_clear_savedwrite(pmd)	pte_pmd(pte_clear_savedwrite(pmd_pte(pmd)))
 
+static inline int pmd_large(pmd_t pmd)
+{
+	return !!(pmd_raw(pmd) & cpu_to_be64(_PAGE_PTE));
+}
+
 #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
 #define pmd_soft_dirty(pmd)    pte_soft_dirty(pmd_pte(pmd))
 #define pmd_mksoft_dirty(pmd)  pte_pmd(pte_mksoft_dirty(pmd_pte(pmd)))
@@ -1151,15 +1166,6 @@ pmd_hugepage_update(struct mm_struct *mm, unsigned long addr, pmd_t *pmdp,
 	return hash__pmd_hugepage_update(mm, addr, pmdp, clr, set);
 }
 
-/*
- * returns true for pmd migration entries, THP, devmap, hugetlb
- * But compile time dependent on THP config
- */
-static inline int pmd_large(pmd_t pmd)
-{
-	return !!(pmd_raw(pmd) & cpu_to_be64(_PAGE_PTE));
-}
-
 static inline pmd_t pmd_mknotpresent(pmd_t pmd)
 {
 	return __pmd(pmd_val(pmd) & ~_PAGE_PRESENT);
Signature · 1 line
2.20.1
author reviewer
#8 lore raw

PATCH 3/3 mm/vmalloc: fix vmalloc_to_page for huge vmap mappings

In reply to Nicholas Piggin

vmalloc_to_page returns NULL for addresses mapped by larger pages[*]. Whether or not a vmap is huge depends on the architecture details, alignments, boot options, etc., which the caller can not be expected to know. Therefore HUGE_VMAP is a regression for vmalloc_to_page.

This change teaches vmalloc_to_page about larger pages, and returns the struct page that corresponds to the offset within the large page. This makes the API agnostic to mapping implementation details.

[*] As explained by commit 029c54b095995 ("mm/vmalloc.c: huge-vmap:
fail gracefully on unexpected huge vmap mappings")

---
 include/asm-generic/4level-fixup.h |  1 +
 include/asm-generic/5level-fixup.h |  1 +
 mm/vmalloc.c                       | 37 +++++++++++++++++++-----------
 3 files changed, 26 insertions(+), 13 deletions(-)
include/asm-generic/4level-fixup.h +1 -0
index e3667c9a33a5..3cc65a4dd093 100644
@@ -20,6 +20,7 @@
 #define pud_none(pud)			0
 #define pud_bad(pud)			0
 #define pud_present(pud)		1
+#define pud_large(pud)			0
 #define pud_ERROR(pud)			do { } while (0)
 #define pud_clear(pud)			pgd_clear(pud)
 #define pud_val(pud)			pgd_val(pud)
include/asm-generic/5level-fixup.h +1 -0
index bb6cb347018c..c4377db09a4f 100644
@@ -22,6 +22,7 @@
 #define p4d_none(p4d)			0
 #define p4d_bad(p4d)			0
 #define p4d_present(p4d)		1
+#define p4d_large(p4d)			0
 #define p4d_ERROR(p4d)			do { } while (0)
 #define p4d_clear(p4d)			pgd_clear(p4d)
 #define p4d_val(p4d)			pgd_val(p4d)
mm/vmalloc.c +24 -13
index 4c9e150e5ad3..4be98f700862 100644
@@ -36,6 +36,7 @@
 #include <linux/rbtree_augmented.h>
 
 #include <linux/uaccess.h>
+#include <asm/pgtable.h>
 #include <asm/tlbflush.h>
 #include <asm/shmparam.h>
 
@@ -284,26 +285,36 @@ struct page *vmalloc_to_page(const void *vmalloc_addr)
 
 	if (pgd_none(*pgd))
 		return NULL;
+
 	p4d = p4d_offset(pgd, addr);
 	if (p4d_none(*p4d))
 		return NULL;
-	pud = pud_offset(p4d, addr);
+	if (WARN_ON_ONCE(p4d_bad(*p4d)))
+		return NULL;
+#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
+	if (p4d_large(*p4d))
+		return p4d_page(*p4d) + ((addr & ~P4D_MASK) >> PAGE_SHIFT);
+#endif
 
-	/*
-	 * Don't dereference bad PUD or PMD (below) entries. This will also
-	 * identify huge mappings, which we may encounter on architectures
-	 * that define CONFIG_HAVE_ARCH_HUGE_VMAP=y. Such regions will be
-	 * identified as vmalloc addresses by is_vmalloc_addr(), but are
-	 * not [unambiguously] associated with a struct page, so there is
-	 * no correct value to return for them.
-	 */
-	WARN_ON_ONCE(pud_bad(*pud));
-	if (pud_none(*pud) || pud_bad(*pud))
+	pud = pud_offset(p4d, addr);
+	if (pud_none(*pud))
+		return NULL;
+	if (WARN_ON_ONCE(pud_bad(*pud)))
 		return NULL;
+#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
+	if (pud_large(*pud))
+		return pud_page(*pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
+#endif
+
 	pmd = pmd_offset(pud, addr);
-	WARN_ON_ONCE(pmd_bad(*pmd));
-	if (pmd_none(*pmd) || pmd_bad(*pmd))
+	if (pmd_none(*pmd))
+		return NULL;
+	if (WARN_ON_ONCE(pmd_bad(*pmd)))
 		return NULL;
+#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
+	if (pmd_large(*pmd))
+		return pmd_page(*pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
+#endif
 
 	ptep = pte_offset_map(pmd, addr);
 	pte = *ptep;
Signature · 1 line
2.20.1
#9 lore raw

Re: [PATCH 3/3] mm/vmalloc: fix vmalloc_to_page for huge vmap mappings

Quoted 56 lines · Nicholas Piggin

vmalloc_to_page returns NULL for addresses mapped by larger pages[*]. Whether or not a vmap is huge depends on the architecture details, alignments, boot options, etc., which the caller can not be expected to know. Therefore HUGE_VMAP is a regression for vmalloc_to_page.

This change teaches vmalloc_to_page about larger pages, and returns the struct page that corresponds to the offset within the large page. This makes the API agnostic to mapping implementation details.

[*] As explained by commit 029c54b095995 ("mm/vmalloc.c: huge-vmap:
fail gracefully on unexpected huge vmap mappings")

---
 include/asm-generic/4level-fixup.h |  1 +
 include/asm-generic/5level-fixup.h |  1 +
 mm/vmalloc.c                       | 37 +++++++++++++++++++-----------
 3 files changed, 26 insertions(+), 13 deletions(-)
include/asm-generic/4level-fixup.h +1 -0
index e3667c9a33a5..3cc65a4dd093 100644
@@ -20,6 +20,7 @@
 #define pud_none(pud)			0
 #define pud_bad(pud)			0
 #define pud_present(pud)		1
+#define pud_large(pud)			0
 #define pud_ERROR(pud)			do { } while (0)
 #define pud_clear(pud)			pgd_clear(pud)
 #define pud_val(pud)			pgd_val(pud)
include/asm-generic/5level-fixup.h +1 -0
index bb6cb347018c..c4377db09a4f 100644
@@ -22,6 +22,7 @@
 #define p4d_none(p4d)			0
 #define p4d_bad(p4d)			0
 #define p4d_present(p4d)		1
+#define p4d_large(p4d)			0
 #define p4d_ERROR(p4d)			do { } while (0)
 #define p4d_clear(p4d)			pgd_clear(p4d)
 #define p4d_val(p4d)			pgd_val(p4d)
mm/vmalloc.c +1 -0
index 4c9e150e5ad3..4be98f700862 100644
@@ -36,6 +36,7 @@
 #include <linux/rbtree_augmented.h>
 
 #include <linux/uaccess.h>
+#include <asm/pgtable.h>
 #include <asm/tlbflush.h>
 #include <asm/shmparam.h>
 

mm/vmalloc.c struct page *vmalloc_to_page(const void *vmalloc_addr) · line 293

Quoted 10 lines
mm/vmalloc.c +3 -1
@@ -284,26 +285,36 @@ struct page *vmalloc_to_page(const void *vmalloc_addr)
 
 	if (pgd_none(*pgd))
 		return NULL;
+
 	p4d = p4d_offset(pgd, addr);
 	if (p4d_none(*p4d))
 		return NULL;
-	pud = pud_offset(p4d, addr);
+	if (WARN_ON_ONCE(p4d_bad(*p4d)))
+		return NULL;

The warning here is a required addition but it needs to be moved after p4d_large() check. Please see the next comment below.

Quoted 15 lines
mm/vmalloc.c +4 -10
+#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
+	if (p4d_large(*p4d))
+		return p4d_page(*p4d) + ((addr & ~P4D_MASK) >> PAGE_SHIFT);
+#endif
 
-	/*
-	 * Don't dereference bad PUD or PMD (below) entries. This will also
-	 * identify huge mappings, which we may encounter on architectures
-	 * that define CONFIG_HAVE_ARCH_HUGE_VMAP=y. Such regions will be
-	 * identified as vmalloc addresses by is_vmalloc_addr(), but are
-	 * not [unambiguously] associated with a struct page, so there is
-	 * no correct value to return for them.
-	 */
-	WARN_ON_ONCE(pud_bad(*pud));
-	if (pud_none(*pud) || pud_bad(*pud))

mm/vmalloc.c struct page *vmalloc_to_page(const void *vmalloc_addr) · line 308

Quoted 10 lines
mm/vmalloc.c +9 -0
+	pud = pud_offset(p4d, addr);
+	if (pud_none(*pud))
+		return NULL;
+	if (WARN_ON_ONCE(pud_bad(*pud)))
 		return NULL;
+#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
+	if (pud_large(*pud))
+		return pud_page(*pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
+#endif
+

pud_bad() on arm64 returns true when the PUD does not point to a next page table page implying the fact that it might be a large/huge entry. I am not sure if the semantics holds good for other architectures too. But on arm64 if pud_large() is true, then pud_bad() will be true as well. So pud_bad() check must happen after pud_large() check. So the sequence here should be

1. pud_none() --> Nothing is in here, return NULL
2. pud_large() --> Return offset page address from the huge page mapping
3. pud_bad() --> Return NULL as there is no more page table level left

Checking pud_bad() first can return NULL for a valid huge mapping.

mm/vmalloc.c struct page *vmalloc_to_page(const void *vmalloc_addr) · line 317

Quoted 11 lines
mm/vmalloc.c +7 -2
 	pmd = pmd_offset(pud, addr);
-	WARN_ON_ONCE(pmd_bad(*pmd));
-	if (pmd_none(*pmd) || pmd_bad(*pmd))
+	if (pmd_none(*pmd))
+		return NULL;
+	if (WARN_ON_ONCE(pmd_bad(*pmd)))
 		return NULL;
+#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
+	if (pmd_large(*pmd))
+		return pmd_page(*pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
+#endif

Ditto.

I see that your previous proposal had this right which got changed in this manner after my comments. Sorry about it.

It was recently when I learned (correctly) that expected semantics of pxx_bad() is that - It does not point to the next page table page. Hence I wonder why is this not renamed as pxx_table() instead to make it absolutely clear.

author reviewer
#10 lore raw

Re: [PATCH 3/3] mm/vmalloc: fix vmalloc_to_page for huge vmap mappings

Anshuman Khandual's on June 24, 2019 4:52 pm:

Quoted 125 lines · Nicholas Piggin
Quoted 56 lines

vmalloc_to_page returns NULL for addresses mapped by larger pages[*]. Whether or not a vmap is huge depends on the architecture details, alignments, boot options, etc., which the caller can not be expected to know. Therefore HUGE_VMAP is a regression for vmalloc_to_page.

This change teaches vmalloc_to_page about larger pages, and returns the struct page that corresponds to the offset within the large page. This makes the API agnostic to mapping implementation details.

[*] As explained by commit 029c54b095995 ("mm/vmalloc.c: huge-vmap:
fail gracefully on unexpected huge vmap mappings")

---
 include/asm-generic/4level-fixup.h |  1 +
 include/asm-generic/5level-fixup.h |  1 +
 mm/vmalloc.c                       | 37 +++++++++++++++++++-----------
 3 files changed, 26 insertions(+), 13 deletions(-)
include/asm-generic/4level-fixup.h +1 -0
index e3667c9a33a5..3cc65a4dd093 100644
@@ -20,6 +20,7 @@
 #define pud_none(pud)			0
 #define pud_bad(pud)			0
 #define pud_present(pud)		1
+#define pud_large(pud)			0
 #define pud_ERROR(pud)			do { } while (0)
 #define pud_clear(pud)			pgd_clear(pud)
 #define pud_val(pud)			pgd_val(pud)
include/asm-generic/5level-fixup.h +1 -0
index bb6cb347018c..c4377db09a4f 100644
@@ -22,6 +22,7 @@
 #define p4d_none(p4d)			0
 #define p4d_bad(p4d)			0
 #define p4d_present(p4d)		1
+#define p4d_large(p4d)			0
 #define p4d_ERROR(p4d)			do { } while (0)
 #define p4d_clear(p4d)			pgd_clear(p4d)
 #define p4d_val(p4d)			pgd_val(p4d)
mm/vmalloc.c +1 -0
index 4c9e150e5ad3..4be98f700862 100644
@@ -36,6 +36,7 @@
 #include <linux/rbtree_augmented.h>
 
 #include <linux/uaccess.h>
+#include <asm/pgtable.h>
 #include <asm/tlbflush.h>
 #include <asm/shmparam.h>
 

mm/vmalloc.c struct page *vmalloc_to_page(const void *vmalloc_addr) · line 293

Quoted 10 lines
mm/vmalloc.c +3 -1
@@ -284,26 +285,36 @@ struct page *vmalloc_to_page(const void *vmalloc_addr)
 
 	if (pgd_none(*pgd))
 		return NULL;
+
 	p4d = p4d_offset(pgd, addr);
 	if (p4d_none(*p4d))
 		return NULL;
-	pud = pud_offset(p4d, addr);
+	if (WARN_ON_ONCE(p4d_bad(*p4d)))
+		return NULL;

The warning here is a required addition but it needs to be moved after p4d_large() check. Please see the next comment below.

Quoted 15 lines
mm/vmalloc.c +4 -10
+#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
+	if (p4d_large(*p4d))
+		return p4d_page(*p4d) + ((addr & ~P4D_MASK) >> PAGE_SHIFT);
+#endif
 
-	/*
-	 * Don't dereference bad PUD or PMD (below) entries. This will also
-	 * identify huge mappings, which we may encounter on architectures
-	 * that define CONFIG_HAVE_ARCH_HUGE_VMAP=y. Such regions will be
-	 * identified as vmalloc addresses by is_vmalloc_addr(), but are
-	 * not [unambiguously] associated with a struct page, so there is
-	 * no correct value to return for them.
-	 */
-	WARN_ON_ONCE(pud_bad(*pud));
-	if (pud_none(*pud) || pud_bad(*pud))

mm/vmalloc.c struct page *vmalloc_to_page(const void *vmalloc_addr) · line 308

Quoted 10 lines
mm/vmalloc.c +9 -0
+	pud = pud_offset(p4d, addr);
+	if (pud_none(*pud))
+		return NULL;
+	if (WARN_ON_ONCE(pud_bad(*pud)))
 		return NULL;
+#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
+	if (pud_large(*pud))
+		return pud_page(*pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
+#endif
+

pud_bad() on arm64 returns true when the PUD does not point to a next page table page implying the fact that it might be a large/huge entry. I am not sure if the semantics holds good for other architectures too. But on arm64 if pud_large() is true, then pud_bad() will be true as well. So pud_bad() check must happen after pud_large() check. So the sequence here should be

1. pud_none() --> Nothing is in here, return NULL
2. pud_large() --> Return offset page address from the huge page mapping
3. pud_bad() --> Return NULL as there is no more page table level left

Checking pud_bad() first can return NULL for a valid huge mapping.

mm/vmalloc.c struct page *vmalloc_to_page(const void *vmalloc_addr) · line 317

Quoted 11 lines
mm/vmalloc.c +7 -2
 	pmd = pmd_offset(pud, addr);
-	WARN_ON_ONCE(pmd_bad(*pmd));
-	if (pmd_none(*pmd) || pmd_bad(*pmd))
+	if (pmd_none(*pmd))
+		return NULL;
+	if (WARN_ON_ONCE(pmd_bad(*pmd)))
 		return NULL;
+#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
+	if (pmd_large(*pmd))
+		return pmd_page(*pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
+#endif

Ditto.

Quoted 7 lines

I see that your previous proposal had this right which got changed in this manner after my comments. Sorry about it.

It was recently when I learned (correctly) that expected semantics of pxx_bad() is that - It does not point to the next page table page. Hence I wonder why is this not renamed as pxx_table() instead to make it absolutely clear.

Okay, I'll change it and resend. It worked okay on powerpc but it looks like the usual precedent is testing for large before bad so we will have to go with that.

Thanks,
Nick

#11 lore raw
In reply to Nicholas Piggin

Hello Nicholas,

Quoted 2 lines · Nicholas Piggin

This is a change broken out from the huge vmap vmalloc series as requested. There is a little bit of dependency juggling across

Thanks for splitting up the previous series and sending this one out.

Quoted 3 lines

trees, but patches are pretty trivial. Ideally if Andrew accepts this patch and queues it up for next, then the arch patches would be merged through those trees then patch 3 gets sent by Andrew.

Fair enough.

Quoted 4 lines

I've tested this with other powerpc and vmalloc patches, with code that explicitly tests vmalloc_to_page on vmalloced memory and results look fine.

- Anshuman