Skip to content

Commit 8ea1d2a

Browse files
tehcastertorvalds
authored andcommitted
mm, frontswap: convert frontswap_enabled to static key
I have noticed that frontswap.h first declares "frontswap_enabled" as extern bool variable, and then overrides it with "#define frontswap_enabled (1)" for CONFIG_FRONTSWAP=Y or (0) when disabled. The bool variable isn't actually instantiated anywhere. This all looks like an unfinished attempt to make frontswap_enabled reflect whether a backend is instantiated. But in the current state, all frontswap hooks call unconditionally into frontswap.c just to check if frontswap_ops is non-NULL. This should at least be checked inline, but we can further eliminate the overhead when CONFIG_FRONTSWAP is enabled and no backend registered, using a static key that is initially disabled, and gets enabled only upon first backend registration. Thus, checks for "frontswap_enabled" are replaced with "frontswap_enabled()" wrapping the static key check. There are two exceptions: - xen's selfballoon_process() was testing frontswap_enabled in code guarded by #ifdef CONFIG_FRONTSWAP, which was effectively always true when reachable. The patch just removes this check. Using frontswap_enabled() does not sound correct here, as this can be true even without xen's own backend being registered. - in SYSCALL_DEFINE2(swapon), change the check to IS_ENABLED(CONFIG_FRONTSWAP) as it seems the bitmap allocation cannot currently be postponed until a backend is registered. This means that frontswap will still have some memory overhead by being configured, but without a backend. After the patch, we can expect that some functions in frontswap.c are called only when frontswap_ops is non-NULL. Change the checks there to VM_BUG_ONs. While at it, convert other BUG_ONs to VM_BUG_ONs as frontswap has been stable for some time. [[email protected]: coding-style fixes] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Vlastimil Babka <[email protected]> Cc: Konrad Rzeszutek Wilk <[email protected]> Cc: Boris Ostrovsky <[email protected]> Cc: David Vrabel <[email protected]> Cc: Juergen Gross <[email protected]> Cc: "Kirill A. Shutemov" <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent fbe84a0 commit 8ea1d2a

File tree

4 files changed

+38
-37
lines changed

4 files changed

+38
-37
lines changed

