Skip to content

Commit 4f89849

Browse files
minchanktorvalds
authored andcommitted
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: Minchan 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: Konrad Rzeszutek Wilk <konrad@darnok.org> Signed-off-by: Bob 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: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f066ea2 commit 4f89849

3 files changed

Lines changed: 35 additions & 19 deletions

File tree

‎include/linux/frontswap.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extern void frontswap_writethrough(bool);
2323
extern void frontswap_tmem_exclusive_gets(bool);
2424

2525
extern bool __frontswap_test(struct swap_info_struct *, pgoff_t);
26-
extern void __frontswap_init(unsigned type);
26+
extern void __frontswap_init(unsigned type, unsigned long *map);
2727
extern int __frontswap_store(struct page *page);
2828
extern int __frontswap_load(struct page *page);
2929
extern void __frontswap_invalidate_page(unsigned, pgoff_t);
@@ -98,10 +98,10 @@ static inline void frontswap_invalidate_area(unsigned type)
9898
__frontswap_invalidate_area(type);
9999
}
100100

101-
static inline void frontswap_init(unsigned type)
101+
static inline void frontswap_init(unsigned type, unsigned long *map)
102102
{
103103
if (frontswap_enabled)
104-
__frontswap_init(type);
104+
__frontswap_init(type, map);
105105
}
106106

107107
#endif /* _LINUX_FRONTSWAP_H */

‎mm/frontswap.c‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,13 @@ struct frontswap_ops *frontswap_register_ops(struct frontswap_ops *ops)
121121
int i;
122122

123123
for (i = 0; i < MAX_SWAPFILES; i++) {
124-
if (test_and_clear_bit(i, need_init))
124+
if (test_and_clear_bit(i, need_init)) {
125+
struct swap_info_struct *sis = swap_info[i];
126+
/* __frontswap_init _should_ have set it! */
127+
if (!sis->frontswap_map)
128+
return ERR_PTR(-EINVAL);
125129
ops->init(i);
130+
}
126131
}
127132
/*
128133
* We MUST have frontswap_ops set _after_ the frontswap_init's
@@ -156,20 +161,30 @@ EXPORT_SYMBOL(frontswap_tmem_exclusive_gets);
156161
/*
157162
* Called when a swap device is swapon'd.
158163
*/
159-
void __frontswap_init(unsigned type)
164+
void __frontswap_init(unsigned type, unsigned long *map)
160165
{
161166
struct swap_info_struct *sis = swap_info[type];
162167

163-
if (frontswap_ops) {
164-
BUG_ON(sis == NULL);
165-
if (sis->frontswap_map == NULL)
166-
return;
168+
BUG_ON(sis == NULL);
169+
170+
/*
171+
* p->frontswap is a bitmap that we MUST have to figure out which page
172+
* has gone in frontswap. Without it there is no point of continuing.
173+
*/
174+
if (WARN_ON(!map))
175+
return;
176+
/*
177+
* Irregardless of whether the frontswap backend has been loaded
178+
* before this function or it will be later, we _MUST_ have the
179+
* p->frontswap set to something valid to work properly.
180+
*/
181+
frontswap_map_set(sis, map);
182+
if (frontswap_ops)
167183
frontswap_ops->init(type);
168-
} else {
184+
else {
169185
BUG_ON(type > MAX_SWAPFILES);
170186
set_bit(type, need_init);
171187
}
172-
173188
}
174189
EXPORT_SYMBOL(__frontswap_init);
175190

‎mm/swapfile.c‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,8 +1509,7 @@ static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
15091509
}
15101510

15111511
static void _enable_swap_info(struct swap_info_struct *p, int prio,
1512-
unsigned char *swap_map,
1513-
unsigned long *frontswap_map)
1512+
unsigned char *swap_map)
15141513
{
15151514
int i, prev;
15161515

@@ -1519,7 +1518,6 @@ static void _enable_swap_info(struct swap_info_struct *p, int prio,
15191518
else
15201519
p->prio = --least_priority;
15211520
p->swap_map = swap_map;
1522-
frontswap_map_set(p, frontswap_map);
15231521
p->flags |= SWP_WRITEOK;
15241522
atomic_long_add(p->pages, &nr_swap_pages);
15251523
total_swap_pages += p->pages;
@@ -1542,10 +1540,10 @@ static void enable_swap_info(struct swap_info_struct *p, int prio,
15421540
unsigned char *swap_map,
15431541
unsigned long *frontswap_map)
15441542
{
1543+
frontswap_init(p->type, frontswap_map);
15451544
spin_lock(&swap_lock);
15461545
spin_lock(&p->lock);
1547-
_enable_swap_info(p, prio, swap_map, frontswap_map);
1548-
frontswap_init(p->type);
1546+
_enable_swap_info(p, prio, swap_map);
15491547
spin_unlock(&p->lock);
15501548
spin_unlock(&swap_lock);
15511549
}
@@ -1554,7 +1552,7 @@ static void reinsert_swap_info(struct swap_info_struct *p)
15541552
{
15551553
spin_lock(&swap_lock);
15561554
spin_lock(&p->lock);
1557-
_enable_swap_info(p, p->prio, p->swap_map, frontswap_map_get(p));
1555+
_enable_swap_info(p, p->prio, p->swap_map);
15581556
spin_unlock(&p->lock);
15591557
spin_unlock(&swap_lock);
15601558
}
@@ -1563,6 +1561,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
15631561
{
15641562
struct swap_info_struct *p = NULL;
15651563
unsigned char *swap_map;
1564+
unsigned long *frontswap_map;
15661565
struct file *swap_file, *victim;
15671566
struct address_space *mapping;
15681567
struct inode *inode;
@@ -1662,12 +1661,14 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
16621661
swap_map = p->swap_map;
16631662
p->swap_map = NULL;
16641663
p->flags = 0;
1665-
frontswap_invalidate_area(type);
1664+
frontswap_map = frontswap_map_get(p);
1665+
frontswap_map_set(p, NULL);
16661666
spin_unlock(&p->lock);
16671667
spin_unlock(&swap_lock);
1668+
frontswap_invalidate_area(type);
16681669
mutex_unlock(&swapon_mutex);
16691670
vfree(swap_map);
1670-
vfree(frontswap_map_get(p));
1671+
vfree(frontswap_map);
16711672
/* Destroy swap account informatin */
16721673
swap_cgroup_swapoff(type);
16731674

0 commit comments

Comments
 (0)