Skip to contentSkip to conversation

mm: remove swapcache page early

RFC In review Minchan Kim 8 participants 25 messages Mar 27, 2013 – Apr 8, 2013

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

RFC 1/1 mm: remove swapcache page early

Swap subsystem does lazy swap slot free with expecting the page would be swapped out again so we can't avoid unnecessary write.

But the problem in in-memory swap is that it consumes memory space until vm_swap_full(ie, used half of all of swap device) condition meet. It could be bad if we use multiple swap device, small in-memory swap and big storage swap or in-memory swap alone.

This patch changes vm_swap_full logic slightly so it could free swap slot early if the backed device is really fast.
For it, I used SWP_SOLIDSTATE but It might be controversial. So let's add Ccing Shaohua and Hugh.
If it's a problem for SSD, I'd like to create new type SWP_INMEMORY or something for z* family.

Other problem is zram is block device so that it can set SWP_INMEMORY or SWP_SOLIDSTATE easily(ie, actually, zram is already done) but I have no idea to use it for frontswap.

Any idea?

Other optimize point is we remove it unconditionally when we found it's exclusive when swap in happen.
It could help frontswap family, too.
What do you think about it?

Cc Hugh Dickins <[email protected]>
Cc Dan Magenheimer <[email protected]>
Cc Seth Jennings <[email protected]>
Cc Nitin Gupta <[email protected]>
Cc Konrad Rzeszutek Wilk <[email protected]>
Cc Shaohua Li <[email protected]>
---
 include/linux/swap.h | 11 ++++++++---
 mm/memory.c          |  3 ++-
 mm/swapfile.c        | 11 +++++++----
 mm/vmscan.c          |  2 +-
 4 files changed, 18 insertions(+), 9 deletions(-)
include/linux/swap.h +8 -3
index 2818a12..1f4df66 100644
@@ -359,9 +359,14 @@ extern struct page *swapin_readahead(swp_entry_t, gfp_t,
 extern atomic_long_t nr_swap_pages;
 extern long total_swap_pages;
 
-/* Swap 50% full? Release swapcache more aggressively.. */
-static inline bool vm_swap_full(void)
+/*
+ * Swap 50% full or fast backed device?
+ * Release swapcache more aggressively.
+ */
+static inline bool vm_swap_full(struct swap_info_struct *si)
 {
+	if (si->flags & SWP_SOLIDSTATE)
+		return true;
 	return atomic_long_read(&nr_swap_pages) * 2 < total_swap_pages;
 }
 
@@ -405,7 +410,7 @@ mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
 #define get_nr_swap_pages()			0L
 #define total_swap_pages			0L
 #define total_swapcache_pages()			0UL
-#define vm_swap_full()				0
+#define vm_swap_full(si)			0
 
 #define si_swapinfo(val) \
 	do { (val)->freeswap = (val)->totalswap = 0; } while (0)
mm/memory.c +2 -1
index 705473a..1ca21a9 100644
@@ -3084,7 +3084,8 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
 	mem_cgroup_commit_charge_swapin(page, ptr);
 
 	swap_free(entry);
-	if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
+	if (likely(PageSwapCache(page)) && (vm_swap_full(page_swap_info(page))
+			|| (vma->vm_flags & VM_LOCKED) || PageMlocked(page)))
 		try_to_free_swap(page);
 	unlock_page(page);
 	if (page != swapcache) {
mm/swapfile.c +7 -4
index 1bee6fa..f9cc701 100644
@@ -293,7 +293,7 @@ checks:
 		scan_base = offset = si->lowest_bit;
 
 	/* reuse swap entry of cache-only swap if not busy. */
-	if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+	if (vm_swap_full(si) && si->swap_map[offset] == SWAP_HAS_CACHE) {
 		int swap_was_freed;
 		spin_unlock(&si->lock);
 		swap_was_freed = __try_to_reclaim_swap(si, offset);
@@ -382,7 +382,8 @@ scan:
 			spin_lock(&si->lock);
 			goto checks;
 		}
-		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+		if (vm_swap_full(si) &&
+			si->swap_map[offset] == SWAP_HAS_CACHE) {
 			spin_lock(&si->lock);
 			goto checks;
 		}
@@ -397,7 +398,8 @@ scan:
 			spin_lock(&si->lock);
 			goto checks;
 		}
-		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+		if (vm_swap_full(si) &&
+			si->swap_map[offset] == SWAP_HAS_CACHE) {
 			spin_lock(&si->lock);
 			goto checks;
 		}
@@ -763,7 +765,8 @@ int free_swap_and_cache(swp_entry_t entry)
 		 * Also recheck PageSwapCache now page is locked (above).
 		 */
 		if (PageSwapCache(page) && !PageWriteback(page) &&
-				(!page_mapped(page) || vm_swap_full())) {
+				(!page_mapped(page) ||
+				  vm_swap_full(page_swap_info(page)))) {
 			delete_from_swap_cache(page);
 			SetPageDirty(page);
 		}
mm/vmscan.c +1 -1
index df78d17..145c59c 100644
@@ -933,7 +933,7 @@ cull_mlocked:
 
 activate_locked:
 		/* Not a candidate for swapping, so reclaim swap space. */
-		if (PageSwapCache(page) && vm_swap_full())
+		if (PageSwapCache(page) && vm_swap_full(page_swap_info(page)))
 			try_to_free_swap(page);
 		VM_BUG_ON(PageActive(page));
 		SetPageActive(page);
Signature · 7 lines
1.8.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>
#1 lore raw

Hi,

Quoted 8 lines · Minchan Kim

Swap subsystem does lazy swap slot free with expecting the page would be swapped out again so we can't avoid unnecessary write.

But the problem in in-memory swap is that it consumes memory space until vm_swap_full(ie, used half of all of swap device) condition meet. It could be bad if we use multiple swap device, small in-memory swap and big storage swap or in-memory swap alone.

Quoted 6 lines

This patch changes vm_swap_full logic slightly so it could free swap slot early if the backed device is really fast.
For it, I used SWP_SOLIDSTATE but It might be controversial. So let's add Ccing Shaohua and Hugh.
If it's a problem for SSD, I'd like to create new type SWP_INMEMORY or something for z* family.

I perfer to add new SWP_INMEMORY for z* family. as you know SSD and memory is different characteristics.
and if new type is added, it doesn't need to modify lots of codes.

Do you have any data for it? do you get meaningful performance gain or efficiency of z* family? If yes, please share it.

Thank you,
Kyungmin Park

Quoted 129 lines

Other problem is zram is block device so that it can set SWP_INMEMORY or SWP_SOLIDSTATE easily(ie, actually, zram is already done) but I have no idea to use it for frontswap.

Any idea?

Other optimize point is we remove it unconditionally when we found it's exclusive when swap in happen.
It could help frontswap family, too.
What do you think about it?

Cc Hugh Dickins <[email protected]>
Cc Dan Magenheimer <[email protected]>
Cc Seth Jennings <[email protected]>
Cc Nitin Gupta <[email protected]>
Cc Konrad Rzeszutek Wilk <[email protected]>
Cc Shaohua Li <[email protected]>
---
 include/linux/swap.h | 11 ++++++++---
 mm/memory.c          |  3 ++-
 mm/swapfile.c        | 11 +++++++----
 mm/vmscan.c          |  2 +-
 4 files changed, 18 insertions(+), 9 deletions(-)
include/linux/swap.h +8 -3
index 2818a12..1f4df66 100644
@@ -359,9 +359,14 @@ extern struct page *swapin_readahead(swp_entry_t, gfp_t,
 extern atomic_long_t nr_swap_pages;
 extern long total_swap_pages;
 
-/* Swap 50% full? Release swapcache more aggressively.. */
-static inline bool vm_swap_full(void)
+/*
+ * Swap 50% full or fast backed device?
+ * Release swapcache more aggressively.
+ */
+static inline bool vm_swap_full(struct swap_info_struct *si)
 {
+       if (si->flags & SWP_SOLIDSTATE)
+               return true;
        return atomic_long_read(&nr_swap_pages) * 2 < total_swap_pages;
 }
 
@@ -405,7 +410,7 @@ mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
 #define get_nr_swap_pages()                    0L
 #define total_swap_pages                       0L
 #define total_swapcache_pages()                        0UL
-#define vm_swap_full()                         0
+#define vm_swap_full(si)                       0
 
 #define si_swapinfo(val) \
        do { (val)->freeswap = (val)->totalswap = 0; } while (0)
mm/memory.c +2 -1
index 705473a..1ca21a9 100644
@@ -3084,7 +3084,8 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
        mem_cgroup_commit_charge_swapin(page, ptr);
 
        swap_free(entry);
-       if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
+       if (likely(PageSwapCache(page)) && (vm_swap_full(page_swap_info(page))
+                       || (vma->vm_flags & VM_LOCKED) || PageMlocked(page)))
                try_to_free_swap(page);
        unlock_page(page);
        if (page != swapcache) {
mm/swapfile.c +7 -4
index 1bee6fa..f9cc701 100644
@@ -293,7 +293,7 @@ checks:
                scan_base = offset = si->lowest_bit;
 
        /* reuse swap entry of cache-only swap if not busy. */
-       if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+       if (vm_swap_full(si) && si->swap_map[offset] == SWAP_HAS_CACHE) {
                int swap_was_freed;
                spin_unlock(&si->lock);
                swap_was_freed = __try_to_reclaim_swap(si, offset);
@@ -382,7 +382,8 @@ scan:
                        spin_lock(&si->lock);
                        goto checks;
                }
-               if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+               if (vm_swap_full(si) &&
+                       si->swap_map[offset] == SWAP_HAS_CACHE) {
                        spin_lock(&si->lock);
                        goto checks;
                }
@@ -397,7 +398,8 @@ scan:
                        spin_lock(&si->lock);
                        goto checks;
                }
-               if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+               if (vm_swap_full(si) &&
+                       si->swap_map[offset] == SWAP_HAS_CACHE) {
                        spin_lock(&si->lock);
                        goto checks;
                }
@@ -763,7 +765,8 @@ int free_swap_and_cache(swp_entry_t entry)
                 * Also recheck PageSwapCache now page is locked (above).
                 */
                if (PageSwapCache(page) && !PageWriteback(page) &&
-                               (!page_mapped(page) || vm_swap_full())) {
+                               (!page_mapped(page) ||
+                                 vm_swap_full(page_swap_info(page)))) {
                        delete_from_swap_cache(page);
                        SetPageDirty(page);
                }
mm/vmscan.c +1 -2
index df78d17..145c59c 100644
@@ -933,7 +933,7 @@ cull_mlocked:
 
 activate_locked:
                /* Not a candidate for swapping, so reclaim swap space. */
-               if (PageSwapCache(page) && vm_swap_full())
+               if (PageSwapCache(page) && vm_swap_full(page_swap_info(page)))
                        try_to_free_swap(page);
                VM_BUG_ON(PageActive(page));
                SetPageActive(page);
--

1.8.2

Quoted 5 lines

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

--
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
In reply to Minchan Kim
Quoted 15 lines · Minchan Kim

Swap subsystem does lazy swap slot free with expecting the page would be swapped out again so we can't avoid unnecessary write.

But the problem in in-memory swap is that it consumes memory space until vm_swap_full(ie, used half of all of swap device) condition meet. It could be bad if we use multiple swap device, small in-memory swap and big storage swap or in-memory swap alone.

This patch changes vm_swap_full logic slightly so it could free swap slot early if the backed device is really fast.
For it, I used SWP_SOLIDSTATE but It might be controversial. So let's add Ccing Shaohua and Hugh.
If it's a problem for SSD, I'd like to create new type SWP_INMEMORY or something for z* family.

Quoted 6 lines

Other problem is zram is block device so that it can set SWP_INMEMORY or SWP_SOLIDSTATE easily(ie, actually, zram is already done) but I have no idea to use it for frontswap.

Any idea?

Another thinking....in what case, in what system configuration, vm_swap_full() should return false and delay swp_entry freeing ?

Thanks,
-Kame

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

#3 lore raw

Hi Kame,

Quoted 16 lines · Kamezawa Hiroyuki
Quoted 15 lines · Minchan Kim

Swap subsystem does lazy swap slot free with expecting the page would be swapped out again so we can't avoid unnecessary write.

But the problem in in-memory swap is that it consumes memory space until vm_swap_full(ie, used half of all of swap device) condition meet. It could be bad if we use multiple swap device, small in-memory swap and big storage swap or in-memory swap alone.

This patch changes vm_swap_full logic slightly so it could free swap slot early if the backed device is really fast.
For it, I used SWP_SOLIDSTATE but It might be controversial. So let's add Ccing Shaohua and Hugh.
If it's a problem for SSD, I'd like to create new type SWP_INMEMORY or something for z* family.

Quoted 8 lines
Quoted 6 lines

Other problem is zram is block device so that it can set SWP_INMEMORY or SWP_SOLIDSTATE easily(ie, actually, zram is already done) but I have no idea to use it for frontswap.

Any idea?

Another thinking....in what case, in what system configuration, vm_swap_full() should return false and delay swp_entry freeing ?

It's a really good question I had have in mind from long time ago. If I catch your point properly, your question is "Couldn't we remove vm_swap_full logic?"

If so, the answer is "I have no idea and would like to ask it to Hugh".

Academically, it does make sense swap-out page is unlikely to be working set so it could be swap out again and I believe it was merged since we had the workload could be enhanced by the logic at that time.

And I think it's not easy to prove it's useless thesedays because I couldn't have all recent workloads over the world so I'd like to avoid such adventure. :)

Thanks.

Quoted 10 lines

Thanks,
-Kame

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

Signature · 8 lines
Kind regards,
Minchan Kim

--
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>
#4 lore raw
In reply to Minchan Kim
Quoted 10 lines · Minchan Kim

Swap subsystem does lazy swap slot free with expecting the page would be swapped out again so we can't avoid unnecessary write.

But the problem in in-memory swap is that it consumes memory space until vm_swap_full(ie, used half of all of swap device) condition meet. It could be bad if we use multiple swap device, small in-memory swap and big storage swap or in-memory swap alone.

This patch changes vm_swap_full logic slightly so it could free swap slot early if the backed device is really fast.

Great idea!

Quoted 1 line

For it, I used SWP_SOLIDSTATE but It might be controversial.

The comment for SWP_SOLIDSTATE is that "blkdev seeks are cheap". Just because seeks are cheap doesn't mean the read itself is also cheap. For example, QUEUE_FLAG_NONROT is set for mmc devices, but some of them can be pretty slow.

Quoted 3 lines

So let's add Ccing Shaohua and Hugh.
If it's a problem for SSD, I'd like to create new type SWP_INMEMORY or something for z* family.

Afaict, setting SWP_SOLIDSTATE depends on characteristics of the underlying block device (i.e. blk_queue_nonrot()). zram is a block device but zcache and zswap are not.

Any idea by what criteria SWP_INMEMORY would be set?

Also, frontswap backends (zcache and zswap) are a caching layer on top of the real swap device, which might actually be rotating media. So you have the issue of to different characteristics, in-memory caching on top of rotation media, present in a single swap device.

Thanks,
Seth

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

#5 lore raw

Hi Seth,

Quoted 9 lines · Seth Jennings
Quoted 8 lines · Minchan Kim

Swap subsystem does lazy swap slot free with expecting the page would be swapped out again so we can't avoid unnecessary write.

But the problem in in-memory swap is that it consumes memory space until vm_swap_full(ie, used half of all of swap device) condition meet. It could be bad if we use multiple swap device, small in-memory swap and big storage swap or in-memory swap alone.

Quoted 4 lines
Quoted 2 lines

This patch changes vm_swap_full logic slightly so it could free swap slot early if the backed device is really fast.

Great idea!

Thanks!

Quoted 5 lines
Quoted 2 lines

For it, I used SWP_SOLIDSTATE but It might be controversial.

The comment for SWP_SOLIDSTATE is that "blkdev seeks are cheap". Just because seeks are cheap doesn't mean the read itself is also cheap.

The "read" isn't not concern but "write".

Quoted 2 lines

For example, QUEUE_FLAG_NONROT is set for mmc devices, but some of them can be pretty slow.

Yeb.

Quoted 10 lines
Quoted 4 lines

So let's add Ccing Shaohua and Hugh.
If it's a problem for SSD, I'd like to create new type SWP_INMEMORY or something for z* family.

Afaict, setting SWP_SOLIDSTATE depends on characteristics of the underlying block device (i.e. blk_queue_nonrot()). zram is a block device but zcache and zswap are not.

Any idea by what criteria SWP_INMEMORY would be set?

Just in-memory swap, zram, zswap and zcache at the moment. :)

