]> bbs.cooldavid.org Git - net-next-2.6.git/blame - mm/vmscan.c
[PATCH] Swap Migration V5: PF_SWAPWRITE to allow writing to swap
[net-next-2.6.git] / mm / vmscan.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4
LT
66 /* Ask shrink_caches, or shrink_zone to scan at this priority */
67 unsigned int priority;
68
69 /* This context's GFP mask */
6daa0e28 70 gfp_t gfp_mask;
1da177e4
LT
71
72 int may_writepage;
73
74 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
75 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
76 * In this context, it doesn't matter that we scan the
77 * whole list at once. */
78 int swap_cluster_max;
79};
80
81/*
82 * The list of shrinker callbacks used by to apply pressure to
83 * ageable caches.
84 */
85struct shrinker {
86 shrinker_t shrinker;
87 struct list_head list;
88 int seeks; /* seeks to recreate an obj */
89 long nr; /* objs pending delete */
90};
91
92#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
93
94#ifdef ARCH_HAS_PREFETCH
95#define prefetch_prev_lru_page(_page, _base, _field) \
96 do { \
97 if ((_page)->lru.prev != _base) { \
98 struct page *prev; \
99 \
100 prev = lru_to_page(&(_page->lru)); \
101 prefetch(&prev->_field); \
102 } \
103 } while (0)
104#else
105#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
106#endif
107
108#ifdef ARCH_HAS_PREFETCHW
109#define prefetchw_prev_lru_page(_page, _base, _field) \
110 do { \
111 if ((_page)->lru.prev != _base) { \
112 struct page *prev; \
113 \
114 prev = lru_to_page(&(_page->lru)); \
115 prefetchw(&prev->_field); \
116 } \
117 } while (0)
118#else
119#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
120#endif
121
122/*
123 * From 0 .. 100. Higher means more swappy.
124 */
125int vm_swappiness = 60;
126static long total_memory;
127
128static LIST_HEAD(shrinker_list);
129static DECLARE_RWSEM(shrinker_rwsem);
130
131/*
132 * Add a shrinker callback to be called from the vm
133 */
134struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
135{
136 struct shrinker *shrinker;
137
138 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
139 if (shrinker) {
140 shrinker->shrinker = theshrinker;
141 shrinker->seeks = seeks;
142 shrinker->nr = 0;
143 down_write(&shrinker_rwsem);
144 list_add_tail(&shrinker->list, &shrinker_list);
145 up_write(&shrinker_rwsem);
146 }
147 return shrinker;
148}
149EXPORT_SYMBOL(set_shrinker);
150
151/*
152 * Remove one
153 */
154void remove_shrinker(struct shrinker *shrinker)
155{
156 down_write(&shrinker_rwsem);
157 list_del(&shrinker->list);
158 up_write(&shrinker_rwsem);
159 kfree(shrinker);
160}
161EXPORT_SYMBOL(remove_shrinker);
162
163#define SHRINK_BATCH 128
164/*
165 * Call the shrink functions to age shrinkable caches
166 *
167 * Here we assume it costs one seek to replace a lru page and that it also
168 * takes a seek to recreate a cache object. With this in mind we age equal
169 * percentages of the lru and ageable caches. This should balance the seeks
170 * generated by these structures.
171 *
172 * If the vm encounted mapped pages on the LRU it increase the pressure on
173 * slab to avoid swapping.
174 *
175 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
176 *
177 * `lru_pages' represents the number of on-LRU pages in all the zones which
178 * are eligible for the caller's allocation attempt. It is used for balancing
179 * slab reclaim versus page reclaim.
b15e0905
AM
180 *
181 * Returns the number of slab objects which we shrunk.
1da177e4 182 */
9d0243bc 183int shrink_slab(unsigned long scanned, gfp_t gfp_mask, unsigned long lru_pages)
1da177e4
LT
184{
185 struct shrinker *shrinker;
b15e0905 186 int ret = 0;
1da177e4
LT
187
188 if (scanned == 0)
189 scanned = SWAP_CLUSTER_MAX;
190
191 if (!down_read_trylock(&shrinker_rwsem))
b15e0905 192 return 1; /* Assume we'll be able to shrink next time */
1da177e4
LT
193
194 list_for_each_entry(shrinker, &shrinker_list, list) {
195 unsigned long long delta;
196 unsigned long total_scan;
ea164d73 197 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
1da177e4
LT
198
199 delta = (4 * scanned) / shrinker->seeks;
ea164d73 200 delta *= max_pass;
1da177e4
LT
201 do_div(delta, lru_pages + 1);
202 shrinker->nr += delta;
ea164d73
AA
203 if (shrinker->nr < 0) {
204 printk(KERN_ERR "%s: nr=%ld\n",
205 __FUNCTION__, shrinker->nr);
206 shrinker->nr = max_pass;
207 }
208
209 /*
210 * Avoid risking looping forever due to too large nr value:
211 * never try to free more than twice the estimate number of
212 * freeable entries.
213 */
214 if (shrinker->nr > max_pass * 2)
215 shrinker->nr = max_pass * 2;
1da177e4
LT
216
217 total_scan = shrinker->nr;
218 shrinker->nr = 0;
219
220 while (total_scan >= SHRINK_BATCH) {
221 long this_scan = SHRINK_BATCH;
222 int shrink_ret;
b15e0905 223 int nr_before;
1da177e4 224
b15e0905 225 nr_before = (*shrinker->shrinker)(0, gfp_mask);
1da177e4
LT
226 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
227 if (shrink_ret == -1)
228 break;
b15e0905
AM
229 if (shrink_ret < nr_before)
230 ret += nr_before - shrink_ret;
1da177e4
LT
231 mod_page_state(slabs_scanned, this_scan);
232 total_scan -= this_scan;
233
234 cond_resched();
235 }
236
237 shrinker->nr += total_scan;
238 }
239 up_read(&shrinker_rwsem);
b15e0905 240 return ret;
1da177e4
LT
241}
242
243/* Called without lock on whether page is mapped, so answer is unstable */
244static inline int page_mapping_inuse(struct page *page)
245{
246 struct address_space *mapping;
247
248 /* Page is in somebody's page tables. */
249 if (page_mapped(page))
250 return 1;
251
252 /* Be more reluctant to reclaim swapcache than pagecache */
253 if (PageSwapCache(page))
254 return 1;
255
256 mapping = page_mapping(page);
257 if (!mapping)
258 return 0;
259
260 /* File is mmap'd by somebody? */
261 return mapping_mapped(mapping);
262}
263
264static inline int is_page_cache_freeable(struct page *page)
265{
266 return page_count(page) - !!PagePrivate(page) == 2;
267}
268
269static int may_write_to_queue(struct backing_dev_info *bdi)
270{
930d9152 271 if (current->flags & PF_SWAPWRITE)
1da177e4
LT
272 return 1;
273 if (!bdi_write_congested(bdi))
274 return 1;
275 if (bdi == current->backing_dev_info)
276 return 1;
277 return 0;
278}
279
280/*
281 * We detected a synchronous write error writing a page out. Probably
282 * -ENOSPC. We need to propagate that into the address_space for a subsequent
283 * fsync(), msync() or close().
284 *
285 * The tricky part is that after writepage we cannot touch the mapping: nothing
286 * prevents it from being freed up. But we have a ref on the page and once
287 * that page is locked, the mapping is pinned.
288 *
289 * We're allowed to run sleeping lock_page() here because we know the caller has
290 * __GFP_FS.
291 */
292static void handle_write_error(struct address_space *mapping,
293 struct page *page, int error)
294{
295 lock_page(page);
296 if (page_mapping(page) == mapping) {
297 if (error == -ENOSPC)
298 set_bit(AS_ENOSPC, &mapping->flags);
299 else
300 set_bit(AS_EIO, &mapping->flags);
301 }
302 unlock_page(page);
303}
304
305/*
306 * pageout is called by shrink_list() for each dirty page. Calls ->writepage().
307 */
308static pageout_t pageout(struct page *page, struct address_space *mapping)
309{
310 /*
311 * If the page is dirty, only perform writeback if that write
312 * will be non-blocking. To prevent this allocation from being
313 * stalled by pagecache activity. But note that there may be
314 * stalls if we need to run get_block(). We could test
315 * PagePrivate for that.
316 *
317 * If this process is currently in generic_file_write() against
318 * this page's queue, we can perform writeback even if that
319 * will block.
320 *
321 * If the page is swapcache, write it back even if that would
322 * block, for some throttling. This happens by accident, because
323 * swap_backing_dev_info is bust: it doesn't reflect the
324 * congestion state of the swapdevs. Easy to fix, if needed.
325 * See swapfile.c:page_queue_congested().
326 */
327 if (!is_page_cache_freeable(page))
328 return PAGE_KEEP;
329 if (!mapping) {
330 /*
331 * Some data journaling orphaned pages can have
332 * page->mapping == NULL while being dirty with clean buffers.
333 */
323aca6c 334 if (PagePrivate(page)) {
1da177e4
LT
335 if (try_to_free_buffers(page)) {
336 ClearPageDirty(page);
337 printk("%s: orphaned page\n", __FUNCTION__);
338 return PAGE_CLEAN;
339 }
340 }
341 return PAGE_KEEP;
342 }
343 if (mapping->a_ops->writepage == NULL)
344 return PAGE_ACTIVATE;
345 if (!may_write_to_queue(mapping->backing_dev_info))
346 return PAGE_KEEP;
347
348 if (clear_page_dirty_for_io(page)) {
349 int res;
350 struct writeback_control wbc = {
351 .sync_mode = WB_SYNC_NONE,
352 .nr_to_write = SWAP_CLUSTER_MAX,
353 .nonblocking = 1,
354 .for_reclaim = 1,
355 };
356
357 SetPageReclaim(page);
358 res = mapping->a_ops->writepage(page, &wbc);
359 if (res < 0)
360 handle_write_error(mapping, page, res);
994fc28c 361 if (res == AOP_WRITEPAGE_ACTIVATE) {
1da177e4
LT
362 ClearPageReclaim(page);
363 return PAGE_ACTIVATE;
364 }
365 if (!PageWriteback(page)) {
366 /* synchronous write or broken a_ops? */
367 ClearPageReclaim(page);
368 }
369
370 return PAGE_SUCCESS;
371 }
372
373 return PAGE_CLEAN;
374}
375
376/*
377 * shrink_list adds the number of reclaimed pages to sc->nr_reclaimed
378 */
379static int shrink_list(struct list_head *page_list, struct scan_control *sc)
380{
381 LIST_HEAD(ret_pages);
382 struct pagevec freed_pvec;
383 int pgactivate = 0;
384 int reclaimed = 0;
385
386 cond_resched();
387
388 pagevec_init(&freed_pvec, 1);
389 while (!list_empty(page_list)) {
390 struct address_space *mapping;
391 struct page *page;
392 int may_enter_fs;
393 int referenced;
394
395 cond_resched();
396
397 page = lru_to_page(page_list);
398 list_del(&page->lru);
399
400 if (TestSetPageLocked(page))
401 goto keep;
402
403 BUG_ON(PageActive(page));
404
405 sc->nr_scanned++;
406 /* Double the slab pressure for mapped and swapcache pages */
407 if (page_mapped(page) || PageSwapCache(page))
408 sc->nr_scanned++;
409
410 if (PageWriteback(page))
411 goto keep_locked;
412
f7b7fd8f 413 referenced = page_referenced(page, 1);
1da177e4
LT
414 /* In active use or really unfreeable? Activate it. */
415 if (referenced && page_mapping_inuse(page))
416 goto activate_locked;
417
418#ifdef CONFIG_SWAP
419 /*
420 * Anonymous process memory has backing store?
421 * Try to allocate it some swap space here.
422 */
c340010e 423 if (PageAnon(page) && !PageSwapCache(page)) {
1da177e4
LT
424 if (!add_to_swap(page))
425 goto activate_locked;
426 }
427#endif /* CONFIG_SWAP */
428
429 mapping = page_mapping(page);
430 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
431 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
432
433 /*
434 * The page is mapped into the page tables of one or more
435 * processes. Try to unmap it here.
436 */
437 if (page_mapped(page) && mapping) {
438 switch (try_to_unmap(page)) {
439 case SWAP_FAIL:
440 goto activate_locked;
441 case SWAP_AGAIN:
442 goto keep_locked;
443 case SWAP_SUCCESS:
444 ; /* try to free the page below */
445 }
446 }
447
448 if (PageDirty(page)) {
449 if (referenced)
450 goto keep_locked;
451 if (!may_enter_fs)
452 goto keep_locked;
453 if (laptop_mode && !sc->may_writepage)
454 goto keep_locked;
455
456 /* Page is dirty, try to write it out here */
457 switch(pageout(page, mapping)) {
458 case PAGE_KEEP:
459 goto keep_locked;
460 case PAGE_ACTIVATE:
461 goto activate_locked;
462 case PAGE_SUCCESS:
463 if (PageWriteback(page) || PageDirty(page))
464 goto keep;
465 /*
466 * A synchronous write - probably a ramdisk. Go
467 * ahead and try to reclaim the page.
468 */
469 if (TestSetPageLocked(page))
470 goto keep;
471 if (PageDirty(page) || PageWriteback(page))
472 goto keep_locked;
473 mapping = page_mapping(page);
474 case PAGE_CLEAN:
475 ; /* try to free the page below */
476 }
477 }
478
479 /*
480 * If the page has buffers, try to free the buffer mappings
481 * associated with this page. If we succeed we try to free
482 * the page as well.
483 *
484 * We do this even if the page is PageDirty().
485 * try_to_release_page() does not perform I/O, but it is
486 * possible for a page to have PageDirty set, but it is actually
487 * clean (all its buffers are clean). This happens if the
488 * buffers were written out directly, with submit_bh(). ext3
489 * will do this, as well as the blockdev mapping.
490 * try_to_release_page() will discover that cleanness and will
491 * drop the buffers and mark the page clean - it can be freed.
492 *
493 * Rarely, pages can have buffers and no ->mapping. These are
494 * the pages which were not successfully invalidated in
495 * truncate_complete_page(). We try to drop those buffers here
496 * and if that worked, and the page is no longer mapped into
497 * process address space (page_count == 1) it can be freed.
498 * Otherwise, leave the page on the LRU so it is swappable.
499 */
500 if (PagePrivate(page)) {
501 if (!try_to_release_page(page, sc->gfp_mask))
502 goto activate_locked;
503 if (!mapping && page_count(page) == 1)
504 goto free_it;
505 }
506
507 if (!mapping)
508 goto keep_locked; /* truncate got there first */
509
510 write_lock_irq(&mapping->tree_lock);
511
512 /*
513 * The non-racy check for busy page. It is critical to check
514 * PageDirty _after_ making sure that the page is freeable and
515 * not in use by anybody. (pagecache + us == 2)
516 */
3d80636a
LT
517 if (unlikely(page_count(page) != 2))
518 goto cannot_free;
519 smp_rmb();
520 if (unlikely(PageDirty(page)))
521 goto cannot_free;
1da177e4
LT
522
523#ifdef CONFIG_SWAP
524 if (PageSwapCache(page)) {
4c21e2f2 525 swp_entry_t swap = { .val = page_private(page) };
1da177e4
LT
526 __delete_from_swap_cache(page);
527 write_unlock_irq(&mapping->tree_lock);
528 swap_free(swap);
529 __put_page(page); /* The pagecache ref */
530 goto free_it;
531 }
532#endif /* CONFIG_SWAP */
533
534 __remove_from_page_cache(page);
535 write_unlock_irq(&mapping->tree_lock);
536 __put_page(page);
537
538free_it:
539 unlock_page(page);
540 reclaimed++;
541 if (!pagevec_add(&freed_pvec, page))
542 __pagevec_release_nonlru(&freed_pvec);
543 continue;
544
3d80636a
LT
545cannot_free:
546 write_unlock_irq(&mapping->tree_lock);
547 goto keep_locked;
548
1da177e4
LT
549activate_locked:
550 SetPageActive(page);
551 pgactivate++;
552keep_locked:
553 unlock_page(page);
554keep:
555 list_add(&page->lru, &ret_pages);
556 BUG_ON(PageLRU(page));
557 }
558 list_splice(&ret_pages, page_list);
559 if (pagevec_count(&freed_pvec))
560 __pagevec_release_nonlru(&freed_pvec);
561 mod_page_state(pgactivate, pgactivate);
562 sc->nr_reclaimed += reclaimed;
563 return reclaimed;
564}
565
566/*
567 * zone->lru_lock is heavily contended. Some of the functions that
568 * shrink the lists perform better by taking out a batch of pages
569 * and working on them outside the LRU lock.
570 *
571 * For pagecache intensive workloads, this function is the hottest
572 * spot in the kernel (apart from copy_*_user functions).
573 *
574 * Appropriate locks must be held before calling this function.
575 *
576 * @nr_to_scan: The number of pages to look through on the list.
577 * @src: The LRU list to pull pages off.
578 * @dst: The temp list to put pages on to.
579 * @scanned: The number of pages that were scanned.
580 *
581 * returns how many pages were moved onto *@dst.
582 */
583static int isolate_lru_pages(int nr_to_scan, struct list_head *src,
584 struct list_head *dst, int *scanned)
585{
586 int nr_taken = 0;
587 struct page *page;
588 int scan = 0;
589
590 while (scan++ < nr_to_scan && !list_empty(src)) {
591 page = lru_to_page(src);
592 prefetchw_prev_lru_page(page, src, flags);
593
21eac81f
CL
594 switch (__isolate_lru_page(page)) {
595 case 1:
596 /* Succeeded to isolate page */
597 list_move(&page->lru, dst);
1da177e4 598 nr_taken++;
21eac81f
CL
599 break;
600 case -ENOENT:
601 /* Not possible to isolate */
602 list_move(&page->lru, src);
603 break;
604 default:
605 BUG();
1da177e4
LT
606 }
607 }
608
609 *scanned = scan;
610 return nr_taken;
611}
612
21eac81f
CL
613static void lru_add_drain_per_cpu(void *dummy)
614{
615 lru_add_drain();
616}
617
618/*
619 * Isolate one page from the LRU lists and put it on the
620 * indicated list. Do necessary cache draining if the
621 * page is not on the LRU lists yet.
622 *
623 * Result:
624 * 0 = page not on LRU list
625 * 1 = page removed from LRU list and added to the specified list.
626 * -ENOENT = page is being freed elsewhere.
627 */
628int isolate_lru_page(struct page *page)
629{
630 int rc = 0;
631 struct zone *zone = page_zone(page);
632
633redo:
634 spin_lock_irq(&zone->lru_lock);
635 rc = __isolate_lru_page(page);
636 if (rc == 1) {
637 if (PageActive(page))
638 del_page_from_active_list(zone, page);
639 else
640 del_page_from_inactive_list(zone, page);
641 }
642 spin_unlock_irq(&zone->lru_lock);
643 if (rc == 0) {
644 /*
645 * Maybe this page is still waiting for a cpu to drain it
646 * from one of the lru lists?
647 */
648 rc = schedule_on_each_cpu(lru_add_drain_per_cpu, NULL);
649 if (rc == 0 && PageLRU(page))
650 goto redo;
651 }
652 return rc;
653}
654
1da177e4
LT
655/*
656 * shrink_cache() adds the number of pages reclaimed to sc->nr_reclaimed
657 */
658static void shrink_cache(struct zone *zone, struct scan_control *sc)
659{
660 LIST_HEAD(page_list);
661 struct pagevec pvec;
662 int max_scan = sc->nr_to_scan;
663
664 pagevec_init(&pvec, 1);
665
666 lru_add_drain();
667 spin_lock_irq(&zone->lru_lock);
668 while (max_scan > 0) {
669 struct page *page;
670 int nr_taken;
671 int nr_scan;
672 int nr_freed;
673
674 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
675 &zone->inactive_list,
676 &page_list, &nr_scan);
677 zone->nr_inactive -= nr_taken;
678 zone->pages_scanned += nr_scan;
679 spin_unlock_irq(&zone->lru_lock);
680
681 if (nr_taken == 0)
682 goto done;
683
684 max_scan -= nr_scan;
1da177e4 685 nr_freed = shrink_list(&page_list, sc);
1da177e4 686
a74609fa
NP
687 local_irq_disable();
688 if (current_is_kswapd()) {
689 __mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
690 __mod_page_state(kswapd_steal, nr_freed);
691 } else
692 __mod_page_state_zone(zone, pgscan_direct, nr_scan);
693 __mod_page_state_zone(zone, pgsteal, nr_freed);
694
695 spin_lock(&zone->lru_lock);
1da177e4
LT
696 /*
697 * Put back any unfreeable pages.
698 */
699 while (!list_empty(&page_list)) {
700 page = lru_to_page(&page_list);
701 if (TestSetPageLRU(page))
702 BUG();
703 list_del(&page->lru);
704 if (PageActive(page))
705 add_page_to_active_list(zone, page);
706 else
707 add_page_to_inactive_list(zone, page);
708 if (!pagevec_add(&pvec, page)) {
709 spin_unlock_irq(&zone->lru_lock);
710 __pagevec_release(&pvec);
711 spin_lock_irq(&zone->lru_lock);
712 }
713 }
714 }
715 spin_unlock_irq(&zone->lru_lock);
716done:
717 pagevec_release(&pvec);
718}
719
21eac81f
CL
720static inline void move_to_lru(struct page *page)
721{
722 list_del(&page->lru);
723 if (PageActive(page)) {
724 /*
725 * lru_cache_add_active checks that
726 * the PG_active bit is off.
727 */
728 ClearPageActive(page);
729 lru_cache_add_active(page);
730 } else {
731 lru_cache_add(page);
732 }
733 put_page(page);
734}
735
736/*
737 * Add isolated pages on the list back to the LRU
738 *
739 * returns the number of pages put back.
740 */
741int putback_lru_pages(struct list_head *l)
742{
743 struct page *page;
744 struct page *page2;
745 int count = 0;
746
747 list_for_each_entry_safe(page, page2, l, lru) {
748 move_to_lru(page);
749 count++;
750 }
751 return count;
752}
753
1da177e4
LT
754/*
755 * This moves pages from the active list to the inactive list.
756 *
757 * We move them the other way if the page is referenced by one or more
758 * processes, from rmap.
759 *
760 * If the pages are mostly unmapped, the processing is fast and it is
761 * appropriate to hold zone->lru_lock across the whole operation. But if
762 * the pages are mapped, the processing is slow (page_referenced()) so we
763 * should drop zone->lru_lock around each page. It's impossible to balance
764 * this, so instead we remove the pages from the LRU while processing them.
765 * It is safe to rely on PG_active against the non-LRU pages in here because
766 * nobody will play with that bit on a non-LRU page.
767 *
768 * The downside is that we have to touch page->_count against each page.
769 * But we had to alter page->flags anyway.
770 */
771static void
772refill_inactive_zone(struct zone *zone, struct scan_control *sc)
773{
774 int pgmoved;
775 int pgdeactivate = 0;
776 int pgscanned;
777 int nr_pages = sc->nr_to_scan;
778 LIST_HEAD(l_hold); /* The pages which were snipped off */
779 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
780 LIST_HEAD(l_active); /* Pages to go onto the active_list */
781 struct page *page;
782 struct pagevec pvec;
783 int reclaim_mapped = 0;
784 long mapped_ratio;
785 long distress;
786 long swap_tendency;
787
788 lru_add_drain();
789 spin_lock_irq(&zone->lru_lock);
790 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
791 &l_hold, &pgscanned);
792 zone->pages_scanned += pgscanned;
793 zone->nr_active -= pgmoved;
794 spin_unlock_irq(&zone->lru_lock);
795
796 /*
797 * `distress' is a measure of how much trouble we're having reclaiming
798 * pages. 0 -> no problems. 100 -> great trouble.
799 */
800 distress = 100 >> zone->prev_priority;
801
802 /*
803 * The point of this algorithm is to decide when to start reclaiming
804 * mapped memory instead of just pagecache. Work out how much memory
805 * is mapped.
806 */
807 mapped_ratio = (sc->nr_mapped * 100) / total_memory;
808
809 /*
810 * Now decide how much we really want to unmap some pages. The mapped
811 * ratio is downgraded - just because there's a lot of mapped memory
812 * doesn't necessarily mean that page reclaim isn't succeeding.
813 *
814 * The distress ratio is important - we don't want to start going oom.
815 *
816 * A 100% value of vm_swappiness overrides this algorithm altogether.
817 */
818 swap_tendency = mapped_ratio / 2 + distress + vm_swappiness;
819
820 /*
821 * Now use this metric to decide whether to start moving mapped memory
822 * onto the inactive list.
823 */
824 if (swap_tendency >= 100)
825 reclaim_mapped = 1;
826
827 while (!list_empty(&l_hold)) {
828 cond_resched();
829 page = lru_to_page(&l_hold);
830 list_del(&page->lru);
831 if (page_mapped(page)) {
832 if (!reclaim_mapped ||
833 (total_swap_pages == 0 && PageAnon(page)) ||
f7b7fd8f 834 page_referenced(page, 0)) {
1da177e4
LT
835 list_add(&page->lru, &l_active);
836 continue;
837 }
838 }
839 list_add(&page->lru, &l_inactive);
840 }
841
842 pagevec_init(&pvec, 1);
843 pgmoved = 0;
844 spin_lock_irq(&zone->lru_lock);
845 while (!list_empty(&l_inactive)) {
846 page = lru_to_page(&l_inactive);
847 prefetchw_prev_lru_page(page, &l_inactive, flags);
848 if (TestSetPageLRU(page))
849 BUG();
850 if (!TestClearPageActive(page))
851 BUG();
852 list_move(&page->lru, &zone->inactive_list);
853 pgmoved++;
854 if (!pagevec_add(&pvec, page)) {
855 zone->nr_inactive += pgmoved;
856 spin_unlock_irq(&zone->lru_lock);
857 pgdeactivate += pgmoved;
858 pgmoved = 0;
859 if (buffer_heads_over_limit)
860 pagevec_strip(&pvec);
861 __pagevec_release(&pvec);
862 spin_lock_irq(&zone->lru_lock);
863 }
864 }
865 zone->nr_inactive += pgmoved;
866 pgdeactivate += pgmoved;
867 if (buffer_heads_over_limit) {
868 spin_unlock_irq(&zone->lru_lock);
869 pagevec_strip(&pvec);
870 spin_lock_irq(&zone->lru_lock);
871 }
872
873 pgmoved = 0;
874 while (!list_empty(&l_active)) {
875 page = lru_to_page(&l_active);
876 prefetchw_prev_lru_page(page, &l_active, flags);
877 if (TestSetPageLRU(page))
878 BUG();
879 BUG_ON(!PageActive(page));
880 list_move(&page->lru, &zone->active_list);
881 pgmoved++;
882 if (!pagevec_add(&pvec, page)) {
883 zone->nr_active += pgmoved;
884 pgmoved = 0;
885 spin_unlock_irq(&zone->lru_lock);
886 __pagevec_release(&pvec);
887 spin_lock_irq(&zone->lru_lock);
888 }
889 }
890 zone->nr_active += pgmoved;
a74609fa
NP
891 spin_unlock(&zone->lru_lock);
892
893 __mod_page_state_zone(zone, pgrefill, pgscanned);
894 __mod_page_state(pgdeactivate, pgdeactivate);
895 local_irq_enable();
1da177e4 896
a74609fa 897 pagevec_release(&pvec);
1da177e4
LT
898}
899
900/*
901 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
902 */
903static void
904shrink_zone(struct zone *zone, struct scan_control *sc)
905{
906 unsigned long nr_active;
907 unsigned long nr_inactive;
908
53e9a615
MH
909 atomic_inc(&zone->reclaim_in_progress);
910
1da177e4
LT
911 /*
912 * Add one to `nr_to_scan' just to make sure that the kernel will
913 * slowly sift through the active list.
914 */
915 zone->nr_scan_active += (zone->nr_active >> sc->priority) + 1;
916 nr_active = zone->nr_scan_active;
917 if (nr_active >= sc->swap_cluster_max)
918 zone->nr_scan_active = 0;
919 else
920 nr_active = 0;
921
922 zone->nr_scan_inactive += (zone->nr_inactive >> sc->priority) + 1;
923 nr_inactive = zone->nr_scan_inactive;
924 if (nr_inactive >= sc->swap_cluster_max)
925 zone->nr_scan_inactive = 0;
926 else
927 nr_inactive = 0;
928
1da177e4
LT
929 while (nr_active || nr_inactive) {
930 if (nr_active) {
931 sc->nr_to_scan = min(nr_active,
932 (unsigned long)sc->swap_cluster_max);
933 nr_active -= sc->nr_to_scan;
934 refill_inactive_zone(zone, sc);
935 }
936
937 if (nr_inactive) {
938 sc->nr_to_scan = min(nr_inactive,
939 (unsigned long)sc->swap_cluster_max);
940 nr_inactive -= sc->nr_to_scan;
941 shrink_cache(zone, sc);
1da177e4
LT
942 }
943 }
944
945 throttle_vm_writeout();
53e9a615
MH
946
947 atomic_dec(&zone->reclaim_in_progress);
1da177e4
LT
948}
949
950/*
951 * This is the direct reclaim path, for page-allocating processes. We only
952 * try to reclaim pages from zones which will satisfy the caller's allocation
953 * request.
954 *
955 * We reclaim from a zone even if that zone is over pages_high. Because:
956 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
957 * allocation or
958 * b) The zones may be over pages_high but they must go *over* pages_high to
959 * satisfy the `incremental min' zone defense algorithm.
960 *
961 * Returns the number of reclaimed pages.
962 *
963 * If a zone is deemed to be full of pinned pages then just give it a light
964 * scan then give up on it.
965 */
966static void
967shrink_caches(struct zone **zones, struct scan_control *sc)
968{
969 int i;
970
971 for (i = 0; zones[i] != NULL; i++) {
972 struct zone *zone = zones[i];
973
f3fe6512 974 if (!populated_zone(zone))
1da177e4
LT
975 continue;
976
9bf2229f 977 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1da177e4
LT
978 continue;
979
980 zone->temp_priority = sc->priority;
981 if (zone->prev_priority > sc->priority)
982 zone->prev_priority = sc->priority;
983
984 if (zone->all_unreclaimable && sc->priority != DEF_PRIORITY)
985 continue; /* Let kswapd poll it */
986
987 shrink_zone(zone, sc);
988 }
989}
990
991/*
992 * This is the main entry point to direct page reclaim.
993 *
994 * If a full scan of the inactive list fails to free enough memory then we
995 * are "out of memory" and something needs to be killed.
996 *
997 * If the caller is !__GFP_FS then the probability of a failure is reasonably
998 * high - the zone may be full of dirty or under-writeback pages, which this
999 * caller can't do much about. We kick pdflush and take explicit naps in the
1000 * hope that some of these pages can be written. But if the allocating task
1001 * holds filesystem locks which prevent writeout this might not work, and the
1002 * allocation attempt will fail.
1003 */
6daa0e28 1004int try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
1da177e4
LT
1005{
1006 int priority;
1007 int ret = 0;
1008 int total_scanned = 0, total_reclaimed = 0;
1009 struct reclaim_state *reclaim_state = current->reclaim_state;
1010 struct scan_control sc;
1011 unsigned long lru_pages = 0;
1012 int i;
1013
1014 sc.gfp_mask = gfp_mask;
1015 sc.may_writepage = 0;
1016
1017 inc_page_state(allocstall);
1018
1019 for (i = 0; zones[i] != NULL; i++) {
1020 struct zone *zone = zones[i];
1021
9bf2229f 1022 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1da177e4
LT
1023 continue;
1024
1025 zone->temp_priority = DEF_PRIORITY;
1026 lru_pages += zone->nr_active + zone->nr_inactive;
1027 }
1028
1029 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1030 sc.nr_mapped = read_page_state(nr_mapped);
1031 sc.nr_scanned = 0;
1032 sc.nr_reclaimed = 0;
1033 sc.priority = priority;
1034 sc.swap_cluster_max = SWAP_CLUSTER_MAX;
f7b7fd8f
RR
1035 if (!priority)
1036 disable_swap_token();
1da177e4
LT
1037 shrink_caches(zones, &sc);
1038 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
1039 if (reclaim_state) {
1040 sc.nr_reclaimed += reclaim_state->reclaimed_slab;
1041 reclaim_state->reclaimed_slab = 0;
1042 }
1043 total_scanned += sc.nr_scanned;
1044 total_reclaimed += sc.nr_reclaimed;
1045 if (total_reclaimed >= sc.swap_cluster_max) {
1046 ret = 1;
1047 goto out;
1048 }
1049
1050 /*
1051 * Try to write back as many pages as we just scanned. This
1052 * tends to cause slow streaming writers to write data to the
1053 * disk smoothly, at the dirtying rate, which is nice. But
1054 * that's undesirable in laptop mode, where we *want* lumpy
1055 * writeout. So in laptop mode, write out the whole world.
1056 */
1057 if (total_scanned > sc.swap_cluster_max + sc.swap_cluster_max/2) {
687a21ce 1058 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
1da177e4
LT
1059 sc.may_writepage = 1;
1060 }
1061
1062 /* Take a nap, wait for some writeback to complete */
1063 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
1064 blk_congestion_wait(WRITE, HZ/10);
1065 }
1066out:
1067 for (i = 0; zones[i] != 0; i++) {
1068 struct zone *zone = zones[i];
1069
9bf2229f 1070 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1da177e4
LT
1071 continue;
1072
1073 zone->prev_priority = zone->temp_priority;
1074 }
1075 return ret;
1076}
1077
1078/*
1079 * For kswapd, balance_pgdat() will work across all this node's zones until
1080 * they are all at pages_high.
1081 *
1082 * If `nr_pages' is non-zero then it is the number of pages which are to be
1083 * reclaimed, regardless of the zone occupancies. This is a software suspend
1084 * special.
1085 *
1086 * Returns the number of pages which were actually freed.
1087 *
1088 * There is special handling here for zones which are full of pinned pages.
1089 * This can happen if the pages are all mlocked, or if they are all used by
1090 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1091 * What we do is to detect the case where all pages in the zone have been
1092 * scanned twice and there has been zero successful reclaim. Mark the zone as
1093 * dead and from now on, only perform a short scan. Basically we're polling
1094 * the zone for when the problem goes away.
1095 *
1096 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1097 * zones which have free_pages > pages_high, but once a zone is found to have
1098 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1099 * of the number of free pages in the lower zones. This interoperates with
1100 * the page allocator fallback scheme to ensure that aging of pages is balanced
1101 * across the zones.
1102 */
1103static int balance_pgdat(pg_data_t *pgdat, int nr_pages, int order)
1104{
1105 int to_free = nr_pages;
1106 int all_zones_ok;
1107 int priority;
1108 int i;
1109 int total_scanned, total_reclaimed;
1110 struct reclaim_state *reclaim_state = current->reclaim_state;
1111 struct scan_control sc;
1112
1113loop_again:
1114 total_scanned = 0;
1115 total_reclaimed = 0;
1116 sc.gfp_mask = GFP_KERNEL;
1117 sc.may_writepage = 0;
1118 sc.nr_mapped = read_page_state(nr_mapped);
1119
1120 inc_page_state(pageoutrun);
1121
1122 for (i = 0; i < pgdat->nr_zones; i++) {
1123 struct zone *zone = pgdat->node_zones + i;
1124
1125 zone->temp_priority = DEF_PRIORITY;
1126 }
1127
1128 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1129 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1130 unsigned long lru_pages = 0;
1131
f7b7fd8f
RR
1132 /* The swap token gets in the way of swapout... */
1133 if (!priority)
1134 disable_swap_token();
1135
1da177e4
LT
1136 all_zones_ok = 1;
1137
1138 if (nr_pages == 0) {
1139 /*
1140 * Scan in the highmem->dma direction for the highest
1141 * zone which needs scanning
1142 */
1143 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1144 struct zone *zone = pgdat->node_zones + i;
1145
f3fe6512 1146 if (!populated_zone(zone))
1da177e4
LT
1147 continue;
1148
1149 if (zone->all_unreclaimable &&
1150 priority != DEF_PRIORITY)
1151 continue;
1152
1153 if (!zone_watermark_ok(zone, order,
7fb1d9fc 1154 zone->pages_high, 0, 0)) {
1da177e4
LT
1155 end_zone = i;
1156 goto scan;
1157 }
1158 }
1159 goto out;
1160 } else {
1161 end_zone = pgdat->nr_zones - 1;
1162 }
1163scan:
1164 for (i = 0; i <= end_zone; i++) {
1165 struct zone *zone = pgdat->node_zones + i;
1166
1167 lru_pages += zone->nr_active + zone->nr_inactive;
1168 }
1169
1170 /*
1171 * Now scan the zone in the dma->highmem direction, stopping
1172 * at the last zone which needs scanning.
1173 *
1174 * We do this because the page allocator works in the opposite
1175 * direction. This prevents the page allocator from allocating
1176 * pages behind kswapd's direction of progress, which would
1177 * cause too much scanning of the lower zones.
1178 */
1179 for (i = 0; i <= end_zone; i++) {
1180 struct zone *zone = pgdat->node_zones + i;
b15e0905 1181 int nr_slab;
1da177e4 1182
f3fe6512 1183 if (!populated_zone(zone))
1da177e4
LT
1184 continue;
1185
1186 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1187 continue;
1188
1189 if (nr_pages == 0) { /* Not software suspend */
1190 if (!zone_watermark_ok(zone, order,
7fb1d9fc 1191 zone->pages_high, end_zone, 0))
1da177e4
LT
1192 all_zones_ok = 0;
1193 }
1194 zone->temp_priority = priority;
1195 if (zone->prev_priority > priority)
1196 zone->prev_priority = priority;
1197 sc.nr_scanned = 0;
1198 sc.nr_reclaimed = 0;
1199 sc.priority = priority;
1200 sc.swap_cluster_max = nr_pages? nr_pages : SWAP_CLUSTER_MAX;
1e7e5a90 1201 atomic_inc(&zone->reclaim_in_progress);
1da177e4 1202 shrink_zone(zone, &sc);
1e7e5a90 1203 atomic_dec(&zone->reclaim_in_progress);
1da177e4 1204 reclaim_state->reclaimed_slab = 0;
b15e0905
AM
1205 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1206 lru_pages);
1da177e4
LT
1207 sc.nr_reclaimed += reclaim_state->reclaimed_slab;
1208 total_reclaimed += sc.nr_reclaimed;
1209 total_scanned += sc.nr_scanned;
1210 if (zone->all_unreclaimable)
1211 continue;
b15e0905
AM
1212 if (nr_slab == 0 && zone->pages_scanned >=
1213 (zone->nr_active + zone->nr_inactive) * 4)
1da177e4
LT
1214 zone->all_unreclaimable = 1;
1215 /*
1216 * If we've done a decent amount of scanning and
1217 * the reclaim ratio is low, start doing writepage
1218 * even in laptop mode
1219 */
1220 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
1221 total_scanned > total_reclaimed+total_reclaimed/2)
1222 sc.may_writepage = 1;
1223 }
1224 if (nr_pages && to_free > total_reclaimed)
1225 continue; /* swsusp: need to do more work */
1226 if (all_zones_ok)
1227 break; /* kswapd: all done */
1228 /*
1229 * OK, kswapd is getting into trouble. Take a nap, then take
1230 * another pass across the zones.
1231 */
1232 if (total_scanned && priority < DEF_PRIORITY - 2)
1233 blk_congestion_wait(WRITE, HZ/10);
1234
1235 /*
1236 * We do this so kswapd doesn't build up large priorities for
1237 * example when it is freeing in parallel with allocators. It
1238 * matches the direct reclaim path behaviour in terms of impact
1239 * on zone->*_priority.
1240 */
1241 if ((total_reclaimed >= SWAP_CLUSTER_MAX) && (!nr_pages))
1242 break;
1243 }
1244out:
1245 for (i = 0; i < pgdat->nr_zones; i++) {
1246 struct zone *zone = pgdat->node_zones + i;
1247
1248 zone->prev_priority = zone->temp_priority;
1249 }
1250 if (!all_zones_ok) {
1251 cond_resched();
1252 goto loop_again;
1253 }
1254
1255 return total_reclaimed;
1256}
1257
1258/*
1259 * The background pageout daemon, started as a kernel thread
1260 * from the init process.
1261 *
1262 * This basically trickles out pages so that we have _some_
1263 * free memory available even if there is no other activity
1264 * that frees anything up. This is needed for things like routing
1265 * etc, where we otherwise might have all activity going on in
1266 * asynchronous contexts that cannot page things out.
1267 *
1268 * If there are applications that are active memory-allocators
1269 * (most normal use), this basically shouldn't matter.
1270 */
1271static int kswapd(void *p)
1272{
1273 unsigned long order;
1274 pg_data_t *pgdat = (pg_data_t*)p;
1275 struct task_struct *tsk = current;
1276 DEFINE_WAIT(wait);
1277 struct reclaim_state reclaim_state = {
1278 .reclaimed_slab = 0,
1279 };
1280 cpumask_t cpumask;
1281
1282 daemonize("kswapd%d", pgdat->node_id);
1283 cpumask = node_to_cpumask(pgdat->node_id);
1284 if (!cpus_empty(cpumask))
1285 set_cpus_allowed(tsk, cpumask);
1286 current->reclaim_state = &reclaim_state;
1287
1288 /*
1289 * Tell the memory management that we're a "memory allocator",
1290 * and that if we need more memory we should get access to it
1291 * regardless (see "__alloc_pages()"). "kswapd" should
1292 * never get caught in the normal page freeing logic.
1293 *
1294 * (Kswapd normally doesn't need memory anyway, but sometimes
1295 * you need a small amount of memory in order to be able to
1296 * page out something else, and this flag essentially protects
1297 * us from recursively trying to free more memory as we're
1298 * trying to free the first piece of memory in the first place).
1299 */
930d9152 1300 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
1da177e4
LT
1301
1302 order = 0;
1303 for ( ; ; ) {
1304 unsigned long new_order;
3e1d1d28
CL
1305
1306 try_to_freeze();
1da177e4
LT
1307
1308 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1309 new_order = pgdat->kswapd_max_order;
1310 pgdat->kswapd_max_order = 0;
1311 if (order < new_order) {
1312 /*
1313 * Don't sleep if someone wants a larger 'order'
1314 * allocation
1315 */
1316 order = new_order;
1317 } else {
1318 schedule();
1319 order = pgdat->kswapd_max_order;
1320 }
1321 finish_wait(&pgdat->kswapd_wait, &wait);
1322
1323 balance_pgdat(pgdat, 0, order);
1324 }
1325 return 0;
1326}
1327
1328/*
1329 * A zone is low on free memory, so wake its kswapd task to service it.
1330 */
1331void wakeup_kswapd(struct zone *zone, int order)
1332{
1333 pg_data_t *pgdat;
1334
f3fe6512 1335 if (!populated_zone(zone))
1da177e4
LT
1336 return;
1337
1338 pgdat = zone->zone_pgdat;
7fb1d9fc 1339 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
1da177e4
LT
1340 return;
1341 if (pgdat->kswapd_max_order < order)
1342 pgdat->kswapd_max_order = order;
9bf2229f 1343 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1da177e4 1344 return;
8d0986e2 1345 if (!waitqueue_active(&pgdat->kswapd_wait))
1da177e4 1346 return;
8d0986e2 1347 wake_up_interruptible(&pgdat->kswapd_wait);
1da177e4
LT
1348}
1349
1350#ifdef CONFIG_PM
1351/*
1352 * Try to free `nr_pages' of memory, system-wide. Returns the number of freed
1353 * pages.
1354 */
1355int shrink_all_memory(int nr_pages)
1356{
1357 pg_data_t *pgdat;
1358 int nr_to_free = nr_pages;
1359 int ret = 0;
1360 struct reclaim_state reclaim_state = {
1361 .reclaimed_slab = 0,
1362 };
1363
1364 current->reclaim_state = &reclaim_state;
1365 for_each_pgdat(pgdat) {
1366 int freed;
1367 freed = balance_pgdat(pgdat, nr_to_free, 0);
1368 ret += freed;
1369 nr_to_free -= freed;
1370 if (nr_to_free <= 0)
1371 break;
1372 }
1373 current->reclaim_state = NULL;
1374 return ret;
1375}
1376#endif
1377
1378#ifdef CONFIG_HOTPLUG_CPU
1379/* It's optimal to keep kswapds on the same CPUs as their memory, but
1380 not required for correctness. So if the last cpu in a node goes
1381 away, we get changed to run anywhere: as the first one comes back,
1382 restore their cpu bindings. */
1383static int __devinit cpu_callback(struct notifier_block *nfb,
1384 unsigned long action,
1385 void *hcpu)
1386{
1387 pg_data_t *pgdat;
1388 cpumask_t mask;
1389
1390 if (action == CPU_ONLINE) {
1391 for_each_pgdat(pgdat) {
1392 mask = node_to_cpumask(pgdat->node_id);
1393 if (any_online_cpu(mask) != NR_CPUS)
1394 /* One of our CPUs online: restore mask */
1395 set_cpus_allowed(pgdat->kswapd, mask);
1396 }
1397 }
1398 return NOTIFY_OK;
1399}
1400#endif /* CONFIG_HOTPLUG_CPU */
1401
1402static int __init kswapd_init(void)
1403{
1404 pg_data_t *pgdat;
1405 swap_setup();
1406 for_each_pgdat(pgdat)
1407 pgdat->kswapd
1408 = find_task_by_pid(kernel_thread(kswapd, pgdat, CLONE_KERNEL));
1409 total_memory = nr_free_pagecache_pages();
1410 hotcpu_notifier(cpu_callback, 0);
1411 return 0;
1412}
1413
1414module_init(kswapd_init)