Skip to contentSkip to conversation

swap: choose swap device according to numa node

RFC PATCH In review Aaron Lu 3 participants 6 messages Apr 29, 2016 – Jul 5, 2016

Subsystems (4)
lore
Share & tools
lore

Aaron Lu

author
#0 lore raw

RFC PATCH 1/1 swap: choose swap device according to numa node

If the system has more than one swap device and swap device has the node information, we can make use of this information to decide which swap device to use in get_swap_page.

The current code uses a priority based list, swap_avail_list, to decide which swap device to use each time and if multiple swap devices share the same priority, they are used round robin. This patch change the previous single global swap_avail_list into a per-numa-node list, i.e. for each numa node, it sees its own priority based list of available swap devices. This will require checking a swap device's node value during swap on time and then promote its priority(more on thie below) in the swap_avail_list according to which node's list it is being added to. Once this is done, there should be little, if not none, cost in get_swap_page time.

The current swap device's priority is set as: user can set a >=0 value, or the system will pick one by starting from -1 then downwards. And the priority value in the swap_avail_list is the negated value of the swap device's priority due to plist is sorted from low to high. The new policy doesn't change the semantics for priority >=0 cases, the previous starting from -1 then downwards now becomes starting from -2 then downwards. -1 is reserved as the promoted value.

Take an 4-node EX machine as an example, suppose 4 swap devices are available, each sit on a different node:
swapA on node 0
swapB on node 1
swapC on node 2
swapD on node 3

After they are all swapped on in the sequence of ABCD.

Current behaviour:
their priorities will be:
swapA: -1
swapB: -2
swapC: -3
swapD: -4
And their position in the global swap_avail_list will be:
swapA -> swapB -> swapC -> swapD
prio:1 prio:2 prio:3 prio:4

New behaviour:
their priorities will be(note that -1 is skipped):
swapA: -2
swapB: -3
swapC: -4
swapD: -5
And their positions in the 4 swap_avail_lists[node] will be:
swap_avail_lists[0]: /* node 0's available swap device list */
swapA -> swapB -> swapC -> swapD
prio:1 prio:3 prio:4 prio:5
swap_avali_lists[1]: /* node 1's available swap device list */
swapB -> swapA -> swapC -> swapD
prio:1 prio:2 prio:4 prio:5
swap_avail_lists[2]: /* node 2's available swap device list */
swapC -> swapA -> swapB -> swapD
prio:1 prio:2 prio:3 prio:5
swap_avail_lists[3]: /* node 3's available swap device list */
swapD -> swapA -> swapB -> swapC
prio:1 prio:2 prio:3 prio:4

The test case used is:
https://git.kernel.org/cgit/linux/kernel/git/wfg/vm-scalability.git/tree/case-swap-w-seq
https://git.kernel.org/cgit/linux/kernel/git/wfg/vm-scalability.git/tree/usemem.c
What the test does is: start N process, each map a region of anonymous space and then write to it sequentially to trigger swap outs. On Haswell EP 2 node machine with 128GiB memory, two persistent memory devices are created, each with a size of 48GiB sitting on a different node are used as swap devices, they are swapped on without being specified a priority value and the test result is:
1 task/write size is around 95GiB
throughput of v4.5: 1475358.0
throughput of the patch: 1751160.0
18% increase in throughput
16 task/write size of each is around 6.6GiB
throughput of v4.5: 2148972.4
throughput of the patch: 5713310.0
165% increase in throughput

The huge increase is partly due to the lock contention on the single swapper_space's radix tree lock since v4.5 will always use the higher priority swap device till it's full before using another one. Setting them with the same priority could avoid this, so here are the results considering this case:
1 task/write size is around 95GiB
throughput of v4.5: 1475358.0
throughput of v4.5(swap device with equal priority): 1707893.4 throughput of the patch: 1751160.0
almost the same for the latter two
16 task/write size of each is around 6.6GiB
throughput of v4.5: 2148972.4
throughput of v4.5(swap device with equal priority): 3804688.25 throughput of the patch: 5713310.0
increase reduced to 50%

Comments are appreciated.

---
 include/linux/swap.h |  4 +--
 mm/swap.c            | 12 +++++++-
 mm/swapfile.c        | 81 ++++++++++++++++++++++++++++++++++------------------
 mm/vmscan.c          |  6 ++--
 4 files changed, 71 insertions(+), 32 deletions(-)
