4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
8 * This file contains the default values for the operation of the
9 * Linux VM subsystem. Fine-tuning documentation can be found in
10 * Documentation/sysctl/vm.txt.
12 * Swap aging added 23.2.95, Stephen Tweedie.
13 * Buffermem limits added 12.3.98, Rik van Riel.
17 #include <linux/sched.h>
18 #include <linux/kernel_stat.h>
19 #include <linux/swap.h>
20 #include <linux/mman.h>
21 #include <linux/pagemap.h>
22 #include <linux/pagevec.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/mm_inline.h>
26 #include <linux/buffer_head.h> /* for try_to_release_page() */
27 #include <linux/percpu_counter.h>
28 #include <linux/percpu.h>
29 #include <linux/cpu.h>
30 #include <linux/notifier.h>
31 #include <linux/backing-dev.h>
32 #include <linux/memcontrol.h>
33 #include <linux/interrupt.h>
37 /* How many pages do we try to swap or page in/out together? */
41 * On PREEMPT_RT we don't want to disable preemption for cpu variables.
42 * We grab a cpu and then use that cpu to lock the variables accordingly.
44 * (On !PREEMPT_RT this turns into normal preempt-off sections, as before.)
46 static DEFINE_PER_CPU_LOCKED(struct pagevec
[NR_LRU_LISTS
], lru_add_pvecs
);
47 static DEFINE_PER_CPU_LOCKED(struct pagevec
, lru_rotate_pvecs
);
49 #define swap_get_cpu_var_irq_save(var, flags, cpu) \
52 &get_cpu_var_locked(var, &cpu); \
55 #define swap_put_cpu_var_irq_restore(var, flags, cpu) \
56 put_cpu_var_locked(var, cpu)
58 #define swap_get_cpu_var(var, cpu) \
59 &get_cpu_var_locked(var, &cpu)
61 #define swap_put_cpu_var(var, cpu) \
62 put_cpu_var_locked(var, cpu)
64 #define swap_per_cpu_lock(var, cpu) \
66 spin_lock(&__get_cpu_lock(var, cpu)); \
67 &__get_cpu_var_locked(var, cpu); \
70 #define swap_per_cpu_unlock(var, cpu) \
71 spin_unlock(&__get_cpu_lock(var, cpu));
73 #define swap_get_cpu() raw_smp_processor_id()
75 #define swap_put_cpu()
78 * This path almost never happens for VM activity - pages are normally
79 * freed via pagevecs. But it gets used by networking.
81 static void __page_cache_release(struct page
*page
)
85 struct zone
*zone
= page_zone(page
);
87 spin_lock_irqsave(&zone
->lru_lock
, flags
);
88 VM_BUG_ON(!PageLRU(page
));
90 del_page_from_lru(zone
, page
);
91 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
96 static void put_compound_page(struct page
*page
)
98 page
= compound_head(page
);
99 if (put_page_testzero(page
)) {
100 compound_page_dtor
*dtor
;
102 dtor
= get_compound_page_dtor(page
);
107 void put_page(struct page
*page
)
109 if (unlikely(PageCompound(page
)))
110 put_compound_page(page
);
111 else if (put_page_testzero(page
))
112 __page_cache_release(page
);
114 EXPORT_SYMBOL(put_page
);
117 * put_pages_list() - release a list of pages
118 * @pages: list of pages threaded on page->lru
120 * Release a list of pages which are strung together on page.lru. Currently
121 * used by read_cache_pages() and related error recovery code.
123 void put_pages_list(struct list_head
*pages
)
125 while (!list_empty(pages
)) {
128 victim
= list_entry(pages
->prev
, struct page
, lru
);
129 list_del(&victim
->lru
);
130 page_cache_release(victim
);
133 EXPORT_SYMBOL(put_pages_list
);
136 * pagevec_move_tail() must be called with IRQ disabled.
137 * Otherwise this may cause nasty races.
139 static void pagevec_move_tail(struct pagevec
*pvec
)
143 struct zone
*zone
= NULL
;
145 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
146 struct page
*page
= pvec
->pages
[i
];
147 struct zone
*pagezone
= page_zone(page
);
149 if (pagezone
!= zone
) {
151 spin_unlock(&zone
->lru_lock
);
153 spin_lock(&zone
->lru_lock
);
155 if (PageLRU(page
) && !PageActive(page
) && !PageUnevictable(page
)) {
156 int lru
= page_is_file_cache(page
);
157 list_move_tail(&page
->lru
, &zone
->lru
[lru
].list
);
162 spin_unlock(&zone
->lru_lock
);
163 __count_vm_events(PGROTATED
, pgmoved
);
164 release_pages(pvec
->pages
, pvec
->nr
, pvec
->cold
);
165 pagevec_reinit(pvec
);
169 * Writeback is about to end against a page which has been marked for immediate
170 * reclaim. If it still appears to be reclaimable, move it to the tail of the
173 void rotate_reclaimable_page(struct page
*page
)
175 if (!PageLocked(page
) && !PageDirty(page
) && !PageActive(page
) &&
176 !PageUnevictable(page
) && PageLRU(page
)) {
177 struct pagevec
*pvec
;
181 page_cache_get(page
);
182 pvec
= swap_get_cpu_var_irq_save(lru_rotate_pvecs
, flags
, cpu
);
183 if (!pagevec_add(pvec
, page
))
184 pagevec_move_tail(pvec
);
185 swap_put_cpu_var_irq_restore(lru_rotate_pvecs
, flags
, cpu
);
189 static void update_page_reclaim_stat(struct zone
*zone
, struct page
*page
,
190 int file
, int rotated
)
192 struct zone_reclaim_stat
*reclaim_stat
= &zone
->reclaim_stat
;
193 struct zone_reclaim_stat
*memcg_reclaim_stat
;
195 memcg_reclaim_stat
= mem_cgroup_get_reclaim_stat_from_page(page
);
197 reclaim_stat
->recent_scanned
[file
]++;
199 reclaim_stat
->recent_rotated
[file
]++;
201 if (!memcg_reclaim_stat
)
204 memcg_reclaim_stat
->recent_scanned
[file
]++;
206 memcg_reclaim_stat
->recent_rotated
[file
]++;
210 * FIXME: speed this up?
212 void activate_page(struct page
*page
)
214 struct zone
*zone
= page_zone(page
);
216 spin_lock_irq(&zone
->lru_lock
);
217 if (PageLRU(page
) && !PageActive(page
) && !PageUnevictable(page
)) {
218 int file
= page_is_file_cache(page
);
219 int lru
= LRU_BASE
+ file
;
220 del_page_from_lru_list(zone
, page
, lru
);
224 add_page_to_lru_list(zone
, page
, lru
);
225 __count_vm_event(PGACTIVATE
);
227 update_page_reclaim_stat(zone
, page
, !!file
, 1);
229 spin_unlock_irq(&zone
->lru_lock
);
233 * Mark a page as having seen activity.
235 * inactive,unreferenced -> inactive,referenced
236 * inactive,referenced -> active,unreferenced
237 * active,unreferenced -> active,referenced
239 void mark_page_accessed(struct page
*page
)
241 if (!PageActive(page
) && !PageUnevictable(page
) &&
242 PageReferenced(page
) && PageLRU(page
)) {
244 ClearPageReferenced(page
);
245 } else if (!PageReferenced(page
)) {
246 SetPageReferenced(page
);
250 EXPORT_SYMBOL(mark_page_accessed
);
252 void __lru_cache_add(struct page
*page
, enum lru_list lru
)
254 struct pagevec
*pvec
;
257 pvec
= swap_get_cpu_var(lru_add_pvecs
, cpu
)[lru
];
258 page_cache_get(page
);
259 if (!pagevec_add(pvec
, page
))
260 ____pagevec_lru_add(pvec
, lru
);
261 swap_put_cpu_var(lru_add_pvecs
, cpu
);
265 * lru_cache_add_lru - add a page to a page list
266 * @page: the page to be added to the LRU.
267 * @lru: the LRU list to which the page is added.
269 void lru_cache_add_lru(struct page
*page
, enum lru_list lru
)
271 if (PageActive(page
)) {
272 VM_BUG_ON(PageUnevictable(page
));
273 ClearPageActive(page
);
274 } else if (PageUnevictable(page
)) {
275 VM_BUG_ON(PageActive(page
));
276 ClearPageUnevictable(page
);
279 VM_BUG_ON(PageLRU(page
) || PageActive(page
) || PageUnevictable(page
));
280 __lru_cache_add(page
, lru
);
284 * add_page_to_unevictable_list - add a page to the unevictable list
285 * @page: the page to be added to the unevictable list
287 * Add page directly to its zone's unevictable list. To avoid races with
288 * tasks that might be making the page evictable, through eg. munlock,
289 * munmap or exit, while it's not on the lru, we want to add the page
290 * while it's locked or otherwise "invisible" to other tasks. This is
291 * difficult to do when using the pagevec cache, so bypass that.
293 void add_page_to_unevictable_list(struct page
*page
)
295 struct zone
*zone
= page_zone(page
);
297 spin_lock_irq(&zone
->lru_lock
);
298 SetPageUnevictable(page
);
300 add_page_to_lru_list(zone
, page
, LRU_UNEVICTABLE
);
301 spin_unlock_irq(&zone
->lru_lock
);
305 * Drain pages out of the cpu's pagevecs.
306 * Either "cpu" is the current CPU, and preemption has already been
307 * disabled; or "cpu" is being hot-unplugged, and is already dead.
309 static void drain_cpu_pagevecs(int cpu
)
311 struct pagevec
*pvecs
, *pvec
;
314 pvecs
= swap_per_cpu_lock(lru_add_pvecs
, cpu
)[0];
316 pvec
= &pvecs
[lru
- LRU_BASE
];
317 if (pagevec_count(pvec
))
318 ____pagevec_lru_add(pvec
, lru
);
320 swap_per_cpu_unlock(lru_add_pvecs
, cpu
);
322 pvec
= swap_per_cpu_lock(lru_rotate_pvecs
, cpu
);
323 if (pagevec_count(pvec
)) {
326 /* No harm done if a racing interrupt already did this */
327 local_irq_save_nort(flags
);
328 pagevec_move_tail(pvec
);
329 local_irq_restore_nort(flags
);
331 swap_per_cpu_unlock(lru_rotate_pvecs
, cpu
);
334 void lru_add_drain(void)
338 cpu
= swap_get_cpu();
339 drain_cpu_pagevecs(cpu
);
343 static void lru_add_drain_per_cpu(struct work_struct
*dummy
)
349 * Returns 0 for success
351 int lru_add_drain_all(void)
353 return schedule_on_each_cpu(lru_add_drain_per_cpu
);
357 * Batched page_cache_release(). Decrement the reference count on all the
358 * passed pages. If it fell to zero then remove the page from the LRU and
361 * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
362 * for the remainder of the operation.
364 * The locking in this function is against shrink_inactive_list(): we recheck
365 * the page count inside the lock to see whether shrink_inactive_list()
366 * grabbed the page via the LRU. If it did, give up: shrink_inactive_list()
369 void release_pages(struct page
**pages
, int nr
, int cold
)
372 struct pagevec pages_to_free
;
373 struct zone
*zone
= NULL
;
374 unsigned long uninitialized_var(flags
);
376 pagevec_init(&pages_to_free
, cold
);
377 for (i
= 0; i
< nr
; i
++) {
378 struct page
*page
= pages
[i
];
380 if (unlikely(PageCompound(page
))) {
382 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
385 put_compound_page(page
);
389 if (!put_page_testzero(page
))
393 struct zone
*pagezone
= page_zone(page
);
395 if (pagezone
!= zone
) {
397 spin_unlock_irqrestore(&zone
->lru_lock
,
400 spin_lock_irqsave(&zone
->lru_lock
, flags
);
402 VM_BUG_ON(!PageLRU(page
));
403 __ClearPageLRU(page
);
404 del_page_from_lru(zone
, page
);
407 if (!pagevec_add(&pages_to_free
, page
)) {
409 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
412 __pagevec_free(&pages_to_free
);
413 pagevec_reinit(&pages_to_free
);
417 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
419 pagevec_free(&pages_to_free
);
423 * The pages which we're about to release may be in the deferred lru-addition
424 * queues. That would prevent them from really being freed right now. That's
425 * OK from a correctness point of view but is inefficient - those pages may be
426 * cache-warm and we want to give them back to the page allocator ASAP.
428 * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
429 * and __pagevec_lru_add_active() call release_pages() directly to avoid
432 void __pagevec_release(struct pagevec
*pvec
)
435 release_pages(pvec
->pages
, pagevec_count(pvec
), pvec
->cold
);
436 pagevec_reinit(pvec
);
439 EXPORT_SYMBOL(__pagevec_release
);
442 * Add the passed pages to the LRU, then drop the caller's refcount
443 * on them. Reinitialises the caller's pagevec.
445 void ____pagevec_lru_add(struct pagevec
*pvec
, enum lru_list lru
)
448 struct zone
*zone
= NULL
;
450 VM_BUG_ON(is_unevictable_lru(lru
));
452 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
453 struct page
*page
= pvec
->pages
[i
];
454 struct zone
*pagezone
= page_zone(page
);
458 if (pagezone
!= zone
) {
460 spin_unlock_irq(&zone
->lru_lock
);
462 spin_lock_irq(&zone
->lru_lock
);
464 VM_BUG_ON(PageActive(page
));
465 VM_BUG_ON(PageUnevictable(page
));
466 VM_BUG_ON(PageLRU(page
));
468 active
= is_active_lru(lru
);
469 file
= is_file_lru(lru
);
472 update_page_reclaim_stat(zone
, page
, file
, active
);
473 add_page_to_lru_list(zone
, page
, lru
);
476 spin_unlock_irq(&zone
->lru_lock
);
477 release_pages(pvec
->pages
, pvec
->nr
, pvec
->cold
);
478 pagevec_reinit(pvec
);
481 EXPORT_SYMBOL(____pagevec_lru_add
);
484 * Try to drop buffers from the pages in a pagevec
486 void pagevec_strip(struct pagevec
*pvec
)
490 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
491 struct page
*page
= pvec
->pages
[i
];
493 if (PagePrivate(page
) && trylock_page(page
)) {
494 if (PagePrivate(page
))
495 try_to_release_page(page
, 0);
502 * pagevec_swap_free - try to free swap space from the pages in a pagevec
503 * @pvec: pagevec with swapcache pages to free the swap space of
505 * The caller needs to hold an extra reference to each page and
506 * not hold the page lock on the pages. This function uses a
507 * trylock on the page lock so it may not always free the swap
508 * space associated with a page.
510 void pagevec_swap_free(struct pagevec
*pvec
)
514 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
515 struct page
*page
= pvec
->pages
[i
];
517 if (PageSwapCache(page
) && trylock_page(page
)) {
518 try_to_free_swap(page
);
525 * pagevec_lookup - gang pagecache lookup
526 * @pvec: Where the resulting pages are placed
527 * @mapping: The address_space to search
528 * @start: The starting page index
529 * @nr_pages: The maximum number of pages
531 * pagevec_lookup() will search for and return a group of up to @nr_pages pages
532 * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
533 * reference against the pages in @pvec.
535 * The search returns a group of mapping-contiguous pages with ascending
536 * indexes. There may be holes in the indices due to not-present pages.
538 * pagevec_lookup() returns the number of pages which were found.
540 unsigned pagevec_lookup(struct pagevec
*pvec
, struct address_space
*mapping
,
541 pgoff_t start
, unsigned nr_pages
)
543 pvec
->nr
= find_get_pages(mapping
, start
, nr_pages
, pvec
->pages
);
544 return pagevec_count(pvec
);
547 EXPORT_SYMBOL(pagevec_lookup
);
549 unsigned pagevec_lookup_tag(struct pagevec
*pvec
, struct address_space
*mapping
,
550 pgoff_t
*index
, int tag
, unsigned nr_pages
)
552 pvec
->nr
= find_get_pages_tag(mapping
, index
, tag
,
553 nr_pages
, pvec
->pages
);
554 return pagevec_count(pvec
);
557 EXPORT_SYMBOL(pagevec_lookup_tag
);
560 * Perform any setup for the swap system
562 void __init
swap_setup(void)
564 unsigned long megs
= num_physpages
>> (20 - PAGE_SHIFT
);
567 bdi_init(swapper_space
.backing_dev_info
);
570 /* Use a smaller cluster for small-memory machines */
576 * Right now other parts of the system means that we
577 * _really_ don't want to cluster much more