swap.h 14 KB

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