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 /*
11  * Return code to denote that requested number of
12  * frontswap pages are unused(moved to page cache).
13  * Used in shmem_unuse and try_to_unuse.
14  */
15 #define FRONTSWAP_PAGES_UNUSED	2
16 
17 struct frontswap_ops {
18 	void (*init)(unsigned); /* this swap type was just swapon'ed */
19 	int (*store)(unsigned, pgoff_t, struct page *); /* store a page */
20 	int (*load)(unsigned, pgoff_t, struct page *); /* load a page */
21 	void (*invalidate_page)(unsigned, pgoff_t); /* page no longer needed */
22 	void (*invalidate_area)(unsigned); /* swap type just swapoff'ed */
23 	struct frontswap_ops *next; /* private pointer to next ops */
24 };
25 
26 extern void frontswap_register_ops(struct frontswap_ops *ops);
27 extern void frontswap_shrink(unsigned long);
28 extern unsigned long frontswap_curr_pages(void);
29 extern void frontswap_writethrough(bool);
30 #define FRONTSWAP_HAS_EXCLUSIVE_GETS
31 extern void frontswap_tmem_exclusive_gets(bool);
32 
33 extern bool __frontswap_test(struct swap_info_struct *, pgoff_t);
34 extern void __frontswap_init(unsigned type, unsigned long *map);
35 extern int __frontswap_store(struct page *page);
36 extern int __frontswap_load(struct page *page);
37 extern void __frontswap_invalidate_page(unsigned, pgoff_t);
38 extern void __frontswap_invalidate_area(unsigned);
39 
40 #ifdef CONFIG_FRONTSWAP
41 extern struct static_key_false frontswap_enabled_key;
42 
frontswap_enabled(void)43 static inline bool frontswap_enabled(void)
44 {
45 	return static_branch_unlikely(&frontswap_enabled_key);
46 }
47 
frontswap_test(struct swap_info_struct * sis,pgoff_t offset)48 static inline bool frontswap_test(struct swap_info_struct *sis, pgoff_t offset)
49 {
50 	return __frontswap_test(sis, offset);
51 }
52 
frontswap_map_set(struct swap_info_struct * p,unsigned long * map)53 static inline void frontswap_map_set(struct swap_info_struct *p,
54 				     unsigned long *map)
55 {
56 	p->frontswap_map = map;
57 }
58 
frontswap_map_get(struct swap_info_struct * p)59 static inline unsigned long *frontswap_map_get(struct swap_info_struct *p)
60 {
61 	return p->frontswap_map;
62 }
63 #else
64 /* all inline routines become no-ops and all externs are ignored */
65 
frontswap_enabled(void)66 static inline bool frontswap_enabled(void)
67 {
68 	return false;
69 }
70 
frontswap_test(struct swap_info_struct * sis,pgoff_t offset)71 static inline bool frontswap_test(struct swap_info_struct *sis, pgoff_t offset)
72 {
73 	return false;
74 }
75 
frontswap_map_set(struct swap_info_struct * p,unsigned long * map)76 static inline void frontswap_map_set(struct swap_info_struct *p,
77 				     unsigned long *map)
78 {
79 }
80 
frontswap_map_get(struct swap_info_struct * p)81 static inline unsigned long *frontswap_map_get(struct swap_info_struct *p)
82 {
83 	return NULL;
84 }
85 #endif
86 
frontswap_store(struct page * page)87 static inline int frontswap_store(struct page *page)
88 {
89 	if (frontswap_enabled())
90 		return __frontswap_store(page);
91 
92 	return -1;
93 }
94 
frontswap_load(struct page * page)95 static inline int frontswap_load(struct page *page)
96 {
97 	if (frontswap_enabled())
98 		return __frontswap_load(page);
99 
100 	return -1;
101 }
102 
frontswap_invalidate_page(unsigned type,pgoff_t offset)103 static inline void frontswap_invalidate_page(unsigned type, pgoff_t offset)
104 {
105 	if (frontswap_enabled())
106 		__frontswap_invalidate_page(type, offset);
107 }
108 
frontswap_invalidate_area(unsigned type)109 static inline void frontswap_invalidate_area(unsigned type)
110 {
111 	if (frontswap_enabled())
112 		__frontswap_invalidate_area(type);
113 }
114 
frontswap_init(unsigned type,unsigned long * map)115 static inline void frontswap_init(unsigned type, unsigned long *map)
116 {
117 #ifdef CONFIG_FRONTSWAP
118 	__frontswap_init(type, map);
119 #endif
120 }
121 
122 #endif /* _LINUX_FRONTSWAP_H */
123