Commit c74df32c authored by Hugh Dickins's avatar Hugh Dickins Committed by Linus Torvalds

[PATCH] mm: ptd_alloc take ptlock

Second step in pushing down the page_table_lock.  Remove the temporary
bridging hack from __pud_alloc, __pmd_alloc, __pte_alloc: expect callers not
to hold page_table_lock, whether it's on init_mm or a user mm; take
page_table_lock internally to check if a racing task already allocated.

Convert their callers from common code.  But avoid coming back to change them
again later: instead of moving the spin_lock(&mm->page_table_lock) down,
switch over to new macros pte_alloc_map_lock and pte_unmap_unlock, which
encapsulate the mapping+locking and unlocking+unmapping together, and in the
end may use alternatives to the mm page_table_lock itself.

These callers all hold mmap_sem (some exclusively, some not), so at no level
can a page table be whipped away from beneath them; and pte_alloc uses the
"atomic" pmd_present to test whether it needs to allocate.  It appears that on
all arches we can safely descend without page_table_lock.
Signed-off-by: default avatarHugh Dickins <hugh@veritas.com>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 1bb3630e
...@@ -309,25 +309,24 @@ void install_arg_page(struct vm_area_struct *vma, ...@@ -309,25 +309,24 @@ void install_arg_page(struct vm_area_struct *vma,
pud_t * pud; pud_t * pud;
pmd_t * pmd; pmd_t * pmd;
pte_t * pte; pte_t * pte;
spinlock_t *ptl;
if (unlikely(anon_vma_prepare(vma))) if (unlikely(anon_vma_prepare(vma)))
goto out_sig; goto out;
flush_dcache_page(page); flush_dcache_page(page);
pgd = pgd_offset(mm, address); pgd = pgd_offset(mm, address);
spin_lock(&mm->page_table_lock);
pud = pud_alloc(mm, pgd, address); pud = pud_alloc(mm, pgd, address);
if (!pud) if (!pud)
goto out; goto out;
pmd = pmd_alloc(mm, pud, address); pmd = pmd_alloc(mm, pud, address);
if (!pmd) if (!pmd)
goto out; goto out;
pte = pte_alloc_map(mm, pmd, address); pte = pte_alloc_map_lock(mm, pmd, address, &ptl);
if (!pte) if (!pte)
goto out; goto out;
if (!pte_none(*pte)) { if (!pte_none(*pte)) {
pte_unmap(pte); pte_unmap_unlock(pte, ptl);
goto out; goto out;
} }
inc_mm_counter(mm, anon_rss); inc_mm_counter(mm, anon_rss);
...@@ -335,14 +334,11 @@ void install_arg_page(struct vm_area_struct *vma, ...@@ -335,14 +334,11 @@ void install_arg_page(struct vm_area_struct *vma,
set_pte_at(mm, address, pte, pte_mkdirty(pte_mkwrite(mk_pte( set_pte_at(mm, address, pte, pte_mkdirty(pte_mkwrite(mk_pte(
page, vma->vm_page_prot)))); page, vma->vm_page_prot))));
page_add_anon_rmap(page, vma, address); page_add_anon_rmap(page, vma, address);
pte_unmap(pte); pte_unmap_unlock(pte, ptl);
spin_unlock(&mm->page_table_lock);
/* no need for flush_tlb */ /* no need for flush_tlb */
return; return;
out: out:
spin_unlock(&mm->page_table_lock);
out_sig:
__free_page(page); __free_page(page);
force_sig(SIGKILL, current); force_sig(SIGKILL, current);
} }
......
...@@ -779,10 +779,28 @@ static inline pmd_t *pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long a ...@@ -779,10 +779,28 @@ static inline pmd_t *pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long a
} }
#endif /* CONFIG_MMU && !__ARCH_HAS_4LEVEL_HACK */ #endif /* CONFIG_MMU && !__ARCH_HAS_4LEVEL_HACK */
#define pte_offset_map_lock(mm, pmd, address, ptlp) \
({ \
spinlock_t *__ptl = &(mm)->page_table_lock; \
pte_t *__pte = pte_offset_map(pmd, address); \
*(ptlp) = __ptl; \
spin_lock(__ptl); \
__pte; \
})
#define pte_unmap_unlock(pte, ptl) do { \
spin_unlock(ptl); \
pte_unmap(pte); \
} while (0)
#define pte_alloc_map(mm, pmd, address) \ #define pte_alloc_map(mm, pmd, address) \
((unlikely(!pmd_present(*(pmd))) && __pte_alloc(mm, pmd, address))? \ ((unlikely(!pmd_present(*(pmd))) && __pte_alloc(mm, pmd, address))? \
NULL: pte_offset_map(pmd, address)) NULL: pte_offset_map(pmd, address))
#define pte_alloc_map_lock(mm, pmd, address, ptlp) \
((unlikely(!pmd_present(*(pmd))) && __pte_alloc(mm, pmd, address))? \
NULL: pte_offset_map_lock(mm, pmd, address, ptlp))
#define pte_alloc_kernel(pmd, address) \ #define pte_alloc_kernel(pmd, address) \
((unlikely(!pmd_present(*(pmd))) && __pte_alloc_kernel(pmd, address))? \ ((unlikely(!pmd_present(*(pmd))) && __pte_alloc_kernel(pmd, address))? \
NULL: pte_offset_kernel(pmd, address)) NULL: pte_offset_kernel(pmd, address))
......
...@@ -255,7 +255,6 @@ static inline int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) ...@@ -255,7 +255,6 @@ static inline int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
/* /*
* Link in the new vma and copy the page table entries. * Link in the new vma and copy the page table entries.
*/ */
spin_lock(&mm->page_table_lock);
*pprev = tmp; *pprev = tmp;
pprev = &tmp->vm_next; pprev = &tmp->vm_next;
...@@ -265,7 +264,6 @@ static inline int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) ...@@ -265,7 +264,6 @@ static inline int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
mm->map_count++; mm->map_count++;
retval = copy_page_range(mm, oldmm, tmp); retval = copy_page_range(mm, oldmm, tmp);
spin_unlock(&mm->page_table_lock);
if (tmp->vm_ops && tmp->vm_ops->open) if (tmp->vm_ops && tmp->vm_ops->open)
tmp->vm_ops->open(tmp); tmp->vm_ops->open(tmp);
......
...@@ -63,23 +63,20 @@ int install_page(struct mm_struct *mm, struct vm_area_struct *vma, ...@@ -63,23 +63,20 @@ int install_page(struct mm_struct *mm, struct vm_area_struct *vma,
pud_t *pud; pud_t *pud;
pgd_t *pgd; pgd_t *pgd;
pte_t pte_val; pte_t pte_val;
spinlock_t *ptl;
BUG_ON(vma->vm_flags & VM_RESERVED); BUG_ON(vma->vm_flags & VM_RESERVED);
pgd = pgd_offset(mm, addr); pgd = pgd_offset(mm, addr);
spin_lock(&mm->page_table_lock);
pud = pud_alloc(mm, pgd, addr); pud = pud_alloc(mm, pgd, addr);
if (!pud) if (!pud)
goto err_unlock; goto out;
pmd = pmd_alloc(mm, pud, addr); pmd = pmd_alloc(mm, pud, addr);
if (!pmd) if (!pmd)
goto err_unlock; goto out;
pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
pte = pte_alloc_map(mm, pmd, addr);
if (!pte) if (!pte)
goto err_unlock; goto out;
/* /*
* This page may have been truncated. Tell the * This page may have been truncated. Tell the
...@@ -89,10 +86,10 @@ int install_page(struct mm_struct *mm, struct vm_area_struct *vma, ...@@ -89,10 +86,10 @@ int install_page(struct mm_struct *mm, struct vm_area_struct *vma,
inode = vma->vm_file->f_mapping->host; inode = vma->vm_file->f_mapping->host;
size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
if (!page->mapping || page->index >= size) if (!page->mapping || page->index >= size)
goto err_unlock; goto unlock;
err = -ENOMEM; err = -ENOMEM;
if (page_mapcount(page) > INT_MAX/2) if (page_mapcount(page) > INT_MAX/2)
goto err_unlock; goto unlock;
if (pte_none(*pte) || !zap_pte(mm, vma, addr, pte)) if (pte_none(*pte) || !zap_pte(mm, vma, addr, pte))
inc_mm_counter(mm, file_rss); inc_mm_counter(mm, file_rss);
...@@ -101,17 +98,15 @@ int install_page(struct mm_struct *mm, struct vm_area_struct *vma, ...@@ -101,17 +98,15 @@ int install_page(struct mm_struct *mm, struct vm_area_struct *vma,
set_pte_at(mm, addr, pte, mk_pte(page, prot)); set_pte_at(mm, addr, pte, mk_pte(page, prot));
page_add_file_rmap(page); page_add_file_rmap(page);
pte_val = *pte; pte_val = *pte;
pte_unmap(pte);
update_mmu_cache(vma, addr, pte_val); update_mmu_cache(vma, addr, pte_val);
err = 0; err = 0;
err_unlock: unlock:
spin_unlock(&mm->page_table_lock); pte_unmap_unlock(pte, ptl);
out:
return err; return err;
} }
EXPORT_SYMBOL(install_page); EXPORT_SYMBOL(install_page);
/* /*
* Install a file pte to a given virtual memory address, release any * Install a file pte to a given virtual memory address, release any
* previously existing mapping. * previously existing mapping.
...@@ -125,23 +120,20 @@ int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma, ...@@ -125,23 +120,20 @@ int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
pud_t *pud; pud_t *pud;
pgd_t *pgd; pgd_t *pgd;
pte_t pte_val; pte_t pte_val;
spinlock_t *ptl;
BUG_ON(vma->vm_flags & VM_RESERVED); BUG_ON(vma->vm_flags & VM_RESERVED);
pgd = pgd_offset(mm, addr); pgd = pgd_offset(mm, addr);
spin_lock(&mm->page_table_lock);
pud = pud_alloc(mm, pgd, addr); pud = pud_alloc(mm, pgd, addr);
if (!pud) if (!pud)
goto err_unlock; goto out;
pmd = pmd_alloc(mm, pud, addr); pmd = pmd_alloc(mm, pud, addr);
if (!pmd) if (!pmd)
goto err_unlock; goto out;
pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
pte = pte_alloc_map(mm, pmd, addr);
if (!pte) if (!pte)
goto err_unlock; goto out;
if (!pte_none(*pte) && zap_pte(mm, vma, addr, pte)) { if (!pte_none(*pte) && zap_pte(mm, vma, addr, pte)) {
update_hiwater_rss(mm); update_hiwater_rss(mm);
...@@ -150,17 +142,13 @@ int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma, ...@@ -150,17 +142,13 @@ int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
set_pte_at(mm, addr, pte, pgoff_to_pte(pgoff)); set_pte_at(mm, addr, pte, pgoff_to_pte(pgoff));
pte_val = *pte; pte_val = *pte;
pte_unmap(pte);
update_mmu_cache(vma, addr, pte_val); update_mmu_cache(vma, addr, pte_val);
spin_unlock(&mm->page_table_lock); pte_unmap_unlock(pte, ptl);
return 0; err = 0;
out:
err_unlock:
spin_unlock(&mm->page_table_lock);
return err; return err;
} }
/*** /***
* sys_remap_file_pages - remap arbitrary pages of a shared backing store * sys_remap_file_pages - remap arbitrary pages of a shared backing store
* file within an existing vma. * file within an existing vma.
......
...@@ -277,12 +277,15 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, ...@@ -277,12 +277,15 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
unsigned long addr; unsigned long addr;
for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) { for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
src_pte = huge_pte_offset(src, addr);
if (!src_pte)
continue;
dst_pte = huge_pte_alloc(dst, addr); dst_pte = huge_pte_alloc(dst, addr);
if (!dst_pte) if (!dst_pte)
goto nomem; goto nomem;
spin_lock(&dst->page_table_lock);
spin_lock(&src->page_table_lock); spin_lock(&src->page_table_lock);
src_pte = huge_pte_offset(src, addr); if (!pte_none(*src_pte)) {
if (src_pte && !pte_none(*src_pte)) {
entry = *src_pte; entry = *src_pte;
ptepage = pte_page(entry); ptepage = pte_page(entry);
get_page(ptepage); get_page(ptepage);
...@@ -290,6 +293,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, ...@@ -290,6 +293,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
set_huge_pte_at(dst, addr, dst_pte, entry); set_huge_pte_at(dst, addr, dst_pte, entry);
} }
spin_unlock(&src->page_table_lock); spin_unlock(&src->page_table_lock);
spin_unlock(&dst->page_table_lock);
} }
return 0; return 0;
...@@ -354,7 +358,6 @@ int hugetlb_prefault(struct address_space *mapping, struct vm_area_struct *vma) ...@@ -354,7 +358,6 @@ int hugetlb_prefault(struct address_space *mapping, struct vm_area_struct *vma)
hugetlb_prefault_arch_hook(mm); hugetlb_prefault_arch_hook(mm);
spin_lock(&mm->page_table_lock);
for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) { for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
unsigned long idx; unsigned long idx;
pte_t *pte = huge_pte_alloc(mm, addr); pte_t *pte = huge_pte_alloc(mm, addr);
...@@ -389,11 +392,12 @@ int hugetlb_prefault(struct address_space *mapping, struct vm_area_struct *vma) ...@@ -389,11 +392,12 @@ int hugetlb_prefault(struct address_space *mapping, struct vm_area_struct *vma)
goto out; goto out;
} }
} }
spin_lock(&mm->page_table_lock);
add_mm_counter(mm, file_rss, HPAGE_SIZE / PAGE_SIZE); add_mm_counter(mm, file_rss, HPAGE_SIZE / PAGE_SIZE);
set_huge_pte_at(mm, addr, pte, make_huge_pte(vma, page)); set_huge_pte_at(mm, addr, pte, make_huge_pte(vma, page));
spin_unlock(&mm->page_table_lock);
} }
out: out:
spin_unlock(&mm->page_table_lock);
return ret; return ret;
} }
......
...@@ -282,14 +282,11 @@ void free_pgtables(struct mmu_gather **tlb, struct vm_area_struct *vma, ...@@ -282,14 +282,11 @@ void free_pgtables(struct mmu_gather **tlb, struct vm_area_struct *vma,
int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address) int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
{ {
struct page *new; struct page *new = pte_alloc_one(mm, address);
spin_unlock(&mm->page_table_lock);
new = pte_alloc_one(mm, address);
spin_lock(&mm->page_table_lock);
if (!new) if (!new)
return -ENOMEM; return -ENOMEM;
spin_lock(&mm->page_table_lock);
if (pmd_present(*pmd)) /* Another has populated it */ if (pmd_present(*pmd)) /* Another has populated it */
pte_free(new); pte_free(new);
else { else {
...@@ -297,6 +294,7 @@ int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address) ...@@ -297,6 +294,7 @@ int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
inc_page_state(nr_page_table_pages); inc_page_state(nr_page_table_pages);
pmd_populate(mm, pmd, new); pmd_populate(mm, pmd, new);
} }
spin_unlock(&mm->page_table_lock);
return 0; return 0;
} }
...@@ -344,9 +342,6 @@ void print_bad_pte(struct vm_area_struct *vma, pte_t pte, unsigned long vaddr) ...@@ -344,9 +342,6 @@ void print_bad_pte(struct vm_area_struct *vma, pte_t pte, unsigned long vaddr)
* copy one vm_area from one task to the other. Assumes the page tables * copy one vm_area from one task to the other. Assumes the page tables
* already present in the new task to be cleared in the whole range * already present in the new task to be cleared in the whole range
* covered by this vma. * covered by this vma.
*
* dst->page_table_lock is held on entry and exit,
* but may be dropped within p[mg]d_alloc() and pte_alloc_map().
*/ */
static inline void static inline void
...@@ -419,17 +414,19 @@ static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, ...@@ -419,17 +414,19 @@ static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
unsigned long addr, unsigned long end) unsigned long addr, unsigned long end)
{ {
pte_t *src_pte, *dst_pte; pte_t *src_pte, *dst_pte;
spinlock_t *src_ptl, *dst_ptl;
int progress = 0; int progress = 0;
int rss[2]; int rss[2];
again: again:
rss[1] = rss[0] = 0; rss[1] = rss[0] = 0;
dst_pte = pte_alloc_map(dst_mm, dst_pmd, addr); dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
if (!dst_pte) if (!dst_pte)
return -ENOMEM; return -ENOMEM;
src_pte = pte_offset_map_nested(src_pmd, addr); src_pte = pte_offset_map_nested(src_pmd, addr);
src_ptl = &src_mm->page_table_lock;
spin_lock(src_ptl);
spin_lock(&src_mm->page_table_lock);
do { do {
/* /*
* We are holding two locks at this point - either of them * We are holding two locks at this point - either of them
...@@ -438,8 +435,8 @@ again: ...@@ -438,8 +435,8 @@ again:
if (progress >= 32) { if (progress >= 32) {
progress = 0; progress = 0;
if (need_resched() || if (need_resched() ||
need_lockbreak(&src_mm->page_table_lock) || need_lockbreak(src_ptl) ||
need_lockbreak(&dst_mm->page_table_lock)) need_lockbreak(dst_ptl))
break; break;
} }
if (pte_none(*src_pte)) { if (pte_none(*src_pte)) {
...@@ -449,12 +446,12 @@ again: ...@@ -449,12 +446,12 @@ again:
copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, vma, addr, rss); copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, vma, addr, rss);
progress += 8; progress += 8;
} while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
spin_unlock(&src_mm->page_table_lock);
spin_unlock(src_ptl);
pte_unmap_nested(src_pte - 1); pte_unmap_nested(src_pte - 1);
pte_unmap(dst_pte - 1);
add_mm_rss(dst_mm, rss[0], rss[1]); add_mm_rss(dst_mm, rss[0], rss[1]);
cond_resched_lock(&dst_mm->page_table_lock); pte_unmap_unlock(dst_pte - 1, dst_ptl);
cond_resched();
if (addr != end) if (addr != end)
goto again; goto again;
return 0; return 0;
...@@ -1049,8 +1046,9 @@ static int zeromap_pte_range(struct mm_struct *mm, pmd_t *pmd, ...@@ -1049,8 +1046,9 @@ static int zeromap_pte_range(struct mm_struct *mm, pmd_t *pmd,
unsigned long addr, unsigned long end, pgprot_t prot) unsigned long addr, unsigned long end, pgprot_t prot)
{ {
pte_t *pte; pte_t *pte;
spinlock_t *ptl;
pte = pte_alloc_map(mm, pmd, addr); pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
if (!pte) if (!pte)
return -ENOMEM; return -ENOMEM;
do { do {
...@@ -1062,7 +1060,7 @@ static int zeromap_pte_range(struct mm_struct *mm, pmd_t *pmd, ...@@ -1062,7 +1060,7 @@ static int zeromap_pte_range(struct mm_struct *mm, pmd_t *pmd,
BUG_ON(!pte_none(*pte)); BUG_ON(!pte_none(*pte));
set_pte_at(mm, addr, pte, zero_pte); set_pte_at(mm, addr, pte, zero_pte);
} while (pte++, addr += PAGE_SIZE, addr != end); } while (pte++, addr += PAGE_SIZE, addr != end);
pte_unmap(pte - 1); pte_unmap_unlock(pte - 1, ptl);
return 0; return 0;
} }
...@@ -1112,14 +1110,12 @@ int zeromap_page_range(struct vm_area_struct *vma, ...@@ -1112,14 +1110,12 @@ int zeromap_page_range(struct vm_area_struct *vma,
BUG_ON(addr >= end); BUG_ON(addr >= end);
pgd = pgd_offset(mm, addr); pgd = pgd_offset(mm, addr);
flush_cache_range(vma, addr, end); flush_cache_range(vma, addr, end);
spin_lock(&mm->page_table_lock);
do { do {
next = pgd_addr_end(addr, end); next = pgd_addr_end(addr, end);
err = zeromap_pud_range(mm, pgd, addr, next, prot); err = zeromap_pud_range(mm, pgd, addr, next, prot);
if (err) if (err)
break; break;
} while (pgd++, addr = next, addr != end); } while (pgd++, addr = next, addr != end);
spin_unlock(&mm->page_table_lock);
return err; return err;
} }
...@@ -1133,8 +1129,9 @@ static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, ...@@ -1133,8 +1129,9 @@ static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
unsigned long pfn, pgprot_t prot) unsigned long pfn, pgprot_t prot)
{ {
pte_t *pte; pte_t *pte;
spinlock_t *ptl;
pte = pte_alloc_map(mm, pmd, addr); pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
if (!pte) if (!pte)
return -ENOMEM; return -ENOMEM;
do { do {
...@@ -1142,7 +1139,7 @@ static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, ...@@ -1142,7 +1139,7 @@ static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
set_pte_at(mm, addr, pte, pfn_pte(pfn, prot)); set_pte_at(mm, addr, pte, pfn_pte(pfn, prot));
pfn++; pfn++;
} while (pte++, addr += PAGE_SIZE, addr != end); } while (pte++, addr += PAGE_SIZE, addr != end);
pte_unmap(pte - 1); pte_unmap_unlock(pte - 1, ptl);
return 0; return 0;
} }
...@@ -1210,7 +1207,6 @@ int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, ...@@ -1210,7 +1207,6 @@ int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
pfn -= addr >> PAGE_SHIFT; pfn -= addr >> PAGE_SHIFT;
pgd = pgd_offset(mm, addr); pgd = pgd_offset(mm, addr);
flush_cache_range(vma, addr, end); flush_cache_range(vma, addr, end);
spin_lock(&mm->page_table_lock);
do { do {
next = pgd_addr_end(addr, end); next = pgd_addr_end(addr, end);
err = remap_pud_range(mm, pgd, addr, next, err = remap_pud_range(mm, pgd, addr, next,
...@@ -1218,7 +1214,6 @@ int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, ...@@ -1218,7 +1214,6 @@ int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
if (err) if (err)
break; break;
} while (pgd++, addr = next, addr != end); } while (pgd++, addr = next, addr != end);
spin_unlock(&mm->page_table_lock);
return err; return err;
} }
EXPORT_SYMBOL(remap_pfn_range); EXPORT_SYMBOL(remap_pfn_range);
...@@ -1985,17 +1980,9 @@ static int do_file_page(struct mm_struct *mm, struct vm_area_struct *vma, ...@@ -1985,17 +1980,9 @@ static int do_file_page(struct mm_struct *mm, struct vm_area_struct *vma,
* with external mmu caches can use to update those (ie the Sparc or * with external mmu caches can use to update those (ie the Sparc or
* PowerPC hashed page tables that act as extended TLBs). * PowerPC hashed page tables that act as extended TLBs).
* *
* Note the "page_table_lock". It is to protect against kswapd removing * We enter with non-exclusive mmap_sem (to exclude vma changes,
* pages from under us. Note that kswapd only ever _removes_ pages, never * but allow concurrent faults), and pte mapped but not yet locked.
* adds them. As such, once we have noticed that the page is not present, * We return with mmap_sem still held, but pte unmapped and unlocked.
* we can drop the lock early.
*
* The adding of pages is protected by the MM semaphore (which we hold),
* so we don't need to worry about a page being suddenly been added into
* our VM.
*
* We enter with the pagetable spinlock held, we are supposed to
* release it when done.
*/ */
static inline int handle_pte_fault(struct mm_struct *mm, static inline int handle_pte_fault(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long address, struct vm_area_struct *vma, unsigned long address,
...@@ -2003,6 +1990,7 @@ static inline int handle_pte_fault(struct mm_struct *mm, ...@@ -2003,6 +1990,7 @@ static inline int handle_pte_fault(struct mm_struct *mm,
{ {
pte_t entry; pte_t entry;
spin_lock(&mm->page_table_lock);
entry = *pte; entry = *pte;
if (!pte_present(entry)) { if (!pte_present(entry)) {
if (pte_none(entry)) { if (pte_none(entry)) {
...@@ -2051,30 +2039,18 @@ int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma, ...@@ -2051,30 +2039,18 @@ int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
if (unlikely(is_vm_hugetlb_page(vma))) if (unlikely(is_vm_hugetlb_page(vma)))
return hugetlb_fault(mm, vma, address, write_access); return hugetlb_fault(mm, vma, address, write_access);
/*
* We need the page table lock to synchronize with kswapd
* and the SMP-safe atomic PTE updates.
*/
pgd = pgd_offset(mm, address); pgd = pgd_offset(mm, address);
spin_lock(&mm->page_table_lock);
pud = pud_alloc(mm, pgd, address); pud = pud_alloc(mm, pgd, address);
if (!pud) if (!pud)
goto oom; return VM_FAULT_OOM;
pmd = pmd_alloc(mm, pud, address); pmd = pmd_alloc(mm, pud, address);
if (!pmd) if (!pmd)
goto oom; return VM_FAULT_OOM;
pte = pte_alloc_map(mm, pmd, address); pte = pte_alloc_map(mm, pmd, address);
if (!pte) if (!pte)
goto oom; return VM_FAULT_OOM;
return handle_pte_fault(mm, vma, address, pte, pmd, write_access); return handle_pte_fault(mm, vma, address, pte, pmd, write_access);
oom:
spin_unlock(&mm->page_table_lock);
return VM_FAULT_OOM;
} }
#ifndef __PAGETABLE_PUD_FOLDED #ifndef __PAGETABLE_PUD_FOLDED
...@@ -2084,23 +2060,15 @@ int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma, ...@@ -2084,23 +2060,15 @@ int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
*/ */
int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
{ {
pud_t *new; pud_t *new = pud_alloc_one(mm, address);
if (!new)
if (mm != &init_mm) /* Temporary bridging hack */
spin_unlock(&mm->page_table_lock);
new = pud_alloc_one(mm, address);
if (!new) {
if (mm != &init_mm) /* Temporary bridging hack */
spin_lock(&mm->page_table_lock);
return -ENOMEM; return -ENOMEM;
}
spin_lock(&mm->page_table_lock); spin_lock(&mm->page_table_lock);
if (pgd_present(*pgd)) /* Another has populated it */ if (pgd_present(*pgd)) /* Another has populated it */
pud_free(new); pud_free(new);
else else
pgd_populate(mm, pgd, new); pgd_populate(mm, pgd, new);
if (mm == &init_mm) /* Temporary bridging hack */
spin_unlock(&mm->page_table_lock); spin_unlock(&mm->page_table_lock);
return 0; return 0;
} }
...@@ -2113,16 +2081,9 @@ int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) ...@@ -2113,16 +2081,9 @@ int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
*/ */
int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
{ {
pmd_t *new; pmd_t *new = pmd_alloc_one(mm, address);
if (!new)
if (mm != &init_mm) /* Temporary bridging hack */
spin_unlock(&mm->page_table_lock);
new = pmd_alloc_one(mm, address);
if (!new) {
if (mm != &init_mm) /* Temporary bridging hack */
spin_lock(&mm->page_table_lock);
return -ENOMEM; return -ENOMEM;
}
spin_lock(&mm->page_table_lock); spin_lock(&mm->page_table_lock);
#ifndef __ARCH_HAS_4LEVEL_HACK #ifndef __ARCH_HAS_4LEVEL_HACK
...@@ -2136,7 +2097,6 @@ int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) ...@@ -2136,7 +2097,6 @@ int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
else else
pgd_populate(mm, pud, new); pgd_populate(mm, pud, new);
#endif /* __ARCH_HAS_4LEVEL_HACK */ #endif /* __ARCH_HAS_4LEVEL_HACK */
if (mm == &init_mm) /* Temporary bridging hack */
spin_unlock(&mm->page_table_lock); spin_unlock(&mm->page_table_lock);
return 0; return 0;
} }
......
...@@ -28,9 +28,6 @@ static pmd_t *get_old_pmd(struct mm_struct *mm, unsigned long addr) ...@@ -28,9 +28,6 @@ static pmd_t *get_old_pmd(struct mm_struct *mm, unsigned long addr)
pud_t *pud; pud_t *pud;
pmd_t *pmd; pmd_t *pmd;
/*
* We don't need page_table_lock: we have mmap_sem exclusively.
*/
pgd = pgd_offset(mm, addr); pgd = pgd_offset(mm, addr);
if (pgd_none_or_clear_bad(pgd)) if (pgd_none_or_clear_bad(pgd))
return NULL; return NULL;
...@@ -50,25 +47,20 @@ static pmd_t *alloc_new_pmd(struct mm_struct *mm, unsigned long addr) ...@@ -50,25 +47,20 @@ static pmd_t *alloc_new_pmd(struct mm_struct *mm, unsigned long addr)
{ {
pgd_t *pgd; pgd_t *pgd;
pud_t *pud; pud_t *pud;
pmd_t *pmd = NULL; pmd_t *pmd;
/*
* We do need page_table_lock: because allocators expect that.
*/
spin_lock(&mm->page_table_lock);
pgd = pgd_offset(mm, addr); pgd = pgd_offset(mm, addr);
pud = pud_alloc(mm, pgd, addr); pud = pud_alloc(mm, pgd, addr);
if (!pud) if (!pud)
goto out; return NULL;
pmd = pmd_alloc(mm, pud, addr); pmd = pmd_alloc(mm, pud, addr);
if (!pmd) if (!pmd)
goto out; return NULL;
if (!pmd_present(*pmd) && __pte_alloc(mm, pmd, addr)) if (!pmd_present(*pmd) && __pte_alloc(mm, pmd, addr))
pmd = NULL; return NULL;
out:
spin_unlock(&mm->page_table_lock);
return pmd; return pmd;
} }
...@@ -80,6 +72,7 @@ static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd, ...@@ -80,6 +72,7 @@ static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
struct address_space *mapping = NULL; struct address_space *mapping = NULL;
struct mm_struct *mm = vma->vm_mm; struct mm_struct *mm = vma->vm_mm;
pte_t *old_pte, *new_pte, pte; pte_t *old_pte, *new_pte, pte;
spinlock_t *old_ptl;
if (vma->vm_file) { if (vma->vm_file) {
/* /*
...@@ -95,8 +88,7 @@ static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd, ...@@ -95,8 +88,7 @@ static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
new_vma->vm_truncate_count = 0; new_vma->vm_truncate_count = 0;
} }
spin_lock(&mm->page_table_lock); old_pte = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl);
old_pte = pte_offset_map(old_pmd, old_addr);
new_pte = pte_offset_map_nested(new_pmd, new_addr); new_pte = pte_offset_map_nested(new_pmd, new_addr);
for (; old_addr < old_end; old_pte++, old_addr += PAGE_SIZE, for (; old_addr < old_end; old_pte++, old_addr += PAGE_SIZE,
...@@ -110,8 +102,7 @@ static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd, ...@@ -110,8 +102,7 @@ static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
} }
pte_unmap_nested(new_pte - 1); pte_unmap_nested(new_pte - 1);
pte_unmap(old_pte - 1); pte_unmap_unlock(old_pte - 1, old_ptl);
spin_unlock(&mm->page_table_lock);
if (mapping) if (mapping)
spin_unlock(&mapping->i_mmap_lock); spin_unlock(&mapping->i_mmap_lock);
} }
......
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