提交 d3efb4d3 编写于 作者: N Nicholas Piggin 提交者: Yang Yingliang

mm/vmalloc: fix vmalloc_to_page for huge vmap mappings

ascend inclusion
category: feature
bugzilla: NA
CVE: NA

https://lwn.net/ml/linux-kernel/20200825145753.529284-2-npiggin@gmail.com/
--------------

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 029c54b0 ("mm/vmalloc.c: huge-vmap:
    fail gracefully on unexpected huge vmap mappings")
Signed-off-by: NNicholas Piggin <npiggin@gmail.com>
Signed-off-by: NRui Xiang <rui.xiang@huawei.com>
Reviewed-by: NDing Tianhong <dingtianhong@huawei.com>
Reviewed-by: NZefan Li <lizefan@huawei.com>
Signed-off-by: NYang Yingliang <yangyingliang@huawei.com>
上级 d82f43f9
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <asm/tlbflush.h> #include <asm/tlbflush.h>
#include <asm/shmparam.h> #include <asm/shmparam.h>
#include <asm/pgtable.h>
#include "internal.h" #include "internal.h"
...@@ -289,7 +290,9 @@ int is_vmalloc_or_module_addr(const void *x) ...@@ -289,7 +290,9 @@ int is_vmalloc_or_module_addr(const void *x)
} }
/* /*
* Walk a vmap address to the struct page it maps. * Walk a vmap address to the struct page it maps. Huge vmap mappings will
* return the tail page that corresponds to the base page address, which
* matches small vmap mappings.
*/ */
struct page *vmalloc_to_page(const void *vmalloc_addr) struct page *vmalloc_to_page(const void *vmalloc_addr)
{ {
...@@ -309,25 +312,33 @@ struct page *vmalloc_to_page(const void *vmalloc_addr) ...@@ -309,25 +312,33 @@ struct page *vmalloc_to_page(const void *vmalloc_addr)
if (pgd_none(*pgd)) if (pgd_none(*pgd))
return NULL; return NULL;
if (WARN_ON_ONCE(pgd_leaf(*pgd)))
return NULL; /* XXX: no allowance for huge pgd */
if (WARN_ON_ONCE(pgd_bad(*pgd)))
return NULL;
p4d = p4d_offset(pgd, addr); p4d = p4d_offset(pgd, addr);
if (p4d_none(*p4d)) if (p4d_none(*p4d))
return NULL; return NULL;
pud = pud_offset(p4d, addr); if (p4d_leaf(*p4d))
return p4d_page(*p4d) + ((addr & ~P4D_MASK) >> PAGE_SHIFT);
if (WARN_ON_ONCE(p4d_bad(*p4d)))
return NULL;
/* pud = pud_offset(p4d, addr);
* Don't dereference bad PUD or PMD (below) entries. This will also if (pud_none(*pud))
* identify huge mappings, which we may encounter on architectures return NULL;
* that define CONFIG_HAVE_ARCH_HUGE_VMAP=y. Such regions will be if (pud_leaf(*pud))
* identified as vmalloc addresses by is_vmalloc_addr(), but are return pud_page(*pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
* not [unambiguously] associated with a struct page, so there is if (WARN_ON_ONCE(pud_bad(*pud)))
* no correct value to return for them.
*/
WARN_ON_ONCE(pud_bad(*pud));
if (pud_none(*pud) || pud_bad(*pud))
return NULL; return NULL;
pmd = pmd_offset(pud, addr); pmd = pmd_offset(pud, addr);
WARN_ON_ONCE(pmd_bad(*pmd)); if (pmd_none(*pmd))
if (pmd_none(*pmd) || pmd_bad(*pmd)) return NULL;
if (pmd_leaf(*pmd))
return pmd_page(*pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
if (WARN_ON_ONCE(pmd_bad(*pmd)))
return NULL; return NULL;
ptep = pte_offset_map(pmd, addr); ptep = pte_offset_map(pmd, addr);
...@@ -335,6 +346,7 @@ struct page *vmalloc_to_page(const void *vmalloc_addr) ...@@ -335,6 +346,7 @@ struct page *vmalloc_to_page(const void *vmalloc_addr)
if (pte_present(pte)) if (pte_present(pte))
page = pte_page(pte); page = pte_page(pte);
pte_unmap(ptep); pte_unmap(ptep);
return page; return page;
} }
EXPORT_SYMBOL(vmalloc_to_page); EXPORT_SYMBOL(vmalloc_to_page);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册