Skip to contentSkip to conversation

mm, trivial: Simplify swap related code in try_to_unmap_one()

PATCH In review Huang, Ying 2 participants 5 messages Mar 31, 2020 – Apr 1, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm, trivial: Simplify swap related code in try_to_unmap_one()

From: Huang Ying <[email protected]>

Because PageSwapCache() will always return false if PageSwapBacked() returns false, and PageSwapBacked() will be check for MADV_FREE pages in try_to_unmap_one(). The swap related code in try_to_unmap_one() can be simplified to improve the readability.

Cc Michal Hocko <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Rik van Riel <[email protected]>
---
 mm/rmap.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)
mm/rmap.c +14 -13
index 2126fd4a254b..cd3c406aeac7 100644
@@ -1613,19 +1613,6 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
 		} else if (PageAnon(page)) {
 			swp_entry_t entry = { .val = page_private(subpage) };
 			pte_t swp_pte;
-			/*
-			 * Store the swap location in the pte.
-			 * See handle_pte_fault() ...
-			 */
-			if (unlikely(PageSwapBacked(page) != PageSwapCache(page))) {
-				WARN_ON_ONCE(1);
-				ret = false;
-				/* We have to invalidate as we cleared the pte */
-				mmu_notifier_invalidate_range(mm, address,
-							address + PAGE_SIZE);
-				page_vma_mapped_walk_done(&pvmw);
-				break;
-			}
 
 			/* MADV_FREE page check */
 			if (!PageSwapBacked(page)) {
@@ -1648,6 +1635,20 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
 				break;
 			}
 
+			/*
+			 * Store the swap location in the pte.
+			 * See handle_pte_fault() ...
+			 */
+			if (unlikely(!PageSwapCache(page))) {
+				WARN_ON_ONCE(1);
+				ret = false;
+				/* We have to invalidate as we cleared the pte */
+				mmu_notifier_invalidate_range(mm, address,
+							address + PAGE_SIZE);
+				page_vma_mapped_walk_done(&pvmw);
+				break;
+			}
+
 			if (swap_duplicate(entry) < 0) {
 				set_pte_at(mm, address, pvmw.pte, pteval);
 				ret = false;
Signature · 1 line
2.25.0
#1 lore raw
Quoted 6 lines · Ying

From: Huang Ying <[email protected]>

Because PageSwapCache() will always return false if PageSwapBacked() returns false, and PageSwapBacked() will be check for MADV_FREE pages in try_to_unmap_one(). The swap related code in try_to_unmap_one() can be simplified to improve the readability.

My understanding is that this is a sanity check to let us know if something breaks. Do we really want to get rid of it? Maybe it is not really useful but if that is the case then the changelog should reflect this fact.

Quoted 56 lines
Cc Michal Hocko <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Rik van Riel <[email protected]>
---
 mm/rmap.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)
mm/rmap.c +14 -13
index 2126fd4a254b..cd3c406aeac7 100644
@@ -1613,19 +1613,6 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
 		} else if (PageAnon(page)) {
 			swp_entry_t entry = { .val = page_private(subpage) };
 			pte_t swp_pte;
-			/*
-			 * Store the swap location in the pte.
-			 * See handle_pte_fault() ...
-			 */
-			if (unlikely(PageSwapBacked(page) != PageSwapCache(page))) {
-				WARN_ON_ONCE(1);
-				ret = false;
-				/* We have to invalidate as we cleared the pte */
-				mmu_notifier_invalidate_range(mm, address,
-							address + PAGE_SIZE);
-				page_vma_mapped_walk_done(&pvmw);
-				break;
-			}
 
 			/* MADV_FREE page check */
 			if (!PageSwapBacked(page)) {
@@ -1648,6 +1635,20 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
 				break;
 			}
 
+			/*
+			 * Store the swap location in the pte.
+			 * See handle_pte_fault() ...
+			 */
+			if (unlikely(!PageSwapCache(page))) {
+				WARN_ON_ONCE(1);
+				ret = false;
+				/* We have to invalidate as we cleared the pte */
+				mmu_notifier_invalidate_range(mm, address,
+							address + PAGE_SIZE);
+				page_vma_mapped_walk_done(&pvmw);
+				break;
+			}
+
 			if (swap_duplicate(entry) < 0) {
 				set_pte_at(mm, address, pvmw.pte, pteval);
 				ret = false;
Signature · 1 line
2.25.0
Signature · 2 lines
Michal Hocko
SUSE Labs
#2 lore raw
Quoted 8 lines · Michal Hocko
Quoted 6 lines · Ying

From: Huang Ying <[email protected]>

Because PageSwapCache() will always return false if PageSwapBacked() returns false, and PageSwapBacked() will be check for MADV_FREE pages in try_to_unmap_one(). The swap related code in try_to_unmap_one() can be simplified to improve the readability.

Quoted 4 lines

My understanding is that this is a sanity check to let us know if something breaks. Do we really want to get rid of it? Maybe it is not really useful but if that is the case then the changelog should reflect this fact.

Now the definition of PageSwapCache() is,

static __always_inline int PageSwapCache(struct page *page) {

#ifdef CONFIG_THP_SWAP
	page = compound_head(page);
#endif
	return PageSwapBacked(page) && test_bit(PG_swapcache, &page->flags);
}

So, if PageSwapBacked() returns false, PageSwapCache() will always return false. The original checking,

- if (unlikely(PageSwapBacked(page) != PageSwapCache(page))) {

is equivalent to

- if (unlikely(PageSwapBacked(page) && !PageSwapCache(page))) {

Then what is the check !PageSwapBacked() && PageSwapCache() for? To prevent someone to change the definition of PageSwapCache() in the future to break this?

Best Regards,
Huang, Ying

#3 lore raw

On Wed 01-04-20 09:11:13, Huang, Ying wrote:
[...]

Quoted 3 lines

Then what is the check !PageSwapBacked() && PageSwapCache() for? To prevent someone to change the definition of PageSwapCache() in the future to break this?

Yes this is my understading. It is essentially an assert that enforces the assumption about swap cache vs. swap backed being coupled.

Signature · 2 lines
Michal Hocko
SUSE Labs
#4 lore raw
Quoted 8 lines · Michal Hocko

On Wed 01-04-20 09:11:13, Huang, Ying wrote:
[...]

Quoted 3 lines

Then what is the check !PageSwapBacked() && PageSwapCache() for? To prevent someone to change the definition of PageSwapCache() in the future to break this?

Yes this is my understading. It is essentially an assert that enforces the assumption about swap cache vs. swap backed being coupled.

OK. Then we can just keep the current code.

Best Regards,
Huang, Ying