Quoted 5 lines

Also, frontswap backends (zcache and zswap) are a caching layer on top of the real swap device, which might actually be rotating media. So you have the issue of to different characteristics, in-memory caching on top of rotation media, present in a single swap device.

Please read my patch completely. I already pointed out the problem and Hugh and Dan are suggesting ideas.

Thanks!

Quoted 9 lines

Thanks,
Seth

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

Signature · 8 lines
Kind regards,
Minchan Kim

--
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
In reply to Minchan Kim
Quoted 22 lines

From: Minchan Kim [mailto:[email protected]]
Subject: [RFC] mm: remove swapcache page early

Swap subsystem does lazy swap slot free with expecting the page would be swapped out again so we can't avoid unnecessary write.

But the problem in in-memory swap is that it consumes memory space until vm_swap_full(ie, used half of all of swap device) condition meet. It could be bad if we use multiple swap device, small in-memory swap and big storage swap or in-memory swap alone.

This patch changes vm_swap_full logic slightly so it could free swap slot early if the backed device is really fast.
For it, I used SWP_SOLIDSTATE but It might be controversial. So let's add Ccing Shaohua and Hugh.
If it's a problem for SSD, I'd like to create new type SWP_INMEMORY or something for z* family.

Other problem is zram is block device so that it can set SWP_INMEMORY or SWP_SOLIDSTATE easily(ie, actually, zram is already done) but I have no idea to use it for frontswap.

Quoted 5 lines

Any idea?

Other optimize point is we remove it unconditionally when we found it's exclusive when swap in happen.
It could help frontswap family, too.

By passing a struct page * to vm_swap_full() you can then call frontswap_test()... if it returns true, then vm_swap_full can return true. Note that this precisely checks whether the page is in zcache/zswap or not, so Seth's concern that some pages may be in-memory and some may be in rotating storage is no longer an issue.

Quoted 1 line

What do you think about it?

By removing the page from swapcache, you are now increasing the risk that pages will "thrash" between uncompressed state (in swapcache) and compressed state (in z*). I think this is a better tradeoff though than keeping a copy of both the compressed page AND the uncompressed page in memory.

You should probably rename vm_swap_full() because you are now overloading it with other meanings. Maybe vm_swap_reclaimable()?

Do you have any measurements? I think you are correct that it may help a LOT.

Thanks,
Dan

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

#7 lore raw
In reply to Minchan Kim
Quoted 2 lines · Minchan Kim

Swap subsystem does lazy swap slot free with expecting the page would be swapped out again so we can't avoid unnecessary write.

so we can avoid unnecessary write.

Quoted 5 lines

But the problem in in-memory swap is that it consumes memory space until vm_swap_full(ie, used half of all of swap device) condition meet. It could be bad if we use multiple swap device, small in-memory swap and big storage swap or in-memory swap alone.

That is a very good realization: it's surprising that none of us thought of it before - no disrespect to you, well done, thank you.

And I guess swap readahead is utterly unhelpful in this case too.

Quoted 4 lines

This patch changes vm_swap_full logic slightly so it could free swap slot early if the backed device is really fast. For it, I used SWP_SOLIDSTATE but It might be controversial.

But I strongly disagree with almost everything in your patch :) I disagree with addressing it in vm_swap_full(), I disagree that it can be addressed by device, I disagree that it has anything to do with SWP_SOLIDSTATE.

This is not a problem with swapping to /dev/ram0 or to /dev/zram0, is it? In those cases, a fixed amount of memory has been set aside for swap, and it works out just like with disk block devices. The memory set aside may be wasted, but that is accepted upfront.

Similarly, this is not a problem with swapping to SSD. There might or might not be other reasons for adjusting the vm_swap_full() logic for SSD or generally, but those have nothing to do with this issue.

The problem here is peculiar to frontswap, and the variably sized memory behind it, isn't it? We are accustomed to using swap to free up memory by transferring its data to some other, cheaper but slower resource.

But in the case of frontswap and zmem (I'll say that to avoid thinking through which backends are actually involved), it is not a cheaper and slower resource, but the very same memory we are trying to save: swap is stolen from the memory under reclaim, so any duplication becomes counter-productive (if we ignore cpu compression/decompression costs:
I have no idea how fair it is to do so, but anyone who chooses zmem is prepared to pay some cpu price for that).

And because it's a frontswap thing, we cannot decide this by device:
frontswap may or may not stand in front of each device. There is no problem with swapcache duplicated on disk (until that area approaches being full or fragmented), but at the higher level we cannot see what is in zmem and what is on disk: we only want to free up the zmem dup.

I believe the answer is for frontswap/zmem to invalidate the frontswap copy of the page (to free up the compressed memory when possible) and SetPageDirty on the PageUptodate PageSwapCache page when swapping in (setting page dirty so nothing will later go to read it from the unfreed location on backing swap disk, which was never written).

We cannot rely on freeing the swap itself, because in general there may be multiple references to the swap, and we only satisfy the one which has faulted. It may or may not be a good idea to use rmap to locate the other places to insert pte in place of swap entry, to resolve them all at once; but we have chosen not to do so in the past, and there's no need for that, if the zmem gets invalidated and the swapcache page set dirty.

Hugh

Quoted 131 lines

So let's add Ccing Shaohua and Hugh.
If it's a problem for SSD, I'd like to create new type SWP_INMEMORY or something for z* family.

Other problem is zram is block device so that it can set SWP_INMEMORY or SWP_SOLIDSTATE easily(ie, actually, zram is already done) but I have no idea to use it for frontswap.

Any idea?

Other optimize point is we remove it unconditionally when we found it's exclusive when swap in happen.
It could help frontswap family, too.
What do you think about it?

Cc Hugh Dickins <[email protected]>
Cc Dan Magenheimer <[email protected]>
Cc Seth Jennings <[email protected]>
Cc Nitin Gupta <[email protected]>
Cc Konrad Rzeszutek Wilk <[email protected]>
Cc Shaohua Li <[email protected]>
---
 include/linux/swap.h | 11 ++++++++---
 mm/memory.c          |  3 ++-
 mm/swapfile.c        | 11 +++++++----
 mm/vmscan.c          |  2 +-
 4 files changed, 18 insertions(+), 9 deletions(-)
include/linux/swap.h +8 -3
index 2818a12..1f4df66 100644
@@ -359,9 +359,14 @@ extern struct page *swapin_readahead(swp_entry_t, gfp_t,
 extern atomic_long_t nr_swap_pages;
 extern long total_swap_pages;
 
-/* Swap 50% full? Release swapcache more aggressively.. */
-static inline bool vm_swap_full(void)
+/*
+ * Swap 50% full or fast backed device?
+ * Release swapcache more aggressively.
+ */
+static inline bool vm_swap_full(struct swap_info_struct *si)
 {
+	if (si->flags & SWP_SOLIDSTATE)
+		return true;
 	return atomic_long_read(&nr_swap_pages) * 2 < total_swap_pages;
 }
 
@@ -405,7 +410,7 @@ mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
 #define get_nr_swap_pages()			0L
 #define total_swap_pages			0L
 #define total_swapcache_pages()			0UL
-#define vm_swap_full()				0
+#define vm_swap_full(si)			0
 
 #define si_swapinfo(val) \
 	do { (val)->freeswap = (val)->totalswap = 0; } while (0)
mm/memory.c +2 -1
index 705473a..1ca21a9 100644
@@ -3084,7 +3084,8 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
 	mem_cgroup_commit_charge_swapin(page, ptr);
 
 	swap_free(entry);
-	if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
+	if (likely(PageSwapCache(page)) && (vm_swap_full(page_swap_info(page))
+			|| (vma->vm_flags & VM_LOCKED) || PageMlocked(page)))
 		try_to_free_swap(page);
 	unlock_page(page);
 	if (page != swapcache) {
mm/swapfile.c +7 -4
index 1bee6fa..f9cc701 100644
@@ -293,7 +293,7 @@ checks:
 		scan_base = offset = si->lowest_bit;
 
 	/* reuse swap entry of cache-only swap if not busy. */
-	if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+	if (vm_swap_full(si) && si->swap_map[offset] == SWAP_HAS_CACHE) {
 		int swap_was_freed;
 		spin_unlock(&si->lock);
 		swap_was_freed = __try_to_reclaim_swap(si, offset);
@@ -382,7 +382,8 @@ scan:
 			spin_lock(&si->lock);
 			goto checks;
 		}
-		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+		if (vm_swap_full(si) &&
+			si->swap_map[offset] == SWAP_HAS_CACHE) {
 			spin_lock(&si->lock);
 			goto checks;
 		}
@@ -397,7 +398,8 @@ scan:
 			spin_lock(&si->lock);
 			goto checks;
 		}
-		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+		if (vm_swap_full(si) &&
+			si->swap_map[offset] == SWAP_HAS_CACHE) {
 			spin_lock(&si->lock);
 			goto checks;
 		}
@@ -763,7 +765,8 @@ int free_swap_and_cache(swp_entry_t entry)
 		 * Also recheck PageSwapCache now page is locked (above).
 		 */
 		if (PageSwapCache(page) && !PageWriteback(page) &&
-				(!page_mapped(page) || vm_swap_full())) {
+				(!page_mapped(page) ||
+				  vm_swap_full(page_swap_info(page)))) {
 			delete_from_swap_cache(page);
 			SetPageDirty(page);
 		}
mm/vmscan.c +1 -1
index df78d17..145c59c 100644
@@ -933,7 +933,7 @@ cull_mlocked:
 
 activate_locked:
 		/* Not a candidate for swapping, so reclaim swap space. */
-		if (PageSwapCache(page) && vm_swap_full())
+		if (PageSwapCache(page) && vm_swap_full(page_swap_info(page)))
 			try_to_free_swap(page);
 		VM_BUG_ON(PageActive(page));
 		SetPageActive(page);
Signature · 1 line
1.8.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>

#8 lore raw
Quoted 9 lines

From: Hugh Dickins [mailto:[email protected]]
Subject: Re: [RFC] mm: remove swapcache page early

Quoted 2 lines · Minchan Kim

Swap subsystem does lazy swap slot free with expecting the page would be swapped out again so we can't avoid unnecessary write.

so we can avoid unnecessary write.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

But the problem in in-memory swap is that it consumes memory space until vm_swap_full(ie, used half of all of swap device) condition meet. It could be bad if we use multiple swap device, small in-memory swap and big storage swap or in-memory swap alone.

That is a very good realization: it's surprising that none of us thought of it before - no disrespect to you, well done, thank you.

Yes, my compliments also Minchan. This problem has been thought of before but this patch is the first to identify a possible solution.

Quoted 1 line

And I guess swap readahead is utterly unhelpful in this case too.

Yes... as is any "swap writeahead". Excuse my ignorance, but I think this is not done in the swap subsystem but instead the kernel assumes write-coalescing will be done in the block I/O subsystem, which means swap writeahead would affect zram but not zcache/zswap (since frontswap subverts the block I/O subsystem).

However I think a swap-readahead solution would be helpful to zram as well as zcache/zswap.

Quoted 9 lines
Quoted 3 lines

This patch changes vm_swap_full logic slightly so it could free swap slot early if the backed device is really fast. For it, I used SWP_SOLIDSTATE but It might be controversial.

But I strongly disagree with almost everything in your patch :) I disagree with addressing it in vm_swap_full(), I disagree that it can be addressed by device, I disagree that it has anything to do with SWP_SOLIDSTATE.

