1#ifndef _LINUX_SWAP_H
2#define _LINUX_SWAP_H
3
4#include <linux/spinlock.h>
5#include <linux/linkage.h>
6#include <linux/mmzone.h>
7#include <linux/list.h>
8#include <linux/memcontrol.h>
9#include <linux/sched.h>
10#include <linux/node.h>
11#include <linux/fs.h>
12#include <linux/atomic.h>
13#include <linux/page-flags.h>
14#include <asm/page.h>
15
16struct notifier_block;
17
18struct bio;
19
20#define SWAP_FLAG_PREFER 0x8000
21#define SWAP_FLAG_PRIO_MASK 0x7fff
22#define SWAP_FLAG_PRIO_SHIFT 0
23#define SWAP_FLAG_DISCARD 0x10000
24#define SWAP_FLAG_DISCARD_ONCE 0x20000
25#define SWAP_FLAG_DISCARD_PAGES 0x40000
26
27#define SWAP_FLAGS_VALID (SWAP_FLAG_PRIO_MASK | SWAP_FLAG_PREFER | \
28 SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | \
29 SWAP_FLAG_DISCARD_PAGES)
30
31static inline int current_is_kswapd(void)
32{
33 return current->flags & PF_KSWAPD;
34}
35
36
37
38
39
40
41
42
43
44#define MAX_SWAPFILES_SHIFT 5
45
46
47
48
49
50
51
52
53
54
55#ifdef CONFIG_MIGRATION
56#define SWP_MIGRATION_NUM 2
57#define SWP_MIGRATION_READ (MAX_SWAPFILES + SWP_HWPOISON_NUM)
58#define SWP_MIGRATION_WRITE (MAX_SWAPFILES + SWP_HWPOISON_NUM + 1)
59#else
60#define SWP_MIGRATION_NUM 0
61#endif
62
63
64
65
66#ifdef CONFIG_MEMORY_FAILURE
67#define SWP_HWPOISON_NUM 1
68#define SWP_HWPOISON MAX_SWAPFILES
69#else
70#define SWP_HWPOISON_NUM 0
71#endif
72
73#define MAX_SWAPFILES \
74 ((1 << MAX_SWAPFILES_SHIFT) - SWP_MIGRATION_NUM - SWP_HWPOISON_NUM)
75
76
77
78
79
80
81
82
83
84
85
86
87
88union swap_header {
89 struct {
90 char reserved[PAGE_SIZE - 10];
91 char magic[10];
92 } magic;
93 struct {
94 char bootbits[1024];
95 __u32 version;
96 __u32 last_page;
97 __u32 nr_badpages;
98 unsigned char sws_uuid[16];
99 unsigned char sws_volume[16];
100 __u32 padding[117];
101 __u32 badpages[1];
102 } info;
103};
104
105
106
107
108
109typedef struct {
110 unsigned long val;
111} swp_entry_t;
112
113
114
115
116
117struct reclaim_state {
118 unsigned long reclaimed_slab;
119};
120
121#ifdef __KERNEL__
122
123struct address_space;
124struct sysinfo;
125struct writeback_control;
126struct zone;
127
128
129
130
131
132
133
134
135
136struct swap_extent {
137 struct list_head list;
138 pgoff_t start_page;
139 pgoff_t nr_pages;
140 sector_t start_block;
141};
142
143
144
145
146#define __swapoffset(x) ((unsigned long)&((union swap_header *)0)->x)
147#define MAX_SWAP_BADPAGES \
148 ((__swapoffset(magic.magic) - __swapoffset(info.badpages)) / sizeof(int))
149
150enum {
151 SWP_USED = (1 << 0),
152 SWP_WRITEOK = (1 << 1),
153 SWP_DISCARDABLE = (1 << 2),
154 SWP_DISCARDING = (1 << 3),
155 SWP_SOLIDSTATE = (1 << 4),
156 SWP_CONTINUED = (1 << 5),
157 SWP_BLKDEV = (1 << 6),
158 SWP_FILE = (1 << 7),
159 SWP_AREA_DISCARD = (1 << 8),
160 SWP_PAGE_DISCARD = (1 << 9),
161
162 SWP_SCANNING = (1 << 10),
163};
164
165#define SWAP_CLUSTER_MAX 32UL
166#define COMPACT_CLUSTER_MAX SWAP_CLUSTER_MAX
167
168
169
170
171
172
173
174
175#define KSWAPD_ZONE_BALANCE_GAP_RATIO 100
176
177#define SWAP_MAP_MAX 0x3e
178#define SWAP_MAP_BAD 0x3f
179#define SWAP_HAS_CACHE 0x40
180#define SWAP_CONT_MAX 0x7f
181#define COUNT_CONTINUED 0x80
182#define SWAP_MAP_SHMEM 0xbf
183
184
185
186
187
188
189
190
191
192
193
194struct swap_cluster_info {
195 unsigned int data:24;
196 unsigned int flags:8;
197};
198#define CLUSTER_FLAG_FREE 1
199#define CLUSTER_FLAG_NEXT_NULL 2
200
201
202
203
204
205
206struct percpu_cluster {
207 struct swap_cluster_info index;
208 unsigned int next;
209};
210
211
212
213
214struct swap_info_struct {
215 unsigned long flags;
216 signed short prio;
217 signed char type;
218 signed char next;
219 unsigned int max;
220 unsigned char *swap_map;
221 struct swap_cluster_info *cluster_info;
222 struct swap_cluster_info free_cluster_head;
223 struct swap_cluster_info free_cluster_tail;
224 unsigned int lowest_bit;
225 unsigned int highest_bit;
226 unsigned int pages;
227 unsigned int inuse_pages;
228 unsigned int cluster_next;
229 unsigned int cluster_nr;
230 struct percpu_cluster __percpu *percpu_cluster;
231 struct swap_extent *curr_swap_extent;
232 struct swap_extent first_swap_extent;
233 struct block_device *bdev;
234 struct file *swap_file;
235 unsigned int old_block_size;
236#ifdef CONFIG_FRONTSWAP
237 unsigned long *frontswap_map;
238 atomic_t frontswap_pages;
239#endif
240 spinlock_t lock;
241
242
243
244
245
246
247
248
249
250
251
252
253 struct work_struct discard_work;
254 struct swap_cluster_info discard_cluster_head;
255 struct swap_cluster_info discard_cluster_tail;
256};
257
258struct swap_list_t {
259 int head;
260 int next;
261};
262
263
264extern unsigned long totalram_pages;
265extern unsigned long totalreserve_pages;
266extern unsigned long dirty_balance_reserve;
267extern unsigned long nr_free_buffer_pages(void);
268extern unsigned long nr_free_pagecache_pages(void);
269
270
271#define nr_free_pages() global_page_state(NR_FREE_PAGES)
272
273
274
275extern void __lru_cache_add(struct page *);
276extern void lru_cache_add(struct page *);
277extern void lru_add_page_tail(struct page *page, struct page *page_tail,
278 struct lruvec *lruvec, struct list_head *head);
279extern void activate_page(struct page *);
280extern void mark_page_accessed(struct page *);
281extern void lru_add_drain(void);
282extern void lru_add_drain_cpu(int cpu);
283extern void lru_add_drain_all(void);
284extern void rotate_reclaimable_page(struct page *page);
285extern void deactivate_page(struct page *page);
286extern void swap_setup(void);
287
288extern void add_page_to_unevictable_list(struct page *page);
289
290
291
292
293
294static inline void lru_cache_add_anon(struct page *page)
295{
296 ClearPageActive(page);
297 __lru_cache_add(page);
298}
299
300static inline void lru_cache_add_file(struct page *page)
301{
302 ClearPageActive(page);
303 __lru_cache_add(page);
304}
305
306
307extern unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
308 gfp_t gfp_mask, nodemask_t *mask);
309extern int __isolate_lru_page(struct page *page, isolate_mode_t mode);
310extern unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem,
311 gfp_t gfp_mask, bool noswap);
312extern unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *mem,
313 gfp_t gfp_mask, bool noswap,
314 struct zone *zone,
315 unsigned long *nr_scanned);
316extern unsigned long shrink_all_memory(unsigned long nr_pages);
317extern int vm_swappiness;
318extern int remove_mapping(struct address_space *mapping, struct page *page);
319extern unsigned long vm_total_pages;
320
321#ifdef CONFIG_NUMA
322extern int zone_reclaim_mode;
323extern int sysctl_min_unmapped_ratio;
324extern int sysctl_min_slab_ratio;
325extern int zone_reclaim(struct zone *, gfp_t, unsigned int);
326#else
327#define zone_reclaim_mode 0
328static inline int zone_reclaim(struct zone *z, gfp_t mask, unsigned int order)
329{
330 return 0;
331}
332#endif
333
334extern int page_evictable(struct page *page);
335extern void check_move_unevictable_pages(struct page **, int nr_pages);
336
337extern unsigned long scan_unevictable_pages;
338extern int scan_unevictable_handler(struct ctl_table *, int,
339 void __user *, size_t *, loff_t *);
340#ifdef CONFIG_NUMA
341extern int scan_unevictable_register_node(struct node *node);
342extern void scan_unevictable_unregister_node(struct node *node);
343#else
344static inline int scan_unevictable_register_node(struct node *node)
345{
346 return 0;
347}
348static inline void scan_unevictable_unregister_node(struct node *node)
349{
350}
351#endif
352
353extern int kswapd_run(int nid);
354extern void kswapd_stop(int nid);
355#ifdef CONFIG_MEMCG
356extern int mem_cgroup_swappiness(struct mem_cgroup *mem);
357#else
358static inline int mem_cgroup_swappiness(struct mem_cgroup *mem)
359{
360 return vm_swappiness;
361}
362#endif
363#ifdef CONFIG_MEMCG_SWAP
364extern void mem_cgroup_uncharge_swap(swp_entry_t ent);
365#else
366static inline void mem_cgroup_uncharge_swap(swp_entry_t ent)
367{
368}
369#endif
370#ifdef CONFIG_SWAP
371
372extern int swap_readpage(struct page *);
373extern int swap_writepage(struct page *page, struct writeback_control *wbc);
374extern void end_swap_bio_write(struct bio *bio, int err);
375extern int __swap_writepage(struct page *page, struct writeback_control *wbc,
376 void (*end_write_func)(struct bio *, int));
377extern int swap_set_page_dirty(struct page *page);
378extern void end_swap_bio_read(struct bio *bio, int err);
379
380int add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
381 unsigned long nr_pages, sector_t start_block);
382int generic_swapfile_activate(struct swap_info_struct *, struct file *,
383 sector_t *);
384
385
386extern struct address_space swapper_spaces[];
387#define swap_address_space(entry) (&swapper_spaces[swp_type(entry)])
388extern unsigned long total_swapcache_pages(void);
389extern void show_swap_cache_info(void);
390extern int add_to_swap(struct page *, struct list_head *list);
391extern int add_to_swap_cache(struct page *, swp_entry_t, gfp_t);
392extern int __add_to_swap_cache(struct page *page, swp_entry_t entry);
393extern void __delete_from_swap_cache(struct page *);
394extern void delete_from_swap_cache(struct page *);
395extern void free_page_and_swap_cache(struct page *);
396extern void free_pages_and_swap_cache(struct page **, int);
397extern struct page *lookup_swap_cache(swp_entry_t);
398extern struct page *read_swap_cache_async(swp_entry_t, gfp_t,
399 struct vm_area_struct *vma, unsigned long addr);
400extern struct page *swapin_readahead(swp_entry_t, gfp_t,
401 struct vm_area_struct *vma, unsigned long addr);
402
403
404extern atomic_long_t nr_swap_pages;
405extern long total_swap_pages;
406
407
408static inline bool vm_swap_full(void)
409{
410 return atomic_long_read(&nr_swap_pages) * 2 < total_swap_pages;
411}
412
413static inline long get_nr_swap_pages(void)
414{
415 return atomic_long_read(&nr_swap_pages);
416}
417
418extern void si_swapinfo(struct sysinfo *);
419extern swp_entry_t get_swap_page(void);
420extern swp_entry_t get_swap_page_of_type(int);
421extern int add_swap_count_continuation(swp_entry_t, gfp_t);
422extern void swap_shmem_alloc(swp_entry_t);
423extern int swap_duplicate(swp_entry_t);
424extern int swapcache_prepare(swp_entry_t);
425extern void swap_free(swp_entry_t);
426extern void swapcache_free(swp_entry_t, struct page *page);
427extern int free_swap_and_cache(swp_entry_t);
428extern int swap_type_of(dev_t, sector_t, struct block_device **);
429extern unsigned int count_swap_pages(int, int);
430extern sector_t map_swap_page(struct page *, struct block_device **);
431extern sector_t swapdev_block(int, pgoff_t);
432extern int page_swapcount(struct page *);
433extern struct swap_info_struct *page_swap_info(struct page *);
434extern int reuse_swap_page(struct page *);
435extern int try_to_free_swap(struct page *);
436struct backing_dev_info;
437
438#ifdef CONFIG_MEMCG
439extern void
440mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout);
441#else
442static inline void
443mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
444{
445}
446#endif
447
448#else
449
450#define swap_address_space(entry) (NULL)
451#define get_nr_swap_pages() 0L
452#define total_swap_pages 0L
453#define total_swapcache_pages() 0UL
454#define vm_swap_full() 0
455
456#define si_swapinfo(val) \
457 do { (val)->freeswap = (val)->totalswap = 0; } while (0)
458
459
460#define free_page_and_swap_cache(page) \
461 page_cache_release(page)
462#define free_pages_and_swap_cache(pages, nr) \
463 release_pages((pages), (nr), 0);
464
465static inline void show_swap_cache_info(void)
466{
467}
468
469#define free_swap_and_cache(swp) is_migration_entry(swp)
470#define swapcache_prepare(swp) is_migration_entry(swp)
471
472static inline int add_swap_count_continuation(swp_entry_t swp, gfp_t gfp_mask)
473{
474 return 0;
475}
476
477static inline void swap_shmem_alloc(swp_entry_t swp)
478{
479}
480
481static inline int swap_duplicate(swp_entry_t swp)
482{
483 return 0;
484}
485
486static inline void swap_free(swp_entry_t swp)
487{
488}
489
490static inline void swapcache_free(swp_entry_t swp, struct page *page)
491{
492}
493
494static inline struct page *swapin_readahead(swp_entry_t swp, gfp_t gfp_mask,
495 struct vm_area_struct *vma, unsigned long addr)
496{
497 return NULL;
498}
499
500static inline int swap_writepage(struct page *p, struct writeback_control *wbc)
501{
502 return 0;
503}
504
505static inline struct page *lookup_swap_cache(swp_entry_t swp)
506{
507 return NULL;
508}
509
510static inline int add_to_swap(struct page *page, struct list_head *list)
511{
512 return 0;
513}
514
515static inline int add_to_swap_cache(struct page *page, swp_entry_t entry,
516 gfp_t gfp_mask)
517{
518 return -1;
519}
520
521static inline void __delete_from_swap_cache(struct page *page)
522{
523}
524
525static inline void delete_from_swap_cache(struct page *page)
526{
527}
528
529static inline int page_swapcount(struct page *page)
530{
531 return 0;
532}
533
534#define reuse_swap_page(page) (page_mapcount(page) == 1)
535
536static inline int try_to_free_swap(struct page *page)
537{
538 return 0;
539}
540
541static inline swp_entry_t get_swap_page(void)
542{
543 swp_entry_t entry;
544 entry.val = 0;
545 return entry;
546}
547
548static inline void
549mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
550{
551}
552
553#endif
554#endif
555#endif
556