mm: fix race condition in MADV_FREE
Subsystems (5)
cover mm: fix race condition in MADV_FREE
From: Shaohua Li <[email protected]>
Artem Savkov reported a race condition[1] in MADV_FREE. MADV_FREE clear pte dirty bit and then mark the page lazyfree. There is no lock to prevent the page is added to swap cache between these two steps by page reclaim. There are two problems:
- page in swapcache is marked lazyfree (clear SwapBacked). This confuses some code pathes, like page fault handling.
- The page is added into swapcache, and freed but the page isn't swapout because pte isn't dity. This will cause data corruption.
The patches will fix the issues.
Thanks,
Shaohua
[1] https://marc.info/?l=linux-mm&m=150589811300667&w=2
Shaohua Li (2):
mm: avoid marking swap cached page as lazyfree
mm: fix data corruption caused by lazyfree page
mm/swap.c | 4 ++-- mm/vmscan.c | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-)
Signature · 7 lines
2.9.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/2 mm: avoid marking swap cached page as lazyfree
From: Shaohua Li <[email protected]>
MADV_FREE clears pte dirty bit and then marks the page lazyfree (clear SwapBacked). There is no lock to prevent the page is added to swap cache between these two steps by page reclaim. If the page is added to swap cache, marking the page lazyfree will confuse page fault if the page is reclaimed and refault.
Reported-and-tested-y: Artem Savkov <[email protected]>
Fix: 802a3a92ad7a(mm: reclaim MADV_FREE pages)
--- mm/swap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/swap.c +2 -2
@@ -575,7 +575,7 @@ static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec, void *arg) {if (PageLRU (page) &&PageAnon (page) &&PageSwapBacked (page) && - !PageUnevictable (page)) { + !PageSwapCache (page) && !PageUnevictable (page)) {bool active =PageActive (page); del_page_from_lru_list(page, lruvec,
@@ -665,7 +665,7 @@ void deactivate_file_page(struct page *page)void mark_page_lazyfree (struct page *page) {if (PageLRU (page) &&PageAnon (page) &&PageSwapBacked (page) && - !PageUnevictable (page)) { + !PageSwapCache (page) && !PageUnevictable (page)) {struct pagevec *pvec = &get_cpu_var (lru_lazyfree_pvecs);get_page (page);
Signature · 7 lines
2.9.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 1/2] mm: avoid marking swap cached page as lazyfree
Quoted 13 lines · Shaohua Li
From: Shaohua Li <[email protected]>
MADV_FREEclears pte dirty bit and then marks the page lazyfree (clear
SwapBacked). There is no lock to prevent the page is added to swap cache
between these two steps by page reclaim. If the page is added to swap cache, marking the page lazyfree will confuse page fault if the page is
reclaimed and refault.Reported-and-tested-y: Artem Savkov <[email protected]>
Fix: 802a3a92ad7a(mm: reclaimMADV_FREEpages)
Quoted 11 lines
Signed-off-by Shaohua Li <[email protected]>Cc Johannes Weiner <[email protected]>Cc Michal Hocko <[email protected]>Cc Hillf Danton <[email protected]>Cc Minchan Kim <[email protected]>Cc Hugh Dickins <[email protected]>Cc Rik van Riel <[email protected]>Cc Mel Gorman <[email protected]>Cc Andrew Morton <[email protected]>
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 2/2 mm: fix data corruption caused by lazyfree page
In reply to Shaohua LiFrom: Shaohua Li <[email protected]>
MADV_FREE clears pte dirty bit and then marks the page lazyfree (clear SwapBacked). There is no lock to prevent the page is added to swap cache between these two steps by page reclaim. If page reclaim finds such page, it will simply add the page to swap cache without pageout the page to swap because the page is marked as clean. Next time, page fault will read data from the swap slot which doesn't have the original data, so we have a data corruption. To fix issue, we mark the page dirty and pageout the page.
However, we shouldn't dirty all pages which is clean and in swap cache. swapin page is swap cache and clean too. So we only dirty page which is added into swap cache in page reclaim, which shouldn't be swapin page. Normal anonymous pages should be dirty already.
Reported-and-tested-y: Artem Savkov <[email protected]>
Fix: 802a3a92ad7a(mm: reclaim MADV_FREE pages)
--- mm/vmscan.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
mm/vmscan.c +12 -0
@@ -980,6 +980,7 @@ static unsigned long shrink_page_list(struct list_head *page_list,int may_enter_fs;enum page_references references =PAGEREF_RECLAIM_CLEAN ;bool dirty, writeback; +bool new_swap_page = false;cond_resched ();
@@ -1165,6 +1166,7 @@ static unsigned long shrink_page_list(struct list_head *page_list,/* Adding to swap updated mapping */ mapping =page_mapping (page); + new_swap_page = true; } }else if (unlikely (PageTransHuge (page))) {/* Split file THP */
@@ -1185,6 +1187,16 @@ static unsigned long shrink_page_list(struct list_head *page_list, nr_unmap_fail++; goto activate_locked; } + +/* +* MADV_FREE clear pte dirty bit, but not yet clear +* SwapBacked for a page. We can't directly free the +* page because we already set swap entry in pte. The +* check guarantees this is such page and not a clean +* swapin page +*/ +if (!PageDirty (page) && new_swap_page) +set_page_dirty (page); }if (PageDirty (page)) {
Signature · 7 lines
2.9.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 2/2] mm: fix data corruption caused by lazyfree page
Quoted 71 lines · Shaohua Li
From: Shaohua Li <[email protected]>
MADV_FREEclears pte dirty bit and then marks the page lazyfree (clear SwapBacked). There is no lock to prevent the page is added to swap cache between these two steps by page reclaim. If page reclaim finds such page, it will simply add the page to swap cache without pageout the page to swap because the page is marked as clean. Next time, page fault will read data from the swap slot which doesn't have the original data, so we have a data corruption. To fix issue, we mark the page dirty and pageout the page.However, we shouldn't dirty all pages which is clean and in swap cache. swapin page is swap cache and clean too. So we only dirty page which is added into swap cache in page reclaim, which shouldn't be swapin page. Normal anonymous pages should be dirty already.
Reported-and-tested-y: Artem Savkov <[email protected]>
Fix: 802a3a92ad7a(mm: reclaimMADV_FREEpages)Signed-off-by Shaohua Li <[email protected]>Cc Johannes Weiner <[email protected]>Cc Michal Hocko <[email protected]>Cc Hillf Danton <[email protected]>Cc Minchan Kim <[email protected]>Cc Hugh Dickins <[email protected]>Cc Rik van Riel <[email protected]>Cc Mel Gorman <[email protected]>Cc Andrew Morton <[email protected]>--- mm/vmscan.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)mm/vmscan.c +12
-0@@ -980,6 +980,7 @@ static unsigned long shrink_page_list(struct list_head *page_list,int may_enter_fs;enum page_references references =PAGEREF_RECLAIM_CLEAN ;bool dirty, writeback; +bool new_swap_page = false;cond_resched ();@@ -1165,6 +1166,7 @@ static unsigned long shrink_page_list(struct list_head *page_list,/* Adding to swap updated mapping */ mapping =page_mapping (page); + new_swap_page = true; } }else if (unlikely (PageTransHuge (page))) {/* Split file THP */ @@ -1185,6 +1187,16 @@ static unsigned long shrink_page_list(struct list_head *page_list, nr_unmap_fail++; goto activate_locked; } + +/* +* MADV_FREE clear pte dirty bit, but not yet clear +* SwapBacked for a page. We can't directly free the +* page because we already set swap entry in pte. The +* check guarantees this is such page and not a clean +* swapin page +*/ +if (!PageDirty (page) && new_swap_page) +set_page_dirty (page); }if (PageDirty (page)) {Signature · 1 line
2.9.5
Couldn't we simple roll back to the logic before MADV_FREE's birth?
mm/swap_state.c +2 -1
@@ -231,7 +231,7 @@ int add_to_swap(struct page *page)* deadlock in the swap out path. */ /* -* Add it to the swap cache. +* Add it to the swap cache and mark it dirty */ err =add_to_swap_cache (page, entry, __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN);
@@ -243,6 +243,7 @@ int add_to_swap(struct page *page)*/ goto fail; +SetPageDirty (page);return 1 ; fail:
To me, it would be more simple/readable rather than introducing a new branch in complicated shrink_page_list.
And I don't see why we cannot merge [1/2] and [2/2].
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 2/2] mm: fix data corruption caused by lazyfree page
Quoted 97 lines · Minchan Kim
Quoted 71 lines · Shaohua Li
From: Shaohua Li <[email protected]>
MADV_FREEclears pte dirty bit and then marks the page lazyfree (clear SwapBacked). There is no lock to prevent the page is added to swap cache between these two steps by page reclaim. If page reclaim finds such page, it will simply add the page to swap cache without pageout the page to swap because the page is marked as clean. Next time, page fault will read data from the swap slot which doesn't have the original data, so we have a data corruption. To fix issue, we mark the page dirty and pageout the page.However, we shouldn't dirty all pages which is clean and in swap cache. swapin page is swap cache and clean too. So we only dirty page which is added into swap cache in page reclaim, which shouldn't be swapin page. Normal anonymous pages should be dirty already.
Reported-and-tested-y: Artem Savkov <[email protected]>
Fix: 802a3a92ad7a(mm: reclaimMADV_FREEpages)Signed-off-by Shaohua Li <[email protected]>Cc Johannes Weiner <[email protected]>Cc Michal Hocko <[email protected]>Cc Hillf Danton <[email protected]>Cc Minchan Kim <[email protected]>Cc Hugh Dickins <[email protected]>Cc Rik van Riel <[email protected]>Cc Mel Gorman <[email protected]>Cc Andrew Morton <[email protected]>--- mm/vmscan.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)mm/vmscan.c +12
-0@@ -980,6 +980,7 @@ static unsigned long shrink_page_list(struct list_head *page_list,int may_enter_fs;enum page_references references =PAGEREF_RECLAIM_CLEAN ;bool dirty, writeback; +bool new_swap_page = false;cond_resched ();@@ -1165,6 +1166,7 @@ static unsigned long shrink_page_list(struct list_head *page_list,/* Adding to swap updated mapping */ mapping =page_mapping (page); + new_swap_page = true; } }else if (unlikely (PageTransHuge (page))) {/* Split file THP */ @@ -1185,6 +1187,16 @@ static unsigned long shrink_page_list(struct list_head *page_list, nr_unmap_fail++; goto activate_locked; } + +/* +* MADV_FREE clear pte dirty bit, but not yet clear +* SwapBacked for a page. We can't directly free the +* page because we already set swap entry in pte. The +* check guarantees this is such page and not a clean +* swapin page +*/ +if (!PageDirty (page) && new_swap_page) +set_page_dirty (page); }if (PageDirty (page)) {Signature · 1 line
2.9.5Couldn't we simple roll back to the logic before MADV_FREE's birth?
mm/swap_state.c +2
-1@@ -231,7 +231,7 @@ int add_to_swap(struct page *page)* deadlock in the swap out path. */ /* -* Add it to the swap cache. +* Add it to the swap cache and mark it dirty */ err =add_to_swap_cache (page, entry, __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN);@@ -243,6 +243,7 @@ int add_to_swap(struct page *page)*/ goto fail; +SetPageDirty (page);return 1 ; fail:
Quoted 2 lines
To me, it would be more simple/readable rather than introducing a new branch in complicated
shrink_page_list.
This is neat, thanks for the suggestion! I'll use set_page_dirty, becuase swapcache set_page_dirty not just set the page dirty.
Quoted 1 line
And I don't see why we cannot merge [1/2] and [2/2].
I feel two separate patches are more clear, but I'll let Andrew decide.
Thanks,
Shaohua
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>