Skip to contentSkip to conversation

mm: swap: avoid to writepage when a page is !PageSwapCache

PATCH In review Liu Ping Fan 2 participants 3 messages Jul 2, 2014

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

PATCH 1/1 mm: swap: avoid to writepage when a page is !PageSwapCache

There is race between do_swap_page() and swap_writepage(), if do_swap_page() had deleted a page from swap cache, there is no need to write it. So changing the ret of try_to_free_swap() to make swap_writepage() aware of this scene.

---
 mm/swapfile.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/swapfile.c +1 -1
index 4c524f7..9d80671 100644
@@ -910,7 +910,7 @@ int try_to_free_swap(struct page *page)
 	VM_BUG_ON_PAGE(!PageLocked(page), page);
 
 	if (!PageSwapCache(page))
-		return 0;
+		return -1;
 	if (PageWriteback(page))
 		return 0;
 	if (page_swapcount(page))
Signature · 7 lines
1.8.1.4

--
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>
#1 lore raw
Quoted 4 lines · Liu Ping Fan

There is race between do_swap_page() and swap_writepage(), if do_swap_page() had deleted a page from swap cache, there is no need to write it. So changing the ret of try_to_free_swap() to make swap_writepage() aware of this scene.

Is this an inefficiency that you have noticed in practice, or something that you think you spotted by code inspection?

I don't see how it can happen: all the places I know of that call swap_writepage() (including vmscan.c's mapping->a_ops->writepage) have not dropped page lock since setting or checking PageSwapCache, and page lock is supposed to protect against deletion from swap cache.

Has that changed? Please point out where.

Quoted 11 lines
---
 mm/swapfile.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/swapfile.c +0 -0
index 4c524f7..9d80671 100644

mm/swapfile.c int try_to_free_swap(struct page *page) · line 913

Quoted 5 lines
mm/swapfile.c +1 -1
@@ -910,7 +910,7 @@ int try_to_free_swap(struct page *page)
 	VM_BUG_ON_PAGE(!PageLocked(page), page);
 
 	if (!PageSwapCache(page))
-		return 0;
+		return -1;

Previously it returned either 0 or 1, which is what __try_to_reclaim_swap() says it returns; so better to stick to 0 or 1, unless you have good reason to add a distinct value.

It's true that by the time __try_to_reclaim_swap() has got the page lock, the page might have been removed from swap cache, and we could then treat that as swap_was_freed (even though it was not freed by the caller).

But it's a very narrow window, and no great advantage to do so:
I don't think it's worth changing try_to_free_swap() semantics for, but you could persuade us.

Hugh

Quoted 5 lines
mm/swapfile.c +0 -0
 	if (PageWriteback(page))
 		return 0;
 	if (page_swapcount(page))
Signature · 1 line
1.8.1.4

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

#2 lore raw
Quoted 10 lines · Hugh Dickins
Quoted 4 lines · Liu Ping Fan

There is race between do_swap_page() and swap_writepage(), if do_swap_page() had deleted a page from swap cache, there is no need to write it. So changing the ret of try_to_free_swap() to make swap_writepage() aware of this scene.

Is this an inefficiency that you have noticed in practice, or something that you think you spotted by code inspection?

just spotted by code inspection.

Quoted 7 lines

I don't see how it can happen: all the places I know of that call swap_writepage() (including vmscan.c's mapping->a_ops->writepage) have not dropped page lock since setting or checking PageSwapCache, and page lock is supposed to protect against deletion from swap cache.

Has that changed? Please point out where.

No, my fault. Thanks for making me aware of this.

Quoted 21 lines
Quoted 11 lines
---
 mm/swapfile.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/swapfile.c +0 -0
index 4c524f7..9d80671 100644

mm/swapfile.c int try_to_free_swap(struct page *page) · line 913

Quoted 5 lines
mm/swapfile.c +1 -1
@@ -910,7 +910,7 @@ int try_to_free_swap(struct page *page)
      VM_BUG_ON_PAGE(!PageLocked(page), page);
 
      if (!PageSwapCache(page))
-             return 0;
+             return -1;

Previously it returned either 0 or 1, which is what __try_to_reclaim_swap() says it returns; so better to stick to 0 or 1, unless you have good reason to add a distinct value.

Quoted 8 lines

It's true that by the time __try_to_reclaim_swap() has got the page lock, the page might have been removed from swap cache, and we could then treat that as swap_was_freed (even though it was not freed by the caller).

But it's a very narrow window, and no great advantage to do so:
I don't think it's worth changing try_to_free_swap() semantics for, but you could persuade us.

Got it, and it is meaningless to do that.

Thanks,
Fan

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