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