blob: 8f6ad13d34f5a24ffb635b5a6bb38f5116515e72 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/vmscan.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 *
6 * Swap reorganised 29.12.95, Stephen Tweedie.
7 * kswapd added: 7.1.96 sct
8 * Removed kswapd_ctl limits, and swap out as many pages as needed
9 * to bring the system back to freepages.high: 2.4.97, Rik van Riel.
10 * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com).
11 * Multiqueue VM started 5.8.00, Rik van Riel.
12 */
13
14#include <linux/mm.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/kernel_stat.h>
18#include <linux/swap.h>
19#include <linux/pagemap.h>
20#include <linux/init.h>
21#include <linux/highmem.h>
22#include <linux/file.h>
23#include <linux/writeback.h>
24#include <linux/blkdev.h>
25#include <linux/buffer_head.h> /* for try_to_release_page(),
26 buffer_heads_over_limit */
27#include <linux/mm_inline.h>
28#include <linux/pagevec.h>
29#include <linux/backing-dev.h>
30#include <linux/rmap.h>
31#include <linux/topology.h>
32#include <linux/cpu.h>
33#include <linux/cpuset.h>
34#include <linux/notifier.h>
35#include <linux/rwsem.h>
36
37#include <asm/tlbflush.h>
38#include <asm/div64.h>
39
40#include <linux/swapops.h>
41
42/* possible outcome of pageout() */
43typedef enum {
44 /* failed to write page out, page is locked */
45 PAGE_KEEP,
46 /* move page to the active list, page is locked */
47 PAGE_ACTIVATE,
48 /* page has been sent to the disk successfully, page is unlocked */
49 PAGE_SUCCESS,
50 /* page is clean and locked */
51 PAGE_CLEAN,
52} pageout_t;
53
54struct scan_control {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 /* Incremented by the number of inactive pages that were scanned */
56 unsigned long nr_scanned;
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 unsigned long nr_mapped; /* From page_state */
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 /* This context's GFP mask */
Al Viro6daa0e22005-10-21 03:18:50 -040061 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 int may_writepage;
64
Christoph Lameterf1fd1062006-01-18 17:42:30 -080065 /* Can pages be swapped as part of reclaim? */
66 int may_swap;
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
69 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
70 * In this context, it doesn't matter that we scan the
71 * whole list at once. */
72 int swap_cluster_max;
73};
74
75/*
76 * The list of shrinker callbacks used by to apply pressure to
77 * ageable caches.
78 */
79struct shrinker {
80 shrinker_t shrinker;
81 struct list_head list;
82 int seeks; /* seeks to recreate an obj */
83 long nr; /* objs pending delete */
84};
85
86#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
87
88#ifdef ARCH_HAS_PREFETCH
89#define prefetch_prev_lru_page(_page, _base, _field) \
90 do { \
91 if ((_page)->lru.prev != _base) { \
92 struct page *prev; \
93 \
94 prev = lru_to_page(&(_page->lru)); \
95 prefetch(&prev->_field); \
96 } \
97 } while (0)
98#else
99#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
100#endif
101
102#ifdef ARCH_HAS_PREFETCHW
103#define prefetchw_prev_lru_page(_page, _base, _field) \
104 do { \
105 if ((_page)->lru.prev != _base) { \
106 struct page *prev; \
107 \
108 prev = lru_to_page(&(_page->lru)); \
109 prefetchw(&prev->_field); \
110 } \
111 } while (0)
112#else
113#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
114#endif
115
116/*
117 * From 0 .. 100. Higher means more swappy.
118 */
119int vm_swappiness = 60;
120static long total_memory;
121
122static LIST_HEAD(shrinker_list);
123static DECLARE_RWSEM(shrinker_rwsem);
124
125/*
126 * Add a shrinker callback to be called from the vm
127 */
128struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
129{
130 struct shrinker *shrinker;
131
132 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
133 if (shrinker) {
134 shrinker->shrinker = theshrinker;
135 shrinker->seeks = seeks;
136 shrinker->nr = 0;
137 down_write(&shrinker_rwsem);
138 list_add_tail(&shrinker->list, &shrinker_list);
139 up_write(&shrinker_rwsem);
140 }
141 return shrinker;
142}
143EXPORT_SYMBOL(set_shrinker);
144
145/*
146 * Remove one
147 */
148void remove_shrinker(struct shrinker *shrinker)
149{
150 down_write(&shrinker_rwsem);
151 list_del(&shrinker->list);
152 up_write(&shrinker_rwsem);
153 kfree(shrinker);
154}
155EXPORT_SYMBOL(remove_shrinker);
156
157#define SHRINK_BATCH 128
158/*
159 * Call the shrink functions to age shrinkable caches
160 *
161 * Here we assume it costs one seek to replace a lru page and that it also
162 * takes a seek to recreate a cache object. With this in mind we age equal
163 * percentages of the lru and ageable caches. This should balance the seeks
164 * generated by these structures.
165 *
166 * If the vm encounted mapped pages on the LRU it increase the pressure on
167 * slab to avoid swapping.
168 *
169 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
170 *
171 * `lru_pages' represents the number of on-LRU pages in all the zones which
172 * are eligible for the caller's allocation attempt. It is used for balancing
173 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700174 *
175 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
Andrew Morton69e05942006-03-22 00:08:19 -0800177unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
178 unsigned long lru_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct shrinker *shrinker;
Andrew Morton69e05942006-03-22 00:08:19 -0800181 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 if (scanned == 0)
184 scanned = SWAP_CLUSTER_MAX;
185
186 if (!down_read_trylock(&shrinker_rwsem))
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700187 return 1; /* Assume we'll be able to shrink next time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 list_for_each_entry(shrinker, &shrinker_list, list) {
190 unsigned long long delta;
191 unsigned long total_scan;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800192 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 delta = (4 * scanned) / shrinker->seeks;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800195 delta *= max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 do_div(delta, lru_pages + 1);
197 shrinker->nr += delta;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800198 if (shrinker->nr < 0) {
199 printk(KERN_ERR "%s: nr=%ld\n",
200 __FUNCTION__, shrinker->nr);
201 shrinker->nr = max_pass;
202 }
203
204 /*
205 * Avoid risking looping forever due to too large nr value:
206 * never try to free more than twice the estimate number of
207 * freeable entries.
208 */
209 if (shrinker->nr > max_pass * 2)
210 shrinker->nr = max_pass * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 total_scan = shrinker->nr;
213 shrinker->nr = 0;
214
215 while (total_scan >= SHRINK_BATCH) {
216 long this_scan = SHRINK_BATCH;
217 int shrink_ret;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700218 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700220 nr_before = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
222 if (shrink_ret == -1)
223 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700224 if (shrink_ret < nr_before)
225 ret += nr_before - shrink_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 mod_page_state(slabs_scanned, this_scan);
227 total_scan -= this_scan;
228
229 cond_resched();
230 }
231
232 shrinker->nr += total_scan;
233 }
234 up_read(&shrinker_rwsem);
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700235 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238/* Called without lock on whether page is mapped, so answer is unstable */
239static inline int page_mapping_inuse(struct page *page)
240{
241 struct address_space *mapping;
242
243 /* Page is in somebody's page tables. */
244 if (page_mapped(page))
245 return 1;
246
247 /* Be more reluctant to reclaim swapcache than pagecache */
248 if (PageSwapCache(page))
249 return 1;
250
251 mapping = page_mapping(page);
252 if (!mapping)
253 return 0;
254
255 /* File is mmap'd by somebody? */
256 return mapping_mapped(mapping);
257}
258
259static inline int is_page_cache_freeable(struct page *page)
260{
261 return page_count(page) - !!PagePrivate(page) == 2;
262}
263
264static int may_write_to_queue(struct backing_dev_info *bdi)
265{
Christoph Lameter930d9152006-01-08 01:00:47 -0800266 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return 1;
268 if (!bdi_write_congested(bdi))
269 return 1;
270 if (bdi == current->backing_dev_info)
271 return 1;
272 return 0;
273}
274
275/*
276 * We detected a synchronous write error writing a page out. Probably
277 * -ENOSPC. We need to propagate that into the address_space for a subsequent
278 * fsync(), msync() or close().
279 *
280 * The tricky part is that after writepage we cannot touch the mapping: nothing
281 * prevents it from being freed up. But we have a ref on the page and once
282 * that page is locked, the mapping is pinned.
283 *
284 * We're allowed to run sleeping lock_page() here because we know the caller has
285 * __GFP_FS.
286 */
287static void handle_write_error(struct address_space *mapping,
288 struct page *page, int error)
289{
290 lock_page(page);
291 if (page_mapping(page) == mapping) {
292 if (error == -ENOSPC)
293 set_bit(AS_ENOSPC, &mapping->flags);
294 else
295 set_bit(AS_EIO, &mapping->flags);
296 }
297 unlock_page(page);
298}
299
300/*
301 * pageout is called by shrink_list() for each dirty page. Calls ->writepage().
302 */
303static pageout_t pageout(struct page *page, struct address_space *mapping)
304{
305 /*
306 * If the page is dirty, only perform writeback if that write
307 * will be non-blocking. To prevent this allocation from being
308 * stalled by pagecache activity. But note that there may be
309 * stalls if we need to run get_block(). We could test
310 * PagePrivate for that.
311 *
312 * If this process is currently in generic_file_write() against
313 * this page's queue, we can perform writeback even if that
314 * will block.
315 *
316 * If the page is swapcache, write it back even if that would
317 * block, for some throttling. This happens by accident, because
318 * swap_backing_dev_info is bust: it doesn't reflect the
319 * congestion state of the swapdevs. Easy to fix, if needed.
320 * See swapfile.c:page_queue_congested().
321 */
322 if (!is_page_cache_freeable(page))
323 return PAGE_KEEP;
324 if (!mapping) {
325 /*
326 * Some data journaling orphaned pages can have
327 * page->mapping == NULL while being dirty with clean buffers.
328 */
akpm@osdl.org323aca62005-04-16 15:24:06 -0700329 if (PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (try_to_free_buffers(page)) {
331 ClearPageDirty(page);
332 printk("%s: orphaned page\n", __FUNCTION__);
333 return PAGE_CLEAN;
334 }
335 }
336 return PAGE_KEEP;
337 }
338 if (mapping->a_ops->writepage == NULL)
339 return PAGE_ACTIVATE;
340 if (!may_write_to_queue(mapping->backing_dev_info))
341 return PAGE_KEEP;
342
343 if (clear_page_dirty_for_io(page)) {
344 int res;
345 struct writeback_control wbc = {
346 .sync_mode = WB_SYNC_NONE,
347 .nr_to_write = SWAP_CLUSTER_MAX,
348 .nonblocking = 1,
349 .for_reclaim = 1,
350 };
351
352 SetPageReclaim(page);
353 res = mapping->a_ops->writepage(page, &wbc);
354 if (res < 0)
355 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800356 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 ClearPageReclaim(page);
358 return PAGE_ACTIVATE;
359 }
360 if (!PageWriteback(page)) {
361 /* synchronous write or broken a_ops? */
362 ClearPageReclaim(page);
363 }
364
365 return PAGE_SUCCESS;
366 }
367
368 return PAGE_CLEAN;
369}
370
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800371static int remove_mapping(struct address_space *mapping, struct page *page)
372{
373 if (!mapping)
374 return 0; /* truncate got there first */
375
376 write_lock_irq(&mapping->tree_lock);
377
378 /*
379 * The non-racy check for busy page. It is critical to check
380 * PageDirty _after_ making sure that the page is freeable and
381 * not in use by anybody. (pagecache + us == 2)
382 */
383 if (unlikely(page_count(page) != 2))
384 goto cannot_free;
385 smp_rmb();
386 if (unlikely(PageDirty(page)))
387 goto cannot_free;
388
389 if (PageSwapCache(page)) {
390 swp_entry_t swap = { .val = page_private(page) };
391 __delete_from_swap_cache(page);
392 write_unlock_irq(&mapping->tree_lock);
393 swap_free(swap);
394 __put_page(page); /* The pagecache ref */
395 return 1;
396 }
397
398 __remove_from_page_cache(page);
399 write_unlock_irq(&mapping->tree_lock);
400 __put_page(page);
401 return 1;
402
403cannot_free:
404 write_unlock_irq(&mapping->tree_lock);
405 return 0;
406}
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408/*
Andrew Morton05ff5132006-03-22 00:08:20 -0800409 * shrink_list return the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
Andrew Morton69e05942006-03-22 00:08:19 -0800411static unsigned long shrink_list(struct list_head *page_list,
412 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 LIST_HEAD(ret_pages);
415 struct pagevec freed_pvec;
416 int pgactivate = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800417 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 cond_resched();
420
421 pagevec_init(&freed_pvec, 1);
422 while (!list_empty(page_list)) {
423 struct address_space *mapping;
424 struct page *page;
425 int may_enter_fs;
426 int referenced;
427
428 cond_resched();
429
430 page = lru_to_page(page_list);
431 list_del(&page->lru);
432
433 if (TestSetPageLocked(page))
434 goto keep;
435
436 BUG_ON(PageActive(page));
437
438 sc->nr_scanned++;
Christoph Lameter80e43422006-02-11 17:55:53 -0800439
440 if (!sc->may_swap && page_mapped(page))
441 goto keep_locked;
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /* Double the slab pressure for mapped and swapcache pages */
444 if (page_mapped(page) || PageSwapCache(page))
445 sc->nr_scanned++;
446
447 if (PageWriteback(page))
448 goto keep_locked;
449
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800450 referenced = page_referenced(page, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /* In active use or really unfreeable? Activate it. */
452 if (referenced && page_mapping_inuse(page))
453 goto activate_locked;
454
455#ifdef CONFIG_SWAP
456 /*
457 * Anonymous process memory has backing store?
458 * Try to allocate it some swap space here.
459 */
Lee Schermerhornc3400102005-10-29 18:15:51 -0700460 if (PageAnon(page) && !PageSwapCache(page)) {
Christoph Lameterf1fd1062006-01-18 17:42:30 -0800461 if (!sc->may_swap)
462 goto keep_locked;
Christoph Lameter1480a542006-01-08 01:00:53 -0800463 if (!add_to_swap(page, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 goto activate_locked;
465 }
466#endif /* CONFIG_SWAP */
467
468 mapping = page_mapping(page);
469 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
470 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
471
472 /*
473 * The page is mapped into the page tables of one or more
474 * processes. Try to unmap it here.
475 */
476 if (page_mapped(page) && mapping) {
Christoph Lameteraa3f18b32006-02-01 03:05:32 -0800477 /*
478 * No unmapping if we do not swap
479 */
480 if (!sc->may_swap)
481 goto keep_locked;
482
Christoph Lametera48d07a2006-02-01 03:05:38 -0800483 switch (try_to_unmap(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 case SWAP_FAIL:
485 goto activate_locked;
486 case SWAP_AGAIN:
487 goto keep_locked;
488 case SWAP_SUCCESS:
489 ; /* try to free the page below */
490 }
491 }
492
493 if (PageDirty(page)) {
494 if (referenced)
495 goto keep_locked;
496 if (!may_enter_fs)
497 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -0800498 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 goto keep_locked;
500
501 /* Page is dirty, try to write it out here */
502 switch(pageout(page, mapping)) {
503 case PAGE_KEEP:
504 goto keep_locked;
505 case PAGE_ACTIVATE:
506 goto activate_locked;
507 case PAGE_SUCCESS:
508 if (PageWriteback(page) || PageDirty(page))
509 goto keep;
510 /*
511 * A synchronous write - probably a ramdisk. Go
512 * ahead and try to reclaim the page.
513 */
514 if (TestSetPageLocked(page))
515 goto keep;
516 if (PageDirty(page) || PageWriteback(page))
517 goto keep_locked;
518 mapping = page_mapping(page);
519 case PAGE_CLEAN:
520 ; /* try to free the page below */
521 }
522 }
523
524 /*
525 * If the page has buffers, try to free the buffer mappings
526 * associated with this page. If we succeed we try to free
527 * the page as well.
528 *
529 * We do this even if the page is PageDirty().
530 * try_to_release_page() does not perform I/O, but it is
531 * possible for a page to have PageDirty set, but it is actually
532 * clean (all its buffers are clean). This happens if the
533 * buffers were written out directly, with submit_bh(). ext3
534 * will do this, as well as the blockdev mapping.
535 * try_to_release_page() will discover that cleanness and will
536 * drop the buffers and mark the page clean - it can be freed.
537 *
538 * Rarely, pages can have buffers and no ->mapping. These are
539 * the pages which were not successfully invalidated in
540 * truncate_complete_page(). We try to drop those buffers here
541 * and if that worked, and the page is no longer mapped into
542 * process address space (page_count == 1) it can be freed.
543 * Otherwise, leave the page on the LRU so it is swappable.
544 */
545 if (PagePrivate(page)) {
546 if (!try_to_release_page(page, sc->gfp_mask))
547 goto activate_locked;
548 if (!mapping && page_count(page) == 1)
549 goto free_it;
550 }
551
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800552 if (!remove_mapping(mapping, page))
553 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555free_it:
556 unlock_page(page);
Andrew Morton05ff5132006-03-22 00:08:20 -0800557 nr_reclaimed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (!pagevec_add(&freed_pvec, page))
559 __pagevec_release_nonlru(&freed_pvec);
560 continue;
561
562activate_locked:
563 SetPageActive(page);
564 pgactivate++;
565keep_locked:
566 unlock_page(page);
567keep:
568 list_add(&page->lru, &ret_pages);
569 BUG_ON(PageLRU(page));
570 }
571 list_splice(&ret_pages, page_list);
572 if (pagevec_count(&freed_pvec))
573 __pagevec_release_nonlru(&freed_pvec);
574 mod_page_state(pgactivate, pgactivate);
Andrew Morton05ff5132006-03-22 00:08:20 -0800575 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800578#ifdef CONFIG_MIGRATION
Christoph Lameter8419c312006-01-08 01:00:52 -0800579static inline void move_to_lru(struct page *page)
580{
581 list_del(&page->lru);
582 if (PageActive(page)) {
583 /*
584 * lru_cache_add_active checks that
585 * the PG_active bit is off.
586 */
587 ClearPageActive(page);
588 lru_cache_add_active(page);
589 } else {
590 lru_cache_add(page);
591 }
592 put_page(page);
593}
594
595/*
Nick Piggin053837f2006-01-18 17:42:27 -0800596 * Add isolated pages on the list back to the LRU.
Christoph Lameter8419c312006-01-08 01:00:52 -0800597 *
598 * returns the number of pages put back.
599 */
Andrew Morton69e05942006-03-22 00:08:19 -0800600unsigned long putback_lru_pages(struct list_head *l)
Christoph Lameter8419c312006-01-08 01:00:52 -0800601{
602 struct page *page;
603 struct page *page2;
Andrew Morton69e05942006-03-22 00:08:19 -0800604 unsigned long count = 0;
Christoph Lameter8419c312006-01-08 01:00:52 -0800605
606 list_for_each_entry_safe(page, page2, l, lru) {
607 move_to_lru(page);
608 count++;
609 }
610 return count;
611}
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613/*
Christoph Lametere965f962006-02-01 03:05:41 -0800614 * Non migratable page
615 */
616int fail_migrate_page(struct page *newpage, struct page *page)
617{
618 return -EIO;
619}
620EXPORT_SYMBOL(fail_migrate_page);
621
622/*
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800623 * swapout a single page
624 * page is locked upon entry, unlocked on exit
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800625 */
626static int swap_page(struct page *page)
627{
628 struct address_space *mapping = page_mapping(page);
629
630 if (page_mapped(page) && mapping)
Christoph Lameter418aade2006-02-10 01:51:15 -0800631 if (try_to_unmap(page, 1) != SWAP_SUCCESS)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800632 goto unlock_retry;
633
634 if (PageDirty(page)) {
635 /* Page is dirty, try to write it out here */
636 switch(pageout(page, mapping)) {
637 case PAGE_KEEP:
638 case PAGE_ACTIVATE:
639 goto unlock_retry;
640
641 case PAGE_SUCCESS:
642 goto retry;
643
644 case PAGE_CLEAN:
645 ; /* try to free the page below */
646 }
647 }
648
649 if (PagePrivate(page)) {
650 if (!try_to_release_page(page, GFP_KERNEL) ||
651 (!mapping && page_count(page) == 1))
652 goto unlock_retry;
653 }
654
655 if (remove_mapping(mapping, page)) {
656 /* Success */
657 unlock_page(page);
658 return 0;
659 }
660
661unlock_retry:
662 unlock_page(page);
663
664retry:
Christoph Lameterd0d96322006-01-08 01:00:55 -0800665 return -EAGAIN;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800666}
Christoph Lametere965f962006-02-01 03:05:41 -0800667EXPORT_SYMBOL(swap_page);
Christoph Lametera48d07a2006-02-01 03:05:38 -0800668
669/*
670 * Page migration was first developed in the context of the memory hotplug
671 * project. The main authors of the migration code are:
672 *
673 * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
674 * Hirokazu Takahashi <taka@valinux.co.jp>
675 * Dave Hansen <haveblue@us.ibm.com>
676 * Christoph Lameter <clameter@sgi.com>
677 */
678
679/*
680 * Remove references for a page and establish the new page with the correct
681 * basic settings to be able to stop accesses to the page.
682 */
Christoph Lametere965f962006-02-01 03:05:41 -0800683int migrate_page_remove_references(struct page *newpage,
Christoph Lametera48d07a2006-02-01 03:05:38 -0800684 struct page *page, int nr_refs)
685{
686 struct address_space *mapping = page_mapping(page);
687 struct page **radix_pointer;
688
689 /*
690 * Avoid doing any of the following work if the page count
691 * indicates that the page is in use or truncate has removed
692 * the page.
693 */
694 if (!mapping || page_mapcount(page) + nr_refs != page_count(page))
Christoph Lameter4983da02006-03-14 19:50:19 -0800695 return -EAGAIN;
Christoph Lametera48d07a2006-02-01 03:05:38 -0800696
697 /*
698 * Establish swap ptes for anonymous pages or destroy pte
699 * maps for files.
700 *
701 * In order to reestablish file backed mappings the fault handlers
702 * will take the radix tree_lock which may then be used to stop
703 * processses from accessing this page until the new page is ready.
704 *
705 * A process accessing via a swap pte (an anonymous page) will take a
706 * page_lock on the old page which will block the process until the
707 * migration attempt is complete. At that time the PageSwapCache bit
708 * will be examined. If the page was migrated then the PageSwapCache
709 * bit will be clear and the operation to retrieve the page will be
710 * retried which will find the new page in the radix tree. Then a new
711 * direct mapping may be generated based on the radix tree contents.
712 *
713 * If the page was not migrated then the PageSwapCache bit
714 * is still set and the operation may continue.
715 */
Christoph Lameter4983da02006-03-14 19:50:19 -0800716 if (try_to_unmap(page, 1) == SWAP_FAIL)
717 /* A vma has VM_LOCKED set -> Permanent failure */
718 return -EPERM;
Christoph Lametera48d07a2006-02-01 03:05:38 -0800719
720 /*
721 * Give up if we were unable to remove all mappings.
722 */
723 if (page_mapcount(page))
Christoph Lameter4983da02006-03-14 19:50:19 -0800724 return -EAGAIN;
Christoph Lametera48d07a2006-02-01 03:05:38 -0800725
726 write_lock_irq(&mapping->tree_lock);
727
728 radix_pointer = (struct page **)radix_tree_lookup_slot(
729 &mapping->page_tree,
730 page_index(page));
731
732 if (!page_mapping(page) || page_count(page) != nr_refs ||
733 *radix_pointer != page) {
734 write_unlock_irq(&mapping->tree_lock);
Christoph Lameter4983da02006-03-14 19:50:19 -0800735 return -EAGAIN;
Christoph Lametera48d07a2006-02-01 03:05:38 -0800736 }
737
738 /*
739 * Now we know that no one else is looking at the page.
740 *
741 * Certain minimal information about a page must be available
742 * in order for other subsystems to properly handle the page if they
743 * find it through the radix tree update before we are finished
744 * copying the page.
745 */
746 get_page(newpage);
747 newpage->index = page->index;
748 newpage->mapping = page->mapping;
749 if (PageSwapCache(page)) {
750 SetPageSwapCache(newpage);
751 set_page_private(newpage, page_private(page));
752 }
753
754 *radix_pointer = newpage;
755 __put_page(page);
756 write_unlock_irq(&mapping->tree_lock);
757
758 return 0;
759}
Christoph Lametere965f962006-02-01 03:05:41 -0800760EXPORT_SYMBOL(migrate_page_remove_references);
Christoph Lametera48d07a2006-02-01 03:05:38 -0800761
762/*
763 * Copy the page to its new location
764 */
765void migrate_page_copy(struct page *newpage, struct page *page)
766{
767 copy_highpage(newpage, page);
768
769 if (PageError(page))
770 SetPageError(newpage);
771 if (PageReferenced(page))
772 SetPageReferenced(newpage);
773 if (PageUptodate(page))
774 SetPageUptodate(newpage);
775 if (PageActive(page))
776 SetPageActive(newpage);
777 if (PageChecked(page))
778 SetPageChecked(newpage);
779 if (PageMappedToDisk(page))
780 SetPageMappedToDisk(newpage);
781
782 if (PageDirty(page)) {
783 clear_page_dirty_for_io(page);
784 set_page_dirty(newpage);
785 }
786
787 ClearPageSwapCache(page);
788 ClearPageActive(page);
789 ClearPagePrivate(page);
790 set_page_private(page, 0);
791 page->mapping = NULL;
792
793 /*
794 * If any waiters have accumulated on the new page then
795 * wake them up.
796 */
797 if (PageWriteback(newpage))
798 end_page_writeback(newpage);
799}
Christoph Lametere965f962006-02-01 03:05:41 -0800800EXPORT_SYMBOL(migrate_page_copy);
Christoph Lametera48d07a2006-02-01 03:05:38 -0800801
802/*
803 * Common logic to directly migrate a single page suitable for
804 * pages that do not use PagePrivate.
805 *
806 * Pages are locked upon entry and exit.
807 */
808int migrate_page(struct page *newpage, struct page *page)
809{
Christoph Lameter4983da02006-03-14 19:50:19 -0800810 int rc;
811
Christoph Lametera48d07a2006-02-01 03:05:38 -0800812 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
813
Christoph Lameter4983da02006-03-14 19:50:19 -0800814 rc = migrate_page_remove_references(newpage, page, 2);
815
816 if (rc)
817 return rc;
Christoph Lametera48d07a2006-02-01 03:05:38 -0800818
819 migrate_page_copy(newpage, page);
820
Christoph Lametera3351e52006-02-01 03:05:39 -0800821 /*
822 * Remove auxiliary swap entries and replace
823 * them with real ptes.
824 *
825 * Note that a real pte entry will allow processes that are not
826 * waiting on the page lock to use the new page via the page tables
827 * before the new page is unlocked.
828 */
829 remove_from_swap(newpage);
Christoph Lametera48d07a2006-02-01 03:05:38 -0800830 return 0;
831}
Christoph Lametere965f962006-02-01 03:05:41 -0800832EXPORT_SYMBOL(migrate_page);
Christoph Lametera48d07a2006-02-01 03:05:38 -0800833
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800834/*
835 * migrate_pages
836 *
837 * Two lists are passed to this function. The first list
838 * contains the pages isolated from the LRU to be migrated.
839 * The second list contains new pages that the pages isolated
840 * can be moved to. If the second list is NULL then all
841 * pages are swapped out.
842 *
843 * The function returns after 10 attempts or if no pages
Christoph Lameter418aade2006-02-10 01:51:15 -0800844 * are movable anymore because to has become empty
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800845 * or no retryable pages exist anymore.
846 *
Christoph Lameterd0d96322006-01-08 01:00:55 -0800847 * Return: Number of pages not migrated when "to" ran empty.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800848 */
Andrew Morton69e05942006-03-22 00:08:19 -0800849unsigned long migrate_pages(struct list_head *from, struct list_head *to,
Christoph Lameterd4984712006-01-08 01:00:55 -0800850 struct list_head *moved, struct list_head *failed)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800851{
Andrew Morton69e05942006-03-22 00:08:19 -0800852 unsigned long retry;
853 unsigned long nr_failed = 0;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800854 int pass = 0;
855 struct page *page;
856 struct page *page2;
857 int swapwrite = current->flags & PF_SWAPWRITE;
Christoph Lameterd0d96322006-01-08 01:00:55 -0800858 int rc;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800859
860 if (!swapwrite)
861 current->flags |= PF_SWAPWRITE;
862
863redo:
864 retry = 0;
865
Christoph Lameterd4984712006-01-08 01:00:55 -0800866 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lametera48d07a2006-02-01 03:05:38 -0800867 struct page *newpage = NULL;
868 struct address_space *mapping;
869
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800870 cond_resched();
871
Christoph Lameterd0d96322006-01-08 01:00:55 -0800872 rc = 0;
873 if (page_count(page) == 1)
Christoph Lameteree274972006-01-08 01:00:54 -0800874 /* page was freed from under us. So we are done. */
Christoph Lameterd0d96322006-01-08 01:00:55 -0800875 goto next;
876
Christoph Lametera48d07a2006-02-01 03:05:38 -0800877 if (to && list_empty(to))
878 break;
879
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800880 /*
881 * Skip locked pages during the first two passes to give the
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800882 * functions holding the lock time to release the page. Later we
883 * use lock_page() to have a higher chance of acquiring the
884 * lock.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800885 */
Christoph Lameterd0d96322006-01-08 01:00:55 -0800886 rc = -EAGAIN;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800887 if (pass > 2)
888 lock_page(page);
889 else
890 if (TestSetPageLocked(page))
Christoph Lameterd0d96322006-01-08 01:00:55 -0800891 goto next;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800892
893 /*
894 * Only wait on writeback if we have already done a pass where
895 * we we may have triggered writeouts for lots of pages.
896 */
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800897 if (pass > 0) {
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800898 wait_on_page_writeback(page);
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800899 } else {
Christoph Lameterd0d96322006-01-08 01:00:55 -0800900 if (PageWriteback(page))
901 goto unlock_page;
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800902 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800903
Christoph Lameterd0d96322006-01-08 01:00:55 -0800904 /*
905 * Anonymous pages must have swap cache references otherwise
906 * the information contained in the page maps cannot be
907 * preserved.
908 */
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800909 if (PageAnon(page) && !PageSwapCache(page)) {
Christoph Lameter1480a542006-01-08 01:00:53 -0800910 if (!add_to_swap(page, GFP_KERNEL)) {
Christoph Lameterd0d96322006-01-08 01:00:55 -0800911 rc = -ENOMEM;
912 goto unlock_page;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800913 }
914 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800915
Christoph Lametera48d07a2006-02-01 03:05:38 -0800916 if (!to) {
917 rc = swap_page(page);
918 goto next;
919 }
920
921 newpage = lru_to_page(to);
922 lock_page(newpage);
923
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800924 /*
Christoph Lametera48d07a2006-02-01 03:05:38 -0800925 * Pages are properly locked and writeback is complete.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800926 * Try to migrate the page.
927 */
Christoph Lametera48d07a2006-02-01 03:05:38 -0800928 mapping = page_mapping(page);
929 if (!mapping)
930 goto unlock_both;
931
Christoph Lametere965f962006-02-01 03:05:41 -0800932 if (mapping->a_ops->migratepage) {
Christoph Lameter418aade2006-02-10 01:51:15 -0800933 /*
934 * Most pages have a mapping and most filesystems
935 * should provide a migration function. Anonymous
936 * pages are part of swap space which also has its
937 * own migration function. This is the most common
938 * path for page migration.
939 */
Christoph Lametere965f962006-02-01 03:05:41 -0800940 rc = mapping->a_ops->migratepage(newpage, page);
941 goto unlock_both;
942 }
943
Christoph Lametera48d07a2006-02-01 03:05:38 -0800944 /*
Christoph Lameter418aade2006-02-10 01:51:15 -0800945 * Default handling if a filesystem does not provide
946 * a migration function. We can only migrate clean
947 * pages so try to write out any dirty pages first.
Christoph Lametera48d07a2006-02-01 03:05:38 -0800948 */
949 if (PageDirty(page)) {
950 switch (pageout(page, mapping)) {
951 case PAGE_KEEP:
952 case PAGE_ACTIVATE:
953 goto unlock_both;
954
955 case PAGE_SUCCESS:
956 unlock_page(newpage);
957 goto next;
958
959 case PAGE_CLEAN:
960 ; /* try to migrate the page below */
961 }
962 }
Christoph Lameter418aade2006-02-10 01:51:15 -0800963
Christoph Lametera48d07a2006-02-01 03:05:38 -0800964 /*
Christoph Lameter418aade2006-02-10 01:51:15 -0800965 * Buffers are managed in a filesystem specific way.
966 * We must have no buffers or drop them.
Christoph Lametera48d07a2006-02-01 03:05:38 -0800967 */
968 if (!page_has_buffers(page) ||
969 try_to_release_page(page, GFP_KERNEL)) {
970 rc = migrate_page(newpage, page);
971 goto unlock_both;
972 }
973
974 /*
975 * On early passes with mapped pages simply
976 * retry. There may be a lock held for some
977 * buffers that may go away. Later
978 * swap them out.
979 */
980 if (pass > 4) {
Christoph Lameter418aade2006-02-10 01:51:15 -0800981 /*
982 * Persistently unable to drop buffers..... As a
983 * measure of last resort we fall back to
984 * swap_page().
985 */
Christoph Lametera48d07a2006-02-01 03:05:38 -0800986 unlock_page(newpage);
987 newpage = NULL;
988 rc = swap_page(page);
989 goto next;
990 }
991
992unlock_both:
993 unlock_page(newpage);
Christoph Lameterd0d96322006-01-08 01:00:55 -0800994
995unlock_page:
996 unlock_page(page);
997
998next:
999 if (rc == -EAGAIN) {
1000 retry++;
1001 } else if (rc) {
1002 /* Permanent failure */
1003 list_move(&page->lru, failed);
1004 nr_failed++;
1005 } else {
Christoph Lametera48d07a2006-02-01 03:05:38 -08001006 if (newpage) {
1007 /* Successful migration. Return page to LRU */
1008 move_to_lru(newpage);
1009 }
Christoph Lameterd4984712006-01-08 01:00:55 -08001010 list_move(&page->lru, moved);
Christoph Lameterd4984712006-01-08 01:00:55 -08001011 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001012 }
1013 if (retry && pass++ < 10)
1014 goto redo;
1015
1016 if (!swapwrite)
1017 current->flags &= ~PF_SWAPWRITE;
1018
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001019 return nr_failed + retry;
1020}
Christoph Lameter8419c312006-01-08 01:00:52 -08001021
Christoph Lameter8419c312006-01-08 01:00:52 -08001022/*
1023 * Isolate one page from the LRU lists and put it on the
Nick Piggin053837f2006-01-18 17:42:27 -08001024 * indicated list with elevated refcount.
Christoph Lameter8419c312006-01-08 01:00:52 -08001025 *
1026 * Result:
1027 * 0 = page not on LRU list
1028 * 1 = page removed from LRU list and added to the specified list.
Christoph Lameter8419c312006-01-08 01:00:52 -08001029 */
1030int isolate_lru_page(struct page *page)
1031{
Nick Piggin053837f2006-01-18 17:42:27 -08001032 int ret = 0;
Christoph Lameter8419c312006-01-08 01:00:52 -08001033
Nick Piggin053837f2006-01-18 17:42:27 -08001034 if (PageLRU(page)) {
1035 struct zone *zone = page_zone(page);
1036 spin_lock_irq(&zone->lru_lock);
Nick Piggin8d438f92006-03-22 00:07:59 -08001037 if (PageLRU(page)) {
Nick Piggin053837f2006-01-18 17:42:27 -08001038 ret = 1;
1039 get_page(page);
Nick Piggin8d438f92006-03-22 00:07:59 -08001040 ClearPageLRU(page);
Nick Piggin053837f2006-01-18 17:42:27 -08001041 if (PageActive(page))
1042 del_page_from_active_list(zone, page);
1043 else
1044 del_page_from_inactive_list(zone, page);
1045 }
1046 spin_unlock_irq(&zone->lru_lock);
Christoph Lameter8419c312006-01-08 01:00:52 -08001047 }
Nick Piggin053837f2006-01-18 17:42:27 -08001048
1049 return ret;
Christoph Lameter8419c312006-01-08 01:00:52 -08001050}
Christoph Lameter7cbe34c2006-01-08 01:00:49 -08001051#endif
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001052
1053/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 * zone->lru_lock is heavily contended. Some of the functions that
1055 * shrink the lists perform better by taking out a batch of pages
1056 * and working on them outside the LRU lock.
1057 *
1058 * For pagecache intensive workloads, this function is the hottest
1059 * spot in the kernel (apart from copy_*_user functions).
1060 *
1061 * Appropriate locks must be held before calling this function.
1062 *
1063 * @nr_to_scan: The number of pages to look through on the list.
1064 * @src: The LRU list to pull pages off.
1065 * @dst: The temp list to put pages on to.
1066 * @scanned: The number of pages that were scanned.
1067 *
1068 * returns how many pages were moved onto *@dst.
1069 */
Andrew Morton69e05942006-03-22 00:08:19 -08001070static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
1071 struct list_head *src, struct list_head *dst,
1072 unsigned long *scanned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Andrew Morton69e05942006-03-22 00:08:19 -08001074 unsigned long nr_taken = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 struct page *page;
Andrew Morton69e05942006-03-22 00:08:19 -08001076 unsigned long scan = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 while (scan++ < nr_to_scan && !list_empty(src)) {
Nick Piggin7c8ee9a2006-03-22 00:08:03 -08001079 struct list_head *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 page = lru_to_page(src);
1081 prefetchw_prev_lru_page(page, src, flags);
1082
Nick Piggin8d438f92006-03-22 00:07:59 -08001083 BUG_ON(!PageLRU(page));
1084
Nick Piggin053837f2006-01-18 17:42:27 -08001085 list_del(&page->lru);
Nick Piggin7c8ee9a2006-03-22 00:08:03 -08001086 target = src;
1087 if (likely(get_page_unless_zero(page))) {
Nick Piggin053837f2006-01-18 17:42:27 -08001088 /*
Nick Piggin7c8ee9a2006-03-22 00:08:03 -08001089 * Be careful not to clear PageLRU until after we're
1090 * sure the page is not being freed elsewhere -- the
1091 * page release code relies on it.
Nick Piggin053837f2006-01-18 17:42:27 -08001092 */
Nick Piggin7c8ee9a2006-03-22 00:08:03 -08001093 ClearPageLRU(page);
1094 target = dst;
1095 nr_taken++;
1096 } /* else it is being freed elsewhere */
Nick Piggin46453a62006-03-22 00:07:58 -08001097
Nick Piggin7c8ee9a2006-03-22 00:08:03 -08001098 list_add(&page->lru, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100
1101 *scanned = scan;
1102 return nr_taken;
1103}
1104
1105/*
Andrew Morton05ff5132006-03-22 00:08:20 -08001106 * shrink_cache() return the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 */
Andrew Morton05ff5132006-03-22 00:08:20 -08001108static unsigned long shrink_cache(unsigned long max_scan, struct zone *zone,
1109 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 LIST_HEAD(page_list);
1112 struct pagevec pvec;
Andrew Morton69e05942006-03-22 00:08:19 -08001113 unsigned long nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001114 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 pagevec_init(&pvec, 1);
1117
1118 lru_add_drain();
1119 spin_lock_irq(&zone->lru_lock);
Andrew Morton69e05942006-03-22 00:08:19 -08001120 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 struct page *page;
Andrew Morton69e05942006-03-22 00:08:19 -08001122 unsigned long nr_taken;
1123 unsigned long nr_scan;
1124 unsigned long nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
1127 &zone->inactive_list,
1128 &page_list, &nr_scan);
1129 zone->nr_inactive -= nr_taken;
1130 zone->pages_scanned += nr_scan;
1131 spin_unlock_irq(&zone->lru_lock);
1132
1133 if (nr_taken == 0)
1134 goto done;
1135
Andrew Morton69e05942006-03-22 00:08:19 -08001136 nr_scanned += nr_scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 nr_freed = shrink_list(&page_list, sc);
Andrew Morton05ff5132006-03-22 00:08:20 -08001138 nr_reclaimed += nr_freed;
Nick Piggina74609f2006-01-06 00:11:20 -08001139 local_irq_disable();
1140 if (current_is_kswapd()) {
1141 __mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
1142 __mod_page_state(kswapd_steal, nr_freed);
1143 } else
1144 __mod_page_state_zone(zone, pgscan_direct, nr_scan);
1145 __mod_page_state_zone(zone, pgsteal, nr_freed);
1146
1147 spin_lock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /*
1149 * Put back any unfreeable pages.
1150 */
1151 while (!list_empty(&page_list)) {
1152 page = lru_to_page(&page_list);
Nick Piggin8d438f92006-03-22 00:07:59 -08001153 BUG_ON(PageLRU(page));
1154 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 list_del(&page->lru);
1156 if (PageActive(page))
1157 add_page_to_active_list(zone, page);
1158 else
1159 add_page_to_inactive_list(zone, page);
1160 if (!pagevec_add(&pvec, page)) {
1161 spin_unlock_irq(&zone->lru_lock);
1162 __pagevec_release(&pvec);
1163 spin_lock_irq(&zone->lru_lock);
1164 }
1165 }
Andrew Morton69e05942006-03-22 00:08:19 -08001166 } while (nr_scanned < max_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 spin_unlock_irq(&zone->lru_lock);
1168done:
1169 pagevec_release(&pvec);
Andrew Morton05ff5132006-03-22 00:08:20 -08001170 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
1173/*
1174 * This moves pages from the active list to the inactive list.
1175 *
1176 * We move them the other way if the page is referenced by one or more
1177 * processes, from rmap.
1178 *
1179 * If the pages are mostly unmapped, the processing is fast and it is
1180 * appropriate to hold zone->lru_lock across the whole operation. But if
1181 * the pages are mapped, the processing is slow (page_referenced()) so we
1182 * should drop zone->lru_lock around each page. It's impossible to balance
1183 * this, so instead we remove the pages from the LRU while processing them.
1184 * It is safe to rely on PG_active against the non-LRU pages in here because
1185 * nobody will play with that bit on a non-LRU page.
1186 *
1187 * The downside is that we have to touch page->_count against each page.
1188 * But we had to alter page->flags anyway.
1189 */
1190static void
Andrew Morton69e05942006-03-22 00:08:19 -08001191refill_inactive_zone(unsigned long nr_pages, struct zone *zone,
1192 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Andrew Morton69e05942006-03-22 00:08:19 -08001194 unsigned long pgmoved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 int pgdeactivate = 0;
Andrew Morton69e05942006-03-22 00:08:19 -08001196 unsigned long pgscanned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 LIST_HEAD(l_hold); /* The pages which were snipped off */
1198 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
1199 LIST_HEAD(l_active); /* Pages to go onto the active_list */
1200 struct page *page;
1201 struct pagevec pvec;
1202 int reclaim_mapped = 0;
Christoph Lameter2903fb12006-02-11 17:55:55 -08001203
1204 if (unlikely(sc->may_swap)) {
1205 long mapped_ratio;
1206 long distress;
1207 long swap_tendency;
1208
1209 /*
1210 * `distress' is a measure of how much trouble we're having
1211 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
1212 */
1213 distress = 100 >> zone->prev_priority;
1214
1215 /*
1216 * The point of this algorithm is to decide when to start
1217 * reclaiming mapped memory instead of just pagecache. Work out
1218 * how much memory
1219 * is mapped.
1220 */
1221 mapped_ratio = (sc->nr_mapped * 100) / total_memory;
1222
1223 /*
1224 * Now decide how much we really want to unmap some pages. The
1225 * mapped ratio is downgraded - just because there's a lot of
1226 * mapped memory doesn't necessarily mean that page reclaim
1227 * isn't succeeding.
1228 *
1229 * The distress ratio is important - we don't want to start
1230 * going oom.
1231 *
1232 * A 100% value of vm_swappiness overrides this algorithm
1233 * altogether.
1234 */
1235 swap_tendency = mapped_ratio / 2 + distress + vm_swappiness;
1236
1237 /*
1238 * Now use this metric to decide whether to start moving mapped
1239 * memory onto the inactive list.
1240 */
1241 if (swap_tendency >= 100)
1242 reclaim_mapped = 1;
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 lru_add_drain();
1246 spin_lock_irq(&zone->lru_lock);
1247 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
1248 &l_hold, &pgscanned);
1249 zone->pages_scanned += pgscanned;
1250 zone->nr_active -= pgmoved;
1251 spin_unlock_irq(&zone->lru_lock);
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 while (!list_empty(&l_hold)) {
1254 cond_resched();
1255 page = lru_to_page(&l_hold);
1256 list_del(&page->lru);
1257 if (page_mapped(page)) {
1258 if (!reclaim_mapped ||
1259 (total_swap_pages == 0 && PageAnon(page)) ||
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001260 page_referenced(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 list_add(&page->lru, &l_active);
1262 continue;
1263 }
1264 }
1265 list_add(&page->lru, &l_inactive);
1266 }
1267
1268 pagevec_init(&pvec, 1);
1269 pgmoved = 0;
1270 spin_lock_irq(&zone->lru_lock);
1271 while (!list_empty(&l_inactive)) {
1272 page = lru_to_page(&l_inactive);
1273 prefetchw_prev_lru_page(page, &l_inactive, flags);
Nick Piggin8d438f92006-03-22 00:07:59 -08001274 BUG_ON(PageLRU(page));
1275 SetPageLRU(page);
Nick Piggin4c84cac2006-03-22 00:08:00 -08001276 BUG_ON(!PageActive(page));
1277 ClearPageActive(page);
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 list_move(&page->lru, &zone->inactive_list);
1280 pgmoved++;
1281 if (!pagevec_add(&pvec, page)) {
1282 zone->nr_inactive += pgmoved;
1283 spin_unlock_irq(&zone->lru_lock);
1284 pgdeactivate += pgmoved;
1285 pgmoved = 0;
1286 if (buffer_heads_over_limit)
1287 pagevec_strip(&pvec);
1288 __pagevec_release(&pvec);
1289 spin_lock_irq(&zone->lru_lock);
1290 }
1291 }
1292 zone->nr_inactive += pgmoved;
1293 pgdeactivate += pgmoved;
1294 if (buffer_heads_over_limit) {
1295 spin_unlock_irq(&zone->lru_lock);
1296 pagevec_strip(&pvec);
1297 spin_lock_irq(&zone->lru_lock);
1298 }
1299
1300 pgmoved = 0;
1301 while (!list_empty(&l_active)) {
1302 page = lru_to_page(&l_active);
1303 prefetchw_prev_lru_page(page, &l_active, flags);
Nick Piggin8d438f92006-03-22 00:07:59 -08001304 BUG_ON(PageLRU(page));
1305 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 BUG_ON(!PageActive(page));
1307 list_move(&page->lru, &zone->active_list);
1308 pgmoved++;
1309 if (!pagevec_add(&pvec, page)) {
1310 zone->nr_active += pgmoved;
1311 pgmoved = 0;
1312 spin_unlock_irq(&zone->lru_lock);
1313 __pagevec_release(&pvec);
1314 spin_lock_irq(&zone->lru_lock);
1315 }
1316 }
1317 zone->nr_active += pgmoved;
Nick Piggina74609f2006-01-06 00:11:20 -08001318 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Nick Piggina74609f2006-01-06 00:11:20 -08001320 __mod_page_state_zone(zone, pgrefill, pgscanned);
1321 __mod_page_state(pgdeactivate, pgdeactivate);
1322 local_irq_enable();
1323
1324 pagevec_release(&pvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
1327/*
1328 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
1329 */
Andrew Morton05ff5132006-03-22 00:08:20 -08001330static unsigned long shrink_zone(int priority, struct zone *zone,
1331 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
1333 unsigned long nr_active;
1334 unsigned long nr_inactive;
Christoph Lameter86959492006-03-22 00:08:18 -08001335 unsigned long nr_to_scan;
Andrew Morton05ff5132006-03-22 00:08:20 -08001336 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Martin Hicks53e9a612005-09-03 15:54:51 -07001338 atomic_inc(&zone->reclaim_in_progress);
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 /*
1341 * Add one to `nr_to_scan' just to make sure that the kernel will
1342 * slowly sift through the active list.
1343 */
Christoph Lameter86959492006-03-22 00:08:18 -08001344 zone->nr_scan_active += (zone->nr_active >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 nr_active = zone->nr_scan_active;
1346 if (nr_active >= sc->swap_cluster_max)
1347 zone->nr_scan_active = 0;
1348 else
1349 nr_active = 0;
1350
Christoph Lameter86959492006-03-22 00:08:18 -08001351 zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 nr_inactive = zone->nr_scan_inactive;
1353 if (nr_inactive >= sc->swap_cluster_max)
1354 zone->nr_scan_inactive = 0;
1355 else
1356 nr_inactive = 0;
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 while (nr_active || nr_inactive) {
1359 if (nr_active) {
Christoph Lameter86959492006-03-22 00:08:18 -08001360 nr_to_scan = min(nr_active,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -08001362 nr_active -= nr_to_scan;
1363 refill_inactive_zone(nr_to_scan, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365
1366 if (nr_inactive) {
Christoph Lameter86959492006-03-22 00:08:18 -08001367 nr_to_scan = min(nr_inactive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -08001369 nr_inactive -= nr_to_scan;
Andrew Morton05ff5132006-03-22 00:08:20 -08001370 nr_reclaimed += shrink_cache(nr_to_scan, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372 }
1373
1374 throttle_vm_writeout();
Martin Hicks53e9a612005-09-03 15:54:51 -07001375
1376 atomic_dec(&zone->reclaim_in_progress);
Andrew Morton05ff5132006-03-22 00:08:20 -08001377 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
1380/*
1381 * This is the direct reclaim path, for page-allocating processes. We only
1382 * try to reclaim pages from zones which will satisfy the caller's allocation
1383 * request.
1384 *
1385 * We reclaim from a zone even if that zone is over pages_high. Because:
1386 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
1387 * allocation or
1388 * b) The zones may be over pages_high but they must go *over* pages_high to
1389 * satisfy the `incremental min' zone defense algorithm.
1390 *
1391 * Returns the number of reclaimed pages.
1392 *
1393 * If a zone is deemed to be full of pinned pages then just give it a light
1394 * scan then give up on it.
1395 */
Andrew Morton05ff5132006-03-22 00:08:20 -08001396static unsigned long shrink_caches(int priority, struct zone **zones,
1397 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Andrew Morton05ff5132006-03-22 00:08:20 -08001399 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 int i;
1401
1402 for (i = 0; zones[i] != NULL; i++) {
1403 struct zone *zone = zones[i];
1404
Con Kolivasf3fe6512006-01-06 00:11:15 -08001405 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 continue;
1407
Paul Jackson9bf22292005-09-06 15:18:12 -07001408 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 continue;
1410
Christoph Lameter86959492006-03-22 00:08:18 -08001411 zone->temp_priority = priority;
1412 if (zone->prev_priority > priority)
1413 zone->prev_priority = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Christoph Lameter86959492006-03-22 00:08:18 -08001415 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 continue; /* Let kswapd poll it */
1417
Andrew Morton05ff5132006-03-22 00:08:20 -08001418 nr_reclaimed += shrink_zone(priority, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
Andrew Morton05ff5132006-03-22 00:08:20 -08001420 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
1423/*
1424 * This is the main entry point to direct page reclaim.
1425 *
1426 * If a full scan of the inactive list fails to free enough memory then we
1427 * are "out of memory" and something needs to be killed.
1428 *
1429 * If the caller is !__GFP_FS then the probability of a failure is reasonably
1430 * high - the zone may be full of dirty or under-writeback pages, which this
1431 * caller can't do much about. We kick pdflush and take explicit naps in the
1432 * hope that some of these pages can be written. But if the allocating task
1433 * holds filesystem locks which prevent writeout this might not work, and the
1434 * allocation attempt will fail.
1435 */
Andrew Morton69e05942006-03-22 00:08:19 -08001436unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 int priority;
1439 int ret = 0;
Andrew Morton69e05942006-03-22 00:08:19 -08001440 unsigned long total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001441 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 struct reclaim_state *reclaim_state = current->reclaim_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 unsigned long lru_pages = 0;
1444 int i;
Andrew Morton179e9632006-03-22 00:08:18 -08001445 struct scan_control sc = {
1446 .gfp_mask = gfp_mask,
1447 .may_writepage = !laptop_mode,
1448 .swap_cluster_max = SWAP_CLUSTER_MAX,
1449 .may_swap = 1,
1450 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 inc_page_state(allocstall);
1453
1454 for (i = 0; zones[i] != NULL; i++) {
1455 struct zone *zone = zones[i];
1456
Paul Jackson9bf22292005-09-06 15:18:12 -07001457 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 continue;
1459
1460 zone->temp_priority = DEF_PRIORITY;
1461 lru_pages += zone->nr_active + zone->nr_inactive;
1462 }
1463
1464 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1465 sc.nr_mapped = read_page_state(nr_mapped);
1466 sc.nr_scanned = 0;
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001467 if (!priority)
1468 disable_swap_token();
Andrew Morton05ff5132006-03-22 00:08:20 -08001469 nr_reclaimed += shrink_caches(priority, zones, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
1471 if (reclaim_state) {
Andrew Morton05ff5132006-03-22 00:08:20 -08001472 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 reclaim_state->reclaimed_slab = 0;
1474 }
1475 total_scanned += sc.nr_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001476 if (nr_reclaimed >= sc.swap_cluster_max) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 ret = 1;
1478 goto out;
1479 }
1480
1481 /*
1482 * Try to write back as many pages as we just scanned. This
1483 * tends to cause slow streaming writers to write data to the
1484 * disk smoothly, at the dirtying rate, which is nice. But
1485 * that's undesirable in laptop mode, where we *want* lumpy
1486 * writeout. So in laptop mode, write out the whole world.
1487 */
Andrew Morton179e9632006-03-22 00:08:18 -08001488 if (total_scanned > sc.swap_cluster_max +
1489 sc.swap_cluster_max / 2) {
Pekka J Enberg687a21c2005-06-28 20:44:55 -07001490 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 sc.may_writepage = 1;
1492 }
1493
1494 /* Take a nap, wait for some writeback to complete */
1495 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
1496 blk_congestion_wait(WRITE, HZ/10);
1497 }
1498out:
1499 for (i = 0; zones[i] != 0; i++) {
1500 struct zone *zone = zones[i];
1501
Paul Jackson9bf22292005-09-06 15:18:12 -07001502 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 continue;
1504
1505 zone->prev_priority = zone->temp_priority;
1506 }
1507 return ret;
1508}
1509
1510/*
1511 * For kswapd, balance_pgdat() will work across all this node's zones until
1512 * they are all at pages_high.
1513 *
1514 * If `nr_pages' is non-zero then it is the number of pages which are to be
1515 * reclaimed, regardless of the zone occupancies. This is a software suspend
1516 * special.
1517 *
1518 * Returns the number of pages which were actually freed.
1519 *
1520 * There is special handling here for zones which are full of pinned pages.
1521 * This can happen if the pages are all mlocked, or if they are all used by
1522 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1523 * What we do is to detect the case where all pages in the zone have been
1524 * scanned twice and there has been zero successful reclaim. Mark the zone as
1525 * dead and from now on, only perform a short scan. Basically we're polling
1526 * the zone for when the problem goes away.
1527 *
1528 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1529 * zones which have free_pages > pages_high, but once a zone is found to have
1530 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1531 * of the number of free pages in the lower zones. This interoperates with
1532 * the page allocator fallback scheme to ensure that aging of pages is balanced
1533 * across the zones.
1534 */
Andrew Morton69e05942006-03-22 00:08:19 -08001535static unsigned long balance_pgdat(pg_data_t *pgdat, unsigned long nr_pages,
1536 int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Andrew Morton69e05942006-03-22 00:08:19 -08001538 unsigned long to_free = nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 int all_zones_ok;
1540 int priority;
1541 int i;
Andrew Morton69e05942006-03-22 00:08:19 -08001542 unsigned long total_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001543 unsigned long nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 struct reclaim_state *reclaim_state = current->reclaim_state;
Andrew Morton179e9632006-03-22 00:08:18 -08001545 struct scan_control sc = {
1546 .gfp_mask = GFP_KERNEL,
1547 .may_swap = 1,
1548 .swap_cluster_max = nr_pages ? nr_pages : SWAP_CLUSTER_MAX,
1549 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551loop_again:
1552 total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001553 nr_reclaimed = 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001554 sc.may_writepage = !laptop_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 sc.nr_mapped = read_page_state(nr_mapped);
1556
1557 inc_page_state(pageoutrun);
1558
1559 for (i = 0; i < pgdat->nr_zones; i++) {
1560 struct zone *zone = pgdat->node_zones + i;
1561
1562 zone->temp_priority = DEF_PRIORITY;
1563 }
1564
1565 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1566 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1567 unsigned long lru_pages = 0;
1568
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001569 /* The swap token gets in the way of swapout... */
1570 if (!priority)
1571 disable_swap_token();
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 all_zones_ok = 1;
1574
1575 if (nr_pages == 0) {
1576 /*
1577 * Scan in the highmem->dma direction for the highest
1578 * zone which needs scanning
1579 */
1580 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1581 struct zone *zone = pgdat->node_zones + i;
1582
Con Kolivasf3fe6512006-01-06 00:11:15 -08001583 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 continue;
1585
1586 if (zone->all_unreclaimable &&
1587 priority != DEF_PRIORITY)
1588 continue;
1589
1590 if (!zone_watermark_ok(zone, order,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001591 zone->pages_high, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 end_zone = i;
1593 goto scan;
1594 }
1595 }
1596 goto out;
1597 } else {
1598 end_zone = pgdat->nr_zones - 1;
1599 }
1600scan:
1601 for (i = 0; i <= end_zone; i++) {
1602 struct zone *zone = pgdat->node_zones + i;
1603
1604 lru_pages += zone->nr_active + zone->nr_inactive;
1605 }
1606
1607 /*
1608 * Now scan the zone in the dma->highmem direction, stopping
1609 * at the last zone which needs scanning.
1610 *
1611 * We do this because the page allocator works in the opposite
1612 * direction. This prevents the page allocator from allocating
1613 * pages behind kswapd's direction of progress, which would
1614 * cause too much scanning of the lower zones.
1615 */
1616 for (i = 0; i <= end_zone; i++) {
1617 struct zone *zone = pgdat->node_zones + i;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001618 int nr_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Con Kolivasf3fe6512006-01-06 00:11:15 -08001620 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 continue;
1622
1623 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1624 continue;
1625
1626 if (nr_pages == 0) { /* Not software suspend */
1627 if (!zone_watermark_ok(zone, order,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001628 zone->pages_high, end_zone, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 all_zones_ok = 0;
1630 }
1631 zone->temp_priority = priority;
1632 if (zone->prev_priority > priority)
1633 zone->prev_priority = priority;
1634 sc.nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001635 nr_reclaimed += shrink_zone(priority, zone, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 reclaim_state->reclaimed_slab = 0;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001637 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1638 lru_pages);
Andrew Morton05ff5132006-03-22 00:08:20 -08001639 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 total_scanned += sc.nr_scanned;
1641 if (zone->all_unreclaimable)
1642 continue;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001643 if (nr_slab == 0 && zone->pages_scanned >=
1644 (zone->nr_active + zone->nr_inactive) * 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 zone->all_unreclaimable = 1;
1646 /*
1647 * If we've done a decent amount of scanning and
1648 * the reclaim ratio is low, start doing writepage
1649 * even in laptop mode
1650 */
1651 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
Andrew Morton05ff5132006-03-22 00:08:20 -08001652 total_scanned > nr_reclaimed + nr_reclaimed / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 sc.may_writepage = 1;
1654 }
Andrew Morton05ff5132006-03-22 00:08:20 -08001655 if (nr_pages && to_free > nr_reclaimed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 continue; /* swsusp: need to do more work */
1657 if (all_zones_ok)
1658 break; /* kswapd: all done */
1659 /*
1660 * OK, kswapd is getting into trouble. Take a nap, then take
1661 * another pass across the zones.
1662 */
1663 if (total_scanned && priority < DEF_PRIORITY - 2)
1664 blk_congestion_wait(WRITE, HZ/10);
1665
1666 /*
1667 * We do this so kswapd doesn't build up large priorities for
1668 * example when it is freeing in parallel with allocators. It
1669 * matches the direct reclaim path behaviour in terms of impact
1670 * on zone->*_priority.
1671 */
Andrew Morton05ff5132006-03-22 00:08:20 -08001672 if ((nr_reclaimed >= SWAP_CLUSTER_MAX) && !nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 break;
1674 }
1675out:
1676 for (i = 0; i < pgdat->nr_zones; i++) {
1677 struct zone *zone = pgdat->node_zones + i;
1678
1679 zone->prev_priority = zone->temp_priority;
1680 }
1681 if (!all_zones_ok) {
1682 cond_resched();
1683 goto loop_again;
1684 }
1685
Andrew Morton05ff5132006-03-22 00:08:20 -08001686 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
1689/*
1690 * The background pageout daemon, started as a kernel thread
1691 * from the init process.
1692 *
1693 * This basically trickles out pages so that we have _some_
1694 * free memory available even if there is no other activity
1695 * that frees anything up. This is needed for things like routing
1696 * etc, where we otherwise might have all activity going on in
1697 * asynchronous contexts that cannot page things out.
1698 *
1699 * If there are applications that are active memory-allocators
1700 * (most normal use), this basically shouldn't matter.
1701 */
1702static int kswapd(void *p)
1703{
1704 unsigned long order;
1705 pg_data_t *pgdat = (pg_data_t*)p;
1706 struct task_struct *tsk = current;
1707 DEFINE_WAIT(wait);
1708 struct reclaim_state reclaim_state = {
1709 .reclaimed_slab = 0,
1710 };
1711 cpumask_t cpumask;
1712
1713 daemonize("kswapd%d", pgdat->node_id);
1714 cpumask = node_to_cpumask(pgdat->node_id);
1715 if (!cpus_empty(cpumask))
1716 set_cpus_allowed(tsk, cpumask);
1717 current->reclaim_state = &reclaim_state;
1718
1719 /*
1720 * Tell the memory management that we're a "memory allocator",
1721 * and that if we need more memory we should get access to it
1722 * regardless (see "__alloc_pages()"). "kswapd" should
1723 * never get caught in the normal page freeing logic.
1724 *
1725 * (Kswapd normally doesn't need memory anyway, but sometimes
1726 * you need a small amount of memory in order to be able to
1727 * page out something else, and this flag essentially protects
1728 * us from recursively trying to free more memory as we're
1729 * trying to free the first piece of memory in the first place).
1730 */
Christoph Lameter930d9152006-01-08 01:00:47 -08001731 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 order = 0;
1734 for ( ; ; ) {
1735 unsigned long new_order;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001736
1737 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1740 new_order = pgdat->kswapd_max_order;
1741 pgdat->kswapd_max_order = 0;
1742 if (order < new_order) {
1743 /*
1744 * Don't sleep if someone wants a larger 'order'
1745 * allocation
1746 */
1747 order = new_order;
1748 } else {
1749 schedule();
1750 order = pgdat->kswapd_max_order;
1751 }
1752 finish_wait(&pgdat->kswapd_wait, &wait);
1753
1754 balance_pgdat(pgdat, 0, order);
1755 }
1756 return 0;
1757}
1758
1759/*
1760 * A zone is low on free memory, so wake its kswapd task to service it.
1761 */
1762void wakeup_kswapd(struct zone *zone, int order)
1763{
1764 pg_data_t *pgdat;
1765
Con Kolivasf3fe6512006-01-06 00:11:15 -08001766 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return;
1768
1769 pgdat = zone->zone_pgdat;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001770 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 return;
1772 if (pgdat->kswapd_max_order < order)
1773 pgdat->kswapd_max_order = order;
Paul Jackson9bf22292005-09-06 15:18:12 -07001774 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001776 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001778 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
1781#ifdef CONFIG_PM
1782/*
1783 * Try to free `nr_pages' of memory, system-wide. Returns the number of freed
1784 * pages.
1785 */
Andrew Morton69e05942006-03-22 00:08:19 -08001786unsigned long shrink_all_memory(unsigned long nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 pg_data_t *pgdat;
Andrew Morton69e05942006-03-22 00:08:19 -08001789 unsigned long nr_to_free = nr_pages;
1790 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 struct reclaim_state reclaim_state = {
1792 .reclaimed_slab = 0,
1793 };
1794
1795 current->reclaim_state = &reclaim_state;
1796 for_each_pgdat(pgdat) {
Andrew Morton69e05942006-03-22 00:08:19 -08001797 unsigned long freed;
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 freed = balance_pgdat(pgdat, nr_to_free, 0);
1800 ret += freed;
1801 nr_to_free -= freed;
Andrew Morton69e05942006-03-22 00:08:19 -08001802 if ((long)nr_to_free <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 break;
1804 }
1805 current->reclaim_state = NULL;
1806 return ret;
1807}
1808#endif
1809
1810#ifdef CONFIG_HOTPLUG_CPU
1811/* It's optimal to keep kswapds on the same CPUs as their memory, but
1812 not required for correctness. So if the last cpu in a node goes
1813 away, we get changed to run anywhere: as the first one comes back,
1814 restore their cpu bindings. */
1815static int __devinit cpu_callback(struct notifier_block *nfb,
Andrew Morton69e05942006-03-22 00:08:19 -08001816 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 pg_data_t *pgdat;
1819 cpumask_t mask;
1820
1821 if (action == CPU_ONLINE) {
1822 for_each_pgdat(pgdat) {
1823 mask = node_to_cpumask(pgdat->node_id);
1824 if (any_online_cpu(mask) != NR_CPUS)
1825 /* One of our CPUs online: restore mask */
1826 set_cpus_allowed(pgdat->kswapd, mask);
1827 }
1828 }
1829 return NOTIFY_OK;
1830}
1831#endif /* CONFIG_HOTPLUG_CPU */
1832
1833static int __init kswapd_init(void)
1834{
1835 pg_data_t *pgdat;
Andrew Morton69e05942006-03-22 00:08:19 -08001836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 swap_setup();
Andrew Morton69e05942006-03-22 00:08:19 -08001838 for_each_pgdat(pgdat) {
1839 pid_t pid;
1840
1841 pid = kernel_thread(kswapd, pgdat, CLONE_KERNEL);
1842 BUG_ON(pid < 0);
1843 pgdat->kswapd = find_task_by_pid(pid);
1844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 total_memory = nr_free_pagecache_pages();
1846 hotcpu_notifier(cpu_callback, 0);
1847 return 0;
1848}
1849
1850module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001851
1852#ifdef CONFIG_NUMA
1853/*
1854 * Zone reclaim mode
1855 *
1856 * If non-zero call zone_reclaim when the number of free pages falls below
1857 * the watermarks.
1858 *
1859 * In the future we may add flags to the mode. However, the page allocator
1860 * should only have to check that zone_reclaim_mode != 0 before calling
1861 * zone_reclaim().
1862 */
1863int zone_reclaim_mode __read_mostly;
1864
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001865#define RECLAIM_OFF 0
1866#define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1867#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1868#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001869#define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001870
Christoph Lameter9eeff232006-01-18 17:42:31 -08001871/*
1872 * Mininum time between zone reclaim scans
1873 */
Christoph Lameter2a11ff02006-02-01 03:05:33 -08001874int zone_reclaim_interval __read_mostly = 30*HZ;
Christoph Lametera92f7122006-02-01 03:05:32 -08001875
1876/*
1877 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1878 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1879 * a zone.
1880 */
1881#define ZONE_RECLAIM_PRIORITY 4
1882
Christoph Lameter9eeff232006-01-18 17:42:31 -08001883/*
1884 * Try to free up some pages from this zone through reclaim.
1885 */
Andrew Morton179e9632006-03-22 00:08:18 -08001886static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001887{
Andrew Morton69e05942006-03-22 00:08:19 -08001888 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001889 struct task_struct *p = current;
1890 struct reclaim_state reclaim_state;
Christoph Lameter86959492006-03-22 00:08:18 -08001891 int priority;
Andrew Morton05ff5132006-03-22 00:08:20 -08001892 unsigned long nr_reclaimed = 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001893 struct scan_control sc = {
1894 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1895 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
1896 .nr_mapped = read_page_state(nr_mapped),
Andrew Morton69e05942006-03-22 00:08:19 -08001897 .swap_cluster_max = max_t(unsigned long, nr_pages,
1898 SWAP_CLUSTER_MAX),
Andrew Morton179e9632006-03-22 00:08:18 -08001899 .gfp_mask = gfp_mask,
1900 };
Christoph Lameter9eeff232006-01-18 17:42:31 -08001901
1902 disable_swap_token();
Christoph Lameter9eeff232006-01-18 17:42:31 -08001903 cond_resched();
Christoph Lameterd4f77962006-02-24 13:04:22 -08001904 /*
1905 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1906 * and we also need to be able to write out pages for RECLAIM_WRITE
1907 * and RECLAIM_SWAP.
1908 */
1909 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001910 reclaim_state.reclaimed_slab = 0;
1911 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db232006-02-01 03:05:29 -08001912
Christoph Lametera92f7122006-02-01 03:05:32 -08001913 /*
1914 * Free memory by calling shrink zone with increasing priorities
1915 * until we have enough memory freed.
1916 */
Christoph Lameter86959492006-03-22 00:08:18 -08001917 priority = ZONE_RECLAIM_PRIORITY;
Christoph Lametera92f7122006-02-01 03:05:32 -08001918 do {
Andrew Morton05ff5132006-03-22 00:08:20 -08001919 nr_reclaimed += shrink_zone(priority, zone, &sc);
Christoph Lameter86959492006-03-22 00:08:18 -08001920 priority--;
Andrew Morton05ff5132006-03-22 00:08:20 -08001921 } while (priority >= 0 && nr_reclaimed < nr_pages);
Christoph Lameterc84db232006-02-01 03:05:29 -08001922
Andrew Morton05ff5132006-03-22 00:08:20 -08001923 if (nr_reclaimed < nr_pages && (zone_reclaim_mode & RECLAIM_SLAB)) {
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001924 /*
1925 * shrink_slab does not currently allow us to determine
1926 * how many pages were freed in the zone. So we just
1927 * shake the slab and then go offnode for a single allocation.
1928 *
1929 * shrink_slab will free memory on all zones and may take
1930 * a long time.
1931 */
1932 shrink_slab(sc.nr_scanned, gfp_mask, order);
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001933 }
1934
Christoph Lameter9eeff232006-01-18 17:42:31 -08001935 p->reclaim_state = NULL;
Christoph Lameterd4f77962006-02-24 13:04:22 -08001936 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
Christoph Lameter9eeff232006-01-18 17:42:31 -08001937
Andrew Morton05ff5132006-03-22 00:08:20 -08001938 if (nr_reclaimed == 0)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001939 zone->last_unsuccessful_zone_reclaim = jiffies;
1940
Andrew Morton05ff5132006-03-22 00:08:20 -08001941 return nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001942}
Andrew Morton179e9632006-03-22 00:08:18 -08001943
1944int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1945{
1946 cpumask_t mask;
1947 int node_id;
1948
1949 /*
1950 * Do not reclaim if there was a recent unsuccessful attempt at zone
1951 * reclaim. In that case we let allocations go off node for the
1952 * zone_reclaim_interval. Otherwise we would scan for each off-node
1953 * page allocation.
1954 */
1955 if (time_before(jiffies,
1956 zone->last_unsuccessful_zone_reclaim + zone_reclaim_interval))
1957 return 0;
1958
1959 /*
1960 * Avoid concurrent zone reclaims, do not reclaim in a zone that does
1961 * not have reclaimable pages and if we should not delay the allocation
1962 * then do not scan.
1963 */
1964 if (!(gfp_mask & __GFP_WAIT) ||
1965 zone->all_unreclaimable ||
1966 atomic_read(&zone->reclaim_in_progress) > 0 ||
1967 (current->flags & PF_MEMALLOC))
1968 return 0;
1969
1970 /*
1971 * Only run zone reclaim on the local zone or on zones that do not
1972 * have associated processors. This will favor the local processor
1973 * over remote processors and spread off node memory allocations
1974 * as wide as possible.
1975 */
1976 node_id = zone->zone_pgdat->node_id;
1977 mask = node_to_cpumask(node_id);
1978 if (!cpus_empty(mask) && node_id != numa_node_id())
1979 return 0;
1980 return __zone_reclaim(zone, gfp_mask, order);
1981}
Christoph Lameter9eeff232006-01-18 17:42:31 -08001982#endif