Skip to content

Commit ebc2a1a

Browse files
Shaohua Litorvalds
authored andcommitted
swap: make cluster allocation per-cpu
swap cluster allocation is to get better request merge to improve performance. But the cluster is shared globally, if multiple tasks are doing swap, this will cause interleave disk access. While multiple tasks swap is quite common, for example, each numa node has a kswapd thread doing swap and multiple threads/processes doing direct page reclaim. ioscheduler can't help too much here, because tasks don't send swapout IO down to block layer in the meantime. Block layer does merge some IOs, but a lot not, depending on how many tasks are doing swapout concurrently. In practice, I've seen a lot of small size IO in swapout workloads. We makes the cluster allocation per-cpu here. The interleave disk access issue goes away. All tasks swapout to their own cluster, so swapout will become sequential, which can be easily merged to big size IO. If one CPU can't get its per-cpu cluster (for example, there is no free cluster anymore in the swap), it will fallback to scan swap_map. The CPU can still continue swap. We don't need recycle free swap entries of other CPUs. In my test (swap to a 2-disk raid0 partition), this improves around 10% swapout throughput, and request size is increased significantly. How does this impact swap readahead is uncertain though. On one side, page reclaim always isolates and swaps several adjancent pages, this will make page reclaim write the pages sequentially and benefit readahead. On the other side, several CPU write pages interleave means the pages don't live _sequentially_ but relatively _near_. In the per-cpu allocation case, if adjancent pages are written by different cpus, they will live relatively _far_. So how this impacts swap readahead depends on how many pages page reclaim isolates and swaps one time. If the number is big, this patch will benefit swap readahead. Of course, this is about sequential access pattern. The patch has no impact for random access pattern, because the new cluster allocation algorithm is just for SSD. Alternative solution is organizing swap layout to be per-mm instead of this per-cpu approach. In the per-mm layout, we allocate a disk range for each mm, so pages of one mm live in swap disk adjacently. per-mm layout has potential issues of lock contention if multiple reclaimers are swap pages from one mm. For a sequential workload, per-mm layout is better to implement swap readahead, because pages from the mm are adjacent in disk. But per-cpu layout isn't very bad in this workload, as page reclaim always isolates and swaps several pages one time, such pages will still live in disk sequentially and readahead can utilize this. For a random workload, per-mm layout isn't beneficial of request merge, because it's quite possible pages from different mm are swapout in the meantime and IO can't be merged in per-mm layout. while with per-cpu layout we can merge requests from any mm. Considering random workload is more popular in workloads with swap (and per-cpu approach isn't too bad for sequential workload too), I'm choosing per-cpu layout. [akpm@linux-foundation.org: coding-style fixes] Signed-off-by: Shaohua Li <shli@fusionio.com> Cc: Rik van Riel <riel@redhat.com> Cc: Minchan Kim <minchan@kernel.org> Cc: Kyungmin Park <kmpark@infradead.org> Cc: Hugh Dickins <hughd@google.com> Cc: Rafael Aquini <aquini@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent edfe23d commit ebc2a1a

2 files changed

Lines changed: 102 additions & 34 deletions

File tree

‎include/linux/swap.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,16 @@ struct swap_cluster_info {
198198
#define CLUSTER_FLAG_FREE 1 /* This cluster is free */
199199
#define CLUSTER_FLAG_NEXT_NULL 2 /* This cluster has no next cluster */
200200

201+
/*
202+
* We assign a cluster to each CPU, so each CPU can allocate swap entry from
203+
* its own cluster and swapout sequentially. The purpose is to optimize swapout
204+
* throughput.
205+
*/
206+
struct percpu_cluster {
207+
struct swap_cluster_info index; /* Current cluster index */
208+
unsigned int next; /* Likely next allocation offset */
209+
};
210+
201211
/*
202212
* The in-memory structure used to track swap areas.
203213
*/
@@ -217,6 +227,7 @@ struct swap_info_struct {
217227
unsigned int inuse_pages; /* number of those currently in use */
218228
unsigned int cluster_next; /* likely index for next allocation */
219229
unsigned int cluster_nr; /* countdown to next cluster search */
230+
struct percpu_cluster __percpu *percpu_cluster; /* per cpu's swap location */
220231
struct swap_extent *curr_swap_extent;
221232
struct swap_extent first_swap_extent;
222233
struct block_device *bdev; /* swap device or bdev of swap file */

‎mm/swapfile.c‎

Lines changed: 91 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -392,13 +392,78 @@ static void dec_cluster_info_page(struct swap_info_struct *p,
392392
* It's possible scan_swap_map() uses a free cluster in the middle of free
393393
* cluster list. Avoiding such abuse to avoid list corruption.
394394
*/
395-
static inline bool scan_swap_map_recheck_cluster(struct swap_info_struct *si,
395+
static bool
396+
scan_swap_map_ssd_cluster_conflict(struct swap_info_struct *si,
396397
unsigned long offset)
397398
{
399+
struct percpu_cluster *percpu_cluster;
400+
bool conflict;
401+
398402
offset /= SWAPFILE_CLUSTER;
399-
return !cluster_is_null(&si->free_cluster_head) &&
403+
conflict = !cluster_is_null(&si->free_cluster_head) &&
400404
offset != cluster_next(&si->free_cluster_head) &&
401405
cluster_is_free(&si->cluster_info[offset]);
406+
407+
if (!conflict)
408+
return false;
409+
410+
percpu_cluster = this_cpu_ptr(si->percpu_cluster);
411+
cluster_set_null(&percpu_cluster->index);
412+
return true;
413+
}
414+
415+
/*
416+
* Try to get a swap entry from current cpu's swap entry pool (a cluster). This
417+
* might involve allocating a new cluster for current CPU too.
418+
*/
419+
static void scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
420+
unsigned long *offset, unsigned long *scan_base)
421+
{
422+
struct percpu_cluster *cluster;
423+
bool found_free;
424+
unsigned long tmp;
425+
426+
new_cluster:
427+
cluster = this_cpu_ptr(si->percpu_cluster);
428+
if (cluster_is_null(&cluster->index)) {
429+
if (!cluster_is_null(&si->free_cluster_head)) {
430+
cluster->index = si->free_cluster_head;
431+
cluster->next = cluster_next(&cluster->index) *
432+
SWAPFILE_CLUSTER;
433+
} else if (!cluster_is_null(&si->discard_cluster_head)) {
434+
/*
435+
* we don't have free cluster but have some clusters in
436+
* discarding, do discard now and reclaim them
437+
*/
438+
swap_do_scheduled_discard(si);
439+
*scan_base = *offset = si->cluster_next;
440+
goto new_cluster;
441+
} else
442+
return;
443+
}
444+
445+
found_free = false;
446+
447+
/*
448+
* Other CPUs can use our cluster if they can't find a free cluster,
449+
* check if there is still free entry in the cluster
450+
*/
451+
tmp = cluster->next;
452+
while (tmp < si->max && tmp < (cluster_next(&cluster->index) + 1) *
453+
SWAPFILE_CLUSTER) {
454+
if (!si->swap_map[tmp]) {
455+
found_free = true;
456+
break;
457+
}
458+
tmp++;
459+
}
460+
if (!found_free) {
461+
cluster_set_null(&cluster->index);
462+
goto new_cluster;
463+
}
464+
cluster->next = tmp + 1;
465+
*offset = tmp;
466+
*scan_base = tmp;
402467
}
403468

404469
static unsigned long scan_swap_map(struct swap_info_struct *si,
@@ -423,41 +488,17 @@ static unsigned long scan_swap_map(struct swap_info_struct *si,
423488
si->flags += SWP_SCANNING;
424489
scan_base = offset = si->cluster_next;
425490

491+
/* SSD algorithm */
492+
if (si->cluster_info) {
493+
scan_swap_map_try_ssd_cluster(si, &offset, &scan_base);
494+
goto checks;
495+
}
496+
426497
if (unlikely(!si->cluster_nr--)) {
427498
if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
428499
si->cluster_nr = SWAPFILE_CLUSTER - 1;
429500
goto checks;
430501
}
431-
check_cluster:
432-
if (!cluster_is_null(&si->free_cluster_head)) {
433-
offset = cluster_next(&si->free_cluster_head) *
434-
SWAPFILE_CLUSTER;
435-
last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
436-
si->cluster_next = offset;
437-
si->cluster_nr = SWAPFILE_CLUSTER - 1;
438-
goto checks;
439-
} else if (si->cluster_info) {
440-
/*
441-
* we don't have free cluster but have some clusters in
442-
* discarding, do discard now and reclaim them
443-
*/
444-
if (!cluster_is_null(&si->discard_cluster_head)) {
445-
si->cluster_nr = 0;
446-
swap_do_scheduled_discard(si);
447-
scan_base = offset = si->cluster_next;
448-
if (!si->cluster_nr)
449-
goto check_cluster;
450-
si->cluster_nr--;
451-
goto checks;
452-
}
453-
454-
/*
455-
* Checking free cluster is fast enough, we can do the
456-
* check every time
457-
*/
458-
si->cluster_nr = 0;
459-
goto checks;
460-
}
461502

462503
spin_unlock(&si->lock);
463504

@@ -516,8 +557,10 @@ static unsigned long scan_swap_map(struct swap_info_struct *si,
516557
}
517558

518559
checks:
519-
if (scan_swap_map_recheck_cluster(si, offset))
520-
goto check_cluster;
560+
if (si->cluster_info) {
561+
while (scan_swap_map_ssd_cluster_conflict(si, offset))
562+
scan_swap_map_try_ssd_cluster(si, &offset, &scan_base);
563+
}
521564
if (!(si->flags & SWP_WRITEOK))
522565
goto no_page;
523566
if (!si->highest_bit)
@@ -1884,6 +1927,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
18841927
spin_unlock(&swap_lock);
18851928
frontswap_invalidate_area(type);
18861929
mutex_unlock(&swapon_mutex);
1930+
free_percpu(p->percpu_cluster);
1931+
p->percpu_cluster = NULL;
18871932
vfree(swap_map);
18881933
vfree(cluster_info);
18891934
vfree(frontswap_map);
@@ -2403,6 +2448,16 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
24032448
error = -ENOMEM;
24042449
goto bad_swap;
24052450
}
2451+
p->percpu_cluster = alloc_percpu(struct percpu_cluster);
2452+
if (!p->percpu_cluster) {
2453+
error = -ENOMEM;
2454+
goto bad_swap;
2455+
}
2456+
for_each_possible_cpu(i) {
2457+
struct percpu_cluster *cluster;
2458+
cluster = per_cpu_ptr(p->percpu_cluster, i);
2459+
cluster_set_null(&cluster->index);
2460+
}
24062461
}
24072462

24082463
error = swap_cgroup_swapon(p->type, maxpages);
@@ -2475,6 +2530,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
24752530
error = 0;
24762531
goto out;
24772532
bad_swap:
2533+
free_percpu(p->percpu_cluster);
2534+
p->percpu_cluster = NULL;
24782535
if (inode && S_ISBLK(inode->i_mode) && p->bdev) {
24792536
set_blocksize(p->bdev, p->old_block_size);
24802537
blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);

0 commit comments

Comments
 (0)