Skip to contentSkip to conversation

mm, swap: Fix swapoff with KSM pages

PATCH Applied Huang Ying 3 participants 4 messages Dec 26, 2018 – Dec 28, 2018

Subsystems (2)
lore
Share & tools
lore

Huang Ying

author
#0 lore raw

PATCH 1/1 mm, swap: Fix swapoff with KSM pages

KSM pages may be mapped to the multiple VMAs that cannot be reached from one anon_vma. So during swapin, a new copy of the page need to be generated if a different anon_vma is needed, please refer to comments of ksm_might_need_to_copy() for details.

During swapoff, unuse_vma() uses anon_vma (if available) to locate VMA and virtual address mapped to the page, so not all mappings to a swapped out KSM page could be found. So in try_to_unuse(), even if the swap count of a swap entry isn't zero, the page needs to be deleted from swap cache, so that, in the next round a new page could be allocated and swapin for the other mappings of the swapped out KSM page.

But this contradicts with the THP swap support. Where the THP could be deleted from swap cache only after the swap count of every swap entry in the huge swap cluster backing the THP has reach 0. So try_to_unuse() is changed in commit e07098294adf ("mm, THP, swap:
support to reclaim swap space for THP swapped out") to check that before delete a page from swap cache, but this has broken KSM swapoff too.

Fortunately, KSM is for the normal pages only, so the original behavior for KSM pages could be restored easily via checking PageTransCompound(). That is how this patch works.

Fixes e07098294adf ("mm, THP, swap: support to reclaim swap space for THP swapped out")
Reported-and-Tested-and-Acked-by Hugh Dickins <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Shaohua Li <[email protected]>
Cc Daniel Jordan <[email protected]>
---
 mm/swapfile.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/swapfile.c +2 -1
index 8688ae65ef58..20d3c0f47a5f 100644
@@ -2197,7 +2197,8 @@ int try_to_unuse(unsigned int type, bool frontswap,
 		 */
 		if (PageSwapCache(page) &&
 		    likely(page_private(page) == entry.val) &&
-		    !page_swapped(page))
+		    (!PageTransCompound(page) ||
+		     !swap_page_trans_huge_swapped(si, entry)))
 			delete_from_swap_cache(compound_head(page));
 
 		/*
Signature · 1 line
2.19.2
#1 lore raw

Hi, Andrew,

This patch is based on linus' tree instead of the head of mmotm tree because it is to fix a bug there.

The bug is introduced by commit e07098294adf ("mm, THP, swap: support to reclaim swap space for THP swapped out"), which is merged by v4.14-rc1. So I think we should backport the fix to from 4.14 on. But Hugh thinks it may be rare for the KSM pages being in the swap device when swapoff, so nobody reports the bug so far.

Best Regards,
Huang, Ying

Quoted 51 lines · Huang Ying

KSM pages may be mapped to the multiple VMAs that cannot be reached from one anon_vma. So during swapin, a new copy of the page need to be generated if a different anon_vma is needed, please refer to comments of ksm_might_need_to_copy() for details.

During swapoff, unuse_vma() uses anon_vma (if available) to locate VMA and virtual address mapped to the page, so not all mappings to a swapped out KSM page could be found. So in try_to_unuse(), even if the swap count of a swap entry isn't zero, the page needs to be deleted from swap cache, so that, in the next round a new page could be allocated and swapin for the other mappings of the swapped out KSM page.

But this contradicts with the THP swap support. Where the THP could be deleted from swap cache only after the swap count of every swap entry in the huge swap cluster backing the THP has reach 0. So try_to_unuse() is changed in commit e07098294adf ("mm, THP, swap:
support to reclaim swap space for THP swapped out") to check that before delete a page from swap cache, but this has broken KSM swapoff too.

Fortunately, KSM is for the normal pages only, so the original behavior for KSM pages could be restored easily via checking PageTransCompound(). That is how this patch works.

Fixes e07098294adf ("mm, THP, swap: support to reclaim swap space for THP swapped out")
Reported-and-Tested-and-Acked-by Hugh Dickins <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Shaohua Li <[email protected]>
Cc Daniel Jordan <[email protected]>
---
 mm/swapfile.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/swapfile.c +2 -1
index 8688ae65ef58..20d3c0f47a5f 100644
@@ -2197,7 +2197,8 @@ int try_to_unuse(unsigned int type, bool frontswap,
 		 */
 		if (PageSwapCache(page) &&
 		    likely(page_private(page) == entry.val) &&
-		    !page_swapped(page))
+		    (!PageTransCompound(page) ||
+		     !swap_page_trans_huge_swapped(si, entry)))
 			delete_from_swap_cache(compound_head(page));
 
 		/*
maintainer
#2 lore raw
In reply to Huang Ying
Quoted 30 lines · On Wed, 26 Dec 2018 13:15:22 +0800 Huang Ying <[email protected]> wrote:

KSM pages may be mapped to the multiple VMAs that cannot be reached from one anon_vma. So during swapin, a new copy of the page need to be generated if a different anon_vma is needed, please refer to comments of ksm_might_need_to_copy() for details.

During swapoff, unuse_vma() uses anon_vma (if available) to locate VMA and virtual address mapped to the page, so not all mappings to a swapped out KSM page could be found. So in try_to_unuse(), even if the swap count of a swap entry isn't zero, the page needs to be deleted from swap cache, so that, in the next round a new page could be allocated and swapin for the other mappings of the swapped out KSM page.

But this contradicts with the THP swap support. Where the THP could be deleted from swap cache only after the swap count of every swap entry in the huge swap cluster backing the THP has reach 0. So try_to_unuse() is changed in commit e07098294adf ("mm, THP, swap:
support to reclaim swap space for THP swapped out") to check that before delete a page from swap cache, but this has broken KSM swapoff too.

Fortunately, KSM is for the normal pages only, so the original behavior for KSM pages could be restored easily via checking PageTransCompound(). That is how this patch works.

...

mm/swapfile.c int try_to_unuse(unsigned int type, bool frontswap, · line 2203

Quoted 8 lines
mm/swapfile.c +2 -1
@@ -2197,7 +2197,8 @@ int try_to_unuse(unsigned int type, bool frontswap,
 		 */
 		if (PageSwapCache(page) &&
 		    likely(page_private(page) == entry.val) &&
-		    !page_swapped(page))
+		    (!PageTransCompound(page) ||
+		     !swap_page_trans_huge_swapped(si, entry)))
 			delete_from_swap_cache(compound_head(page));
 

The patch "mm, swap: rid swapoff of quadratic complexity" changes this code significantly. There are a few issues with that patch so I'll drop it for now.

Vineeth, please ensure that future versions retain the above fix, thanks.

#3 lore raw

Thanks for letting me know Andrew! I shall include all the fixes in the next iteration.

Thanks,
Vineeth

Quoted 51 lines · Andrew Morton
Quoted 30 lines · On Wed, 26 Dec 2018 13:15:22 +0800 Huang Ying <[email protected]> wrote:

KSM pages may be mapped to the multiple VMAs that cannot be reached from one anon_vma. So during swapin, a new copy of the page need to be generated if a different anon_vma is needed, please refer to comments of ksm_might_need_to_copy() for details.

During swapoff, unuse_vma() uses anon_vma (if available) to locate VMA and virtual address mapped to the page, so not all mappings to a swapped out KSM page could be found. So in try_to_unuse(), even if the swap count of a swap entry isn't zero, the page needs to be deleted from swap cache, so that, in the next round a new page could be allocated and swapin for the other mappings of the swapped out KSM page.

But this contradicts with the THP swap support. Where the THP could be deleted from swap cache only after the swap count of every swap entry in the huge swap cluster backing the THP has reach 0. So try_to_unuse() is changed in commit e07098294adf ("mm, THP, swap:
support to reclaim swap space for THP swapped out") to check that before delete a page from swap cache, but this has broken KSM swapoff too.

Fortunately, KSM is for the normal pages only, so the original behavior for KSM pages could be restored easily via checking PageTransCompound(). That is how this patch works.

...

mm/swapfile.c int try_to_unuse(unsigned int type, bool frontswap, · line 2203

Quoted 8 lines
mm/swapfile.c +2 -1
@@ -2197,7 +2197,8 @@ int try_to_unuse(unsigned int type, bool frontswap,
               */
              if (PageSwapCache(page) &&
                  likely(page_private(page) == entry.val) &&
-                 !page_swapped(page))
+                 (!PageTransCompound(page) ||
+                  !swap_page_trans_huge_swapped(si, entry)))
                      delete_from_swap_cache(compound_head(page));
 

The patch "mm, swap: rid swapoff of quadratic complexity" changes this code significantly. There are a few issues with that patch so I'll drop it for now.

Vineeth, please ensure that future versions retain the above fix, thanks.