Quoted 4 lines

This is not a problem with swapping to /dev/ram0 or to /dev/zram0, is it? In those cases, a fixed amount of memory has been set aside for swap, and it works out just like with disk block devices. The memory set aside may be wasted, but that is accepted upfront.

It is (I believe) also a problem with swapping to ram. Two copies of the same page are kept in memory in different places, right? Fixed vs variable size is irrelevant I think. Or am I misunderstanding something about swap-to-ram?

Quoted 3 lines

Similarly, this is not a problem with swapping to SSD. There might or might not be other reasons for adjusting the vm_swap_full() logic for SSD or generally, but those have nothing to do with this issue.

I think it is at least highly related. The key issue is the tradeoff of the likelihood that the page will soon be read/written again while it is in swap cache vs the time/resource-usage necessary to "reconstitute" the page into swap cache. Reconstituting from disk requires a LOT of elapsed time. Reconstituting from
an SSD likely takes much less time. Reconstituting from zcache/zram takes thousands of CPU cycles.

Quoted 4 lines

The problem here is peculiar to frontswap, and the variably sized memory behind it, isn't it? We are accustomed to using swap to free up memory by transferring its data to some other, cheaper but slower resource.

Frontswap does make the problem more complex because some pages are in "fairly fast" storage (zcache, needs decompression) and some are on the actual (usually) rotating media. Fortunately, differentiating between these two cases is just a table lookup (see frontswap_test).

Quoted 7 lines

But in the case of frontswap and zmem (I'll say that to avoid thinking through which backends are actually involved), it is not a cheaper and slower resource, but the very same memory we are trying to save: swap is stolen from the memory under reclaim, so any duplication becomes counter-productive (if we ignore cpu compression/decompression costs:
I have no idea how fair it is to do so, but anyone who chooses zmem is prepared to pay some cpu price for that).

Exactly. There is some "robbing of Peter to pay Paul" and other complex resource tradeoffs. Presumably, though, it is not "the very same memory we are trying to save" but a fraction of it, saving the same page of data more efficiently in memory, using less than a page, at some CPU cost.

Quoted 5 lines

And because it's a frontswap thing, we cannot decide this by device:
frontswap may or may not stand in front of each device. There is no problem with swapcache duplicated on disk (until that area approaches being full or fragmented), but at the higher level we cannot see what is in zmem and what is on disk: we only want to free up the zmem dup.

I *think* frontswap_test(page) resolves this problem, as long as we have a specific page available to use as a parameter.

Quoted 5 lines

I believe the answer is for frontswap/zmem to invalidate the frontswap copy of the page (to free up the compressed memory when possible) and SetPageDirty on the PageUptodate PageSwapCache page when swapping in (setting page dirty so nothing will later go to read it from the unfreed location on backing swap disk, which was never written).

There are two duplication issues: (1) When can the page be removed from the swap cache after a call to frontswap_store; and (2) When can the page be removed from the frontswap storage after it has been brought back into memory via frontswap_load.

This patch from Minchan addresses (1). The issue you are raising here is (2). You may not know that (2) has recently been solved in frontswap, at least for zcache. See frontswap_exclusive_gets_enabled. If this is enabled (and it is for zcache but not yet for zswap), what you suggest (SetPageDirty) is what happens.

Quoted 7 lines

We cannot rely on freeing the swap itself, because in general there may be multiple references to the swap, and we only satisfy the one which has faulted. It may or may not be a good idea to use rmap to locate the other places to insert pte in place of swap entry, to resolve them all at once; but we have chosen not to do so in the past, and there's no need for that, if the zmem gets invalidated and the swapcache page set dirty.

I see. Minchan's patch handles the removal "reactively"... it might be possible to handle it more proactively. Or it may be possible to take the number of references into account when deciding whether to frontswap_store the page as, presumably, the likelihood of needing to "reconstitute" the page sooner increases with each additional reference.

Quoted 1 line

Hugh

Very useful thoughts, Hugh. Thanks much and looking forward to more discussion at LSF/MM!

Dan

P.S. When I refer to zcache, I am referring to the version in drivers/staging/zcache in 3.9. The code in drivers/staging/zcache in 3.8 is "old zcache"... "new zcache" is in drivers/staging/ramster in 3.8. Sorry for any confusion...

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

#9 lore raw
Quoted 20 lines · Dan Magenheimer
Quoted 9 lines

From: Hugh Dickins [mailto:[email protected]]
Subject: Re: [RFC] mm: remove swapcache page early

Quoted 2 lines · Minchan Kim

Swap subsystem does lazy swap slot free with expecting the page would be swapped out again so we can't avoid unnecessary write.

so we can avoid unnecessary write.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

But the problem in in-memory swap is that it consumes memory space until vm_swap_full(ie, used half of all of swap device) condition meet. It could be bad if we use multiple swap device, small in-memory swap and big storage swap or in-memory swap alone.

That is a very good realization: it's surprising that none of us thought of it before - no disrespect to you, well done, thank you.

Yes, my compliments also Minchan. This problem has been thought of before but this patch is the first to identify a possible solution.

Quoted 7 lines
Quoted 1 line

And I guess swap readahead is utterly unhelpful in this case too.

Yes... as is any "swap writeahead". Excuse my ignorance, but I think this is not done in the swap subsystem but instead the kernel assumes write-coalescing will be done in the block I/O subsystem, which means swap writeahead would affect zram but not zcache/zswap (since frontswap subverts the block I/O subsystem).

I don't know what swap writeahead is; but write coalescing, yes. I don't see any problem with it in this context.

Quoted 3 lines

However I think a swap-readahead solution would be helpful to zram as well as zcache/zswap.

Whereas swap readahead on zmem is uncompressing zmem to pagecache which may never be needed, and may take a circuit of the inactive LRU before it gets reclaimed (if it turns out not to be needed, at least it will remain clean and be easily reclaimed).

Quoted 15 lines
Quoted 10 lines
Quoted 4 lines

This patch changes vm_swap_full logic slightly so it could free swap slot early if the backed device is really fast. For it, I used SWP_SOLIDSTATE but It might be controversial.

But I strongly disagree with almost everything in your patch :) I disagree with addressing it in vm_swap_full(), I disagree that it can be addressed by device, I disagree that it has anything to do with SWP_SOLIDSTATE.

Quoted 4 lines

This is not a problem with swapping to /dev/ram0 or to /dev/zram0, is it? In those cases, a fixed amount of memory has been set aside for swap, and it works out just like with disk block devices. The memory set aside may be wasted, but that is accepted upfront.

Quoted 4 lines

It is (I believe) also a problem with swapping to ram. Two copies of the same page are kept in memory in different places, right? Fixed vs variable size is irrelevant I think. Or am I misunderstanding something about swap-to-ram?

I may be misrembering how /dev/ram0 works, or simply assuming that if you want to use it for swap (interesting for testing, but probably not for general use), then you make sure to allocate each page of it in advance.

The pages of /dev/ram0 don't get freed, or not before it's closed (swapoff'ed) anyway. Yes, swapcache would be duplicating data from other memory into /dev/ram0 memory; but that /dev/ram0 memory has been set aside for this purpose, and removing from swapcache won't free any more memory.

Quoted 5 lines
Quoted 4 lines

Similarly, this is not a problem with swapping to SSD. There might or might not be other reasons for adjusting the vm_swap_full() logic for SSD or generally, but those have nothing to do with this issue.

Quoted 7 lines

I think it is at least highly related. The key issue is the tradeoff of the likelihood that the page will soon be read/written again while it is in swap cache vs the time/resource-usage necessary to "reconstitute" the page into swap cache. Reconstituting from disk requires a LOT of elapsed time. Reconstituting from
an SSD likely takes much less time. Reconstituting from zcache/zram takes thousands of CPU cycles.

I acknowledge my complete ignorance of how to judge the tradeoff between memory usage and cpu usage, but I think Minchan's main concern was with the memory usage. Neither hard disk nor SSD is occupying memory.

Quoted 20 lines
Quoted 5 lines

The problem here is peculiar to frontswap, and the variably sized memory behind it, isn't it? We are accustomed to using swap to free up memory by transferring its data to some other, cheaper but slower resource.

Frontswap does make the problem more complex because some pages are in "fairly fast" storage (zcache, needs decompression) and some are on the actual (usually) rotating media. Fortunately, differentiating between these two cases is just a table lookup (see frontswap_test).

Quoted 7 lines

But in the case of frontswap and zmem (I'll say that to avoid thinking through which backends are actually involved), it is not a cheaper and slower resource, but the very same memory we are trying to save: swap is stolen from the memory under reclaim, so any duplication becomes counter-productive (if we ignore cpu compression/decompression costs:
I have no idea how fair it is to do so, but anyone who chooses zmem is prepared to pay some cpu price for that).

Quoted 5 lines

Exactly. There is some "robbing of Peter to pay Paul" and other complex resource tradeoffs. Presumably, though, it is not "the very same memory we are trying to save" but a fraction of it, saving the same page of data more efficiently in memory, using less than a page, at some CPU cost.

Yes, I'm not saying that frontswap/zmem is pointless: just agreeing with Minchan that in this case the duplication inherent in swapcache can be waste of memory that we should try to avoid.

Quoted 16 lines
Quoted 6 lines

And because it's a frontswap thing, we cannot decide this by device:
frontswap may or may not stand in front of each device. There is no problem with swapcache duplicated on disk (until that area approaches being full or fragmented), but at the higher level we cannot see what is in zmem and what is on disk: we only want to free up the zmem dup.

I *think* frontswap_test(page) resolves this problem, as long as we have a specific page available to use as a parameter.

Quoted 5 lines

I believe the answer is for frontswap/zmem to invalidate the frontswap copy of the page (to free up the compressed memory when possible) and SetPageDirty on the PageUptodate PageSwapCache page when swapping in (setting page dirty so nothing will later go to read it from the unfreed location on backing swap disk, which was never written).

Quoted 6 lines

There are two duplication issues: (1) When can the page be removed from the swap cache after a call to frontswap_store; and (2) When can the page be removed from the frontswap storage after it has been brought back into memory via frontswap_load.

This patch from Minchan addresses (1).

Ying Han was reminding me of this case a couple of hours ago, we don't see a problem there: when frontswap_store() succeeds, there's an end_page_writeback() as there should be, and shrink_page_list() should reclaim the page immediately. So I think (1) is already handled and Minchan was not trying to address it.

Quoted 5 lines

The issue you are raising
here is (2). You may not know that (2) has recently been solved in frontswap, at least for zcache. See frontswap_exclusive_gets_enabled. If this is enabled (and it is for zcache but not yet for zswap), what you suggest (SetPageDirty) is what happens.

Ah, and I have a dim, perhaps mistaken, memory that I gave you input on that before, suggesting the SetPageDirty. Good, sounds like the solution is already in place, if not actually activated.

Thanks, must dash,
Hugh

Quoted 21 lines
Quoted 8 lines

We cannot rely on freeing the swap itself, because in general there may be multiple references to the swap, and we only satisfy the one which has faulted. It may or may not be a good idea to use rmap to locate the other places to insert pte in place of swap entry, to resolve them all at once; but we have chosen not to do so in the past, and there's no need for that, if the zmem gets invalidated and the swapcache page set dirty.

I see. Minchan's patch handles the removal "reactively"... it might be possible to handle it more proactively. Or it may be possible to take the number of references into account when deciding whether to frontswap_store the page as, presumably, the likelihood of needing to "reconstitute" the page sooner increases with each additional reference.

Quoted 1 line

Hugh

Very useful thoughts, Hugh. Thanks much and looking forward to more discussion at LSF/MM!

Quoted 6 lines

Dan

P.S. When I refer to zcache, I am referring to the version in drivers/staging/zcache in 3.9. The code in drivers/staging/zcache in 3.8 is "old zcache"... "new zcache" is in drivers/staging/ramster in 3.8. Sorry for any confusion...

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

#10 lore raw
Quoted 33 lines · Hugh Dickins
Quoted 20 lines · Dan Magenheimer
Quoted 9 lines

From: Hugh Dickins [mailto:[email protected]]
Subject: Re: [RFC] mm: remove swapcache page early

Quoted 2 lines · Minchan Kim

Swap subsystem does lazy swap slot free with expecting the page would be swapped out again so we can't avoid unnecessary write.

so we can avoid unnecessary write.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

But the problem in in-memory swap is that it consumes memory space until vm_swap_full(ie, used half of all of swap device) condition meet. It could be bad if we use multiple swap device, small in-memory swap and big storage swap or in-memory swap alone.

That is a very good realization: it's surprising that none of us thought of it before - no disrespect to you, well done, thank you.

Yes, my compliments also Minchan. This problem has been thought of before but this patch is the first to identify a possible solution.

Quoted 7 lines
Quoted 1 line

And I guess swap readahead is utterly unhelpful in this case too.

Yes... as is any "swap writeahead". Excuse my ignorance, but I think this is not done in the swap subsystem but instead the kernel assumes write-coalescing will be done in the block I/O subsystem, which means swap writeahead would affect zram but not zcache/zswap (since frontswap subverts the block I/O subsystem).

I don't know what swap writeahead is; but write coalescing, yes. I don't see any problem with it in this context.

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

However I think a swap-readahead solution would be helpful to zram as well as zcache/zswap.

Whereas swap readahead on zmem is uncompressing zmem to pagecache which may never be needed, and may take a circuit of the inactive LRU before it gets reclaimed (if it turns out not to be needed, at least it will remain clean and be easily reclaimed).

But it could evict more important pages before reaching out the tail. That's thing we really want to avoid if possible.

Quoted 45 lines
Quoted 16 lines
Quoted 11 lines
Quoted 5 lines

This patch changes vm_swap_full logic slightly so it could free swap slot early if the backed device is really fast. For it, I used SWP_SOLIDSTATE but It might be controversial.

But I strongly disagree with almost everything in your patch :) I disagree with addressing it in vm_swap_full(), I disagree that it can be addressed by device, I disagree that it has anything to do with SWP_SOLIDSTATE.

