Skip to content

Commit ec8acf2

Browse files
Shaohua Litorvalds
authored andcommitted
swap: add per-partition lock for swapfile
swap_lock is heavily contended when I test swap to 3 fast SSD (even slightly slower than swap to 2 such SSD). The main contention comes from swap_info_get(). This patch tries to fix the gap with adding a new per-partition lock. Global data like nr_swapfiles, total_swap_pages, least_priority and swap_list are still protected by swap_lock. nr_swap_pages is an atomic now, it can be changed without swap_lock. In theory, it's possible get_swap_page() finds no swap pages but actually there are free swap pages. But sounds not a big problem. Accessing partition specific data (like scan_swap_map and so on) is only protected by swap_info_struct.lock. Changing swap_info_struct.flags need hold swap_lock and swap_info_struct.lock, because scan_scan_map() will check it. read the flags is ok with either the locks hold. If both swap_lock and swap_info_struct.lock must be hold, we always hold the former first to avoid deadlock. swap_entry_free() can change swap_list. To delete that code, we add a new highest_priority_index. Whenever get_swap_page() is called, we check it. If it's valid, we use it. It's a pity get_swap_page() still holds swap_lock(). But in practice, swap_lock() isn't heavily contended in my test with this patch (or I can say there are other much more heavier bottlenecks like TLB flush). And BTW, looks get_swap_page() doesn't really need the lock. We never free swap_info[] and we check SWAP_WRITEOK flag. The only risk without the lock is we could swapout to some low priority swap, but we can quickly recover after several rounds of swap, so sounds not a big deal to me. But I'd prefer to fix this if it's a real problem. "swap: make each swap partition have one address_space" improved the swapout speed from 1.7G/s to 2G/s. This patch further improves the speed to 2.3G/s, so around 15% improvement. It's a multi-process test, so TLB flush isn't the biggest bottleneck before the patches. [arnd@arndb.de: fix it for nommu] [hughd@google.com: add missing unlock] [minchan@kernel.org: get rid of lockdep whinge on sys_swapon] Signed-off-by: Shaohua Li <shli@fusionio.com> Cc: Hugh Dickins <hughd@google.com> Cc: Rik van Riel <riel@redhat.com> Cc: Minchan Kim <minchan.kim@gmail.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Seth Jennings <sjenning@linux.vnet.ibm.com> Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com> Cc: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com> Cc: Dan Magenheimer <dan.magenheimer@oracle.com> Cc: Stephen Rothwell <sfr@canb.auug.org.au> Signed-off-by: Arnd Bergmann <arnd@arndb.de> Signed-off-by: Hugh Dickins <hughd@google.com> Signed-off-by: Minchan Kim <minchan@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 33806f0 commit ec8acf2

8 files changed

Lines changed: 145 additions & 60 deletions

File tree

‎arch/sparc/mm/init_32.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void show_mem(unsigned int filter)
5757
printk("Mem-info:\n");
5858
show_free_areas(filter);
5959
printk("Free swap: %6ldkB\n",
60-
nr_swap_pages << (PAGE_SHIFT-10));
60+
get_nr_swap_pages() << (PAGE_SHIFT-10));
6161
printk("%ld pages of RAM\n", totalram_pages);
6262
printk("%ld free pages\n", nr_free_pages());
6363
}

‎arch/tile/mm/pgtable.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void show_mem(unsigned int filter)
6161
global_page_state(NR_PAGETABLE),
6262
global_page_state(NR_BOUNCE),
6363
global_page_state(NR_FILE_PAGES),
64-
nr_swap_pages);
64+
get_nr_swap_pages());
6565

6666
for_each_zone(zone) {
6767
unsigned long flags, order, total = 0, largest_order = -1;

‎include/linux/swap.h‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,25 @@ struct swap_info_struct {
202202
unsigned long *frontswap_map; /* frontswap in-use, one bit per page */
203203
atomic_t frontswap_pages; /* frontswap pages in-use counter */
204204
#endif
205+
spinlock_t lock; /*
206+
* protect map scan related fields like
207+
* swap_map, lowest_bit, highest_bit,
208+
* inuse_pages, cluster_next,
209+
* cluster_nr, lowest_alloc and
210+
* highest_alloc. other fields are only
211+
* changed at swapon/swapoff, so are
212+
* protected by swap_lock. changing
213+
* flags need hold this lock and
214+
* swap_lock. If both locks need hold,
215+
* hold swap_lock first.
216+
*/
205217
};
206218

207219
struct swap_list_t {
208220
int head; /* head of priority-ordered swapfile list */
209221
int next; /* swapfile to be used next */
210222
};
211223

212-
/* Swap 50% full? Release swapcache more aggressively.. */
213-
#define vm_swap_full() (nr_swap_pages*2 < total_swap_pages)
214-
215224
/* linux/mm/page_alloc.c */
216225
extern unsigned long totalram_pages;
217226
extern unsigned long totalreserve_pages;
@@ -347,8 +356,20 @@ extern struct page *swapin_readahead(swp_entry_t, gfp_t,
347356
struct vm_area_struct *vma, unsigned long addr);
348357

349358
/* linux/mm/swapfile.c */
350-
extern long nr_swap_pages;
359+
extern atomic_long_t nr_swap_pages;
351360
extern long total_swap_pages;
361+
362+
/* Swap 50% full? Release swapcache more aggressively.. */
363+
static inline bool vm_swap_full(void)
364+
{
365+
return atomic_long_read(&nr_swap_pages) * 2 < total_swap_pages;
366+
}
367+
368+
static inline long get_nr_swap_pages(void)
369+
{
370+
return atomic_long_read(&nr_swap_pages);
371+
}
372+
352373
extern void si_swapinfo(struct sysinfo *);
353374
extern swp_entry_t get_swap_page(void);
354375
extern swp_entry_t get_swap_page_of_type(int);
@@ -381,9 +402,10 @@ mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
381402

382403
#else /* CONFIG_SWAP */
383404

384-
#define nr_swap_pages 0L
405+
#define get_nr_swap_pages() 0L
385406
#define total_swap_pages 0L
386407
#define total_swapcache_pages() 0UL
408+
#define vm_swap_full() 0
387409

388410
#define si_swapinfo(val) \
389411
do { (val)->freeswap = (val)->totalswap = 0; } while (0)

‎mm/mmap.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
144144
*/
145145
free -= global_page_state(NR_SHMEM);
146146

147-
free += nr_swap_pages;
147+
free += get_nr_swap_pages();
148148

149149
/*
150150
* Any slabs which are created with the

‎mm/nommu.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1907,7 +1907,7 @@ int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
19071907
*/
19081908
free -= global_page_state(NR_SHMEM);
19091909

1910-
free += nr_swap_pages;
1910+
free += get_nr_swap_pages();
19111911

19121912
/*
19131913
* Any slabs which are created with the

‎mm/swap_state.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ void show_swap_cache_info(void)
6969
printk("Swap cache stats: add %lu, delete %lu, find %lu/%lu\n",
7070
swap_cache_info.add_total, swap_cache_info.del_total,
7171
swap_cache_info.find_success, swap_cache_info.find_total);
72-
printk("Free swap = %ldkB\n", nr_swap_pages << (PAGE_SHIFT - 10));
72+
printk("Free swap = %ldkB\n",
73+
get_nr_swap_pages() << (PAGE_SHIFT - 10));
7374
printk("Total swap = %lukB\n", total_swap_pages << (PAGE_SHIFT - 10));
7475
}
7576

0 commit comments

Comments
 (0)