swap.h 5.4 KB

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