diff options
author | Hugh Dickins <hughd@google.com> | 2010-09-19 22:40:22 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2010-09-20 13:44:37 -0400 |
commit | 31c4a3d3a0f84a5847665f8aa0552d188389f791 (patch) | |
tree | 6dbc630213c899c82030e38c9fa1125c060ef2fe /mm | |
parent | 2422084a94fcd5038406261b331672a13c92c050 (diff) |
mm: further fix swapin race condition
Commit 4969c1192d15 ("mm: fix swapin race condition") is now agreed to
be incomplete. There's a race, not very much less likely than the
original race envisaged, in which it is further necessary to check that
the swapcache page's swap has not changed.
Here's the reasoning: cast in terms of reuse_swap_page(), but probably
could be reformulated to rely on try_to_free_swap() instead, or on
swapoff+swapon.
A, faults into do_swap_page(): does page1 = lookup_swap_cache(swap1) and
comes through the lock_page(page1).
B, a racing thread of the same process, faults on the same address: does
page1 = lookup_swap_cache(swap1) and now waits in lock_page(page1), but
for whatever reason is unlucky not to get the lock any time soon.
A carries on through do_swap_page(), a write fault, but cannot reuse the
swap page1 (another reference to swap1). Unlocks the page1 (but B
doesn't get it yet), does COW in do_wp_page(), page2 now in that pte.
C, perhaps the parent of A+B, comes in and write faults the same swap
page1 into its mm, reuse_swap_page() succeeds this time, swap1 is freed.
kswapd comes in after some time (B still unlucky) and swaps out some
pages from A+B and C: it allocates the original swap1 to page2 in A+B,
and some other swap2 to the original page1 now in C. But does not
immediately free page1 (actually it couldn't: B holds a reference),
leaving it in swap cache for now.
B at last gets the lock on page1, hooray! Is PageSwapCache(page1)? Yes.
Is pte_same(*page_table, orig_pte)? Yes, because page2 has now been
given the swap1 which page1 used to have. So B proceeds to insert page1
into A+B's page_table, though its content now belongs to C, quite
different from what A wrote there.
B ought to have checked that page1's swap was still swap1.
Signed-off-by: Hugh Dickins <hughd@google.com>
Reviewed-by: Rik van Riel <riel@redhat.com>
Cc: stable@kernel.org
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm')
-rw-r--r-- | mm/memory.c | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/mm/memory.c b/mm/memory.c index 71b161b73bb5..0e18b4d649ec 100644 --- a/mm/memory.c +++ b/mm/memory.c | |||
@@ -2680,10 +2680,12 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, | |||
2680 | delayacct_clear_flag(DELAYACCT_PF_SWAPIN); | 2680 | delayacct_clear_flag(DELAYACCT_PF_SWAPIN); |
2681 | 2681 | ||
2682 | /* | 2682 | /* |
2683 | * Make sure try_to_free_swap didn't release the swapcache | 2683 | * Make sure try_to_free_swap or reuse_swap_page or swapoff did not |
2684 | * from under us. The page pin isn't enough to prevent that. | 2684 | * release the swapcache from under us. The page pin, and pte_same |
2685 | * test below, are not enough to exclude that. Even if it is still | ||
2686 | * swapcache, we need to check that the page's swap has not changed. | ||
2685 | */ | 2687 | */ |
2686 | if (unlikely(!PageSwapCache(page))) | 2688 | if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val)) |
2687 | goto out_page; | 2689 | goto out_page; |
2688 | 2690 | ||
2689 | if (ksm_might_need_to_copy(page, vma, address)) { | 2691 | if (ksm_might_need_to_copy(page, vma, address)) { |