blob: 40fb37828e8c4993bbc848b816d84fc279299762 [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 {
55 /* Ask refill_inactive_zone, or shrink_cache to scan this many pages */
56 unsigned long nr_to_scan;
57
58 /* Incremented by the number of inactive pages that were scanned */
59 unsigned long nr_scanned;
60
61 /* Incremented by the number of pages reclaimed */
62 unsigned long nr_reclaimed;
63
64 unsigned long nr_mapped; /* From page_state */
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 /* Ask shrink_caches, or shrink_zone to scan at this priority */
67 unsigned int priority;
68
69 /* This context's GFP mask */
Al Viro6daa0e22005-10-21 03:18:50 -040070 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 int may_writepage;
73
Christoph Lameterf1fd1062006-01-18 17:42:30 -080074 /* Can pages be swapped as part of reclaim? */
75 int may_swap;
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
78 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
79 * In this context, it doesn't matter that we scan the
80 * whole list at once. */
81 int swap_cluster_max;
82};
83
84/*
85 * The list of shrinker callbacks used by to apply pressure to
86 * ageable caches.
87 */
88struct shrinker {
89 shrinker_t shrinker;
90 struct list_head list;
91 int seeks; /* seeks to recreate an obj */
92 long nr; /* objs pending delete */
93};
94
95#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
96
97#ifdef ARCH_HAS_PREFETCH
98#define prefetch_prev_lru_page(_page, _base, _field) \
99 do { \
100 if ((_page)->lru.prev != _base) { \
101 struct page *prev; \
102 \
103 prev = lru_to_page(&(_page->lru)); \
104 prefetch(&prev->_field); \
105 } \
106 } while (0)
107#else
108#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
109#endif
110
111#ifdef ARCH_HAS_PREFETCHW
112#define prefetchw_prev_lru_page(_page, _base, _field) \
113 do { \
114 if ((_page)->lru.prev != _base) { \
115 struct page *prev; \
116 \
117 prev = lru_to_page(&(_page->lru)); \
118 prefetchw(&prev->_field); \
119 } \
120 } while (0)
121#else
122#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
123#endif
124
125/*
126 * From 0 .. 100. Higher means more swappy.
127 */
128int vm_swappiness = 60;
129static long total_memory;
130
131static LIST_HEAD(shrinker_list);
132static DECLARE_RWSEM(shrinker_rwsem);
133
134/*
135 * Add a shrinker callback to be called from the vm
136 */
137struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
138{
139 struct shrinker *shrinker;
140
141 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
142 if (shrinker) {
143 shrinker->shrinker = theshrinker;
144 shrinker->seeks = seeks;
145 shrinker->nr = 0;
146 down_write(&shrinker_rwsem);
147 list_add_tail(&shrinker->list, &shrinker_list);
148 up_write(&shrinker_rwsem);
149 }
150 return shrinker;
151}
152EXPORT_SYMBOL(set_shrinker);
153
154/*
155 * Remove one
156 */
157void remove_shrinker(struct shrinker *shrinker)
158{
159 down_write(&shrinker_rwsem);
160 list_del(&shrinker->list);
161 up_write(&shrinker_rwsem);
162 kfree(shrinker);
163}
164EXPORT_SYMBOL(remove_shrinker);
165
166#define SHRINK_BATCH 128
167/*
168 * Call the shrink functions to age shrinkable caches
169 *
170 * Here we assume it costs one seek to replace a lru page and that it also
171 * takes a seek to recreate a cache object. With this in mind we age equal
172 * percentages of the lru and ageable caches. This should balance the seeks
173 * generated by these structures.
174 *
175 * If the vm encounted mapped pages on the LRU it increase the pressure on
176 * slab to avoid swapping.
177 *
178 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
179 *
180 * `lru_pages' represents the number of on-LRU pages in all the zones which
181 * are eligible for the caller's allocation attempt. It is used for balancing
182 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700183 *
184 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
Andrew Morton9d0243b2006-01-08 01:00:39 -0800186int shrink_slab(unsigned long scanned, gfp_t gfp_mask, unsigned long lru_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 struct shrinker *shrinker;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700189 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 if (scanned == 0)
192 scanned = SWAP_CLUSTER_MAX;
193
194 if (!down_read_trylock(&shrinker_rwsem))
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700195 return 1; /* Assume we'll be able to shrink next time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 list_for_each_entry(shrinker, &shrinker_list, list) {
198 unsigned long long delta;
199 unsigned long total_scan;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800200 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 delta = (4 * scanned) / shrinker->seeks;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800203 delta *= max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 do_div(delta, lru_pages + 1);
205 shrinker->nr += delta;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800206 if (shrinker->nr < 0) {
207 printk(KERN_ERR "%s: nr=%ld\n",
208 __FUNCTION__, shrinker->nr);
209 shrinker->nr = max_pass;
210 }
211
212 /*
213 * Avoid risking looping forever due to too large nr value:
214 * never try to free more than twice the estimate number of
215 * freeable entries.
216 */
217 if (shrinker->nr > max_pass * 2)
218 shrinker->nr = max_pass * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 total_scan = shrinker->nr;
221 shrinker->nr = 0;
222
223 while (total_scan >= SHRINK_BATCH) {
224 long this_scan = SHRINK_BATCH;
225 int shrink_ret;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700226 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700228 nr_before = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
230 if (shrink_ret == -1)
231 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700232 if (shrink_ret < nr_before)
233 ret += nr_before - shrink_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 mod_page_state(slabs_scanned, this_scan);
235 total_scan -= this_scan;
236
237 cond_resched();
238 }
239
240 shrinker->nr += total_scan;
241 }
242 up_read(&shrinker_rwsem);
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700243 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246/* Called without lock on whether page is mapped, so answer is unstable */
247static inline int page_mapping_inuse(struct page *page)
248{
249 struct address_space *mapping;
250
251 /* Page is in somebody's page tables. */
252 if (page_mapped(page))
253 return 1;
254
255 /* Be more reluctant to reclaim swapcache than pagecache */
256 if (PageSwapCache(page))
257 return 1;
258
259 mapping = page_mapping(page);
260 if (!mapping)
261 return 0;
262
263 /* File is mmap'd by somebody? */
264 return mapping_mapped(mapping);
265}
266
267static inline int is_page_cache_freeable(struct page *page)
268{
269 return page_count(page) - !!PagePrivate(page) == 2;
270}
271
272static int may_write_to_queue(struct backing_dev_info *bdi)
273{
Christoph Lameter930d9152006-01-08 01:00:47 -0800274 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return 1;
276 if (!bdi_write_congested(bdi))
277 return 1;
278 if (bdi == current->backing_dev_info)
279 return 1;
280 return 0;
281}
282
283/*
284 * We detected a synchronous write error writing a page out. Probably
285 * -ENOSPC. We need to propagate that into the address_space for a subsequent
286 * fsync(), msync() or close().
287 *
288 * The tricky part is that after writepage we cannot touch the mapping: nothing
289 * prevents it from being freed up. But we have a ref on the page and once
290 * that page is locked, the mapping is pinned.
291 *
292 * We're allowed to run sleeping lock_page() here because we know the caller has
293 * __GFP_FS.
294 */
295static void handle_write_error(struct address_space *mapping,
296 struct page *page, int error)
297{
298 lock_page(page);
299 if (page_mapping(page) == mapping) {
300 if (error == -ENOSPC)
301 set_bit(AS_ENOSPC, &mapping->flags);
302 else
303 set_bit(AS_EIO, &mapping->flags);
304 }
305 unlock_page(page);
306}
307
308/*
309 * pageout is called by shrink_list() for each dirty page. Calls ->writepage().
310 */
311static pageout_t pageout(struct page *page, struct address_space *mapping)
312{
313 /*
314 * If the page is dirty, only perform writeback if that write
315 * will be non-blocking. To prevent this allocation from being
316 * stalled by pagecache activity. But note that there may be
317 * stalls if we need to run get_block(). We could test
318 * PagePrivate for that.
319 *
320 * If this process is currently in generic_file_write() against
321 * this page's queue, we can perform writeback even if that
322 * will block.
323 *
324 * If the page is swapcache, write it back even if that would
325 * block, for some throttling. This happens by accident, because
326 * swap_backing_dev_info is bust: it doesn't reflect the
327 * congestion state of the swapdevs. Easy to fix, if needed.
328 * See swapfile.c:page_queue_congested().
329 */
330 if (!is_page_cache_freeable(page))
331 return PAGE_KEEP;
332 if (!mapping) {
333 /*
334 * Some data journaling orphaned pages can have
335 * page->mapping == NULL while being dirty with clean buffers.
336 */
akpm@osdl.org323aca62005-04-16 15:24:06 -0700337 if (PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (try_to_free_buffers(page)) {
339 ClearPageDirty(page);
340 printk("%s: orphaned page\n", __FUNCTION__);
341 return PAGE_CLEAN;
342 }
343 }
344 return PAGE_KEEP;
345 }
346 if (mapping->a_ops->writepage == NULL)
347 return PAGE_ACTIVATE;
348 if (!may_write_to_queue(mapping->backing_dev_info))
349 return PAGE_KEEP;
350
351 if (clear_page_dirty_for_io(page)) {
352 int res;
353 struct writeback_control wbc = {
354 .sync_mode = WB_SYNC_NONE,
355 .nr_to_write = SWAP_CLUSTER_MAX,
356 .nonblocking = 1,
357 .for_reclaim = 1,
358 };
359
360 SetPageReclaim(page);
361 res = mapping->a_ops->writepage(page, &wbc);
362 if (res < 0)
363 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800364 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 ClearPageReclaim(page);
366 return PAGE_ACTIVATE;
367 }
368 if (!PageWriteback(page)) {
369 /* synchronous write or broken a_ops? */
370 ClearPageReclaim(page);
371 }
372
373 return PAGE_SUCCESS;
374 }
375
376 return PAGE_CLEAN;
377}
378
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800379static int remove_mapping(struct address_space *mapping, struct page *page)
380{
381 if (!mapping)
382 return 0; /* truncate got there first */
383
384 write_lock_irq(&mapping->tree_lock);
385
386 /*
387 * The non-racy check for busy page. It is critical to check
388 * PageDirty _after_ making sure that the page is freeable and
389 * not in use by anybody. (pagecache + us == 2)
390 */
391 if (unlikely(page_count(page) != 2))
392 goto cannot_free;
393 smp_rmb();
394 if (unlikely(PageDirty(page)))
395 goto cannot_free;
396
397 if (PageSwapCache(page)) {
398 swp_entry_t swap = { .val = page_private(page) };
399 __delete_from_swap_cache(page);
400 write_unlock_irq(&mapping->tree_lock);
401 swap_free(swap);
402 __put_page(page); /* The pagecache ref */
403 return 1;
404 }
405
406 __remove_from_page_cache(page);
407 write_unlock_irq(&mapping->tree_lock);
408 __put_page(page);
409 return 1;
410
411cannot_free:
412 write_unlock_irq(&mapping->tree_lock);
413 return 0;
414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/*
417 * shrink_list adds the number of reclaimed pages to sc->nr_reclaimed
418 */
419static int shrink_list(struct list_head *page_list, struct scan_control *sc)
420{
421 LIST_HEAD(ret_pages);
422 struct pagevec freed_pvec;
423 int pgactivate = 0;
424 int reclaimed = 0;
425
426 cond_resched();
427
428 pagevec_init(&freed_pvec, 1);
429 while (!list_empty(page_list)) {
430 struct address_space *mapping;
431 struct page *page;
432 int may_enter_fs;
433 int referenced;
434
435 cond_resched();
436
437 page = lru_to_page(page_list);
438 list_del(&page->lru);
439
440 if (TestSetPageLocked(page))
441 goto keep;
442
443 BUG_ON(PageActive(page));
444
445 sc->nr_scanned++;
Christoph Lameter80e43422006-02-11 17:55:53 -0800446
447 if (!sc->may_swap && page_mapped(page))
448 goto keep_locked;
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /* Double the slab pressure for mapped and swapcache pages */
451 if (page_mapped(page) || PageSwapCache(page))
452 sc->nr_scanned++;
453
454 if (PageWriteback(page))
455 goto keep_locked;
456
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800457 referenced = page_referenced(page, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 /* In active use or really unfreeable? Activate it. */
459 if (referenced && page_mapping_inuse(page))
460 goto activate_locked;
461
462#ifdef CONFIG_SWAP
463 /*
464 * Anonymous process memory has backing store?
465 * Try to allocate it some swap space here.
466 */
Lee Schermerhornc3400102005-10-29 18:15:51 -0700467 if (PageAnon(page) && !PageSwapCache(page)) {
Christoph Lameterf1fd1062006-01-18 17:42:30 -0800468 if (!sc->may_swap)
469 goto keep_locked;
Christoph Lameter1480a542006-01-08 01:00:53 -0800470 if (!add_to_swap(page, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 goto activate_locked;
472 }
473#endif /* CONFIG_SWAP */
474
475 mapping = page_mapping(page);
476 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
477 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
478
479 /*
480 * The page is mapped into the page tables of one or more
481 * processes. Try to unmap it here.
482 */
483 if (page_mapped(page) && mapping) {
Christoph Lameteraa3f18b2006-02-01 03:05:32 -0800484 /*
485 * No unmapping if we do not swap
486 */
487 if (!sc->may_swap)
488 goto keep_locked;
489
Christoph Lametera48d07a2006-02-01 03:05:38 -0800490 switch (try_to_unmap(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 case SWAP_FAIL:
492 goto activate_locked;
493 case SWAP_AGAIN:
494 goto keep_locked;
495 case SWAP_SUCCESS:
496 ; /* try to free the page below */
497 }
498 }
499
500 if (PageDirty(page)) {
501 if (referenced)
502 goto keep_locked;
503 if (!may_enter_fs)
504 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -0800505 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto keep_locked;
507
508 /* Page is dirty, try to write it out here */
509 switch(pageout(page, mapping)) {
510 case PAGE_KEEP:
511 goto keep_locked;
512 case PAGE_ACTIVATE:
513 goto activate_locked;
514 case PAGE_SUCCESS:
515 if (PageWriteback(page) || PageDirty(page))
516 goto keep;
517 /*
518 * A synchronous write - probably a ramdisk. Go
519 * ahead and try to reclaim the page.
520 */
521 if (TestSetPageLocked(page))
522 goto keep;
523 if (PageDirty(page) || PageWriteback(page))
524 goto keep_locked;
525 mapping = page_mapping(page);
526 case PAGE_CLEAN:
527 ; /* try to free the page below */
528 }
529 }
530
531 /*
532 * If the page has buffers, try to free the buffer mappings
533 * associated with this page. If we succeed we try to free
534 * the page as well.
535 *
536 * We do this even if the page is PageDirty().
537 * try_to_release_page() does not perform I/O, but it is
538 * possible for a page to have PageDirty set, but it is actually
539 * clean (all its buffers are clean). This happens if the
540 * buffers were written out directly, with submit_bh(). ext3
541 * will do this, as well as the blockdev mapping.
542 * try_to_release_page() will discover that cleanness and will
543 * drop the buffers and mark the page clean - it can be freed.
544 *
545 * Rarely, pages can have buffers and no ->mapping. These are
546 * the pages which were not successfully invalidated in
547 * truncate_complete_page(). We try to drop those buffers here
548 * and if that worked, and the page is no longer mapped into
549 * process address space (page_count == 1) it can be freed.
550 * Otherwise, leave the page on the LRU so it is swappable.
551 */
552 if (PagePrivate(page)) {
553 if (!try_to_release_page(page, sc->gfp_mask))
554 goto activate_locked;
555 if (!mapping && page_count(page) == 1)
556 goto free_it;
557 }
558
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800559 if (!remove_mapping(mapping, page))
560 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562free_it:
563 unlock_page(page);
564 reclaimed++;
565 if (!pagevec_add(&freed_pvec, page))
566 __pagevec_release_nonlru(&freed_pvec);
567 continue;
568
569activate_locked:
570 SetPageActive(page);
571 pgactivate++;
572keep_locked:
573 unlock_page(page);
574keep:
575 list_add(&page->lru, &ret_pages);
576 BUG_ON(PageLRU(page));
577 }
578 list_splice(&ret_pages, page_list);
579 if (pagevec_count(&freed_pvec))
580 __pagevec_release_nonlru(&freed_pvec);
581 mod_page_state(pgactivate, pgactivate);
582 sc->nr_reclaimed += reclaimed;
583 return reclaimed;
584}
585
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800586#ifdef CONFIG_MIGRATION
Christoph Lameter8419c312006-01-08 01:00:52 -0800587static inline void move_to_lru(struct page *page)
588{
589 list_del(&page->lru);
590 if (PageActive(page)) {
591 /*
592 * lru_cache_add_active checks that
593 * the PG_active bit is off.
594 */
595 ClearPageActive(page);
596 lru_cache_add_active(page);
597 } else {
598 lru_cache_add(page);
599 }
600 put_page(page);
601}
602
603/*
Nick Piggin053837f2006-01-18 17:42:27 -0800604 * Add isolated pages on the list back to the LRU.
Christoph Lameter8419c312006-01-08 01:00:52 -0800605 *
606 * returns the number of pages put back.
607 */
608int putback_lru_pages(struct list_head *l)
609{
610 struct page *page;
611 struct page *page2;
612 int count = 0;
613
614 list_for_each_entry_safe(page, page2, l, lru) {
615 move_to_lru(page);
616 count++;
617 }
618 return count;
619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621/*
Christoph Lametere965f962006-02-01 03:05:41 -0800622 * Non migratable page
623 */
624int fail_migrate_page(struct page *newpage, struct page *page)
625{
626 return -EIO;
627}
628EXPORT_SYMBOL(fail_migrate_page);
629
630/*
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800631 * swapout a single page
632 * page is locked upon entry, unlocked on exit
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800633 */
634static int swap_page(struct page *page)
635{
636 struct address_space *mapping = page_mapping(page);
637
638 if (page_mapped(page) && mapping)
Christoph Lameter418aade2006-02-10 01:51:15 -0800639 if (try_to_unmap(page, 1) != SWAP_SUCCESS)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800640 goto unlock_retry;
641
642 if (PageDirty(page)) {
643 /* Page is dirty, try to write it out here */
644 switch(pageout(page, mapping)) {
645 case PAGE_KEEP:
646 case PAGE_ACTIVATE:
647 goto unlock_retry;
648
649 case PAGE_SUCCESS:
650 goto retry;
651
652 case PAGE_CLEAN:
653 ; /* try to free the page below */
654 }
655 }
656
657 if (PagePrivate(page)) {
658 if (!try_to_release_page(page, GFP_KERNEL) ||
659 (!mapping && page_count(page) == 1))
660 goto unlock_retry;
661 }
662
663 if (remove_mapping(mapping, page)) {
664 /* Success */
665 unlock_page(page);
666 return 0;
667 }
668
669unlock_retry:
670 unlock_page(page);
671
672retry:
Christoph Lameterd0d96322006-01-08 01:00:55 -0800673 return -EAGAIN;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800674}
Christoph Lametere965f962006-02-01 03:05:41 -0800675EXPORT_SYMBOL(swap_page);
Christoph Lametera48d07a2006-02-01 03:05:38 -0800676
677/*
678 * Page migration was first developed in the context of the memory hotplug
679 * project. The main authors of the migration code are:
680 *
681 * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
682 * Hirokazu Takahashi <taka@valinux.co.jp>
683 * Dave Hansen <haveblue@us.ibm.com>
684 * Christoph Lameter <clameter@sgi.com>
685 */
686
687/*
688 * Remove references for a page and establish the new page with the correct
689 * basic settings to be able to stop accesses to the page.
690 */
Christoph Lametere965f962006-02-01 03:05:41 -0800691int migrate_page_remove_references(struct page *newpage,
Christoph Lametera48d07a2006-02-01 03:05:38 -0800692 struct page *page, int nr_refs)
693{
694 struct address_space *mapping = page_mapping(page);
695 struct page **radix_pointer;
696
697 /*
698 * Avoid doing any of the following work if the page count
699 * indicates that the page is in use or truncate has removed
700 * the page.
701 */
702 if (!mapping || page_mapcount(page) + nr_refs != page_count(page))
Christoph Lameter4983da02006-03-14 19:50:19 -0800703 return -EAGAIN;
Christoph Lametera48d07a2006-02-01 03:05:38 -0800704
705 /*
706 * Establish swap ptes for anonymous pages or destroy pte
707 * maps for files.
708 *
709 * In order to reestablish file backed mappings the fault handlers
710 * will take the radix tree_lock which may then be used to stop
711 * processses from accessing this page until the new page is ready.
712 *
713 * A process accessing via a swap pte (an anonymous page) will take a
714 * page_lock on the old page which will block the process until the
715 * migration attempt is complete. At that time the PageSwapCache bit
716 * will be examined. If the page was migrated then the PageSwapCache
717 * bit will be clear and the operation to retrieve the page will be
718 * retried which will find the new page in the radix tree. Then a new
719 * direct mapping may be generated based on the radix tree contents.
720 *
721 * If the page was not migrated then the PageSwapCache bit
722 * is still set and the operation may continue.
723 */
Christoph Lameter4983da02006-03-14 19:50:19 -0800724 if (try_to_unmap(page, 1) == SWAP_FAIL)
725 /* A vma has VM_LOCKED set -> Permanent failure */
726 return -EPERM;
Christoph Lametera48d07a2006-02-01 03:05:38 -0800727
728 /*
729 * Give up if we were unable to remove all mappings.
730 */
731 if (page_mapcount(page))
Christoph Lameter4983da02006-03-14 19:50:19 -0800732 return -EAGAIN;
Christoph Lametera48d07a2006-02-01 03:05:38 -0800733
734 write_lock_irq(&mapping->tree_lock);
735
736 radix_pointer = (struct page **)radix_tree_lookup_slot(
737 &mapping->page_tree,
738 page_index(page));
739
740 if (!page_mapping(page) || page_count(page) != nr_refs ||
741 *radix_pointer != page) {
742 write_unlock_irq(&mapping->tree_lock);
Christoph Lameter4983da02006-03-14 19:50:19 -0800743 return -EAGAIN;
Christoph Lametera48d07a2006-02-01 03:05:38 -0800744 }
745
746 /*
747 * Now we know that no one else is looking at the page.
748 *
749 * Certain minimal information about a page must be available
750 * in order for other subsystems to properly handle the page if they
751 * find it through the radix tree update before we are finished
752 * copying the page.
753 */
754 get_page(newpage);
755 newpage->index = page->index;
756 newpage->mapping = page->mapping;
757 if (PageSwapCache(page)) {
758 SetPageSwapCache(newpage);
759 set_page_private(newpage, page_private(page));
760 }
761
762 *radix_pointer = newpage;
763 __put_page(page);
764 write_unlock_irq(&mapping->tree_lock);
765
766 return 0;
767}
Christoph Lametere965f962006-02-01 03:05:41 -0800768EXPORT_SYMBOL(migrate_page_remove_references);
Christoph Lametera48d07a2006-02-01 03:05:38 -0800769
770/*
771 * Copy the page to its new location
772 */
773void migrate_page_copy(struct page *newpage, struct page *page)
774{
775 copy_highpage(newpage, page);
776
777 if (PageError(page))
778 SetPageError(newpage);
779 if (PageReferenced(page))
780 SetPageReferenced(newpage);
781 if (PageUptodate(page))
782 SetPageUptodate(newpage);
783 if (PageActive(page))
784 SetPageActive(newpage);
785 if (PageChecked(page))
786 SetPageChecked(newpage);
787 if (PageMappedToDisk(page))
788 SetPageMappedToDisk(newpage);
789
790 if (PageDirty(page)) {
791 clear_page_dirty_for_io(page);
792 set_page_dirty(newpage);
793 }
794
795 ClearPageSwapCache(page);
796 ClearPageActive(page);
797 ClearPagePrivate(page);
798 set_page_private(page, 0);
799 page->mapping = NULL;
800
801 /*
802 * If any waiters have accumulated on the new page then
803 * wake them up.
804 */
805 if (PageWriteback(newpage))
806 end_page_writeback(newpage);
807}
Christoph Lametere965f962006-02-01 03:05:41 -0800808EXPORT_SYMBOL(migrate_page_copy);
Christoph Lametera48d07a2006-02-01 03:05:38 -0800809
810/*
811 * Common logic to directly migrate a single page suitable for
812 * pages that do not use PagePrivate.
813 *
814 * Pages are locked upon entry and exit.
815 */
816int migrate_page(struct page *newpage, struct page *page)
817{
Christoph Lameter4983da02006-03-14 19:50:19 -0800818 int rc;
819
Christoph Lametera48d07a2006-02-01 03:05:38 -0800820 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
821
Christoph Lameter4983da02006-03-14 19:50:19 -0800822 rc = migrate_page_remove_references(newpage, page, 2);
823
824 if (rc)
825 return rc;
Christoph Lametera48d07a2006-02-01 03:05:38 -0800826
827 migrate_page_copy(newpage, page);
828
Christoph Lametera3351e52006-02-01 03:05:39 -0800829 /*
830 * Remove auxiliary swap entries and replace
831 * them with real ptes.
832 *
833 * Note that a real pte entry will allow processes that are not
834 * waiting on the page lock to use the new page via the page tables
835 * before the new page is unlocked.
836 */
837 remove_from_swap(newpage);
Christoph Lametera48d07a2006-02-01 03:05:38 -0800838 return 0;
839}
Christoph Lametere965f962006-02-01 03:05:41 -0800840EXPORT_SYMBOL(migrate_page);
Christoph Lametera48d07a2006-02-01 03:05:38 -0800841
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800842/*
843 * migrate_pages
844 *
845 * Two lists are passed to this function. The first list
846 * contains the pages isolated from the LRU to be migrated.
847 * The second list contains new pages that the pages isolated
848 * can be moved to. If the second list is NULL then all
849 * pages are swapped out.
850 *
851 * The function returns after 10 attempts or if no pages
Christoph Lameter418aade2006-02-10 01:51:15 -0800852 * are movable anymore because to has become empty
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800853 * or no retryable pages exist anymore.
854 *
Christoph Lameterd0d96322006-01-08 01:00:55 -0800855 * Return: Number of pages not migrated when "to" ran empty.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800856 */
Christoph Lameterd4984712006-01-08 01:00:55 -0800857int migrate_pages(struct list_head *from, struct list_head *to,
858 struct list_head *moved, struct list_head *failed)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800859{
860 int retry;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800861 int nr_failed = 0;
862 int pass = 0;
863 struct page *page;
864 struct page *page2;
865 int swapwrite = current->flags & PF_SWAPWRITE;
Christoph Lameterd0d96322006-01-08 01:00:55 -0800866 int rc;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800867
868 if (!swapwrite)
869 current->flags |= PF_SWAPWRITE;
870
871redo:
872 retry = 0;
873
Christoph Lameterd4984712006-01-08 01:00:55 -0800874 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lametera48d07a2006-02-01 03:05:38 -0800875 struct page *newpage = NULL;
876 struct address_space *mapping;
877
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800878 cond_resched();
879
Christoph Lameterd0d96322006-01-08 01:00:55 -0800880 rc = 0;
881 if (page_count(page) == 1)
Christoph Lameteree274972006-01-08 01:00:54 -0800882 /* page was freed from under us. So we are done. */
Christoph Lameterd0d96322006-01-08 01:00:55 -0800883 goto next;
884
Christoph Lametera48d07a2006-02-01 03:05:38 -0800885 if (to && list_empty(to))
886 break;
887
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800888 /*
889 * Skip locked pages during the first two passes to give the
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800890 * functions holding the lock time to release the page. Later we
891 * use lock_page() to have a higher chance of acquiring the
892 * lock.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800893 */
Christoph Lameterd0d96322006-01-08 01:00:55 -0800894 rc = -EAGAIN;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800895 if (pass > 2)
896 lock_page(page);
897 else
898 if (TestSetPageLocked(page))
Christoph Lameterd0d96322006-01-08 01:00:55 -0800899 goto next;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800900
901 /*
902 * Only wait on writeback if we have already done a pass where
903 * we we may have triggered writeouts for lots of pages.
904 */
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800905 if (pass > 0) {
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800906 wait_on_page_writeback(page);
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800907 } else {
Christoph Lameterd0d96322006-01-08 01:00:55 -0800908 if (PageWriteback(page))
909 goto unlock_page;
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800910 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800911
Christoph Lameterd0d96322006-01-08 01:00:55 -0800912 /*
913 * Anonymous pages must have swap cache references otherwise
914 * the information contained in the page maps cannot be
915 * preserved.
916 */
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800917 if (PageAnon(page) && !PageSwapCache(page)) {
Christoph Lameter1480a542006-01-08 01:00:53 -0800918 if (!add_to_swap(page, GFP_KERNEL)) {
Christoph Lameterd0d96322006-01-08 01:00:55 -0800919 rc = -ENOMEM;
920 goto unlock_page;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800921 }
922 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800923
Christoph Lametera48d07a2006-02-01 03:05:38 -0800924 if (!to) {
925 rc = swap_page(page);
926 goto next;
927 }
928
929 newpage = lru_to_page(to);
930 lock_page(newpage);
931
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800932 /*
Christoph Lametera48d07a2006-02-01 03:05:38 -0800933 * Pages are properly locked and writeback is complete.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800934 * Try to migrate the page.
935 */
Christoph Lametera48d07a2006-02-01 03:05:38 -0800936 mapping = page_mapping(page);
937 if (!mapping)
938 goto unlock_both;
939
Christoph Lametere965f962006-02-01 03:05:41 -0800940 if (mapping->a_ops->migratepage) {
Christoph Lameter418aade2006-02-10 01:51:15 -0800941 /*
942 * Most pages have a mapping and most filesystems
943 * should provide a migration function. Anonymous
944 * pages are part of swap space which also has its
945 * own migration function. This is the most common
946 * path for page migration.
947 */
Christoph Lametere965f962006-02-01 03:05:41 -0800948 rc = mapping->a_ops->migratepage(newpage, page);
949 goto unlock_both;
950 }
951
Christoph Lametera48d07a2006-02-01 03:05:38 -0800952 /*
Christoph Lameter418aade2006-02-10 01:51:15 -0800953 * Default handling if a filesystem does not provide
954 * a migration function. We can only migrate clean
955 * pages so try to write out any dirty pages first.
Christoph Lametera48d07a2006-02-01 03:05:38 -0800956 */
957 if (PageDirty(page)) {
958 switch (pageout(page, mapping)) {
959 case PAGE_KEEP:
960 case PAGE_ACTIVATE:
961 goto unlock_both;
962
963 case PAGE_SUCCESS:
964 unlock_page(newpage);
965 goto next;
966
967 case PAGE_CLEAN:
968 ; /* try to migrate the page below */
969 }
970 }
Christoph Lameter418aade2006-02-10 01:51:15 -0800971
Christoph Lametera48d07a2006-02-01 03:05:38 -0800972 /*
Christoph Lameter418aade2006-02-10 01:51:15 -0800973 * Buffers are managed in a filesystem specific way.
974 * We must have no buffers or drop them.
Christoph Lametera48d07a2006-02-01 03:05:38 -0800975 */
976 if (!page_has_buffers(page) ||
977 try_to_release_page(page, GFP_KERNEL)) {
978 rc = migrate_page(newpage, page);
979 goto unlock_both;
980 }
981
982 /*
983 * On early passes with mapped pages simply
984 * retry. There may be a lock held for some
985 * buffers that may go away. Later
986 * swap them out.
987 */
988 if (pass > 4) {
Christoph Lameter418aade2006-02-10 01:51:15 -0800989 /*
990 * Persistently unable to drop buffers..... As a
991 * measure of last resort we fall back to
992 * swap_page().
993 */
Christoph Lametera48d07a2006-02-01 03:05:38 -0800994 unlock_page(newpage);
995 newpage = NULL;
996 rc = swap_page(page);
997 goto next;
998 }
999
1000unlock_both:
1001 unlock_page(newpage);
Christoph Lameterd0d96322006-01-08 01:00:55 -08001002
1003unlock_page:
1004 unlock_page(page);
1005
1006next:
1007 if (rc == -EAGAIN) {
1008 retry++;
1009 } else if (rc) {
1010 /* Permanent failure */
1011 list_move(&page->lru, failed);
1012 nr_failed++;
1013 } else {
Christoph Lametera48d07a2006-02-01 03:05:38 -08001014 if (newpage) {
1015 /* Successful migration. Return page to LRU */
1016 move_to_lru(newpage);
1017 }
Christoph Lameterd4984712006-01-08 01:00:55 -08001018 list_move(&page->lru, moved);
Christoph Lameterd4984712006-01-08 01:00:55 -08001019 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001020 }
1021 if (retry && pass++ < 10)
1022 goto redo;
1023
1024 if (!swapwrite)
1025 current->flags &= ~PF_SWAPWRITE;
1026
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001027 return nr_failed + retry;
1028}
Christoph Lameter8419c312006-01-08 01:00:52 -08001029
Christoph Lameter8419c312006-01-08 01:00:52 -08001030/*
1031 * Isolate one page from the LRU lists and put it on the
Nick Piggin053837f2006-01-18 17:42:27 -08001032 * indicated list with elevated refcount.
Christoph Lameter8419c312006-01-08 01:00:52 -08001033 *
1034 * Result:
1035 * 0 = page not on LRU list
1036 * 1 = page removed from LRU list and added to the specified list.
Christoph Lameter8419c312006-01-08 01:00:52 -08001037 */
1038int isolate_lru_page(struct page *page)
1039{
Nick Piggin053837f2006-01-18 17:42:27 -08001040 int ret = 0;
Christoph Lameter8419c312006-01-08 01:00:52 -08001041
Nick Piggin053837f2006-01-18 17:42:27 -08001042 if (PageLRU(page)) {
1043 struct zone *zone = page_zone(page);
1044 spin_lock_irq(&zone->lru_lock);
Nick Piggin8d438f92006-03-22 00:07:59 -08001045 if (PageLRU(page)) {
Nick Piggin053837f2006-01-18 17:42:27 -08001046 ret = 1;
1047 get_page(page);
Nick Piggin8d438f92006-03-22 00:07:59 -08001048 ClearPageLRU(page);
Nick Piggin053837f2006-01-18 17:42:27 -08001049 if (PageActive(page))
1050 del_page_from_active_list(zone, page);
1051 else
1052 del_page_from_inactive_list(zone, page);
1053 }
1054 spin_unlock_irq(&zone->lru_lock);
Christoph Lameter8419c312006-01-08 01:00:52 -08001055 }
Nick Piggin053837f2006-01-18 17:42:27 -08001056
1057 return ret;
Christoph Lameter8419c312006-01-08 01:00:52 -08001058}
Christoph Lameter7cbe34c2006-01-08 01:00:49 -08001059#endif
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001060
1061/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 * zone->lru_lock is heavily contended. Some of the functions that
1063 * shrink the lists perform better by taking out a batch of pages
1064 * and working on them outside the LRU lock.
1065 *
1066 * For pagecache intensive workloads, this function is the hottest
1067 * spot in the kernel (apart from copy_*_user functions).
1068 *
1069 * Appropriate locks must be held before calling this function.
1070 *
1071 * @nr_to_scan: The number of pages to look through on the list.
1072 * @src: The LRU list to pull pages off.
1073 * @dst: The temp list to put pages on to.
1074 * @scanned: The number of pages that were scanned.
1075 *
1076 * returns how many pages were moved onto *@dst.
1077 */
1078static int isolate_lru_pages(int nr_to_scan, struct list_head *src,
1079 struct list_head *dst, int *scanned)
1080{
1081 int nr_taken = 0;
1082 struct page *page;
1083 int scan = 0;
1084
1085 while (scan++ < nr_to_scan && !list_empty(src)) {
1086 page = lru_to_page(src);
1087 prefetchw_prev_lru_page(page, src, flags);
1088
Nick Piggin8d438f92006-03-22 00:07:59 -08001089 BUG_ON(!PageLRU(page));
1090
Nick Piggin053837f2006-01-18 17:42:27 -08001091 list_del(&page->lru);
Nick Piggin46453a62006-03-22 00:07:58 -08001092 if (unlikely(get_page_testone(page))) {
Nick Piggin053837f2006-01-18 17:42:27 -08001093 /*
1094 * It is being freed elsewhere
1095 */
1096 __put_page(page);
Nick Piggin053837f2006-01-18 17:42:27 -08001097 list_add(&page->lru, src);
1098 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
Nick Piggin46453a62006-03-22 00:07:58 -08001100
1101 /*
1102 * Be careful not to clear PageLRU until after we're sure
1103 * the page is not being freed elsewhere -- the page release
1104 * code relies on it.
1105 */
Nick Piggin8d438f92006-03-22 00:07:59 -08001106 ClearPageLRU(page);
Nick Piggin46453a62006-03-22 00:07:58 -08001107 list_add(&page->lru, dst);
1108 nr_taken++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110
1111 *scanned = scan;
1112 return nr_taken;
1113}
1114
1115/*
1116 * shrink_cache() adds the number of pages reclaimed to sc->nr_reclaimed
1117 */
1118static void shrink_cache(struct zone *zone, struct scan_control *sc)
1119{
1120 LIST_HEAD(page_list);
1121 struct pagevec pvec;
1122 int max_scan = sc->nr_to_scan;
1123
1124 pagevec_init(&pvec, 1);
1125
1126 lru_add_drain();
1127 spin_lock_irq(&zone->lru_lock);
1128 while (max_scan > 0) {
1129 struct page *page;
1130 int nr_taken;
1131 int nr_scan;
1132 int nr_freed;
1133
1134 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
1135 &zone->inactive_list,
1136 &page_list, &nr_scan);
1137 zone->nr_inactive -= nr_taken;
1138 zone->pages_scanned += nr_scan;
1139 spin_unlock_irq(&zone->lru_lock);
1140
1141 if (nr_taken == 0)
1142 goto done;
1143
1144 max_scan -= nr_scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 nr_freed = shrink_list(&page_list, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Nick Piggina74609f2006-01-06 00:11:20 -08001147 local_irq_disable();
1148 if (current_is_kswapd()) {
1149 __mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
1150 __mod_page_state(kswapd_steal, nr_freed);
1151 } else
1152 __mod_page_state_zone(zone, pgscan_direct, nr_scan);
1153 __mod_page_state_zone(zone, pgsteal, nr_freed);
1154
1155 spin_lock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /*
1157 * Put back any unfreeable pages.
1158 */
1159 while (!list_empty(&page_list)) {
1160 page = lru_to_page(&page_list);
Nick Piggin8d438f92006-03-22 00:07:59 -08001161 BUG_ON(PageLRU(page));
1162 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 list_del(&page->lru);
1164 if (PageActive(page))
1165 add_page_to_active_list(zone, page);
1166 else
1167 add_page_to_inactive_list(zone, page);
1168 if (!pagevec_add(&pvec, page)) {
1169 spin_unlock_irq(&zone->lru_lock);
1170 __pagevec_release(&pvec);
1171 spin_lock_irq(&zone->lru_lock);
1172 }
1173 }
1174 }
1175 spin_unlock_irq(&zone->lru_lock);
1176done:
1177 pagevec_release(&pvec);
1178}
1179
1180/*
1181 * This moves pages from the active list to the inactive list.
1182 *
1183 * We move them the other way if the page is referenced by one or more
1184 * processes, from rmap.
1185 *
1186 * If the pages are mostly unmapped, the processing is fast and it is
1187 * appropriate to hold zone->lru_lock across the whole operation. But if
1188 * the pages are mapped, the processing is slow (page_referenced()) so we
1189 * should drop zone->lru_lock around each page. It's impossible to balance
1190 * this, so instead we remove the pages from the LRU while processing them.
1191 * It is safe to rely on PG_active against the non-LRU pages in here because
1192 * nobody will play with that bit on a non-LRU page.
1193 *
1194 * The downside is that we have to touch page->_count against each page.
1195 * But we had to alter page->flags anyway.
1196 */
1197static void
1198refill_inactive_zone(struct zone *zone, struct scan_control *sc)
1199{
1200 int pgmoved;
1201 int pgdeactivate = 0;
1202 int pgscanned;
1203 int nr_pages = sc->nr_to_scan;
1204 LIST_HEAD(l_hold); /* The pages which were snipped off */
1205 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
1206 LIST_HEAD(l_active); /* Pages to go onto the active_list */
1207 struct page *page;
1208 struct pagevec pvec;
1209 int reclaim_mapped = 0;
Christoph Lameter2903fb12006-02-11 17:55:55 -08001210
1211 if (unlikely(sc->may_swap)) {
1212 long mapped_ratio;
1213 long distress;
1214 long swap_tendency;
1215
1216 /*
1217 * `distress' is a measure of how much trouble we're having
1218 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
1219 */
1220 distress = 100 >> zone->prev_priority;
1221
1222 /*
1223 * The point of this algorithm is to decide when to start
1224 * reclaiming mapped memory instead of just pagecache. Work out
1225 * how much memory
1226 * is mapped.
1227 */
1228 mapped_ratio = (sc->nr_mapped * 100) / total_memory;
1229
1230 /*
1231 * Now decide how much we really want to unmap some pages. The
1232 * mapped ratio is downgraded - just because there's a lot of
1233 * mapped memory doesn't necessarily mean that page reclaim
1234 * isn't succeeding.
1235 *
1236 * The distress ratio is important - we don't want to start
1237 * going oom.
1238 *
1239 * A 100% value of vm_swappiness overrides this algorithm
1240 * altogether.
1241 */
1242 swap_tendency = mapped_ratio / 2 + distress + vm_swappiness;
1243
1244 /*
1245 * Now use this metric to decide whether to start moving mapped
1246 * memory onto the inactive list.
1247 */
1248 if (swap_tendency >= 100)
1249 reclaim_mapped = 1;
1250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 lru_add_drain();
1253 spin_lock_irq(&zone->lru_lock);
1254 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
1255 &l_hold, &pgscanned);
1256 zone->pages_scanned += pgscanned;
1257 zone->nr_active -= pgmoved;
1258 spin_unlock_irq(&zone->lru_lock);
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 while (!list_empty(&l_hold)) {
1261 cond_resched();
1262 page = lru_to_page(&l_hold);
1263 list_del(&page->lru);
1264 if (page_mapped(page)) {
1265 if (!reclaim_mapped ||
1266 (total_swap_pages == 0 && PageAnon(page)) ||
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001267 page_referenced(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 list_add(&page->lru, &l_active);
1269 continue;
1270 }
1271 }
1272 list_add(&page->lru, &l_inactive);
1273 }
1274
1275 pagevec_init(&pvec, 1);
1276 pgmoved = 0;
1277 spin_lock_irq(&zone->lru_lock);
1278 while (!list_empty(&l_inactive)) {
1279 page = lru_to_page(&l_inactive);
1280 prefetchw_prev_lru_page(page, &l_inactive, flags);
Nick Piggin8d438f92006-03-22 00:07:59 -08001281 BUG_ON(PageLRU(page));
1282 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (!TestClearPageActive(page))
1284 BUG();
1285 list_move(&page->lru, &zone->inactive_list);
1286 pgmoved++;
1287 if (!pagevec_add(&pvec, page)) {
1288 zone->nr_inactive += pgmoved;
1289 spin_unlock_irq(&zone->lru_lock);
1290 pgdeactivate += pgmoved;
1291 pgmoved = 0;
1292 if (buffer_heads_over_limit)
1293 pagevec_strip(&pvec);
1294 __pagevec_release(&pvec);
1295 spin_lock_irq(&zone->lru_lock);
1296 }
1297 }
1298 zone->nr_inactive += pgmoved;
1299 pgdeactivate += pgmoved;
1300 if (buffer_heads_over_limit) {
1301 spin_unlock_irq(&zone->lru_lock);
1302 pagevec_strip(&pvec);
1303 spin_lock_irq(&zone->lru_lock);
1304 }
1305
1306 pgmoved = 0;
1307 while (!list_empty(&l_active)) {
1308 page = lru_to_page(&l_active);
1309 prefetchw_prev_lru_page(page, &l_active, flags);
Nick Piggin8d438f92006-03-22 00:07:59 -08001310 BUG_ON(PageLRU(page));
1311 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 BUG_ON(!PageActive(page));
1313 list_move(&page->lru, &zone->active_list);
1314 pgmoved++;
1315 if (!pagevec_add(&pvec, page)) {
1316 zone->nr_active += pgmoved;
1317 pgmoved = 0;
1318 spin_unlock_irq(&zone->lru_lock);
1319 __pagevec_release(&pvec);
1320 spin_lock_irq(&zone->lru_lock);
1321 }
1322 }
1323 zone->nr_active += pgmoved;
Nick Piggina74609f2006-01-06 00:11:20 -08001324 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Nick Piggina74609f2006-01-06 00:11:20 -08001326 __mod_page_state_zone(zone, pgrefill, pgscanned);
1327 __mod_page_state(pgdeactivate, pgdeactivate);
1328 local_irq_enable();
1329
1330 pagevec_release(&pvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
1333/*
1334 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
1335 */
1336static void
1337shrink_zone(struct zone *zone, struct scan_control *sc)
1338{
1339 unsigned long nr_active;
1340 unsigned long nr_inactive;
1341
Martin Hicks53e9a612005-09-03 15:54:51 -07001342 atomic_inc(&zone->reclaim_in_progress);
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 /*
1345 * Add one to `nr_to_scan' just to make sure that the kernel will
1346 * slowly sift through the active list.
1347 */
1348 zone->nr_scan_active += (zone->nr_active >> sc->priority) + 1;
1349 nr_active = zone->nr_scan_active;
1350 if (nr_active >= sc->swap_cluster_max)
1351 zone->nr_scan_active = 0;
1352 else
1353 nr_active = 0;
1354
1355 zone->nr_scan_inactive += (zone->nr_inactive >> sc->priority) + 1;
1356 nr_inactive = zone->nr_scan_inactive;
1357 if (nr_inactive >= sc->swap_cluster_max)
1358 zone->nr_scan_inactive = 0;
1359 else
1360 nr_inactive = 0;
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 while (nr_active || nr_inactive) {
1363 if (nr_active) {
1364 sc->nr_to_scan = min(nr_active,
1365 (unsigned long)sc->swap_cluster_max);
1366 nr_active -= sc->nr_to_scan;
1367 refill_inactive_zone(zone, sc);
1368 }
1369
1370 if (nr_inactive) {
1371 sc->nr_to_scan = min(nr_inactive,
1372 (unsigned long)sc->swap_cluster_max);
1373 nr_inactive -= sc->nr_to_scan;
1374 shrink_cache(zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376 }
1377
1378 throttle_vm_writeout();
Martin Hicks53e9a612005-09-03 15:54:51 -07001379
1380 atomic_dec(&zone->reclaim_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
1383/*
1384 * This is the direct reclaim path, for page-allocating processes. We only
1385 * try to reclaim pages from zones which will satisfy the caller's allocation
1386 * request.
1387 *
1388 * We reclaim from a zone even if that zone is over pages_high. Because:
1389 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
1390 * allocation or
1391 * b) The zones may be over pages_high but they must go *over* pages_high to
1392 * satisfy the `incremental min' zone defense algorithm.
1393 *
1394 * Returns the number of reclaimed pages.
1395 *
1396 * If a zone is deemed to be full of pinned pages then just give it a light
1397 * scan then give up on it.
1398 */
1399static void
1400shrink_caches(struct zone **zones, struct scan_control *sc)
1401{
1402 int i;
1403
1404 for (i = 0; zones[i] != NULL; i++) {
1405 struct zone *zone = zones[i];
1406
Con Kolivasf3fe6512006-01-06 00:11:15 -08001407 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 continue;
1409
Paul Jackson9bf22292005-09-06 15:18:12 -07001410 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 continue;
1412
1413 zone->temp_priority = sc->priority;
1414 if (zone->prev_priority > sc->priority)
1415 zone->prev_priority = sc->priority;
1416
1417 if (zone->all_unreclaimable && sc->priority != DEF_PRIORITY)
1418 continue; /* Let kswapd poll it */
1419
1420 shrink_zone(zone, sc);
1421 }
1422}
1423
1424/*
1425 * This is the main entry point to direct page reclaim.
1426 *
1427 * If a full scan of the inactive list fails to free enough memory then we
1428 * are "out of memory" and something needs to be killed.
1429 *
1430 * If the caller is !__GFP_FS then the probability of a failure is reasonably
1431 * high - the zone may be full of dirty or under-writeback pages, which this
1432 * caller can't do much about. We kick pdflush and take explicit naps in the
1433 * hope that some of these pages can be written. But if the allocating task
1434 * holds filesystem locks which prevent writeout this might not work, and the
1435 * allocation attempt will fail.
1436 */
Al Viro6daa0e22005-10-21 03:18:50 -04001437int try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 int priority;
1440 int ret = 0;
1441 int total_scanned = 0, total_reclaimed = 0;
1442 struct reclaim_state *reclaim_state = current->reclaim_state;
1443 struct scan_control sc;
1444 unsigned long lru_pages = 0;
1445 int i;
1446
1447 sc.gfp_mask = gfp_mask;
Christoph Lameter52a83632006-02-01 03:05:28 -08001448 sc.may_writepage = !laptop_mode;
Christoph Lameterf1fd1062006-01-18 17:42:30 -08001449 sc.may_swap = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 inc_page_state(allocstall);
1452
1453 for (i = 0; zones[i] != NULL; i++) {
1454 struct zone *zone = zones[i];
1455
Paul Jackson9bf22292005-09-06 15:18:12 -07001456 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 continue;
1458
1459 zone->temp_priority = DEF_PRIORITY;
1460 lru_pages += zone->nr_active + zone->nr_inactive;
1461 }
1462
1463 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1464 sc.nr_mapped = read_page_state(nr_mapped);
1465 sc.nr_scanned = 0;
1466 sc.nr_reclaimed = 0;
1467 sc.priority = priority;
1468 sc.swap_cluster_max = SWAP_CLUSTER_MAX;
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001469 if (!priority)
1470 disable_swap_token();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 shrink_caches(zones, &sc);
1472 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
1473 if (reclaim_state) {
1474 sc.nr_reclaimed += reclaim_state->reclaimed_slab;
1475 reclaim_state->reclaimed_slab = 0;
1476 }
1477 total_scanned += sc.nr_scanned;
1478 total_reclaimed += sc.nr_reclaimed;
1479 if (total_reclaimed >= sc.swap_cluster_max) {
1480 ret = 1;
1481 goto out;
1482 }
1483
1484 /*
1485 * Try to write back as many pages as we just scanned. This
1486 * tends to cause slow streaming writers to write data to the
1487 * disk smoothly, at the dirtying rate, which is nice. But
1488 * that's undesirable in laptop mode, where we *want* lumpy
1489 * writeout. So in laptop mode, write out the whole world.
1490 */
1491 if (total_scanned > sc.swap_cluster_max + sc.swap_cluster_max/2) {
Pekka J Enberg687a21c2005-06-28 20:44:55 -07001492 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 sc.may_writepage = 1;
1494 }
1495
1496 /* Take a nap, wait for some writeback to complete */
1497 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
1498 blk_congestion_wait(WRITE, HZ/10);
1499 }
1500out:
1501 for (i = 0; zones[i] != 0; i++) {
1502 struct zone *zone = zones[i];
1503
Paul Jackson9bf22292005-09-06 15:18:12 -07001504 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 continue;
1506
1507 zone->prev_priority = zone->temp_priority;
1508 }
1509 return ret;
1510}
1511
1512/*
1513 * For kswapd, balance_pgdat() will work across all this node's zones until
1514 * they are all at pages_high.
1515 *
1516 * If `nr_pages' is non-zero then it is the number of pages which are to be
1517 * reclaimed, regardless of the zone occupancies. This is a software suspend
1518 * special.
1519 *
1520 * Returns the number of pages which were actually freed.
1521 *
1522 * There is special handling here for zones which are full of pinned pages.
1523 * This can happen if the pages are all mlocked, or if they are all used by
1524 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1525 * What we do is to detect the case where all pages in the zone have been
1526 * scanned twice and there has been zero successful reclaim. Mark the zone as
1527 * dead and from now on, only perform a short scan. Basically we're polling
1528 * the zone for when the problem goes away.
1529 *
1530 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1531 * zones which have free_pages > pages_high, but once a zone is found to have
1532 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1533 * of the number of free pages in the lower zones. This interoperates with
1534 * the page allocator fallback scheme to ensure that aging of pages is balanced
1535 * across the zones.
1536 */
1537static int balance_pgdat(pg_data_t *pgdat, int nr_pages, int order)
1538{
1539 int to_free = nr_pages;
1540 int all_zones_ok;
1541 int priority;
1542 int i;
1543 int total_scanned, total_reclaimed;
1544 struct reclaim_state *reclaim_state = current->reclaim_state;
1545 struct scan_control sc;
1546
1547loop_again:
1548 total_scanned = 0;
1549 total_reclaimed = 0;
1550 sc.gfp_mask = GFP_KERNEL;
Christoph Lameter52a83632006-02-01 03:05:28 -08001551 sc.may_writepage = !laptop_mode;
Christoph Lameterf1fd1062006-01-18 17:42:30 -08001552 sc.may_swap = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 sc.nr_mapped = read_page_state(nr_mapped);
1554
1555 inc_page_state(pageoutrun);
1556
1557 for (i = 0; i < pgdat->nr_zones; i++) {
1558 struct zone *zone = pgdat->node_zones + i;
1559
1560 zone->temp_priority = DEF_PRIORITY;
1561 }
1562
1563 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1564 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1565 unsigned long lru_pages = 0;
1566
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001567 /* The swap token gets in the way of swapout... */
1568 if (!priority)
1569 disable_swap_token();
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 all_zones_ok = 1;
1572
1573 if (nr_pages == 0) {
1574 /*
1575 * Scan in the highmem->dma direction for the highest
1576 * zone which needs scanning
1577 */
1578 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1579 struct zone *zone = pgdat->node_zones + i;
1580
Con Kolivasf3fe6512006-01-06 00:11:15 -08001581 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 continue;
1583
1584 if (zone->all_unreclaimable &&
1585 priority != DEF_PRIORITY)
1586 continue;
1587
1588 if (!zone_watermark_ok(zone, order,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001589 zone->pages_high, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 end_zone = i;
1591 goto scan;
1592 }
1593 }
1594 goto out;
1595 } else {
1596 end_zone = pgdat->nr_zones - 1;
1597 }
1598scan:
1599 for (i = 0; i <= end_zone; i++) {
1600 struct zone *zone = pgdat->node_zones + i;
1601
1602 lru_pages += zone->nr_active + zone->nr_inactive;
1603 }
1604
1605 /*
1606 * Now scan the zone in the dma->highmem direction, stopping
1607 * at the last zone which needs scanning.
1608 *
1609 * We do this because the page allocator works in the opposite
1610 * direction. This prevents the page allocator from allocating
1611 * pages behind kswapd's direction of progress, which would
1612 * cause too much scanning of the lower zones.
1613 */
1614 for (i = 0; i <= end_zone; i++) {
1615 struct zone *zone = pgdat->node_zones + i;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001616 int nr_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Con Kolivasf3fe6512006-01-06 00:11:15 -08001618 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 continue;
1620
1621 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1622 continue;
1623
1624 if (nr_pages == 0) { /* Not software suspend */
1625 if (!zone_watermark_ok(zone, order,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001626 zone->pages_high, end_zone, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 all_zones_ok = 0;
1628 }
1629 zone->temp_priority = priority;
1630 if (zone->prev_priority > priority)
1631 zone->prev_priority = priority;
1632 sc.nr_scanned = 0;
1633 sc.nr_reclaimed = 0;
1634 sc.priority = priority;
1635 sc.swap_cluster_max = nr_pages? nr_pages : SWAP_CLUSTER_MAX;
1636 shrink_zone(zone, &sc);
1637 reclaim_state->reclaimed_slab = 0;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001638 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1639 lru_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 sc.nr_reclaimed += reclaim_state->reclaimed_slab;
1641 total_reclaimed += sc.nr_reclaimed;
1642 total_scanned += sc.nr_scanned;
1643 if (zone->all_unreclaimable)
1644 continue;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001645 if (nr_slab == 0 && zone->pages_scanned >=
1646 (zone->nr_active + zone->nr_inactive) * 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 zone->all_unreclaimable = 1;
1648 /*
1649 * If we've done a decent amount of scanning and
1650 * the reclaim ratio is low, start doing writepage
1651 * even in laptop mode
1652 */
1653 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
1654 total_scanned > total_reclaimed+total_reclaimed/2)
1655 sc.may_writepage = 1;
1656 }
1657 if (nr_pages && to_free > total_reclaimed)
1658 continue; /* swsusp: need to do more work */
1659 if (all_zones_ok)
1660 break; /* kswapd: all done */
1661 /*
1662 * OK, kswapd is getting into trouble. Take a nap, then take
1663 * another pass across the zones.
1664 */
1665 if (total_scanned && priority < DEF_PRIORITY - 2)
1666 blk_congestion_wait(WRITE, HZ/10);
1667
1668 /*
1669 * We do this so kswapd doesn't build up large priorities for
1670 * example when it is freeing in parallel with allocators. It
1671 * matches the direct reclaim path behaviour in terms of impact
1672 * on zone->*_priority.
1673 */
1674 if ((total_reclaimed >= SWAP_CLUSTER_MAX) && (!nr_pages))
1675 break;
1676 }
1677out:
1678 for (i = 0; i < pgdat->nr_zones; i++) {
1679 struct zone *zone = pgdat->node_zones + i;
1680
1681 zone->prev_priority = zone->temp_priority;
1682 }
1683 if (!all_zones_ok) {
1684 cond_resched();
1685 goto loop_again;
1686 }
1687
1688 return total_reclaimed;
1689}
1690
1691/*
1692 * The background pageout daemon, started as a kernel thread
1693 * from the init process.
1694 *
1695 * This basically trickles out pages so that we have _some_
1696 * free memory available even if there is no other activity
1697 * that frees anything up. This is needed for things like routing
1698 * etc, where we otherwise might have all activity going on in
1699 * asynchronous contexts that cannot page things out.
1700 *
1701 * If there are applications that are active memory-allocators
1702 * (most normal use), this basically shouldn't matter.
1703 */
1704static int kswapd(void *p)
1705{
1706 unsigned long order;
1707 pg_data_t *pgdat = (pg_data_t*)p;
1708 struct task_struct *tsk = current;
1709 DEFINE_WAIT(wait);
1710 struct reclaim_state reclaim_state = {
1711 .reclaimed_slab = 0,
1712 };
1713 cpumask_t cpumask;
1714
1715 daemonize("kswapd%d", pgdat->node_id);
1716 cpumask = node_to_cpumask(pgdat->node_id);
1717 if (!cpus_empty(cpumask))
1718 set_cpus_allowed(tsk, cpumask);
1719 current->reclaim_state = &reclaim_state;
1720
1721 /*
1722 * Tell the memory management that we're a "memory allocator",
1723 * and that if we need more memory we should get access to it
1724 * regardless (see "__alloc_pages()"). "kswapd" should
1725 * never get caught in the normal page freeing logic.
1726 *
1727 * (Kswapd normally doesn't need memory anyway, but sometimes
1728 * you need a small amount of memory in order to be able to
1729 * page out something else, and this flag essentially protects
1730 * us from recursively trying to free more memory as we're
1731 * trying to free the first piece of memory in the first place).
1732 */
Christoph Lameter930d9152006-01-08 01:00:47 -08001733 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 order = 0;
1736 for ( ; ; ) {
1737 unsigned long new_order;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001738
1739 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1742 new_order = pgdat->kswapd_max_order;
1743 pgdat->kswapd_max_order = 0;
1744 if (order < new_order) {
1745 /*
1746 * Don't sleep if someone wants a larger 'order'
1747 * allocation
1748 */
1749 order = new_order;
1750 } else {
1751 schedule();
1752 order = pgdat->kswapd_max_order;
1753 }
1754 finish_wait(&pgdat->kswapd_wait, &wait);
1755
1756 balance_pgdat(pgdat, 0, order);
1757 }
1758 return 0;
1759}
1760
1761/*
1762 * A zone is low on free memory, so wake its kswapd task to service it.
1763 */
1764void wakeup_kswapd(struct zone *zone, int order)
1765{
1766 pg_data_t *pgdat;
1767
Con Kolivasf3fe6512006-01-06 00:11:15 -08001768 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 return;
1770
1771 pgdat = zone->zone_pgdat;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001772 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return;
1774 if (pgdat->kswapd_max_order < order)
1775 pgdat->kswapd_max_order = order;
Paul Jackson9bf22292005-09-06 15:18:12 -07001776 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001778 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001780 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
1783#ifdef CONFIG_PM
1784/*
1785 * Try to free `nr_pages' of memory, system-wide. Returns the number of freed
1786 * pages.
1787 */
1788int shrink_all_memory(int nr_pages)
1789{
1790 pg_data_t *pgdat;
1791 int nr_to_free = nr_pages;
1792 int ret = 0;
1793 struct reclaim_state reclaim_state = {
1794 .reclaimed_slab = 0,
1795 };
1796
1797 current->reclaim_state = &reclaim_state;
1798 for_each_pgdat(pgdat) {
1799 int freed;
1800 freed = balance_pgdat(pgdat, nr_to_free, 0);
1801 ret += freed;
1802 nr_to_free -= freed;
1803 if (nr_to_free <= 0)
1804 break;
1805 }
1806 current->reclaim_state = NULL;
1807 return ret;
1808}
1809#endif
1810
1811#ifdef CONFIG_HOTPLUG_CPU
1812/* It's optimal to keep kswapds on the same CPUs as their memory, but
1813 not required for correctness. So if the last cpu in a node goes
1814 away, we get changed to run anywhere: as the first one comes back,
1815 restore their cpu bindings. */
1816static int __devinit cpu_callback(struct notifier_block *nfb,
1817 unsigned long action,
1818 void *hcpu)
1819{
1820 pg_data_t *pgdat;
1821 cpumask_t mask;
1822
1823 if (action == CPU_ONLINE) {
1824 for_each_pgdat(pgdat) {
1825 mask = node_to_cpumask(pgdat->node_id);
1826 if (any_online_cpu(mask) != NR_CPUS)
1827 /* One of our CPUs online: restore mask */
1828 set_cpus_allowed(pgdat->kswapd, mask);
1829 }
1830 }
1831 return NOTIFY_OK;
1832}
1833#endif /* CONFIG_HOTPLUG_CPU */
1834
1835static int __init kswapd_init(void)
1836{
1837 pg_data_t *pgdat;
1838 swap_setup();
1839 for_each_pgdat(pgdat)
1840 pgdat->kswapd
1841 = find_task_by_pid(kernel_thread(kswapd, pgdat, CLONE_KERNEL));
1842 total_memory = nr_free_pagecache_pages();
1843 hotcpu_notifier(cpu_callback, 0);
1844 return 0;
1845}
1846
1847module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001848
1849#ifdef CONFIG_NUMA
1850/*
1851 * Zone reclaim mode
1852 *
1853 * If non-zero call zone_reclaim when the number of free pages falls below
1854 * the watermarks.
1855 *
1856 * In the future we may add flags to the mode. However, the page allocator
1857 * should only have to check that zone_reclaim_mode != 0 before calling
1858 * zone_reclaim().
1859 */
1860int zone_reclaim_mode __read_mostly;
1861
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001862#define RECLAIM_OFF 0
1863#define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1864#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1865#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001866#define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001867
Christoph Lameter9eeff232006-01-18 17:42:31 -08001868/*
1869 * Mininum time between zone reclaim scans
1870 */
Christoph Lameter2a11ff02006-02-01 03:05:33 -08001871int zone_reclaim_interval __read_mostly = 30*HZ;
Christoph Lametera92f7122006-02-01 03:05:32 -08001872
1873/*
1874 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1875 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1876 * a zone.
1877 */
1878#define ZONE_RECLAIM_PRIORITY 4
1879
Christoph Lameter9eeff232006-01-18 17:42:31 -08001880/*
1881 * Try to free up some pages from this zone through reclaim.
1882 */
1883int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1884{
Christoph Lameter89288622006-02-01 03:05:25 -08001885 int nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001886 struct task_struct *p = current;
1887 struct reclaim_state reclaim_state;
Christoph Lameter89288622006-02-01 03:05:25 -08001888 struct scan_control sc;
Christoph Lameter42c722d2006-02-01 03:05:26 -08001889 cpumask_t mask;
1890 int node_id;
Christoph Lameter89288622006-02-01 03:05:25 -08001891
1892 if (time_before(jiffies,
Christoph Lameter2a11ff02006-02-01 03:05:33 -08001893 zone->last_unsuccessful_zone_reclaim + zone_reclaim_interval))
Christoph Lameter89288622006-02-01 03:05:25 -08001894 return 0;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001895
1896 if (!(gfp_mask & __GFP_WAIT) ||
Christoph Lameter9eeff232006-01-18 17:42:31 -08001897 zone->all_unreclaimable ||
Christoph Lametera6bf5272006-03-09 17:33:47 -08001898 atomic_read(&zone->reclaim_in_progress) > 0 ||
1899 (p->flags & PF_MEMALLOC))
Christoph Lameter9eeff232006-01-18 17:42:31 -08001900 return 0;
1901
Christoph Lameter42c722d2006-02-01 03:05:26 -08001902 node_id = zone->zone_pgdat->node_id;
1903 mask = node_to_cpumask(node_id);
1904 if (!cpus_empty(mask) && node_id != numa_node_id())
1905 return 0;
1906
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001907 sc.may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE);
1908 sc.may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP);
Christoph Lameter89288622006-02-01 03:05:25 -08001909 sc.nr_scanned = 0;
1910 sc.nr_reclaimed = 0;
Christoph Lametera92f7122006-02-01 03:05:32 -08001911 sc.priority = ZONE_RECLAIM_PRIORITY + 1;
Christoph Lameter89288622006-02-01 03:05:25 -08001912 sc.nr_mapped = read_page_state(nr_mapped);
1913 sc.gfp_mask = gfp_mask;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001914
1915 disable_swap_token();
1916
Christoph Lameter89288622006-02-01 03:05:25 -08001917 nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001918 if (nr_pages > SWAP_CLUSTER_MAX)
1919 sc.swap_cluster_max = nr_pages;
1920 else
1921 sc.swap_cluster_max = SWAP_CLUSTER_MAX;
1922
1923 cond_resched();
Christoph Lameterd4f77962006-02-24 13:04:22 -08001924 /*
1925 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1926 * and we also need to be able to write out pages for RECLAIM_WRITE
1927 * and RECLAIM_SWAP.
1928 */
1929 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001930 reclaim_state.reclaimed_slab = 0;
1931 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db232006-02-01 03:05:29 -08001932
Christoph Lametera92f7122006-02-01 03:05:32 -08001933 /*
1934 * Free memory by calling shrink zone with increasing priorities
1935 * until we have enough memory freed.
1936 */
1937 do {
1938 sc.priority--;
1939 shrink_zone(zone, &sc);
1940
1941 } while (sc.nr_reclaimed < nr_pages && sc.priority > 0);
Christoph Lameterc84db232006-02-01 03:05:29 -08001942
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001943 if (sc.nr_reclaimed < nr_pages && (zone_reclaim_mode & RECLAIM_SLAB)) {
1944 /*
1945 * shrink_slab does not currently allow us to determine
1946 * how many pages were freed in the zone. So we just
1947 * shake the slab and then go offnode for a single allocation.
1948 *
1949 * shrink_slab will free memory on all zones and may take
1950 * a long time.
1951 */
1952 shrink_slab(sc.nr_scanned, gfp_mask, order);
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001953 }
1954
Christoph Lameter9eeff232006-01-18 17:42:31 -08001955 p->reclaim_state = NULL;
Christoph Lameterd4f77962006-02-24 13:04:22 -08001956 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
Christoph Lameter9eeff232006-01-18 17:42:31 -08001957
1958 if (sc.nr_reclaimed == 0)
1959 zone->last_unsuccessful_zone_reclaim = jiffies;
1960
Christoph Lameterc84db232006-02-01 03:05:29 -08001961 return sc.nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001962}
1963#endif
1964