Skip to content

Commit 81a0298

Browse files
yhuang-inteltorvalds
authored andcommitted
mm, swap: don't use VMA based swap readahead if HDD is used as swap
VMA based swap readahead will readahead the virtual pages that is continuous in the virtual address space. While the original swap readahead will readahead the swap slots that is continuous in the swap device. Although VMA based swap readahead is more correct for the swap slots to be readahead, it will trigger more small random readings, which may cause the performance of HDD (hard disk) to degrade heavily, and may finally exceed the benefit. To avoid the issue, in this patch, if the HDD is used as swap, the VMA based swap readahead will be disabled, and the original swap readahead will be used instead. Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: "Huang, Ying" <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Minchan Kim <[email protected]> Cc: Rik van Riel <[email protected]> Cc: Shaohua Li <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Fengguang Wu <[email protected]> Cc: Tim Chen <[email protected]> Cc: Dave Hansen <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent d9bfcfd commit 81a0298

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

include/linux/swap.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,16 +400,17 @@ extern struct page *do_swap_page_readahead(swp_entry_t fentry, gfp_t gfp_mask,
400400
struct vm_fault *vmf,
401401
struct vma_swap_readahead *swap_ra);
402402

403-
static inline bool swap_use_vma_readahead(void)
404-
{
405-
return READ_ONCE(swap_vma_readahead);
406-
}
407-
408403
/* linux/mm/swapfile.c */
409404
extern atomic_long_t nr_swap_pages;
410405
extern long total_swap_pages;
406+
extern atomic_t nr_rotate_swap;
411407
extern bool has_usable_swap(void);
412408

409+
static inline bool swap_use_vma_readahead(void)
410+
{
411+
return READ_ONCE(swap_vma_readahead) && !atomic_read(&nr_rotate_swap);
412+
}
413+
413414
/* Swap 50% full? Release swapcache more aggressively.. */
414415
static inline bool vm_swap_full(void)
415416
{

mm/swapfile.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait);
9696
/* Activity counter to indicate that a swapon or swapoff has occurred */
9797
static atomic_t proc_poll_event = ATOMIC_INIT(0);
9898

99+
atomic_t nr_rotate_swap = ATOMIC_INIT(0);
100+
99101
static inline unsigned char swap_count(unsigned char ent)
100102
{
101103
return ent & ~SWAP_HAS_CACHE; /* may include SWAP_HAS_CONT flag */
@@ -2569,6 +2571,9 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
25692571
if (p->flags & SWP_CONTINUED)
25702572
free_swap_count_continuations(p);
25712573

2574+
if (!p->bdev || !blk_queue_nonrot(bdev_get_queue(p->bdev)))
2575+
atomic_dec(&nr_rotate_swap);
2576+
25722577
mutex_lock(&swapon_mutex);
25732578
spin_lock(&swap_lock);
25742579
spin_lock(&p->lock);
@@ -3145,7 +3150,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
31453150
cluster = per_cpu_ptr(p->percpu_cluster, cpu);
31463151
cluster_set_null(&cluster->index);
31473152
}
3148-
}
3153+
} else
3154+
atomic_inc(&nr_rotate_swap);
31493155

31503156
error = swap_cgroup_swapon(p->type, maxpages);
31513157
if (error)

0 commit comments

Comments
 (0)