swap.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. #ifndef _LINUX_SWAP_H
  2. #define _LINUX_SWAP_H
  3. #include <linux/spinlock.h>
  4. #include <linux/linkage.h>
  5. #include <linux/mmzone.h>
  6. #include <linux/list.h>
  7. #include <linux/memcontrol.h>
  8. #include <linux/sched.h>
  9. #include <linux/node.h>
  10. #include <asm/atomic.h>
  11. #include <asm/page.h>
  12. struct notifier_block;
  13. struct bio;
  14. #define SWAP_FLAG_PREFER 0x8000 /* set if swap priority specified */
  15. #define SWAP_FLAG_PRIO_MASK 0x7fff
  16. #define SWAP_FLAG_PRIO_SHIFT 0
  17. static inline int current_is_kswapd(void)
  18. {
  19. return current->flags & PF_KSWAPD;
  20. }
  21. /*
  22. * MAX_SWAPFILES defines the maximum number of swaptypes: things which can
  23. * be swapped to. The swap type and the offset into that swap type are
  24. * encoded into pte's and into pgoff_t's in the swapcache. Using five bits
  25. * for the type means that the maximum number of swapcache pages is 27 bits
  26. * on 32-bit-pgoff_t architectures. And that assumes that the architecture packs
  27. * the type/offset into the pte as 5/27 as well.
  28. */
  29. #define MAX_SWAPFILES_SHIFT 5
  30. #ifndef CONFIG_MIGRATION
  31. #define MAX_SWAPFILES (1 << MAX_SWAPFILES_SHIFT)
  32. #else
  33. /* Use last two entries for page migration swap entries */
  34. #define MAX_SWAPFILES ((1 << MAX_SWAPFILES_SHIFT)-2)
  35. #define SWP_MIGRATION_READ MAX_SWAPFILES
  36. #define SWP_MIGRATION_WRITE (MAX_SWAPFILES + 1)
  37. #endif
  38. /*
  39. * Magic header for a swap area. The first part of the union is
  40. * what the swap magic looks like for the old (limited to 128MB)
  41. * swap area format, the second part of the union adds - in the
  42. * old reserved area - some extra information. Note that the first
  43. * kilobyte is reserved for boot loader or disk label stuff...
  44. *
  45. * Having the magic at the end of the PAGE_SIZE makes detecting swap
  46. * areas somewhat tricky on machines that support multiple page sizes.
  47. * For 2.5 we'll probably want to move the magic to just beyond the
  48. * bootbits...
  49. */
  50. union swap_header {
  51. struct {
  52. char reserved[PAGE_SIZE - 10];
  53. char magic[10]; /* SWAP-SPACE or SWAPSPACE2 */
  54. } magic;
  55. struct {
  56. char bootbits[1024]; /* Space for disklabel etc. */
  57. __u32 version;
  58. __u32 last_page;
  59. __u32 nr_badpages;
  60. unsigned char sws_uuid[16];
  61. unsigned char sws_volume[16];
  62. __u32 padding[117];
  63. __u32 badpages[1];
  64. } info;
  65. };
  66. /* A swap entry has to fit into a "unsigned long", as
  67. * the entry is hidden in the "index" field of the
  68. * swapper address space.
  69. */
  70. typedef struct {
  71. unsigned long val;
  72. } swp_entry_t;
  73. /*
  74. * current->reclaim_state points to one of these when a task is running
  75. * memory reclaim
  76. */
  77. struct reclaim_state {
  78. unsigned long reclaimed_slab;
  79. };
  80. #ifdef __KERNEL__
  81. struct address_space;
  82. struct sysinfo;
  83. struct writeback_control;
  84. struct zone;
  85. /*
  86. * A swap extent maps a range of a swapfile's PAGE_SIZE pages onto a range of
  87. * disk blocks. A list of swap extents maps the entire swapfile. (Where the
  88. * term `swapfile' refers to either a blockdevice or an IS_REG file. Apart
  89. * from setup, they're handled identically.
  90. *
  91. * We always assume that blocks are of size PAGE_SIZE.
  92. */
  93. struct swap_extent {
  94. struct list_head list;
  95. pgoff_t start_page;
  96. pgoff_t nr_pages;
  97. sector_t start_block;
  98. };
  99. /*
  100. * Max bad pages in the new format..
  101. */
  102. #define __swapoffset(x) ((unsigned long)&((union swap_header *)0)->x)
  103. #define MAX_SWAP_BADPAGES \
  104. ((__swapoffset(magic.magic) - __swapoffset(info.badpages)) / sizeof(int))
  105. enum {
  106. SWP_USED = (1 << 0), /* is slot in swap_info[] used? */
  107. SWP_WRITEOK = (1 << 1), /* ok to write to this swap? */
  108. SWP_DISCARDABLE = (1 << 2), /* blkdev supports discard */
  109. SWP_DISCARDING = (1 << 3), /* now discarding a free cluster */
  110. SWP_SOLIDSTATE = (1 << 4), /* blkdev seeks are cheap */
  111. /* add others here before... */
  112. SWP_SCANNING = (1 << 8), /* refcount in scan_swap_map */
  113. };
  114. #define SWAP_CLUSTER_MAX 32
  115. #define SWAP_MAP_MAX 0x7fff
  116. #define SWAP_MAP_BAD 0x8000
  117. /*
  118. * The in-memory structure used to track swap areas.
  119. */
  120. struct swap_info_struct {
  121. unsigned long flags;
  122. int prio; /* swap priority */
  123. int next; /* next entry on swap list */
  124. struct file *swap_file;
  125. struct block_device *bdev;
  126. struct list_head extent_list;
  127. struct swap_extent *curr_swap_extent;
  128. unsigned short *swap_map;
  129. unsigned int lowest_bit;
  130. unsigned int highest_bit;
  131. unsigned int lowest_alloc; /* while preparing discard cluster */
  132. unsigned int highest_alloc; /* while preparing discard cluster */
  133. unsigned int cluster_next;
  134. unsigned int cluster_nr;
  135. unsigned int pages;
  136. unsigned int max;
  137. unsigned int inuse_pages;
  138. unsigned int old_block_size;
  139. };
  140. struct swap_list_t {
  141. int head; /* head of priority-ordered swapfile list */
  142. int next; /* swapfile to be used next */
  143. };
  144. /* Swap 50% full? Release swapcache more aggressively.. */
  145. #define vm_swap_full() (nr_swap_pages*2 < total_swap_pages)
  146. /* linux/mm/page_alloc.c */
  147. extern unsigned long totalram_pages;
  148. extern unsigned long totalreserve_pages;
  149. extern unsigned int nr_free_buffer_pages(void);
  150. extern unsigned int nr_free_pagecache_pages(void);
  151. /* Definition of global_page_state not available yet */
  152. #define nr_free_pages() global_page_state(NR_FREE_PAGES)
  153. /* linux/mm/swap.c */
  154. extern void __lru_cache_add(struct page *, enum lru_list lru);
  155. extern void lru_cache_add_lru(struct page *, enum lru_list lru);
  156. extern void activate_page(struct page *);
  157. extern void mark_page_accessed(struct page *);
  158. extern void lru_add_drain(void);
  159. extern int lru_add_drain_all(void);
  160. extern void rotate_reclaimable_page(struct page *page);
  161. extern void swap_setup(void);
  162. extern void add_page_to_unevictable_list(struct page *page);
  163. /**
  164. * lru_cache_add: add a page to the page lists
  165. * @page: the page to add
  166. */
  167. static inline void lru_cache_add_anon(struct page *page)
  168. {
  169. __lru_cache_add(page, LRU_INACTIVE_ANON);
  170. }
  171. static inline void lru_cache_add_active_anon(struct page *page)
  172. {
  173. __lru_cache_add(page, LRU_ACTIVE_ANON);
  174. }
  175. static inline void lru_cache_add_file(struct page *page)
  176. {
  177. __lru_cache_add(page, LRU_INACTIVE_FILE);
  178. }
  179. static inline void lru_cache_add_active_file(struct page *page)
  180. {
  181. __lru_cache_add(page, LRU_ACTIVE_FILE);
  182. }
  183. /* linux/mm/vmscan.c */
  184. extern unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
  185. gfp_t gfp_mask, nodemask_t *mask);
  186. extern unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem,
  187. gfp_t gfp_mask, bool noswap,
  188. unsigned int swappiness);
  189. extern int __isolate_lru_page(struct page *page, int mode, int file);
  190. extern unsigned long shrink_all_memory(unsigned long nr_pages);
  191. extern int vm_swappiness;
  192. extern int remove_mapping(struct address_space *mapping, struct page *page);
  193. extern long vm_total_pages;
  194. #ifdef CONFIG_NUMA
  195. extern int zone_reclaim_mode;
  196. extern int sysctl_min_unmapped_ratio;
  197. extern int sysctl_min_slab_ratio;
  198. extern int zone_reclaim(struct zone *, gfp_t, unsigned int);
  199. #else
  200. #define zone_reclaim_mode 0
  201. static inline int zone_reclaim(struct zone *z, gfp_t mask, unsigned int order)
  202. {
  203. return 0;
  204. }
  205. #endif
  206. #ifdef CONFIG_UNEVICTABLE_LRU
  207. extern int page_evictable(struct page *page, struct vm_area_struct *vma);
  208. extern void scan_mapping_unevictable_pages(struct address_space *);
  209. extern unsigned long scan_unevictable_pages;
  210. extern int scan_unevictable_handler(struct ctl_table *, int, struct file *,
  211. void __user *, size_t *, loff_t *);
  212. extern int scan_unevictable_register_node(struct node *node);
  213. extern void scan_unevictable_unregister_node(struct node *node);
  214. #else
  215. static inline int page_evictable(struct page *page,
  216. struct vm_area_struct *vma)
  217. {
  218. return 1;
  219. }
  220. static inline void scan_mapping_unevictable_pages(struct address_space *mapping)
  221. {
  222. }
  223. static inline int scan_unevictable_register_node(struct node *node)
  224. {
  225. return 0;
  226. }
  227. static inline void scan_unevictable_unregister_node(struct node *node) { }
  228. #endif
  229. extern int kswapd_run(int nid);
  230. #ifdef CONFIG_MMU
  231. /* linux/mm/shmem.c */
  232. extern int shmem_unuse(swp_entry_t entry, struct page *page);
  233. #endif /* CONFIG_MMU */
  234. extern void swap_unplug_io_fn(struct backing_dev_info *, struct page *);
  235. #ifdef CONFIG_SWAP
  236. /* linux/mm/page_io.c */
  237. extern int swap_readpage(struct file *, struct page *);
  238. extern int swap_writepage(struct page *page, struct writeback_control *wbc);
  239. extern void end_swap_bio_read(struct bio *bio, int err);
  240. /* linux/mm/swap_state.c */
  241. extern struct address_space swapper_space;
  242. #define total_swapcache_pages swapper_space.nrpages
  243. extern void show_swap_cache_info(void);
  244. extern int add_to_swap(struct page *);
  245. extern int add_to_swap_cache(struct page *, swp_entry_t, gfp_t);
  246. extern void __delete_from_swap_cache(struct page *);
  247. extern void delete_from_swap_cache(struct page *);
  248. extern void free_page_and_swap_cache(struct page *);
  249. extern void free_pages_and_swap_cache(struct page **, int);
  250. extern struct page *lookup_swap_cache(swp_entry_t);
  251. extern struct page *read_swap_cache_async(swp_entry_t, gfp_t,
  252. struct vm_area_struct *vma, unsigned long addr);
  253. extern struct page *swapin_readahead(swp_entry_t, gfp_t,
  254. struct vm_area_struct *vma, unsigned long addr);
  255. /* linux/mm/swapfile.c */
  256. extern long nr_swap_pages;
  257. extern long total_swap_pages;
  258. extern void si_swapinfo(struct sysinfo *);
  259. extern swp_entry_t get_swap_page(void);
  260. extern swp_entry_t get_swap_page_of_type(int);
  261. extern int swap_duplicate(swp_entry_t);
  262. extern int valid_swaphandles(swp_entry_t, unsigned long *);
  263. extern void swap_free(swp_entry_t);
  264. extern int free_swap_and_cache(swp_entry_t);
  265. extern int swap_type_of(dev_t, sector_t, struct block_device **);
  266. extern unsigned int count_swap_pages(int, int);
  267. extern sector_t map_swap_page(struct swap_info_struct *, pgoff_t);
  268. extern sector_t swapdev_block(int, pgoff_t);
  269. extern struct swap_info_struct *get_swap_info_struct(unsigned);
  270. extern int reuse_swap_page(struct page *);
  271. extern int try_to_free_swap(struct page *);
  272. struct backing_dev_info;
  273. /* linux/mm/thrash.c */
  274. extern struct mm_struct * swap_token_mm;
  275. extern void grab_swap_token(void);
  276. extern void __put_swap_token(struct mm_struct *);
  277. static inline int has_swap_token(struct mm_struct *mm)
  278. {
  279. return (mm == swap_token_mm);
  280. }
  281. static inline void put_swap_token(struct mm_struct *mm)
  282. {
  283. if (has_swap_token(mm))
  284. __put_swap_token(mm);
  285. }
  286. static inline void disable_swap_token(void)
  287. {
  288. put_swap_token(swap_token_mm);
  289. }
  290. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  291. extern void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent);
  292. #else
  293. static inline void
  294. mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
  295. {
  296. }
  297. #endif
  298. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  299. extern void mem_cgroup_uncharge_swap(swp_entry_t ent);
  300. #else
  301. static inline void mem_cgroup_uncharge_swap(swp_entry_t ent)
  302. {
  303. }
  304. #endif
  305. #else /* CONFIG_SWAP */
  306. #define nr_swap_pages 0L
  307. #define total_swap_pages 0L
  308. #define total_swapcache_pages 0UL
  309. #define si_swapinfo(val) \
  310. do { (val)->freeswap = (val)->totalswap = 0; } while (0)
  311. /* only sparc can not include linux/pagemap.h in this file
  312. * so leave page_cache_release and release_pages undeclared... */
  313. #define free_page_and_swap_cache(page) \
  314. page_cache_release(page)
  315. #define free_pages_and_swap_cache(pages, nr) \
  316. release_pages((pages), (nr), 0);
  317. static inline void show_swap_cache_info(void)
  318. {
  319. }
  320. #define free_swap_and_cache(swp) is_migration_entry(swp)
  321. #define swap_duplicate(swp) is_migration_entry(swp)
  322. static inline void swap_free(swp_entry_t swp)
  323. {
  324. }
  325. static inline struct page *swapin_readahead(swp_entry_t swp, gfp_t gfp_mask,
  326. struct vm_area_struct *vma, unsigned long addr)
  327. {
  328. return NULL;
  329. }
  330. static inline int swap_writepage(struct page *p, struct writeback_control *wbc)
  331. {
  332. return 0;
  333. }
  334. static inline struct page *lookup_swap_cache(swp_entry_t swp)
  335. {
  336. return NULL;
  337. }
  338. static inline int add_to_swap(struct page *page)
  339. {
  340. return 0;
  341. }
  342. static inline int add_to_swap_cache(struct page *page, swp_entry_t entry,
  343. gfp_t gfp_mask)
  344. {
  345. return -1;
  346. }
  347. static inline void __delete_from_swap_cache(struct page *page)
  348. {
  349. }
  350. static inline void delete_from_swap_cache(struct page *page)
  351. {
  352. }
  353. #define reuse_swap_page(page) (page_mapcount(page) == 1)
  354. static inline int try_to_free_swap(struct page *page)
  355. {
  356. return 0;
  357. }
  358. static inline swp_entry_t get_swap_page(void)
  359. {
  360. swp_entry_t entry;
  361. entry.val = 0;
  362. return entry;
  363. }
  364. /* linux/mm/thrash.c */
  365. #define put_swap_token(x) do { } while(0)
  366. #define grab_swap_token() do { } while(0)
  367. #define has_swap_token(x) 0
  368. #define disable_swap_token() do { } while(0)
  369. static inline int mem_cgroup_cache_charge_swapin(struct page *page,
  370. struct mm_struct *mm, gfp_t mask, bool locked)
  371. {
  372. return 0;
  373. }
  374. static inline void
  375. mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
  376. {
  377. }
  378. #endif /* CONFIG_SWAP */
  379. #endif /* __KERNEL__*/
  380. #endif /* _LINUX_SWAP_H */