提交 4f89849d 编写于 作者: M Minchan Kim 提交者: Linus Torvalds

frontswap: get rid of swap_lock dependency

Frontswap initialization routine depends on swap_lock, which want to be
atomic about frontswap's first appearance.  IOW, frontswap is not present
and will fail all calls OR frontswap is fully functional but if new
swap_info_struct isn't registered by enable_swap_info, swap subsystem
doesn't start I/O so there is no race between init procedure and page I/O
working on frontswap.

So let's remove unnecessary swap_lock dependency.

Cc: Dan Magenheimer <dan.magenheimer@oracle.com>
Signed-off-by: NMinchan Kim <minchan@kernel.org>
[v1: Rebased on my branch, reworked to work with backends loading late]
[v2: Added a check for !map]
[v3: Made the invalidate path follow the init path]
[v4: Address comments by Wanpeng Li <liwanp@linux.vnet.ibm.com>]
Signed-off-by: NKonrad Rzeszutek Wilk <konrad@darnok.org>
Signed-off-by: NBob Liu <lliubbo@gmail.com>
Cc: Wanpeng Li <liwanp@linux.vnet.ibm.com>
Cc: Andor Daam <andor.daam@googlemail.com>
Cc: Florian Schmaus <fschmaus@gmail.com>
Cc: Stefan Hengelein <ilendir@googlemail.com>
Signed-off-by: NAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: NLinus Torvalds <torvalds@linux-foundation.org>
上级 f066ea23
...@@ -23,7 +23,7 @@ extern void frontswap_writethrough(bool); ...@@ -23,7 +23,7 @@ extern void frontswap_writethrough(bool);
extern void frontswap_tmem_exclusive_gets(bool); extern void frontswap_tmem_exclusive_gets(bool);
extern bool __frontswap_test(struct swap_info_struct *, pgoff_t); extern bool __frontswap_test(struct swap_info_struct *, pgoff_t);
extern void __frontswap_init(unsigned type); extern void __frontswap_init(unsigned type, unsigned long *map);
extern int __frontswap_store(struct page *page); extern int __frontswap_store(struct page *page);
extern int __frontswap_load(struct page *page); extern int __frontswap_load(struct page *page);
extern void __frontswap_invalidate_page(unsigned, pgoff_t); extern void __frontswap_invalidate_page(unsigned, pgoff_t);
...@@ -98,10 +98,10 @@ static inline void frontswap_invalidate_area(unsigned type) ...@@ -98,10 +98,10 @@ static inline void frontswap_invalidate_area(unsigned type)
__frontswap_invalidate_area(type); __frontswap_invalidate_area(type);
} }
static inline void frontswap_init(unsigned type) static inline void frontswap_init(unsigned type, unsigned long *map)
{ {
if (frontswap_enabled) if (frontswap_enabled)
__frontswap_init(type); __frontswap_init(type, map);
} }
#endif /* _LINUX_FRONTSWAP_H */ #endif /* _LINUX_FRONTSWAP_H */
...@@ -121,8 +121,13 @@ struct frontswap_ops *frontswap_register_ops(struct frontswap_ops *ops) ...@@ -121,8 +121,13 @@ struct frontswap_ops *frontswap_register_ops(struct frontswap_ops *ops)
int i; int i;
for (i = 0; i < MAX_SWAPFILES; i++) { for (i = 0; i < MAX_SWAPFILES; i++) {
if (test_and_clear_bit(i, need_init)) if (test_and_clear_bit(i, need_init)) {
struct swap_info_struct *sis = swap_info[i];
/* __frontswap_init _should_ have set it! */
if (!sis->frontswap_map)
return ERR_PTR(-EINVAL);
ops->init(i); ops->init(i);
}
} }
/* /*
* We MUST have frontswap_ops set _after_ the frontswap_init's * We MUST have frontswap_ops set _after_ the frontswap_init's
...@@ -156,20 +161,30 @@ EXPORT_SYMBOL(frontswap_tmem_exclusive_gets); ...@@ -156,20 +161,30 @@ EXPORT_SYMBOL(frontswap_tmem_exclusive_gets);
/* /*
* Called when a swap device is swapon'd. * Called when a swap device is swapon'd.
*/ */
void __frontswap_init(unsigned type) void __frontswap_init(unsigned type, unsigned long *map)
{ {
struct swap_info_struct *sis = swap_info[type]; struct swap_info_struct *sis = swap_info[type];
if (frontswap_ops) { BUG_ON(sis == NULL);
BUG_ON(sis == NULL);
if (sis->frontswap_map == NULL) /*
return; * p->frontswap is a bitmap that we MUST have to figure out which page
* has gone in frontswap. Without it there is no point of continuing.
*/
if (WARN_ON(!map))
return;
/*
* Irregardless of whether the frontswap backend has been loaded
* before this function or it will be later, we _MUST_ have the
* p->frontswap set to something valid to work properly.
*/
frontswap_map_set(sis, map);
if (frontswap_ops)
frontswap_ops->init(type); frontswap_ops->init(type);
} else { else {
BUG_ON(type > MAX_SWAPFILES); BUG_ON(type > MAX_SWAPFILES);
set_bit(type, need_init); set_bit(type, need_init);
} }
} }
EXPORT_SYMBOL(__frontswap_init); EXPORT_SYMBOL(__frontswap_init);
......
...@@ -1509,8 +1509,7 @@ static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) ...@@ -1509,8 +1509,7 @@ static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
} }
static void _enable_swap_info(struct swap_info_struct *p, int prio, static void _enable_swap_info(struct swap_info_struct *p, int prio,
unsigned char *swap_map, unsigned char *swap_map)
unsigned long *frontswap_map)
{ {
int i, prev; int i, prev;
...@@ -1519,7 +1518,6 @@ static void _enable_swap_info(struct swap_info_struct *p, int prio, ...@@ -1519,7 +1518,6 @@ static void _enable_swap_info(struct swap_info_struct *p, int prio,
else else
p->prio = --least_priority; p->prio = --least_priority;
p->swap_map = swap_map; p->swap_map = swap_map;
frontswap_map_set(p, frontswap_map);
p->flags |= SWP_WRITEOK; p->flags |= SWP_WRITEOK;
atomic_long_add(p->pages, &nr_swap_pages); atomic_long_add(p->pages, &nr_swap_pages);
total_swap_pages += p->pages; total_swap_pages += p->pages;
...@@ -1542,10 +1540,10 @@ static void enable_swap_info(struct swap_info_struct *p, int prio, ...@@ -1542,10 +1540,10 @@ static void enable_swap_info(struct swap_info_struct *p, int prio,
unsigned char *swap_map, unsigned char *swap_map,
unsigned long *frontswap_map) unsigned long *frontswap_map)
{ {
frontswap_init(p->type, frontswap_map);
spin_lock(&swap_lock); spin_lock(&swap_lock);
spin_lock(&p->lock); spin_lock(&p->lock);
_enable_swap_info(p, prio, swap_map, frontswap_map); _enable_swap_info(p, prio, swap_map);
frontswap_init(p->type);
spin_unlock(&p->lock); spin_unlock(&p->lock);
spin_unlock(&swap_lock); spin_unlock(&swap_lock);
} }
...@@ -1554,7 +1552,7 @@ static void reinsert_swap_info(struct swap_info_struct *p) ...@@ -1554,7 +1552,7 @@ static void reinsert_swap_info(struct swap_info_struct *p)
{ {
spin_lock(&swap_lock); spin_lock(&swap_lock);
spin_lock(&p->lock); spin_lock(&p->lock);
_enable_swap_info(p, p->prio, p->swap_map, frontswap_map_get(p)); _enable_swap_info(p, p->prio, p->swap_map);
spin_unlock(&p->lock); spin_unlock(&p->lock);
spin_unlock(&swap_lock); spin_unlock(&swap_lock);
} }
...@@ -1563,6 +1561,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) ...@@ -1563,6 +1561,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
{ {
struct swap_info_struct *p = NULL; struct swap_info_struct *p = NULL;
unsigned char *swap_map; unsigned char *swap_map;
unsigned long *frontswap_map;
struct file *swap_file, *victim; struct file *swap_file, *victim;
struct address_space *mapping; struct address_space *mapping;
struct inode *inode; struct inode *inode;
...@@ -1662,12 +1661,14 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) ...@@ -1662,12 +1661,14 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
swap_map = p->swap_map; swap_map = p->swap_map;
p->swap_map = NULL; p->swap_map = NULL;
p->flags = 0; p->flags = 0;
frontswap_invalidate_area(type); frontswap_map = frontswap_map_get(p);
frontswap_map_set(p, NULL);
spin_unlock(&p->lock); spin_unlock(&p->lock);
spin_unlock(&swap_lock); spin_unlock(&swap_lock);
frontswap_invalidate_area(type);
mutex_unlock(&swapon_mutex); mutex_unlock(&swapon_mutex);
vfree(swap_map); vfree(swap_map);
vfree(frontswap_map_get(p)); vfree(frontswap_map);
/* Destroy swap account informatin */ /* Destroy swap account informatin */
swap_cgroup_swapoff(type); swap_cgroup_swapoff(type);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册