include/linux/swap.h +2 -2
index d18b65c53dbb..eafda3ac42eb 100644
@@ -207,7 +207,7 @@ struct swap_info_struct {
 	unsigned long	flags;		/* SWP_USED etc: see above */
 	signed short	prio;		/* swap priority of this type */
 	struct plist_node list;		/* entry in swap_active_head */
-	struct plist_node avail_list;	/* entry in swap_avail_head */
+	struct plist_node avail_lists[MAX_NUMNODES];	/* entry in swap_avail_head */
 	signed char	type;		/* strange name for an index */
 	unsigned int	max;		/* extent of the swap_map */
 	unsigned char *swap_map;	/* vmalloc'ed array of usage counts */
@@ -308,7 +308,7 @@ extern void lru_add_drain_all(void);
 extern void rotate_reclaimable_page(struct page *page);
 extern void deactivate_file_page(struct page *page);
 extern void deactivate_page(struct page *page);
-extern void swap_setup(void);
+extern int swap_setup(void);
 
 extern void add_page_to_unevictable_list(struct page *page);
 
mm/swap.c +11 -1
index 09fe5e97714a..fad3368bb0d1 100644
@@ -943,14 +943,22 @@ EXPORT_SYMBOL(pagevec_lookup_tag);
 /*
  * Perform any setup for the swap system
  */
-void __init swap_setup(void)
+int __init swap_setup(void)
 {
 	unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
 #ifdef CONFIG_SWAP
 	int i;
+	extern struct plist_head *swap_avail_heads;
 
 	for (i = 0; i < MAX_SWAPFILES; i++)
 		spin_lock_init(&swapper_spaces[i].tree_lock);
+
+	swap_avail_heads = kmalloc(nr_node_ids * sizeof(struct plist_head), GFP_KERNEL);
+	if (!swap_avail_heads)
+		return -ENOMEM;
+
+	for (i = 0; i < nr_node_ids; i++)
+		plist_head_init(&swap_avail_heads[i]);
 #endif
 
 	/* Use a smaller cluster for small-memory machines */
@@ -962,4 +970,6 @@ void __init swap_setup(void)
 	 * Right now other parts of the system means that we
 	 * _really_ don't want to cluster much more
 	 */
+
+	return 0;
 }
mm/swapfile.c +54 -27

191 lines not on this page · raw message

mm/vmscan.c +4 -2
index 71b1c29948db..dd7e44a315b0 100644
@@ -3659,9 +3659,11 @@ void kswapd_stop(int nid)
 
 static int __init kswapd_init(void)
 {
-	int nid;
+	int nid, err;
 
-	swap_setup();
+	err = swap_setup();
+	if (err)
+		return err;
 	for_each_node_state(nid, N_MEMORY)
  		kswapd_run(nid);
 	hotcpu_notifier(cpu_callback, 0);
Signature · 7 lines
2.5.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw
Quoted 87 lines

-----Original Message-----
From: Lu, Aaron
Sent: Friday, April 29, 2016 1:34 AM
To: Linux MM
Cc: Chen, Tim C; Huang, Ying
Subject: [RFC PATCH] swap: choose swap device according to numa node

If the system has more than one swap device and swap device has the node information, we can make use of this information to decide which swap device to use in get_swap_page.

The current code uses a priority based list, swap_avail_list, to decide which swap device to use each time and if multiple swap devices share the same priority, they are used round robin. This patch change the previous single global swap_avail_list into a per-numa-node list, i.e.
for each numa node, it sees its own priority based list of available swap devices. This will require checking a swap device's node value during swap on time and then promote its priority(more on thie below) in the swap_avail_list according to which node's list it is being added to. Once this is done, there should be little, if not none, cost in get_swap_page time.

The current swap device's priority is set as: user can set a >=0 value, or the system will pick one by starting from -1 then downwards.
And the priority value in the swap_avail_list is the negated value of the swap device's priority due to plist is sorted from low to high. The new policy doesn't change the semantics for priority >=0 cases, the previous starting from -1 then downwards now becomes starting from -2 then downwards. -1 is reserved as the promoted value.

Take an 4-node EX machine as an example, suppose 4 swap devices are available, each sit on a different node:
swapA on node 0
swapB on node 1
swapC on node 2
swapD on node 3

After they are all swapped on in the sequence of ABCD.

Current behaviour:
their priorities will be:
swapA: -1
swapB: -2
swapC: -3
swapD: -4
And their position in the global swap_avail_list will be:
swapA -> swapB -> swapC -> swapD
prio:1 prio:2 prio:3 prio:4

New behaviour:
their priorities will be(note that -1 is skipped):
swapA: -2
swapB: -3
swapC: -4
swapD: -5
And their positions in the 4 swap_avail_lists[node] will be:
swap_avail_lists[0]: /* node 0's available swap device list */
swapA -> swapB -> swapC -> swapD
prio:1 prio:3 prio:4 prio:5
swap_avali_lists[1]: /* node 1's available swap device list */
swapB -> swapA -> swapC -> swapD
prio:1 prio:2 prio:4 prio:5
swap_avail_lists[2]: /* node 2's available swap device list */
swapC -> swapA -> swapB -> swapD
prio:1 prio:2 prio:3 prio:5
swap_avail_lists[3]: /* node 3's available swap device list */
swapD -> swapA -> swapB -> swapC
prio:1 prio:2 prio:3 prio:4

The test case used is:
https://git.kernel.org/cgit/linux/kernel/git/wfg/vm-scalability.git/tree/case-
swap-w-seq
https://git.kernel.org/cgit/linux/kernel/git/wfg/vm-scalability.git/tree/usemem.c
What the test does is: start N process, each map a region of anonymous space and then write to it sequentially to trigger swap outs.
On Haswell EP 2 node machine with 128GiB memory, two persistent memory devices are created, each with a size of 48GiB sitting on a different node are used as swap devices, they are swapped on without being specified a priority value and the test result is:
1 task/write size is around 95GiB
throughput of v4.5: 1475358.0
throughput of the patch: 1751160.0
18% increase in throughput
16 task/write size of each is around 6.6GiB throughput of v4.5: 2148972.4 throughput of the patch: 5713310.0 165% increase in throughput

Quoted 12 lines

The huge increase is partly due to the lock contention on the single swapper_space's radix tree lock since v4.5 will always use the higher priority swap device till it's full before using another one. Setting them with the same priority could avoid this, so here are the results considering this case:
1 task/write size is around 95GiB
throughput of v4.5: 1475358.0
throughput of v4.5(swap device with equal priority): 1707893.4 throughput of the patch: 1751160.0 almost the same for the latter two
16 task/write size of each is around 6.6GiB throughput of v4.5: 2148972.4 throughput of v4.5(swap device with equal priority): 3804688.25 throughput of the patch: 5713310.0 increase reduced to 50%

Wonder if choosing the swap device by numa node is the most effective way to spread the pages among the swap devices. The speedup comes from spreading the swap activities among equal priority swap devices to reduce contention on swap devices. If the activities are mostly confined to 1 node, then we still could have contention on a device.

An alternative may be we pick another swap device on each pass of shrink_page_list to try to swap pages.

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>

author
#2 lore raw
Quoted 2 lines · Tim C

Wonder if choosing the swap device by numa node is the most effective way to spread the pages among the swap devices.

The intent of this patch is not to spread the pages among the swap devices(thus to reduce lock contention on swap device's radix tree), it's about speed up the IOs :-)

Quoted 2 lines

The speedup comes from spreading the swap activities among equal priority swap devices to reduce contention on swap devices.

For v4.5, yes. And for this patch, it also has speed ups by doing IOs locally.

Quoted 2 lines

If the activities are mostly confined to 1 node, then we still could have contention on a device.

Indeed, but I suppose that would normally happen if people has played with numactl themselves? Otherwise, the scheduler would probably spread the threads evenly.

Ying suggests we use a config for people to turn this off in his reply and I can of course add that.

Quoted 2 lines

An alternative may be we pick another swap device on each pass of shrink_page_list to try to swap pages.

This can be achieved by setting the two swap devices with equal priority and then the two swap devices will be used round robin. I have already used it as a comparing config:
throughput of v4.5(swap device with equal priority)

Thanks for the comments!

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

author
#3 lore raw

RFC PATCH 1/1 swap: choose swap device according to numa node

In reply to Aaron Lu

Resend:
This is a resend, the original patch doesn't catch much attention. It may not be a big deal for swap devices that used to be hosted on HDD but with devices like 3D Xpoint to be used as swap device, it could make a real difference if we consider NUMA information when doing IO. Comments are appreciated, thanks for your time.

Original changelog:
If the system has more than one swap device and swap device has the node information, we can make use of this information to decide which swap device to use in get_swap_page.

The current code uses a priority based list, swap_avail_list, to decide which swap device to use each time and if multiple swap devices share the same priority, they are used round robin. This patch change the previous single global swap_avail_list into a per-numa-node list, i.e. for each numa node, it sees its own priority based list of available swap devices. This will require checking a swap device's node value during swap on time and then promote its priority(more on this below) in the swap_avail_list according to which node's list it is being added to. Once this is done, there should be little, if not none, cost in get_swap_page time.

The current swap device's priority is set as: user can set a >=0 value, or the system will pick one by starting from -1 then downwards. And the priority value in the swap_avail_list is the negated value of the swap device's priority due to plist is sorted from low to high. The new policy doesn't change the semantics for priority >=0 cases, the previous starting from -1 then downwards now becomes starting from -2 then downwards. -1 is reserved as the promoted value.

Take an 4-node EX machine as an example, suppose 4 swap devices are available, each sit on a different node:
swapA on node 0
swapB on node 1
swapC on node 2
swapD on node 3

After they are all swapped on in the sequence of ABCD.

Current behaviour:
their priorities will be:
swapA: -1
swapB: -2
swapC: -3
swapD: -4
And their position in the global swap_avail_list will be:
swapA -> swapB -> swapC -> swapD
prio:1 prio:2 prio:3 prio:4

New behaviour:
their priorities will be(note that -1 is skipped):
swapA: -2
swapB: -3
swapC: -4
swapD: -5
And their positions in the 4 swap_avail_lists[node] will be:
swap_avail_lists[0]: /* node 0's available swap device list */
swapA -> swapB -> swapC -> swapD
prio:1 prio:3 prio:4 prio:5
swap_avali_lists[1]: /* node 1's available swap device list */
swapB -> swapA -> swapC -> swapD
prio:1 prio:2 prio:4 prio:5
swap_avail_lists[2]: /* node 2's available swap device list */
swapC -> swapA -> swapB -> swapD
prio:1 prio:2 prio:3 prio:5
swap_avail_lists[3]: /* node 3's available swap device list */
swapD -> swapA -> swapB -> swapC
prio:1 prio:2 prio:3 prio:4

The test case used is:
https://git.kernel.org/cgit/linux/kernel/git/wfg/vm-scalability.git/tree/case-swap-w-seq
https://git.kernel.org/cgit/linux/kernel/git/wfg/vm-scalability.git/tree/usemem.c
What the test does is: start N process, each map a region of anonymous space and then write to it sequentially to trigger swap outs. On Haswell EP 2 node machine with 128GiB memory, two persistent memory devices are created, each with a size of 48GiB sitting on a different node are used as swap devices, they are swapped on without being specified a priority value and the test result is:
1 task/write size is around 95GiB
throughput of v4.5: 1475358.0
throughput of the patch: 1751160.0
18% increase in throughput
16 task/write size of each is around 6.6GiB
throughput of v4.5: 2148972.4
throughput of the patch: 5713310.0
165% increase in throughput

The huge increase is partly due to the lock contention on the single swapper_space's radix tree lock since v4.5 will always use the higher priority swap device till it's full before using another one. Setting them with the same priority could avoid this, so here are the results considering this case:
1 task/write size is around 95GiB
throughput of v4.5: 1475358.0
throughput of v4.5(swap device with equal priority): 1707893.4 throughput of the patch: 1751160.0
almost the same for the latter two
16 task/write size of each is around 6.6GiB
throughput of v4.5: 2148972.4
throughput of v4.5(swap device with equal priority): 3804688.25 throughput of the patch: 5713310.0
increase reduced to 50%

Comments are appreciated.

---
 include/linux/swap.h |  4 +--
 mm/swap.c            | 12 +++++++-
 mm/swapfile.c        | 81 ++++++++++++++++++++++++++++++++++------------------
 mm/vmscan.c          |  6 ++--
 4 files changed, 71 insertions(+), 32 deletions(-)
include/linux/swap.h +2 -2
index d18b65c53dbb..eafda3ac42eb 100644
@@ -207,7 +207,7 @@ struct swap_info_struct {
 	unsigned long	flags;		/* SWP_USED etc: see above */
 	signed short	prio;		/* swap priority of this type */
 	struct plist_node list;		/* entry in swap_active_head */
-	struct plist_node avail_list;	/* entry in swap_avail_head */
+	struct plist_node avail_lists[MAX_NUMNODES];	/* entry in swap_avail_head */
 	signed char	type;		/* strange name for an index */
 	unsigned int	max;		/* extent of the swap_map */
 	unsigned char *swap_map;	/* vmalloc'ed array of usage counts */
@@ -308,7 +308,7 @@ extern void lru_add_drain_all(void);
 extern void rotate_reclaimable_page(struct page *page);
 extern void deactivate_file_page(struct page *page);
 extern void deactivate_page(struct page *page);
-extern void swap_setup(void);
+extern int swap_setup(void);
 
 extern void add_page_to_unevictable_list(struct page *page);
 
mm/swap.c +11 -1
index 09fe5e97714a..fad3368bb0d1 100644
@@ -943,14 +943,22 @@ EXPORT_SYMBOL(pagevec_lookup_tag);
 /*
  * Perform any setup for the swap system
  */
-void __init swap_setup(void)
+int __init swap_setup(void)
 {
 	unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
 #ifdef CONFIG_SWAP
 	int i;
+	extern struct plist_head *swap_avail_heads;
 
 	for (i = 0; i < MAX_SWAPFILES; i++)
 		spin_lock_init(&swapper_spaces[i].tree_lock);
+
+	swap_avail_heads = kmalloc(nr_node_ids * sizeof(struct plist_head), GFP_KERNEL);
+	if (!swap_avail_heads)
+		return -ENOMEM;
+
+	for (i = 0; i < nr_node_ids; i++)
+		plist_head_init(&swap_avail_heads[i]);
 #endif
 
 	/* Use a smaller cluster for small-memory machines */
@@ -962,4 +970,6 @@ void __init swap_setup(void)
 	 * Right now other parts of the system means that we
 	 * _really_ don't want to cluster much more
 	 */
+
+	return 0;
 }
mm/swapfile.c +54 -27

191 lines not on this page · raw message

mm/vmscan.c +4 -2
index 71b1c29948db..dd7e44a315b0 100644
@@ -3659,9 +3659,11 @@ void kswapd_stop(int nid)
 
 static int __init kswapd_init(void)
 {
-	int nid;
+	int nid, err;
 
-	swap_setup();
+	err = swap_setup();
+	if (err)
+		return err;
 	for_each_node_state(nid, N_MEMORY)
  		kswapd_run(nid);
 	hotcpu_notifier(cpu_callback, 0);
Signature · 1 line
2.5.5
#4 lore raw
Quoted 7 lines · Aaron Lu

Resend:
This is a resend, the original patch doesn't catch much attention. It may not be a big deal for swap devices that used to be hosted on HDD but with devices like 3D Xpoint to be used as swap device, it could make a real difference if we consider NUMA information when doing IO. Comments are appreciated, thanks for your time.

-------------------------%<-------------------------

Quoted 8 lines
mm/vmscan.c +0 -0
index 71b1c29948db..dd7e44a315b0 100644
@@ -3659,9 +3659,11 @@ void kswapd_stop(int nid)
 
 static int __init kswapd_init(void)
 {

mm/vmscan.c void kswapd_stop(int nid) · line 3669

Quoted 10 lines
mm/vmscan.c +4 -2
-       int nid;
+       int nid, err;
 
-       swap_setup();
+       err = swap_setup();
+       if (err)
+               return err;
        for_each_node_state(nid, N_MEMORY)
                kswapd_run(nid);
        hotcpu_notifier(cpu_callback, 0);

In original implementation, although swap_setup failed, the swapd would also be created, since swapd is not only used for swap out but also for other page reclaim, so this change above might modify its semantic? Sorry if I understand incorrectly.

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

author
#5 lore raw
Quoted 20 lines · Yu Chen
Quoted 7 lines · Aaron Lu

Resend:
This is a resend, the original patch doesn't catch much attention. It may not be a big deal for swap devices that used to be hosted on HDD but with devices like 3D Xpoint to be used as swap device, it could make a real difference if we consider NUMA information when doing IO. Comments are appreciated, thanks for your time.

-------------------------%<-------------------------

Quoted 11 lines
mm/vmscan.c +1 -1
index 71b1c29948db..dd7e44a315b0 100644
@@ -3659,9 +3659,11 @@ void kswapd_stop(int nid)
 
 static int __init kswapd_init(void)
 {
-       int nid;
+       int nid, err;
 
Quoted 8 lines

mm/vmscan.c void kswapd_stop(int nid) · line 3669

Quoted 7 lines
mm/vmscan.c +3 -1
-       swap_setup();
+       err = swap_setup();
+       if (err)
+               return err;
        for_each_node_state(nid, N_MEMORY)
                kswapd_run(nid);
        hotcpu_notifier(cpu_callback, 0);

In original implementation, although swap_setup failed,

In current implementaion swap_setup never fail :-)

Quoted 4 lines

the swapd would also be created, since swapd is not only used for swap out but also for other page reclaim, so this change above might modify its semantic? Sorry if I understand incorrectly.

Indeed it's a behaviour change. The only reason swap_setup can return an error code now is when it fails to allocate nr_node_ids * sizeof(struct plist_head) memory and if that happens, I don't think it makes much sense to continue boot the system.

Thanks,
Aaron

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