Skip to content

Commit 4969c11

Browse files
aarcange-rhtorvalds
authored andcommitted
mm: fix swapin race condition
The pte_same check is reliable only if the swap entry remains pinned (by the page lock on swapcache). We've also to ensure the swapcache isn't removed before we take the lock as try_to_free_swap won't care about the page pin. One of the possible impacts of this patch is that a KSM-shared page can point to the anon_vma of another process, which could exit before the page is freed. This can leave a page with a pointer to a recycled anon_vma object, or worse, a pointer to something that is no longer an anon_vma. [riel@redhat.com: changelog help] Signed-off-by: Andrea Arcangeli <aarcange@redhat.com> Acked-by: Hugh Dickins <hughd@google.com> Reviewed-by: Rik van Riel <riel@redhat.com> Cc: <stable@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 7c5367f commit 4969c11

3 files changed

Lines changed: 43 additions & 19 deletions

File tree

‎include/linux/ksm.h‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
struct stable_node;
1717
struct mem_cgroup;
1818

19+
struct page *ksm_does_need_to_copy(struct page *page,
20+
struct vm_area_struct *vma, unsigned long address);
21+
1922
#ifdef CONFIG_KSM
2023
int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
2124
unsigned long end, int advice, unsigned long *vm_flags);
@@ -70,19 +73,14 @@ static inline void set_page_stable_node(struct page *page,
7073
* We'd like to make this conditional on vma->vm_flags & VM_MERGEABLE,
7174
* but what if the vma was unmerged while the page was swapped out?
7275
*/
73-
struct page *ksm_does_need_to_copy(struct page *page,
74-
struct vm_area_struct *vma, unsigned long address);
75-
static inline struct page *ksm_might_need_to_copy(struct page *page,
76+
static inline int ksm_might_need_to_copy(struct page *page,
7677
struct vm_area_struct *vma, unsigned long address)
7778
{
7879
struct anon_vma *anon_vma = page_anon_vma(page);
7980

80-
if (!anon_vma ||
81-
(anon_vma->root == vma->anon_vma->root &&
82-
page->index == linear_page_index(vma, address)))
83-
return page;
84-
85-
return ksm_does_need_to_copy(page, vma, address);
81+
return anon_vma &&
82+
(anon_vma->root != vma->anon_vma->root ||
83+
page->index != linear_page_index(vma, address));
8684
}
8785

8886
int page_referenced_ksm(struct page *page,
@@ -115,10 +113,10 @@ static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
115113
return 0;
116114
}
117115

118-
static inline struct page *ksm_might_need_to_copy(struct page *page,
116+
static inline int ksm_might_need_to_copy(struct page *page,
119117
struct vm_area_struct *vma, unsigned long address)
120118
{
121-
return page;
119+
return 0;
122120
}
123121

124122
static inline int page_referenced_ksm(struct page *page,

‎mm/ksm.c‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,8 +1504,6 @@ struct page *ksm_does_need_to_copy(struct page *page,
15041504
{
15051505
struct page *new_page;
15061506

1507-
unlock_page(page); /* any racers will COW it, not modify it */
1508-
15091507
new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
15101508
if (new_page) {
15111509
copy_user_highpage(new_page, page, address, vma);
@@ -1521,7 +1519,6 @@ struct page *ksm_does_need_to_copy(struct page *page,
15211519
add_page_to_unevictable_list(new_page);
15221520
}
15231521

1524-
page_cache_release(page);
15251522
return new_page;
15261523
}
15271524

‎mm/memory.c‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2623,7 +2623,7 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
26232623
unsigned int flags, pte_t orig_pte)
26242624
{
26252625
spinlock_t *ptl;
2626-
struct page *page;
2626+
struct page *page, *swapcache = NULL;
26272627
swp_entry_t entry;
26282628
pte_t pte;
26292629
struct mem_cgroup *ptr = NULL;
@@ -2679,10 +2679,23 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
26792679
lock_page(page);
26802680
delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
26812681

2682-
page = ksm_might_need_to_copy(page, vma, address);
2683-
if (!page) {
2684-
ret = VM_FAULT_OOM;
2685-
goto out;
2682+
/*
2683+
* Make sure try_to_free_swap didn't release the swapcache
2684+
* from under us. The page pin isn't enough to prevent that.
2685+
*/
2686+
if (unlikely(!PageSwapCache(page)))
2687+
goto out_page;
2688+
2689+
if (ksm_might_need_to_copy(page, vma, address)) {
2690+
swapcache = page;
2691+
page = ksm_does_need_to_copy(page, vma, address);
2692+
2693+
if (unlikely(!page)) {
2694+
ret = VM_FAULT_OOM;
2695+
page = swapcache;
2696+
swapcache = NULL;
2697+
goto out_page;
2698+
}
26862699
}
26872700

26882701
if (mem_cgroup_try_charge_swapin(mm, page, GFP_KERNEL, &ptr)) {
@@ -2735,6 +2748,18 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
27352748
if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
27362749
try_to_free_swap(page);
27372750
unlock_page(page);
2751+
if (swapcache) {
2752+
/*
2753+
* Hold the lock to avoid the swap entry to be reused
2754+
* until we take the PT lock for the pte_same() check
2755+
* (to avoid false positives from pte_same). For
2756+
* further safety release the lock after the swap_free
2757+
* so that the swap count won't change under a
2758+
* parallel locked swapcache.
2759+
*/
2760+
unlock_page(swapcache);
2761+
page_cache_release(swapcache);
2762+
}
27382763

27392764
if (flags & FAULT_FLAG_WRITE) {
27402765
ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte);
@@ -2756,6 +2781,10 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
27562781
unlock_page(page);
27572782
out_release:
27582783
page_cache_release(page);
2784+
if (swapcache) {
2785+
unlock_page(swapcache);
2786+
page_cache_release(swapcache);
2787+
}
27592788
return ret;
27602789
}
27612790

0 commit comments

Comments
 (0)