Commit 31e855ea authored by Hugh Dickins's avatar Hugh Dickins Committed by Linus Torvalds

ksm: remove redundancies when merging page

There is no need for replace_page() to calculate a write-protected prot
vm_page_prot must already be write-protected for an anonymous page (see
mm/memory.c do_anonymous_page() for similar reliance on vm_page_prot).

There is no need for try_to_merge_one_page() to get_page and put_page on
newpage and oldpage: in every case we already hold a reference to each of
them.

But some instinct makes me move try_to_merge_one_page()'s unlock_page of
oldpage down after replace_page(): that doesn't increase contention on the
ksm page, and makes thinking about the transition easier.
Signed-off-by: default avatarHugh Dickins <hugh.dickins@tiscali.co.uk>
Cc: Izik Eidus <ieidus@redhat.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 93d17715
...@@ -647,7 +647,7 @@ static int write_protect_page(struct vm_area_struct *vma, struct page *page, ...@@ -647,7 +647,7 @@ static int write_protect_page(struct vm_area_struct *vma, struct page *page,
* Check that no O_DIRECT or similar I/O is in progress on the * Check that no O_DIRECT or similar I/O is in progress on the
* page * page
*/ */
if ((page_mapcount(page) + 2 + swapped) != page_count(page)) { if (page_mapcount(page) + 1 + swapped != page_count(page)) {
set_pte_at_notify(mm, addr, ptep, entry); set_pte_at_notify(mm, addr, ptep, entry);
goto out_unlock; goto out_unlock;
} }
...@@ -682,11 +682,8 @@ static int replace_page(struct vm_area_struct *vma, struct page *oldpage, ...@@ -682,11 +682,8 @@ static int replace_page(struct vm_area_struct *vma, struct page *oldpage,
pte_t *ptep; pte_t *ptep;
spinlock_t *ptl; spinlock_t *ptl;
unsigned long addr; unsigned long addr;
pgprot_t prot;
int err = -EFAULT; int err = -EFAULT;
prot = vm_get_page_prot(vma->vm_flags & ~VM_WRITE);
addr = page_address_in_vma(oldpage, vma); addr = page_address_in_vma(oldpage, vma);
if (addr == -EFAULT) if (addr == -EFAULT)
goto out; goto out;
...@@ -714,7 +711,7 @@ static int replace_page(struct vm_area_struct *vma, struct page *oldpage, ...@@ -714,7 +711,7 @@ static int replace_page(struct vm_area_struct *vma, struct page *oldpage,
flush_cache_page(vma, addr, pte_pfn(*ptep)); flush_cache_page(vma, addr, pte_pfn(*ptep));
ptep_clear_flush(vma, addr, ptep); ptep_clear_flush(vma, addr, ptep);
set_pte_at_notify(mm, addr, ptep, mk_pte(newpage, prot)); set_pte_at_notify(mm, addr, ptep, mk_pte(newpage, vma->vm_page_prot));
page_remove_rmap(oldpage); page_remove_rmap(oldpage);
put_page(oldpage); put_page(oldpage);
...@@ -746,13 +743,9 @@ static int try_to_merge_one_page(struct vm_area_struct *vma, ...@@ -746,13 +743,9 @@ static int try_to_merge_one_page(struct vm_area_struct *vma,
if (!(vma->vm_flags & VM_MERGEABLE)) if (!(vma->vm_flags & VM_MERGEABLE))
goto out; goto out;
if (!PageAnon(oldpage)) if (!PageAnon(oldpage))
goto out; goto out;
get_page(newpage);
get_page(oldpage);
/* /*
* We need the page lock to read a stable PageSwapCache in * We need the page lock to read a stable PageSwapCache in
* write_protect_page(). We use trylock_page() instead of * write_protect_page(). We use trylock_page() instead of
...@@ -761,25 +754,18 @@ static int try_to_merge_one_page(struct vm_area_struct *vma, ...@@ -761,25 +754,18 @@ static int try_to_merge_one_page(struct vm_area_struct *vma,
* then come back to this page when it is unlocked. * then come back to this page when it is unlocked.
*/ */
if (!trylock_page(oldpage)) if (!trylock_page(oldpage))
goto out_putpage; goto out;
/* /*
* If this anonymous page is mapped only here, its pte may need * If this anonymous page is mapped only here, its pte may need
* to be write-protected. If it's mapped elsewhere, all of its * to be write-protected. If it's mapped elsewhere, all of its
* ptes are necessarily already write-protected. But in either * ptes are necessarily already write-protected. But in either
* case, we need to lock and check page_count is not raised. * case, we need to lock and check page_count is not raised.
*/ */
if (write_protect_page(vma, oldpage, &orig_pte)) { if (write_protect_page(vma, oldpage, &orig_pte) == 0 &&
unlock_page(oldpage); pages_identical(oldpage, newpage))
goto out_putpage;
}
unlock_page(oldpage);
if (pages_identical(oldpage, newpage))
err = replace_page(vma, oldpage, newpage, orig_pte); err = replace_page(vma, oldpage, newpage, orig_pte);
out_putpage: unlock_page(oldpage);
put_page(oldpage);
put_page(newpage);
out: out:
return err; return err;
} }
......
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