Skip to content

Commit

Permalink
frontswap: remove support for multiple ops
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Christoph Hellwig authored and torvalds committed Jan 22, 2022
1 parent 633423a commit 1da0d94
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 42 deletions.
3 changes: 1 addition & 2 deletions include/linux/frontswap.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ struct frontswap_ops {
int (*load)(unsigned, pgoff_t, struct page *); /* load a page */
void (*invalidate_page)(unsigned, pgoff_t); /* page no longer needed */
void (*invalidate_area)(unsigned); /* swap type just swapoff'ed */
struct frontswap_ops *next; /* private pointer to next ops */
};

extern void frontswap_register_ops(struct frontswap_ops *ops);
int frontswap_register_ops(const struct frontswap_ops *ops);

extern void frontswap_init(unsigned type, unsigned long *map);
extern int __frontswap_store(struct page *page);
Expand Down
50 changes: 12 additions & 38 deletions mm/frontswap.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ DEFINE_STATIC_KEY_FALSE(frontswap_enabled_key);
* may be registered, but implementations can never deregister. This
* is a simple singly-linked list of all registered implementations.
*/
static struct frontswap_ops *frontswap_ops __read_mostly;

#define for_each_frontswap_ops(ops) \
for ((ops) = frontswap_ops; (ops); (ops) = (ops)->next)
static const struct frontswap_ops *frontswap_ops __read_mostly;

#ifdef CONFIG_DEBUG_FS
/*
Expand Down Expand Up @@ -97,18 +94,14 @@ static inline void inc_frontswap_invalidates(void) { }
/*
* Register operations for frontswap
*/
void frontswap_register_ops(struct frontswap_ops *ops)
int frontswap_register_ops(const struct frontswap_ops *ops)
{
/*
* Setting frontswap_ops must happen after the ops->init() calls
* above; cmpxchg implies smp_mb() which will ensure the init is
* complete at this point.
*/
do {
ops->next = frontswap_ops;
} while (cmpxchg(&frontswap_ops, ops->next, ops) != ops->next);
if (frontswap_ops)
return -EINVAL;

frontswap_ops = ops;
static_branch_inc(&frontswap_enabled_key);
return 0;
}

/*
Expand All @@ -117,7 +110,6 @@ void frontswap_register_ops(struct frontswap_ops *ops)
void frontswap_init(unsigned type, unsigned long *map)
{
struct swap_info_struct *sis = swap_info[type];
struct frontswap_ops *ops;

VM_BUG_ON(sis == NULL);

Expand All @@ -133,9 +125,7 @@ void frontswap_init(unsigned type, unsigned long *map)
* p->frontswap set to something valid to work properly.
*/
frontswap_map_set(sis, map);

for_each_frontswap_ops(ops)
ops->init(type);
frontswap_ops->init(type);
}

static bool __frontswap_test(struct swap_info_struct *sis,
Expand Down Expand Up @@ -174,7 +164,6 @@ int __frontswap_store(struct page *page)
int type = swp_type(entry);
struct swap_info_struct *sis = swap_info[type];
pgoff_t offset = swp_offset(entry);
struct frontswap_ops *ops;

VM_BUG_ON(!frontswap_ops);
VM_BUG_ON(!PageLocked(page));
Expand All @@ -188,16 +177,10 @@ int __frontswap_store(struct page *page)
*/
if (__frontswap_test(sis, offset)) {
__frontswap_clear(sis, offset);
for_each_frontswap_ops(ops)
ops->invalidate_page(type, offset);
frontswap_ops->invalidate_page(type, offset);
}

/* Try to store in each implementation, until one succeeds. */
for_each_frontswap_ops(ops) {
ret = ops->store(type, offset, page);
if (!ret) /* successful store */
break;
}
ret = frontswap_ops->store(type, offset, page);
if (ret == 0) {
__frontswap_set(sis, offset);
inc_frontswap_succ_stores();
Expand All @@ -220,7 +203,6 @@ int __frontswap_load(struct page *page)
int type = swp_type(entry);
struct swap_info_struct *sis = swap_info[type];
pgoff_t offset = swp_offset(entry);
struct frontswap_ops *ops;

VM_BUG_ON(!frontswap_ops);
VM_BUG_ON(!PageLocked(page));
Expand All @@ -230,11 +212,7 @@ int __frontswap_load(struct page *page)
return -1;

/* Try loading from each implementation, until one succeeds. */
for_each_frontswap_ops(ops) {
ret = ops->load(type, offset, page);
if (!ret) /* successful load */
break;
}
ret = frontswap_ops->load(type, offset, page);
if (ret == 0)
inc_frontswap_loads();
return ret;
Expand All @@ -247,16 +225,14 @@ int __frontswap_load(struct page *page)
void __frontswap_invalidate_page(unsigned type, pgoff_t offset)
{
struct swap_info_struct *sis = swap_info[type];
struct frontswap_ops *ops;

VM_BUG_ON(!frontswap_ops);
VM_BUG_ON(sis == NULL);

if (!__frontswap_test(sis, offset))
return;

for_each_frontswap_ops(ops)
ops->invalidate_page(type, offset);
frontswap_ops->invalidate_page(type, offset);
__frontswap_clear(sis, offset);
inc_frontswap_invalidates();
}
Expand All @@ -268,16 +244,14 @@ void __frontswap_invalidate_page(unsigned type, pgoff_t offset)
void __frontswap_invalidate_area(unsigned type)
{
struct swap_info_struct *sis = swap_info[type];
struct frontswap_ops *ops;

VM_BUG_ON(!frontswap_ops);
VM_BUG_ON(sis == NULL);

if (sis->frontswap_map == NULL)
return;

for_each_frontswap_ops(ops)
ops->invalidate_area(type);
frontswap_ops->invalidate_area(type);
atomic_set(&sis->frontswap_pages, 0);
bitmap_zero(sis->frontswap_map, sis->max);
}
Expand Down
8 changes: 6 additions & 2 deletions mm/zswap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ static void zswap_frontswap_init(unsigned type)
zswap_trees[type] = tree;
}

static struct frontswap_ops zswap_frontswap_ops = {
static const struct frontswap_ops zswap_frontswap_ops = {
.store = zswap_frontswap_store,
.load = zswap_frontswap_load,
.invalidate_page = zswap_frontswap_invalidate_page,
Expand Down Expand Up @@ -1475,11 +1475,15 @@ static int __init init_zswap(void)
if (!shrink_wq)
goto fallback_fail;

frontswap_register_ops(&zswap_frontswap_ops);
ret = frontswap_register_ops(&zswap_frontswap_ops);
if (ret)
goto destroy_wq;
if (zswap_debugfs_init())
pr_warn("debugfs initialization failed\n");
return 0;

destroy_wq:
destroy_workqueue(shrink_wq);
fallback_fail:
if (pool)
zswap_pool_destroy(pool);
Expand Down

0 comments on commit 1da0d94

Please sign in to comment.