Skip to content

Commit 1da0d94

Browse files
Christoph Hellwigtorvalds
authored andcommitted
frontswap: remove support for multiple ops
There is only a single instance of frontswap ops in the kernel, so simplify the frontswap code by removing support for multiple operations. Link: https://lkml.kernel.org/r/20211224062246.1258487-13-hch@lst.de Signed-off-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Juergen Gross <jgross@suse.com> Cc: Dan Streetman <ddstreet@ieee.org> Cc: Geert Uytterhoeven <geert@linux-m68k.org> Cc: Hugh Dickins <hughd@google.com> Cc: Konrad Rzeszutek Wilk <Konrad.wilk@oracle.com> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Seth Jennings <sjenning@redhat.com> Cc: Vitaly Wool <vitaly.wool@konsulko.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 633423a commit 1da0d94

File tree

3 files changed

+19
-42
lines changed

3 files changed

+19
-42
lines changed

include/linux/frontswap.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ struct frontswap_ops {
1313
int (*load)(unsigned, pgoff_t, struct page *); /* load a page */
1414
void (*invalidate_page)(unsigned, pgoff_t); /* page no longer needed */
1515
void (*invalidate_area)(unsigned); /* swap type just swapoff'ed */
16-
struct frontswap_ops *next; /* private pointer to next ops */
1716
};
1817

19-
extern void frontswap_register_ops(struct frontswap_ops *ops);
18+
int frontswap_register_ops(const struct frontswap_ops *ops);
2019

2120
extern void frontswap_init(unsigned type, unsigned long *map);
2221
extern int __frontswap_store(struct page *page);

mm/frontswap.c

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ DEFINE_STATIC_KEY_FALSE(frontswap_enabled_key);
2727
* may be registered, but implementations can never deregister. This
2828
* is a simple singly-linked list of all registered implementations.
2929
*/
30-
static struct frontswap_ops *frontswap_ops __read_mostly;
31-
32-
#define for_each_frontswap_ops(ops) \
33-
for ((ops) = frontswap_ops; (ops); (ops) = (ops)->next)
30+
static const struct frontswap_ops *frontswap_ops __read_mostly;
3431

3532
#ifdef CONFIG_DEBUG_FS
3633
/*
@@ -97,18 +94,14 @@ static inline void inc_frontswap_invalidates(void) { }
9794
/*
9895
* Register operations for frontswap
9996
*/
100-
void frontswap_register_ops(struct frontswap_ops *ops)
97+
int frontswap_register_ops(const struct frontswap_ops *ops)
10198
{
102-
/*
103-
* Setting frontswap_ops must happen after the ops->init() calls
104-
* above; cmpxchg implies smp_mb() which will ensure the init is
105-
* complete at this point.
106-
*/
107-
do {
108-
ops->next = frontswap_ops;
109-
} while (cmpxchg(&frontswap_ops, ops->next, ops) != ops->next);
99+
if (frontswap_ops)
100+
return -EINVAL;
110101

102+
frontswap_ops = ops;
111103
static_branch_inc(&frontswap_enabled_key);
104+
return 0;
112105
}
113106

114107
/*
@@ -117,7 +110,6 @@ void frontswap_register_ops(struct frontswap_ops *ops)
117110
void frontswap_init(unsigned type, unsigned long *map)
118111
{
119112
struct swap_info_struct *sis = swap_info[type];
120-
struct frontswap_ops *ops;
121113

122114
VM_BUG_ON(sis == NULL);
123115

@@ -133,9 +125,7 @@ void frontswap_init(unsigned type, unsigned long *map)
133125
* p->frontswap set to something valid to work properly.
134126
*/
135127
frontswap_map_set(sis, map);
136-
137-
for_each_frontswap_ops(ops)
138-
ops->init(type);
128+
frontswap_ops->init(type);
139129
}
140130

