Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Minchan Kim and committed by
Linus Torvalds
4f89849d f066ea23

+35 -19
+3 -3
include/linux/frontswap.h
··· 23 23 extern void frontswap_tmem_exclusive_gets(bool); 24 24 25 25 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); 27 27 extern int __frontswap_store(struct page *page); 28 28 extern int __frontswap_load(struct page *page); 29 29 extern void __frontswap_invalidate_page(unsigned, pgoff_t); ··· 98 98 __frontswap_invalidate_area(type); 99 99 } 100 100 101 - static inline void frontswap_init(unsigned type) 101 + static inline void frontswap_init(unsigned type, unsigned long *map) 102 102 { 103 103 if (frontswap_enabled) 104 - __frontswap_init(type); 104 + __frontswap_init(type, map); 105 105 } 106 106 107 107 #endif /* _LINUX_FRONTSWAP_H */
+23 -8
mm/frontswap.c
··· 121 121 int i; 122 122 123 123 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); 125 129 ops->init(i); 130 + } 126 131 } 127 132 /* 128 133 * We MUST have frontswap_ops set _after_ the frontswap_init's ··· 161 156 /* 162 157 * Called when a swap device is swapon'd. 163 158 */ 164 - void __frontswap_init(unsigned type) 159 + void __frontswap_init(unsigned type, unsigned long *map) 165 160 { 166 161 struct swap_info_struct *sis = swap_info[type]; 167 162 168 - if (frontswap_ops) { 169 - BUG_ON(sis == NULL); 170 - if (sis->frontswap_map == NULL) 171 - return; 163 + BUG_ON(sis == NULL); 164 + 165 + /* 166 + * p->frontswap is a bitmap that we MUST have to figure out which page 167 + * has gone in frontswap. Without it there is no point of continuing. 168 + */ 169 + if (WARN_ON(!map)) 170 + return; 171 + /* 172 + * Irregardless of whether the frontswap backend has been loaded 173 + * before this function or it will be later, we _MUST_ have the 174 + * p->frontswap set to something valid to work properly. 175 + */ 176 + frontswap_map_set(sis, map); 177 + if (frontswap_ops) 172 178 frontswap_ops->init(type); 173 - } else { 179 + else { 174 180 BUG_ON(type > MAX_SWAPFILES); 175 181 set_bit(type, need_init); 176 182 } 177 - 178 183 } 179 184 EXPORT_SYMBOL(__frontswap_init); 180 185
+9 -8
mm/swapfile.c
··· 1509 1509 } 1510 1510 1511 1511 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) 1514 1513 { 1515 1514 int i, prev; 1516 1515 ··· 1518 1519 else 1519 1520 p->prio = --least_priority; 1520 1521 p->swap_map = swap_map; 1521 - frontswap_map_set(p, frontswap_map); 1522 1522 p->flags |= SWP_WRITEOK; 1523 1523 atomic_long_add(p->pages, &nr_swap_pages); 1524 1524 total_swap_pages += p->pages; ··· 1540 1542 unsigned char *swap_map, 1541 1543 unsigned long *frontswap_map) 1542 1544 { 1545 + frontswap_init(p->type, frontswap_map); 1543 1546 spin_lock(&swap_lock); 1544 1547 spin_lock(&p->lock); 1545 - _enable_swap_info(p, prio, swap_map, frontswap_map); 1546 - frontswap_init(p->type); 1548 + _enable_swap_info(p, prio, swap_map); 1547 1549 spin_unlock(&p->lock); 1548 1550 spin_unlock(&swap_lock); 1549 1551 } ··· 1552 1554 { 1553 1555 spin_lock(&swap_lock); 1554 1556 spin_lock(&p->lock); 1555 - _enable_swap_info(p, p->prio, p->swap_map, frontswap_map_get(p)); 1557 + _enable_swap_info(p, p->prio, p->swap_map); 1556 1558 spin_unlock(&p->lock); 1557 1559 spin_unlock(&swap_lock); 1558 1560 } ··· 1561 1563 { 1562 1564 struct swap_info_struct *p = NULL; 1563 1565 unsigned char *swap_map; 1566 + unsigned long *frontswap_map; 1564 1567 struct file *swap_file, *victim; 1565 1568 struct address_space *mapping; 1566 1569 struct inode *inode; ··· 1661 1662 swap_map = p->swap_map; 1662 1663 p->swap_map = NULL; 1663 1664 p->flags = 0; 1664 - frontswap_invalidate_area(type); 1665 + frontswap_map = frontswap_map_get(p); 1666 + frontswap_map_set(p, NULL); 1665 1667 spin_unlock(&p->lock); 1666 1668 spin_unlock(&swap_lock); 1669 + frontswap_invalidate_area(type); 1667 1670 mutex_unlock(&swapon_mutex); 1668 1671 vfree(swap_map); 1669 - vfree(frontswap_map_get(p)); 1672 + vfree(frontswap_map); 1670 1673 /* Destroy swap account informatin */ 1671 1674 swap_cgroup_swapoff(type); 1672 1675