drivers/xen/xen-selfballoon.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ static void selfballoon_process(struct work_struct *work)
195195
MB2PAGES(selfballoon_reserved_mb);
196196
#ifdef CONFIG_FRONTSWAP
197197
/* allow space for frontswap pages to be repatriated */
198-
if (frontswap_selfshrinking && frontswap_enabled)
198+
if (frontswap_selfshrinking)
199199
goal_pages += frontswap_curr_pages();
200200
#endif
201201
if (cur_pages > goal_pages)
@@ -230,7 +230,7 @@ static void selfballoon_process(struct work_struct *work)
230230
reset_timer = true;
231231
}
232232
#ifdef CONFIG_FRONTSWAP
233-
if (frontswap_selfshrinking && frontswap_enabled) {
233+
if (frontswap_selfshrinking) {
234234
frontswap_selfshrink();
235235
reset_timer = true;
236236
}

include/linux/frontswap.h

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <linux/swap.h>
55
#include <linux/mm.h>
66
#include <linux/bitops.h>
7+
#include <linux/jump_label.h>
78

89
struct frontswap_ops {
910
void (*init)(unsigned); /* this swap type was just swapon'ed */
@@ -14,7 +15,6 @@ struct frontswap_ops {
1415
struct frontswap_ops *next; /* private pointer to next ops */
1516
};
1617

17-
extern bool frontswap_enabled;
1818
extern void frontswap_register_ops(struct frontswap_ops *ops);
1919
extern void frontswap_shrink(unsigned long);
2020
extern unsigned long frontswap_curr_pages(void);
@@ -30,7 +30,12 @@ extern void __frontswap_invalidate_page(unsigned, pgoff_t);
3030
extern void __frontswap_invalidate_area(unsigned);
3131

3232
#ifdef CONFIG_FRONTSWAP
33-
#define frontswap_enabled (1)
33+
extern struct static_key_false frontswap_enabled_key;
34+
35+
static inline bool frontswap_enabled(void)
36+
{
37+
return static_branch_unlikely(&frontswap_enabled_key);
38+
}
3439

3540
static inline bool frontswap_test(struct swap_info_struct *sis, pgoff_t offset)
3641
{
@@ -50,7 +55,10 @@ static inline unsigned long *frontswap_map_get(struct swap_info_struct *p)
5055
#else
5156
/* all inline routines become no-ops and all externs are ignored */
5257

53-
#define frontswap_enabled (0)
58+
static inline bool frontswap_enabled(void)
59+
{
60+
return false;
61+
}
5462

5563
static inline bool frontswap_test(struct swap_info_struct *sis, pgoff_t offset)
5664
{
@@ -70,37 +78,35 @@ static inline unsigned long *frontswap_map_get(struct swap_info_struct *p)
7078

7179
static inline int frontswap_store(struct page *page)
7280
{
73-
int ret = -1;
81+
if (frontswap_enabled())
82+
return __frontswap_store(page);
7483

75-
if (frontswap_enabled)
76-
ret = __frontswap_store(page);
77-
return ret;
84+
return -1;
7885
}
7986

8087
static inline int frontswap_load(struct page *page)
8188
{
82-
int ret = -1;
89+
if (frontswap_enabled())
90+
return __frontswap_load(page);
8391

84-
if (frontswap_enabled)
85-
ret = __frontswap_load(page);
86-
return ret;
92+
return -1;
8793
}
8894

8995
static inline void frontswap_invalidate_page(unsigned type, pgoff_t offset)
9096
{
91-
if (frontswap_enabled)
97+
if (frontswap_enabled())
9298
__frontswap_invalidate_page(type, offset);
9399
}
94100

95101
static inline void frontswap_invalidate_area(unsigned type)
96102
{
97-
if (frontswap_enabled)
103+
if (frontswap_enabled())
98104
__frontswap_invalidate_area(type);
99105
}
100106

101107
static inline void frontswap_init(unsigned type, unsigned long *map)
102108
{
103-
if (frontswap_enabled)
109+
if (frontswap_enabled())
104110
__frontswap_init(type, map);
105111
}
106112

mm/frontswap.c

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <linux/frontswap.h>
2121
#include <linux/swapfile.h>
2222

23+
DEFINE_STATIC_KEY_FALSE(frontswap_enabled_key);
24+
2325
/*
2426
* frontswap_ops are added by frontswap_register_ops, and provide the
2527
* frontswap "backend" implementation functions. Multiple implementations
@@ -139,6 +141,8 @@ void frontswap_register_ops(struct frontswap_ops *ops)
139141
ops->next = frontswap_ops;
140142
} while (cmpxchg(&frontswap_ops, ops->next, ops) != ops->next);
141143

144+
static_branch_inc(&frontswap_enabled_key);
145+
142146
spin_lock(&swap_lock);
143147
plist_for_each_entry(si, &swap_active_head, list) {
144148
if (si->frontswap_map)
@@ -189,7 +193,7 @@ void __frontswap_init(unsigned type, unsigned long *map)
189193
struct swap_info_struct *sis = swap_info[type];
190194
struct frontswap_ops *ops;
191195

192-
BUG_ON(sis == NULL);
196+
VM_BUG_ON(sis == NULL);
193197

194198
/*
195199
* p->frontswap is a bitmap that we MUST have to figure out which page
@@ -248,15 +252,9 @@ int __frontswap_store(struct page *page)
248252
pgoff_t offset = swp_offset(entry);
249253
struct frontswap_ops *ops;
250254

251-
/*
252-
* Return if no backend registed.
253-
* Don't need to inc frontswap_failed_stores here.
254-
*/
255-
if (!frontswap_ops)
256-
return -1;
257-
258-
BUG_ON(!PageLocked(page));
259-
BUG_ON(sis == NULL);
255+
VM_BUG_ON(!frontswap_ops);
256+
VM_BUG_ON(!PageLocked(page));
257+
VM_BUG_ON(sis == NULL);
260258

261259
/*
262260
* If a dup, we must remove the old page first; we can't leave the
@@ -303,11 +301,10 @@ int __frontswap_load(struct page *page)
303301
pgoff_t offset = swp_offset(entry);
304302
struct frontswap_ops *ops;
305303

306-
if (!frontswap_ops)
307-
return -1;
304+
VM_BUG_ON(!frontswap_ops);
305+
VM_BUG_ON(!PageLocked(page));
306+
VM_BUG_ON(sis == NULL);
308307

309-
BUG_ON(!PageLocked(page));
310-
BUG_ON(sis == NULL);
311308
if (!__frontswap_test(sis, offset))
312309
return -1;
313310

@@ -337,10 +334,9 @@ void __frontswap_invalidate_page(unsigned type, pgoff_t offset)
337334
struct swap_info_struct *sis = swap_info[type];
338335
struct frontswap_ops *ops;
339336

340-
if (!frontswap_ops)
341-
return;
337+
VM_BUG_ON(!frontswap_ops);
338+
VM_BUG_ON(sis == NULL);
342339

343-
BUG_ON(sis == NULL);
344340
if (!__frontswap_test(sis, offset))
345341
return;
346342

@@ -360,10 +356,9 @@ void __frontswap_invalidate_area(unsigned type)
360356
struct swap_info_struct *sis = swap_info[type];
361357
struct frontswap_ops *ops;
362358

363-
if (!frontswap_ops)
364-
return;
359+
VM_BUG_ON(!frontswap_ops);
360+
VM_BUG_ON(sis == NULL);
365361

366-
BUG_ON(sis == NULL);
367362
if (sis->frontswap_map == NULL)
368363
return;
369364

mm/swapfile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2493,7 +2493,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
24932493
goto bad_swap;
24942494
}
24952495
/* frontswap enabled? set up bit-per-page map for frontswap */
2496-
if (frontswap_enabled)
2496+
if (IS_ENABLED(CONFIG_FRONTSWAP))
24972497
frontswap_map = vzalloc(BITS_TO_LONGS(maxpages) * sizeof(long));
24982498

24992499
if (p->bdev &&(swap_flags & SWAP_FLAG_DISCARD) && swap_discardable(p)) {

0 commit comments

Comments
 (0)