Quoted 4 lines

This is not a problem with swapping to /dev/ram0 or to /dev/zram0, is it? In those cases, a fixed amount of memory has been set aside for swap, and it works out just like with disk block devices. The memory set aside may be wasted, but that is accepted upfront.

Quoted 4 lines

It is (I believe) also a problem with swapping to ram. Two copies of the same page are kept in memory in different places, right? Fixed vs variable size is irrelevant I think. Or am I misunderstanding something about swap-to-ram?

I may be misrembering how /dev/ram0 works, or simply assuming that if you want to use it for swap (interesting for testing, but probably not for general use), then you make sure to allocate each page of it in advance.

The pages of /dev/ram0 don't get freed, or not before it's closed (swapoff'ed) anyway. Yes, swapcache would be duplicating data from other memory into /dev/ram0 memory; but that /dev/ram0 memory has been set aside for this purpose, and removing from swapcache won't free any more memory.

Quoted 5 lines
Quoted 4 lines

Similarly, this is not a problem with swapping to SSD. There might or might not be other reasons for adjusting the vm_swap_full() logic for SSD or generally, but those have nothing to do with this issue.

Quoted 7 lines

I think it is at least highly related. The key issue is the tradeoff of the likelihood that the page will soon be read/written again while it is in swap cache vs the time/resource-usage necessary to "reconstitute" the page into swap cache. Reconstituting from disk requires a LOT of elapsed time. Reconstituting from
an SSD likely takes much less time. Reconstituting from zcache/zram takes thousands of CPU cycles.

Quoted 4 lines

I acknowledge my complete ignorance of how to judge the tradeoff between memory usage and cpu usage, but I think Minchan's main concern was with the memory usage. Neither hard disk nor SSD is occupying memory.

Hmm, It seems I misunderstood Dan's opinion in previous thread. You're right, Hugh. My main concern is memory usage but the rationale I used SWP_SOLIDSTATE is writing on SSD could be cheap rather than storage. Yeb, it depends on SSD's internal's FTL algorith and fragment ratio due to wear-leveling. That's why I said "It might be controversial".

Quoted 52 lines
Quoted 21 lines
Quoted 6 lines

The problem here is peculiar to frontswap, and the variably sized memory behind it, isn't it? We are accustomed to using swap to free up memory by transferring its data to some other, cheaper but slower resource.

Frontswap does make the problem more complex because some pages are in "fairly fast" storage (zcache, needs decompression) and some are on the actual (usually) rotating media. Fortunately, differentiating between these two cases is just a table lookup (see frontswap_test).

Quoted 7 lines

But in the case of frontswap and zmem (I'll say that to avoid thinking through which backends are actually involved), it is not a cheaper and slower resource, but the very same memory we are trying to save: swap is stolen from the memory under reclaim, so any duplication becomes counter-productive (if we ignore cpu compression/decompression costs:
I have no idea how fair it is to do so, but anyone who chooses zmem is prepared to pay some cpu price for that).

Quoted 5 lines

Exactly. There is some "robbing of Peter to pay Paul" and other complex resource tradeoffs. Presumably, though, it is not "the very same memory we are trying to save" but a fraction of it, saving the same page of data more efficiently in memory, using less than a page, at some CPU cost.

Yes, I'm not saying that frontswap/zmem is pointless: just agreeing with Minchan that in this case the duplication inherent in swapcache can be waste of memory that we should try to avoid.

Quoted 21 lines
Quoted 6 lines

And because it's a frontswap thing, we cannot decide this by device:
frontswap may or may not stand in front of each device. There is no problem with swapcache duplicated on disk (until that area approaches being full or fragmented), but at the higher level we cannot see what is in zmem and what is on disk: we only want to free up the zmem dup.

I *think* frontswap_test(page) resolves this problem, as long as we have a specific page available to use as a parameter.

Quoted 5 lines

I believe the answer is for frontswap/zmem to invalidate the frontswap copy of the page (to free up the compressed memory when possible) and SetPageDirty on the PageUptodate PageSwapCache page when swapping in (setting page dirty so nothing will later go to read it from the unfreed location on backing swap disk, which was never written).

There are two duplication issues: (1) When can the page be removed from the swap cache after a call to frontswap_store; and (2) When can the page be removed from the frontswap storage after it has been brought back into memory via frontswap_load.

Quoted 7 lines
Quoted 1 line

This patch from Minchan addresses (1).

Ying Han was reminding me of this case a couple of hours ago, we don't see a problem there: when frontswap_store() succeeds, there's an end_page_writeback() as there should be, and shrink_page_list() should reclaim the page immediately. So I think (1) is already handled and Minchan was not trying to address it.

Absolutely.

Quoted 47 lines
Quoted 6 lines

The issue you are raising
here is (2). You may not know that (2) has recently been solved in frontswap, at least for zcache. See frontswap_exclusive_gets_enabled. If this is enabled (and it is for zcache but not yet for zswap), what you suggest (SetPageDirty) is what happens.

Ah, and I have a dim, perhaps mistaken, memory that I gave you input on that before, suggesting the SetPageDirty. Good, sounds like the solution is already in place, if not actually activated.

Thanks, must dash,
Hugh

Quoted 21 lines
Quoted 8 lines

We cannot rely on freeing the swap itself, because in general there may be multiple references to the swap, and we only satisfy the one which has faulted. It may or may not be a good idea to use rmap to locate the other places to insert pte in place of swap entry, to resolve them all at once; but we have chosen not to do so in the past, and there's no need for that, if the zmem gets invalidated and the swapcache page set dirty.

I see. Minchan's patch handles the removal "reactively"... it might be possible to handle it more proactively. Or it may be possible to take the number of references into account when deciding whether to frontswap_store the page as, presumably, the likelihood of needing to "reconstitute" the page sooner increases with each additional reference.

Quoted 1 line

Hugh

Very useful thoughts, Hugh. Thanks much and looking forward to more discussion at LSF/MM!

Quoted 6 lines

Dan

P.S. When I refer to zcache, I am referring to the version in drivers/staging/zcache in 3.9. The code in drivers/staging/zcache in 3.8 is "old zcache"... "new zcache" is in drivers/staging/ramster in 3.8. Sorry for any confusion...

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

Signature · 8 lines
Kind regards,
Minchan Kim

--
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>
#11 lore raw
Quoted 95 lines · Minchan Kim
Quoted 33 lines · Hugh Dickins
Quoted 20 lines · Dan Magenheimer
Quoted 9 lines

From: Hugh Dickins [mailto:[email protected]]
Subject: Re: [RFC] mm: remove swapcache page early

Quoted 2 lines · Minchan Kim

Swap subsystem does lazy swap slot free with expecting the page would be swapped out again so we can't avoid unnecessary write.

so we can avoid unnecessary write.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

But the problem in in-memory swap is that it consumes memory space until vm_swap_full(ie, used half of all of swap device) condition meet. It could be bad if we use multiple swap device, small in-memory swap and big storage swap or in-memory swap alone.

That is a very good realization: it's surprising that none of us thought of it before - no disrespect to you, well done, thank you.

Yes, my compliments also Minchan. This problem has been thought of before but this patch is the first to identify a possible solution.

Quoted 7 lines
Quoted 1 line

And I guess swap readahead is utterly unhelpful in this case too.

Yes... as is any "swap writeahead". Excuse my ignorance, but I think this is not done in the swap subsystem but instead the kernel assumes write-coalescing will be done in the block I/O subsystem, which means swap writeahead would affect zram but not zcache/zswap (since frontswap subverts the block I/O subsystem).

I don't know what swap writeahead is; but write coalescing, yes. I don't see any problem with it in this context.

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

However I think a swap-readahead solution would be helpful to zram as well as zcache/zswap.

Whereas swap readahead on zmem is uncompressing zmem to pagecache which may never be needed, and may take a circuit of the inactive LRU before it gets reclaimed (if it turns out not to be needed, at least it will remain clean and be easily reclaimed).

But it could evict more important pages before reaching out the tail. That's thing we really want to avoid if possible.

Quoted 45 lines
Quoted 16 lines
Quoted 11 lines
Quoted 5 lines

This patch changes vm_swap_full logic slightly so it could free swap slot early if the backed device is really fast. For it, I used SWP_SOLIDSTATE but It might be controversial.

But I strongly disagree with almost everything in your patch :) I disagree with addressing it in vm_swap_full(), I disagree that it can be addressed by device, I disagree that it has anything to do with SWP_SOLIDSTATE.

Quoted 4 lines

This is not a problem with swapping to /dev/ram0 or to /dev/zram0, is it? In those cases, a fixed amount of memory has been set aside for swap, and it works out just like with disk block devices. The memory set aside may be wasted, but that is accepted upfront.

Quoted 4 lines

It is (I believe) also a problem with swapping to ram. Two copies of the same page are kept in memory in different places, right? Fixed vs variable size is irrelevant I think. Or am I misunderstanding something about swap-to-ram?

I may be misrembering how /dev/ram0 works, or simply assuming that if you want to use it for swap (interesting for testing, but probably not for general use), then you make sure to allocate each page of it in advance.

