Skip to contentSkip to conversation

mm: fix race condition in MADV_FREE

PATCH Applied Shaohua Li 4 participants 9 messages Sep 26, 2017

Subsystems (3)
lore
Share & tools
lore

Shaohua Li

author
#0 lore raw

v3 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 dirty. This will cause data corruption.

The patches will fix the issues.

I knew Minchan suggested these should be combined to one patch, but I really think the separation makes things clearer because these are two issues even they are stemmed from the same race.

Thanks,
Shaohua

V2->V3:
- reword patch log and code comments, no code change

V1->V2:
- dirty page in add_to_swap instead of in shrink_page_list as suggested by Minchan

Shaohua Li (2):
mm: avoid marking swap cached page as lazyfree
mm: fix data corruption caused by lazyfree page

 mm/swap.c       |  4 ++--
 mm/swap_state.c | 11 +++++++++++
 2 files changed, 13 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>
author
#1 lore raw

PATCH v3 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. Page reclaim could add the page to swap cache and unmap the page. After page reclaim, the page is added back to lru. At that time, we probably start draining per-cpu pagevec and mark the page lazyfree. So the page could be in a state with SwapBacked cleared and PG_swapcache set. Next time there is a refault in the virtual address, do_swap_page can find the page from swap cache but the page has PageSwapCache false because SwapBacked isn't set, so do_swap_page will bail out and do nothing. The task will keep running into fault handler.

Reported-and-tested-by Artem Savkov <[email protected]>

Fix: 802a3a92ad7a(mm: reclaim MADV_FREE pages)

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 Mel Gorman <[email protected]>
Cc Andrew Morton <[email protected]>
---
 mm/swap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/swap.c +2 -2
index 9295ae9..a77d68f 100644
@@ -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>
Acked-by on v3 1/2
#2 lore raw

Re: [PATCH V3 1/2] mm: avoid marking swap cached page as lazyfree

Quoted 13 lines · Shaohua Li

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. Page reclaim could add the page to swap cache and unmap the page. After page reclaim, the page is added back to lru. At that time, we probably start draining per-cpu pagevec and mark the page lazyfree. So the page could be in a state with SwapBacked cleared and PG_swapcache set. Next time there is a refault in the virtual address, do_swap_page can find the page from swap cache but the page has PageSwapCache false because SwapBacked isn't set, so do_swap_page will bail out and do nothing. The task will keep running into fault handler.

The patch lgtm, but for the changelog it probably makes sense to start with the user-visible behavior, i.e. the endlessly looping swap fault handler because it thinks it's racing with the swap slot being freed.

Makes it easier for other distro/vendor people to identify this for backporting.

On that note, I think this should go into 4.13 and be tagged for 4.12 stable.

Quoted 12 lines
Reported-and-tested-by Artem Savkov <[email protected]>

Fix: 802a3a92ad7a(mm: reclaim MADV_FREE pages)

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 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>

Acked-by on v3 1/2
#3 lore raw

Re: [PATCH V3 1/2] mm: avoid marking swap cached page as lazyfree

In reply to Shaohua Li
Quoted 13 lines · Shaohua Li

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. Page reclaim could add the page to swap cache and unmap the page. After page reclaim, the page is added back to lru. At that time, we probably start draining per-cpu pagevec and mark the page lazyfree. So the page could be in a state with SwapBacked cleared and PG_swapcache set. Next time there is a refault in the virtual address, do_swap_page can find the page from swap cache but the page has PageSwapCache false because SwapBacked isn't set, so do_swap_page will bail out and do nothing. The task will keep running into fault handler.

Thanks for the clarification in the changelog. It is much more clear now!

Quoted 12 lines
Reported-and-tested-by Artem Savkov <[email protected]>

Fix: 802a3a92ad7a(mm: reclaim MADV_FREE pages)

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 Mel Gorman <[email protected]>
Cc Andrew Morton <[email protected]>

