1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_FRONTSWAP_H
3 #define _LINUX_FRONTSWAP_H
4 
5 #include <linux/swap.h>
6 #include <linux/mm.h>
7 #include <linux/bitops.h>
8 #include <linux/jump_label.h>
9 
10 struct frontswap_ops {
11 	void (*init)(unsigned); /* this swap type was just swapon'ed */
12 	int (*store)(unsigned, pgoff_t, struct page *); /* store a page */
13 	int (*load)(unsigned, pgoff_t, struct page *); /* load a page */
14 	void (*invalidate_page)(unsigned, pgoff_t); /* page no longer needed */
15 	void (*invalidate_area)(unsigned); /* swap type just swapoff'ed */
16 };
17 
18 int frontswap_register_ops(const struct frontswap_ops *ops);
19 
20 extern void frontswap_init(unsigned type, unsigned long *map);
21 extern int __frontswap_store(struct page *page);
22 extern int __frontswap_load(struct page *page);
23 extern void __frontswap_invalidate_page(unsigned, pgoff_t);
24 extern void __frontswap_invalidate_area(unsigned);
25 
26 #ifdef CONFIG_FRONTSWAP
27 extern struct static_key_false frontswap_enabled_key;
28 
frontswap_enabled(void)29 static inline bool frontswap_enabled(void)
30 {
31 	return static_branch_unlikely(&frontswap_enabled_key);
32 }
33 
frontswap_map_set(struct swap_info_struct * p,unsigned long * map)34 static inline void frontswap_map_set(struct swap_info_struct *p,
35 				     unsigned long *map)
36 {
37 	p->frontswap_map = map;
38 }
39 
frontswap_map_get(struct swap_info_struct * p)40 static inline unsigned long *frontswap_map_get(struct swap_info_struct *p)
41 {
42 	return p->frontswap_map;
43 }
44 #else
45 /* all inline routines become no-ops and all externs are ignored */
46 
frontswap_enabled(void)47 static inline bool frontswap_enabled(void)
48 {
49 	return false;
50 }
51 
frontswap_map_set(struct swap_info_struct * p,unsigned long * map)52 static inline void frontswap_map_set(struct swap_info_struct *p,
53 				     unsigned long *map)
54 {
55 }
56 
frontswap_map_get(struct swap_info_struct * p)57 static inline unsigned long *frontswap_map_get(struct swap_info_struct *p)
58 {
59 	return NULL;
60 }
61 #endif
62 
frontswap_store(struct page * page)63 static inline int frontswap_store(struct page *page)
64 {
65 	if (frontswap_enabled())
66 		return __frontswap_store(page);
67 
68 	return -1;
69 }
70 
frontswap_load(struct page * page)71 static inline int frontswap_load(struct page *page)
72 {
73 	if (frontswap_enabled())
74 		return __frontswap_load(page);
75 
76 	return -1;
77 }
78 
frontswap_invalidate_page(unsigned type,pgoff_t offset)79 static inline void frontswap_invalidate_page(unsigned type, pgoff_t offset)
80 {
81 	if (frontswap_enabled())
82 		__frontswap_invalidate_page(type, offset);
83 }
84 
frontswap_invalidate_area(unsigned type)85 static inline void frontswap_invalidate_area(unsigned type)
86 {
87 	if (frontswap_enabled())
88 		__frontswap_invalidate_area(type);
89 }
90 
91 #endif /* _LINUX_FRONTSWAP_H */
92