]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - mm/swap.c
Mem Policy: add MPOL_F_MEMS_ALLOWED get_mempolicy() flag
[mirror_ubuntu-bionic-kernel.git] / mm / swap.c
CommitLineData
1da177e4
LT
1/*
2 * linux/mm/swap.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 */
6
7/*
8 * This file contains the default values for the opereation of the
9 * Linux VM subsystem. Fine-tuning documentation can be found in
10 * Documentation/sysctl/vm.txt.
11 * Started 18.12.91
12 * Swap aging added 23.2.95, Stephen Tweedie.
13 * Buffermem limits added 12.3.98, Rik van Riel.
14 */
15
16#include <linux/mm.h>
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() */
1da177e4
LT
27#include <linux/percpu_counter.h>
28#include <linux/percpu.h>
29#include <linux/cpu.h>
30#include <linux/notifier.h>
1da177e4
LT
31
32/* How many pages do we try to swap or page in/out together? */
33int page_cluster;
34
b221385b
AB
35/*
36 * This path almost never happens for VM activity - pages are normally
37 * freed via pagevecs. But it gets used by networking.
38 */
39static void fastcall __page_cache_release(struct page *page)
40{
41 if (PageLRU(page)) {
42 unsigned long flags;
43 struct zone *zone = page_zone(page);
44
45 spin_lock_irqsave(&zone->lru_lock, flags);
46 VM_BUG_ON(!PageLRU(page));
47 __ClearPageLRU(page);
48 del_page_from_lru(zone, page);
49 spin_unlock_irqrestore(&zone->lru_lock, flags);
50 }
51 free_hot_page(page);
52}
53
8519fb30 54static void put_compound_page(struct page *page)
1da177e4 55{
d85f3385 56 page = compound_head(page);
8519fb30 57 if (put_page_testzero(page)) {
33f2ef89 58 compound_page_dtor *dtor;
1da177e4 59
33f2ef89 60 dtor = get_compound_page_dtor(page);
8519fb30 61 (*dtor)(page);
1da177e4 62 }
8519fb30
NP
63}
64
65void put_page(struct page *page)
66{
67 if (unlikely(PageCompound(page)))
68 put_compound_page(page);
69 else if (put_page_testzero(page))
1da177e4
LT
70 __page_cache_release(page);
71}
72EXPORT_SYMBOL(put_page);
1da177e4 73
1d7ea732
AZ
74/**
75 * put_pages_list(): release a list of pages
76 *
77 * Release a list of pages which are strung together on page.lru. Currently
78 * used by read_cache_pages() and related error recovery code.
79 *
80 * @pages: list of pages threaded on page->lru
81 */
82void put_pages_list(struct list_head *pages)
83{
84 while (!list_empty(pages)) {
85 struct page *victim;
86
87 victim = list_entry(pages->prev, struct page, lru);
88 list_del(&victim->lru);
89 page_cache_release(victim);
90 }
91}
92EXPORT_SYMBOL(put_pages_list);
93
1da177e4
LT
94/*
95 * Writeback is about to end against a page which has been marked for immediate
96 * reclaim. If it still appears to be reclaimable, move it to the tail of the
97 * inactive list. The page still has PageWriteback set, which will pin it.
98 *
99 * We don't expect many pages to come through here, so don't bother batching
100 * things up.
101 *
102 * To avoid placing the page at the tail of the LRU while PG_writeback is still
103 * set, this function will clear PG_writeback before performing the page
104 * motion. Do that inside the lru lock because once PG_writeback is cleared
105 * we may not touch the page.
106 *
107 * Returns zero if it cleared PG_writeback.
108 */
109int rotate_reclaimable_page(struct page *page)
110{
111 struct zone *zone;
112 unsigned long flags;
113
114 if (PageLocked(page))
115 return 1;
116 if (PageDirty(page))
117 return 1;
118 if (PageActive(page))
119 return 1;
120 if (!PageLRU(page))
121 return 1;
122
123 zone = page_zone(page);
124 spin_lock_irqsave(&zone->lru_lock, flags);
125 if (PageLRU(page) && !PageActive(page)) {
1bfba4e8 126 list_move_tail(&page->lru, &zone->inactive_list);
f8891e5e 127 __count_vm_event(PGROTATED);
1da177e4
LT
128 }
129 if (!test_clear_page_writeback(page))
130 BUG();
131 spin_unlock_irqrestore(&zone->lru_lock, flags);
132 return 0;
133}
134
135/*
136 * FIXME: speed this up?
137 */
138void fastcall activate_page(struct page *page)
139{
140 struct zone *zone = page_zone(page);
141
142 spin_lock_irq(&zone->lru_lock);
143 if (PageLRU(page) && !PageActive(page)) {
144 del_page_from_inactive_list(zone, page);
145 SetPageActive(page);
146 add_page_to_active_list(zone, page);
f8891e5e 147 __count_vm_event(PGACTIVATE);
1da177e4
LT
148 }
149 spin_unlock_irq(&zone->lru_lock);
150}
151
152/*
153 * Mark a page as having seen activity.
154 *
155 * inactive,unreferenced -> inactive,referenced
156 * inactive,referenced -> active,unreferenced
157 * active,unreferenced -> active,referenced
158 */
159void fastcall mark_page_accessed(struct page *page)
160{
161 if (!PageActive(page) && PageReferenced(page) && PageLRU(page)) {
162 activate_page(page);
163 ClearPageReferenced(page);
164 } else if (!PageReferenced(page)) {
165 SetPageReferenced(page);
166 }
167}
168
169EXPORT_SYMBOL(mark_page_accessed);
170
171/**
172 * lru_cache_add: add a page to the page lists
173 * @page: the page to add
174 */
175static DEFINE_PER_CPU(struct pagevec, lru_add_pvecs) = { 0, };
176static DEFINE_PER_CPU(struct pagevec, lru_add_active_pvecs) = { 0, };
177
178void fastcall lru_cache_add(struct page *page)
179{
180 struct pagevec *pvec = &get_cpu_var(lru_add_pvecs);
181
182 page_cache_get(page);
183 if (!pagevec_add(pvec, page))
184 __pagevec_lru_add(pvec);
185 put_cpu_var(lru_add_pvecs);
186}
187
188void fastcall lru_cache_add_active(struct page *page)
189{
190 struct pagevec *pvec = &get_cpu_var(lru_add_active_pvecs);
191
192 page_cache_get(page);
193 if (!pagevec_add(pvec, page))
194 __pagevec_lru_add_active(pvec);
195 put_cpu_var(lru_add_active_pvecs);
196}
197
80bfed90 198static void __lru_add_drain(int cpu)
1da177e4 199{
80bfed90 200 struct pagevec *pvec = &per_cpu(lru_add_pvecs, cpu);
1da177e4 201
80bfed90 202 /* CPU is dead, so no locking needed. */
1da177e4
LT
203 if (pagevec_count(pvec))
204 __pagevec_lru_add(pvec);
80bfed90 205 pvec = &per_cpu(lru_add_active_pvecs, cpu);
1da177e4
LT
206 if (pagevec_count(pvec))
207 __pagevec_lru_add_active(pvec);
80bfed90
AM
208}
209
210void lru_add_drain(void)
211{
212 __lru_add_drain(get_cpu());
213 put_cpu();
1da177e4
LT
214}
215
053837fc 216#ifdef CONFIG_NUMA
c4028958 217static void lru_add_drain_per_cpu(struct work_struct *dummy)
053837fc
NP
218{
219 lru_add_drain();
220}
221
222/*
223 * Returns 0 for success
224 */
225int lru_add_drain_all(void)
226{
c4028958 227 return schedule_on_each_cpu(lru_add_drain_per_cpu);
053837fc
NP
228}
229
230#else
231
232/*
233 * Returns 0 for success
234 */
235int lru_add_drain_all(void)
236{
237 lru_add_drain();
238 return 0;
239}
240#endif
241
1da177e4
LT
242/*
243 * Batched page_cache_release(). Decrement the reference count on all the
244 * passed pages. If it fell to zero then remove the page from the LRU and
245 * free it.
246 *
247 * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
248 * for the remainder of the operation.
249 *
250 * The locking in this function is against shrink_cache(): we recheck the
251 * page count inside the lock to see whether shrink_cache grabbed the page
252 * via the LRU. If it did, give up: shrink_cache will free it.
253 */
254void release_pages(struct page **pages, int nr, int cold)
255{
256 int i;
257 struct pagevec pages_to_free;
258 struct zone *zone = NULL;
259
260 pagevec_init(&pages_to_free, cold);
261 for (i = 0; i < nr; i++) {
262 struct page *page = pages[i];
1da177e4 263
8519fb30
NP
264 if (unlikely(PageCompound(page))) {
265 if (zone) {
266 spin_unlock_irq(&zone->lru_lock);
267 zone = NULL;
268 }
269 put_compound_page(page);
270 continue;
271 }
272
b5810039 273 if (!put_page_testzero(page))
1da177e4
LT
274 continue;
275
46453a6e
NP
276 if (PageLRU(page)) {
277 struct zone *pagezone = page_zone(page);
278 if (pagezone != zone) {
279 if (zone)
280 spin_unlock_irq(&zone->lru_lock);
281 zone = pagezone;
282 spin_lock_irq(&zone->lru_lock);
283 }
725d704e 284 VM_BUG_ON(!PageLRU(page));
67453911 285 __ClearPageLRU(page);
1da177e4 286 del_page_from_lru(zone, page);
46453a6e
NP
287 }
288
289 if (!pagevec_add(&pages_to_free, page)) {
290 if (zone) {
1da177e4 291 spin_unlock_irq(&zone->lru_lock);
46453a6e 292 zone = NULL;
1da177e4 293 }
46453a6e
NP
294 __pagevec_free(&pages_to_free);
295 pagevec_reinit(&pages_to_free);
296 }
1da177e4
LT
297 }
298 if (zone)
299 spin_unlock_irq(&zone->lru_lock);
300
301 pagevec_free(&pages_to_free);
302}
303
304/*
305 * The pages which we're about to release may be in the deferred lru-addition
306 * queues. That would prevent them from really being freed right now. That's
307 * OK from a correctness point of view but is inefficient - those pages may be
308 * cache-warm and we want to give them back to the page allocator ASAP.
309 *
310 * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
311 * and __pagevec_lru_add_active() call release_pages() directly to avoid
312 * mutual recursion.
313 */
314void __pagevec_release(struct pagevec *pvec)
315{
316 lru_add_drain();
317 release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
318 pagevec_reinit(pvec);
319}
320
7f285701
SF
321EXPORT_SYMBOL(__pagevec_release);
322
1da177e4
LT
323/*
324 * pagevec_release() for pages which are known to not be on the LRU
325 *
326 * This function reinitialises the caller's pagevec.
327 */
328void __pagevec_release_nonlru(struct pagevec *pvec)
329{
330 int i;
331 struct pagevec pages_to_free;
332
333 pagevec_init(&pages_to_free, pvec->cold);
1da177e4
LT
334 for (i = 0; i < pagevec_count(pvec); i++) {
335 struct page *page = pvec->pages[i];
336
725d704e 337 VM_BUG_ON(PageLRU(page));
1da177e4
LT
338 if (put_page_testzero(page))
339 pagevec_add(&pages_to_free, page);
340 }
341 pagevec_free(&pages_to_free);
342 pagevec_reinit(pvec);
343}
344
345/*
346 * Add the passed pages to the LRU, then drop the caller's refcount
347 * on them. Reinitialises the caller's pagevec.
348 */
349void __pagevec_lru_add(struct pagevec *pvec)
350{
351 int i;
352 struct zone *zone = NULL;
353
354 for (i = 0; i < pagevec_count(pvec); i++) {
355 struct page *page = pvec->pages[i];
356 struct zone *pagezone = page_zone(page);
357
358 if (pagezone != zone) {
359 if (zone)
360 spin_unlock_irq(&zone->lru_lock);
361 zone = pagezone;
362 spin_lock_irq(&zone->lru_lock);
363 }
725d704e 364 VM_BUG_ON(PageLRU(page));
8d438f96 365 SetPageLRU(page);
1da177e4
LT
366 add_page_to_inactive_list(zone, page);
367 }
368 if (zone)
369 spin_unlock_irq(&zone->lru_lock);
370 release_pages(pvec->pages, pvec->nr, pvec->cold);
371 pagevec_reinit(pvec);
372}
373
374EXPORT_SYMBOL(__pagevec_lru_add);
375
376void __pagevec_lru_add_active(struct pagevec *pvec)
377{
378 int i;
379 struct zone *zone = NULL;
380
381 for (i = 0; i < pagevec_count(pvec); i++) {
382 struct page *page = pvec->pages[i];
383 struct zone *pagezone = page_zone(page);
384
385 if (pagezone != zone) {
386 if (zone)
387 spin_unlock_irq(&zone->lru_lock);
388 zone = pagezone;
389 spin_lock_irq(&zone->lru_lock);
390 }
725d704e 391 VM_BUG_ON(PageLRU(page));
8d438f96 392 SetPageLRU(page);
725d704e 393 VM_BUG_ON(PageActive(page));
4c84cacf 394 SetPageActive(page);
1da177e4
LT
395 add_page_to_active_list(zone, page);
396 }
397 if (zone)
398 spin_unlock_irq(&zone->lru_lock);
399 release_pages(pvec->pages, pvec->nr, pvec->cold);
400 pagevec_reinit(pvec);
401}
402
403/*
404 * Try to drop buffers from the pages in a pagevec
405 */
406void pagevec_strip(struct pagevec *pvec)
407{
408 int i;
409
410 for (i = 0; i < pagevec_count(pvec); i++) {
411 struct page *page = pvec->pages[i];
412
413 if (PagePrivate(page) && !TestSetPageLocked(page)) {
5b40dc78
CL
414 if (PagePrivate(page))
415 try_to_release_page(page, 0);
1da177e4
LT
416 unlock_page(page);
417 }
418 }
419}
420
421/**
422 * pagevec_lookup - gang pagecache lookup
423 * @pvec: Where the resulting pages are placed
424 * @mapping: The address_space to search
425 * @start: The starting page index
426 * @nr_pages: The maximum number of pages
427 *
428 * pagevec_lookup() will search for and return a group of up to @nr_pages pages
429 * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
430 * reference against the pages in @pvec.
431 *
432 * The search returns a group of mapping-contiguous pages with ascending
433 * indexes. There may be holes in the indices due to not-present pages.
434 *
435 * pagevec_lookup() returns the number of pages which were found.
436 */
437unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
438 pgoff_t start, unsigned nr_pages)
439{
440 pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
441 return pagevec_count(pvec);
442}
443
78539fdf
CH
444EXPORT_SYMBOL(pagevec_lookup);
445
1da177e4
LT
446unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
447 pgoff_t *index, int tag, unsigned nr_pages)
448{
449 pvec->nr = find_get_pages_tag(mapping, index, tag,
450 nr_pages, pvec->pages);
451 return pagevec_count(pvec);
452}
453
7f285701 454EXPORT_SYMBOL(pagevec_lookup_tag);
1da177e4
LT
455
456#ifdef CONFIG_SMP
457/*
458 * We tolerate a little inaccuracy to avoid ping-ponging the counter between
459 * CPUs
460 */
461#define ACCT_THRESHOLD max(16, NR_CPUS * 2)
462
463static DEFINE_PER_CPU(long, committed_space) = 0;
464
465void vm_acct_memory(long pages)
466{
467 long *local;
468
469 preempt_disable();
470 local = &__get_cpu_var(committed_space);
471 *local += pages;
472 if (*local > ACCT_THRESHOLD || *local < -ACCT_THRESHOLD) {
473 atomic_add(*local, &vm_committed_space);
474 *local = 0;
475 }
476 preempt_enable();
477}
1da177e4
LT
478
479#ifdef CONFIG_HOTPLUG_CPU
1da177e4
LT
480
481/* Drop the CPU's cached committed space back into the central pool. */
482static int cpu_swap_callback(struct notifier_block *nfb,
483 unsigned long action,
484 void *hcpu)
485{
486 long *committed;
487
488 committed = &per_cpu(committed_space, (long)hcpu);
8bb78442 489 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
1da177e4
LT
490 atomic_add(*committed, &vm_committed_space);
491 *committed = 0;
80bfed90 492 __lru_add_drain((long)hcpu);
1da177e4
LT
493 }
494 return NOTIFY_OK;
495}
496#endif /* CONFIG_HOTPLUG_CPU */
497#endif /* CONFIG_SMP */
498
1da177e4
LT
499/*
500 * Perform any setup for the swap system
501 */
502void __init swap_setup(void)
503{
504 unsigned long megs = num_physpages >> (20 - PAGE_SHIFT);
505
506 /* Use a smaller cluster for small-memory machines */
507 if (megs < 16)
508 page_cluster = 2;
509 else
510 page_cluster = 3;
511 /*
512 * Right now other parts of the system means that we
513 * _really_ don't want to cluster much more
514 */
02316067 515#ifdef CONFIG_HOTPLUG_CPU
1da177e4 516 hotcpu_notifier(cpu_swap_callback, 0);
02316067 517#endif
1da177e4 518}