Marking for stable as suggested by Johannes makes perfect sense to me.

Quoted 29 lines
---
 mm/swap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/swap.c +2 -2
index 9295ae9..a77d68f 100644
@@ -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 · 1 line
2.9.5
Signature · 8 lines
Michal Hocko
SUSE Labs

--
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>
Acked-by on v3 1/2
#4 lore raw

Re: [PATCH V3 1/2] mm: avoid marking swap cached page as lazyfree

In reply to Shaohua Li
Quoted 13 lines · Shaohua Li

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. Page reclaim could add the page to swap cache and unmap the page. After page reclaim, the page is added back to lru. At that time, we probably start draining per-cpu pagevec and mark the page lazyfree. So the page could be in a state with SwapBacked cleared and PG_swapcache set. Next time there is a refault in the virtual address, do_swap_page can find the page from swap cache but the page has PageSwapCache false because SwapBacked isn't set, so do_swap_page will bail out and do nothing. The task will keep running into fault handler.

With new description, I got why you want to seperate this. Yub, it should be separated. Sorry for the noise. What I was missing is PageSwapCache's change which checked PG_swapbacked as well as PG_swapcache. I didn't notice that the change.

--
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>

author
#5 lore raw

PATCH v3 2/2 mm: fix data corruption caused by lazyfree page

In reply to Shaohua Li

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 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. As Minchan suggested, simply dirty the page in add_to_swap can do the job.

Fix: 802a3a92ad7a(mm: reclaim MADV_FREE pages)

Cc Johannes Weiner <[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/swap_state.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
mm/swap_state.c +11 -0
index 71ce2d1..ed91091 100644
@@ -242,6 +242,17 @@ int add_to_swap(struct page *page)
 		 * clear SWAP_HAS_CACHE flag.
 		 */
 		goto fail;
+	/*
+	 * Normally the page will be dirtied in unmap because its pte should be
+	 * dirty. A special case is MADV_FREE page. The page'e pte could have
+	 * dirty bit cleared but the page's SwapBacked bit is still set because
+	 * clearing the dirty bit and SwapBacked bit has no lock protected. For
+	 * such page, unmap will not set dirty bit for it, so page reclaim will
+	 * not write the page out. This can cause data corruption when the page
+	 * is swap in later. Always setting the dirty bit for the page solves
+	 * the problem.
+	 */
+	set_page_dirty(page);
 
 	return 1;
 
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>
#6 lore raw

Re: [PATCH V3 2/2] mm: fix data corruption caused by lazyfree page

Quoted 10 lines · Shaohua Li

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 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.

Reclaim and MADV_FREE hold the page lock when manipulating the dirty and the swapcache state.

Instead of undoing a racing MADV_FREE in reclaim, wouldn't it be safe to check the dirty bit before add_to_swap() and skip clean pages?

--
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>

author
#7 lore raw

Re: [PATCH V3 2/2] mm: fix data corruption caused by lazyfree page

Quoted 12 lines · Johannes Weiner
Quoted 10 lines · Shaohua Li

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 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.

Quoted 5 lines

Reclaim and MADV_FREE hold the page lock when manipulating the dirty and the swapcache state.

Instead of undoing a racing MADV_FREE in reclaim, wouldn't it be safe to check the dirty bit before add_to_swap() and skip clean pages?

That would work, but I don't see an easy/clean way to check the dirty bit. Since the race is rare, I think this optimiztion isn't worthy.

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>

Acked-by on v3 2/2
#8 lore raw

Re: [PATCH V3 2/2] mm: fix data corruption caused by lazyfree page

In reply to Shaohua Li
Quoted 17 lines · Shaohua Li

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 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. As Minchan suggested, simply dirty the page in add_to_swap can do the job.

Quoted 3 lines

Fix: 802a3a92ad7a(mm: reclaim MADV_FREE pages)

--
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>