Skip to contentSkip to conversation

THP swap: Delay splitting THP during swapping out

RFC In review Huang, Ying 3 participants 27 messages Aug 9, 2016 – Aug 24, 2016

Subsystems (6)
Memory management MM core memcg THP +2 more subsystems
lore
Share & tools
lore
#0 lore raw

cover THP swap: Delay splitting THP during swapping out

From: Huang Ying <[email protected]>

This patchset is based on 8/4 head of mmotm/master.

This is the first step for Transparent Huge Page (THP) swap support. The plan is to delaying splitting THP step by step and avoid splitting THP finally during THP swapping out and swapping in.

The advantages of THP swap support are:

- Batch swap operations for THP to reduce lock acquiring/releasing, including allocating/freeing swap space, adding/deleting to/from swap cache, and writing/reading swap space, etc.

- THP swap space read/write will be 2M sequence IO. It is particularly helpful for swap read, which usually are 4k random IO.

- It will help memory fragmentation, especially when THP is heavily used by the applications. 2M continuous pages will be free up after THP swapping out.

As the first step, in this patchset, the splitting huge page is delayed from almost the first step of swapping out to after allocating the swap space for THP and adding the THP into swap cache. This will reduce lock acquiring/releasing for locks used for swap space and swap cache management.

With the patchset, the swap out bandwidth improved 12.1% in vm-scalability swap-w-seq test case with 16 processes on a Xeon E5 v3 system. To test sequence swap out, the test case uses 16 processes sequentially allocate and write to anonymous pages until RAM and part of the swap device is used up.

The detailed compare result is as follow,

base base+patchset
---------------- --------------------------
%stddev %change %stddev
\ | \

   1118821 A+-  0%     +12.1%    1254241 A+-  1%  vmstat.swap.so
   2460636 A+-  1%     +10.6%    2720983 A+-  1%  vm-scalability.throughput
    308.79 A+-  1%      -7.9%     284.53 A+-  1%  vm-scalability.time.elapsed_time
      1639 A+-  4%    +232.3%       5446 A+-  1%  meminfo.SwapCached
      0.70 A+-  3%      +8.7%       0.77 A+-  5%  perf-stat.ipc
      9.82 A+-  8%     -31.6%       6.72 A+-  2%  perf-profile.cycles-pp._raw_spin_lock_irq.__add_to_swap_cache.add_to_swap_cache.add_to_swap.shrink_page_list

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

RFC 1/11 swap: Add swap_cluster_list

From: Huang Ying <[email protected]>

This is a code clean up patch without functionality changes. The swap_cluster_list data structure and its operations is introduced to provide some better encapsulation for free cluster and discard cluster list operations. This avoid some code duplication, improved the code readability, and reduced the total line number.

Cc Hugh Dickins <[email protected]>
Cc Shaohua Li <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Rik van Riel <[email protected]>
---
 include/linux/swap.h |  11 +++--
 mm/swapfile.c        | 132 ++++++++++++++++++++++++---------------------------
 2 files changed, 69 insertions(+), 74 deletions(-)
include/linux/swap.h +7 -4
index b17cc48..ed41bec 100644
@@ -191,6 +191,11 @@ struct percpu_cluster {
 	unsigned int next; /* Likely next allocation offset */
 };
 
+struct swap_cluster_list {
+	struct swap_cluster_info head;
+	struct swap_cluster_info tail;
+};
+
 /*
  * The in-memory structure used to track swap areas.
  */
@@ -203,8 +208,7 @@ struct swap_info_struct {
 	unsigned int	max;		/* extent of the swap_map */
 	unsigned char *swap_map;	/* vmalloc'ed array of usage counts */
 	struct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */
-	struct swap_cluster_info free_cluster_head; /* free cluster list head */
-	struct swap_cluster_info free_cluster_tail; /* free cluster list tail */
+	struct swap_cluster_list free_clusters; /* free clusters list */
 	unsigned int lowest_bit;	/* index of first free in swap_map */
 	unsigned int highest_bit;	/* index of last free in swap_map */
 	unsigned int pages;		/* total of usable pages of swap */
@@ -235,8 +239,7 @@ struct swap_info_struct {
 					 * first.
 					 */
 	struct work_struct discard_work; /* discard worker */
-	struct swap_cluster_info discard_cluster_head; /* list head of discard clusters */
-	struct swap_cluster_info discard_cluster_tail; /* list tail of discard clusters */
+	struct swap_cluster_list discard_clusters; /* discard clusters list */
 };
 
 /* linux/mm/workingset.c */
mm/swapfile.c +62 -70

194 lines not on this page · raw message

Signature · 7 lines
2.8.1

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

RFC 2/11 swap: Change SWAPFILE_CLUSTER to 512

In reply to Huang, Ying

From: Huang Ying <[email protected]>

In this patch, the size of swap cluster is changed to that of THP on x86_64 (512). This is for THP (Transparent Huge Page) swap support on x86_64. Where one swap cluster will be used to hold the contents of each THP swapped out. And some information of the swapped out THP (such as compound map count) will be recorded in the swap_cluster_info data structure.

In effect, this will enlarge swap cluster size by 2 times. Which may make it harder to find a free cluster when swap space becomes fragmented. So that, this may reduce the continuous swap space allocation and sequence write if that happens in theory. The performance test in 0day show no regressions caused by this.

Cc Hugh Dickins <[email protected]>
Cc Shaohua Li <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Rik van Riel <[email protected]>
---
 mm/swapfile.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/swapfile.c +1 -1
index 09e3877..18f9292 100644
@@ -196,7 +196,7 @@ static void discard_swap_cluster(struct swap_info_struct *si,
 	}
 }
 
-#define SWAPFILE_CLUSTER	256
+#define SWAPFILE_CLUSTER	512
 #define LATENCY_LIMIT		256
 
 static inline void cluster_set_flag(struct swap_cluster_info *info,
Signature · 7 lines
2.8.1

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

RFC 3/11 mm, memcg: Add swap_cgroup_iter iterator

In reply to Huang, Ying

From: Huang Ying <[email protected]>

Swap cgroup uses a discontinuous array to store the information for the swap entries. lookup_swap_cgroup() provides the good encapsulation to access one element of the discontinuous array. To make it easier to access multiple elements of the discontinuous array, an iterator for swap cgroup named swap_cgroup_iter is added in this patch.

This will be used for transparent huge page (THP) swap support. Where the swap_cgroup for multiple swap entries will be changed together.

Cc Andrea Arcangeli <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Vladimir Davydov <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Tejun Heo <[email protected]>
---
 mm/swap_cgroup.c | 62 +++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 46 insertions(+), 16 deletions(-)
mm/swap_cgroup.c +46 -16
index 310ac0b..3563b8b 100644
@@ -18,6 +18,13 @@ struct swap_cgroup {
 };
 #define SC_PER_PAGE	(PAGE_SIZE/sizeof(struct swap_cgroup))
 
+struct swap_cgroup_iter {
+	struct swap_cgroup_ctrl *ctrl;
+	struct swap_cgroup *sc;
+	swp_entry_t entry;
+	unsigned long flags;
+};
+
 /*
  * SwapCgroup implements "lookup" and "exchange" operations.
  * In typical usage, this swap_cgroup is accessed via memcg's charge/uncharge
@@ -75,6 +82,34 @@ static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent,
 	return sc + offset % SC_PER_PAGE;
 }
 
+static void swap_cgroup_iter_init(struct swap_cgroup_iter *iter, swp_entry_t ent)
+{
+	iter->entry = ent;
+	iter->sc = lookup_swap_cgroup(ent, &iter->ctrl);
+	spin_lock_irqsave(&iter->ctrl->lock, iter->flags);
+}
+
+static void swap_cgroup_iter_exit(struct swap_cgroup_iter *iter)
+{
+	spin_unlock_irqrestore(&iter->ctrl->lock, iter->flags);
+}
+
+/*
+ * swap_cgroup is stored in a kind of discontinuous array.  That is,
+ * they are continuous in one page, but not across page boundary.  And
+ * there is one lock for each page.
+ */
+static void swap_cgroup_iter_advance(struct swap_cgroup_iter *iter)
+{
+	iter->sc++;
+	iter->entry.val++;
+	if (!(((unsigned long)iter->sc) & PAGE_MASK)) {
+		spin_unlock_irqrestore(&iter->ctrl->lock, iter->flags);
+		iter->sc = lookup_swap_cgroup(iter->entry, &iter->ctrl);
+		spin_lock_irqsave(&iter->ctrl->lock, iter->flags);
+	}
+}
+
 /**
  * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
  * @ent: swap entry to be cmpxchged
@@ -87,20 +122,18 @@ static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent,
 unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
 					unsigned short old, unsigned short new)
 {
-	struct swap_cgroup_ctrl *ctrl;
-	struct swap_cgroup *sc;
-	unsigned long flags;
+	struct swap_cgroup_iter iter;
 	unsigned short retval;
 
-	sc = lookup_swap_cgroup(ent, &ctrl);
+	swap_cgroup_iter_init(&iter, ent);
 
-	spin_lock_irqsave(&ctrl->lock, flags);
-	retval = sc->id;
+	retval = iter.sc->id;
 	if (retval == old)
-		sc->id = new;
+		iter.sc->id = new;
 	else
 		retval = 0;
-	spin_unlock_irqrestore(&ctrl->lock, flags);
+
+	swap_cgroup_iter_exit(&iter);
 	return retval;
 }
 
@@ -114,18 +147,15 @@ unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
  */
 unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
 {
-	struct swap_cgroup_ctrl *ctrl;
-	struct swap_cgroup *sc;
+	struct swap_cgroup_iter iter;
 	unsigned short old;
-	unsigned long flags;
 
-	sc = lookup_swap_cgroup(ent, &ctrl);
+	swap_cgroup_iter_init(&iter, ent);
 
-	spin_lock_irqsave(&ctrl->lock, flags);
-	old = sc->id;
-	sc->id = id;
-	spin_unlock_irqrestore(&ctrl->lock, flags);
+	old = iter.sc->id;
+	iter.sc->id = id;
 
+	swap_cgroup_iter_exit(&iter);
 	return old;
 }
 
Signature · 7 lines
2.8.1

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

RFC 4/11 mm, memcg: Support to charge/uncharge multiple swap entries

In reply to Huang, Ying

From: Huang Ying <[email protected]>

This patch make it possible to charge or uncharge a set of continuous swap entries in swap cgroup. The number of swap entries is specified via an added parameter.

This will be used for THP (Transparent Huge Page) swap support. Where a whole swap cluster backing a THP may be allocated and freed as a whole. So a set of continuous swap entries (512 on x86_64) backing one THP need to be charged or uncharged together. This will batch the cgroup operations for THP swap too.

Cc Andrea Arcangeli <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Vladimir Davydov <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Tejun Heo <[email protected]>
---
 include/linux/swap.h        | 11 +++++----
 include/linux/swap_cgroup.h |  6 +++--
 mm/memcontrol.c             | 54 +++++++++++++++++++++++++--------------------
 mm/shmem.c                  |  2 +-
 mm/swap_cgroup.c            | 17 ++++++++++----
 mm/swap_state.c             |  2 +-
 mm/swapfile.c               |  2 +-
 7 files changed, 57 insertions(+), 37 deletions(-)
include/linux/swap.h +7 -4
index ed41bec..6988bce 100644
@@ -550,8 +550,9 @@ static inline int mem_cgroup_swappiness(struct mem_cgroup *mem)
 
 #ifdef CONFIG_MEMCG_SWAP
 extern void mem_cgroup_swapout(struct page *page, swp_entry_t entry);
-extern int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry);
-extern void mem_cgroup_uncharge_swap(swp_entry_t entry);
+extern int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry,
+				      unsigned int nr_entries);
+extern void mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_entries);
 extern long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg);
 extern bool mem_cgroup_swap_full(struct page *page);
 #else
@@ -560,12 +561,14 @@ static inline void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
 }
 
 static inline int mem_cgroup_try_charge_swap(struct page *page,
-					     swp_entry_t entry)
+					     swp_entry_t entry,
+					     unsigned int nr_entries)
 {
 	return 0;
 }
 
-static inline void mem_cgroup_uncharge_swap(swp_entry_t entry)
+static inline void mem_cgroup_uncharge_swap(swp_entry_t entry,
+					    unsigned int nr_entries)
 {
 }
 
include/linux/swap_cgroup.h +4 -2
index 145306b..b2b8ec7 100644
@@ -7,7 +7,8 @@
 
 extern unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
 					unsigned short old, unsigned short new);
-extern unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id);
+extern unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id,
+					 unsigned int nr_ents);
 extern unsigned short lookup_swap_cgroup_id(swp_entry_t ent);
 extern int swap_cgroup_swapon(int type, unsigned long max_pages);
 extern void swap_cgroup_swapoff(int type);
@@ -15,7 +16,8 @@ extern void swap_cgroup_swapoff(int type);
 #else
 
 static inline
-unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
+unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id,
+				  unsigned int nr_ents)
 {
 	return 0;
 }
mm/memcontrol.c +30 -24

122 lines not on this page · raw message

mm/shmem.c +1 -1
index 7f7748a..fa4067e 100644
@@ -1248,7 +1248,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
 	if (!swap.val)
 		goto redirty;
 
-	if (mem_cgroup_try_charge_swap(page, swap))
+	if (mem_cgroup_try_charge_swap(page, swap, 1))
 		goto free_swap;
 
 	/*
mm/swap_cgroup.c +13 -4
index 3563b8b..a2cafbd 100644
@@ -138,14 +138,16 @@ unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
 }
 
 /**
- * swap_cgroup_record - record mem_cgroup for this swp_entry.
- * @ent: swap entry to be recorded into
+ * swap_cgroup_record - record mem_cgroup for a set of swap entries
+ * @ent: the first swap entry to be recorded into
  * @id: mem_cgroup to be recorded
+ * @nr_ents: number of swap entries to be recorded
  *
  * Returns old value at success, 0 at failure.
  * (Of course, old value can be 0.)
  */
-unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
+unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id,
+				  unsigned int nr_ents)
 {
 	struct swap_cgroup_iter iter;
 	unsigned short old;
@@ -153,7 +155,14 @@ unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
 	swap_cgroup_iter_init(&iter, ent);
 
 	old = iter.sc->id;
-	iter.sc->id = id;
+	for (;;) {
+		VM_BUG_ON(iter.sc->id != old);
+		iter.sc->id = id;
+		nr_ents--;
+		if (!nr_ents)
+			break;
+		swap_cgroup_iter_advance(&iter);
+	}
 
 	swap_cgroup_iter_exit(&iter);
 	return old;
mm/swap_state.c +1 -1
index c8310a3..2013793 100644
@@ -170,7 +170,7 @@ int add_to_swap(struct page *page, struct list_head *list)
 	if (!entry.val)
 		return 0;
 
-	if (mem_cgroup_try_charge_swap(page, entry)) {
+	if (mem_cgroup_try_charge_swap(page, entry, 1)) {
 		swapcache_free(entry);
 		return 0;
 	}
mm/swapfile.c +1 -1
index 18f9292..25363c2 100644
@@ -802,7 +802,7 @@ static unsigned char swap_entry_free(struct swap_info_struct *p,
 
 	/* free if no reference */
 	if (!usage) {
-		mem_cgroup_uncharge_swap(entry);
+		mem_cgroup_uncharge_swap(entry, 1);
 		dec_cluster_info_page(p, p->cluster_info, offset);
 		if (offset < p->lowest_bit)
 			p->lowest_bit = offset;
Signature · 7 lines
2.8.1

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

RFC 8/11 mm, THP, swap: Support to add/delete THP to/from swap cache

In reply to Huang, Ying

From: Huang Ying <[email protected]>

With this patch, a THP (Transparent Huge Page) can be added/deleted to/from swap cache as a set of sub-pages (512 on x86_64).

This will be used for Transparent Huge Page (THP) swap support. Where one THP may be added/delted to/from the swap cache. This will batch swap cache operations to reduce the lock acquire/release times for THP swap too.

Cc Hugh Dickins <[email protected]>
Cc Shaohua Li <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
---
 include/linux/page-flags.h |  2 +-
 mm/swap_state.c            | 57 +++++++++++++++++++++++++++++++---------------
 2 files changed, 40 insertions(+), 19 deletions(-)
include/linux/page-flags.h +1 -1
index 74e4dda..f5bcbea 100644
@@ -314,7 +314,7 @@ PAGEFLAG_FALSE(HighMem)
 #endif
 
 #ifdef CONFIG_SWAP
-PAGEFLAG(SwapCache, swapcache, PF_NO_COMPOUND)
+PAGEFLAG(SwapCache, swapcache, PF_NO_TAIL)
 #else
 PAGEFLAG_FALSE(SwapCache)
 #endif
mm/swap_state.c +39 -18

112 lines not on this page · raw message

Signature · 1 line
2.8.1
#5 lore raw

RFC 5/11 mm, THP, swap: Add swap cluster allocate/free functions

In reply to Huang, Ying

From: Huang Ying <[email protected]>

The swap cluster allocation/free functions are added based on the existing swap cluster management mechanism for SSD. These functions don't work for traditional hard disk because the existing swap cluster management mechanism doesn't work for it. The hard disk support may be added if someone really need that. But that needn't be included in this patchset.

This will be used for THP (Transparent Huge Page) swap support. Where one swap cluster will hold the contents of each THP swapped out.

Cc Andrea Arcangeli <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Shaohua Li <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Rik van Riel <[email protected]>
---
 mm/swapfile.c | 194 +++++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 137 insertions(+), 57 deletions(-)
mm/swapfile.c +137 -57
index 25363c2..d710e0e 100644
@@ -322,6 +322,14 @@ static void swap_cluster_schedule_discard(struct swap_info_struct *si,
 	schedule_work(&si->discard_work);
 }
 
+static void __free_cluster(struct swap_info_struct *si, unsigned long idx)
+{
+	struct swap_cluster_info *ci = si->cluster_info;
+
+	cluster_set_flag(ci + idx, CLUSTER_FLAG_FREE);
+	cluster_list_add_tail(&si->free_clusters, ci, idx);
+}
+
 /*
  * Doing discard actually. After a cluster discard is finished, the cluster
  * will be added to free cluster list. caller should hold si->lock.
@@ -341,8 +349,7 @@ static void swap_do_scheduled_discard(struct swap_info_struct *si)
 				SWAPFILE_CLUSTER);
 
 		spin_lock(&si->lock);
-		cluster_set_flag(&info[idx], CLUSTER_FLAG_FREE);
-		cluster_list_add_tail(&si->free_clusters, info, idx);
+		__free_cluster(si, idx);
 		memset(si->swap_map + idx * SWAPFILE_CLUSTER,
 				0, SWAPFILE_CLUSTER);
 	}
@@ -359,6 +366,34 @@ static void swap_discard_work(struct work_struct *work)
 	spin_unlock(&si->lock);
 }
 
+static void alloc_cluster(struct swap_info_struct *si, unsigned long idx)
+{
+	struct swap_cluster_info *ci = si->cluster_info;
+
+	VM_BUG_ON(cluster_list_first(&si->free_clusters) != idx);
+	cluster_list_del_first(&si->free_clusters, ci);
+	cluster_set_count_flag(ci + idx, 0, 0);
+}
+
+static void free_cluster(struct swap_info_struct *si, unsigned long idx)
+{
+	struct swap_cluster_info *ci = si->cluster_info + idx;
+
+	VM_BUG_ON(cluster_count(ci) != 0);
+	/*
+	 * If the swap is discardable, prepare discard the cluster
+	 * instead of free it immediately. The cluster will be freed
+	 * after discard.
+	 */
+	if ((si->flags & (SWP_WRITEOK | SWP_PAGE_DISCARD)) ==
+	    (SWP_WRITEOK | SWP_PAGE_DISCARD)) {
+		swap_cluster_schedule_discard(si, idx);
+		return;
+	}
+
+	__free_cluster(si, idx);
+}
+
 /*
  * The cluster corresponding to page_nr will be used. The cluster will be
  * removed from free cluster list and its usage counter will be increased.
@@ -370,11 +405,8 @@ static void inc_cluster_info_page(struct swap_info_struct *p,
 
 	if (!cluster_info)
 		return;
-	if (cluster_is_free(&cluster_info[idx])) {
-		VM_BUG_ON(cluster_list_first(&p->free_clusters) != idx);
-		cluster_list_del_first(&p->free_clusters, cluster_info);
-		cluster_set_count_flag(&cluster_info[idx], 0, 0);
-	}
+	if (cluster_is_free(&cluster_info[idx]))
+		alloc_cluster(p, idx);
 
 	VM_BUG_ON(cluster_count(&cluster_info[idx]) >= SWAPFILE_CLUSTER);
 	cluster_set_count(&cluster_info[idx],
@@ -398,21 +430,8 @@ static void dec_cluster_info_page(struct swap_info_struct *p,
 	cluster_set_count(&cluster_info[idx],
 		cluster_count(&cluster_info[idx]) - 1);
 
-	if (cluster_count(&cluster_info[idx]) == 0) {
-		/*
-		 * If the swap is discardable, prepare discard the cluster
-		 * instead of free it immediately. The cluster will be freed
-		 * after discard.
-		 */
-		if ((p->flags & (SWP_WRITEOK | SWP_PAGE_DISCARD)) ==
-				 (SWP_WRITEOK | SWP_PAGE_DISCARD)) {
-			swap_cluster_schedule_discard(p, idx);
-			return;
-		}
-
-		cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE);
-		cluster_list_add_tail(&p->free_clusters, cluster_info, idx);
-	}
+	if (cluster_count(&cluster_info[idx]) == 0)
+		free_cluster(p, idx);
 }
 
 /*
@@ -493,6 +512,68 @@ new_cluster:
 	*scan_base = tmp;
 }
 
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+static inline unsigned int huge_cluster_nr_entries(bool huge)
+{
+	return huge ? SWAPFILE_CLUSTER : 1;
+}
+#else
+#define huge_cluster_nr_entries(huge)	1
+#endif
+
+static void __swap_entry_alloc(struct swap_info_struct *si, unsigned long offset,
+			       bool huge)
+{
+	unsigned int nr_entries = huge_cluster_nr_entries(huge);
+	unsigned int end = offset + nr_entries - 1;
+
+	if (offset == si->lowest_bit)
+		si->lowest_bit += nr_entries;
+	if (end == si->highest_bit)
+		si->highest_bit -= nr_entries;
+	si->inuse_pages += nr_entries;
+	if (si->inuse_pages == si->pages) {
+		si->lowest_bit = si->max;
+		si->highest_bit = 0;
+		spin_lock(&swap_avail_lock);
+		plist_del(&si->avail_list, &swap_avail_head);
+		spin_unlock(&swap_avail_lock);
+	}
+}
+
+static void __swap_entry_free(struct swap_info_struct *si, unsigned long offset,
+			      bool huge)
+{
+	unsigned int nr_entries = huge_cluster_nr_entries(huge);
+	unsigned long end = offset + nr_entries - 1;
+	void (*swap_slot_free_notify)(struct block_device *, unsigned long);
+
+	if (offset < si->lowest_bit)
+		si->lowest_bit = offset;
+	if (end > si->highest_bit) {
+		bool was_full = !si->highest_bit;
+
+		si->highest_bit = end;
+		if (was_full && (si->flags & SWP_WRITEOK)) {
+			spin_lock(&swap_avail_lock);
+			WARN_ON(!plist_node_empty(&si->avail_list));
+			if (plist_node_empty(&si->avail_list))
+				plist_add(&si->avail_list, &swap_avail_head);
+			spin_unlock(&swap_avail_lock);
+		}
+	}
+	atomic_long_add(nr_entries, &nr_swap_pages);
+	si->inuse_pages -= nr_entries;
+	if (si->flags & SWP_BLKDEV)
+		swap_slot_free_notify = si->bdev->bd_disk->fops->swap_slot_free_notify;
+	while (offset <= end) {
+		frontswap_invalidate_page(si->type, offset);
+		if (swap_slot_free_notify)
+			swap_slot_free_notify(si->bdev, offset);
+		offset++;
+	}
+}
+
 static unsigned long scan_swap_map(struct swap_info_struct *si,
 				   unsigned char usage)
 {
@@ -587,18 +668,7 @@ checks:
 	if (si->swap_map[offset])
 		goto scan;
 
-	if (offset == si->lowest_bit)
-		si->lowest_bit++;
-	if (offset == si->highest_bit)
-		si->highest_bit--;
-	si->inuse_pages++;
-	if (si->inuse_pages == si->pages) {
-		si->lowest_bit = si->max;
-		si->highest_bit = 0;
-		spin_lock(&swap_avail_lock);
-		plist_del(&si->avail_list, &swap_avail_head);
-		spin_unlock(&swap_avail_lock);
-	}
+	__swap_entry_alloc(si, offset, false);
 	si->swap_map[offset] = usage;
 	inc_cluster_info_page(si, si->cluster_info, offset);
 	si->cluster_next = offset + 1;
@@ -645,6 +715,38 @@ no_page:
 	return 0;
 }
 
+static void swap_free_huge_cluster(struct swap_info_struct *si, unsigned long idx)
+{
+	struct swap_cluster_info *ci = si->cluster_info + idx;
+	unsigned long offset = idx * SWAPFILE_CLUSTER;
+
+	cluster_set_count_flag(ci, 0, 0);
+	free_cluster(si, idx);
+	__swap_entry_free(si, offset, true);
+}
+
+static unsigned long swap_alloc_huge_cluster(struct swap_info_struct *si)
+{
+	unsigned long idx;
+	struct swap_cluster_info *ci;
+	unsigned long offset, i;
+	unsigned char *map;
+
+	if (cluster_list_empty(&si->free_clusters))
+		return 0;
+	idx = cluster_list_first(&si->free_clusters);
+	alloc_cluster(si, idx);
+	ci = si->cluster_info + idx;
+	cluster_set_count_flag(ci, SWAPFILE_CLUSTER, 0);
+
+	offset = idx * SWAPFILE_CLUSTER;
+	__swap_entry_alloc(si, offset, true);
+	map = si->swap_map + offset;
+	for (i = 0; i < SWAPFILE_CLUSTER; i++)
+		map[i] = SWAP_HAS_CACHE;
+	return offset;
+}
+
 swp_entry_t get_swap_page(void)
 {
 	struct swap_info_struct *si, *next;
@@ -804,29 +906,7 @@ static unsigned char swap_entry_free(struct swap_info_struct *p,
 	if (!usage) {
 		mem_cgroup_uncharge_swap(entry, 1);
 		dec_cluster_info_page(p, p->cluster_info, offset);
-		if (offset < p->lowest_bit)
-			p->lowest_bit = offset;
-		if (offset > p->highest_bit) {
-			bool was_full = !p->highest_bit;
-			p->highest_bit = offset;
-			if (was_full && (p->flags & SWP_WRITEOK)) {
-				spin_lock(&swap_avail_lock);
-				WARN_ON(!plist_node_empty(&p->avail_list));
-				if (plist_node_empty(&p->avail_list))
-					plist_add(&p->avail_list,
-						  &swap_avail_head);
-				spin_unlock(&swap_avail_lock);
-			}
-		}
-		atomic_long_inc(&nr_swap_pages);
-		p->inuse_pages--;
-		frontswap_invalidate_page(p->type, offset);
-		if (p->flags & SWP_BLKDEV) {
-			struct gendisk *disk = p->bdev->bd_disk;
-			if (disk->fops->swap_slot_free_notify)
-				disk->fops->swap_slot_free_notify(p->bdev,
-								  offset);
-		}
+		__swap_entry_free(p, offset, false);
 	}
 
 	return usage;
Signature · 7 lines
2.8.1

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

RFC 9/11 mm, THP: Add can_split_huge_page()

In reply to Huang, Ying

From: Huang Ying <[email protected]>

Separates checking whether we can split the huge page from split_huge_page_to_list() into a function. This will help to check that before splitting the THP (Transparent Huge Page) really.

This will be used for delaying splitting THP during swapping out. Where for a THP, we will allocate a swap cluster, add the THP into swap cache, then split the THP. To avoid unnecessary operations for un-splittable THP, we will check that firstly.

There is no functionality change in this patch.

Cc Andrea Arcangeli <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Ebru Akagunduz <[email protected]>
---
 include/linux/huge_mm.h |  6 ++++++
 mm/huge_memory.c        | 13 ++++++++++++-
 2 files changed, 18 insertions(+), 1 deletion(-)
include/linux/huge_mm.h +6 -0
index 6f14de4..95ccbb4 100644
@@ -90,6 +90,7 @@ extern unsigned long transparent_hugepage_flags;
 extern void prep_transhuge_page(struct page *page);
 extern void free_transhuge_page(struct page *page);
 
+bool can_split_huge_page(struct page *page);
 int split_huge_page_to_list(struct page *page, struct list_head *list);
 static inline int split_huge_page(struct page *page)
 {
@@ -169,6 +170,11 @@ void put_huge_zero_page(void);
 static inline void prep_transhuge_page(struct page *page) {}
 
 #define transparent_hugepage_flags 0UL
+static inline bool
+can_split_huge_page(struct page *page)
+{
+	return false;
+}
 static inline int
 split_huge_page_to_list(struct page *page, struct list_head *list)
 {
mm/huge_memory.c +12 -1
index 2373f0a..af65413 100644
@@ -1954,6 +1954,17 @@ int page_trans_huge_mapcount(struct page *page, int *total_mapcount)
 	return ret;
 }
 
+/* Racy check whether the huge page can be split */
+bool can_split_huge_page(struct page *page)
+{
+	int extra_pins = 0;
+
+	/* Additional pins from radix tree */
+	if (!PageAnon(page))
+		extra_pins = HPAGE_PMD_NR;
+	return total_mapcount(page) == page_count(page) - extra_pins - 1;
+}
+
 /*
  * This function splits huge page into normal pages. @page can point to any
  * subpage of huge page to split. Split doesn't change the position of @page.
@@ -2024,7 +2035,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)
 	 * Racy check if we can split the page, before freeze_page() will
 	 * split PMDs
 	 */