The pages of /dev/ram0 don't get freed, or not before it's closed (swapoff'ed) anyway. Yes, swapcache would be duplicating data from other memory into /dev/ram0 memory; but that /dev/ram0 memory has been set aside for this purpose, and removing from swapcache won't free any more memory.

Quoted 5 lines
Quoted 4 lines

Similarly, this is not a problem with swapping to SSD. There might or might not be other reasons for adjusting the vm_swap_full() logic for SSD or generally, but those have nothing to do with this issue.

Quoted 7 lines

I think it is at least highly related. The key issue is the tradeoff of the likelihood that the page will soon be read/written again while it is in swap cache vs the time/resource-usage necessary to "reconstitute" the page into swap cache. Reconstituting from disk requires a LOT of elapsed time. Reconstituting from
an SSD likely takes much less time. Reconstituting from zcache/zram takes thousands of CPU cycles.

Quoted 4 lines

I acknowledge my complete ignorance of how to judge the tradeoff between memory usage and cpu usage, but I think Minchan's main concern was with the memory usage. Neither hard disk nor SSD is occupying memory.

Quoted 5 lines

Hmm, It seems I misunderstood Dan's opinion in previous thread. You're right, Hugh. My main concern is memory usage but the rationale I used SWP_SOLIDSTATE is writing on SSD could be cheap rather than storage. Yeb, it depends on SSD's internal's FTL algorith and fragment ratio due to wear-leveling. That's why I said "It might be controversial".

Even SSD is fast, there is tradeoff. And unncessary write to SSD should be avoided if possible, because write makes wear out faster and makes subsequent write slower potentially (if garbage collection runs).

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>

#12 lore raw
In reply to Hugh Dickins
Quoted 13 lines

From: Hugh Dickins [mailto:[email protected]]
Subject: RE: [RFC] mm: remove swapcache page early

Quoted 8 lines · Dan Magenheimer
Quoted 3 lines

From: Hugh Dickins [mailto:[email protected]]
Subject: Re: [RFC] mm: remove swapcache page early

The issue you are raising
here is (2). You may not know that (2) has recently been solved in frontswap, at least for zcache. See frontswap_exclusive_gets_enabled. If this is enabled (and it is for zcache but not yet for zswap), what you suggest (SetPageDirty) is what happens.

Quoted 6 lines

Ah, and I have a dim, perhaps mistaken, memory that I gave you input on that before, suggesting the SetPageDirty. Good, sounds like the solution is already in place, if not actually activated.

Thanks, must dash,
Hugh

Hi Hugh --

Credit where it is due... Yes, I do recall now that the idea was originally yours. It went on a to-do list where I eventually tried it and it worked... I'm sorry I had forgotten and neglected to give you credit!

(BTW, it is activated for zcache in 3.9.)

Thanks,
Dan

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

#13 lore raw
In reply to Dan Magenheimer

Hi Dan,

Quoted 14 lines · Dan Magenheimer
Quoted 14 lines

From: Hugh Dickins [mailto:[email protected]]
Subject: Re: [RFC] mm: remove swapcache page early

Quoted 2 lines · Minchan Kim

Swap subsystem does lazy swap slot free with expecting the page would be swapped out again so we can't avoid unnecessary write.

so we can avoid unnecessary write.

Quoted 5 lines

But the problem in in-memory swap is that it consumes memory space until vm_swap_full(ie, used half of all of swap device) condition meet. It could be bad if we use multiple swap device, small in-memory swap and big storage swap or in-memory swap alone.

Quoted 5 lines
Quoted 2 lines

That is a very good realization: it's surprising that none of us thought of it before - no disrespect to you, well done, thank you.

Yes, my compliments also Minchan. This problem has been thought of before but this patch is the first to identify a possible solution.

Thanks!

Quoted 8 lines
Quoted 2 lines

And I guess swap readahead is utterly unhelpful in this case too.

Yes... as is any "swap writeahead". Excuse my ignorance, but I think this is not done in the swap subsystem but instead the kernel assumes write-coalescing will be done in the block I/O subsystem, which means swap writeahead would affect zram but not zcache/zswap (since frontswap subverts the block I/O subsystem).

Frankly speaking, I don't know why you mentioned "swap writeahead" in this point. Anyway, I dobut how it effect zram, too. A gain I can have a mind is compress ratio would be high thorough multiple page compression all at once.

Quoted 3 lines

However I think a swap-readahead solution would be helpful to zram as well as zcache/zswap.

Hmm, why? swap-readahead is just hint to reduce big stall time to reduce on big seek overhead storage. But in-memory swap is no cost for seeking. So unnecessary swap-readahead can make memory pressure high and it could cause another page swap out so it could be swap-thrashing. And for good swap-readahead hit ratio, swap device shouldn't be fragmented. But as you know, there are many factor to prevent it in the kernel now and Shaohua is tackling on it.

Quoted 24 lines
Quoted 10 lines
Quoted 4 lines

This patch changes vm_swap_full logic slightly so it could free swap slot early if the backed device is really fast. For it, I used SWP_SOLIDSTATE but It might be controversial.

But I strongly disagree with almost everything in your patch :) I disagree with addressing it in vm_swap_full(), I disagree that it can be addressed by device, I disagree that it has anything to do with SWP_SOLIDSTATE.

Quoted 4 lines

This is not a problem with swapping to /dev/ram0 or to /dev/zram0, is it? In those cases, a fixed amount of memory has been set aside for swap, and it works out just like with disk block devices. The memory set aside may be wasted, but that is accepted upfront.

It is (I believe) also a problem with swapping to ram. Two copies of the same page are kept in memory in different places, right? Fixed vs variable size is irrelevant I think. Or am I misunderstanding something about swap-to-ram?

Quoted 3 lines

Similarly, this is not a problem with swapping to SSD. There might or might not be other reasons for adjusting the vm_swap_full() logic for SSD or generally, but those have nothing to do with this issue.

Quoted 7 lines

I think it is at least highly related. The key issue is the tradeoff of the likelihood that the page will soon be read/written again while it is in swap cache vs the time/resource-usage necessary to "reconstitute" the page into swap cache. Reconstituting from disk requires a LOT of elapsed time. Reconstituting from
an SSD likely takes much less time. Reconstituting from zcache/zram takes thousands of CPU cycles.

Yeb. That's why I wanted to use SWP_SOLIDSTATE.

Quoted 11 lines
Quoted 5 lines

The problem here is peculiar to frontswap, and the variably sized memory behind it, isn't it? We are accustomed to using swap to free up memory by transferring its data to some other, cheaper but slower resource.

Frontswap does make the problem more complex because some pages are in "fairly fast" storage (zcache, needs decompression) and some are on the actual (usually) rotating media. Fortunately, differentiating between these two cases is just a table lookup (see frontswap_test).

Yeb, I thouht it could be a last resort because I'd like to avoid lookup every swapin if possible.

Quoted 15 lines
Quoted 8 lines

But in the case of frontswap and zmem (I'll say that to avoid thinking through which backends are actually involved), it is not a cheaper and slower resource, but the very same memory we are trying to save: swap is stolen from the memory under reclaim, so any duplication becomes counter-productive (if we ignore cpu compression/decompression costs:
I have no idea how fair it is to do so, but anyone who chooses zmem is prepared to pay some cpu price for that).

Exactly. There is some "robbing of Peter to pay Paul" and other complex resource tradeoffs. Presumably, though, it is not "the very same memory we are trying to save" but a fraction of it, saving the same page of data more efficiently in memory, using less than a page, at some CPU cost.

Quoted 8 lines
Quoted 5 lines

And because it's a frontswap thing, we cannot decide this by device:
frontswap may or may not stand in front of each device. There is no problem with swapcache duplicated on disk (until that area approaches being full or fragmented), but at the higher level we cannot see what is in zmem and what is on disk: we only want to free up the zmem dup.

I *think* frontswap_test(page) resolves this problem, as long as we have a specific page available to use as a parameter.

Agreed. Will do the method if we all agree on the way because there isn't better approach.

Quoted 7 lines
Quoted 6 lines

I believe the answer is for frontswap/zmem to invalidate the frontswap copy of the page (to free up the compressed memory when possible) and SetPageDirty on the PageUptodate PageSwapCache page when swapping in (setting page dirty so nothing will later go to read it from the unfreed location on backing swap disk, which was never written).

Quoted 6 lines

There are two duplication issues: (1) When can the page be removed from the swap cache after a call to frontswap_store; and (2) When can the page be removed from the frontswap storage after it has been brought back into memory via frontswap_load.

This patch from Minchan addresses (1). The issue you are raising

No. I am addressing (2).

Quoted 4 lines

here is (2). You may not know that (2) has recently been solved in frontswap, at least for zcache. See frontswap_exclusive_gets_enabled. If this is enabled (and it is for zcache but not yet for zswap), what you suggest (SetPageDirty) is what happens.

I am blind on zcache so I didn't see it. Anyway, I'd like to address it on zram and zswap.

Quoted 16 lines
Quoted 8 lines

We cannot rely on freeing the swap itself, because in general there may be multiple references to the swap, and we only satisfy the one which has faulted. It may or may not be a good idea to use rmap to locate the other places to insert pte in place of swap entry, to resolve them all at once; but we have chosen not to do so in the past, and there's no need for that, if the zmem gets invalidated and the swapcache page set dirty.

I see. Minchan's patch handles the removal "reactively"... it might be possible to handle it more proactively. Or it may be possible to take the number of references into account when deciding whether to frontswap_store the page as, presumably, the likelihood of needing to "reconstitute" the page sooner increases with each additional reference.

Quoted 4 lines
Quoted 1 line

Hugh

Very useful thoughts, Hugh. Thanks much and looking forward to more discussion at LSF/MM!

Dan, Your thought is VERY useful. Thanks much and looking forward to more discsussion at LFS/MM!

Signature · 8 lines
Kind regards,
Minchan Kim

--
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>
#14 lore raw
Quoted 24 lines

From: Minchan Kim [mailto:[email protected]]
Subject: Re: [RFC] mm: remove swapcache page early

Hi Dan,

Quoted 9 lines · Dan Magenheimer
Quoted 8 lines

From: Hugh Dickins [mailto:[email protected]]
Subject: Re: [RFC] mm: remove swapcache page early

I believe the answer is for frontswap/zmem to invalidate the frontswap copy of the page (to free up the compressed memory when possible) and SetPageDirty on the PageUptodate PageSwapCache page when swapping in (setting page dirty so nothing will later go to read it from the unfreed location on backing swap disk, which was never written).

Quoted 6 lines

There are two duplication issues: (1) When can the page be removed from the swap cache after a call to frontswap_store; and (2) When can the page be removed from the frontswap storage after it has been brought back into memory via frontswap_load.

This patch from Minchan addresses (1). The issue you are raising

No. I am addressing (2).

Quoted 7 lines
Quoted 4 lines

here is (2). You may not know that (2) has recently been solved in frontswap, at least for zcache. See frontswap_exclusive_gets_enabled. If this is enabled (and it is for zcache but not yet for zswap), what you suggest (SetPageDirty) is what happens.

I am blind on zcache so I didn't see it. Anyway, I'd like to address it on zram and zswap.

Zswap can enable it trivially by adding a function call in init_zswap. (Note that it is not enabled by default for all frontswap backends because it is another complicated tradeoff of cpu time vs memory space that needs more study on a broad set of workloads.)

I wonder if something like this would have a similar result for zram? (Completely untested... snippet stolen from swap_entry_free with SetPageDirty added... doesn't compile yet, but should give you the idea.)

mm/page_io.c +11 -0
index 56276fe..2d10988 100644
@@ -81,7 +81,17 @@ void end_swap_bio_read(struct bio *bio, int err)
 				iminor(bio->bi_bdev->bd_inode),
 				(unsigned long long)bio->bi_sector);
 	} else {
+		struct swap_info_struct *sis;
+
 		SetPageUptodate(page);
+		sis = page_swap_info(page);
+		if (sis->flags & SWP_BLKDEV) {
+			struct gendisk *disk = sis->bdev->bd_disk;
+			if (disk->fops->swap_slot_free_notify) {
+				SetPageDirty(page);
+				disk->fops->swap_slot_free_notify(sis->bdev,
+								  offset);
+			}
+		}
 	}
 	unlock_page(page);

bio_put(bio);

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

#15 lore raw
Quoted 32 lines · Dan Magenheimer
Quoted 24 lines

From: Minchan Kim [mailto:[email protected]]
Subject: Re: [RFC] mm: remove swapcache page early

Hi Dan,

Quoted 9 lines · Dan Magenheimer
Quoted 8 lines

From: Hugh Dickins [mailto:[email protected]]
Subject: Re: [RFC] mm: remove swapcache page early

I believe the answer is for frontswap/zmem to invalidate the frontswap copy of the page (to free up the compressed memory when possible) and SetPageDirty on the PageUptodate PageSwapCache page when swapping in (setting page dirty so nothing will later go to read it from the unfreed location on backing swap disk, which was never written).

Quoted 6 lines

There are two duplication issues: (1) When can the page be removed from the swap cache after a call to frontswap_store; and (2) When can the page be removed from the frontswap storage after it has been brought back into memory via frontswap_load.

This patch from Minchan addresses (1). The issue you are raising

No. I am addressing (2).

Quoted 7 lines
Quoted 4 lines

here is (2). You may not know that (2) has recently been solved in frontswap, at least for zcache. See frontswap_exclusive_gets_enabled. If this is enabled (and it is for zcache but not yet for zswap), what you suggest (SetPageDirty) is what happens.

I am blind on zcache so I didn't see it. Anyway, I'd like to address it on zram and zswap.

Quoted 8 lines

Zswap can enable it trivially by adding a function call in init_zswap. (Note that it is not enabled by default for all frontswap backends because it is another complicated tradeoff of cpu time vs memory space that needs more study on a broad set of workloads.)

