xref: /linux/mm/swap.h (revision b8b9488d50b7150bd4830dfff487e8d4ef6589ba)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _MM_SWAP_H
3 #define _MM_SWAP_H
4 
5 struct mempolicy;
6 
7 #ifdef CONFIG_SWAP
8 #include <linux/swapops.h> /* for swp_offset */
9 #include <linux/blk_types.h> /* for bio_end_io_t */
10 
11 /* linux/mm/page_io.c */
12 int sio_pool_init(void);
13 struct swap_iocb;
14 void swap_read_folio(struct folio *folio, bool do_poll,
15 		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);
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 page *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 #else /* CONFIG_SWAP */
85 struct swap_iocb;
86 static inline void swap_read_folio(struct folio *folio, bool do_poll,
87 		struct swap_iocb **plug)
88 {
89 }
90 static inline void swap_write_unplug(struct swap_iocb *sio)
91 {
92 }
93 
94 static inline struct address_space *swap_address_space(swp_entry_t entry)
95 {
96 	return NULL;
97 }
98 
99 static inline pgoff_t swap_cache_index(swp_entry_t entry)
100 {
101 	return 0;
102 }
103 
104 static inline void show_swap_cache_info(void)
105 {
106 }
107 
108 static inline struct folio *swap_cluster_readahead(swp_entry_t entry,
109 			gfp_t gfp_mask, struct mempolicy *mpol, pgoff_t ilx)
110 {
111 	return NULL;
112 }
113 
114 static inline struct page *swapin_readahead(swp_entry_t swp, gfp_t gfp_mask,
115 			struct vm_fault *vmf)
116 {
117 	return NULL;
118 }
119 
120 static inline int swap_writepage(struct page *p, struct writeback_control *wbc)
121 {
122 	return 0;
123 }
124 
125 static inline void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry)
126 {
127 }
128 
129 static inline struct folio *swap_cache_get_folio(swp_entry_t entry,
130 		struct vm_area_struct *vma, unsigned long addr)
131 {
132 	return NULL;
133 }
134 
135 static inline
136 struct folio *filemap_get_incore_folio(struct address_space *mapping,
137 		pgoff_t index)
138 {
139 	return filemap_get_folio(mapping, index);
140 }
141 
142 static inline bool add_to_swap(struct folio *folio)
143 {
144 	return false;
145 }
146 
147 static inline void *get_shadow_from_swap_cache(swp_entry_t entry)
148 {
149 	return NULL;
150 }
151 
152 static inline int add_to_swap_cache(struct folio *folio, swp_entry_t entry,
153 					gfp_t gfp_mask, void **shadowp)
154 {
155 	return -1;
156 }
157 
158 static inline void __delete_from_swap_cache(struct folio *folio,
159 					swp_entry_t entry, void *shadow)
160 {
161 }
162 
163 static inline void delete_from_swap_cache(struct folio *folio)
164 {
165 }
166 
167 static inline void clear_shadow_from_swap_cache(int type, unsigned long begin,
168 				unsigned long end)
169 {
170 }
171 
172 static inline unsigned int folio_swap_flags(struct folio *folio)
173 {
174 	return 0;
175 }
176 #endif /* CONFIG_SWAP */
177 #endif /* _MM_SWAP_H */
178