-	if (total_mapcount(head) != page_count(head) - extra_pins - 1) {
+	if (!can_split_huge_page(head)) {
 		ret = -EBUSY;
 		goto out_unlock;
 	}
Signature · 1 line
2.8.1
#6 lore raw

RFC 6/11 mm, THP, swap: Add get_huge_swap_page()

In reply to Huang, Ying

From: Huang Ying <[email protected]>

A variation of get_swap_page(), get_huge_swap_page(), is added to allocate a swap cluster (512 swap slots) based on the swap cluster allocation function. A fair simple algorithm is used, that is, only the first swap device in priority list will be tried to allocate the swap cluster. The function will fail if that trying is not successful, and the caller will fall back to allocate single swap slot instead. This works good enough for normal cases.

This will be used for THP (Transparent Huge Page) swap support. Where get_huge_swap_page() will be used to allocate one swap cluster for each THP swapped out.

Cc Andrea Arcangeli <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Shaohua Li <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Rik van Riel <[email protected]>
---
 include/linux/swap.h | 21 ++++++++++++++++++++-
 mm/swapfile.c        | 29 +++++++++++++++++++++++------
 2 files changed, 43 insertions(+), 7 deletions(-)
include/linux/swap.h +20 -1
index 6988bce..95a526e 100644
@@ -399,7 +399,7 @@ static inline long get_nr_swap_pages(void)
 }
 
 extern void si_swapinfo(struct sysinfo *);
-extern swp_entry_t get_swap_page(void);
+extern swp_entry_t __get_swap_page(bool huge);
 extern swp_entry_t get_swap_page_of_type(int);
 extern int add_swap_count_continuation(swp_entry_t, gfp_t);
 extern void swap_shmem_alloc(swp_entry_t);
@@ -419,6 +419,20 @@ extern bool reuse_swap_page(struct page *, int *);
 extern int try_to_free_swap(struct page *);
 struct backing_dev_info;
 
+static inline swp_entry_t get_swap_page(void)
+{
+	return __get_swap_page(false);
+}
+
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+extern swp_entry_t get_huge_swap_page(void);
+#else
+static inline swp_entry_t get_huge_swap_page(void)
+{
+	return (swp_entry_t) {0};
+}
+#endif
+
 #else /* CONFIG_SWAP */
 
 #define swap_address_space(entry)		(NULL)
@@ -525,6 +539,11 @@ static inline swp_entry_t get_swap_page(void)
 	return entry;
 }
 
+static inline swp_entry_t get_huge_swap_page(void)
+{
+	return (swp_entry_t) {0};
+}
+
 #endif /* CONFIG_SWAP */
 
 #ifdef CONFIG_MEMCG
mm/swapfile.c +23 -6
index d710e0e..5cd78c7 100644
@@ -747,14 +747,15 @@ static unsigned long swap_alloc_huge_cluster(struct swap_info_struct *si)
 	return offset;
 }
 
-swp_entry_t get_swap_page(void)
+swp_entry_t __get_swap_page(bool huge)
 {
 	struct swap_info_struct *si, *next;
 	pgoff_t offset;
+	int nr_pages = huge_cluster_nr_entries(huge);
 
-	if (atomic_long_read(&nr_swap_pages) <= 0)
+	if (atomic_long_read(&nr_swap_pages) < nr_pages)
 		goto noswap;
-	atomic_long_dec(&nr_swap_pages);
+	atomic_long_sub(nr_pages, &nr_swap_pages);
 
 	spin_lock(&swap_avail_lock);
 
@@ -782,10 +783,15 @@ start_over:
 		}
 
 		/* This is called for allocating swap entry for cache */
-		offset = scan_swap_map(si, SWAP_HAS_CACHE);
+		if (likely(nr_pages == 1))
+			offset = scan_swap_map(si, SWAP_HAS_CACHE);
+		else
+			offset = swap_alloc_huge_cluster(si);
 		spin_unlock(&si->lock);
 		if (offset)
 			return swp_entry(si->type, offset);
+		else if (unlikely(nr_pages != 1))
+			goto fail_alloc;
 		pr_debug("scan_swap_map of si %d failed to find offset\n",
 		       si->type);
 		spin_lock(&swap_avail_lock);
@@ -805,12 +811,23 @@ nextsi:
 	}
 
 	spin_unlock(&swap_avail_lock);
-
-	atomic_long_inc(&nr_swap_pages);
+fail_alloc:
+	atomic_long_add(nr_pages, &nr_swap_pages);
 noswap:
 	return (swp_entry_t) {0};
 }
 