I wonder if something like this would have a similar result for zram? (Completely untested... snippet stolen from swap_entry_free with SetPageDirty added... doesn't compile yet, but should give you the idea.)

Nice idea!

After I see your patch, I realized it was Hugh's suggestion and you implemented it in proper place.

Will resend it after testing. Maybe nextweek.
Thanks!

Quoted 30 lines
mm/page_io.c +11 -1
index 56276fe..2d10988 100644
@@ -81,7 +81,17 @@ void end_swap_bio_read(struct bio *bio, int err)
 				iminor(bio->bi_bdev->bd_inode),
 				(unsigned long long)bio->bi_sector);
 	} else {
+		struct swap_info_struct *sis;
+
 		SetPageUptodate(page);
+		sis = page_swap_info(page);
+		if (sis->flags & SWP_BLKDEV) {
+			struct gendisk *disk = sis->bdev->bd_disk;
+			if (disk->fops->swap_slot_free_notify) {
+				SetPageDirty(page);
+				disk->fops->swap_slot_free_notify(sis->bdev,
+								  offset);
+			}
+		}
 	}
 	unlock_page(page);
 	bio_put(bio);
 
--

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>

Signature · 8 lines
Kind regards,
Minchan Kim

--
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>
#16 lore raw
Quoted 29 lines · Minchan Kim
Quoted 28 lines · Dan Magenheimer
Quoted 27 lines

From: Minchan Kim [mailto:[email protected]]

Quoted 9 lines · Dan Magenheimer
Quoted 8 lines

From: Hugh Dickins [mailto:[email protected]]
Subject: Re: [RFC] mm: remove swapcache page early

I believe the answer is for frontswap/zmem to invalidate the frontswap copy of the page (to free up the compressed memory when possible) and SetPageDirty on the PageUptodate PageSwapCache page when swapping in (setting page dirty so nothing will later go to read it from the unfreed location on backing swap disk, which was never written).

Quoted 6 lines

There are two duplication issues: (1) When can the page be removed from the swap cache after a call to frontswap_store; and (2) When can the page be removed from the frontswap storage after it has been brought back into memory via frontswap_load.

This patch from Minchan addresses (1). The issue you are raising

No. I am addressing (2).

Quoted 4 lines

here is (2). You may not know that (2) has recently been solved in frontswap, at least for zcache. See frontswap_exclusive_gets_enabled. If this is enabled (and it is for zcache but not yet for zswap), what you suggest (SetPageDirty) is what happens.

I am blind on zcache so I didn't see it. Anyway, I'd like to address it on zram and zswap.

Quoted 8 lines
Quoted 8 lines

Zswap can enable it trivially by adding a function call in init_zswap. (Note that it is not enabled by default for all frontswap backends because it is another complicated tradeoff of cpu time vs memory space that needs more study on a broad set of workloads.)

I wonder if something like this would have a similar result for zram? (Completely untested... snippet stolen from swap_entry_free with SetPageDirty added... doesn't compile yet, but should give you the idea.)

Thanks for correcting me on zram (in earlier mail of this thread), yes, I was forgetting about the swap_slot_free_notify entry point which lets that memory be freed.

Quoted 8 lines

Nice idea!

After I see your patch, I realized it was Hugh's suggestion and you implemented it in proper place.

Will resend it after testing. Maybe nextweek.
Thanks!

Be careful, although Dan is right that something like this can be done for zram, I believe you will find that it needs a little more:
either a separate new entry point (not my preference) or a flags arg (or boolean) added to swap_slot_free_notify.

Because this is a different operation: end_swap_bio_read() wants to free up zram's compressed copy of the page, but the swp_entry_t must remain valid until swap_entry_free() can clear up the rest. Precisely how much of the work each should do, you will discover.

Hugh

Quoted 17 lines
Quoted 17 lines
mm/page_io.c +6 -0
index 56276fe..2d10988 100644
@@ -81,7 +81,17 @@ void end_swap_bio_read(struct bio *bio, int err)
 				iminor(bio->bi_bdev->bd_inode),
 				(unsigned long long)bio->bi_sector);
 	} else {
+		struct swap_info_struct *sis;
+
 		SetPageUptodate(page);
+		sis = page_swap_info(page);
+		if (sis->flags & SWP_BLKDEV) {
+			struct gendisk *disk = sis->bdev->bd_disk;
+			if (disk->fops->swap_slot_free_notify) {
Quoted 8 lines

mm/page_io.c void end_swap_bio_read(struct bio *bio, int err)

Quoted 8 lines
mm/page_io.c +5 -0
+				SetPageDirty(page);
+				disk->fops->swap_slot_free_notify(sis->bdev,
+								  offset);
+			}
+		}
 	}
 	unlock_page(page);
 	bio_put(bio);

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

#17 lore raw

Hi Hugh,

Quoted 57 lines · Hugh Dickins
Quoted 29 lines · Minchan Kim
Quoted 28 lines · Dan Magenheimer
Quoted 27 lines

From: Minchan Kim [mailto:[email protected]]

Quoted 9 lines · Dan Magenheimer
Quoted 8 lines

From: Hugh Dickins [mailto:[email protected]]
Subject: Re: [RFC] mm: remove swapcache page early

I believe the answer is for frontswap/zmem to invalidate the frontswap copy of the page (to free up the compressed memory when possible) and SetPageDirty on the PageUptodate PageSwapCache page when swapping in (setting page dirty so nothing will later go to read it from the unfreed location on backing swap disk, which was never written).

Quoted 6 lines

There are two duplication issues: (1) When can the page be removed from the swap cache after a call to frontswap_store; and (2) When can the page be removed from the frontswap storage after it has been brought back into memory via frontswap_load.

This patch from Minchan addresses (1). The issue you are raising

No. I am addressing (2).

Quoted 4 lines

here is (2). You may not know that (2) has recently been solved in frontswap, at least for zcache. See frontswap_exclusive_gets_enabled. If this is enabled (and it is for zcache but not yet for zswap), what you suggest (SetPageDirty) is what happens.

I am blind on zcache so I didn't see it. Anyway, I'd like to address it on zram and zswap.

Quoted 8 lines
Quoted 8 lines

Zswap can enable it trivially by adding a function call in init_zswap. (Note that it is not enabled by default for all frontswap backends because it is another complicated tradeoff of cpu time vs memory space that needs more study on a broad set of workloads.)

I wonder if something like this would have a similar result for zram? (Completely untested... snippet stolen from swap_entry_free with SetPageDirty added... doesn't compile yet, but should give you the idea.)

Thanks for correcting me on zram (in earlier mail of this thread), yes, I was forgetting about the swap_slot_free_notify entry point which lets that memory be freed.

Quoted 8 lines

Nice idea!

After I see your patch, I realized it was Hugh's suggestion and you implemented it in proper place.

Will resend it after testing. Maybe nextweek.
Thanks!

Be careful, although Dan is right that something like this can be done for zram, I believe you will find that it needs a little more:
either a separate new entry point (not my preference) or a flags arg (or boolean) added to swap_slot_free_notify.

Quoted 4 lines

Because this is a different operation: end_swap_bio_read() wants to free up zram's compressed copy of the page, but the swp_entry_t must remain valid until swap_entry_free() can clear up the rest. Precisely how much of the work each should do, you will discover.

First of all, Thanks for noticing it for me!

If I parse your concern correctly, you are concerning about different semantic on two functions.
(end_swap_bio_read's swap_slot_free_notify VS swap_entry_free's one).

But current implementatoin on zram_slot_free_notify could cover both cases properly with luck.

zram_free_page caused by end_swap_bio_read will free compressed copy of the page and zram_free_page caused by swap_entry_free later won't find right index from zram->table and just return.
So I think there is no problem.

Remained problem is zram->stats.notify_free, which could be counted redundantly but not sure it's valuable to count exactly.

If I miss your point, please pinpoint your concern. :)

Thanks!

Signature · 8 lines
Kind regards,
Minchan Kim

--
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>
#18 lore raw
Quoted 32 lines · Minchan Kim
Quoted 12 lines · Hugh Dickins
Quoted 5 lines · Minchan Kim
Quoted 4 lines · Dan Magenheimer

I wonder if something like this would have a similar result for zram? (Completely untested... snippet stolen from swap_entry_free with SetPageDirty added... doesn't compile yet, but should give you the idea.)

Be careful, although Dan is right that something like this can be done for zram, I believe you will find that it needs a little more:
either a separate new entry point (not my preference) or a flags arg (or boolean) added to swap_slot_free_notify.

Quoted 4 lines

Because this is a different operation: end_swap_bio_read() wants to free up zram's compressed copy of the page, but the swp_entry_t must remain valid until swap_entry_free() can clear up the rest. Precisely how much of the work each should do, you will discover.

First of all, Thanks for noticing it for me!

If I parse your concern correctly, you are concerning about different semantic on two functions.
(end_swap_bio_read's swap_slot_free_notify VS swap_entry_free's one).

But current implementatoin on zram_slot_free_notify could cover both cases properly with luck.

zram_free_page caused by end_swap_bio_read will free compressed copy of the page and zram_free_page caused by swap_entry_free later won't find right index from zram->table and just return.
So I think there is no problem.

Quoted 4 lines

Remained problem is zram->stats.notify_free, which could be counted redundantly but not sure it's valuable to count exactly.

If I miss your point, please pinpoint your concern. :)

Looking at it again, I do believe you and Dan are perfectly correct, and I was again the confused one. Though I'd be happier if I could see just how I was misreading it: makes me wonder if I had a great insight that I can no longer grasp hold of! I think I was paranoid about a swp_entry_t getting recycled prematurely: but swap_entry_free remains in control of that - freeing a swap entry is no part of what notify_free gets up to. Sorry for wasting your time.

Hugh

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

#19 lore raw
Quoted 38 lines · Hugh Dickins
Quoted 32 lines · Minchan Kim
Quoted 12 lines · Hugh Dickins
Quoted 5 lines · Minchan Kim
Quoted 4 lines · Dan Magenheimer

I wonder if something like this would have a similar result for zram? (Completely untested... snippet stolen from swap_entry_free with SetPageDirty added... doesn't compile yet, but should give you the idea.)

Be careful, although Dan is right that something like this can be done for zram, I believe you will find that it needs a little more:
either a separate new entry point (not my preference) or a flags arg (or boolean) added to swap_slot_free_notify.

Quoted 4 lines

Because this is a different operation: end_swap_bio_read() wants to free up zram's compressed copy of the page, but the swp_entry_t must remain valid until swap_entry_free() can clear up the rest. Precisely how much of the work each should do, you will discover.

First of all, Thanks for noticing it for me!

If I parse your concern correctly, you are concerning about different semantic on two functions.
(end_swap_bio_read's swap_slot_free_notify VS swap_entry_free's one).

But current implementatoin on zram_slot_free_notify could cover both cases properly with luck.

zram_free_page caused by end_swap_bio_read will free compressed copy of the page and zram_free_page caused by swap_entry_free later won't find right index from zram->table and just return.
So I think there is no problem.

Quoted 4 lines

Remained problem is zram->stats.notify_free, which could be counted redundantly but not sure it's valuable to count exactly.

If I miss your point, please pinpoint your concern. :)

Quoted 7 lines

Looking at it again, I do believe you and Dan are perfectly correct, and I was again the confused one. Though I'd be happier if I could see just how I was misreading it: makes me wonder if I had a great insight that I can no longer grasp hold of! I think I was paranoid about a swp_entry_t getting recycled prematurely: but swap_entry_free remains in control of that - freeing a swap entry is no part of what notify_free gets up to. Sorry for wasting your time.

Hey, Hugh, Please don't do apology.
It gives me a chance to look into that part in detail. It never wasted my time.

And your deep insight and kind advise always makes everybody happier.

Looking forward to seeing you soon in LSF/MM.
Thanks!

Signature · 8 lines
Kind regards,
Minchan Kim

--
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>
#20 lore raw
In reply to Hugh Dickins

Hi Hugh,

Quoted 21 lines · Hugh Dickins
Quoted 2 lines · Minchan Kim

Swap subsystem does lazy swap slot free with expecting the page would be swapped out again so we can't avoid unnecessary write.

so we can avoid unnecessary write.

Quoted 5 lines

But the problem in in-memory swap is that it consumes memory space until vm_swap_full(ie, used half of all of swap device) condition meet. It could be bad if we use multiple swap device, small in-memory swap and big storage swap or in-memory swap alone.

That is a very good realization: it's surprising that none of us thought of it before - no disrespect to you, well done, thank you.

And I guess swap readahead is utterly unhelpful in this case too.

Quoted 4 lines

This patch changes vm_swap_full logic slightly so it could free swap slot early if the backed device is really fast. For it, I used SWP_SOLIDSTATE but It might be controversial.

Quoted 8 lines

But I strongly disagree with almost everything in your patch :) I disagree with addressing it in vm_swap_full(), I disagree that it can be addressed by device, I disagree that it has anything to do with SWP_SOLIDSTATE.

This is not a problem with swapping to /dev/ram0 or to /dev/zram0, is it? In those cases, a fixed amount of memory has been set aside for swap, and it works out just like with disk block devices. The

Brd is okay but it seems you are miunderstanding zram.
The zram doesn't reserve any memory and allocate dynamic memory when swap out happens so it can make duplicate space in pusdo block device and memory.

Quoted 5 lines

memory set aside may be wasted, but that is accepted upfront.

Similarly, this is not a problem with swapping to SSD. There might or might not be other reasons for adjusting the vm_swap_full() logic for SSD or generally, but those have nothing to do with this issue.

