swap.h 15 KB

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