xref: /linux/mm/swap.h (revision 7b667acd69e316c2ed1b47e5dcd9d093be4a843f)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _MM_SWAP_H
3 #define _MM_SWAP_H
4 
5 struct mempolicy;
6 extern int page_cluster;
7 
8 #ifdef CONFIG_SWAP
9 #include <linux/swapops.h> /* for swp_offset */
10 #include <linux/blk_types.h> /* for bio_end_io_t */
11 
12 /* linux/mm/page_io.c */
13 int sio_pool_init(void);
14 struct swap_iocb;
15 void swap_read_folio(struct folio *folio, struct swap_iocb **plug);
16 void __swap_read_unplug(struct swap_iocb *plug);
17 static inline void swap_read_unplug(struct swap_iocb *plug)
18 {
19 	if (unlikely(plug))
20 		__swap_read_unplug(plug);
21 }
22 void swap_write_unplug(struct swap_iocb *sio);
23 int swap_writepage(struct page *page, struct writeback_control *wbc);
24 void __swap_writepage(struct folio *folio, struct writeback_control *wbc);
25 
26 /* linux/mm/swap_state.c */
27 /* One swap address space for each 64M swap space */
28 #define SWAP_ADDRESS_SPACE_SHIFT	14
29 #define SWAP_ADDRESS_SPACE_PAGES	(1 << SWAP_ADDRESS_SPACE_SHIFT)
30 #define SWAP_ADDRESS_SPACE_MASK		(SWAP_ADDRESS_SPACE_PAGES - 1)
31 extern struct address_space *swapper_spaces[];
32 #define swap_address_space(entry)			    \
33 	(&swapper_spaces[swp_type(entry)][swp_offset(entry) \
34 		>> SWAP_ADDRESS_SPACE_SHIFT])
35 
36 /*
37  * Return the swap device position of the swap entry.
38  */
39 static inline loff_t swap_dev_pos(swp_entry_t entry)
40 {
41 	return ((loff_t)swp_offset(entry)) << PAGE_SHIFT;
42 }
43 
44 /*
45  * Return the swap cache index of the swap entry.
46  */
47 static inline pgoff_t swap_cache_index(swp_entry_t entry)
48 {
49 	BUILD_BUG_ON((SWP_OFFSET_MASK | SWAP_ADDRESS_SPACE_MASK) != SWP_OFFSET_MASK);
50 	return swp_offset(entry) & SWAP_ADDRESS_SPACE_MASK;
51 }
52 
53 void show_swap_cache_info(void);
54 bool add_to_swap(struct folio *folio);
55 void *get_shadow_from_swap_cache(swp_entry_t entry);
56 int add_to_swap_cache(struct folio *folio, swp_entry_t entry,
57 		      gfp_t gfp, void **shadowp);
58 void __delete_from_swap_cache(struct folio *folio,
59 			      swp_entry_t entry, void *shadow);
60 void delete_from_swap_cache(struct folio *folio);
61 void clear_shadow_from_swap_cache(int type, unsigned long begin,
62 				  unsigned long end);
63 void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry, int nr);
64 struct folio *swap_cache_get_folio(swp_entry_t entry,
65 		struct vm_area_struct *vma, unsigned long addr);
66 struct folio *filemap_get_incore_folio(struct address_space *mapping,
67 		pgoff_t index);
68 
69 struct folio *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
70 		struct vm_area_struct *vma, unsigned long addr,
71 		struct swap_iocb **plug);
72 struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_flags,
73 		struct mempolicy *mpol, pgoff_t ilx, bool *new_page_allocated,
74 		bool skip_if_exists);
75 struct folio *swap_cluster_readahead(swp_entry_t entry, gfp_t flag,
76 		struct mempolicy *mpol, pgoff_t ilx);
77 struct folio *swapin_readahead(swp_entry_t entry, gfp_t flag,
78 		struct vm_fault *vmf);
79 
80 static inline unsigned int folio_swap_flags(struct folio *folio)
81 {
82 	return swp_swap_info(folio->swap)->flags;
83 }
84 
85 /*
86  * Return the count of contiguous swap entries that share the same
87  * zeromap status as the starting entry. If is_zeromap is not NULL,
88  * it will return the zeromap status of the starting entry.
89  */
90 static inline int swap_zeromap_batch(swp_entry_t entry, int max_nr,
91 		bool *is_zeromap)
92 {
93 	struct swap_info_struct *sis = swp_swap_info(entry);
94 	unsigned long start = swp_offset(entry);
95 	unsigned long end = start + max_nr;
96 	bool first_bit;
97 
98 	first_bit = test_bit(start, sis->zeromap);
99 	if (is_zeromap)
100 		*is_zeromap = first_bit;
101 
102 	if (max_nr <= 1)
103 		return max_nr;
104 	if (first_bit)
105 		return find_next_zero_bit(sis->zeromap, end, start) - start;
106 	else
107 		return find_next_bit(sis->zeromap, end, start) - start;
108 }
109 
110 #else /* CONFIG_SWAP */
111 struct swap_iocb;
112 static inline void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
113 {
114 }
115 static inline void swap_write_unplug(struct swap_iocb *sio)
116 {
117 }
118 
119 static inline struct address_space *swap_address_space(swp_entry_t entry)
120 {
121 	return NULL;
122 }
123 
124 static inline pgoff_t swap_cache_index(swp_entry_t entry)
125 {
126 	return 0;
127 }
128 
129 static inline void show_swap_cache_info(void)
130 {
131 }
132 
133 static inline struct folio *swap_cluster_readahead(swp_entry_t entry,
134 			gfp_t gfp_mask, struct mempolicy *mpol, pgoff_t ilx)
135 {
136 	return NULL;
137 }
138 
139 static inline struct folio *swapin_readahead(swp_entry_t swp, gfp_t gfp_mask,
140 			struct vm_fault *vmf)
141 {
142 	return NULL;
143 }
144 
145 static inline int swap_writepage(struct page *p, struct writeback_control *wbc)
146 {
147 	return 0;
148 }
149 
150 static inline void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry, int nr)
151 {
152 }
153 
154 static inline struct folio *swap_cache_get_folio(swp_entry_t entry,
155 		struct vm_area_struct *vma, unsigned long addr)
156 {
157 	return NULL;
158 }
159 
160 static inline
161 struct folio *filemap_get_incore_folio(struct address_space *mapping,
162 		pgoff_t index)
163 {
164 	return filemap_get_folio(mapping, index);
165 }
166 
167 static inline bool add_to_swap(struct folio *folio)
168 {
169 	return false;
170 }
171 
172 static inline void *get_shadow_from_swap_cache(swp_entry_t entry)
173 {
174 	return NULL;
175 }
176 
177 static inline int add_to_swap_cache(struct folio *folio, swp_entry_t entry,
178 					gfp_t gfp_mask, void **shadowp)
179 {
180 	return -1;
181 }
182 
183 static inline void __delete_from_swap_cache(struct folio *folio,
184 					swp_entry_t entry, void *shadow)
185 {
186 }
187 
188 static inline void delete_from_swap_cache(struct folio *folio)
189 {
190 }
191 
192 static inline void clear_shadow_from_swap_cache(int type, unsigned long begin,
193 				unsigned long end)
194 {
195 }
196 
197 static inline unsigned int folio_swap_flags(struct folio *folio)
198 {
199 	return 0;
200 }
201 
202 static inline int swap_zeromap_batch(swp_entry_t entry, int max_nr,
203 		bool *has_zeromap)
204 {
205 	return 0;
206 }
207 
208 #endif /* CONFIG_SWAP */
209 
210 #endif /* _MM_SWAP_H */
211