141131
static bool __frontswap_test(struct swap_info_struct *sis,
@@ -174,7 +164,6 @@ int __frontswap_store(struct page *page)
174164
int type = swp_type(entry);
175165
struct swap_info_struct *sis = swap_info[type];
176166
pgoff_t offset = swp_offset(entry);
177-
struct frontswap_ops *ops;
178167

179168
VM_BUG_ON(!frontswap_ops);
180169
VM_BUG_ON(!PageLocked(page));
@@ -188,16 +177,10 @@ int __frontswap_store(struct page *page)
188177
*/
189178
if (__frontswap_test(sis, offset)) {
190179
__frontswap_clear(sis, offset);
191-
for_each_frontswap_ops(ops)
192-
ops->invalidate_page(type, offset);
180+
frontswap_ops->invalidate_page(type, offset);
193181
}
194182

195-
/* Try to store in each implementation, until one succeeds. */
196-
for_each_frontswap_ops(ops) {
197-
ret = ops->store(type, offset, page);
198-
if (!ret) /* successful store */
199-
break;
200-
}
183+
ret = frontswap_ops->store(type, offset, page);
201184
if (ret == 0) {
202185
__frontswap_set(sis, offset);
203186
inc_frontswap_succ_stores();
@@ -220,7 +203,6 @@ int __frontswap_load(struct page *page)
220203
int type = swp_type(entry);
221204
struct swap_info_struct *sis = swap_info[type];
222205
pgoff_t offset = swp_offset(entry);
223-
struct frontswap_ops *ops;
224206

225207
VM_BUG_ON(!frontswap_ops);
226208
VM_BUG_ON(!PageLocked(page));
@@ -230,11 +212,7 @@ int __frontswap_load(struct page *page)
230212
return -1;
231213

232214
/* Try loading from each implementation, until one succeeds. */
233-
for_each_frontswap_ops(ops) {
234-
ret = ops->load(type, offset, page);
235-
if (!ret) /* successful load */
236-
break;
237-
}
215+
ret = frontswap_ops->load(type, offset, page);
238216
if (ret == 0)
239217
inc_frontswap_loads();
240218
return ret;
@@ -247,16 +225,14 @@ int __frontswap_load(struct page *page)
247225
void __frontswap_invalidate_page(unsigned type, pgoff_t offset)
248226
{
249227
struct swap_info_struct *sis = swap_info[type];
250-
struct frontswap_ops *ops;
251228

252229
VM_BUG_ON(!frontswap_ops);
253230
VM_BUG_ON(sis == NULL);
254231

255232
if (!__frontswap_test(sis, offset))
256233
return;
257234

258-
for_each_frontswap_ops(ops)
259-
ops->invalidate_page(type, offset);
235+
frontswap_ops->invalidate_page(type, offset);
260236
__frontswap_clear(sis, offset);
261237
inc_frontswap_invalidates();
262238
}
@@ -268,16 +244,14 @@ void __frontswap_invalidate_page(unsigned type, pgoff_t offset)
268244
void __frontswap_invalidate_area(unsigned type)
269245
{
270246
struct swap_info_struct *sis = swap_info[type];
271-
struct frontswap_ops *ops;
272247

273248
VM_BUG_ON(!frontswap_ops);
274249
VM_BUG_ON(sis == NULL);
275250

276251
if (sis->frontswap_map == NULL)
277252
return;
278253

279-
for_each_frontswap_ops(ops)
280-
ops->invalidate_area(type);
254+
frontswap_ops->invalidate_area(type);
281255
atomic_set(&sis->frontswap_pages, 0);
282256
bitmap_zero(sis->frontswap_map, sis->max);
283257
}

mm/zswap.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@ static void zswap_frontswap_init(unsigned type)
13781378
zswap_trees[type] = tree;
13791379
}
13801380

1381-
static struct frontswap_ops zswap_frontswap_ops = {
1381+
static const struct frontswap_ops zswap_frontswap_ops = {
13821382
.store = zswap_frontswap_store,
13831383
.load = zswap_frontswap_load,
13841384
.invalidate_page = zswap_frontswap_invalidate_page,
@@ -1475,11 +1475,15 @@ static int __init init_zswap(void)
14751475
if (!shrink_wq)
14761476
goto fallback_fail;
14771477

1478-
frontswap_register_ops(&zswap_frontswap_ops);
1478+
ret = frontswap_register_ops(&zswap_frontswap_ops);
1479+
if (ret)
1480+
goto destroy_wq;
14791481
if (zswap_debugfs_init())
14801482
pr_warn("debugfs initialization failed\n");
14811483
return 0;
14821484

1485+
destroy_wq:
1486+
destroy_workqueue(shrink_wq);
14831487
fallback_fail:
14841488
if (pool)
14851489
zswap_pool_destroy(pool);

0 commit comments

Comments
 (0)