Yes.

Quoted 3 lines

The problem here is peculiar to frontswap, and the variably sized memory behind it, isn't it? We are accustomed to using swap to free

Zram, too.

Quoted 4 lines

up memory by transferring its data to some other, cheaper but slower resource.

But in the case of frontswap and zmem (I'll say that to avoid thinking

Frankly speaking, I couldn't understand what you means, frontswap and zmem. The frontswap is just layer for hook the swap subsystem.
Real instance of frontswap is zcache and zswap at the moment.
I will understand them as zcache and zswap. Okay?

Quoted 6 lines

through which backends are actually involved), it is not a cheaper and slower resource, but the very same memory we are trying to save: swap is stolen from the memory under reclaim, so any duplication becomes counter-productive (if we ignore cpu compression/decompression costs:
I have no idea how fair it is to do so, but anyone who chooses zmem is prepared to pay some cpu price for that).

Agree.

Quoted 6 lines

And because it's a frontswap thing, we cannot decide this by device:
frontswap may or may not stand in front of each device. There is no problem with swapcache duplicated on disk (until that area approaches being full or fragmented), but at the higher level we cannot see what is in zmem and what is on disk: we only want to free up the zmem dup.

That's what I really have a concern and why I begged idea.

Quoted 6 lines

I believe the answer is for frontswap/zmem to invalidate the frontswap copy of the page (to free up the compressed memory when possible) and SetPageDirty on the PageUptodate PageSwapCache page when swapping in (setting page dirty so nothing will later go to read it from the unfreed location on backing swap disk, which was never written).

You mean that zcache and zswap have to do garbage collection by some policy? It could be but how about zram? It's just pseudo block device and he don't have any knowledge on top of it. It could be swap or normal block device. I mean zram has no information of swap to handle it.

Quoted 8 lines

We cannot rely on freeing the swap itself, because in general there may be multiple references to the swap, and we only satisfy the one which has faulted. It may or may not be a good idea to use rmap to locate the other places to insert pte in place of swap entry, to resolve them all at once; but we have chosen not to do so in the past, and there's no need for that, if the zmem gets invalidated and the swapcache page set dirty.

Yes it could be better but as I mentioned above, it couldn't handle zram case. If there is a solution for zram, I will be happy. :)

And another point, fronstwap is already percolated into swap subsystem very tightly. So I doubt adding one another hook is a really problem.

Thanks for great comment, Hugh!

Quoted 140 lines

Hugh

Quoted 131 lines

So let's add Ccing Shaohua and Hugh.
If it's a problem for SSD, I'd like to create new type SWP_INMEMORY or something for z* family.

Other problem is zram is block device so that it can set SWP_INMEMORY or SWP_SOLIDSTATE easily(ie, actually, zram is already done) but I have no idea to use it for frontswap.

Any idea?

Other optimize point is we remove it unconditionally when we found it's exclusive when swap in happen.
It could help frontswap family, too.
What do you think about it?

Cc Hugh Dickins <[email protected]>
Cc Dan Magenheimer <[email protected]>
Cc Seth Jennings <[email protected]>
Cc Nitin Gupta <[email protected]>
Cc Konrad Rzeszutek Wilk <[email protected]>
Cc Shaohua Li <[email protected]>
---
 include/linux/swap.h | 11 ++++++++---
 mm/memory.c          |  3 ++-
 mm/swapfile.c        | 11 +++++++----
 mm/vmscan.c          |  2 +-
 4 files changed, 18 insertions(+), 9 deletions(-)
include/linux/swap.h +8 -3
index 2818a12..1f4df66 100644
@@ -359,9 +359,14 @@ extern struct page *swapin_readahead(swp_entry_t, gfp_t,
 extern atomic_long_t nr_swap_pages;
 extern long total_swap_pages;
 
-/* Swap 50% full? Release swapcache more aggressively.. */
-static inline bool vm_swap_full(void)
+/*
+ * Swap 50% full or fast backed device?
+ * Release swapcache more aggressively.
+ */
+static inline bool vm_swap_full(struct swap_info_struct *si)
 {
+	if (si->flags & SWP_SOLIDSTATE)
+		return true;
 	return atomic_long_read(&nr_swap_pages) * 2 < total_swap_pages;
 }
 
@@ -405,7 +410,7 @@ mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
 #define get_nr_swap_pages()			0L
 #define total_swap_pages			0L
 #define total_swapcache_pages()			0UL
-#define vm_swap_full()				0
+#define vm_swap_full(si)			0
 
 #define si_swapinfo(val) \
 	do { (val)->freeswap = (val)->totalswap = 0; } while (0)
mm/memory.c +2 -1
index 705473a..1ca21a9 100644
@@ -3084,7 +3084,8 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
 	mem_cgroup_commit_charge_swapin(page, ptr);
 
 	swap_free(entry);
-	if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
+	if (likely(PageSwapCache(page)) && (vm_swap_full(page_swap_info(page))
+			|| (vma->vm_flags & VM_LOCKED) || PageMlocked(page)))
 		try_to_free_swap(page);
 	unlock_page(page);
 	if (page != swapcache) {
mm/swapfile.c +7 -4
index 1bee6fa..f9cc701 100644
@@ -293,7 +293,7 @@ checks:
 		scan_base = offset = si->lowest_bit;
 
 	/* reuse swap entry of cache-only swap if not busy. */
-	if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+	if (vm_swap_full(si) && si->swap_map[offset] == SWAP_HAS_CACHE) {
 		int swap_was_freed;
 		spin_unlock(&si->lock);
 		swap_was_freed = __try_to_reclaim_swap(si, offset);
@@ -382,7 +382,8 @@ scan:
 			spin_lock(&si->lock);
 			goto checks;
 		}
-		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+		if (vm_swap_full(si) &&
+			si->swap_map[offset] == SWAP_HAS_CACHE) {
 			spin_lock(&si->lock);
 			goto checks;
 		}
@@ -397,7 +398,8 @@ scan:
 			spin_lock(&si->lock);
 			goto checks;
 		}
-		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+		if (vm_swap_full(si) &&
+			si->swap_map[offset] == SWAP_HAS_CACHE) {
 			spin_lock(&si->lock);
 			goto checks;
 		}
@@ -763,7 +765,8 @@ int free_swap_and_cache(swp_entry_t entry)
 		 * Also recheck PageSwapCache now page is locked (above).
 		 */
 		if (PageSwapCache(page) && !PageWriteback(page) &&
-				(!page_mapped(page) || vm_swap_full())) {
+				(!page_mapped(page) ||
+				  vm_swap_full(page_swap_info(page)))) {
 			delete_from_swap_cache(page);
 			SetPageDirty(page);
 		}
mm/vmscan.c +1 -1
index df78d17..145c59c 100644
@@ -933,7 +933,7 @@ cull_mlocked:
 
 activate_locked:
 		/* Not a candidate for swapping, so reclaim swap space. */
-		if (PageSwapCache(page) && vm_swap_full())
+		if (PageSwapCache(page) && vm_swap_full(page_swap_info(page)))
 			try_to_free_swap(page);
 		VM_BUG_ON(PageActive(page));
 		SetPageActive(page);
Signature · 1 line
1.8.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>

Signature · 8 lines
Kind regards,
Minchan Kim

--
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>
#21 lore raw
In reply to Hugh Dickins

Hi Hugh,

Quoted 5 lines · Hugh Dickins
Quoted 2 lines · Minchan Kim

Swap subsystem does lazy swap slot free with expecting the page would be swapped out again so we can't avoid unnecessary write.

so we can avoid unnecessary write.

If page can be swap out again, which codes can avoid unnecessary write? Could you point out to me? Thanks in advance. ;-)

Quoted 183 lines
Quoted 4 lines

But the problem in in-memory swap is that it consumes memory space until vm_swap_full(ie, used half of all of swap device) condition meet. It could be bad if we use multiple swap device, small in-memory swap and big storage swap or in-memory swap alone.

That is a very good realization: it's surprising that none of us thought of it before - no disrespect to you, well done, thank you.

And I guess swap readahead is utterly unhelpful in this case too.

Quoted 3 lines

This patch changes vm_swap_full logic slightly so it could free swap slot early if the backed device is really fast. For it, I used SWP_SOLIDSTATE but It might be controversial.

But I strongly disagree with almost everything in your patch :) I disagree with addressing it in vm_swap_full(), I disagree that it can be addressed by device, I disagree that it has anything to do with SWP_SOLIDSTATE.

This is not a problem with swapping to /dev/ram0 or to /dev/zram0, is it? In those cases, a fixed amount of memory has been set aside for swap, and it works out just like with disk block devices. The memory set aside may be wasted, but that is accepted upfront.

Similarly, this is not a problem with swapping to SSD. There might or might not be other reasons for adjusting the vm_swap_full() logic for SSD or generally, but those have nothing to do with this issue.

The problem here is peculiar to frontswap, and the variably sized memory behind it, isn't it? We are accustomed to using swap to free up memory by transferring its data to some other, cheaper but slower resource.

But in the case of frontswap and zmem (I'll say that to avoid thinking through which backends are actually involved), it is not a cheaper and slower resource, but the very same memory we are trying to save: swap is stolen from the memory under reclaim, so any duplication becomes counter-productive (if we ignore cpu compression/decompression costs:
I have no idea how fair it is to do so, but anyone who chooses zmem is prepared to pay some cpu price for that).

And because it's a frontswap thing, we cannot decide this by device:
frontswap may or may not stand in front of each device. There is no problem with swapcache duplicated on disk (until that area approaches being full or fragmented), but at the higher level we cannot see what is in zmem and what is on disk: we only want to free up the zmem dup.

I believe the answer is for frontswap/zmem to invalidate the frontswap copy of the page (to free up the compressed memory when possible) and SetPageDirty on the PageUptodate PageSwapCache page when swapping in (setting page dirty so nothing will later go to read it from the unfreed location on backing swap disk, which was never written).

We cannot rely on freeing the swap itself, because in general there may be multiple references to the swap, and we only satisfy the one which has faulted. It may or may not be a good idea to use rmap to locate the other places to insert pte in place of swap entry, to resolve them all at once; but we have chosen not to do so in the past, and there's no need for that, if the zmem gets invalidated and the swapcache page set dirty.

Hugh

Quoted 122 lines

So let's add Ccing Shaohua and Hugh.
If it's a problem for SSD, I'd like to create new type SWP_INMEMORY or something for z* family.

Other problem is zram is block device so that it can set SWP_INMEMORY or SWP_SOLIDSTATE easily(ie, actually, zram is already done) but I have no idea to use it for frontswap.

Any idea?

Other optimize point is we remove it unconditionally when we found it's exclusive when swap in happen.
It could help frontswap family, too.
What do you think about it?

Cc Hugh Dickins <[email protected]>
Cc Dan Magenheimer <[email protected]>
Cc Seth Jennings <[email protected]>
Cc Nitin Gupta <[email protected]>
Cc Konrad Rzeszutek Wilk <[email protected]>
Cc Shaohua Li <[email protected]>
---
  include/linux/swap.h | 11 ++++++++---
  mm/memory.c          |  3 ++-
  mm/swapfile.c        | 11 +++++++----
  mm/vmscan.c          |  2 +-
  4 files changed, 18 insertions(+), 9 deletions(-)