+
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+swp_entry_t get_huge_swap_page(void)
+{
+	if (SWAPFILE_CLUSTER != HPAGE_PMD_NR)
+		return (swp_entry_t) {0};
+
+	return __get_swap_page(true);
+}
+#endif
+
 /* The only caller of this function is now suspend routine */
 swp_entry_t get_swap_page_of_type(int type)
 {
Signature · 7 lines
2.8.1

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

RFC 10/11 mm, THP, swap: Support to split THP in swap cache

In reply to Huang, Ying

From: Huang Ying <[email protected]>

This patch enhanced the split_huge_page_to_list() to work properly for THP (Transparent Huge Page) in swap cache during swapping out.

This is used for delaying splitting THP during swapping out. Where for a THP to be swapped out, we will allocate a swap cluster, add the THP into the swap cache, then split the THP. The page lock will be held during this process. So in the code path other than swapping out, if the THP need to be split, the PageSwapCache(THP) will be always false.

Cc Andrea Arcangeli <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Ebru Akagunduz <[email protected]>
---
 mm/huge_memory.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
mm/huge_memory.c +12 -5
index af65413..f738a7e 100644
@@ -1772,7 +1772,7 @@ static void __split_huge_page_tail(struct page *head, int tail,
 	 * atomic_set() here would be safe on all archs (and not only on x86),
 	 * it's safer to use atomic_inc()/atomic_add().
 	 */
-	if (PageAnon(head)) {
+	if (PageAnon(head) && !PageSwapCache(head)) {
 		page_ref_inc(page_tail);
 	} else {
 		/* Additional pin to radix tree */
@@ -1783,6 +1783,7 @@ static void __split_huge_page_tail(struct page *head, int tail,
 	page_tail->flags |= (head->flags &
 			((1L << PG_referenced) |
 			 (1L << PG_swapbacked) |
+			 (1L << PG_swapcache) |
 			 (1L << PG_mlocked) |
 			 (1L << PG_uptodate) |
 			 (1L << PG_active) |
@@ -1845,7 +1846,11 @@ static void __split_huge_page(struct page *page, struct list_head *list,
 	ClearPageCompound(head);
 	/* See comment in __split_huge_page_tail() */
 	if (PageAnon(head)) {
-		page_ref_inc(head);
+		/* Additional pin to radix tree of swap cache */
+		if (PageSwapCache(head))
+			page_ref_add(head, 2);
+		else
+			page_ref_inc(head);
 	} else {
 		/* Additional pin to radix tree */
 		page_ref_add(head, 2);
@@ -1957,10 +1962,12 @@ int page_trans_huge_mapcount(struct page *page, int *total_mapcount)
 /* Racy check whether the huge page can be split */
 bool can_split_huge_page(struct page *page)
 {
-	int extra_pins = 0;
+	int extra_pins;
 
 	/* Additional pins from radix tree */
-	if (!PageAnon(page))
+	if (PageAnon(page))
+		extra_pins = PageSwapCache(page) ? HPAGE_PMD_NR : 0;
+	else
 		extra_pins = HPAGE_PMD_NR;
 	return total_mapcount(page) == page_count(page) - extra_pins - 1;
 }
@@ -2013,7 +2020,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)
 			ret = -EBUSY;
 			goto out;
 		}
-		extra_pins = 0;
+		extra_pins = PageSwapCache(head) ? HPAGE_PMD_NR : 0;
 		mapping = NULL;
 		anon_vma_lock_write(anon_vma);
 	} else {
Signature · 1 line
2.8.1
#7 lore raw

RFC 7/11 mm, THP, swap: Support to clear SWAP_HAS_CACHE for huge page

In reply to Huang, Ying

From: Huang Ying <[email protected]>

__swapcache_free() is added to support to clear SWAP_HAS_CACHE for huge page. This will free the specified swap cluster now. Because now this function will be called only in the error path to free the swap cluster just allocated. So the corresponding swap_map[i] == SWAP_HAS_CACHE, that is, the swap count is 0. This makes the implementation simpler than that of the ordinary swap entry.

This will be used for delaying splitting THP (Transparent Huge Page) during swapping out. Where for one THP to swap out, we will allocate a swap cluster, add the THP into swap cache, then split the THP. If anything fails after allocating the swap cluster and before splitting the THP successfully, the swapcache_free_trans_huge() will be used to free the swap space allocated.

Cc Andrea Arcangeli <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Shaohua Li <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Rik van Riel <[email protected]>
---
 include/linux/swap.h |  9 +++++++--
 mm/swapfile.c        | 27 +++++++++++++++++++++++++--
 2 files changed, 32 insertions(+), 4 deletions(-)
include/linux/swap.h +7 -2
index 95a526e..04d963f 100644
@@ -406,7 +406,7 @@ extern void swap_shmem_alloc(swp_entry_t);
 extern int swap_duplicate(swp_entry_t);
 extern int swapcache_prepare(swp_entry_t);
 extern void swap_free(swp_entry_t);
-extern void swapcache_free(swp_entry_t);
+extern void __swapcache_free(swp_entry_t, bool);
 extern int free_swap_and_cache(swp_entry_t);
 extern int swap_type_of(dev_t, sector_t, struct block_device **);
 extern unsigned int count_swap_pages(int, int);
@@ -475,7 +475,7 @@ static inline void swap_free(swp_entry_t swp)
 {
 }
 
-static inline void swapcache_free(swp_entry_t swp)
+static inline void __swapcache_free(swp_entry_t swp, bool huge)
 {
 }
 
@@ -546,6 +546,11 @@ static inline swp_entry_t get_huge_swap_page(void)
 
 #endif /* CONFIG_SWAP */
 
+static inline void swapcache_free(swp_entry_t entry)
+{
+	__swapcache_free(entry, false);
+}
+
 #ifdef CONFIG_MEMCG
 static inline int mem_cgroup_swappiness(struct mem_cgroup *memcg)
 {
mm/swapfile.c +25 -2
index 5cd78c7..be89a2f 100644
@@ -945,15 +945,38 @@ void swap_free(swp_entry_t entry)
 }
 
 /*
+ * Caller should hold si->lock.
+ */
+static void swapcache_free_trans_huge(struct swap_info_struct *si,
+				      swp_entry_t entry)
+{
+	unsigned long offset = swp_offset(entry);
+	unsigned long idx = offset / SWAPFILE_CLUSTER;
+	unsigned char *map;
+	unsigned int i;
+
+	map = si->swap_map + offset;
+	for (i = 0; i < SWAPFILE_CLUSTER; i++) {
+		VM_BUG_ON(map[i] != SWAP_HAS_CACHE);
+		map[i] &= ~SWAP_HAS_CACHE;
+	}
+	mem_cgroup_uncharge_swap(entry, SWAPFILE_CLUSTER);
+	swap_free_huge_cluster(si, idx);
+}
+
+/*
  * Called after dropping swapcache to decrease refcnt to swap entries.
  */
-void swapcache_free(swp_entry_t entry)
+void __swapcache_free(swp_entry_t entry, bool huge)
 {
 	struct swap_info_struct *p;
 
 	p = swap_info_get(entry);
 	if (p) {
-		swap_entry_free(p, entry, SWAP_HAS_CACHE);
+		if (unlikely(huge))
+			swapcache_free_trans_huge(p, entry);
+		else
+			swap_entry_free(p, entry, SWAP_HAS_CACHE);
 		spin_unlock(&p->lock);
 	}
 }
Signature · 7 lines
2.8.1

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

RFC 11/11 mm, THP, swap: Delay splitting THP during swap out

In reply to Huang, Ying

From: Huang Ying <[email protected]>

In this patch, the splitting huge page is delayed from almost the first step of swapping out to after allocating the swap space for THP and adding the THP into swap cache. This will reduce lock acquiring/releasing for locks used for swap space and swap cache management.

This is the also first step for THP (Transparent Huge Page) swap support. The plan is to delaying splitting THP step by step and avoid splitting THP finally.

The advantages of THP swap support are:

- Batch swap operations for THP to reduce lock acquiring/releasing, including allocating/freeing swap space, adding/deleting to/from swap cache, and writing/reading swap space, etc.

- THP swap space read/write will be 2M sequence IO. It is particularly helpful for swap read, which usually are 4k random IO.

- It will help memory fragmentation, especially when THP is heavily used by the applications. 2M continuous pages will be free up after THP swapping out.

With the patchset, the swap out bandwidth improved 12.1% in vm-scalability swap-w-seq test case with 16 processes on a Xeon E5 v3 system. To test sequence swap out, the test case uses 16 processes sequentially allocate and write to anonymous pages until RAM and part of the swap device is used up.

The detailed compare result is as follow,

base base+patchset
---------------- --------------------------
%stddev %change %stddev
\ | \

   1118821 A+-  0%     +12.1%    1254241 A+-  1%  vmstat.swap.so
   2460636 A+-  1%     +10.6%    2720983 A+-  1%  vm-scalability.throughput
    308.79 A+-  1%      -7.9%     284.53 A+-  1%  vm-scalability.time.elapsed_time
      1639 A+-  4%    +232.3%       5446 A+-  1%  meminfo.SwapCached
      0.70 A+-  3%      +8.7%       0.77 A+-  5%  perf-stat.ipc
      9.82 A+-  8%     -31.6%       6.72 A+-  2%  perf-profile.cycles-pp._raw_spin_lock_irq.__add_to_swap_cache.add_to_swap_cache.add_to_swap.shrink_page_list
Cc Hugh Dickins <[email protected]>
Cc Shaohua Li <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
---
 mm/swap_state.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 50 insertions(+), 3 deletions(-)
mm/swap_state.c +50 -3
index a41fd10..5316fbc 100644
@@ -17,6 +17,7 @@
 #include <linux/blkdev.h>
 #include <linux/pagevec.h>
 #include <linux/migrate.h>
+#include <linux/huge_mm.h>
 
 #include <asm/pgtable.h>
 
@@ -172,12 +173,45 @@ void __delete_from_swap_cache(struct page *page)
 	ADD_CACHE_INFO(del_total, nr);
 }
 
+int add_to_swap_trans_huge(struct page *page, struct list_head *list)
+{
+	swp_entry_t entry;
+	int ret = 0;
+
+	/* cannot split, which may be needed during swap in, skip it */
+	if (!can_split_huge_page(page))
+		return -EBUSY;
+	/* fallback to split huge page firstly if no PMD map */
+	if (!compound_mapcount(page))
+		return 0;
+	entry = get_huge_swap_page();
+	if (!entry.val)
+		return 0;
+	if (mem_cgroup_try_charge_swap(page, entry, HPAGE_PMD_NR)) {
+		__swapcache_free(entry, true);
+		return -EOVERFLOW;
+	}
+	ret = add_to_swap_cache(page, entry,
+				__GFP_HIGH | __GFP_NOMEMALLOC|__GFP_NOWARN);
+	/* -ENOMEM radix-tree allocation failure */
+	if (ret) {
+		__swapcache_free(entry, true);
+		return 0;
+	}
+	ret = split_huge_page_to_list(page, list);
+	if (ret) {
+		delete_from_swap_cache(page);
+		return -EBUSY;
+	}
+	return 1;
+}
+
 /**
  * add_to_swap - allocate swap space for a page
  * @page: page we want to move to swap
  *
  * Allocate swap space for the page and add the page to the
- * swap cache.  Caller needs to hold the page lock. 
+ * swap cache.  Caller needs to hold the page lock.
  */
 int add_to_swap(struct page *page, struct list_head *list)
 {
@@ -187,6 +221,14 @@ int add_to_swap(struct page *page, struct list_head *list)
 	VM_BUG_ON_PAGE(!PageLocked(page), page);
 	VM_BUG_ON_PAGE(!PageUptodate(page), page);
 
+	if (unlikely(PageTransHuge(page))) {
+		err = add_to_swap_trans_huge(page, list);
+		if (err < 0)
+			return 0;
+		else if (err > 0)
+			return err;
+		/* fallback to split firstly if return 0 */
+	}
 	entry = get_swap_page();
 	if (!entry.val)
 		return 0;
@@ -306,7 +348,7 @@ struct page * lookup_swap_cache(swp_entry_t entry)
 
 	page = find_get_page(swap_address_space(entry), entry.val);
 
-	if (page) {
+	if (page && likely(!PageCompound(page))) {
 		INC_CACHE_INFO(find_success);
 		if (TestClearPageReadahead(page))
 			atomic_inc(&swapin_readahead_hits);
@@ -332,8 +374,13 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
 		 * that would confuse statistics.
 		 */
 		found_page = find_get_page(swapper_space, entry.val);
-		if (found_page)
+		if (found_page) {
+			if (unlikely(PageCompound(found_page))) {
+				put_page(found_page);
+				found_page = NULL;
+			}
 			break;
+		}
 
 		/*
 		 * Get a new page to read into from swap.
Signature · 7 lines
2.8.1

--
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 Huang, Ying

Hi, All,

Quoted 21 lines · Ying

From: Huang Ying <[email protected]>

This patchset is based on 8/4 head of mmotm/master.

This is the first step for Transparent Huge Page (THP) swap support. The plan is to delaying splitting THP step by step and avoid splitting THP finally during THP swapping out and swapping in.

The advantages of THP swap support are:

- Batch swap operations for THP to reduce lock acquiring/releasing, including allocating/freeing swap space, adding/deleting to/from swap cache, and writing/reading swap space, etc.

- THP swap space read/write will be 2M sequence IO. It is particularly helpful for swap read, which usually are 4k random IO.

- It will help memory fragmentation, especially when THP is heavily used by the applications. 2M continuous pages will be free up after THP swapping out.

Quoted 5 lines

As the first step, in this patchset, the splitting huge page is delayed from almost the first step of swapping out to after allocating the swap space for THP and adding the THP into swap cache. This will reduce lock acquiring/releasing for locks used for swap space and swap cache management.

For this patchset posting,

In general, I want to check the basic design with memory management subsystem maintainers and developers.

For [RFC 01/11] swap: Add swap_cluster_list, it is a cleanup patch. And I think it should be useful independently.

I am not very confident about the memcg part, that is

[RFC 03/11] mm, memcg: Add swap_cgroup_iter iterator
[RFC 04/11] mm, memcg: Support to charge/uncharge multiple swap entries

Please help me to check it.

Best Regards,
Huang, Ying

--
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 Huang, Ying

Hello Huang,

Quoted 7 lines · Ying

From: Huang Ying <[email protected]>

This patchset is based on 8/4 head of mmotm/master.

This is the first step for Transparent Huge Page (THP) swap support. The plan is to delaying splitting THP step by step and avoid splitting THP finally during THP swapping out and swapping in.

What does it mean "delay splitting THP on swapping-in"?

Quoted 7 lines

The advantages of THP swap support are:

- Batch swap operations for THP to reduce lock acquiring/releasing, including allocating/freeing swap space, adding/deleting to/from swap cache, and writing/reading swap space, etc.

Quoted 6 lines

- THP swap space read/write will be 2M sequence IO. It is particularly helpful for swap read, which usually are 4k random IO.

- It will help memory fragmentation, especially when THP is heavily used by the applications. 2M continuous pages will be free up after THP swapping out.

Could we take the benefit for normal pages as well as THP page? I think Tim and me discussed about that a few weeks ago.

Please search below topics.

[1] mm: Batch page reclamation under shink_page_list
[2] mm: Cleanup - Reorganize the shrink_page_list code into smaller functions

It's different with yours which focused on THP swapping while the suggestion would be more general if we can do so it's worth to try it, I think.

Anyway, I hope [1/11] should be merged regardless of the patchset because I believe anyone doesn't feel comfortable with cluser_info functions. ;-)

Thanks.

Quoted 26 lines

As the first step, in this patchset, the splitting huge page is delayed from almost the first step of swapping out to after allocating the swap space for THP and adding the THP into swap cache. This will reduce lock acquiring/releasing for locks used for swap space and swap cache management.

With the patchset, the swap out bandwidth improved 12.1% in vm-scalability swap-w-seq test case with 16 processes on a Xeon E5 v3 system. To test sequence swap out, the test case uses 16 processes sequentially allocate and write to anonymous pages until RAM and part of the swap device is used up.

The detailed compare result is as follow,

base base+patchset
---------------- --------------------------
%stddev %change %stddev
\ | \

   1118821 +-  0%     +12.1%    1254241 +-  1%  vmstat.swap.so
   2460636 +-  1%     +10.6%    2720983 +-  1%  vm-scalability.throughput
    308.79 +-  1%      -7.9%     284.53 +-  1%  vm-scalability.time.elapsed_time
      1639 +-  4%    +232.3%       5446 +-  1%  meminfo.SwapCached
      0.70 +-  3%      +8.7%       0.77 +-  5%  perf-stat.ipc
      9.82 +-  8%     -31.6%       6.72 +-  2%  perf-profile.cycles-pp._raw_spin_lock_irq.__add_to_swap_cache.add_to_swap_cache.add_to_swap.shrink_page_list
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>

#14 lore raw

Hi, Kim,

Quoted 5 lines · Minchan Kim

Hello Huang,

Quoted 2 lines · Ying

From: Huang Ying <[email protected]>

Quoted 7 lines
Quoted 5 lines

This patchset is based on 8/4 head of mmotm/master.

This is the first step for Transparent Huge Page (THP) swap support. The plan is to delaying splitting THP step by step and avoid splitting THP finally during THP swapping out and swapping in.

What does it mean "delay splitting THP on swapping-in"?

Sorry for my poor English. We will only delay splitting the THP during swapping out. The final target is to avoid splitting the THP during swapping out, and swap out/in the THP directly. Thanks for pointing out that. I will revise the patch description in the next version.

Quoted 7 lines
Quoted 7 lines

The advantages of THP swap support are:

- Batch swap operations for THP to reduce lock acquiring/releasing, including allocating/freeing swap space, adding/deleting to/from swap cache, and writing/reading swap space, etc.

Quoted 8 lines
Quoted 6 lines

- THP swap space read/write will be 2M sequence IO. It is particularly helpful for swap read, which usually are 4k random IO.

- It will help memory fragmentation, especially when THP is heavily used by the applications. 2M continuous pages will be free up after THP swapping out.

Could we take the benefit for normal pages as well as THP page?

This patchset benefits the THP swap only. It has no effect for normal pages.

Quoted 1 line

I think Tim and me discussed about that a few weeks ago.

I work closely with Tim on swap optimization. This patchset is the part of our swap optimization plan.

Quoted 7 lines

Please search below topics.

[1] mm: Batch page reclamation under shink_page_list
[2] mm: Cleanup - Reorganize the shrink_page_list code into smaller functions

It's different with yours which focused on THP swapping while the suggestion would be more general if we can do so it's worth to try it, I think.

I think the general optimization above will benefit both normal pages and THP at least for now. And I think there are no hard conflict between those two patchsets.

The THP swap has more opportunity to be optimized, because we can batch 512 operations together more easily. For full THP swap support, unmap a THP could be more efficient with only one swap count operation instead of 512, so do many other operations, such as add/remove from swap cache with multi-order radix tree etc. And it will help memory fragmentation. THP can be kept after swapping out/in, need not to rebuild THP via khugepaged.

But not all pages are huge, so normal pages swap optimization is necessary and good anyway.

Quoted 2 lines

Anyway, I hope [1/11] should be merged regardless of the patchset because I believe anyone doesn't feel comfortable with cluser_info functions. ;-)

Thanks,

Best Regards,
Huang, Ying

[snip]

--
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 15 lines · Ying

Hi, Kim,

Quoted 11 lines · Minchan Kim

Hello Huang,

Quoted 7 lines · Ying

From: Huang Ying <[email protected]>

This patchset is based on 8/4 head of mmotm/master.

This is the first step for Transparent Huge Page (THP) swap support. The plan is to delaying splitting THP step by step and avoid splitting THP finally during THP swapping out and swapping in.

Quoted 6 lines
Quoted 1 line

What does it mean "delay splitting THP on swapping-in"?

Sorry for my poor English. We will only delay splitting the THP during swapping out. The final target is to avoid splitting the THP during swapping out, and swap out/in the THP directly. Thanks for pointing out that. I will revise the patch description in the next version.

Thanks.

Quoted 29 lines
Quoted 8 lines
Quoted 8 lines

The advantages of THP swap support are:

- Batch swap operations for THP to reduce lock acquiring/releasing, including allocating/freeing swap space, adding/deleting to/from swap cache, and writing/reading swap space, etc.

Quoted 8 lines
Quoted 6 lines

- THP swap space read/write will be 2M sequence IO. It is particularly helpful for swap read, which usually are 4k random IO.

- It will help memory fragmentation, especially when THP is heavily used by the applications. 2M continuous pages will be free up after THP swapping out.

Could we take the benefit for normal pages as well as THP page?

This patchset benefits the THP swap only. It has no effect for normal pages.

Quoted 1 line

I think Tim and me discussed about that a few weeks ago.

I work closely with Tim on swap optimization. This patchset is the part of our swap optimization plan.

Quoted 5 lines

Please search below topics.

[1] mm: Batch page reclamation under shink_page_list
[2] mm: Cleanup - Reorganize the shrink_page_list code into smaller functions

Quoted 6 lines
Quoted 2 lines

It's different with yours which focused on THP swapping while the suggestion would be more general if we can do so it's worth to try it, I think.

I think the general optimization above will benefit both normal pages and THP at least for now. And I think there are no hard conflict between those two patchsets.

If we could do general optimzation, I guess THP swap without splitting would be more straight forward.

If we can reclaim batch a certain of pages all at once, it helps we can do scan_swap_map(si, SWAP_HAS_CACHE, nr_pages). The nr_pages could be greater or less than 512 pages. With that, scan_swap_map effectively search empty swap slots from scan_map or free cluser list. Then, needed part from your patchset is to just delay splitting of THP.

Quoted 8 lines

The THP swap has more opportunity to be optimized, because we can batch 512 operations together more easily. For full THP swap support, unmap a THP could be more efficient with only one swap count operation instead of 512, so do many other operations, such as add/remove from swap cache with multi-order radix tree etc. And it will help memory fragmentation. THP can be kept after swapping out/in, need not to rebuild THP via khugepaged.

It seems you increased cluster size to 512 and search a empty cluster for a THP swap. With that approach, I have a concern that once clusters will be fragmented, THP swap support doesn't take benefit at all.

Why do we need a empty cluster for swapping out 512 pages? IOW, below case could work for the goal.

A : Allocated slot
F : Free slot

cluster A cluster B
AAAAFFFF - FFFFAAAA

That's one of the reason I suggested batch reclaim work first and support THP swap based on it. With that, scan_swap_map can be aware of nr_pages and selects right clusters.

With the approach, justfication of THP swap support would be easier, too. IOW, I'm not sure how only THP swap support is valuable in real workload.

Anyways, that's just my two cents.

Quoted 7 lines

But not all pages are huge, so normal pages swap optimization is necessary and good anyway.

Quoted 2 lines

Anyway, I hope [1/11] should be merged regardless of the patchset because I believe anyone doesn't feel comfortable with cluser_info functions. ;-)

Quoted 6 lines

Thanks,

Best Regards,
Huang, Ying

[snip]

--
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 50 lines · Minchan Kim
Quoted 10 lines · Ying

A

Quoted 2 lines

I think Tim and me discussed about that a few weeks ago.

I work closely with Tim on swap optimization.A A This patchset is the part of our swap optimization plan.

Quoted 3 lines

Please search below topics.

Quoted 8 lines
Quoted 5 lines

[1] mm: Batch page reclamation under shink_page_list
[2] mm: Cleanup - Reorganize the shrink_page_list code into smaller functions

It's different with yours which focused on THP swapping while the suggestion would be more general if we can do so it's worth to try it, I think.

I think the general optimization above will benefit both normal pages and THP at least for now.A A And I think there are no hard conflict between those two patchsets.

If we could do general optimzation, I guess THP swap without splitting would be more straight forward.

If we can reclaim batch a certain of pages all at once, it helps we can do scan_swap_map(si, SWAP_HAS_CACHE, nr_pages). The nr_pages could be greater or less than 512 pages. With that, scan_swap_map effectively search empty swap slots from scan_map or free cluser list. Then, needed part from your patchset is to just delay splitting of THP.

Quoted 9 lines

The THP swap has more opportunity to be optimized, because we can batch 512 operations together more easily.A A For full THP swap support, unmap a THP could be more efficient with only one swap count operation instead of 512, so do many other operations, such as add/remove from swap cache with multi-order radix tree etc.A A And it will help memory fragmentation. THP can be kept after swapping out/in, need not to rebuild THP via khugepaged.

It seems you increased cluster size to 512 and search a empty cluster for a THP swap. With that approach, I have a concern that once clusters will be fragmented, THP swap support doesn't take benefit at all.

Why do we need a empty cluster for swapping out 512 pages? IOW, below case could work for the goal.

A : Allocated slot
F : Free slot

cluster AA A A cluster B
AAAAFFFFA A -A A FFFFAAAA

Quoted 8 lines

That's one of the reason I suggested batch reclaim work first and support THP swap based on it. With that, scan_swap_map can be aware of nr_pages and selects right clusters.

With the approach, justfication of THP swap support would be easier, too. IOW, I'm not sure how only THP swap support is valuable in real workload.

Anyways, that's just my two cents.

Minchan,

Scanning for contiguous slots that span clusters may take quite a
long time under fragmentation, and may eventually fail. A In that case the addition scan time overhead may go to waste and defeat the purpose of fast swapping of large page.

The empty cluster lookup on the other hand is very fast.
We treat the empty cluster available case as an opportunity for fast path swap out of large page. A Otherwise, we'll revert to the current
slow path behavior of breaking into normal pages so there's no
regression, and we may get speed up. A We can be considerably faster when a lot of large pages are used. A

Quoted 6 lines
Quoted 6 lines

But not all pages are huge, so normal pages swap optimization is necessary and good anyway.

Yes, optimizing the normal swap pages is still an important goal for us. A THP swap optimization is complementary component. A

We have seen system with THP spend significant cpu cycles breaking up the pages on swap out and then compacting the pages for THP again after swap in. A So if we can avoid this, that will be helpful.

Thanks for your valuable comments.

Tim

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

Quoted 66 lines · Tim Chen
Quoted 50 lines · Minchan Kim
Quoted 10 lines · Ying
Quoted 4 lines

I think Tim and me discussed about that a few weeks ago.

I work closely with Tim on swap optimization. This patchset is the part of our swap optimization plan.

Quoted 3 lines

Please search below topics.

Quoted 8 lines
Quoted 5 lines

[1] mm: Batch page reclamation under shink_page_list
[2] mm: Cleanup - Reorganize the shrink_page_list code into smaller functions

It's different with yours which focused on THP swapping while the suggestion would be more general if we can do so it's worth to try it, I think.

I think the general optimization above will benefit both normal pages and THP at least for now. And I think there are no hard conflict between those two patchsets.

If we could do general optimzation, I guess THP swap without splitting would be more straight forward.

If we can reclaim batch a certain of pages all at once, it helps we can do scan_swap_map(si, SWAP_HAS_CACHE, nr_pages). The nr_pages could be greater or less than 512 pages. With that, scan_swap_map effectively search empty swap slots from scan_map or free cluser list. Then, needed part from your patchset is to just delay splitting of THP.

Quoted 9 lines

The THP swap has more opportunity to be optimized, because we can batch 512 operations together more easily. For full THP swap support, unmap a THP could be more efficient with only one swap count operation instead of 512, so do many other operations, such as add/remove from swap cache with multi-order radix tree etc. And it will help memory fragmentation. THP can be kept after swapping out/in, need not to rebuild THP via khugepaged.

It seems you increased cluster size to 512 and search a empty cluster for a THP swap. With that approach, I have a concern that once clusters will be fragmented, THP swap support doesn't take benefit at all.

Why do we need a empty cluster for swapping out 512 pages? IOW, below case could work for the goal.

A : Allocated slot
F : Free slot

cluster A cluster B
AAAAFFFF - FFFFAAAA

Quoted 8 lines

That's one of the reason I suggested batch reclaim work first and support THP swap based on it. With that, scan_swap_map can be aware of nr_pages and selects right clusters.

With the approach, justfication of THP swap support would be easier, too. IOW, I'm not sure how only THP swap support is valuable in real workload.

Anyways, that's just my two cents.

Minchan,

Scanning for contiguous slots that span clusters may take quite a
long time under fragmentation, and may eventually fail. In that case the addition scan time overhead may go to waste and defeat the purpose of fast swapping of large page.

Quoted 6 lines

The empty cluster lookup on the other hand is very fast.
We treat the empty cluster available case as an opportunity for fast path swap out of large page. Otherwise, we'll revert to the current
slow path behavior of breaking into normal pages so there's no regression, and we may get speed up. We can be considerably faster when a lot of large pages are used.

I didn't mean we should search scan_swap_map firstly without peeking free cluster but what I wanted was we might abstract it into scan_swap_map.

For example, if nr_pages is greather than the size of cluster, we can get empty cluster first and nr_pages - sizeof(cluster) for other free cluster or scanning of current CPU per-cpu cluster. If we cannot find used slot during scanning, we can bail out simply. Then, although we fail to get all* contiguous slots, we get a certain of contiguous slots so it would be benefit for seq write and lock batching point of view at the cost of a little scanning. And it's not specific to THP algorighm.

My point is that once we optimize normal page batch for swap, THP swap support would be more straight forward. But I should admit I didn't look into code in detail so it might have clear hurdle to implement it so I will rely on you guys's decision whether which one is more urgent/ benefit/making good code quality for the goal.

Thanks.

Quoted 9 lines
Quoted 8 lines
Quoted 8 lines

But not all pages are huge, so normal pages swap optimization is necessary and good anyway.

Quoted 8 lines

Yes, optimizing the normal swap pages is still an important goal for us. THP swap optimization is complementary component.

We have seen system with THP spend significant cpu cycles breaking up the pages on swap out and then compacting the pages for THP again after swap in. So if we can avoid this, that will be helpful.

Thanks for your valuable comments.

Thanks for good works.

--
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 80 lines · Minchan Kim

Hi Tim,

Quoted 66 lines · Tim Chen
Quoted 50 lines · Minchan Kim
Quoted 10 lines · Ying
Quoted 4 lines

I think Tim and me discussed about that a few weeks ago.

I work closely with Tim on swap optimization. This patchset is the part of our swap optimization plan.

Quoted 3 lines

Please search below topics.

Quoted 8 lines
Quoted 5 lines

[1] mm: Batch page reclamation under shink_page_list
[2] mm: Cleanup - Reorganize the shrink_page_list code into smaller functions

It's different with yours which focused on THP swapping while the suggestion would be more general if we can do so it's worth to try it, I think.

I think the general optimization above will benefit both normal pages and THP at least for now. And I think there are no hard conflict between those two patchsets.

If we could do general optimzation, I guess THP swap without splitting would be more straight forward.

If we can reclaim batch a certain of pages all at once, it helps we can do scan_swap_map(si, SWAP_HAS_CACHE, nr_pages). The nr_pages could be greater or less than 512 pages. With that, scan_swap_map effectively search empty swap slots from scan_map or free cluser list. Then, needed part from your patchset is to just delay splitting of THP.

Quoted 9 lines

The THP swap has more opportunity to be optimized, because we can batch 512 operations together more easily. For full THP swap support, unmap a THP could be more efficient with only one swap count operation instead of 512, so do many other operations, such as add/remove from swap cache with multi-order radix tree etc. And it will help memory fragmentation. THP can be kept after swapping out/in, need not to rebuild THP via khugepaged.

It seems you increased cluster size to 512 and search a empty cluster for a THP swap. With that approach, I have a concern that once clusters will be fragmented, THP swap support doesn't take benefit at all.

Why do we need a empty cluster for swapping out 512 pages? IOW, below case could work for the goal.

A : Allocated slot
F : Free slot

cluster A cluster B
AAAAFFFF - FFFFAAAA

Quoted 8 lines

That's one of the reason I suggested batch reclaim work first and support THP swap based on it. With that, scan_swap_map can be aware of nr_pages and selects right clusters.

With the approach, justfication of THP swap support would be easier, too. IOW, I'm not sure how only THP swap support is valuable in real workload.

Anyways, that's just my two cents.

Minchan,

Scanning for contiguous slots that span clusters may take quite a
long time under fragmentation, and may eventually fail. In that case the addition scan time overhead may go to waste and defeat the purpose of fast swapping of large page.

Quoted 6 lines

The empty cluster lookup on the other hand is very fast.
We treat the empty cluster available case as an opportunity for fast path swap out of large page. Otherwise, we'll revert to the current
slow path behavior of breaking into normal pages so there's no regression, and we may get speed up. We can be considerably faster when a lot of large pages are used.

I didn't mean we should search scan_swap_map firstly without peeking free cluster but what I wanted was we might abstract it into scan_swap_map.

Quoted 7 lines

For example, if nr_pages is greather than the size of cluster, we can get empty cluster first and nr_pages - sizeof(cluster) for other free cluster or scanning of current CPU per-cpu cluster. If we cannot find used slot during scanning, we can bail out simply. Then, although we fail to get all* contiguous slots, we get a certain of contiguous slots so it would be benefit for seq write and lock batching point of view at the cost of a little scanning. And it's not specific to THP algorighm.

Firstly, if my understanding were correct, to batch the normal pages swapping out, the swap slots need not to be continuous. But for the THP swap support, we need the continuous swap slots. So I think the requirements are quite different between them.

And with the current design of the swap space management, it is quite hard to implement allocating nr_pages continuous free swap slots. To reduce the contention of sis->lock, even to scan one free swap slot, the sis->lock is unlocked during scanning. When we scan nr_pages free swap slots, and there are no nr_pages continuous free swap slots, we need to scan from sis->lowest_bit to sis->highest_bit, and record the largest continuous free swap slots. But when we lock sis->lock again to check, some swap slot inside the largest continuous free swap slots we found may be allocated by other processes. So we may end up with a much smaller number of swap slots or we need to startover again. So I think the simpler solution is to

- When a whole cluster is requested (for the THP), try to allocate a free cluster. Give up if there are no free clusters.

- When a small number of swap slots are requested (for normal swap batching), check only sis->percpu_cluster and return next N free swap slots in it. Because we only scan very small number of swap slots, we can do that with sis->lock held.

BTW: The sis->lock is under heavy contention after the lock contention of swap cache radix tree lock is reduced via batching in 8 processes sequential swapping out test.

Best Regards,
Huang, Ying

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

Hi Huang,

Quoted 90 lines · Ying
Quoted 80 lines · Minchan Kim

Hi Tim,

Quoted 66 lines · Tim Chen
Quoted 50 lines · Minchan Kim
Quoted 10 lines · Ying
Quoted 4 lines

I think Tim and me discussed about that a few weeks ago.

I work closely with Tim on swap optimization.?This patchset is the part of our swap optimization plan.

Quoted 3 lines

Please search below topics.

Quoted 8 lines
Quoted 5 lines

[1] mm: Batch page reclamation under shink_page_list
[2] mm: Cleanup - Reorganize the shrink_page_list code into smaller functions

It's different with yours which focused on THP swapping while the suggestion would be more general if we can do so it's worth to try it, I think.

I think the general optimization above will benefit both normal pages and THP at least for now.?And I think there are no hard conflict between those two patchsets.

If we could do general optimzation, I guess THP swap without splitting would be more straight forward.

If we can reclaim batch a certain of pages all at once, it helps we can do scan_swap_map(si, SWAP_HAS_CACHE, nr_pages). The nr_pages could be greater or less than 512 pages. With that, scan_swap_map effectively search empty swap slots from scan_map or free cluser list. Then, needed part from your patchset is to just delay splitting of THP.

Quoted 9 lines

The THP swap has more opportunity to be optimized, because we can batch 512 operations together more easily.?For full THP swap support, unmap a THP could be more efficient with only one swap count operation instead of 512, so do many other operations, such as add/remove from swap cache with multi-order radix tree etc.?And it will help memory fragmentation. THP can be kept after swapping out/in, need not to rebuild THP via khugepaged.

It seems you increased cluster size to 512 and search a empty cluster for a THP swap. With that approach, I have a concern that once clusters will be fragmented, THP swap support doesn't take benefit at all.

Why do we need a empty cluster for swapping out 512 pages? IOW, below case could work for the goal.

A : Allocated slot
F : Free slot

cluster A?cluster B
AAAAFFFF?-?FFFFAAAA

Quoted 8 lines

That's one of the reason I suggested batch reclaim work first and support THP swap based on it. With that, scan_swap_map can be aware of nr_pages and selects right clusters.

With the approach, justfication of THP swap support would be easier, too. IOW, I'm not sure how only THP swap support is valuable in real workload.

Anyways, that's just my two cents.

Minchan,

Scanning for contiguous slots that span clusters may take quite a
long time under fragmentation, and may eventually fail. In that case the addition scan time overhead may go to waste and defeat the purpose of fast swapping of large page.

Quoted 6 lines

The empty cluster lookup on the other hand is very fast.
We treat the empty cluster available case as an opportunity for fast path swap out of large page. Otherwise, we'll revert to the current
slow path behavior of breaking into normal pages so there's no regression, and we may get speed up. We can be considerably faster when a lot of large pages are used.

I didn't mean we should search scan_swap_map firstly without peeking free cluster but what I wanted was we might abstract it into scan_swap_map.

Quoted 7 lines

For example, if nr_pages is greather than the size of cluster, we can get empty cluster first and nr_pages - sizeof(cluster) for other free cluster or scanning of current CPU per-cpu cluster. If we cannot find used slot during scanning, we can bail out simply. Then, although we fail to get all* contiguous slots, we get a certain of contiguous slots so it would be benefit for seq write and lock batching point of view at the cost of a little scanning. And it's not specific to THP algorighm.

Quoted 4 lines

Firstly, if my understanding were correct, to batch the normal pages swapping out, the swap slots need not to be continuous. But for the THP swap support, we need the continuous swap slots. So I think the requirements are quite different between them.

Hmm, I don't understand.

Let's think about swap slot management layer point of view. It doesn't need to take care of that a amount of batch request is caused by a thp page or multiple normal pages.

A matter is just that VM now asks multiple swap slots for seveal LRU-order pages so swap slot management tries to allocate several slots in a lock. Sure, it would be great if slots are consecutive fully because it means it's fast big sequential write as well as readahead together ideally. However, it would be better even if we didn't get consecutive slots because we get muliple slots all at once by batch.

It's not a THP specific requirement, I think.
Currenlty, SWAP_CLUSTER_MAX might be too small to get a benefit by normal page batch but it could be changed later once we implement batching logic nicely.

Quoted 13 lines

And with the current design of the swap space management, it is quite hard to implement allocating nr_pages continuous free swap slots. To reduce the contention of sis->lock, even to scan one free swap slot, the sis->lock is unlocked during scanning. When we scan nr_pages free swap slots, and there are no nr_pages continuous free swap slots, we need to scan from sis->lowest_bit to sis->highest_bit, and record the largest continuous free swap slots. But when we lock sis->lock again to check, some swap slot inside the largest continuous free swap slots we found may be allocated by other processes. So we may end up with a much smaller number of swap slots or we need to startover again. So I think the simpler solution is to

Quoted 2 lines

- When a whole cluster is requested (for the THP), try to allocate a free cluster. Give up if there are no free clusters.

One thing I'm afraid that it would consume free clusters very fast if adjacent pages around a faulted one doesn't have same hottness/ lifetime. Once it happens, we can't get benefit any more. IMO, it's too conservative and might be worse for the fragment point of view.

We can do further through scanning of per_cpu and/or global swap_map. If it makes lock contention problem, we can release the lock peridically (e.g., every SWAP_CLUSTER_MAX). I don't mean we must search 512 consecutive slots in swap_map but just bail out once we meet a hole during scanning.

Quoted 5 lines

- When a small number of swap slots are requested (for normal swap batching), check only sis->percpu_cluster and return next N free swap slots in it. Because we only scan very small number of swap slots, we can do that with sis->lock held.

Agreed.

If the batch request size is greater than cluster size, we can use a free cluster for (batch size - free cluster slots). If the batch request size is less than cluster, we can use pcp cluster.
If we fails both, we can try to consecutive slots via scanning of swap_map and let's bail out easily if we encounter the hole. About the lock contention, we might need release periodically.

Thanks.

Quoted 7 lines

BTW: The sis->lock is under heavy contention after the lock contention of swap cache radix tree lock is reduced via batching in 8 processes sequential swapping out test.

Best Regards,
Huang, Ying

--
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
Quoted 111 lines · Minchan Kim

Hi Huang,

Quoted 90 lines · Ying
Quoted 80 lines · Minchan Kim

Hi Tim,

Quoted 66 lines · Tim Chen
Quoted 50 lines · Minchan Kim
Quoted 10 lines · Ying
Quoted 4 lines

I think Tim and me discussed about that a few weeks ago.

I work closely with Tim on swap optimization.?This patchset is the part of our swap optimization plan.

Quoted 3 lines

Please search below topics.

Quoted 8 lines
Quoted 5 lines

[1] mm: Batch page reclamation under shink_page_list
[2] mm: Cleanup - Reorganize the shrink_page_list code into smaller functions

It's different with yours which focused on THP swapping while the suggestion would be more general if we can do so it's worth to try it, I think.

I think the general optimization above will benefit both normal pages and THP at least for now.?And I think there are no hard conflict between those two patchsets.

If we could do general optimzation, I guess THP swap without splitting would be more straight forward.

If we can reclaim batch a certain of pages all at once, it helps we can do scan_swap_map(si, SWAP_HAS_CACHE, nr_pages). The nr_pages could be greater or less than 512 pages. With that, scan_swap_map effectively search empty swap slots from scan_map or free cluser list. Then, needed part from your patchset is to just delay splitting of THP.

Quoted 9 lines

The THP swap has more opportunity to be optimized, because we can batch 512 operations together more easily.?For full THP swap support, unmap a THP could be more efficient with only one swap count operation instead of 512, so do many other operations, such as add/remove from swap cache with multi-order radix tree etc.?And it will help memory fragmentation. THP can be kept after swapping out/in, need not to rebuild THP via khugepaged.

It seems you increased cluster size to 512 and search a empty cluster for a THP swap. With that approach, I have a concern that once clusters will be fragmented, THP swap support doesn't take benefit at all.

Why do we need a empty cluster for swapping out 512 pages? IOW, below case could work for the goal.

A : Allocated slot
F : Free slot

cluster A?cluster B
AAAAFFFF?-?FFFFAAAA

Quoted 8 lines

That's one of the reason I suggested batch reclaim work first and support THP swap based on it. With that, scan_swap_map can be aware of nr_pages and selects right clusters.

With the approach, justfication of THP swap support would be easier, too. IOW, I'm not sure how only THP swap support is valuable in real workload.

Anyways, that's just my two cents.

Minchan,

Scanning for contiguous slots that span clusters may take quite a
long time under fragmentation, and may eventually fail. In that case the addition scan time overhead may go to waste and defeat the purpose of fast swapping of large page.

Quoted 6 lines

The empty cluster lookup on the other hand is very fast.
We treat the empty cluster available case as an opportunity for fast path swap out of large page. Otherwise, we'll revert to the current
slow path behavior of breaking into normal pages so there's no regression, and we may get speed up. We can be considerably faster when a lot of large pages are used.

I didn't mean we should search scan_swap_map firstly without peeking free cluster but what I wanted was we might abstract it into scan_swap_map.

Quoted 7 lines

For example, if nr_pages is greather than the size of cluster, we can get empty cluster first and nr_pages - sizeof(cluster) for other free cluster or scanning of current CPU per-cpu cluster. If we cannot find used slot during scanning, we can bail out simply. Then, although we fail to get all* contiguous slots, we get a certain of contiguous slots so it would be benefit for seq write and lock batching point of view at the cost of a little scanning. And it's not specific to THP algorighm.

Quoted 4 lines

Firstly, if my understanding were correct, to batch the normal pages swapping out, the swap slots need not to be continuous. But for the THP swap support, we need the continuous swap slots. So I think the requirements are quite different between them.

Hmm, I don't understand.

Let's think about swap slot management layer point of view. It doesn't need to take care of that a amount of batch request is caused by a thp page or multiple normal pages.

A matter is just that VM now asks multiple swap slots for seveal LRU-order pages so swap slot management tries to allocate several slots in a lock. Sure, it would be great if slots are consecutive fully because it means it's fast big sequential write as well as readahead together ideally. However, it would be better even if we didn't get consecutive slots because we get muliple slots all at once by batch.

Quoted 4 lines

It's not a THP specific requirement, I think.
Currenlty, SWAP_CLUSTER_MAX might be too small to get a benefit by normal page batch but it could be changed later once we implement batching logic nicely.

Consecutive or not may influence the performance of the swap slots allocation function greatly. For example, there is some non-consecutive swap slots at the begin of the swap space, and some consecutive swap slots at the end of the swap space. If the consecutive swap slots are needed, the function may need to scan from the begin to the end. If non-consecutive swap slots are required, just return the swap slots at the begin of the swap space.

Quoted 12 lines
Quoted 12 lines

And with the current design of the swap space management, it is quite hard to implement allocating nr_pages continuous free swap slots. To reduce the contention of sis->lock, even to scan one free swap slot, the sis->lock is unlocked during scanning. When we scan nr_pages free swap slots, and there are no nr_pages continuous free swap slots, we need to scan from sis->lowest_bit to sis->highest_bit, and record the largest continuous free swap slots. But when we lock sis->lock again to check, some swap slot inside the largest continuous free swap slots we found may be allocated by other processes. So we may end up with a much smaller number of swap slots or we need to startover again. So I think the simpler solution is to

Quoted 8 lines
Quoted 2 lines

- When a whole cluster is requested (for the THP), try to allocate a free cluster. Give up if there are no free clusters.

One thing I'm afraid that it would consume free clusters very fast if adjacent pages around a faulted one doesn't have same hottness/ lifetime. Once it happens, we can't get benefit any more. IMO, it's too conservative and might be worse for the fragment point of view.

It is possible. But I think we should start from the simple solution firstly. Instead of jumping to the perfect solution directly. Especially when the simple solution is a subset of the perfect solution. Do you agree?

There are some other difficulties not to use the swap cluster to hold the THP swapped out for the full THP swap support (without splitting).

The THP could be mapped in both PMD and PTE. After the THP is swapped out. There may be swap entry in PMD and PTE too. If a non-head PTE is accessed, how do we know where is the first swap slot for the THP, so that we can swap in the whole THP?

We can have a flag in cluster_info->flag to mark whether the swap cluster backing a THP. So swap in readahead can avoid to read ahead the THP, or it can read ahead the whole THP instead of just several sub-pages of the THP.

And if we use one swap cluster for each THP, we can use cluster_info->data to hold compound map number. That is very convenient.

Best Regards,
Huang, Ying

Quoted 21 lines

We can do further through scanning of per_cpu and/or global swap_map. If it makes lock contention problem, we can release the lock peridically (e.g., every SWAP_CLUSTER_MAX). I don't mean we must search 512 consecutive slots in swap_map but just bail out once we meet a hole during scanning.

Quoted 5 lines

- When a small number of swap slots are requested (for normal swap batching), check only sis->percpu_cluster and return next N free swap slots in it. Because we only scan very small number of swap slots, we can do that with sis->lock held.

Agreed.

If the batch request size is greater than cluster size, we can use a free cluster for (batch size - free cluster slots). If the batch request size is less than cluster, we can use pcp cluster.
If we fails both, we can try to consecutive slots via scanning of swap_map and let's bail out easily if we encounter the hole. About the lock contention, we might need release periodically.

Quoted 9 lines

Thanks.

Quoted 7 lines

BTW: The sis->lock is under heavy contention after the lock contention of swap cache radix tree lock is reduced via batching in 8 processes sequential swapping out test.

Best Regards,
Huang, Ying

--
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
Quoted 118 lines · Ying
Quoted 111 lines · Minchan Kim

Hi Huang,

Quoted 90 lines · Ying
Quoted 80 lines · Minchan Kim

Hi Tim,

Quoted 66 lines · Tim Chen
Quoted 50 lines · Minchan Kim
Quoted 10 lines · Ying
Quoted 4 lines

I think Tim and me discussed about that a few weeks ago.

I work closely with Tim on swap optimization.?This patchset is the part of our swap optimization plan.

Quoted 3 lines

Please search below topics.

Quoted 8 lines
Quoted 5 lines

[1] mm: Batch page reclamation under shink_page_list
[2] mm: Cleanup - Reorganize the shrink_page_list code into smaller functions

It's different with yours which focused on THP swapping while the suggestion would be more general if we can do so it's worth to try it, I think.

I think the general optimization above will benefit both normal pages and THP at least for now.?And I think there are no hard conflict between those two patchsets.

If we could do general optimzation, I guess THP swap without splitting would be more straight forward.

If we can reclaim batch a certain of pages all at once, it helps we can do scan_swap_map(si, SWAP_HAS_CACHE, nr_pages). The nr_pages could be greater or less than 512 pages. With that, scan_swap_map effectively search empty swap slots from scan_map or free cluser list. Then, needed part from your patchset is to just delay splitting of THP.

Quoted 9 lines

The THP swap has more opportunity to be optimized, because we can batch 512 operations together more easily.?For full THP swap support, unmap a THP could be more efficient with only one swap count operation instead of 512, so do many other operations, such as add/remove from swap cache with multi-order radix tree etc.?And it will help memory fragmentation. THP can be kept after swapping out/in, need not to rebuild THP via khugepaged.

It seems you increased cluster size to 512 and search a empty cluster for a THP swap. With that approach, I have a concern that once clusters will be fragmented, THP swap support doesn't take benefit at all.

Why do we need a empty cluster for swapping out 512 pages? IOW, below case could work for the goal.

A : Allocated slot
F : Free slot

cluster A?cluster B
AAAAFFFF?-?FFFFAAAA

Quoted 8 lines

That's one of the reason I suggested batch reclaim work first and support THP swap based on it. With that, scan_swap_map can be aware of nr_pages and selects right clusters.

With the approach, justfication of THP swap support would be easier, too. IOW, I'm not sure how only THP swap support is valuable in real workload.

Anyways, that's just my two cents.

Minchan,

Scanning for contiguous slots that span clusters may take quite a
long time under fragmentation, and may eventually fail. In that case the addition scan time overhead may go to waste and defeat the purpose of fast swapping of large page.

Quoted 6 lines

The empty cluster lookup on the other hand is very fast.
We treat the empty cluster available case as an opportunity for fast path swap out of large page. Otherwise, we'll revert to the current
slow path behavior of breaking into normal pages so there's no regression, and we may get speed up. We can be considerably faster when a lot of large pages are used.

I didn't mean we should search scan_swap_map firstly without peeking free cluster but what I wanted was we might abstract it into scan_swap_map.

Quoted 7 lines

For example, if nr_pages is greather than the size of cluster, we can get empty cluster first and nr_pages - sizeof(cluster) for other free cluster or scanning of current CPU per-cpu cluster. If we cannot find used slot during scanning, we can bail out simply. Then, although we fail to get all* contiguous slots, we get a certain of contiguous slots so it would be benefit for seq write and lock batching point of view at the cost of a little scanning. And it's not specific to THP algorighm.

Quoted 4 lines

Firstly, if my understanding were correct, to batch the normal pages swapping out, the swap slots need not to be continuous. But for the THP swap support, we need the continuous swap slots. So I think the requirements are quite different between them.

Hmm, I don't understand.

Let's think about swap slot management layer point of view. It doesn't need to take care of that a amount of batch request is caused by a thp page or multiple normal pages.

A matter is just that VM now asks multiple swap slots for seveal LRU-order pages so swap slot management tries to allocate several slots in a lock. Sure, it would be great if slots are consecutive fully because it means it's fast big sequential write as well as readahead together ideally. However, it would be better even if we didn't get consecutive slots because we get muliple slots all at once by batch.

Quoted 4 lines

It's not a THP specific requirement, I think.
Currenlty, SWAP_CLUSTER_MAX might be too small to get a benefit by normal page batch but it could be changed later once we implement batching logic nicely.

Quoted 7 lines

Consecutive or not may influence the performance of the swap slots allocation function greatly. For example, there is some non-consecutive swap slots at the begin of the swap space, and some consecutive swap slots at the end of the swap space. If the consecutive swap slots are needed, the function may need to scan from the begin to the end. If non-consecutive swap slots are required, just return the swap slots at the begin of the swap space.

Don't get me wrong. I never said consecutive swap slot allocation is not important and should scan swap_map fully for searching consecutive swap slot.

Both multiple normal page swap and a THP swap, consecutive swap slot allocation is important so that it's a same requirement so I want to abstract it regardless of THP swap.

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines

And with the current design of the swap space management, it is quite hard to implement allocating nr_pages continuous free swap slots. To reduce the contention of sis->lock, even to scan one free swap slot, the sis->lock is unlocked during scanning. When we scan nr_pages free swap slots, and there are no nr_pages continuous free swap slots, we need to scan from sis->lowest_bit to sis->highest_bit, and record the largest continuous free swap slots. But when we lock sis->lock again to check, some swap slot inside the largest continuous free swap slots we found may be allocated by other processes. So we may end up with a much smaller number of swap slots or we need to startover again. So I think the simpler solution is to

Quoted 8 lines
Quoted 2 lines

- When a whole cluster is requested (for the THP), try to allocate a free cluster. Give up if there are no free clusters.

One thing I'm afraid that it would consume free clusters very fast if adjacent pages around a faulted one doesn't have same hottness/ lifetime. Once it happens, we can't get benefit any more. IMO, it's too conservative and might be worse for the fragment point of view.

Quoted 4 lines

It is possible. But I think we should start from the simple solution firstly. Instead of jumping to the perfect solution directly. Especially when the simple solution is a subset of the perfect solution. Do you agree?

If simple solution works well and is hard to prove it's not bad than as-is, I agree. But my concern is about that it would consume free clusters so fast that it can affect badly for other workload.

Quoted 8 lines

There are some other difficulties not to use the swap cluster to hold the THP swapped out for the full THP swap support (without splitting).

The THP could be mapped in both PMD and PTE. After the THP is swapped out. There may be swap entry in PMD and PTE too. If a non-head PTE is accessed, how do we know where is the first swap slot for the THP, so that we can swap in the whole THP?

You mean you want to swapin 2M pages all at once? Hmm, I'm not sure it's a good idea. We don't have any evidence 512 pages have same time locality. They were just LRU in-order due to split implementation, not time locality. A thing we can bet is any processes sharing the THP doesn't touch a subpage in 512 pages so it's really *cold*. For such cold 512 page swap-in, I am really not sure.

Quoted 8 lines

We can have a flag in cluster_info->flag to mark whether the swap cluster backing a THP. So swap in readahead can avoid to read ahead the THP, or it can read ahead the whole THP instead of just several sub-pages of the THP.

And if we use one swap cluster for each THP, we can use cluster_info->data to hold compound map number. That is very convenient.

Huang,

If you think my points are enough valid, just continue your work regardless of my comment. I don't want to waste your time if it helps your workload really. And I will defer the decision to other MM people.

What I just wanted is to make swap batch for normal pages first and then support THP swap based upon it because normal page batching would more general optimization for us and I thought it will make your work more simple.

Thanks.

--
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
Quoted 188 lines · Minchan Kim
Quoted 118 lines · Ying
Quoted 111 lines · Minchan Kim

Hi Huang,

Quoted 90 lines · Ying
Quoted 80 lines · Minchan Kim

Hi Tim,

Quoted 66 lines · Tim Chen
Quoted 50 lines · Minchan Kim
Quoted 10 lines · Ying
Quoted 4 lines

I think Tim and me discussed about that a few weeks ago.

I work closely with Tim on swap optimization.?This patchset is the part of our swap optimization plan.

Quoted 3 lines

Please search below topics.

Quoted 8 lines
Quoted 5 lines

[1] mm: Batch page reclamation under shink_page_list
[2] mm: Cleanup - Reorganize the shrink_page_list code into smaller functions

It's different with yours which focused on THP swapping while the suggestion would be more general if we can do so it's worth to try it, I think.

I think the general optimization above will benefit both normal pages and THP at least for now.?And I think there are no hard conflict between those two patchsets.

If we could do general optimzation, I guess THP swap without splitting would be more straight forward.

If we can reclaim batch a certain of pages all at once, it helps we can do scan_swap_map(si, SWAP_HAS_CACHE, nr_pages). The nr_pages could be greater or less than 512 pages. With that, scan_swap_map effectively search empty swap slots from scan_map or free cluser list. Then, needed part from your patchset is to just delay splitting of THP.

Quoted 9 lines

The THP swap has more opportunity to be optimized, because we can batch 512 operations together more easily.?For full THP swap support, unmap a THP could be more efficient with only one swap count operation instead of 512, so do many other operations, such as add/remove from swap cache with multi-order radix tree etc.?And it will help memory fragmentation. THP can be kept after swapping out/in, need not to rebuild THP via khugepaged.

It seems you increased cluster size to 512 and search a empty cluster for a THP swap. With that approach, I have a concern that once clusters will be fragmented, THP swap support doesn't take benefit at all.

Why do we need a empty cluster for swapping out 512 pages? IOW, below case could work for the goal.

A : Allocated slot
F : Free slot

cluster A?cluster B
AAAAFFFF?-?FFFFAAAA

Quoted 8 lines

That's one of the reason I suggested batch reclaim work first and support THP swap based on it. With that, scan_swap_map can be aware of nr_pages and selects right clusters.

With the approach, justfication of THP swap support would be easier, too. IOW, I'm not sure how only THP swap support is valuable in real workload.

Anyways, that's just my two cents.

Minchan,

Scanning for contiguous slots that span clusters may take quite a
long time under fragmentation, and may eventually fail. In that case the addition scan time overhead may go to waste and defeat the purpose of fast swapping of large page.

Quoted 6 lines

The empty cluster lookup on the other hand is very fast.
We treat the empty cluster available case as an opportunity for fast path swap out of large page. Otherwise, we'll revert to the current
slow path behavior of breaking into normal pages so there's no regression, and we may get speed up. We can be considerably faster when a lot of large pages are used.

I didn't mean we should search scan_swap_map firstly without peeking free cluster but what I wanted was we might abstract it into scan_swap_map.

Quoted 7 lines

For example, if nr_pages is greather than the size of cluster, we can get empty cluster first and nr_pages - sizeof(cluster) for other free cluster or scanning of current CPU per-cpu cluster. If we cannot find used slot during scanning, we can bail out simply. Then, although we fail to get all* contiguous slots, we get a certain of contiguous slots so it would be benefit for seq write and lock batching point of view at the cost of a little scanning. And it's not specific to THP algorighm.

Quoted 4 lines

Firstly, if my understanding were correct, to batch the normal pages swapping out, the swap slots need not to be continuous. But for the THP swap support, we need the continuous swap slots. So I think the requirements are quite different between them.

Hmm, I don't understand.

Let's think about swap slot management layer point of view. It doesn't need to take care of that a amount of batch request is caused by a thp page or multiple normal pages.

A matter is just that VM now asks multiple swap slots for seveal LRU-order pages so swap slot management tries to allocate several slots in a lock. Sure, it would be great if slots are consecutive fully because it means it's fast big sequential write as well as readahead together ideally. However, it would be better even if we didn't get consecutive slots because we get muliple slots all at once by batch.

Quoted 4 lines

It's not a THP specific requirement, I think.
Currenlty, SWAP_CLUSTER_MAX might be too small to get a benefit by normal page batch but it could be changed later once we implement batching logic nicely.

Quoted 7 lines

Consecutive or not may influence the performance of the swap slots allocation function greatly. For example, there is some non-consecutive swap slots at the begin of the swap space, and some consecutive swap slots at the end of the swap space. If the consecutive swap slots are needed, the function may need to scan from the begin to the end. If non-consecutive swap slots are required, just return the swap slots at the begin of the swap space.

Don't get me wrong. I never said consecutive swap slot allocation is not important and should scan swap_map fully for searching consecutive swap slot.

Both multiple normal page swap and a THP swap, consecutive swap slot allocation is important so that it's a same requirement so I want to abstract it regardless of THP swap.

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines

And with the current design of the swap space management, it is quite hard to implement allocating nr_pages continuous free swap slots. To reduce the contention of sis->lock, even to scan one free swap slot, the sis->lock is unlocked during scanning. When we scan nr_pages free swap slots, and there are no nr_pages continuous free swap slots, we need to scan from sis->lowest_bit to sis->highest_bit, and record the largest continuous free swap slots. But when we lock sis->lock again to check, some swap slot inside the largest continuous free swap slots we found may be allocated by other processes. So we may end up with a much smaller number of swap slots or we need to startover again. So I think the simpler solution is to

Quoted 8 lines
Quoted 2 lines

- When a whole cluster is requested (for the THP), try to allocate a free cluster. Give up if there are no free clusters.

One thing I'm afraid that it would consume free clusters very fast if adjacent pages around a faulted one doesn't have same hottness/ lifetime. Once it happens, we can't get benefit any more. IMO, it's too conservative and might be worse for the fragment point of view.

Quoted 4 lines

It is possible. But I think we should start from the simple solution firstly. Instead of jumping to the perfect solution directly. Especially when the simple solution is a subset of the perfect solution. Do you agree?

If simple solution works well and is hard to prove it's not bad than as-is, I agree. But my concern is about that it would consume free clusters so fast that it can affect badly for other workload.

Quoted 8 lines

There are some other difficulties not to use the swap cluster to hold the THP swapped out for the full THP swap support (without splitting).

The THP could be mapped in both PMD and PTE. After the THP is swapped out. There may be swap entry in PMD and PTE too. If a non-head PTE is accessed, how do we know where is the first swap slot for the THP, so that we can swap in the whole THP?

You mean you want to swapin 2M pages all at once? Hmm, I'm not sure it's a good idea. We don't have any evidence 512 pages have same time locality. They were just LRU in-order due to split implementation, not time locality. A thing we can bet is any processes sharing the THP doesn't touch a subpage in 512 pages so it's really *cold*. For such cold 512 page swap-in, I am really not sure.

Quoted 6 lines

We can have a flag in cluster_info->flag to mark whether the swap cluster backing a THP. So swap in readahead can avoid to read ahead the THP, or it can read ahead the whole THP instead of just several sub-pages of the THP.

Quoted 6 lines
Quoted 2 lines

And if we use one swap cluster for each THP, we can use cluster_info->data to hold compound map number. That is very convenient.

Huang,

If you think my points are enough valid, just continue your work

If you don't think

Oops, typo.

--
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
In reply to Minchan Kim
Quoted 127 lines · Minchan Kim
Quoted 118 lines · Ying
Quoted 111 lines · Minchan Kim

Hi Huang,

Quoted 90 lines · Ying
Quoted 80 lines · Minchan Kim

Hi Tim,

Quoted 66 lines · Tim Chen
Quoted 50 lines · Minchan Kim
Quoted 10 lines · Ying
Quoted 4 lines

I think Tim and me discussed about that a few weeks ago.

I work closely with Tim on swap optimization.?This patchset is the part of our swap optimization plan.

Quoted 3 lines

Please search below topics.

Quoted 8 lines
Quoted 5 lines

[1] mm: Batch page reclamation under shink_page_list
[2] mm: Cleanup - Reorganize the shrink_page_list code into smaller functions

It's different with yours which focused on THP swapping while the suggestion would be more general if we can do so it's worth to try it, I think.

I think the general optimization above will benefit both normal pages and THP at least for now.?And I think there are no hard conflict between those two patchsets.

If we could do general optimzation, I guess THP swap without splitting would be more straight forward.

If we can reclaim batch a certain of pages all at once, it helps we can do scan_swap_map(si, SWAP_HAS_CACHE, nr_pages). The nr_pages could be greater or less than 512 pages. With that, scan_swap_map effectively search empty swap slots from scan_map or free cluser list. Then, needed part from your patchset is to just delay splitting of THP.

Quoted 9 lines

The THP swap has more opportunity to be optimized, because we can batch 512 operations together more easily.?For full THP swap support, unmap a THP could be more efficient with only one swap count operation instead of 512, so do many other operations, such as add/remove from swap cache with multi-order radix tree etc.?And it will help memory fragmentation. THP can be kept after swapping out/in, need not to rebuild THP via khugepaged.

It seems you increased cluster size to 512 and search a empty cluster for a THP swap. With that approach, I have a concern that once clusters will be fragmented, THP swap support doesn't take benefit at all.

Why do we need a empty cluster for swapping out 512 pages? IOW, below case could work for the goal.

A : Allocated slot
F : Free slot

cluster A?cluster B
AAAAFFFF?-?FFFFAAAA

Quoted 8 lines

That's one of the reason I suggested batch reclaim work first and support THP swap based on it. With that, scan_swap_map can be aware of nr_pages and selects right clusters.

With the approach, justfication of THP swap support would be easier, too. IOW, I'm not sure how only THP swap support is valuable in real workload.

Anyways, that's just my two cents.

Minchan,

Scanning for contiguous slots that span clusters may take quite a
long time under fragmentation, and may eventually fail. In that case the addition scan time overhead may go to waste and defeat the purpose of fast swapping of large page.

Quoted 6 lines

The empty cluster lookup on the other hand is very fast.
We treat the empty cluster available case as an opportunity for fast path swap out of large page. Otherwise, we'll revert to the current
slow path behavior of breaking into normal pages so there's no regression, and we may get speed up. We can be considerably faster when a lot of large pages are used.

I didn't mean we should search scan_swap_map firstly without peeking free cluster but what I wanted was we might abstract it into scan_swap_map.

Quoted 7 lines

For example, if nr_pages is greather than the size of cluster, we can get empty cluster first and nr_pages - sizeof(cluster) for other free cluster or scanning of current CPU per-cpu cluster. If we cannot find used slot during scanning, we can bail out simply. Then, although we fail to get all* contiguous slots, we get a certain of contiguous slots so it would be benefit for seq write and lock batching point of view at the cost of a little scanning. And it's not specific to THP algorighm.

Quoted 4 lines

Firstly, if my understanding were correct, to batch the normal pages swapping out, the swap slots need not to be continuous. But for the THP swap support, we need the continuous swap slots. So I think the requirements are quite different between them.

Hmm, I don't understand.

Let's think about swap slot management layer point of view. It doesn't need to take care of that a amount of batch request is caused by a thp page or multiple normal pages.

A matter is just that VM now asks multiple swap slots for seveal LRU-order pages so swap slot management tries to allocate several slots in a lock. Sure, it would be great if slots are consecutive fully because it means it's fast big sequential write as well as readahead together ideally. However, it would be better even if we didn't get consecutive slots because we get muliple slots all at once by batch.

Quoted 4 lines

It's not a THP specific requirement, I think.
Currenlty, SWAP_CLUSTER_MAX might be too small to get a benefit by normal page batch but it could be changed later once we implement batching logic nicely.

Quoted 7 lines

Consecutive or not may influence the performance of the swap slots allocation function greatly. For example, there is some non-consecutive swap slots at the begin of the swap space, and some consecutive swap slots at the end of the swap space. If the consecutive swap slots are needed, the function may need to scan from the begin to the end. If non-consecutive swap slots are required, just return the swap slots at the begin of the swap space.

Quoted 3 lines

Don't get me wrong. I never said consecutive swap slot allocation is not important and should scan swap_map fully for searching consecutive swap slot.

Sorry, I am confused. For multiple normal page swapping, Non-consecutive allocation is important or not? If both consecutive and non-consecutive allocation are important, how to balance between them? Restrict the scanning number?

For the THP swap, consecutive is mandatory. We need to add a parameter to specify that at least so that the allocator can try harder and use free cluster directly?

For multiple normal pages swapping, exactly consecutive swap slots allocation isn't so important. Just nearby enough should be OK for them. For example in the same swap cluster, so that it can be processed in lower level disk hardware with high efficiency (same disk segment etc.).

Quoted 26 lines

Both multiple normal page swap and a THP swap, consecutive swap slot allocation is important so that it's a same requirement so I want to abstract it regardless of THP swap.

Quoted 22 lines
Quoted 21 lines
Quoted 13 lines

And with the current design of the swap space management, it is quite hard to implement allocating nr_pages continuous free swap slots. To reduce the contention of sis->lock, even to scan one free swap slot, the sis->lock is unlocked during scanning. When we scan nr_pages free swap slots, and there are no nr_pages continuous free swap slots, we need to scan from sis->lowest_bit to sis->highest_bit, and record the largest continuous free swap slots. But when we lock sis->lock again to check, some swap slot inside the largest continuous free swap slots we found may be allocated by other processes. So we may end up with a much smaller number of swap slots or we need to startover again. So I think the simpler solution is to

Quoted 2 lines

- When a whole cluster is requested (for the THP), try to allocate a free cluster. Give up if there are no free clusters.

One thing I'm afraid that it would consume free clusters very fast if adjacent pages around a faulted one doesn't have same hottness/ lifetime. Once it happens, we can't get benefit any more. IMO, it's too conservative and might be worse for the fragment point of view.

Quoted 8 lines
Quoted 4 lines

It is possible. But I think we should start from the simple solution firstly. Instead of jumping to the perfect solution directly. Especially when the simple solution is a subset of the perfect solution. Do you agree?

If simple solution works well and is hard to prove it's not bad than as-is, I agree. But my concern is about that it would consume free clusters so fast that it can affect badly for other workload.

If we allocate consecutive swap slots other than free clusters for the THP, the free clusters will be consumed quickly too. Because not all sub-pages may be freed together. The swap space will become fragmented anyway. The situation may be better a little, but I don't think there will be huge difference here. There may be situations that 2 consecutive non-free clusters to have 512 consecutive swap slots, but I don't think that will be many.

I think we need some other ways to deal with fragmented swap problem. For example, one way is to have a list for not fully used cluster list and use that for per_cpu cluster too. Another way is starting to reclaim swap space during swapping in if the swap space becomes fragmented. And swapping in 2M page together and free the cluster backing it could be a good way to help fragmentation.

And I think, my change here will not trigger regression. For swapping out the THP, current code has almost the same behavior as for free clusters with my code. The percpu cluster will be used up, then next free cluster is used. So for each THP, one free cluster will be consumed.

Quoted 8 lines
Quoted 7 lines

There are some other difficulties not to use the swap cluster to hold the THP swapped out for the full THP swap support (without splitting).

The THP could be mapped in both PMD and PTE. After the THP is swapped out. There may be swap entry in PMD and PTE too. If a non-head PTE is accessed, how do we know where is the first swap slot for the THP, so that we can swap in the whole THP?

Quoted 6 lines

You mean you want to swapin 2M pages all at once? Hmm, I'm not sure it's a good idea. We don't have any evidence 512 pages have same time locality. They were just LRU in-order due to split implementation, not time locality. A thing we can bet is any processes sharing the THP doesn't touch a subpage in 512 pages so it's really *cold*. For such cold 512 page swap-in, I am really not sure.

On a system with /sys/kernel/mm/transparent_hugepage/enabled set to always, most anonymous pages could be THP. It could be helpful to swap out/in THP together. Do you think so? And because it is 2M sequential read, the performance is good. Only more memory may be needed, but you use more memory if you use THP anyway.

My point is, this depends on the workload. Swapping out/in THP could benefit quite some workloads. We may provide a choice for the users to turn off it when necessary. But I don't think we should make it impossible at all. Do you agree?

Quoted 5 lines
Quoted 5 lines

We can have a flag in cluster_info->flag to mark whether the swap cluster backing a THP. So swap in readahead can avoid to read ahead the THP, or it can read ahead the whole THP instead of just several sub-pages of the THP.

Quoted 8 lines
Quoted 2 lines

And if we use one swap cluster for each THP, we can use cluster_info->data to hold compound map number. That is very convenient.

Huang,

If you think my points are enough valid, just continue your work regardless of my comment. I don't want to waste your time if it helps your workload really. And I will defer the decision to other MM people.

I think your comments are good for me. Thanks a lot for your comments. We may have some different idea about requirement. But I think the discussion is good. If I give up swapping in 2M THP as a whole. You solution looks good for me now. We can use cluster_info->data to accelerate scanning, and we can give up scanning after some trying to avoid too much lock contention.

Quoted 4 lines

What I just wanted is to make swap batch for normal pages first and then support THP swap based upon it because normal page batching would more general optimization for us and I thought it will make your work more simple.

The problem is that swapping in 2M THP requirement makes it hard for the THP swap support to use the swap allocation mechanism for normal pages swapping batching. But there may be some other places that the THP swap can take advantage of. So I think it may be more reasonable for the normal pages swapping optimization go firstly. But we can discuss the basic design of the THP swapping. Do you agree? For example, whether supporting swapping in 2M THP as a whole? If so, how to do it?

Best Regards,
Huang, Ying

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

Hi, Minchan,

Quoted 2 lines · Minchan Kim

Anyway, I hope [1/11] should be merged regardless of the patchset because I believe anyone doesn't feel comfortable with cluser_info functions. ;-)

I want to send out 1/11 separately. Can I add your "Acked-by:" for it?

Best Regards,
Huang, Ying

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

#25 lore raw

Hi Huang,

On my side, there are more urgent works now so I didn't have a time to see our ongoing discussion. I will continue after settle down works, maybe next week. Sorry.

Quoted 7 lines · Ying

Hi, Minchan,

Quoted 2 lines · Minchan Kim

Anyway, I hope [1/11] should be merged regardless of the patchset because I believe anyone doesn't feel comfortable with cluser_info functions. ;-)

I want to send out 1/11 separately. Can I add your "Acked-by:" for it?

Sure.

Thanks.

#26 lore raw
Quoted 5 lines · Minchan Kim

Hi Huang,

On my side, there are more urgent works now so I didn't have a time to see our ongoing discussion. I will continue after settle down works, maybe next week. Sorry.

No problem. Thanks for your review so far!

Quoted 10 lines · Ying
Quoted 7 lines

Hi, Minchan,

Quoted 2 lines · Minchan Kim

Anyway, I hope [1/11] should be merged regardless of the patchset because I believe anyone doesn't feel comfortable with cluser_info functions. ;-)

I want to send out 1/11 separately. Can I add your "Acked-by:" for it?

Sure.

Thanks!

Best Regards,
Huang, Ying

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