mm/swapfile.c: select swap devices of default priority round robin
Subsystems (4)
v5 cover mm/swapfile.c: select swap devices of default priority round robin
Currently, on system with multiple swap devices, swap allocation will select one swap device according to priority. The swap device with the highest priority will be chosen to allocate firstly.
People can specify a priority from 0 to 32767 when swapon a swap device, or the system will set it from -2 then downwards by default. Meanwhile, on NUMA system, the swap device with node_id will be considered first on that NUMA node of the node_id.
In the current code, an array of plist, swap_avail_heads[nid], is used to organize swap devices on each NUMA node. For each NUMA node, there is a plist organizing all swap devices. The 'prio' value in the plist is the negated value of the device's priority due to plist being sorted from low to high. The swap device owning one node_id will be promoted to the front position on that NUMA node, then other swap devices are put in order of their default priority.
E.g I got a system with 8 NUMA nodes, and I setup 4 zram partition as swap devices.
Current behaviour:
their priorities will be(note that -1 is skipped):
NAME TYPE SIZE USED PRIO
/dev/zram0 partition 16G 0B -2 /dev/zram1 partition 16G 0B -3 /dev/zram2 partition 16G 0B -4 /dev/zram3 partition 16G 0B -5
And their positions in the 8 swap_avail_lists[nid] will be:
swap_avail_lists[0]: /* node 0's available swap device list */
zram0 -> zram1 -> zram2 -> zram3
prio:1 prio:3 prio:4 prio:5
swap_avali_lists[1]: /* node 1's available swap device list */
zram1 -> zram0 -> zram2 -> zram3
prio:1 prio:2 prio:4 prio:5
swap_avail_lists[2]: /* node 2's available swap device list */
zram2 -> zram0 -> zram1 -> zram3
prio:1 prio:2 prio:3 prio:5
swap_avail_lists[3]: /* node 3's available swap device list */
zram3 -> zram0 -> zram1 -> zram2
prio:1 prio:2 prio:3 prio:4
swap_avail_lists[4-7]: /* node 4,5,6,7's available swap device list */
zram0 -> zram1 -> zram2 -> zram3
prio:2 prio:3 prio:4 prio:5
The adjustment for swap device with node_id intended to decrease the pressure of lock contention for one swap device by taking different swap device on different node. The adjustment was introduced in commit a2468cc9bfdf ("swap: choose swap device according to numa node"). However, the adjustment is a little coarse-grained. On the node, the swap device sharing the node's id will always be selected firstly by node's CPUs until exhausted, then next one. And on other nodes where no swap device shares its node id, swap device with priority '-2' will be selected firstly until exhausted, then next with priority '-3'.
This is the swapon output during the process high pressure vm-scability test is being taken. It's clearly showing zram0 is heavily exploited until exhausted.
===================================
[root@hp-dl385g10-03 ~]# swapon
NAME TYPE SIZE USED PRIO
/dev/zram0 partition 16G 15.7G -2 /dev/zram1 partition 16G 3.4G -3 /dev/zram2 partition 16G 3.4G -4 /dev/zram3 partition 16G 2.6G -5
The node based strategy on selecting swap device is much better then the old way one by one selecting swap device. However it is still unreasonable because swap devices are assumed to have similar accessing speed if no priority is specified when swapon. It's unfair and doesn't make sense just because one swap device is swapped on firstly, its priority will be higher than the one swapped on later.
So in this patchset, change is made to select the swap device round robin if default priority. In code, the plist array swap_avail_heads[nid] is replaced with a plist swap_avail_head which reverts commit a2468cc9bfdf. Meanwhile, on top of the revert, further change is taken to make any device w/o specified priority get the same default priority '-1'. Surely, swap device with specified priority are always put foremost, this is not impacted. If you care about their different accessing speed, then use 'swapon -p xx' to deploy priority for your swap devices.
New behaviour:
swap_avail_list: /* one global available swap device list */
zram0 -> zram1 -> zram2 -> zram3
prio:1 prio:1 prio:1 prio:1
This is the swapon output during the process high pressure vm-scability being taken, all is selected round robin:
=======================================
[root@hp-dl385g10-03 linux]# swapon
NAME TYPE SIZE USED PRIO
/dev/zram0 partition 16G 12.6G -1 /dev/zram1 partition 16G 12.6G -1 /dev/zram2 partition 16G 12.6G -1 /dev/zram3 partition 16G 12.6G -1
With the change, we can see about 18% efficiency promotion as below:
vm-scability test:
==================
Test with:
usemem --init-time -O -y -x -n 31 2G (4G memcg, zram as swap)
Before: After: System time: 637.92 s 526.74 s (lower is better) Sum Throughput: 3546.56 MB/s 4207.56 MB/s (higher is better) Single process Throughput: 114.40 MB/s 135.72 MB/s (higher is better) free latency: 10138455.99 us 6810119.01 us (low is better)
Changelog:
==========v4->v5:
------
- Rebase on the latest mm-new;
- Clean up the relics of swap_numa in Documentation/admin-guide/mm/index.rst.
v3->v4:
------
- Rebase on the latest mm-new;
- Add Chris's Suggested-by and Acked-by.
v2->v3:
-------
- Split the v2 patch into two parts, one is reverting commit a2468cc9bfdf, the 2nd is making change to set default priority as -1 for all swap devices which makes swapping out select swap device round robin. This eases patch reviewing which is suggested by Chris, thanks.
- Fix a LKP reported issue I mistakenly added other debugging code into v2 patch. clean that up.
v1->v2:
-------
- Remove Documentation/admin-guide/mm/swap_numa.rst;
- Add back mistakenly removed lockdep_assert_held() line;
- Remove the unneeded code comment in _enable_swap_info(). Thanks a lot for careful reviewing from Chris, YoungJun and Kairui.
Baoquan He (2):
mm/swap: do not choose swap device according to numa node mm/swap: select swap device with default priority round robin
Documentation/admin-guide/mm/index.rst | 1 - Documentation/admin-guide/mm/swap_numa.rst | 78 --------------- include/linux/swap.h | 11 +-- mm/swapfile.c | 106 ++++----------------- 4 files changed, 17 insertions(+), 179 deletions(-) delete mode 100644 Documentation/admin-guide/mm/swap_numa.rst
Signature · 1 line
2.41.0
PATCH v5 1/2 mm/swap: do not choose swap device according to numa node
This reverts commit a2468cc9bfdf ("swap: choose swap device according to numa node").
After this patch, the behaviour will change back to pre-commit a2468cc9bfdf. Means the priority will be set from -1 then downwards by default, and when swapping, it will exhault swap device one by one according to priority from high to low. This is preparation work for later change.
[root@hp-dl385g10-03 ~]# swapon
NAME TYPE SIZE USED PRIO
/dev/zram0 partition 16G 16G -1 /dev/zram1 partition 16G 966.2M -2 /dev/zram2 partition 16G 0B -3 /dev/zram3 partition 16G 0B -4
--- Documentation/admin-guide/mm/index.rst | 1 - Documentation/admin-guide/mm/swap_numa.rst | 78 --------------------- include/linux/swap.h | 11 +-- mm/swapfile.c | 80 ++++------------------ 4 files changed, 15 insertions(+), 155 deletions(-) delete mode 100644 Documentation/admin-guide/mm/swap_numa.rst
Documentation/admin-guide/mm/index.rst +0 -1
@@ -39,7 +39,6 @@ the Linux memory management. shrinker_debugfs slab soft-dirty - swap_numa transhuge userfaultfd zswap
Documentation/admin-guide/mm/swap_numa.rst deleted +0 -78
78 lines not on this page · raw message
include/linux/swap.h +1 -10
@@ -301,16 +301,7 @@ struct swap_info_struct {struct work_struct discard_work;/* discard worker */ struct work_struct reclaim_work;/* reclaim worker */ struct list_head discard_clusters;/* discard clusters list */ -struct plist_node avail_lists[];/* -* entries in swap_avail_heads, one -* entry per node. -* Must be last as the number of the -* array is nr_node_ids, which is not -* a fixed value so have to allocate -* dynamically. -* And it has to be an array so that -* plist_for_each_* can work. -*/ +struct plist_node avail_list;/* entry in swap_avail_head */ };static inline swp_entry_t page_swap_entry (struct page *page)
mm/swapfile.c +14 -66
207 lines not on this page · raw message
Signature · 1 line
2.41.0
Re: [PATCH v5 mm-new 1/2] mm/swap: do not choose swap device according to numa node
Quoted 12 lines · Baoquan He
This reverts commit
a2468cc9bfdf("swap: choose swap device according to numa node").After this patch, the behaviour will change back to pre-commit
a2468cc9bfdf. Means the priority will be set from -1 then downwards by default, and when swapping, it will exhault swap device one by one according to priority from high to low. This is preparation work for later change.[root@hp-dl385g10-03 ~]# swapon
NAME TYPE SIZE USED PRIO
Quoted 8 lines
/dev/zram0 partition 16G 16G -1 /dev/zram1 partition 16G 966.2M -2 /dev/zram2 partition 16G 0B -3 /dev/zram3 partition 16G 0B -4Suggested-by Chris Li <[email protected]>Acked-by Chris Li <[email protected]>Signed-off-by Baoquan He <[email protected]>
LGTM.
PATCH v5 2/2 mm/swap: select swap device with default priority round robin
In reply to Baoquan HeSwap devices are assumed to have similar accessing speed when swapon if no priority is specified. It's unfair and doesn't make sense just because one swap device is swapped on firstly, its priority will be higher than the one swapped on later.
Here, set all swap devicess to have priority '-1' by default. With this change, swap device with default priority will be selected round robin when swapping out. This can improve the swapping efficiency a lot among multiple swap devices with default priority.
Below are swapon output during the processes when high pressure vm-scability test is being taken:
1) This is pre-commit a2468cc9bfdf, swap device is selectd one by one by priority from high to low when one swap device is exhausted:
------------------------------------
[root@hp-dl385g10-03 ~]# swapon
NAME TYPE SIZE USED PRIO
/dev/zram0 partition 16G 16G -1 /dev/zram1 partition 16G 966.2M -2 /dev/zram2 partition 16G 0B -3 /dev/zram3 partition 16G 0B -4
2) This is behaviour with commit a2468cc9bfdf, on node, swap device sharing the same node id is selected firstly until exhausted; while on node no swap device sharing the node id it selects the one with highest priority until exhaustd:
------------------------------------
[root@hp-dl385g10-03 ~]# swapon
NAME TYPE SIZE USED PRIO
/dev/zram0 partition 16G 15.7G -2 /dev/zram1 partition 16G 3.4G -3 /dev/zram2 partition 16G 3.4G -4 /dev/zram3 partition 16G 2.6G -5
3) After this patch applied, swap devices with default priority are selectd round robin:
------------------------------------
[root@hp-dl385g10-03 block]# swapon
NAME TYPE SIZE USED PRIO
/dev/zram0 partition 16G 6.6G -1 /dev/zram1 partition 16G 6.6G -1 /dev/zram2 partition 16G 6.6G -1 /dev/zram3 partition 16G 6.6G -1
With the change, about 18% efficiency promotion relative to node based way as below. (Surely, the pre-commit a2468cc9bfdf way is the worst.)
vm-scability test:
==================
Test with:
usemem --init-time -O -y -x -n 31 2G (4G memcg, zram as swap)
one by one: node based: round robin: System time: 1087.38 s 637.92 s 526.74 s (lower is better) Sum Throughput: 2036.55 MB/s 3546.56 MB/s 4207.56 MB/s (higher is better) Single process Throughput: 65.69 MB/s 114.40 MB/s 135.72 MB/s (high is better) free latency: 15769409.48 us 10138455.99 us 6810119.01 us(lower is better)
--- mm/swapfile.c | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-)
mm/swapfile.c +4 -26
@@ -74,7 +74,7 @@ atomic_long_t nr_swap_pages;EXPORT_SYMBOL_GPL (nr_swap_pages);/* protected with swap_lock. reading in vm_swap_full() doesn't need lock */ long total_swap_pages; -static int least_priority; +#define DEF_SWAP_PRIO -1unsigned long swapfile_maximum_size;#ifdef CONFIG_MIGRATION bool swap_migration_ad_supported;
@@ -2707,10 +2707,7 @@ static void setup_swap_info(struct swap_info_struct *si, int prio,struct swap_cluster_info *cluster_info, unsigned long *zeromap) { -if (prio >=0 ) - si->prio = prio; -else - si->prio = --least_priority; + si->prio = prio;/* * the plist prio is negated because plist ordering is * low-to-high, while swap ordering is high-to-low
@@ -2728,16 +2725,7 @@ static void _enable_swap_info(struct swap_info_struct *si) total_swap_pages += si->pages;assert_spin_locked (&swap_lock); -/* -* both lists are plists, and thus priority ordered. -* swap_active_head needs to be priority ordered for swapoff(), -* which on removal of any swap_info_struct with an auto-assigned -* (i.e. negative) priority increments the auto-assigned priority -* of any lower-priority swap_info_structs. -* swap_avail_head needs to be priority ordered for folio_alloc_swap(), -* which allocates swap pages from the highest available priority -* swap_info_struct. -*/ +plist_add (&si->list, &swap_active_head);/* Add back to available list */
@@ -2887,16 +2875,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) }spin_lock (&p->lock);del_from_avail_list (p, true); -if (p->prio <0 ) { -struct swap_info_struct *si = p; - -plist_for_each_entry_continue (si, &swap_active_head, list) { - si->prio++; - si->list.prio--; - si->avail_list.prio--; - } - least_priority++; - }plist_del (&p->list, &swap_active_head);atomic_long_sub (p->pages, &nr_swap_pages); total_swap_pages -= p->pages;
@@ -3607,7 +3585,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) }mutex_lock (&swapon_mutex); - prio =-1 ; + prio =DEF_SWAP_PRIO ;if (swap_flags &SWAP_FLAG_PREFER ) prio = swap_flags &SWAP_FLAG_PRIO_MASK ;enable_swap_info (si, prio, swap_map, cluster_info, zeromap);
Signature · 1 line
2.41.0
Re: [PATCH v5 mm-new 2/2] mm/swap: select swap device with default priority round robin
Quoted 10 lines · Baoquan He
Swap devices are assumed to have similar accessing speed when swapon if no priority is specified. It's unfair and doesn't make sense just because one swap device is swapped on firstly, its priority will be higher than the one swapped on later.
Here, set all swap devicess to have priority '-1' by default. With this change, swap device with default priority will be selected round robin when swapping out. This can improve the swapping efficiency a lot among multiple swap devices with default priority.
Yeah, I agree. In a set up with multiple swap devices, this can spread out the swap writes, potentially improving throughput.
Quoted 49 lines
Below are swapon output during the processes when high pressure vm-scability test is being taken:
1) This is pre-commit
a2468cc9bfdf, swap device is selectd one by one by priority from high to low when one swap device is exhausted:
------------------------------------
[root@hp-dl385g10-03 ~]# swapon
NAME TYPE SIZE USED PRIO/dev/zram0 partition 16G 16G -1 /dev/zram1 partition 16G 966.2M -2 /dev/zram2 partition 16G 0B -3 /dev/zram3 partition 16G 0B -42) This is behaviour with commit
a2468cc9bfdf, on node, swap device sharing the same node id is selected firstly until exhausted; while on node no swap device sharing the node id it selects the one with highest priority until exhaustd:
------------------------------------
[root@hp-dl385g10-03 ~]# swapon
NAME TYPE SIZE USED PRIO/dev/zram0 partition 16G 15.7G -2 /dev/zram1 partition 16G 3.4G -3 /dev/zram2 partition 16G 3.4G -4 /dev/zram3 partition 16G 2.6G -53) After this patch applied, swap devices with default priority are selectd round robin:
------------------------------------
[root@hp-dl385g10-03 block]# swapon
NAME TYPE SIZE USED PRIO/dev/zram0 partition 16G 6.6G -1 /dev/zram1 partition 16G 6.6G -1 /dev/zram2 partition 16G 6.6G -1 /dev/zram3 partition 16G 6.6G -1With the change, about 18% efficiency promotion relative to node based way as below. (Surely, the pre-commit
a2468cc9bfdfway is the worst.)vm-scability test:
==================
Test with:
usemem --init-time -O -y -x -n 31 2G (4G memcg, zram as swap)one by one: node based: round robin: System time: 1087.38 s 637.92 s 526.74 s (lower is better) Sum Throughput: 2036.55 MB/s 3546.56 MB/s 4207.56 MB/s (higher is better) Single process Throughput: 65.69 MB/s 114.40 MB/s 135.72 MB/s (high is better) free latency: 15769409.48 us 10138455.99 us 6810119.01 us(lower is better)
Quoted 3 lines
Suggested-by Chris Li <[email protected]>Acked-by Chris Li <[email protected]>Signed-off-by Baoquan He <[email protected]>
Quoted 151 lines · Baoquan He
Currently, on system with multiple swap devices, swap allocation will select one swap device according to priority. The swap device with the highest priority will be chosen to allocate firstly.
People can specify a priority from 0 to 32767 when swapon a swap device, or the system will set it from -2 then downwards by default. Meanwhile, on NUMA system, the swap device with
node_idwill be considered first on that NUMA node of thenode_id.In the current code, an array of plist, swap_avail_heads[nid], is used to organize swap devices on each NUMA node. For each NUMA node, there is a plist organizing all swap devices. The 'prio' value in the plist is the negated value of the device's priority due to plist being sorted from low to high. The swap device owning one
node_idwill be promoted to the front position on that NUMA node, then other swap devices are put in order of their default priority.E.g I got a system with 8 NUMA nodes, and I setup 4 zram partition as swap devices.
Current behaviour:
their priorities will be(note that -1 is skipped):
NAME TYPE SIZE USED PRIO/dev/zram0 partition 16G 0B -2 /dev/zram1 partition 16G 0B -3 /dev/zram2 partition 16G 0B -4 /dev/zram3 partition 16G 0B -5And their positions in the 8 swap_avail_lists[nid] will be:
swap_avail_lists[0]: /* node 0's available swap device list */
zram0 -> zram1 -> zram2 -> zram3
prio:1 prio:3 prio:4 prio:5
swap_avali_lists[1]: /* node 1's available swap device list */
zram1 -> zram0 -> zram2 -> zram3
prio:1 prio:2 prio:4 prio:5
swap_avail_lists[2]: /* node 2's available swap device list */
zram2 -> zram0 -> zram1 -> zram3
prio:1 prio:2 prio:3 prio:5
swap_avail_lists[3]: /* node 3's available swap device list */
zram3 -> zram0 -> zram1 -> zram2
prio:1 prio:2 prio:3 prio:4
swap_avail_lists[4-7]: /* node 4,5,6,7's available swap device list */
zram0 -> zram1 -> zram2 -> zram3
prio:2 prio:3 prio:4 prio:5The adjustment for swap device with
node_idintended to decrease the pressure of lock contention for one swap device by taking different swap device on different node. The adjustment was introduced in commita2468cc9bfdf("swap: choose swap device according to numa node"). However, the adjustment is a little coarse-grained. On the node, the swap device sharing the node's id will always be selected firstly by node's CPUs until exhausted, then next one. And on other nodes where no swap device shares its node id, swap device with priority '-2' will be selected firstly until exhausted, then next with priority '-3'.This is the swapon output during the process high pressure vm-scability test is being taken. It's clearly showing zram0 is heavily exploited until exhausted.
===================================
[root@hp-dl385g10-03 ~]# swapon
NAME TYPE SIZE USED PRIO/dev/zram0 partition 16G 15.7G -2 /dev/zram1 partition 16G 3.4G -3 /dev/zram2 partition 16G 3.4G -4 /dev/zram3 partition 16G 2.6G -5The node based strategy on selecting swap device is much better then the old way one by one selecting swap device. However it is still unreasonable because swap devices are assumed to have similar accessing speed if no priority is specified when swapon. It's unfair and doesn't make sense just because one swap device is swapped on firstly, its priority will be higher than the one swapped on later.
So in this patchset, change is made to select the swap device round robin if default priority. In code, the plist array swap_avail_heads[nid] is replaced with a plist
swap_avail_headwhich reverts commita2468cc9bfdf. Meanwhile, on top of the revert, further change is taken to make any device w/o specified priority get the same default priority '-1'. Surely, swap device with specified priority are always put foremost, this is not impacted. If you care about their different accessing speed, then use 'swapon -p xx' to deploy priority for your swap devices.New behaviour:
swap_avail_list: /* one global available swap device list */
zram0 -> zram1 -> zram2 -> zram3
prio:1 prio:1 prio:1 prio:1This is the swapon output during the process high pressure vm-scability being taken, all is selected round robin:
=======================================
[root@hp-dl385g10-03 linux]# swapon
NAME TYPE SIZE USED PRIO/dev/zram0 partition 16G 12.6G -1 /dev/zram1 partition 16G 12.6G -1 /dev/zram2 partition 16G 12.6G -1 /dev/zram3 partition 16G 12.6G -1With the change, we can see about 18% efficiency promotion as below:
vm-scability test:
==================
Test with:
usemem --init-time -O -y -x -n 31 2G (4G memcg, zram as swap)Before: After: System time: 637.92 s 526.74 s (lower is better) Sum Throughput: 3546.56 MB/s 4207.56 MB/s (higher is better) Single process Throughput: 114.40 MB/s 135.72 MB/s (higher is better) free latency: 10138455.99 us 6810119.01 us (low is better)Changelog:
==========v4->v5:
------
- Rebase on the latest mm-new;
- Clean up the relics ofswap_numainDocumentation/admin-guide/mm/index.rst.
v3->v4:
------
- Rebase on the latest mm-new;
- Add Chris's Suggested-by and Acked-by.
v2->v3:
-------
- Split the v2 patch into two parts, one is reverting commita2468cc9bfdf, the 2nd is making change to set default priority as -1 for all swap devices which makes swapping out select swap device round robin. This eases patch reviewing which is suggested by Chris, thanks.
- Fix a LKP reported issue I mistakenly added other debugging code into v2 patch. clean that up.
v1->v2:
-------
- RemoveDocumentation/admin-guide/mm/swap_numa.rst;
- Add back mistakenly removedlockdep_assert_held()line;
- Remove the unneeded code comment in_enable_swap_info(). Thanks a lot for careful reviewing from Chris, YoungJun and Kairui.Baoquan He (2):
mm/swap: do not choose swap device according to numa node mm/swap: select swap device with default priority round robinDocumentation/admin-guide/mm/index.rst | 1 - Documentation/admin-guide/mm/swap_numa.rst | 78 --------------- include/linux/swap.h | 11 +-- mm/swapfile.c | 106 ++++----------------- 4 files changed, 17 insertions(+), 179 deletions(-) delete mode 100644 Documentation/admin-guide/mm/swap_numa.rst
Quoted 4 lines
--
2.41.0
Glad to see the performance is better and the code is cleaner, thanks!
For the series: