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