Commit 3ed3a4f0 authored by Kirill A. Shutemov's avatar Kirill A. Shutemov Committed by Linus Torvalds

mm: cleanup *pte_alloc* interfaces

There are few things about *pte_alloc*() helpers worth cleaning up:

 - 'vma' argument is unused, let's drop it;

 - most __pte_alloc() callers do speculative check for pmd_none(),
   before taking ptl: let's introduce pte_alloc() macro which does
   the check.

   The only direct user of __pte_alloc left is userfaultfd, which has
   different expectation about atomicity wrt pmd.

 - pte_alloc_map() and pte_alloc_map_lock() are redefined using
   pte_alloc().

[sudeep.holla@arm.com: fix build for arm64 hugetlbpage]
[sfr@canb.auug.org.au: fix arch/arm/mm/mmu.c some more]
Signed-off-by: default avatarKirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: Dave Hansen <dave.hansen@intel.com>
Signed-off-by: default avatarSudeep Holla <sudeep.holla@arm.com>
Acked-by: default avatarKirill A. Shutemov <kirill.shutemov@linux.intel.com>
Signed-off-by: default avatarStephen Rothwell <sfr@canb.auug.org.au>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 5057dcd0
...@@ -732,7 +732,7 @@ static void *__init late_alloc(unsigned long sz) ...@@ -732,7 +732,7 @@ static void *__init late_alloc(unsigned long sz)
return ptr; return ptr;
} }
static pte_t * __init pte_alloc(pmd_t *pmd, unsigned long addr, static pte_t * __init arm_pte_alloc(pmd_t *pmd, unsigned long addr,
unsigned long prot, unsigned long prot,
void *(*alloc)(unsigned long sz)) void *(*alloc)(unsigned long sz))
{ {
...@@ -747,7 +747,7 @@ static pte_t * __init pte_alloc(pmd_t *pmd, unsigned long addr, ...@@ -747,7 +747,7 @@ static pte_t * __init pte_alloc(pmd_t *pmd, unsigned long addr,
static pte_t * __init early_pte_alloc(pmd_t *pmd, unsigned long addr, static pte_t * __init early_pte_alloc(pmd_t *pmd, unsigned long addr,
unsigned long prot) unsigned long prot)
{ {
return pte_alloc(pmd, addr, prot, early_alloc); return arm_pte_alloc(pmd, addr, prot, early_alloc);
} }
static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr, static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
...@@ -756,7 +756,7 @@ static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr, ...@@ -756,7 +756,7 @@ static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
void *(*alloc)(unsigned long sz), void *(*alloc)(unsigned long sz),
bool ng) bool ng)
{ {
pte_t *pte = pte_alloc(pmd, addr, type->prot_l1, alloc); pte_t *pte = arm_pte_alloc(pmd, addr, type->prot_l1, alloc);
do { do {
set_pte_ext(pte, pfn_pte(pfn, __pgprot(type->prot_pte)), set_pte_ext(pte, pfn_pte(pfn, __pgprot(type->prot_pte)),
ng ? PTE_EXT_NG : 0); ng ? PTE_EXT_NG : 0);
......
...@@ -80,7 +80,7 @@ pgd_t *pgd_alloc(struct mm_struct *mm) ...@@ -80,7 +80,7 @@ pgd_t *pgd_alloc(struct mm_struct *mm)
if (!new_pmd) if (!new_pmd)
goto no_pmd; goto no_pmd;
new_pte = pte_alloc_map(mm, NULL, new_pmd, 0); new_pte = pte_alloc_map(mm, new_pmd, 0);
if (!new_pte) if (!new_pte)
goto no_pte; goto no_pte;
......
...@@ -124,7 +124,7 @@ pte_t *huge_pte_alloc(struct mm_struct *mm, ...@@ -124,7 +124,7 @@ pte_t *huge_pte_alloc(struct mm_struct *mm,
* will be no pte_unmap() to correspond with this * will be no pte_unmap() to correspond with this
* pte_alloc_map(). * pte_alloc_map().
*/ */
pte = pte_alloc_map(mm, NULL, pmd, addr); pte = pte_alloc_map(mm, pmd, addr);
} else if (sz == PMD_SIZE) { } else if (sz == PMD_SIZE) {
if (IS_ENABLED(CONFIG_ARCH_WANT_HUGE_PMD_SHARE) && if (IS_ENABLED(CONFIG_ARCH_WANT_HUGE_PMD_SHARE) &&
pud_none(*pud)) pud_none(*pud))
......
...@@ -38,7 +38,7 @@ huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz) ...@@ -38,7 +38,7 @@ huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz)
if (pud) { if (pud) {
pmd = pmd_alloc(mm, pud, taddr); pmd = pmd_alloc(mm, pud, taddr);
if (pmd) if (pmd)
pte = pte_alloc_map(mm, NULL, pmd, taddr); pte = pte_alloc_map(mm, pmd, taddr);
} }
return pte; return pte;
} }
......
...@@ -67,7 +67,7 @@ pte_t *huge_pte_alloc(struct mm_struct *mm, ...@@ -67,7 +67,7 @@ pte_t *huge_pte_alloc(struct mm_struct *mm,
pgd = pgd_offset(mm, addr); pgd = pgd_offset(mm, addr);
pud = pud_offset(pgd, addr); pud = pud_offset(pgd, addr);
pmd = pmd_offset(pud, addr); pmd = pmd_offset(pud, addr);
pte = pte_alloc_map(mm, NULL, pmd, addr); pte = pte_alloc_map(mm, pmd, addr);
pgd->pgd &= ~_PAGE_SZ_MASK; pgd->pgd &= ~_PAGE_SZ_MASK;
pgd->pgd |= _PAGE_SZHUGE; pgd->pgd |= _PAGE_SZHUGE;
......
...@@ -63,7 +63,7 @@ pte_t *huge_pte_alloc(struct mm_struct *mm, ...@@ -63,7 +63,7 @@ pte_t *huge_pte_alloc(struct mm_struct *mm,
if (pud) { if (pud) {
pmd = pmd_alloc(mm, pud, addr); pmd = pmd_alloc(mm, pud, addr);
if (pmd) if (pmd)
pte = pte_alloc_map(mm, NULL, pmd, addr); pte = pte_alloc_map(mm, pmd, addr);
} }
return pte; return pte;
} }
......
...@@ -35,7 +35,7 @@ pte_t *huge_pte_alloc(struct mm_struct *mm, ...@@ -35,7 +35,7 @@ pte_t *huge_pte_alloc(struct mm_struct *mm,
if (pud) { if (pud) {
pmd = pmd_alloc(mm, pud, addr); pmd = pmd_alloc(mm, pud, addr);
if (pmd) if (pmd)
pte = pte_alloc_map(mm, NULL, pmd, addr); pte = pte_alloc_map(mm, pmd, addr);
} }
} }
......
...@@ -146,7 +146,7 @@ pte_t *huge_pte_alloc(struct mm_struct *mm, ...@@ -146,7 +146,7 @@ pte_t *huge_pte_alloc(struct mm_struct *mm,
if (pud) { if (pud) {
pmd = pmd_alloc(mm, pud, addr); pmd = pmd_alloc(mm, pud, addr);
if (pmd) if (pmd)
pte = pte_alloc_map(mm, NULL, pmd, addr); pte = pte_alloc_map(mm, pmd, addr);
} }
return pte; return pte;
} }
......
...@@ -77,7 +77,7 @@ pte_t *huge_pte_alloc(struct mm_struct *mm, ...@@ -77,7 +77,7 @@ pte_t *huge_pte_alloc(struct mm_struct *mm,
else { else {
if (sz != PAGE_SIZE << huge_shift[HUGE_SHIFT_PAGE]) if (sz != PAGE_SIZE << huge_shift[HUGE_SHIFT_PAGE])
panic("Unexpected page size %#lx\n", sz); panic("Unexpected page size %#lx\n", sz);
return pte_alloc_map(mm, NULL, pmd, addr); return pte_alloc_map(mm, pmd, addr);
} }
} }
#else #else
......
...@@ -31,7 +31,7 @@ static int init_stub_pte(struct mm_struct *mm, unsigned long proc, ...@@ -31,7 +31,7 @@ static int init_stub_pte(struct mm_struct *mm, unsigned long proc,
if (!pmd) if (!pmd)
goto out_pmd; goto out_pmd;
pte = pte_alloc_map(mm, NULL, pmd, proc); pte = pte_alloc_map(mm, pmd, proc);
if (!pte) if (!pte)
goto out_pte; goto out_pte;
......
...@@ -54,7 +54,7 @@ pgd_t *get_pgd_slow(struct mm_struct *mm) ...@@ -54,7 +54,7 @@ pgd_t *get_pgd_slow(struct mm_struct *mm)
if (!new_pmd) if (!new_pmd)
goto no_pmd; goto no_pmd;
new_pte = pte_alloc_map(mm, NULL, new_pmd, 0); new_pte = pte_alloc_map(mm, new_pmd, 0);
if (!new_pte) if (!new_pte)
goto no_pte; goto no_pte;
......
...@@ -135,7 +135,7 @@ static int map_tboot_page(unsigned long vaddr, unsigned long pfn, ...@@ -135,7 +135,7 @@ static int map_tboot_page(unsigned long vaddr, unsigned long pfn,
pmd = pmd_alloc(&tboot_mm, pud, vaddr); pmd = pmd_alloc(&tboot_mm, pud, vaddr);
if (!pmd) if (!pmd)
return -1; return -1;
pte = pte_alloc_map(&tboot_mm, NULL, pmd, vaddr); pte = pte_alloc_map(&tboot_mm, pmd, vaddr);
if (!pte) if (!pte)
return -1; return -1;
set_pte_at(&tboot_mm, vaddr, pte, pfn_pte(pfn, prot)); set_pte_at(&tboot_mm, vaddr, pte, pfn_pte(pfn, prot));
......
...@@ -1545,8 +1545,7 @@ static inline void mm_dec_nr_pmds(struct mm_struct *mm) ...@@ -1545,8 +1545,7 @@ static inline void mm_dec_nr_pmds(struct mm_struct *mm)
} }
#endif #endif
int __pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address);
pmd_t *pmd, unsigned long address);
int __pte_alloc_kernel(pmd_t *pmd, unsigned long address); int __pte_alloc_kernel(pmd_t *pmd, unsigned long address);
/* /*
...@@ -1672,15 +1671,15 @@ static inline void pgtable_page_dtor(struct page *page) ...@@ -1672,15 +1671,15 @@ static inline void pgtable_page_dtor(struct page *page)
pte_unmap(pte); \ pte_unmap(pte); \
} while (0) } while (0)
#define pte_alloc_map(mm, vma, pmd, address) \ #define pte_alloc(mm, pmd, address) \
((unlikely(pmd_none(*(pmd))) && __pte_alloc(mm, vma, \ (unlikely(pmd_none(*(pmd))) && __pte_alloc(mm, pmd, address))
pmd, address))? \
NULL: pte_offset_map(pmd, address)) #define pte_alloc_map(mm, pmd, address) \
(pte_alloc(mm, pmd, address) ? NULL : pte_offset_map(pmd, address))
#define pte_alloc_map_lock(mm, pmd, address, ptlp) \ #define pte_alloc_map_lock(mm, pmd, address, ptlp) \
((unlikely(pmd_none(*(pmd))) && __pte_alloc(mm, NULL, \ (pte_alloc(mm, pmd, address) ? \
pmd, address))? \ NULL : pte_offset_map_lock(mm, pmd, address, ptlp))
NULL: pte_offset_map_lock(mm, pmd, address, ptlp))
#define pte_alloc_kernel(pmd, address) \ #define pte_alloc_kernel(pmd, address) \
((unlikely(pmd_none(*(pmd))) && __pte_alloc_kernel(pmd, address))? \ ((unlikely(pmd_none(*(pmd))) && __pte_alloc_kernel(pmd, address))? \
......
...@@ -562,8 +562,7 @@ void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, ...@@ -562,8 +562,7 @@ void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
} }
} }
int __pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
pmd_t *pmd, unsigned long address)
{ {
spinlock_t *ptl; spinlock_t *ptl;
pgtable_t new = pte_alloc_one(mm, address); pgtable_t new = pte_alloc_one(mm, address);
...@@ -3419,12 +3418,11 @@ static int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma, ...@@ -3419,12 +3418,11 @@ static int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
} }
/* /*
* Use __pte_alloc instead of pte_alloc_map, because we can't * Use pte_alloc() instead of pte_alloc_map, because we can't
* run pte_offset_map on the pmd, if an huge pmd could * run pte_offset_map on the pmd, if an huge pmd could
* materialize from under us from a different thread. * materialize from under us from a different thread.
*/ */
if (unlikely(pmd_none(*pmd)) && if (unlikely(pte_alloc(mm, pmd, address)))
unlikely(__pte_alloc(mm, vma, pmd, address)))
return VM_FAULT_OOM; return VM_FAULT_OOM;
/* /*
* If a huge pmd materialized under us just retry later. Use * If a huge pmd materialized under us just retry later. Use
......
...@@ -213,8 +213,7 @@ unsigned long move_page_tables(struct vm_area_struct *vma, ...@@ -213,8 +213,7 @@ unsigned long move_page_tables(struct vm_area_struct *vma,
continue; continue;
VM_BUG_ON(pmd_trans_huge(*old_pmd)); VM_BUG_ON(pmd_trans_huge(*old_pmd));
} }
if (pmd_none(*new_pmd) && __pte_alloc(new_vma->vm_mm, new_vma, if (pte_alloc(new_vma->vm_mm, new_pmd, new_addr))
new_pmd, new_addr))
break; break;
next = (new_addr + PMD_SIZE) & PMD_MASK; next = (new_addr + PMD_SIZE) & PMD_MASK;
if (extent > next - new_addr) if (extent > next - new_addr)
......
...@@ -230,8 +230,7 @@ static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm, ...@@ -230,8 +230,7 @@ static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm,
break; break;
} }
if (unlikely(pmd_none(dst_pmdval)) && if (unlikely(pmd_none(dst_pmdval)) &&
unlikely(__pte_alloc(dst_mm, dst_vma, dst_pmd, unlikely(__pte_alloc(dst_mm, dst_pmd, dst_addr))) {
dst_addr))) {
err = -ENOMEM; err = -ENOMEM;
break; break;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment