swap.h 16 KB

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