blob: 548e023c193b0ae52d94f2d8e4dc753634204ec0 [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>
Rafael J. Wysocki248a0302006-03-22 00:09:04 -080036#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/tlbflush.h>
39#include <asm/div64.h>
40
41#include <linux/swapops.h>
42
Nick Piggin0f8053a2006-03-22 00:08:33 -080043#include "internal.h"
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/* possible outcome of pageout() */
46typedef enum {
47 /* failed to write page out, page is locked */
48 PAGE_KEEP,
49 /* move page to the active list, page is locked */
50 PAGE_ACTIVATE,
51 /* page has been sent to the disk successfully, page is unlocked */
52 PAGE_SUCCESS,
53 /* page is clean and locked */
54 PAGE_CLEAN,
55} pageout_t;
56
57struct scan_control {
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 /* Incremented by the number of inactive pages that were scanned */
59 unsigned long nr_scanned;
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 unsigned long nr_mapped; /* From page_state */
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 /* This context's GFP mask */
Al Viro6daa0e22005-10-21 03:18:50 -040064 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 int may_writepage;
67
Christoph Lameterf1fd1062006-01-18 17:42:30 -080068 /* Can pages be swapped as part of reclaim? */
69 int may_swap;
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
72 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
73 * In this context, it doesn't matter that we scan the
74 * whole list at once. */
75 int swap_cluster_max;
76};
77
78/*
79 * The list of shrinker callbacks used by to apply pressure to
80 * ageable caches.
81 */
82struct shrinker {
83 shrinker_t shrinker;
84 struct list_head list;
85 int seeks; /* seeks to recreate an obj */
86 long nr; /* objs pending delete */
87};
88
89#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
90
91#ifdef ARCH_HAS_PREFETCH
92#define prefetch_prev_lru_page(_page, _base, _field) \
93 do { \
94 if ((_page)->lru.prev != _base) { \
95 struct page *prev; \
96 \
97 prev = lru_to_page(&(_page->lru)); \
98 prefetch(&prev->_field); \
99 } \
100 } while (0)
101#else
102#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
103#endif
104
105#ifdef ARCH_HAS_PREFETCHW
106#define prefetchw_prev_lru_page(_page, _base, _field) \
107 do { \
108 if ((_page)->lru.prev != _base) { \
109 struct page *prev; \
110 \
111 prev = lru_to_page(&(_page->lru)); \
112 prefetchw(&prev->_field); \
113 } \
114 } while (0)
115#else
116#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
117#endif
118
119/*
120 * From 0 .. 100. Higher means more swappy.
121 */
122int vm_swappiness = 60;
123static long total_memory;
124
125static LIST_HEAD(shrinker_list);
126static DECLARE_RWSEM(shrinker_rwsem);
127
128/*
129 * Add a shrinker callback to be called from the vm
130 */
131struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
132{
133 struct shrinker *shrinker;
134
135 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
136 if (shrinker) {
137 shrinker->shrinker = theshrinker;
138 shrinker->seeks = seeks;
139 shrinker->nr = 0;
140 down_write(&shrinker_rwsem);
141 list_add_tail(&shrinker->list, &shrinker_list);
142 up_write(&shrinker_rwsem);
143 }
144 return shrinker;
145}
146EXPORT_SYMBOL(set_shrinker);
147
148/*
149 * Remove one
150 */
151void remove_shrinker(struct shrinker *shrinker)
152{
153 down_write(&shrinker_rwsem);
154 list_del(&shrinker->list);
155 up_write(&shrinker_rwsem);
156 kfree(shrinker);
157}
158EXPORT_SYMBOL(remove_shrinker);
159
160#define SHRINK_BATCH 128
161/*
162 * Call the shrink functions to age shrinkable caches
163 *
164 * Here we assume it costs one seek to replace a lru page and that it also
165 * takes a seek to recreate a cache object. With this in mind we age equal
166 * percentages of the lru and ageable caches. This should balance the seeks
167 * generated by these structures.
168 *
169 * If the vm encounted mapped pages on the LRU it increase the pressure on
170 * slab to avoid swapping.
171 *
172 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
173 *
174 * `lru_pages' represents the number of on-LRU pages in all the zones which
175 * are eligible for the caller's allocation attempt. It is used for balancing
176 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700177 *
178 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 */
Andrew Morton69e05942006-03-22 00:08:19 -0800180unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
181 unsigned long lru_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 struct shrinker *shrinker;
Andrew Morton69e05942006-03-22 00:08:19 -0800184 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 if (scanned == 0)
187 scanned = SWAP_CLUSTER_MAX;
188
189 if (!down_read_trylock(&shrinker_rwsem))
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700190 return 1; /* Assume we'll be able to shrink next time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 list_for_each_entry(shrinker, &shrinker_list, list) {
193 unsigned long long delta;
194 unsigned long total_scan;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800195 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 delta = (4 * scanned) / shrinker->seeks;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800198 delta *= max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 do_div(delta, lru_pages + 1);
200 shrinker->nr += delta;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800201 if (shrinker->nr < 0) {
202 printk(KERN_ERR "%s: nr=%ld\n",
203 __FUNCTION__, shrinker->nr);
204 shrinker->nr = max_pass;
205 }
206
207 /*
208 * Avoid risking looping forever due to too large nr value:
209 * never try to free more than twice the estimate number of
210 * freeable entries.
211 */
212 if (shrinker->nr > max_pass * 2)
213 shrinker->nr = max_pass * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 total_scan = shrinker->nr;
216 shrinker->nr = 0;
217
218 while (total_scan >= SHRINK_BATCH) {
219 long this_scan = SHRINK_BATCH;
220 int shrink_ret;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700221 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700223 nr_before = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
225 if (shrink_ret == -1)
226 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700227 if (shrink_ret < nr_before)
228 ret += nr_before - shrink_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 mod_page_state(slabs_scanned, this_scan);
230 total_scan -= this_scan;
231
232 cond_resched();
233 }
234
235 shrinker->nr += total_scan;
236 }
237 up_read(&shrinker_rwsem);
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700238 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241/* Called without lock on whether page is mapped, so answer is unstable */
242static inline int page_mapping_inuse(struct page *page)
243{
244 struct address_space *mapping;
245
246 /* Page is in somebody's page tables. */
247 if (page_mapped(page))
248 return 1;
249
250 /* Be more reluctant to reclaim swapcache than pagecache */
251 if (PageSwapCache(page))
252 return 1;
253
254 mapping = page_mapping(page);
255 if (!mapping)
256 return 0;
257
258 /* File is mmap'd by somebody? */
259 return mapping_mapped(mapping);
260}
261
262static inline int is_page_cache_freeable(struct page *page)
263{
264 return page_count(page) - !!PagePrivate(page) == 2;
265}
266
267static int may_write_to_queue(struct backing_dev_info *bdi)
268{
Christoph Lameter930d9152006-01-08 01:00:47 -0800269 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return 1;
271 if (!bdi_write_congested(bdi))
272 return 1;
273 if (bdi == current->backing_dev_info)
274 return 1;
275 return 0;
276}
277
278/*
279 * We detected a synchronous write error writing a page out. Probably
280 * -ENOSPC. We need to propagate that into the address_space for a subsequent
281 * fsync(), msync() or close().
282 *
283 * The tricky part is that after writepage we cannot touch the mapping: nothing
284 * prevents it from being freed up. But we have a ref on the page and once
285 * that page is locked, the mapping is pinned.
286 *
287 * We're allowed to run sleeping lock_page() here because we know the caller has
288 * __GFP_FS.
289 */
290static void handle_write_error(struct address_space *mapping,
291 struct page *page, int error)
292{
293 lock_page(page);
294 if (page_mapping(page) == mapping) {
295 if (error == -ENOSPC)
296 set_bit(AS_ENOSPC, &mapping->flags);
297 else
298 set_bit(AS_EIO, &mapping->flags);
299 }
300 unlock_page(page);
301}
302
303/*
Andrew Morton1742f192006-03-22 00:08:21 -0800304 * pageout is called by shrink_page_list() for each dirty page.
305 * Calls ->writepage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 */
307static pageout_t pageout(struct page *page, struct address_space *mapping)
308{
309 /*
310 * If the page is dirty, only perform writeback if that write
311 * will be non-blocking. To prevent this allocation from being
312 * stalled by pagecache activity. But note that there may be
313 * stalls if we need to run get_block(). We could test
314 * PagePrivate for that.
315 *
316 * If this process is currently in generic_file_write() against
317 * this page's queue, we can perform writeback even if that
318 * will block.
319 *
320 * If the page is swapcache, write it back even if that would
321 * block, for some throttling. This happens by accident, because
322 * swap_backing_dev_info is bust: it doesn't reflect the
323 * congestion state of the swapdevs. Easy to fix, if needed.
324 * See swapfile.c:page_queue_congested().
325 */
326 if (!is_page_cache_freeable(page))
327 return PAGE_KEEP;
328 if (!mapping) {
329 /*
330 * Some data journaling orphaned pages can have
331 * page->mapping == NULL while being dirty with clean buffers.
332 */
akpm@osdl.org323aca62005-04-16 15:24:06 -0700333 if (PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (try_to_free_buffers(page)) {
335 ClearPageDirty(page);
336 printk("%s: orphaned page\n", __FUNCTION__);
337 return PAGE_CLEAN;
338 }
339 }
340 return PAGE_KEEP;
341 }
342 if (mapping->a_ops->writepage == NULL)
343 return PAGE_ACTIVATE;
344 if (!may_write_to_queue(mapping->backing_dev_info))
345 return PAGE_KEEP;
346
347 if (clear_page_dirty_for_io(page)) {
348 int res;
349 struct writeback_control wbc = {
350 .sync_mode = WB_SYNC_NONE,
351 .nr_to_write = SWAP_CLUSTER_MAX,
352 .nonblocking = 1,
353 .for_reclaim = 1,
354 };
355
356 SetPageReclaim(page);
357 res = mapping->a_ops->writepage(page, &wbc);
358 if (res < 0)
359 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800360 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 ClearPageReclaim(page);
362 return PAGE_ACTIVATE;
363 }
364 if (!PageWriteback(page)) {
365 /* synchronous write or broken a_ops? */
366 ClearPageReclaim(page);
367 }
368
369 return PAGE_SUCCESS;
370 }
371
372 return PAGE_CLEAN;
373}
374
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800375static int remove_mapping(struct address_space *mapping, struct page *page)
376{
377 if (!mapping)
378 return 0; /* truncate got there first */
379
380 write_lock_irq(&mapping->tree_lock);
381
382 /*
383 * The non-racy check for busy page. It is critical to check
384 * PageDirty _after_ making sure that the page is freeable and
385 * not in use by anybody. (pagecache + us == 2)
386 */
387 if (unlikely(page_count(page) != 2))
388 goto cannot_free;
389 smp_rmb();
390 if (unlikely(PageDirty(page)))
391 goto cannot_free;
392
393 if (PageSwapCache(page)) {
394 swp_entry_t swap = { .val = page_private(page) };
395 __delete_from_swap_cache(page);
396 write_unlock_irq(&mapping->tree_lock);
397 swap_free(swap);
398 __put_page(page); /* The pagecache ref */
399 return 1;
400 }
401
402 __remove_from_page_cache(page);
403 write_unlock_irq(&mapping->tree_lock);
404 __put_page(page);
405 return 1;
406
407cannot_free:
408 write_unlock_irq(&mapping->tree_lock);
409 return 0;
410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412/*
Andrew Morton1742f192006-03-22 00:08:21 -0800413 * shrink_page_list() returns the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 */
Andrew Morton1742f192006-03-22 00:08:21 -0800415static unsigned long shrink_page_list(struct list_head *page_list,
416 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 LIST_HEAD(ret_pages);
419 struct pagevec freed_pvec;
420 int pgactivate = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800421 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 cond_resched();
424
425 pagevec_init(&freed_pvec, 1);
426 while (!list_empty(page_list)) {
427 struct address_space *mapping;
428 struct page *page;
429 int may_enter_fs;
430 int referenced;
431
432 cond_resched();
433
434 page = lru_to_page(page_list);
435 list_del(&page->lru);
436
437 if (TestSetPageLocked(page))
438 goto keep;
439
440 BUG_ON(PageActive(page));
441
442 sc->nr_scanned++;
Christoph Lameter80e43422006-02-11 17:55:53 -0800443
444 if (!sc->may_swap && page_mapped(page))
445 goto keep_locked;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /* Double the slab pressure for mapped and swapcache pages */
448 if (page_mapped(page) || PageSwapCache(page))
449 sc->nr_scanned++;
450
451 if (PageWriteback(page))
452 goto keep_locked;
453
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800454 referenced = page_referenced(page, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /* In active use or really unfreeable? Activate it. */
456 if (referenced && page_mapping_inuse(page))
457 goto activate_locked;
458
459#ifdef CONFIG_SWAP
460 /*
461 * Anonymous process memory has backing store?
462 * Try to allocate it some swap space here.
463 */
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800464 if (PageAnon(page) && !PageSwapCache(page))
Christoph Lameter1480a542006-01-08 01:00:53 -0800465 if (!add_to_swap(page, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 goto activate_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467#endif /* CONFIG_SWAP */
468
469 mapping = page_mapping(page);
470 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
471 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
472
473 /*
474 * The page is mapped into the page tables of one or more
475 * processes. Try to unmap it here.
476 */
477 if (page_mapped(page) && mapping) {
Christoph Lametera48d07a2006-02-01 03:05:38 -0800478 switch (try_to_unmap(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 case SWAP_FAIL:
480 goto activate_locked;
481 case SWAP_AGAIN:
482 goto keep_locked;
483 case SWAP_SUCCESS:
484 ; /* try to free the page below */
485 }
486 }
487
488 if (PageDirty(page)) {
489 if (referenced)
490 goto keep_locked;
491 if (!may_enter_fs)
492 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -0800493 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 goto keep_locked;
495
496 /* Page is dirty, try to write it out here */
497 switch(pageout(page, mapping)) {
498 case PAGE_KEEP:
499 goto keep_locked;
500 case PAGE_ACTIVATE:
501 goto activate_locked;
502 case PAGE_SUCCESS:
503 if (PageWriteback(page) || PageDirty(page))
504 goto keep;
505 /*
506 * A synchronous write - probably a ramdisk. Go
507 * ahead and try to reclaim the page.
508 */
509 if (TestSetPageLocked(page))
510 goto keep;
511 if (PageDirty(page) || PageWriteback(page))
512 goto keep_locked;
513 mapping = page_mapping(page);
514 case PAGE_CLEAN:
515 ; /* try to free the page below */
516 }
517 }
518
519 /*
520 * If the page has buffers, try to free the buffer mappings
521 * associated with this page. If we succeed we try to free
522 * the page as well.
523 *
524 * We do this even if the page is PageDirty().
525 * try_to_release_page() does not perform I/O, but it is
526 * possible for a page to have PageDirty set, but it is actually
527 * clean (all its buffers are clean). This happens if the
528 * buffers were written out directly, with submit_bh(). ext3
529 * will do this, as well as the blockdev mapping.
530 * try_to_release_page() will discover that cleanness and will
531 * drop the buffers and mark the page clean - it can be freed.
532 *
533 * Rarely, pages can have buffers and no ->mapping. These are
534 * the pages which were not successfully invalidated in
535 * truncate_complete_page(). We try to drop those buffers here
536 * and if that worked, and the page is no longer mapped into
537 * process address space (page_count == 1) it can be freed.
538 * Otherwise, leave the page on the LRU so it is swappable.
539 */
540 if (PagePrivate(page)) {
541 if (!try_to_release_page(page, sc->gfp_mask))
542 goto activate_locked;
543 if (!mapping && page_count(page) == 1)
544 goto free_it;
545 }
546
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800547 if (!remove_mapping(mapping, page))
548 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550free_it:
551 unlock_page(page);
Andrew Morton05ff5132006-03-22 00:08:20 -0800552 nr_reclaimed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (!pagevec_add(&freed_pvec, page))
554 __pagevec_release_nonlru(&freed_pvec);
555 continue;
556
557activate_locked:
558 SetPageActive(page);
559 pgactivate++;
560keep_locked:
561 unlock_page(page);
562keep:
563 list_add(&page->lru, &ret_pages);
564 BUG_ON(PageLRU(page));
565 }
566 list_splice(&ret_pages, page_list);
567 if (pagevec_count(&freed_pvec))
568 __pagevec_release_nonlru(&freed_pvec);
569 mod_page_state(pgactivate, pgactivate);
Andrew Morton05ff5132006-03-22 00:08:20 -0800570 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800573#ifdef CONFIG_MIGRATION
Christoph Lameter8419c312006-01-08 01:00:52 -0800574static inline void move_to_lru(struct page *page)
575{
576 list_del(&page->lru);
577 if (PageActive(page)) {
578 /*
579 * lru_cache_add_active checks that
580 * the PG_active bit is off.
581 */
582 ClearPageActive(page);
583 lru_cache_add_active(page);
584 } else {
585 lru_cache_add(page);
586 }
587 put_page(page);
588}
589
590/*
Nick Piggin053837f2006-01-18 17:42:27 -0800591 * Add isolated pages on the list back to the LRU.
Christoph Lameter8419c312006-01-08 01:00:52 -0800592 *
593 * returns the number of pages put back.
594 */
Andrew Morton69e05942006-03-22 00:08:19 -0800595unsigned long putback_lru_pages(struct list_head *l)
Christoph Lameter8419c312006-01-08 01:00:52 -0800596{
597 struct page *page;
598 struct page *page2;
Andrew Morton69e05942006-03-22 00:08:19 -0800599 unsigned long count = 0;
Christoph Lameter8419c312006-01-08 01:00:52 -0800600
601 list_for_each_entry_safe(page, page2, l, lru) {
602 move_to_lru(page);
603 count++;
604 }
605 return count;
606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/*
Christoph Lametere965f962006-02-01 03:05:41 -0800609 * Non migratable page
610 */
611int fail_migrate_page(struct page *newpage, struct page *page)
612{
613 return -EIO;
614}
615EXPORT_SYMBOL(fail_migrate_page);
616
617/*
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800618 * swapout a single page
619 * page is locked upon entry, unlocked on exit
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800620 */
621static int swap_page(struct page *page)
622{
623 struct address_space *mapping = page_mapping(page);
624
625 if (page_mapped(page) && mapping)
Christoph Lameter418aade2006-02-10 01:51:15 -0800626 if (try_to_unmap(page, 1) != SWAP_SUCCESS)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800627 goto unlock_retry;
628
629 if (PageDirty(page)) {
630 /* Page is dirty, try to write it out here */
631 switch(pageout(page, mapping)) {
632 case PAGE_KEEP:
633 case PAGE_ACTIVATE:
634 goto unlock_retry;
635
636 case PAGE_SUCCESS:
637 goto retry;
638
639 case PAGE_CLEAN:
640 ; /* try to free the page below */
641 }
642 }
643
644 if (PagePrivate(page)) {
645 if (!try_to_release_page(page, GFP_KERNEL) ||
646 (!mapping && page_count(page) == 1))
647 goto unlock_retry;
648 }
649
650 if (remove_mapping(mapping, page)) {
651 /* Success */
652 unlock_page(page);
653 return 0;
654 }
655
656unlock_retry:
657 unlock_page(page);
658
659retry:
Christoph Lameterd0d96322006-01-08 01:00:55 -0800660 return -EAGAIN;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800661}
Christoph Lametere965f962006-02-01 03:05:41 -0800662EXPORT_SYMBOL(swap_page);
Christoph Lametera48d07a2006-02-01 03:05:38 -0800663
664/*
665 * Page migration was first developed in the context of the memory hotplug
666 * project. The main authors of the migration code are:
667 *
668 * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
669 * Hirokazu Takahashi <taka@valinux.co.jp>
670 * Dave Hansen <haveblue@us.ibm.com>
671 * Christoph Lameter <clameter@sgi.com>
672 */
673
674/*
675 * Remove references for a page and establish the new page with the correct
676 * basic settings to be able to stop accesses to the page.
677 */
Christoph Lametere965f962006-02-01 03:05:41 -0800678int migrate_page_remove_references(struct page *newpage,
Christoph Lametera48d07a2006-02-01 03:05:38 -0800679 struct page *page, int nr_refs)
680{
681 struct address_space *mapping = page_mapping(page);
682 struct page **radix_pointer;
683
684 /*
685 * Avoid doing any of the following work if the page count
686 * indicates that the page is in use or truncate has removed
687 * the page.
688 */
689 if (!mapping || page_mapcount(page) + nr_refs != page_count(page))
Christoph Lameter4983da02006-03-14 19:50:19 -0800690 return -EAGAIN;
Christoph Lametera48d07a2006-02-01 03:05:38 -0800691
692 /*
693 * Establish swap ptes for anonymous pages or destroy pte
694 * maps for files.
695 *
696 * In order to reestablish file backed mappings the fault handlers
697 * will take the radix tree_lock which may then be used to stop
698 * processses from accessing this page until the new page is ready.
699 *
700 * A process accessing via a swap pte (an anonymous page) will take a
701 * page_lock on the old page which will block the process until the
702 * migration attempt is complete. At that time the PageSwapCache bit
703 * will be examined. If the page was migrated then the PageSwapCache
704 * bit will be clear and the operation to retrieve the page will be
705 * retried which will find the new page in the radix tree. Then a new
706 * direct mapping may be generated based on the radix tree contents.
707 *
708 * If the page was not migrated then the PageSwapCache bit
709 * is still set and the operation may continue.
710 */
Christoph Lameter4983da02006-03-14 19:50:19 -0800711 if (try_to_unmap(page, 1) == SWAP_FAIL)
712 /* A vma has VM_LOCKED set -> Permanent failure */
713 return -EPERM;
Christoph Lametera48d07a2006-02-01 03:05:38 -0800714
715 /*
716 * Give up if we were unable to remove all mappings.
717 */
718 if (page_mapcount(page))
Christoph Lameter4983da02006-03-14 19:50:19 -0800719 return -EAGAIN;
Christoph Lametera48d07a2006-02-01 03:05:38 -0800720
721 write_lock_irq(&mapping->tree_lock);
722
723 radix_pointer = (struct page **)radix_tree_lookup_slot(
724 &mapping->page_tree,
725 page_index(page));
726
727 if (!page_mapping(page) || page_count(page) != nr_refs ||
728 *radix_pointer != page) {
729 write_unlock_irq(&mapping->tree_lock);
Christoph Lameter4983da02006-03-14 19:50:19 -0800730 return -EAGAIN;
Christoph Lametera48d07a2006-02-01 03:05:38 -0800731 }
732
733 /*
734 * Now we know that no one else is looking at the page.
735 *
736 * Certain minimal information about a page must be available
737 * in order for other subsystems to properly handle the page if they
738 * find it through the radix tree update before we are finished
739 * copying the page.
740 */
741 get_page(newpage);
742 newpage->index = page->index;
743 newpage->mapping = page->mapping;
744 if (PageSwapCache(page)) {
745 SetPageSwapCache(newpage);
746 set_page_private(newpage, page_private(page));
747 }
748
749 *radix_pointer = newpage;
750 __put_page(page);
751 write_unlock_irq(&mapping->tree_lock);
752
753 return 0;
754}
Christoph Lametere965f962006-02-01 03:05:41 -0800755EXPORT_SYMBOL(migrate_page_remove_references);
Christoph Lametera48d07a2006-02-01 03:05:38 -0800756
757/*
758 * Copy the page to its new location
759 */
760void migrate_page_copy(struct page *newpage, struct page *page)
761{
762 copy_highpage(newpage, page);
763
764 if (PageError(page))
765 SetPageError(newpage);
766 if (PageReferenced(page))
767 SetPageReferenced(newpage);
768 if (PageUptodate(page))
769 SetPageUptodate(newpage);
770 if (PageActive(page))
771 SetPageActive(newpage);
772 if (PageChecked(page))
773 SetPageChecked(newpage);
774 if (PageMappedToDisk(page))
775 SetPageMappedToDisk(newpage);
776
777 if (PageDirty(page)) {
778 clear_page_dirty_for_io(page);
779 set_page_dirty(newpage);
780 }
781
782 ClearPageSwapCache(page);
783 ClearPageActive(page);
784 ClearPagePrivate(page);
785 set_page_private(page, 0);
786 page->mapping = NULL;
787
788 /*
789 * If any waiters have accumulated on the new page then
790 * wake them up.
791 */
792 if (PageWriteback(newpage))
793 end_page_writeback(newpage);
794}
Christoph Lametere965f962006-02-01 03:05:41 -0800795EXPORT_SYMBOL(migrate_page_copy);
Christoph Lametera48d07a2006-02-01 03:05:38 -0800796
797/*
798 * Common logic to directly migrate a single page suitable for
799 * pages that do not use PagePrivate.
800 *
801 * Pages are locked upon entry and exit.
802 */
803int migrate_page(struct page *newpage, struct page *page)
804{
Christoph Lameter4983da02006-03-14 19:50:19 -0800805 int rc;
806
Christoph Lametera48d07a2006-02-01 03:05:38 -0800807 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
808
Christoph Lameter4983da02006-03-14 19:50:19 -0800809 rc = migrate_page_remove_references(newpage, page, 2);
810
811 if (rc)
812 return rc;
Christoph Lametera48d07a2006-02-01 03:05:38 -0800813
814 migrate_page_copy(newpage, page);
815
Christoph Lametera3351e52006-02-01 03:05:39 -0800816 /*
817 * Remove auxiliary swap entries and replace
818 * them with real ptes.
819 *
820 * Note that a real pte entry will allow processes that are not
821 * waiting on the page lock to use the new page via the page tables
822 * before the new page is unlocked.
823 */
824 remove_from_swap(newpage);
Christoph Lametera48d07a2006-02-01 03:05:38 -0800825 return 0;
826}
Christoph Lametere965f962006-02-01 03:05:41 -0800827EXPORT_SYMBOL(migrate_page);
Christoph Lametera48d07a2006-02-01 03:05:38 -0800828
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800829/*
830 * migrate_pages
831 *
832 * Two lists are passed to this function. The first list
833 * contains the pages isolated from the LRU to be migrated.
834 * The second list contains new pages that the pages isolated
835 * can be moved to. If the second list is NULL then all
836 * pages are swapped out.
837 *
838 * The function returns after 10 attempts or if no pages
Christoph Lameter418aade2006-02-10 01:51:15 -0800839 * are movable anymore because to has become empty
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800840 * or no retryable pages exist anymore.
841 *
Christoph Lameterd0d96322006-01-08 01:00:55 -0800842 * Return: Number of pages not migrated when "to" ran empty.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800843 */
Andrew Morton69e05942006-03-22 00:08:19 -0800844unsigned long migrate_pages(struct list_head *from, struct list_head *to,
Christoph Lameterd4984712006-01-08 01:00:55 -0800845 struct list_head *moved, struct list_head *failed)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800846{
Andrew Morton69e05942006-03-22 00:08:19 -0800847 unsigned long retry;
848 unsigned long nr_failed = 0;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800849 int pass = 0;
850 struct page *page;
851 struct page *page2;
852 int swapwrite = current->flags & PF_SWAPWRITE;
Christoph Lameterd0d96322006-01-08 01:00:55 -0800853 int rc;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800854
855 if (!swapwrite)
856 current->flags |= PF_SWAPWRITE;
857
858redo:
859 retry = 0;
860
Christoph Lameterd4984712006-01-08 01:00:55 -0800861 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lametera48d07a2006-02-01 03:05:38 -0800862 struct page *newpage = NULL;
863 struct address_space *mapping;
864
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800865 cond_resched();
866
Christoph Lameterd0d96322006-01-08 01:00:55 -0800867 rc = 0;
868 if (page_count(page) == 1)
Christoph Lameteree274972006-01-08 01:00:54 -0800869 /* page was freed from under us. So we are done. */
Christoph Lameterd0d96322006-01-08 01:00:55 -0800870 goto next;
871
Christoph Lametera48d07a2006-02-01 03:05:38 -0800872 if (to && list_empty(to))
873 break;
874
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800875 /*
876 * Skip locked pages during the first two passes to give the
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800877 * functions holding the lock time to release the page. Later we
878 * use lock_page() to have a higher chance of acquiring the
879 * lock.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800880 */
Christoph Lameterd0d96322006-01-08 01:00:55 -0800881 rc = -EAGAIN;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800882 if (pass > 2)
883 lock_page(page);
884 else
885 if (TestSetPageLocked(page))
Christoph Lameterd0d96322006-01-08 01:00:55 -0800886 goto next;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800887
888 /*
889 * Only wait on writeback if we have already done a pass where
890 * we we may have triggered writeouts for lots of pages.
891 */
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800892 if (pass > 0) {
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800893 wait_on_page_writeback(page);
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800894 } else {
Christoph Lameterd0d96322006-01-08 01:00:55 -0800895 if (PageWriteback(page))
896 goto unlock_page;
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800897 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800898
Christoph Lameterd0d96322006-01-08 01:00:55 -0800899 /*
900 * Anonymous pages must have swap cache references otherwise
901 * the information contained in the page maps cannot be
902 * preserved.
903 */
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800904 if (PageAnon(page) && !PageSwapCache(page)) {
Christoph Lameter1480a542006-01-08 01:00:53 -0800905 if (!add_to_swap(page, GFP_KERNEL)) {
Christoph Lameterd0d96322006-01-08 01:00:55 -0800906 rc = -ENOMEM;
907 goto unlock_page;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800908 }
909 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800910
Christoph Lametera48d07a2006-02-01 03:05:38 -0800911 if (!to) {
912 rc = swap_page(page);
913 goto next;
914 }
915
916 newpage = lru_to_page(to);
917 lock_page(newpage);
918
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800919 /*
Christoph Lametera48d07a2006-02-01 03:05:38 -0800920 * Pages are properly locked and writeback is complete.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800921 * Try to migrate the page.
922 */
Christoph Lametera48d07a2006-02-01 03:05:38 -0800923 mapping = page_mapping(page);
924 if (!mapping)
925 goto unlock_both;
926
Christoph Lametere965f962006-02-01 03:05:41 -0800927 if (mapping->a_ops->migratepage) {
Christoph Lameter418aade2006-02-10 01:51:15 -0800928 /*
929 * Most pages have a mapping and most filesystems
930 * should provide a migration function. Anonymous
931 * pages are part of swap space which also has its
932 * own migration function. This is the most common
933 * path for page migration.
934 */
Christoph Lametere965f962006-02-01 03:05:41 -0800935 rc = mapping->a_ops->migratepage(newpage, page);
936 goto unlock_both;
937 }
938
Christoph Lametera48d07a2006-02-01 03:05:38 -0800939 /*
Christoph Lameter418aade2006-02-10 01:51:15 -0800940 * Default handling if a filesystem does not provide
941 * a migration function. We can only migrate clean
942 * pages so try to write out any dirty pages first.
Christoph Lametera48d07a2006-02-01 03:05:38 -0800943 */
944 if (PageDirty(page)) {
945 switch (pageout(page, mapping)) {
946 case PAGE_KEEP:
947 case PAGE_ACTIVATE:
948 goto unlock_both;
949
950 case PAGE_SUCCESS:
951 unlock_page(newpage);
952 goto next;
953
954 case PAGE_CLEAN:
955 ; /* try to migrate the page below */
956 }
957 }
Christoph Lameter418aade2006-02-10 01:51:15 -0800958
Christoph Lametera48d07a2006-02-01 03:05:38 -0800959 /*
Christoph Lameter418aade2006-02-10 01:51:15 -0800960 * Buffers are managed in a filesystem specific way.
961 * We must have no buffers or drop them.
Christoph Lametera48d07a2006-02-01 03:05:38 -0800962 */
963 if (!page_has_buffers(page) ||
964 try_to_release_page(page, GFP_KERNEL)) {
965 rc = migrate_page(newpage, page);
966 goto unlock_both;
967 }
968
969 /*
970 * On early passes with mapped pages simply
971 * retry. There may be a lock held for some
972 * buffers that may go away. Later
973 * swap them out.
974 */
975 if (pass > 4) {
Christoph Lameter418aade2006-02-10 01:51:15 -0800976 /*
977 * Persistently unable to drop buffers..... As a
978 * measure of last resort we fall back to
979 * swap_page().
980 */
Christoph Lametera48d07a2006-02-01 03:05:38 -0800981 unlock_page(newpage);
982 newpage = NULL;
983 rc = swap_page(page);
984 goto next;
985 }
986
987unlock_both:
988 unlock_page(newpage);
Christoph Lameterd0d96322006-01-08 01:00:55 -0800989
990unlock_page:
991 unlock_page(page);
992
993next:
994 if (rc == -EAGAIN) {
995 retry++;
996 } else if (rc) {
997 /* Permanent failure */
998 list_move(&page->lru, failed);
999 nr_failed++;
1000 } else {
Christoph Lametera48d07a2006-02-01 03:05:38 -08001001 if (newpage) {
1002 /* Successful migration. Return page to LRU */
1003 move_to_lru(newpage);
1004 }
Christoph Lameterd4984712006-01-08 01:00:55 -08001005 list_move(&page->lru, moved);
Christoph Lameterd4984712006-01-08 01:00:55 -08001006 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001007 }
1008 if (retry && pass++ < 10)
1009 goto redo;
1010
1011 if (!swapwrite)
1012 current->flags &= ~PF_SWAPWRITE;
1013
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001014 return nr_failed + retry;
1015}
Christoph Lameter8419c312006-01-08 01:00:52 -08001016
Christoph Lameter8419c312006-01-08 01:00:52 -08001017/*
1018 * Isolate one page from the LRU lists and put it on the
Nick Piggin053837f2006-01-18 17:42:27 -08001019 * indicated list with elevated refcount.
Christoph Lameter8419c312006-01-08 01:00:52 -08001020 *
1021 * Result:
1022 * 0 = page not on LRU list
1023 * 1 = page removed from LRU list and added to the specified list.
Christoph Lameter8419c312006-01-08 01:00:52 -08001024 */
1025int isolate_lru_page(struct page *page)
1026{
Nick Piggin053837f2006-01-18 17:42:27 -08001027 int ret = 0;
Christoph Lameter8419c312006-01-08 01:00:52 -08001028
Nick Piggin053837f2006-01-18 17:42:27 -08001029 if (PageLRU(page)) {
1030 struct zone *zone = page_zone(page);
1031 spin_lock_irq(&zone->lru_lock);
Nick Piggin8d438f92006-03-22 00:07:59 -08001032 if (PageLRU(page)) {
Nick Piggin053837f2006-01-18 17:42:27 -08001033 ret = 1;
1034 get_page(page);
Nick Piggin8d438f92006-03-22 00:07:59 -08001035 ClearPageLRU(page);
Nick Piggin053837f2006-01-18 17:42:27 -08001036 if (PageActive(page))
1037 del_page_from_active_list(zone, page);
1038 else
1039 del_page_from_inactive_list(zone, page);
1040 }
1041 spin_unlock_irq(&zone->lru_lock);
Christoph Lameter8419c312006-01-08 01:00:52 -08001042 }
Nick Piggin053837f2006-01-18 17:42:27 -08001043
1044 return ret;
Christoph Lameter8419c312006-01-08 01:00:52 -08001045}
Christoph Lameter7cbe34c2006-01-08 01:00:49 -08001046#endif
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001047
1048/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 * zone->lru_lock is heavily contended. Some of the functions that
1050 * shrink the lists perform better by taking out a batch of pages
1051 * and working on them outside the LRU lock.
1052 *
1053 * For pagecache intensive workloads, this function is the hottest
1054 * spot in the kernel (apart from copy_*_user functions).
1055 *
1056 * Appropriate locks must be held before calling this function.
1057 *
1058 * @nr_to_scan: The number of pages to look through on the list.
1059 * @src: The LRU list to pull pages off.
1060 * @dst: The temp list to put pages on to.
1061 * @scanned: The number of pages that were scanned.
1062 *
1063 * returns how many pages were moved onto *@dst.
1064 */
Andrew Morton69e05942006-03-22 00:08:19 -08001065static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
1066 struct list_head *src, struct list_head *dst,
1067 unsigned long *scanned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Andrew Morton69e05942006-03-22 00:08:19 -08001069 unsigned long nr_taken = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 struct page *page;
Wu Fengguangc9b02d92006-03-22 00:08:23 -08001071 unsigned long scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Wu Fengguangc9b02d92006-03-22 00:08:23 -08001073 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
Nick Piggin7c8ee9a2006-03-22 00:08:03 -08001074 struct list_head *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 page = lru_to_page(src);
1076 prefetchw_prev_lru_page(page, src, flags);
1077
Nick Piggin8d438f92006-03-22 00:07:59 -08001078 BUG_ON(!PageLRU(page));
1079
Nick Piggin053837f2006-01-18 17:42:27 -08001080 list_del(&page->lru);
Nick Piggin7c8ee9a2006-03-22 00:08:03 -08001081 target = src;
1082 if (likely(get_page_unless_zero(page))) {
Nick Piggin053837f2006-01-18 17:42:27 -08001083 /*
Nick Piggin7c8ee9a2006-03-22 00:08:03 -08001084 * Be careful not to clear PageLRU until after we're
1085 * sure the page is not being freed elsewhere -- the
1086 * page release code relies on it.
Nick Piggin053837f2006-01-18 17:42:27 -08001087 */
Nick Piggin7c8ee9a2006-03-22 00:08:03 -08001088 ClearPageLRU(page);
1089 target = dst;
1090 nr_taken++;
1091 } /* else it is being freed elsewhere */
Nick Piggin46453a62006-03-22 00:07:58 -08001092
Nick Piggin7c8ee9a2006-03-22 00:08:03 -08001093 list_add(&page->lru, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095
1096 *scanned = scan;
1097 return nr_taken;
1098}
1099
1100/*
Andrew Morton1742f192006-03-22 00:08:21 -08001101 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
1102 * of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 */
Andrew Morton1742f192006-03-22 00:08:21 -08001104static unsigned long shrink_inactive_list(unsigned long max_scan,
1105 struct zone *zone, struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 LIST_HEAD(page_list);
1108 struct pagevec pvec;
Andrew Morton69e05942006-03-22 00:08:19 -08001109 unsigned long nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001110 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 pagevec_init(&pvec, 1);
1113
1114 lru_add_drain();
1115 spin_lock_irq(&zone->lru_lock);
Andrew Morton69e05942006-03-22 00:08:19 -08001116 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 struct page *page;
Andrew Morton69e05942006-03-22 00:08:19 -08001118 unsigned long nr_taken;
1119 unsigned long nr_scan;
1120 unsigned long nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
1123 &zone->inactive_list,
1124 &page_list, &nr_scan);
1125 zone->nr_inactive -= nr_taken;
1126 zone->pages_scanned += nr_scan;
1127 spin_unlock_irq(&zone->lru_lock);
1128
Andrew Morton69e05942006-03-22 00:08:19 -08001129 nr_scanned += nr_scan;
Andrew Morton1742f192006-03-22 00:08:21 -08001130 nr_freed = shrink_page_list(&page_list, sc);
Andrew Morton05ff5132006-03-22 00:08:20 -08001131 nr_reclaimed += nr_freed;
Nick Piggina74609f2006-01-06 00:11:20 -08001132 local_irq_disable();
1133 if (current_is_kswapd()) {
1134 __mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
1135 __mod_page_state(kswapd_steal, nr_freed);
1136 } else
1137 __mod_page_state_zone(zone, pgscan_direct, nr_scan);
1138 __mod_page_state_zone(zone, pgsteal, nr_freed);
1139
Wu Fengguangfb8d14e2006-03-22 00:08:28 -08001140 if (nr_taken == 0)
1141 goto done;
1142
Nick Piggina74609f2006-01-06 00:11:20 -08001143 spin_lock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 /*
1145 * Put back any unfreeable pages.
1146 */
1147 while (!list_empty(&page_list)) {
1148 page = lru_to_page(&page_list);
Nick Piggin8d438f92006-03-22 00:07:59 -08001149 BUG_ON(PageLRU(page));
1150 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 list_del(&page->lru);
1152 if (PageActive(page))
1153 add_page_to_active_list(zone, page);
1154 else
1155 add_page_to_inactive_list(zone, page);
1156 if (!pagevec_add(&pvec, page)) {
1157 spin_unlock_irq(&zone->lru_lock);
1158 __pagevec_release(&pvec);
1159 spin_lock_irq(&zone->lru_lock);
1160 }
1161 }
Andrew Morton69e05942006-03-22 00:08:19 -08001162 } while (nr_scanned < max_scan);
Wu Fengguangfb8d14e2006-03-22 00:08:28 -08001163 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164done:
Wu Fengguangfb8d14e2006-03-22 00:08:28 -08001165 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 pagevec_release(&pvec);
Andrew Morton05ff5132006-03-22 00:08:20 -08001167 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
1170/*
1171 * This moves pages from the active list to the inactive list.
1172 *
1173 * We move them the other way if the page is referenced by one or more
1174 * processes, from rmap.
1175 *
1176 * If the pages are mostly unmapped, the processing is fast and it is
1177 * appropriate to hold zone->lru_lock across the whole operation. But if
1178 * the pages are mapped, the processing is slow (page_referenced()) so we
1179 * should drop zone->lru_lock around each page. It's impossible to balance
1180 * this, so instead we remove the pages from the LRU while processing them.
1181 * It is safe to rely on PG_active against the non-LRU pages in here because
1182 * nobody will play with that bit on a non-LRU page.
1183 *
1184 * The downside is that we have to touch page->_count against each page.
1185 * But we had to alter page->flags anyway.
1186 */
Andrew Morton1742f192006-03-22 00:08:21 -08001187static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
1188 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
Andrew Morton69e05942006-03-22 00:08:19 -08001190 unsigned long pgmoved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 int pgdeactivate = 0;
Andrew Morton69e05942006-03-22 00:08:19 -08001192 unsigned long pgscanned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 LIST_HEAD(l_hold); /* The pages which were snipped off */
1194 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
1195 LIST_HEAD(l_active); /* Pages to go onto the active_list */
1196 struct page *page;
1197 struct pagevec pvec;
1198 int reclaim_mapped = 0;
Christoph Lameter2903fb12006-02-11 17:55:55 -08001199
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -08001200 if (sc->may_swap) {
Christoph Lameter2903fb12006-02-11 17:55:55 -08001201 long mapped_ratio;
1202 long distress;
1203 long swap_tendency;
1204
1205 /*
1206 * `distress' is a measure of how much trouble we're having
1207 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
1208 */
1209 distress = 100 >> zone->prev_priority;
1210
1211 /*
1212 * The point of this algorithm is to decide when to start
1213 * reclaiming mapped memory instead of just pagecache. Work out
1214 * how much memory
1215 * is mapped.
1216 */
1217 mapped_ratio = (sc->nr_mapped * 100) / total_memory;
1218
1219 /*
1220 * Now decide how much we really want to unmap some pages. The
1221 * mapped ratio is downgraded - just because there's a lot of
1222 * mapped memory doesn't necessarily mean that page reclaim
1223 * isn't succeeding.
1224 *
1225 * The distress ratio is important - we don't want to start
1226 * going oom.
1227 *
1228 * A 100% value of vm_swappiness overrides this algorithm
1229 * altogether.
1230 */
1231 swap_tendency = mapped_ratio / 2 + distress + vm_swappiness;
1232
1233 /*
1234 * Now use this metric to decide whether to start moving mapped
1235 * memory onto the inactive list.
1236 */
1237 if (swap_tendency >= 100)
1238 reclaim_mapped = 1;
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 lru_add_drain();
1242 spin_lock_irq(&zone->lru_lock);
1243 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
1244 &l_hold, &pgscanned);
1245 zone->pages_scanned += pgscanned;
1246 zone->nr_active -= pgmoved;
1247 spin_unlock_irq(&zone->lru_lock);
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 while (!list_empty(&l_hold)) {
1250 cond_resched();
1251 page = lru_to_page(&l_hold);
1252 list_del(&page->lru);
1253 if (page_mapped(page)) {
1254 if (!reclaim_mapped ||
1255 (total_swap_pages == 0 && PageAnon(page)) ||
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001256 page_referenced(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 list_add(&page->lru, &l_active);
1258 continue;
1259 }
1260 }
1261 list_add(&page->lru, &l_inactive);
1262 }
1263
1264 pagevec_init(&pvec, 1);
1265 pgmoved = 0;
1266 spin_lock_irq(&zone->lru_lock);
1267 while (!list_empty(&l_inactive)) {
1268 page = lru_to_page(&l_inactive);
1269 prefetchw_prev_lru_page(page, &l_inactive, flags);
Nick Piggin8d438f92006-03-22 00:07:59 -08001270 BUG_ON(PageLRU(page));
1271 SetPageLRU(page);
Nick Piggin4c84cac2006-03-22 00:08:00 -08001272 BUG_ON(!PageActive(page));
1273 ClearPageActive(page);
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 list_move(&page->lru, &zone->inactive_list);
1276 pgmoved++;
1277 if (!pagevec_add(&pvec, page)) {
1278 zone->nr_inactive += pgmoved;
1279 spin_unlock_irq(&zone->lru_lock);
1280 pgdeactivate += pgmoved;
1281 pgmoved = 0;
1282 if (buffer_heads_over_limit)
1283 pagevec_strip(&pvec);
1284 __pagevec_release(&pvec);
1285 spin_lock_irq(&zone->lru_lock);
1286 }
1287 }
1288 zone->nr_inactive += pgmoved;
1289 pgdeactivate += pgmoved;
1290 if (buffer_heads_over_limit) {
1291 spin_unlock_irq(&zone->lru_lock);
1292 pagevec_strip(&pvec);
1293 spin_lock_irq(&zone->lru_lock);
1294 }
1295
1296 pgmoved = 0;
1297 while (!list_empty(&l_active)) {
1298 page = lru_to_page(&l_active);
1299 prefetchw_prev_lru_page(page, &l_active, flags);
Nick Piggin8d438f92006-03-22 00:07:59 -08001300 BUG_ON(PageLRU(page));
1301 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 BUG_ON(!PageActive(page));
1303 list_move(&page->lru, &zone->active_list);
1304 pgmoved++;
1305 if (!pagevec_add(&pvec, page)) {
1306 zone->nr_active += pgmoved;
1307 pgmoved = 0;
1308 spin_unlock_irq(&zone->lru_lock);
1309 __pagevec_release(&pvec);
1310 spin_lock_irq(&zone->lru_lock);
1311 }
1312 }
1313 zone->nr_active += pgmoved;
Nick Piggina74609f2006-01-06 00:11:20 -08001314 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Nick Piggina74609f2006-01-06 00:11:20 -08001316 __mod_page_state_zone(zone, pgrefill, pgscanned);
1317 __mod_page_state(pgdeactivate, pgdeactivate);
1318 local_irq_enable();
1319
1320 pagevec_release(&pvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
1323/*
1324 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
1325 */
Andrew Morton05ff5132006-03-22 00:08:20 -08001326static unsigned long shrink_zone(int priority, struct zone *zone,
1327 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 unsigned long nr_active;
1330 unsigned long nr_inactive;
Christoph Lameter86959492006-03-22 00:08:18 -08001331 unsigned long nr_to_scan;
Andrew Morton05ff5132006-03-22 00:08:20 -08001332 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Martin Hicks53e9a612005-09-03 15:54:51 -07001334 atomic_inc(&zone->reclaim_in_progress);
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 /*
1337 * Add one to `nr_to_scan' just to make sure that the kernel will
1338 * slowly sift through the active list.
1339 */
Christoph Lameter86959492006-03-22 00:08:18 -08001340 zone->nr_scan_active += (zone->nr_active >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 nr_active = zone->nr_scan_active;
1342 if (nr_active >= sc->swap_cluster_max)
1343 zone->nr_scan_active = 0;
1344 else
1345 nr_active = 0;
1346
Christoph Lameter86959492006-03-22 00:08:18 -08001347 zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 nr_inactive = zone->nr_scan_inactive;
1349 if (nr_inactive >= sc->swap_cluster_max)
1350 zone->nr_scan_inactive = 0;
1351 else
1352 nr_inactive = 0;
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 while (nr_active || nr_inactive) {
1355 if (nr_active) {
Christoph Lameter86959492006-03-22 00:08:18 -08001356 nr_to_scan = min(nr_active,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -08001358 nr_active -= nr_to_scan;
Andrew Morton1742f192006-03-22 00:08:21 -08001359 shrink_active_list(nr_to_scan, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
1361
1362 if (nr_inactive) {
Christoph Lameter86959492006-03-22 00:08:18 -08001363 nr_to_scan = min(nr_inactive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -08001365 nr_inactive -= nr_to_scan;
Andrew Morton1742f192006-03-22 00:08:21 -08001366 nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
1367 sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369 }
1370
1371 throttle_vm_writeout();
Martin Hicks53e9a612005-09-03 15:54:51 -07001372
1373 atomic_dec(&zone->reclaim_in_progress);
Andrew Morton05ff5132006-03-22 00:08:20 -08001374 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
1377/*
1378 * This is the direct reclaim path, for page-allocating processes. We only
1379 * try to reclaim pages from zones which will satisfy the caller's allocation
1380 * request.
1381 *
1382 * We reclaim from a zone even if that zone is over pages_high. Because:
1383 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
1384 * allocation or
1385 * b) The zones may be over pages_high but they must go *over* pages_high to
1386 * satisfy the `incremental min' zone defense algorithm.
1387 *
1388 * Returns the number of reclaimed pages.
1389 *
1390 * If a zone is deemed to be full of pinned pages then just give it a light
1391 * scan then give up on it.
1392 */
Andrew Morton1742f192006-03-22 00:08:21 -08001393static unsigned long shrink_zones(int priority, struct zone **zones,
Andrew Morton05ff5132006-03-22 00:08:20 -08001394 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Andrew Morton05ff5132006-03-22 00:08:20 -08001396 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 int i;
1398
1399 for (i = 0; zones[i] != NULL; i++) {
1400 struct zone *zone = zones[i];
1401
Con Kolivasf3fe6512006-01-06 00:11:15 -08001402 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 continue;
1404
Paul Jackson9bf22292005-09-06 15:18:12 -07001405 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 continue;
1407
Christoph Lameter86959492006-03-22 00:08:18 -08001408 zone->temp_priority = priority;
1409 if (zone->prev_priority > priority)
1410 zone->prev_priority = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Christoph Lameter86959492006-03-22 00:08:18 -08001412 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 continue; /* Let kswapd poll it */
1414
Andrew Morton05ff5132006-03-22 00:08:20 -08001415 nr_reclaimed += shrink_zone(priority, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
Andrew Morton05ff5132006-03-22 00:08:20 -08001417 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
1420/*
1421 * This is the main entry point to direct page reclaim.
1422 *
1423 * If a full scan of the inactive list fails to free enough memory then we
1424 * are "out of memory" and something needs to be killed.
1425 *
1426 * If the caller is !__GFP_FS then the probability of a failure is reasonably
1427 * high - the zone may be full of dirty or under-writeback pages, which this
1428 * caller can't do much about. We kick pdflush and take explicit naps in the
1429 * hope that some of these pages can be written. But if the allocating task
1430 * holds filesystem locks which prevent writeout this might not work, and the
1431 * allocation attempt will fail.
1432 */
Andrew Morton69e05942006-03-22 00:08:19 -08001433unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
1435 int priority;
1436 int ret = 0;
Andrew Morton69e05942006-03-22 00:08:19 -08001437 unsigned long total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001438 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 struct reclaim_state *reclaim_state = current->reclaim_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 unsigned long lru_pages = 0;
1441 int i;
Andrew Morton179e9632006-03-22 00:08:18 -08001442 struct scan_control sc = {
1443 .gfp_mask = gfp_mask,
1444 .may_writepage = !laptop_mode,
1445 .swap_cluster_max = SWAP_CLUSTER_MAX,
1446 .may_swap = 1,
1447 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 inc_page_state(allocstall);
1450
1451 for (i = 0; zones[i] != NULL; i++) {
1452 struct zone *zone = zones[i];
1453
Paul Jackson9bf22292005-09-06 15:18:12 -07001454 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 continue;
1456
1457 zone->temp_priority = DEF_PRIORITY;
1458 lru_pages += zone->nr_active + zone->nr_inactive;
1459 }
1460
1461 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1462 sc.nr_mapped = read_page_state(nr_mapped);
1463 sc.nr_scanned = 0;
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001464 if (!priority)
1465 disable_swap_token();
Andrew Morton1742f192006-03-22 00:08:21 -08001466 nr_reclaimed += shrink_zones(priority, zones, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
1468 if (reclaim_state) {
Andrew Morton05ff5132006-03-22 00:08:20 -08001469 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 reclaim_state->reclaimed_slab = 0;
1471 }
1472 total_scanned += sc.nr_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001473 if (nr_reclaimed >= sc.swap_cluster_max) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 ret = 1;
1475 goto out;
1476 }
1477
1478 /*
1479 * Try to write back as many pages as we just scanned. This
1480 * tends to cause slow streaming writers to write data to the
1481 * disk smoothly, at the dirtying rate, which is nice. But
1482 * that's undesirable in laptop mode, where we *want* lumpy
1483 * writeout. So in laptop mode, write out the whole world.
1484 */
Andrew Morton179e9632006-03-22 00:08:18 -08001485 if (total_scanned > sc.swap_cluster_max +
1486 sc.swap_cluster_max / 2) {
Pekka J Enberg687a21c2005-06-28 20:44:55 -07001487 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 sc.may_writepage = 1;
1489 }
1490
1491 /* Take a nap, wait for some writeback to complete */
1492 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
1493 blk_congestion_wait(WRITE, HZ/10);
1494 }
1495out:
1496 for (i = 0; zones[i] != 0; i++) {
1497 struct zone *zone = zones[i];
1498
Paul Jackson9bf22292005-09-06 15:18:12 -07001499 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 continue;
1501
1502 zone->prev_priority = zone->temp_priority;
1503 }
1504 return ret;
1505}
1506
1507/*
1508 * For kswapd, balance_pgdat() will work across all this node's zones until
1509 * they are all at pages_high.
1510 *
1511 * If `nr_pages' is non-zero then it is the number of pages which are to be
1512 * reclaimed, regardless of the zone occupancies. This is a software suspend
1513 * special.
1514 *
1515 * Returns the number of pages which were actually freed.
1516 *
1517 * There is special handling here for zones which are full of pinned pages.
1518 * This can happen if the pages are all mlocked, or if they are all used by
1519 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1520 * What we do is to detect the case where all pages in the zone have been
1521 * scanned twice and there has been zero successful reclaim. Mark the zone as
1522 * dead and from now on, only perform a short scan. Basically we're polling
1523 * the zone for when the problem goes away.
1524 *
1525 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1526 * zones which have free_pages > pages_high, but once a zone is found to have
1527 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1528 * of the number of free pages in the lower zones. This interoperates with
1529 * the page allocator fallback scheme to ensure that aging of pages is balanced
1530 * across the zones.
1531 */
Andrew Morton69e05942006-03-22 00:08:19 -08001532static unsigned long balance_pgdat(pg_data_t *pgdat, unsigned long nr_pages,
1533 int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Andrew Morton69e05942006-03-22 00:08:19 -08001535 unsigned long to_free = nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 int all_zones_ok;
1537 int priority;
1538 int i;
Andrew Morton69e05942006-03-22 00:08:19 -08001539 unsigned long total_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001540 unsigned long nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 struct reclaim_state *reclaim_state = current->reclaim_state;
Andrew Morton179e9632006-03-22 00:08:18 -08001542 struct scan_control sc = {
1543 .gfp_mask = GFP_KERNEL,
1544 .may_swap = 1,
1545 .swap_cluster_max = nr_pages ? nr_pages : SWAP_CLUSTER_MAX,
1546 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548loop_again:
1549 total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001550 nr_reclaimed = 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001551 sc.may_writepage = !laptop_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 sc.nr_mapped = read_page_state(nr_mapped);
1553
1554 inc_page_state(pageoutrun);
1555
1556 for (i = 0; i < pgdat->nr_zones; i++) {
1557 struct zone *zone = pgdat->node_zones + i;
1558
1559 zone->temp_priority = DEF_PRIORITY;
1560 }
1561
1562 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1563 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1564 unsigned long lru_pages = 0;
1565
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001566 /* The swap token gets in the way of swapout... */
1567 if (!priority)
1568 disable_swap_token();
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 all_zones_ok = 1;
1571
1572 if (nr_pages == 0) {
1573 /*
1574 * Scan in the highmem->dma direction for the highest
1575 * zone which needs scanning
1576 */
1577 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1578 struct zone *zone = pgdat->node_zones + i;
1579
Con Kolivasf3fe6512006-01-06 00:11:15 -08001580 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 continue;
1582
1583 if (zone->all_unreclaimable &&
1584 priority != DEF_PRIORITY)
1585 continue;
1586
1587 if (!zone_watermark_ok(zone, order,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001588 zone->pages_high, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 end_zone = i;
1590 goto scan;
1591 }
1592 }
1593 goto out;
1594 } else {
1595 end_zone = pgdat->nr_zones - 1;
1596 }
1597scan:
1598 for (i = 0; i <= end_zone; i++) {
1599 struct zone *zone = pgdat->node_zones + i;
1600
1601 lru_pages += zone->nr_active + zone->nr_inactive;
1602 }
1603
1604 /*
1605 * Now scan the zone in the dma->highmem direction, stopping
1606 * at the last zone which needs scanning.
1607 *
1608 * We do this because the page allocator works in the opposite
1609 * direction. This prevents the page allocator from allocating
1610 * pages behind kswapd's direction of progress, which would
1611 * cause too much scanning of the lower zones.
1612 */
1613 for (i = 0; i <= end_zone; i++) {
1614 struct zone *zone = pgdat->node_zones + i;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001615 int nr_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Con Kolivasf3fe6512006-01-06 00:11:15 -08001617 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 continue;
1619
1620 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1621 continue;
1622
1623 if (nr_pages == 0) { /* Not software suspend */
1624 if (!zone_watermark_ok(zone, order,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001625 zone->pages_high, end_zone, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 all_zones_ok = 0;
1627 }
1628 zone->temp_priority = priority;
1629 if (zone->prev_priority > priority)
1630 zone->prev_priority = priority;
1631 sc.nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001632 nr_reclaimed += shrink_zone(priority, zone, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 reclaim_state->reclaimed_slab = 0;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001634 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1635 lru_pages);
Andrew Morton05ff5132006-03-22 00:08:20 -08001636 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 total_scanned += sc.nr_scanned;
1638 if (zone->all_unreclaimable)
1639 continue;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001640 if (nr_slab == 0 && zone->pages_scanned >=
1641 (zone->nr_active + zone->nr_inactive) * 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 zone->all_unreclaimable = 1;
1643 /*
1644 * If we've done a decent amount of scanning and
1645 * the reclaim ratio is low, start doing writepage
1646 * even in laptop mode
1647 */
1648 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
Andrew Morton05ff5132006-03-22 00:08:20 -08001649 total_scanned > nr_reclaimed + nr_reclaimed / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 sc.may_writepage = 1;
1651 }
Andrew Morton05ff5132006-03-22 00:08:20 -08001652 if (nr_pages && to_free > nr_reclaimed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 continue; /* swsusp: need to do more work */
1654 if (all_zones_ok)
1655 break; /* kswapd: all done */
1656 /*
1657 * OK, kswapd is getting into trouble. Take a nap, then take
1658 * another pass across the zones.
1659 */
1660 if (total_scanned && priority < DEF_PRIORITY - 2)
1661 blk_congestion_wait(WRITE, HZ/10);
1662
1663 /*
1664 * We do this so kswapd doesn't build up large priorities for
1665 * example when it is freeing in parallel with allocators. It
1666 * matches the direct reclaim path behaviour in terms of impact
1667 * on zone->*_priority.
1668 */
Andrew Morton05ff5132006-03-22 00:08:20 -08001669 if ((nr_reclaimed >= SWAP_CLUSTER_MAX) && !nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 break;
1671 }
1672out:
1673 for (i = 0; i < pgdat->nr_zones; i++) {
1674 struct zone *zone = pgdat->node_zones + i;
1675
1676 zone->prev_priority = zone->temp_priority;
1677 }
1678 if (!all_zones_ok) {
1679 cond_resched();
1680 goto loop_again;
1681 }
1682
Andrew Morton05ff5132006-03-22 00:08:20 -08001683 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
1686/*
1687 * The background pageout daemon, started as a kernel thread
1688 * from the init process.
1689 *
1690 * This basically trickles out pages so that we have _some_
1691 * free memory available even if there is no other activity
1692 * that frees anything up. This is needed for things like routing
1693 * etc, where we otherwise might have all activity going on in
1694 * asynchronous contexts that cannot page things out.
1695 *
1696 * If there are applications that are active memory-allocators
1697 * (most normal use), this basically shouldn't matter.
1698 */
1699static int kswapd(void *p)
1700{
1701 unsigned long order;
1702 pg_data_t *pgdat = (pg_data_t*)p;
1703 struct task_struct *tsk = current;
1704 DEFINE_WAIT(wait);
1705 struct reclaim_state reclaim_state = {
1706 .reclaimed_slab = 0,
1707 };
1708 cpumask_t cpumask;
1709
1710 daemonize("kswapd%d", pgdat->node_id);
1711 cpumask = node_to_cpumask(pgdat->node_id);
1712 if (!cpus_empty(cpumask))
1713 set_cpus_allowed(tsk, cpumask);
1714 current->reclaim_state = &reclaim_state;
1715
1716 /*
1717 * Tell the memory management that we're a "memory allocator",
1718 * and that if we need more memory we should get access to it
1719 * regardless (see "__alloc_pages()"). "kswapd" should
1720 * never get caught in the normal page freeing logic.
1721 *
1722 * (Kswapd normally doesn't need memory anyway, but sometimes
1723 * you need a small amount of memory in order to be able to
1724 * page out something else, and this flag essentially protects
1725 * us from recursively trying to free more memory as we're
1726 * trying to free the first piece of memory in the first place).
1727 */
Christoph Lameter930d9152006-01-08 01:00:47 -08001728 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 order = 0;
1731 for ( ; ; ) {
1732 unsigned long new_order;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001733
1734 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1737 new_order = pgdat->kswapd_max_order;
1738 pgdat->kswapd_max_order = 0;
1739 if (order < new_order) {
1740 /*
1741 * Don't sleep if someone wants a larger 'order'
1742 * allocation
1743 */
1744 order = new_order;
1745 } else {
1746 schedule();
1747 order = pgdat->kswapd_max_order;
1748 }
1749 finish_wait(&pgdat->kswapd_wait, &wait);
1750
1751 balance_pgdat(pgdat, 0, order);
1752 }
1753 return 0;
1754}
1755
1756/*
1757 * A zone is low on free memory, so wake its kswapd task to service it.
1758 */
1759void wakeup_kswapd(struct zone *zone, int order)
1760{
1761 pg_data_t *pgdat;
1762
Con Kolivasf3fe6512006-01-06 00:11:15 -08001763 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return;
1765
1766 pgdat = zone->zone_pgdat;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001767 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return;
1769 if (pgdat->kswapd_max_order < order)
1770 pgdat->kswapd_max_order = order;
Paul Jackson9bf22292005-09-06 15:18:12 -07001771 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001773 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001775 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
1778#ifdef CONFIG_PM
1779/*
1780 * Try to free `nr_pages' of memory, system-wide. Returns the number of freed
1781 * pages.
1782 */
Andrew Morton69e05942006-03-22 00:08:19 -08001783unsigned long shrink_all_memory(unsigned long nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
1785 pg_data_t *pgdat;
Andrew Morton69e05942006-03-22 00:08:19 -08001786 unsigned long nr_to_free = nr_pages;
1787 unsigned long ret = 0;
Rafael J. Wysocki248a0302006-03-22 00:09:04 -08001788 unsigned retry = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 struct reclaim_state reclaim_state = {
1790 .reclaimed_slab = 0,
1791 };
1792
1793 current->reclaim_state = &reclaim_state;
Rafael J. Wysocki248a0302006-03-22 00:09:04 -08001794repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 for_each_pgdat(pgdat) {
Andrew Morton69e05942006-03-22 00:08:19 -08001796 unsigned long freed;
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 freed = balance_pgdat(pgdat, nr_to_free, 0);
1799 ret += freed;
1800 nr_to_free -= freed;
Andrew Morton69e05942006-03-22 00:08:19 -08001801 if ((long)nr_to_free <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 break;
1803 }
Rafael J. Wysocki248a0302006-03-22 00:09:04 -08001804 if (retry-- && ret < nr_pages) {
1805 blk_congestion_wait(WRITE, HZ/5);
1806 goto repeat;
1807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 current->reclaim_state = NULL;
1809 return ret;
1810}
1811#endif
1812
1813#ifdef CONFIG_HOTPLUG_CPU
1814/* It's optimal to keep kswapds on the same CPUs as their memory, but
1815 not required for correctness. So if the last cpu in a node goes
1816 away, we get changed to run anywhere: as the first one comes back,
1817 restore their cpu bindings. */
1818static int __devinit cpu_callback(struct notifier_block *nfb,
Andrew Morton69e05942006-03-22 00:08:19 -08001819 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
1821 pg_data_t *pgdat;
1822 cpumask_t mask;
1823
1824 if (action == CPU_ONLINE) {
1825 for_each_pgdat(pgdat) {
1826 mask = node_to_cpumask(pgdat->node_id);
1827 if (any_online_cpu(mask) != NR_CPUS)
1828 /* One of our CPUs online: restore mask */
1829 set_cpus_allowed(pgdat->kswapd, mask);
1830 }
1831 }
1832 return NOTIFY_OK;
1833}
1834#endif /* CONFIG_HOTPLUG_CPU */
1835
1836static int __init kswapd_init(void)
1837{
1838 pg_data_t *pgdat;
Andrew Morton69e05942006-03-22 00:08:19 -08001839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 swap_setup();
Andrew Morton69e05942006-03-22 00:08:19 -08001841 for_each_pgdat(pgdat) {
1842 pid_t pid;
1843
1844 pid = kernel_thread(kswapd, pgdat, CLONE_KERNEL);
1845 BUG_ON(pid < 0);
1846 pgdat->kswapd = find_task_by_pid(pid);
1847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 total_memory = nr_free_pagecache_pages();
1849 hotcpu_notifier(cpu_callback, 0);
1850 return 0;
1851}
1852
1853module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001854
1855#ifdef CONFIG_NUMA
1856/*
1857 * Zone reclaim mode
1858 *
1859 * If non-zero call zone_reclaim when the number of free pages falls below
1860 * the watermarks.
1861 *
1862 * In the future we may add flags to the mode. However, the page allocator
1863 * should only have to check that zone_reclaim_mode != 0 before calling
1864 * zone_reclaim().
1865 */
1866int zone_reclaim_mode __read_mostly;
1867
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001868#define RECLAIM_OFF 0
1869#define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1870#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1871#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001872#define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001873
Christoph Lameter9eeff232006-01-18 17:42:31 -08001874/*
1875 * Mininum time between zone reclaim scans
1876 */
Christoph Lameter2a11ff02006-02-01 03:05:33 -08001877int zone_reclaim_interval __read_mostly = 30*HZ;
Christoph Lametera92f7122006-02-01 03:05:32 -08001878
1879/*
1880 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1881 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1882 * a zone.
1883 */
1884#define ZONE_RECLAIM_PRIORITY 4
1885
Christoph Lameter9eeff232006-01-18 17:42:31 -08001886/*
1887 * Try to free up some pages from this zone through reclaim.
1888 */
Andrew Morton179e9632006-03-22 00:08:18 -08001889static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001890{
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001891 /* Minimum pages needed in order to stay on node */
Andrew Morton69e05942006-03-22 00:08:19 -08001892 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001893 struct task_struct *p = current;
1894 struct reclaim_state reclaim_state;
Christoph Lameter86959492006-03-22 00:08:18 -08001895 int priority;
Andrew Morton05ff5132006-03-22 00:08:20 -08001896 unsigned long nr_reclaimed = 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001897 struct scan_control sc = {
1898 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1899 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
1900 .nr_mapped = read_page_state(nr_mapped),
Andrew Morton69e05942006-03-22 00:08:19 -08001901 .swap_cluster_max = max_t(unsigned long, nr_pages,
1902 SWAP_CLUSTER_MAX),
Andrew Morton179e9632006-03-22 00:08:18 -08001903 .gfp_mask = gfp_mask,
1904 };
Christoph Lameter9eeff232006-01-18 17:42:31 -08001905
1906 disable_swap_token();
Christoph Lameter9eeff232006-01-18 17:42:31 -08001907 cond_resched();
Christoph Lameterd4f77962006-02-24 13:04:22 -08001908 /*
1909 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1910 * and we also need to be able to write out pages for RECLAIM_WRITE
1911 * and RECLAIM_SWAP.
1912 */
1913 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001914 reclaim_state.reclaimed_slab = 0;
1915 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db232006-02-01 03:05:29 -08001916
Christoph Lametera92f7122006-02-01 03:05:32 -08001917 /*
1918 * Free memory by calling shrink zone with increasing priorities
1919 * until we have enough memory freed.
1920 */
Christoph Lameter86959492006-03-22 00:08:18 -08001921 priority = ZONE_RECLAIM_PRIORITY;
Christoph Lametera92f7122006-02-01 03:05:32 -08001922 do {
Andrew Morton05ff5132006-03-22 00:08:20 -08001923 nr_reclaimed += shrink_zone(priority, zone, &sc);
Christoph Lameter86959492006-03-22 00:08:18 -08001924 priority--;
Andrew Morton05ff5132006-03-22 00:08:20 -08001925 } while (priority >= 0 && nr_reclaimed < nr_pages);
Christoph Lameterc84db232006-02-01 03:05:29 -08001926
Andrew Morton05ff5132006-03-22 00:08:20 -08001927 if (nr_reclaimed < nr_pages && (zone_reclaim_mode & RECLAIM_SLAB)) {
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001928 /*
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001929 * shrink_slab() does not currently allow us to determine how
1930 * many pages were freed in this zone. So we just shake the slab
1931 * a bit and then go off node for this particular allocation
1932 * despite possibly having freed enough memory to allocate in
1933 * this zone. If we freed local memory then the next
1934 * allocations will be local again.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001935 *
1936 * shrink_slab will free memory on all zones and may take
1937 * a long time.
1938 */
1939 shrink_slab(sc.nr_scanned, gfp_mask, order);
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001940 }
1941
Christoph Lameter9eeff232006-01-18 17:42:31 -08001942 p->reclaim_state = NULL;
Christoph Lameterd4f77962006-02-24 13:04:22 -08001943 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
Christoph Lameter9eeff232006-01-18 17:42:31 -08001944
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001945 if (nr_reclaimed == 0) {
1946 /*
1947 * We were unable to reclaim enough pages to stay on node. We
1948 * now allow off node accesses for a certain time period before
1949 * trying again to reclaim pages from the local zone.
1950 */
Christoph Lameter9eeff232006-01-18 17:42:31 -08001951 zone->last_unsuccessful_zone_reclaim = jiffies;
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001952 }
Christoph Lameter9eeff232006-01-18 17:42:31 -08001953
Andrew Morton05ff5132006-03-22 00:08:20 -08001954 return nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001955}
Andrew Morton179e9632006-03-22 00:08:18 -08001956
1957int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1958{
1959 cpumask_t mask;
1960 int node_id;
1961
1962 /*
1963 * Do not reclaim if there was a recent unsuccessful attempt at zone
1964 * reclaim. In that case we let allocations go off node for the
1965 * zone_reclaim_interval. Otherwise we would scan for each off-node
1966 * page allocation.
1967 */
1968 if (time_before(jiffies,
1969 zone->last_unsuccessful_zone_reclaim + zone_reclaim_interval))
1970 return 0;
1971
1972 /*
1973 * Avoid concurrent zone reclaims, do not reclaim in a zone that does
1974 * not have reclaimable pages and if we should not delay the allocation
1975 * then do not scan.
1976 */
1977 if (!(gfp_mask & __GFP_WAIT) ||
1978 zone->all_unreclaimable ||
1979 atomic_read(&zone->reclaim_in_progress) > 0 ||
1980 (current->flags & PF_MEMALLOC))
1981 return 0;
1982
1983 /*
1984 * Only run zone reclaim on the local zone or on zones that do not
1985 * have associated processors. This will favor the local processor
1986 * over remote processors and spread off node memory allocations
1987 * as wide as possible.
1988 */
1989 node_id = zone->zone_pgdat->node_id;
1990 mask = node_to_cpumask(node_id);
1991 if (!cpus_empty(mask) && node_id != numa_node_id())
1992 return 0;
1993 return __zone_reclaim(zone, gfp_mask, order);
1994}
Christoph Lameter9eeff232006-01-18 17:42:31 -08001995#endif