swap.h 15 KB

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