xref: /linux/mm/swap.h (revision eb0ece16027f8223d5dc9aaf90124f70577bd22a)
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 void *get_shadow_from_swap_cache(swp_entry_t entry);
55 int add_to_swap_cache(struct folio *folio, swp_entry_t entry,
56 		      gfp_t gfp, void **shadowp);
57 void __delete_from_swap_cache(struct folio *folio,
58 			      swp_entry_t entry, void *shadow);
59 void delete_from_swap_cache(struct folio *folio);
60 void clear_shadow_from_swap_cache(int type, unsigned long begin,
61 				  unsigned long end);
62 void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry, int nr);
63 struct folio *swap_cache_get_folio(swp_entry_t entry,
64 		struct vm_area_struct *vma, unsigned long addr);
65 struct folio *filemap_get_incore_folio(struct address_space *mapping,
66 		pgoff_t index);
67 
68 struct folio *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
69 		struct vm_area_struct *vma, unsigned long addr,
70 		struct swap_iocb **plug);
71 struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_flags,
72 		struct mempolicy *mpol, pgoff_t ilx, bool *new_page_allocated,
73 		bool skip_if_exists);
74 struct folio *swap_cluster_readahead(swp_entry_t entry, gfp_t flag,
75 		struct mempolicy *mpol, pgoff_t ilx);
76 struct folio *swapin_readahead(swp_entry_t entry, gfp_t flag,
77 		struct vm_fault *vmf);
78 
79 static inline unsigned int folio_swap_flags(struct folio *folio)
80 {
81 	return swp_swap_info(folio->swap)->flags;
82 }
83 
84 /*
85  * Return the count of contiguous swap entries that share the same
86  * zeromap status as the starting entry. If is_zeromap is not NULL,
87  * it will return the zeromap status of the starting entry.
88  */
89 static inline int swap_zeromap_batch(swp_entry_t entry, int max_nr,
90 		bool *is_zeromap)
91 {
92 	struct swap_info_struct *sis = swp_swap_info(entry);
93 	unsigned long start = swp_offset(entry);
94 	unsigned long end = start + max_nr;
95 	bool first_bit;
96 
97 	first_bit = test_bit(start, sis->zeromap);
98 	if (is_zeromap)
99 		*is_zeromap = first_bit;
100 
101 	if (max_nr <= 1)
102 		return max_nr;
103 	if (first_bit)
104 		return find_next_zero_bit(sis->zeromap, end, start) - start;
105 	else
106 		return find_next_bit(sis->zeromap, end, start) - start;
107 }
108 
109 #else /* CONFIG_SWAP */
110 struct swap_iocb;
111 static inline void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
112 {
113 }
114 static inline void swap_write_unplug(struct swap_iocb *sio)
115 {
116 }
117 
118 static inline struct address_space *swap_address_space(swp_entry_t entry)
119 {
120 	return NULL;
121 }
122 
123 static inline pgoff_t swap_cache_index(swp_entry_t entry)
124 {
125 	return 0;
126 }
127 
128 static inline void show_swap_cache_info(void)
129 {
130 }
131 
132 static inline struct folio *swap_cluster_readahead(swp_entry_t entry,
133 			gfp_t gfp_mask, struct mempolicy *mpol, pgoff_t ilx)
134 {
135 	return NULL;
136 }
137 
138 static inline struct folio *swapin_readahead(swp_entry_t swp, gfp_t gfp_mask,
139 			struct vm_fault *vmf)
140 {
141 	return NULL;
142 }
143 
144 static inline int swap_writepage(struct page *p, struct writeback_control *wbc)
145 {
146 	return 0;
147 }
148 
149 static inline void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry, int nr)
150 {
151 }
152 
153 static inline struct folio *swap_cache_get_folio(swp_entry_t entry,
154 		struct vm_area_struct *vma, unsigned long addr)
155 {
156 	return NULL;
157 }
158 
159 static inline
160 struct folio *filemap_get_incore_folio(struct address_space *mapping,
161 		pgoff_t index)
162 {
163 	return filemap_get_folio(mapping, index);
164 }
165 
166 static inline void *get_shadow_from_swap_cache(swp_entry_t entry)
167 {
168 	return NULL;
169 }
170 
171 static inline int add_to_swap_cache(struct folio *folio, swp_entry_t entry,
172 					gfp_t gfp_mask, void **shadowp)
173 {
174 	return -1;
175 }
176 
177 static inline void __delete_from_swap_cache(struct folio *folio,
178 					swp_entry_t entry, void *shadow)
179 {
180 }
181 
182 static inline void delete_from_swap_cache(struct folio *folio)
183 {
184 }
185 
186 static inline void clear_shadow_from_swap_cache(int type, unsigned long begin,
187 				unsigned long end)
188 {
189 }
190 
191 static inline unsigned int folio_swap_flags(struct folio *folio)
192 {
193 	return 0;
194 }
195 
196 static inline int swap_zeromap_batch(swp_entry_t entry, int max_nr,
197 		bool *has_zeromap)
198 {
199 	return 0;
200 }
201 
202 #endif /* CONFIG_SWAP */
203 
204 #endif /* _MM_SWAP_H */
205