include/linux/swap.h +8 -3
index 2818a12..1f4df66 100644
@@ -359,9 +359,14 @@ extern struct page *swapin_readahead(swp_entry_t, gfp_t,
  extern atomic_long_t nr_swap_pages;
  extern long total_swap_pages;
 
-/* Swap 50% full? Release swapcache more aggressively.. */
-static inline bool vm_swap_full(void)
+/*
+ * Swap 50% full or fast backed device?
+ * Release swapcache more aggressively.
+ */
+static inline bool vm_swap_full(struct swap_info_struct *si)
  {
+	if (si->flags & SWP_SOLIDSTATE)
+		return true;
  	return atomic_long_read(&nr_swap_pages) * 2 < total_swap_pages;
  }
 
@@ -405,7 +410,7 @@ mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
  #define get_nr_swap_pages()			0L
  #define total_swap_pages			0L
  #define total_swapcache_pages()			0UL
-#define vm_swap_full()				0
+#define vm_swap_full(si)			0
 
  #define si_swapinfo(val) \
  	do { (val)->freeswap = (val)->totalswap = 0; } while (0)
mm/memory.c +2 -1
index 705473a..1ca21a9 100644
@@ -3084,7 +3084,8 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
  	mem_cgroup_commit_charge_swapin(page, ptr);
 
  	swap_free(entry);
-	if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
+	if (likely(PageSwapCache(page)) && (vm_swap_full(page_swap_info(page))
+			|| (vma->vm_flags & VM_LOCKED) || PageMlocked(page)))
  		try_to_free_swap(page);
  	unlock_page(page);
  	if (page != swapcache) {
mm/swapfile.c +7 -4
index 1bee6fa..f9cc701 100644
@@ -293,7 +293,7 @@ checks:
  		scan_base = offset = si->lowest_bit;
 
  	/* reuse swap entry of cache-only swap if not busy. */
-	if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+	if (vm_swap_full(si) && si->swap_map[offset] == SWAP_HAS_CACHE) {
  		int swap_was_freed;
  		spin_unlock(&si->lock);
  		swap_was_freed = __try_to_reclaim_swap(si, offset);
@@ -382,7 +382,8 @@ scan:
  			spin_lock(&si->lock);
  			goto checks;
  		}
-		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+		if (vm_swap_full(si) &&
+			si->swap_map[offset] == SWAP_HAS_CACHE) {
  			spin_lock(&si->lock);
  			goto checks;
  		}
@@ -397,7 +398,8 @@ scan:
  			spin_lock(&si->lock);
  			goto checks;
  		}
-		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+		if (vm_swap_full(si) &&
+			si->swap_map[offset] == SWAP_HAS_CACHE) {
  			spin_lock(&si->lock);
  			goto checks;
  		}
@@ -763,7 +765,8 @@ int free_swap_and_cache(swp_entry_t entry)
  		 * Also recheck PageSwapCache now page is locked (above).
  		 */
  		if (PageSwapCache(page) && !PageWriteback(page) &&
-				(!page_mapped(page) || vm_swap_full())) {
+				(!page_mapped(page) ||
+				  vm_swap_full(page_swap_info(page)))) {
  			delete_from_swap_cache(page);
  			SetPageDirty(page);
  		}
mm/vmscan.c +0 -0
index df78d17..145c59c 100644
@@ -933,7 +933,7 @@ cull_mlocked:
 
Quoted 14 lines
Quoted 9 lines
mm/vmscan.c +1 -1
  activate_locked:
  		/* Not a candidate for swapping, so reclaim swap space. */
-		if (PageSwapCache(page) && vm_swap_full())
+		if (PageSwapCache(page) && vm_swap_full(page_swap_info(page)))
  			try_to_free_swap(page);
  		VM_BUG_ON(PageActive(page));
  		SetPageActive(page);
Signature · 1 line
1.8.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>

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

#22 lore raw

Ping Minchan.

Quoted 209 lines · Simon Jeons

Hi Hugh,

Quoted 5 lines · Hugh Dickins
Quoted 2 lines · Minchan Kim

Swap subsystem does lazy swap slot free with expecting the page would be swapped out again so we can't avoid unnecessary write.

so we can avoid unnecessary write.

If page can be swap out again, which codes can avoid unnecessary write? Could you point out to me? Thanks in advance. ;-)

Quoted 198 lines
Quoted 4 lines

But the problem in in-memory swap is that it consumes memory space until vm_swap_full(ie, used half of all of swap device) condition meet. It could be bad if we use multiple swap device, small in-memory swap and big storage swap or in-memory swap alone.

That is a very good realization: it's surprising that none of us thought of it before - no disrespect to you, well done, thank you.

And I guess swap readahead is utterly unhelpful in this case too.

Quoted 3 lines

This patch changes vm_swap_full logic slightly so it could free swap slot early if the backed device is really fast. For it, I used SWP_SOLIDSTATE but It might be controversial.

But I strongly disagree with almost everything in your patch :) I disagree with addressing it in vm_swap_full(), I disagree that it can be addressed by device, I disagree that it has anything to do with SWP_SOLIDSTATE.

This is not a problem with swapping to /dev/ram0 or to /dev/zram0, is it? In those cases, a fixed amount of memory has been set aside for swap, and it works out just like with disk block devices. The memory set aside may be wasted, but that is accepted upfront.

Similarly, this is not a problem with swapping to SSD. There might or might not be other reasons for adjusting the vm_swap_full() logic for SSD or generally, but those have nothing to do with this issue.

The problem here is peculiar to frontswap, and the variably sized memory behind it, isn't it? We are accustomed to using swap to free up memory by transferring its data to some other, cheaper but slower resource.

But in the case of frontswap and zmem (I'll say that to avoid thinking through which backends are actually involved), it is not a cheaper and slower resource, but the very same memory we are trying to save: swap is stolen from the memory under reclaim, so any duplication becomes counter-productive (if we ignore cpu compression/decompression costs:
I have no idea how fair it is to do so, but anyone who chooses zmem is prepared to pay some cpu price for that).

And because it's a frontswap thing, we cannot decide this by device:
frontswap may or may not stand in front of each device. There is no problem with swapcache duplicated on disk (until that area approaches being full or fragmented), but at the higher level we cannot see what is in zmem and what is on disk: we only want to free up the zmem dup.

I believe the answer is for frontswap/zmem to invalidate the frontswap copy of the page (to free up the compressed memory when possible) and SetPageDirty on the PageUptodate PageSwapCache page when swapping in (setting page dirty so nothing will later go to read it from the unfreed location on backing swap disk, which was never written).

We cannot rely on freeing the swap itself, because in general there may be multiple references to the swap, and we only satisfy the one which has faulted. It may or may not be a good idea to use rmap to locate the other places to insert pte in place of swap entry, to resolve them all at once; but we have chosen not to do so in the past, and there's no need for that, if the zmem gets invalidated and the swapcache page set dirty.

Hugh

Quoted 132 lines

So let's add Ccing Shaohua and Hugh.
If it's a problem for SSD, I'd like to create new type SWP_INMEMORY or something for z* family.

Other problem is zram is block device so that it can set SWP_INMEMORY or SWP_SOLIDSTATE easily(ie, actually, zram is already done) but I have no idea to use it for frontswap.

Any idea?

Other optimize point is we remove it unconditionally when we found it's exclusive when swap in happen.
It could help frontswap family, too.
What do you think about it?

Cc Hugh Dickins <[email protected]>
Cc Dan Magenheimer <[email protected]>
Cc Seth Jennings <[email protected]>
Cc Nitin Gupta <[email protected]>
Cc Konrad Rzeszutek Wilk <[email protected]>
Cc Shaohua Li <[email protected]>
---
  include/linux/swap.h | 11 ++++++++---
  mm/memory.c          |  3 ++-
  mm/swapfile.c        | 11 +++++++----
  mm/vmscan.c          |  2 +-
  4 files changed, 18 insertions(+), 9 deletions(-)
include/linux/swap.h +7 -1
index 2818a12..1f4df66 100644
@@ -359,9 +359,14 @@ extern struct page 
 *swapin_readahead(swp_entry_t, gfp_t,
  extern atomic_long_t nr_swap_pages;
  extern long total_swap_pages;
  -/* Swap 50% full? Release swapcache more aggressively.. */
-static inline bool vm_swap_full(void)
+/*
+ * Swap 50% full or fast backed device?
+ * Release swapcache more aggressively.
+ */
+static inline bool vm_swap_full(struct swap_info_struct *si)
  {
+    if (si->flags & SWP_SOLIDSTATE)
+        return true;
      return atomic_long_read(&nr_swap_pages) * 2 < total_swap_pages;
  }
  @@ -405,7 +410,7 @@ mem_cgroup_uncharge_swapcache(struct page 

*page, swp_entry_t ent, bool swapout)
#define get_nr_swap_pages() 0L
#define total_swap_pages 0L
#define total_swapcache_pages() 0UL

include/linux/swap.h +1 -1
-#define vm_swap_full()                0
+#define vm_swap_full(si)            0
    #define si_swapinfo(val) \
      do { (val)->freeswap = (val)->totalswap = 0; } while (0)
mm/memory.c +2 -1
index 705473a..1ca21a9 100644
@@ -3084,7 +3084,8 @@ static int do_swap_page(struct mm_struct *mm, 
 struct vm_area_struct *vma,
      mem_cgroup_commit_charge_swapin(page, ptr);
        swap_free(entry);
-    if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || 
 PageMlocked(page))
+    if (likely(PageSwapCache(page)) && 
 (vm_swap_full(page_swap_info(page))
+            || (vma->vm_flags & VM_LOCKED) || PageMlocked(page)))
          try_to_free_swap(page);
      unlock_page(page);
      if (page != swapcache) {
mm/swapfile.c +3 -2
index 1bee6fa..f9cc701 100644
@@ -293,7 +293,7 @@ checks:
          scan_base = offset = si->lowest_bit;
        /* reuse swap entry of cache-only swap if not busy. */
-    if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+    if (vm_swap_full(si) && si->swap_map[offset] == SWAP_HAS_CACHE) {
          int swap_was_freed;
          spin_unlock(&si->lock);
          swap_was_freed = __try_to_reclaim_swap(si, offset);
@@ -382,7 +382,8 @@ scan:
              spin_lock(&si->lock);
              goto checks;
          }
-        if (vm_swap_full() && si->swap_map[offset] == 
 SWAP_HAS_CACHE) {
+        if (vm_swap_full(si) &&
+            si->swap_map[offset] == SWAP_HAS_CACHE) {
              spin_lock(&si->lock);
              goto checks;
          }
mm/swapfile.c +2 -1
@@ -397,7 +398,8 @@ scan:
              spin_lock(&si->lock);
              goto checks;
          }
-        if (vm_swap_full() && si->swap_map[offset] == 
 SWAP_HAS_CACHE) {
+        if (vm_swap_full(si) &&
+            si->swap_map[offset] == SWAP_HAS_CACHE) {
              spin_lock(&si->lock);
              goto checks;
          }
mm/swapfile.c +2 -1
@@ -763,7 +765,8 @@ int free_swap_and_cache(swp_entry_t entry)
           * Also recheck PageSwapCache now page is locked (above).
           */
          if (PageSwapCache(page) && !PageWriteback(page) &&
-                (!page_mapped(page) || vm_swap_full())) {
+                (!page_mapped(page) ||
+                  vm_swap_full(page_swap_info(page)))) {
              delete_from_swap_cache(page);
              SetPageDirty(page);
          }
mm/vmscan.c +1 -1
index df78d17..145c59c 100644
@@ -933,7 +933,7 @@ cull_mlocked:
    activate_locked:
          /* Not a candidate for swapping, so reclaim swap space. */
-        if (PageSwapCache(page) && vm_swap_full())
+        if (PageSwapCache(page) && vm_swap_full(page_swap_info(page)))
              try_to_free_swap(page);
          VM_BUG_ON(PageActive(page));
          SetPageActive(page);
Signature · 1 line
1.8.2
Signature · 4 lines
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>

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

#23 lore raw

Hello Simon,

Quoted 12 lines · Simon Jeons

Ping Minchan.

Quoted 10 lines · Simon Jeons

Hi Hugh,

Quoted 5 lines · Hugh Dickins
Quoted 2 lines · Minchan Kim

Swap subsystem does lazy swap slot free with expecting the page would be swapped out again so we can't avoid unnecessary write.

so we can avoid unnecessary write.

If page can be swap out again, which codes can avoid unnecessary write? Could you point out to me? Thanks in advance. ;-)

Look at shrink_page_list.

1) PageAnon(page) && !PageSwapCache()
2) add_to_swap's SetPageDirty
3) __remove_mapping

P.S)
It seems you are misunderstanding. Here isn't proper place to ask a question for your understanding the code. As I know, there are some project(ex, kernelnewbies) and books for study and sharing the knowledge linux kernel.

I recommend Mel's "Understand the Linux Virtual Memory Manager". It's rather outdated but will be very helpful to understand VM of linux kernel. You can get it freely but I hope you pay for. So if author become a billionaire by selecting best book in Amazon, he might print out second edition which covers all of new VM features and may solve all of you curiosity.

It would be a another method to contribute open source project. :)

I believe you talented developers can catch it up with reading the code enoughly and find more bonus knowledge. I think it's why our senior developers yell out RTFM and I follow them.

Cheers!

Signature · 8 lines
Kind regards,
Minchan Kim

--
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>
#24 lore raw
Quoted 33 lines · Minchan Kim

Hello Simon,

Quoted 11 lines · Simon Jeons

Ping Minchan.

Quoted 9 lines · Simon Jeons

Hi Hugh,

Quoted 5 lines · Hugh Dickins
Quoted 2 lines · Minchan Kim

Swap subsystem does lazy swap slot free with expecting the page would be swapped out again so we can't avoid unnecessary write.

so we can avoid unnecessary write.

If page can be swap out again, which codes can avoid unnecessary write? Could you point out to me? Thanks in advance. ;-)

Look at shrink_page_list.

1) PageAnon(page) && !PageSwapCache()
2) add_to_swap's SetPageDirty
3) __remove_mapping

P.S)
It seems you are misunderstanding. Here isn't proper place to ask a question for your understanding the code. As I know, there are some project(ex, kernelnewbies) and books for study and sharing the knowledge linux kernel.

I recommend Mel's "Understand the Linux Virtual Memory Manager". It's rather outdated but will be very helpful to understand VM of linux kernel. You can get it freely but I hope you pay for. So if author become a billionaire by selecting best book in Amazon, he might print out second edition which covers all of new VM features and may solve all of you curiosity.

Quoted 5 lines

It would be a another method to contribute open source project. :)

I believe you talented developers can catch it up with reading the code enoughly and find more bonus knowledge. I think it's why our senior developers yell out RTFM and I follow them.

What's the meaning of RTFM?

Quoted 4 lines

Cheers!

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