vmscan: print shrink_slab symbol name on negative shrinker objects
[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/vmstat.h>
23 #include <linux/file.h>
24 #include <linux/writeback.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/cpuset.h>
35 #include <linux/notifier.h>
36 #include <linux/rwsem.h>
37 #include <linux/delay.h>
38 #include <linux/kthread.h>
39 #include <linux/freezer.h>
40 #include <linux/memcontrol.h>
41 #include <linux/delayacct.h>
42 #include <linux/sysctl.h>
43
44 #include <asm/tlbflush.h>
45 #include <asm/div64.h>
46
47 #include <linux/swapops.h>
48
49 #include "internal.h"
50
51 struct scan_control {
52         /* Incremented by the number of inactive pages that were scanned */
53         unsigned long nr_scanned;
54
55         /* Number of pages freed so far during a call to shrink_zones() */
56         unsigned long nr_reclaimed;
57
58         /* This context's GFP mask */
59         gfp_t gfp_mask;
60
61         int may_writepage;
62
63         /* Can mapped pages be reclaimed? */
64         int may_unmap;
65
66         /* This context's SWAP_CLUSTER_MAX. If freeing memory for
67          * suspend, we effectively ignore SWAP_CLUSTER_MAX.
68          * In this context, it doesn't matter that we scan the
69          * whole list at once. */
70         int swap_cluster_max;
71
72         int swappiness;
73
74         int all_unreclaimable;
75
76         int order;
77
78         /* Which cgroup do we reclaim from */
79         struct mem_cgroup *mem_cgroup;
80
81         /* Pluggable isolate pages callback */
82         unsigned long (*isolate_pages)(unsigned long nr, struct list_head *dst,
83                         unsigned long *scanned, int order, int mode,
84                         struct zone *z, struct mem_cgroup *mem_cont,
85                         int active, int file);
86 };
87
88 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
89
90 #ifdef ARCH_HAS_PREFETCH
91 #define prefetch_prev_lru_page(_page, _base, _field)                    \
92         do {                                                            \
93                 if ((_page)->lru.prev != _base) {                       \
94                         struct page *prev;                              \
95                                                                         \
96                         prev = lru_to_page(&(_page->lru));              \
97                         prefetch(&prev->_field);                        \
98                 }                                                       \
99         } while (0)
100 #else
101 #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
102 #endif
103
104 #ifdef ARCH_HAS_PREFETCHW
105 #define prefetchw_prev_lru_page(_page, _base, _field)                   \
106         do {                                                            \
107                 if ((_page)->lru.prev != _base) {                       \
108                         struct page *prev;                              \
109                                                                         \
110                         prev = lru_to_page(&(_page->lru));              \
111                         prefetchw(&prev->_field);                       \
112                 }                                                       \
113         } while (0)
114 #else
115 #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
116 #endif
117
118 /*
119  * From 0 .. 100.  Higher means more swappy.
120  */
121 int vm_swappiness = 60;
122 long vm_total_pages;    /* The total number of pages which the VM controls */
123
124 static LIST_HEAD(shrinker_list);
125 static DECLARE_RWSEM(shrinker_rwsem);
126
127 #ifdef CONFIG_CGROUP_MEM_RES_CTLR
128 #define scanning_global_lru(sc) (!(sc)->mem_cgroup)
129 #else
130 #define scanning_global_lru(sc) (1)
131 #endif
132
133 static struct zone_reclaim_stat *get_reclaim_stat(struct zone *zone,
134                                                   struct scan_control *sc)
135 {
136         if (!scanning_global_lru(sc))
137                 return mem_cgroup_get_reclaim_stat(sc->mem_cgroup, zone);
138
139         return &zone->reclaim_stat;
140 }
141
142 static unsigned long zone_nr_pages(struct zone *zone, struct scan_control *sc,
143                                    enum lru_list lru)
144 {
145         if (!scanning_global_lru(sc))
146                 return mem_cgroup_zone_nr_pages(sc->mem_cgroup, zone, lru);
147
148         return zone_page_state(zone, NR_LRU_BASE + lru);
149 }
150
151
152 /*
153  * Add a shrinker callback to be called from the vm
154  */
155 void register_shrinker(struct shrinker *shrinker)
156 {
157         shrinker->nr = 0;
158         down_write(&shrinker_rwsem);
159         list_add_tail(&shrinker->list, &shrinker_list);
160         up_write(&shrinker_rwsem);
161 }
162 EXPORT_SYMBOL(register_shrinker);
163
164 /*
165  * Remove one
166  */
167 void unregister_shrinker(struct shrinker *shrinker)
168 {
169         down_write(&shrinker_rwsem);
170         list_del(&shrinker->list);
171         up_write(&shrinker_rwsem);
172 }
173 EXPORT_SYMBOL(unregister_shrinker);
174
175 #define SHRINK_BATCH 128
176 /*
177  * Call the shrink functions to age shrinkable caches
178  *
179  * Here we assume it costs one seek to replace a lru page and that it also
180  * takes a seek to recreate a cache object.  With this in mind we age equal
181  * percentages of the lru and ageable caches.  This should balance the seeks
182  * generated by these structures.
183  *
184  * If the vm encountered mapped pages on the LRU it increase the pressure on
185  * slab to avoid swapping.
186  *
187  * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
188  *
189  * `lru_pages' represents the number of on-LRU pages in all the zones which
190  * are eligible for the caller's allocation attempt.  It is used for balancing
191  * slab reclaim versus page reclaim.
192  *
193  * Returns the number of slab objects which we shrunk.
194  */
195 unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
196                         unsigned long lru_pages)
197 {
198         struct shrinker *shrinker;
199         unsigned long ret = 0;
200
201         if (scanned == 0)
202                 scanned = SWAP_CLUSTER_MAX;
203
204         if (!down_read_trylock(&shrinker_rwsem))
205                 return 1;       /* Assume we'll be able to shrink next time */
206
207         list_for_each_entry(shrinker, &shrinker_list, list) {
208                 unsigned long long delta;
209                 unsigned long total_scan;
210                 unsigned long max_pass = (*shrinker->shrink)(0, gfp_mask);
211
212                 delta = (4 * scanned) / shrinker->seeks;
213                 delta *= max_pass;
214                 do_div(delta, lru_pages + 1);
215                 shrinker->nr += delta;
216                 if (shrinker->nr < 0) {
217                         printk(KERN_ERR "shrink_slab: %pF negative objects to "
218                                "delete nr=%ld\n",
219                                shrinker->shrink, shrinker->nr);
220                         shrinker->nr = max_pass;
221                 }
222
223                 /*
224                  * Avoid risking looping forever due to too large nr value:
225                  * never try to free more than twice the estimate number of
226                  * freeable entries.
227                  */
228                 if (shrinker->nr > max_pass * 2)
229                         shrinker->nr = max_pass * 2;
230
231                 total_scan = shrinker->nr;
232                 shrinker->nr = 0;
233
234                 while (total_scan >= SHRINK_BATCH) {
235                         long this_scan = SHRINK_BATCH;
236                         int shrink_ret;
237                         int nr_before;
238
239                         nr_before = (*shrinker->shrink)(0, gfp_mask);
240                         shrink_ret = (*shrinker->shrink)(this_scan, gfp_mask);
241                         if (shrink_ret == -1)
242                                 break;
243                         if (shrink_ret < nr_before)
244                                 ret += nr_before - shrink_ret;
245                         count_vm_events(SLABS_SCANNED, this_scan);
246                         total_scan -= this_scan;
247
248                         cond_resched();
249                 }
250
251                 shrinker->nr += total_scan;
252         }
253         up_read(&shrinker_rwsem);
254         return ret;
255 }
256
257 /* Called without lock on whether page is mapped, so answer is unstable */
258 static inline int page_mapping_inuse(struct page *page)
259 {
260         struct address_space *mapping;
261
262         /* Page is in somebody's page tables. */
263         if (page_mapped(page))
264                 return 1;
265
266         /* Be more reluctant to reclaim swapcache than pagecache */
267         if (PageSwapCache(page))
268                 return 1;
269
270         mapping = page_mapping(page);
271         if (!mapping)
272                 return 0;
273
274         /* File is mmap'd by somebody? */
275         return mapping_mapped(mapping);
276 }
277
278 static inline int is_page_cache_freeable(struct page *page)
279 {
280         return page_count(page) - !!PagePrivate(page) == 2;
281 }
282
283 static int may_write_to_queue(struct backing_dev_info *bdi)
284 {
285         if (current->flags & PF_SWAPWRITE)
286                 return 1;
287         if (!bdi_write_congested(bdi))
288                 return 1;
289         if (bdi == current->backing_dev_info)
290                 return 1;
291         return 0;
292 }
293
294 /*
295  * We detected a synchronous write error writing a page out.  Probably
296  * -ENOSPC.  We need to propagate that into the address_space for a subsequent
297  * fsync(), msync() or close().
298  *
299  * The tricky part is that after writepage we cannot touch the mapping: nothing
300  * prevents it from being freed up.  But we have a ref on the page and once
301  * that page is locked, the mapping is pinned.
302  *
303  * We're allowed to run sleeping lock_page() here because we know the caller has
304  * __GFP_FS.
305  */
306 static void handle_write_error(struct address_space *mapping,
307                                 struct page *page, int error)
308 {
309         lock_page(page);
310         if (page_mapping(page) == mapping)
311                 mapping_set_error(mapping, error);
312         unlock_page(page);
313 }
314
315 /* Request for sync pageout. */
316 enum pageout_io {
317         PAGEOUT_IO_ASYNC,
318         PAGEOUT_IO_SYNC,
319 };
320
321 /* possible outcome of pageout() */
322 typedef enum {
323         /* failed to write page out, page is locked */
324         PAGE_KEEP,
325         /* move page to the active list, page is locked */
326         PAGE_ACTIVATE,
327         /* page has been sent to the disk successfully, page is unlocked */
328         PAGE_SUCCESS,
329         /* page is clean and locked */
330         PAGE_CLEAN,
331 } pageout_t;
332
333 /*
334  * pageout is called by shrink_page_list() for each dirty page.
335  * Calls ->writepage().
336  */
337 static pageout_t pageout(struct page *page, struct address_space *mapping,
338                                                 enum pageout_io sync_writeback)
339 {
340         /*
341          * If the page is dirty, only perform writeback if that write
342          * will be non-blocking.  To prevent this allocation from being
343          * stalled by pagecache activity.  But note that there may be
344          * stalls if we need to run get_block().  We could test
345          * PagePrivate for that.
346          *
347          * If this process is currently in generic_file_write() against
348          * this page's queue, we can perform writeback even if that
349          * will block.
350          *
351          * If the page is swapcache, write it back even if that would
352          * block, for some throttling. This happens by accident, because
353          * swap_backing_dev_info is bust: it doesn't reflect the
354          * congestion state of the swapdevs.  Easy to fix, if needed.
355          * See swapfile.c:page_queue_congested().
356          */
357         if (!is_page_cache_freeable(page))
358                 return PAGE_KEEP;
359         if (!mapping) {
360                 /*
361                  * Some data journaling orphaned pages can have
362                  * page->mapping == NULL while being dirty with clean buffers.
363                  */
364                 if (PagePrivate(page)) {
365                         if (try_to_free_buffers(page)) {
366                                 ClearPageDirty(page);
367                                 printk("%s: orphaned page\n", __func__);
368                                 return PAGE_CLEAN;
369                         }
370                 }
371                 return PAGE_KEEP;
372         }
373         if (mapping->a_ops->writepage == NULL)
374                 return PAGE_ACTIVATE;
375         if (!may_write_to_queue(mapping->backing_dev_info))
376                 return PAGE_KEEP;
377
378         if (clear_page_dirty_for_io(page)) {
379                 int res;
380                 struct writeback_control wbc = {
381                         .sync_mode = WB_SYNC_NONE,
382                         .nr_to_write = SWAP_CLUSTER_MAX,
383                         .range_start = 0,
384                         .range_end = LLONG_MAX,
385                         .nonblocking = 1,
386                         .for_reclaim = 1,
387                 };
388
389                 SetPageReclaim(page);
390                 res = mapping->a_ops->writepage(page, &wbc);
391                 if (res < 0)
392                         handle_write_error(mapping, page, res);
393                 if (res == AOP_WRITEPAGE_ACTIVATE) {
394                         ClearPageReclaim(page);
395                         return PAGE_ACTIVATE;
396                 }
397
398                 /*
399                  * Wait on writeback if requested to. This happens when
400                  * direct reclaiming a large contiguous area and the
401                  * first attempt to free a range of pages fails.
402                  */
403                 if (PageWriteback(page) && sync_writeback == PAGEOUT_IO_SYNC)
404                         wait_on_page_writeback(page);
405
406                 if (!PageWriteback(page)) {
407                         /* synchronous write or broken a_ops? */
408                         ClearPageReclaim(page);
409                 }
410                 inc_zone_page_state(page, NR_VMSCAN_WRITE);
411                 return PAGE_SUCCESS;
412         }
413
414         return PAGE_CLEAN;
415 }
416
417 /*
418  * Same as remove_mapping, but if the page is removed from the mapping, it
419  * gets returned with a refcount of 0.
420  */
421 static int __remove_mapping(struct address_space *mapping, struct page *page)
422 {
423         BUG_ON(!PageLocked(page));
424         BUG_ON(mapping != page_mapping(page));
425
426         spin_lock_irq(&mapping->tree_lock);
427         /*
428          * The non racy check for a busy page.
429          *
430          * Must be careful with the order of the tests. When someone has
431          * a ref to the page, it may be possible that they dirty it then
432          * drop the reference. So if PageDirty is tested before page_count
433          * here, then the following race may occur:
434          *
435          * get_user_pages(&page);
436          * [user mapping goes away]
437          * write_to(page);
438          *                              !PageDirty(page)    [good]
439          * SetPageDirty(page);
440          * put_page(page);
441          *                              !page_count(page)   [good, discard it]
442          *
443          * [oops, our write_to data is lost]
444          *
445          * Reversing the order of the tests ensures such a situation cannot
446          * escape unnoticed. The smp_rmb is needed to ensure the page->flags
447          * load is not satisfied before that of page->_count.
448          *
449          * Note that if SetPageDirty is always performed via set_page_dirty,
450          * and thus under tree_lock, then this ordering is not required.
451          */
452         if (!page_freeze_refs(page, 2))
453                 goto cannot_free;
454         /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */
455         if (unlikely(PageDirty(page))) {
456                 page_unfreeze_refs(page, 2);
457                 goto cannot_free;
458         }
459
460         if (PageSwapCache(page)) {
461                 swp_entry_t swap = { .val = page_private(page) };
462                 __delete_from_swap_cache(page);
463                 spin_unlock_irq(&mapping->tree_lock);
464                 swap_free(swap);
465         } else {
466                 __remove_from_page_cache(page);
467                 spin_unlock_irq(&mapping->tree_lock);
468         }
469
470         return 1;
471
472 cannot_free:
473         spin_unlock_irq(&mapping->tree_lock);
474         return 0;
475 }
476
477 /*
478  * Attempt to detach a locked page from its ->mapping.  If it is dirty or if
479  * someone else has a ref on the page, abort and return 0.  If it was
480  * successfully detached, return 1.  Assumes the caller has a single ref on
481  * this page.
482  */
483 int remove_mapping(struct address_space *mapping, struct page *page)
484 {
485         if (__remove_mapping(mapping, page)) {
486                 /*
487                  * Unfreezing the refcount with 1 rather than 2 effectively
488                  * drops the pagecache ref for us without requiring another
489                  * atomic operation.
490                  */
491                 page_unfreeze_refs(page, 1);
492                 return 1;
493         }
494         return 0;
495 }
496
497 /**
498  * putback_lru_page - put previously isolated page onto appropriate LRU list
499  * @page: page to be put back to appropriate lru list
500  *
501  * Add previously isolated @page to appropriate LRU list.
502  * Page may still be unevictable for other reasons.
503  *
504  * lru_lock must not be held, interrupts must be enabled.
505  */
506 #ifdef CONFIG_UNEVICTABLE_LRU
507 void putback_lru_page(struct page *page)
508 {
509         int lru;
510         int active = !!TestClearPageActive(page);
511         int was_unevictable = PageUnevictable(page);
512
513         VM_BUG_ON(PageLRU(page));
514
515 redo:
516         ClearPageUnevictable(page);
517
518         if (page_evictable(page, NULL)) {
519                 /*
520                  * For evictable pages, we can use the cache.
521                  * In event of a race, worst case is we end up with an
522                  * unevictable page on [in]active list.
523                  * We know how to handle that.
524                  */
525                 lru = active + page_is_file_cache(page);
526                 lru_cache_add_lru(page, lru);
527         } else {
528                 /*
529                  * Put unevictable pages directly on zone's unevictable
530                  * list.
531                  */
532                 lru = LRU_UNEVICTABLE;
533                 add_page_to_unevictable_list(page);
534         }
535
536         /*
537          * page's status can change while we move it among lru. If an evictable
538          * page is on unevictable list, it never be freed. To avoid that,
539          * check after we added it to the list, again.
540          */
541         if (lru == LRU_UNEVICTABLE && page_evictable(page, NULL)) {
542                 if (!isolate_lru_page(page)) {
543                         put_page(page);
544                         goto redo;
545                 }
546                 /* This means someone else dropped this page from LRU
547                  * So, it will be freed or putback to LRU again. There is
548                  * nothing to do here.
549                  */
550         }
551
552         if (was_unevictable && lru != LRU_UNEVICTABLE)
553                 count_vm_event(UNEVICTABLE_PGRESCUED);
554         else if (!was_unevictable && lru == LRU_UNEVICTABLE)
555                 count_vm_event(UNEVICTABLE_PGCULLED);
556
557         put_page(page);         /* drop ref from isolate */
558 }
559
560 #else /* CONFIG_UNEVICTABLE_LRU */
561
562 void putback_lru_page(struct page *page)
563 {
564         int lru;
565         VM_BUG_ON(PageLRU(page));
566
567         lru = !!TestClearPageActive(page) + page_is_file_cache(page);
568         lru_cache_add_lru(page, lru);
569         put_page(page);
570 }
571 #endif /* CONFIG_UNEVICTABLE_LRU */
572
573
574 /*
575  * shrink_page_list() returns the number of reclaimed pages
576  */
577 static unsigned long shrink_page_list(struct list_head *page_list,
578                                         struct scan_control *sc,
579                                         enum pageout_io sync_writeback)
580 {
581         LIST_HEAD(ret_pages);
582         struct pagevec freed_pvec;
583         int pgactivate = 0;
584         unsigned long nr_reclaimed = 0;
585
586         cond_resched();
587
588         pagevec_init(&freed_pvec, 1);
589         while (!list_empty(page_list)) {
590                 struct address_space *mapping;
591                 struct page *page;
592                 int may_enter_fs;
593                 int referenced;
594
595                 cond_resched();
596
597                 page = lru_to_page(page_list);
598                 list_del(&page->lru);
599
600                 if (!trylock_page(page))
601                         goto keep;
602
603                 VM_BUG_ON(PageActive(page));
604
605                 sc->nr_scanned++;
606
607                 if (unlikely(!page_evictable(page, NULL)))
608                         goto cull_mlocked;
609
610                 if (!sc->may_unmap && page_mapped(page))
611                         goto keep_locked;
612
613                 /* Double the slab pressure for mapped and swapcache pages */
614                 if (page_mapped(page) || PageSwapCache(page))
615                         sc->nr_scanned++;
616
617                 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
618                         (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
619
620                 if (PageWriteback(page)) {
621                         /*
622                          * Synchronous reclaim is performed in two passes,
623                          * first an asynchronous pass over the list to
624                          * start parallel writeback, and a second synchronous
625                          * pass to wait for the IO to complete.  Wait here
626                          * for any page for which writeback has already
627                          * started.
628                          */
629                         if (sync_writeback == PAGEOUT_IO_SYNC && may_enter_fs)
630                                 wait_on_page_writeback(page);
631                         else
632                                 goto keep_locked;
633                 }
634
635                 referenced = page_referenced(page, 1, sc->mem_cgroup);
636                 /* In active use or really unfreeable?  Activate it. */
637                 if (sc->order <= PAGE_ALLOC_COSTLY_ORDER &&
638                                         referenced && page_mapping_inuse(page))
639                         goto activate_locked;
640
641                 /*
642                  * Anonymous process memory has backing store?
643                  * Try to allocate it some swap space here.
644                  */
645                 if (PageAnon(page) && !PageSwapCache(page)) {
646                         if (!(sc->gfp_mask & __GFP_IO))
647                                 goto keep_locked;
648                         if (!add_to_swap(page))
649                                 goto activate_locked;
650                         may_enter_fs = 1;
651                 }
652
653                 mapping = page_mapping(page);
654
655                 /*
656                  * The page is mapped into the page tables of one or more
657                  * processes. Try to unmap it here.
658                  */
659                 if (page_mapped(page) && mapping) {
660                         switch (try_to_unmap(page, 0)) {
661                         case SWAP_FAIL:
662                                 goto activate_locked;
663                         case SWAP_AGAIN:
664                                 goto keep_locked;
665                         case SWAP_MLOCK:
666                                 goto cull_mlocked;
667                         case SWAP_SUCCESS:
668                                 ; /* try to free the page below */
669                         }
670                 }
671
672                 if (PageDirty(page)) {
673                         if (sc->order <= PAGE_ALLOC_COSTLY_ORDER && referenced)
674                                 goto keep_locked;
675                         if (!may_enter_fs)
676                                 goto keep_locked;
677                         if (!sc->may_writepage)
678                                 goto keep_locked;
679
680                         /* Page is dirty, try to write it out here */
681                         switch (pageout(page, mapping, sync_writeback)) {
682                         case PAGE_KEEP:
683                                 goto keep_locked;
684                         case PAGE_ACTIVATE:
685                                 goto activate_locked;
686                         case PAGE_SUCCESS:
687                                 if (PageWriteback(page) || PageDirty(page))
688                                         goto keep;
689                                 /*
690                                  * A synchronous write - probably a ramdisk.  Go
691                                  * ahead and try to reclaim the page.
692                                  */
693                                 if (!trylock_page(page))
694                                         goto keep;
695                                 if (PageDirty(page) || PageWriteback(page))
696                                         goto keep_locked;
697                                 mapping = page_mapping(page);
698                         case PAGE_CLEAN:
699                                 ; /* try to free the page below */
700                         }
701                 }
702
703                 /*
704                  * If the page has buffers, try to free the buffer mappings
705                  * associated with this page. If we succeed we try to free
706                  * the page as well.
707                  *
708                  * We do this even if the page is PageDirty().
709                  * try_to_release_page() does not perform I/O, but it is
710                  * possible for a page to have PageDirty set, but it is actually
711                  * clean (all its buffers are clean).  This happens if the
712                  * buffers were written out directly, with submit_bh(). ext3
713                  * will do this, as well as the blockdev mapping.
714                  * try_to_release_page() will discover that cleanness and will
715                  * drop the buffers and mark the page clean - it can be freed.
716                  *
717                  * Rarely, pages can have buffers and no ->mapping.  These are
718                  * the pages which were not successfully invalidated in
719                  * truncate_complete_page().  We try to drop those buffers here
720                  * and if that worked, and the page is no longer mapped into
721                  * process address space (page_count == 1) it can be freed.
722                  * Otherwise, leave the page on the LRU so it is swappable.
723                  */
724                 if (PagePrivate(page)) {
725                         if (!try_to_release_page(page, sc->gfp_mask))
726                                 goto activate_locked;
727                         if (!mapping && page_count(page) == 1) {
728                                 unlock_page(page);
729                                 if (put_page_testzero(page))
730                                         goto free_it;
731                                 else {
732                                         /*
733                                          * rare race with speculative reference.
734                                          * the speculative reference will free
735                                          * this page shortly, so we may
736                                          * increment nr_reclaimed here (and
737                                          * leave it off the LRU).
738                                          */
739                                         nr_reclaimed++;
740                                         continue;
741                                 }
742                         }
743                 }
744
745                 if (!mapping || !__remove_mapping(mapping, page))
746                         goto keep_locked;
747
748                 /*
749                  * At this point, we have no other references and there is
750                  * no way to pick any more up (removed from LRU, removed
751                  * from pagecache). Can use non-atomic bitops now (and
752                  * we obviously don't have to worry about waking up a process
753                  * waiting on the page lock, because there are no references.
754                  */
755                 __clear_page_locked(page);
756 free_it:
757                 nr_reclaimed++;
758                 if (!pagevec_add(&freed_pvec, page)) {
759                         __pagevec_free(&freed_pvec);
760                         pagevec_reinit(&freed_pvec);
761                 }
762                 continue;
763
764 cull_mlocked:
765                 if (PageSwapCache(page))
766                         try_to_free_swap(page);
767                 unlock_page(page);
768                 putback_lru_page(page);
769                 continue;
770
771 activate_locked:
772                 /* Not a candidate for swapping, so reclaim swap space. */
773                 if (PageSwapCache(page) && vm_swap_full())
774                         try_to_free_swap(page);
775                 VM_BUG_ON(PageActive(page));
776                 SetPageActive(page);
777                 pgactivate++;
778 keep_locked:
779                 unlock_page(page);
780 keep:
781                 list_add(&page->lru, &ret_pages);
782                 VM_BUG_ON(PageLRU(page) || PageUnevictable(page));
783         }
784         list_splice(&ret_pages, page_list);
785         if (pagevec_count(&freed_pvec))
786                 __pagevec_free(&freed_pvec);
787         count_vm_events(PGACTIVATE, pgactivate);
788         return nr_reclaimed;
789 }
790
791 /* LRU Isolation modes. */
792 #define ISOLATE_INACTIVE 0      /* Isolate inactive pages. */
793 #define ISOLATE_ACTIVE 1        /* Isolate active pages. */
794 #define ISOLATE_BOTH 2          /* Isolate both active and inactive pages. */
795
796 /*
797  * Attempt to remove the specified page from its LRU.  Only take this page
798  * if it is of the appropriate PageActive status.  Pages which are being
799  * freed elsewhere are also ignored.
800  *
801  * page:        page to consider
802  * mode:        one of the LRU isolation modes defined above
803  *
804  * returns 0 on success, -ve errno on failure.
805  */
806 int __isolate_lru_page(struct page *page, int mode, int file)
807 {
808         int ret = -EINVAL;
809
810         /* Only take pages on the LRU. */
811         if (!PageLRU(page))
812                 return ret;
813
814         /*
815          * When checking the active state, we need to be sure we are
816          * dealing with comparible boolean values.  Take the logical not
817          * of each.
818          */
819         if (mode != ISOLATE_BOTH && (!PageActive(page) != !mode))
820                 return ret;
821
822         if (mode != ISOLATE_BOTH && (!page_is_file_cache(page) != !file))
823                 return ret;
824
825         /*
826          * When this function is being called for lumpy reclaim, we
827          * initially look into all LRU pages, active, inactive and
828          * unevictable; only give shrink_page_list evictable pages.
829          */
830         if (PageUnevictable(page))
831                 return ret;
832
833         ret = -EBUSY;
834
835         if (likely(get_page_unless_zero(page))) {
836                 /*
837                  * Be careful not to clear PageLRU until after we're
838                  * sure the page is not being freed elsewhere -- the
839                  * page release code relies on it.
840                  */
841                 ClearPageLRU(page);
842                 ret = 0;
843                 mem_cgroup_del_lru(page);
844         }
845
846         return ret;
847 }
848
849 /*
850  * zone->lru_lock is heavily contended.  Some of the functions that
851  * shrink the lists perform better by taking out a batch of pages
852  * and working on them outside the LRU lock.
853  *
854  * For pagecache intensive workloads, this function is the hottest
855  * spot in the kernel (apart from copy_*_user functions).
856  *
857  * Appropriate locks must be held before calling this function.
858  *
859  * @nr_to_scan: The number of pages to look through on the list.
860  * @src:        The LRU list to pull pages off.
861  * @dst:        The temp list to put pages on to.
862  * @scanned:    The number of pages that were scanned.
863  * @order:      The caller's attempted allocation order
864  * @mode:       One of the LRU isolation modes
865  * @file:       True [1] if isolating file [!anon] pages
866  *
867  * returns how many pages were moved onto *@dst.
868  */
869 static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
870                 struct list_head *src, struct list_head *dst,
871                 unsigned long *scanned, int order, int mode, int file)
872 {
873         unsigned long nr_taken = 0;
874         unsigned long scan;
875
876         for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
877                 struct page *page;
878                 unsigned long pfn;
879                 unsigned long end_pfn;
880                 unsigned long page_pfn;
881                 int zone_id;
882
883                 page = lru_to_page(src);
884                 prefetchw_prev_lru_page(page, src, flags);
885
886                 VM_BUG_ON(!PageLRU(page));
887
888                 switch (__isolate_lru_page(page, mode, file)) {
889                 case 0:
890                         list_move(&page->lru, dst);
891                         nr_taken++;
892                         break;
893
894                 case -EBUSY:
895                         /* else it is being freed elsewhere */
896                         list_move(&page->lru, src);
897                         continue;
898
899                 default:
900                         BUG();
901                 }
902
903                 if (!order)
904                         continue;
905
906                 /*
907                  * Attempt to take all pages in the order aligned region
908                  * surrounding the tag page.  Only take those pages of
909                  * the same active state as that tag page.  We may safely
910                  * round the target page pfn down to the requested order
911                  * as the mem_map is guarenteed valid out to MAX_ORDER,
912                  * where that page is in a different zone we will detect
913                  * it from its zone id and abort this block scan.
914                  */
915                 zone_id = page_zone_id(page);
916                 page_pfn = page_to_pfn(page);
917                 pfn = page_pfn & ~((1 << order) - 1);
918                 end_pfn = pfn + (1 << order);
919                 for (; pfn < end_pfn; pfn++) {
920                         struct page *cursor_page;
921
922                         /* The target page is in the block, ignore it. */
923                         if (unlikely(pfn == page_pfn))
924                                 continue;
925
926                         /* Avoid holes within the zone. */
927                         if (unlikely(!pfn_valid_within(pfn)))
928                                 break;
929
930                         cursor_page = pfn_to_page(pfn);
931
932                         /* Check that we have not crossed a zone boundary. */
933                         if (unlikely(page_zone_id(cursor_page) != zone_id))
934                                 continue;
935                         switch (__isolate_lru_page(cursor_page, mode, file)) {
936                         case 0:
937                                 list_move(&cursor_page->lru, dst);
938                                 nr_taken++;
939                                 scan++;
940                                 break;
941
942                         case -EBUSY:
943                                 /* else it is being freed elsewhere */
944                                 list_move(&cursor_page->lru, src);
945                         default:
946                                 break;  /* ! on LRU or wrong list */
947                         }
948                 }
949         }
950
951         *scanned = scan;
952         return nr_taken;
953 }
954
955 static unsigned long isolate_pages_global(unsigned long nr,
956                                         struct list_head *dst,
957                                         unsigned long *scanned, int order,
958                                         int mode, struct zone *z,
959                                         struct mem_cgroup *mem_cont,
960                                         int active, int file)
961 {
962         int lru = LRU_BASE;
963         if (active)
964                 lru += LRU_ACTIVE;
965         if (file)
966                 lru += LRU_FILE;
967         return isolate_lru_pages(nr, &z->lru[lru].list, dst, scanned, order,
968                                                                 mode, !!file);
969 }
970
971 /*
972  * clear_active_flags() is a helper for shrink_active_list(), clearing
973  * any active bits from the pages in the list.
974  */
975 static unsigned long clear_active_flags(struct list_head *page_list,
976                                         unsigned int *count)
977 {
978         int nr_active = 0;
979         int lru;
980         struct page *page;
981
982         list_for_each_entry(page, page_list, lru) {
983                 lru = page_is_file_cache(page);
984                 if (PageActive(page)) {
985                         lru += LRU_ACTIVE;
986                         ClearPageActive(page);
987                         nr_active++;
988                 }
989                 count[lru]++;
990         }
991
992         return nr_active;
993 }
994
995 /**
996  * isolate_lru_page - tries to isolate a page from its LRU list
997  * @page: page to isolate from its LRU list
998  *
999  * Isolates a @page from an LRU list, clears PageLRU and adjusts the
1000  * vmstat statistic corresponding to whatever LRU list the page was on.
1001  *
1002  * Returns 0 if the page was removed from an LRU list.
1003  * Returns -EBUSY if the page was not on an LRU list.
1004  *
1005  * The returned page will have PageLRU() cleared.  If it was found on
1006  * the active list, it will have PageActive set.  If it was found on
1007  * the unevictable list, it will have the PageUnevictable bit set. That flag
1008  * may need to be cleared by the caller before letting the page go.
1009  *
1010  * The vmstat statistic corresponding to the list on which the page was
1011  * found will be decremented.
1012  *
1013  * Restrictions:
1014  * (1) Must be called with an elevated refcount on the page. This is a
1015  *     fundamentnal difference from isolate_lru_pages (which is called
1016  *     without a stable reference).
1017  * (2) the lru_lock must not be held.
1018  * (3) interrupts must be enabled.
1019  */
1020 int isolate_lru_page(struct page *page)
1021 {
1022         int ret = -EBUSY;
1023
1024         if (PageLRU(page)) {
1025                 struct zone *zone = page_zone(page);
1026
1027                 spin_lock_irq(&zone->lru_lock);
1028                 if (PageLRU(page) && get_page_unless_zero(page)) {
1029                         int lru = page_lru(page);
1030                         ret = 0;
1031                         ClearPageLRU(page);
1032
1033                         del_page_from_lru_list(zone, page, lru);
1034                 }
1035                 spin_unlock_irq(&zone->lru_lock);
1036         }
1037         return ret;
1038 }
1039
1040 /*
1041  * shrink_inactive_list() is a helper for shrink_zone().  It returns the number
1042  * of reclaimed pages
1043  */
1044 static unsigned long shrink_inactive_list(unsigned long max_scan,
1045                         struct zone *zone, struct scan_control *sc,
1046                         int priority, int file)
1047 {
1048         LIST_HEAD(page_list);
1049         struct pagevec pvec;
1050         unsigned long nr_scanned = 0;
1051         unsigned long nr_reclaimed = 0;
1052         struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
1053
1054         pagevec_init(&pvec, 1);
1055
1056         lru_add_drain();
1057         spin_lock_irq(&zone->lru_lock);
1058         do {
1059                 struct page *page;
1060                 unsigned long nr_taken;
1061                 unsigned long nr_scan;
1062                 unsigned long nr_freed;
1063                 unsigned long nr_active;
1064                 unsigned int count[NR_LRU_LISTS] = { 0, };
1065                 int mode = ISOLATE_INACTIVE;
1066
1067                 /*
1068                  * If we need a large contiguous chunk of memory, or have
1069                  * trouble getting a small set of contiguous pages, we
1070                  * will reclaim both active and inactive pages.
1071                  *
1072                  * We use the same threshold as pageout congestion_wait below.
1073                  */
1074                 if (sc->order > PAGE_ALLOC_COSTLY_ORDER)
1075                         mode = ISOLATE_BOTH;
1076                 else if (sc->order && priority < DEF_PRIORITY - 2)
1077                         mode = ISOLATE_BOTH;
1078
1079                 nr_taken = sc->isolate_pages(sc->swap_cluster_max,
1080                              &page_list, &nr_scan, sc->order, mode,
1081                                 zone, sc->mem_cgroup, 0, file);
1082                 nr_active = clear_active_flags(&page_list, count);
1083                 __count_vm_events(PGDEACTIVATE, nr_active);
1084
1085                 __mod_zone_page_state(zone, NR_ACTIVE_FILE,
1086                                                 -count[LRU_ACTIVE_FILE]);
1087                 __mod_zone_page_state(zone, NR_INACTIVE_FILE,
1088                                                 -count[LRU_INACTIVE_FILE]);
1089                 __mod_zone_page_state(zone, NR_ACTIVE_ANON,
1090                                                 -count[LRU_ACTIVE_ANON]);
1091                 __mod_zone_page_state(zone, NR_INACTIVE_ANON,
1092                                                 -count[LRU_INACTIVE_ANON]);
1093
1094                 if (scanning_global_lru(sc))
1095                         zone->pages_scanned += nr_scan;
1096
1097                 reclaim_stat->recent_scanned[0] += count[LRU_INACTIVE_ANON];
1098                 reclaim_stat->recent_scanned[0] += count[LRU_ACTIVE_ANON];
1099                 reclaim_stat->recent_scanned[1] += count[LRU_INACTIVE_FILE];
1100                 reclaim_stat->recent_scanned[1] += count[LRU_ACTIVE_FILE];
1101
1102                 spin_unlock_irq(&zone->lru_lock);
1103
1104                 nr_scanned += nr_scan;
1105                 nr_freed = shrink_page_list(&page_list, sc, PAGEOUT_IO_ASYNC);
1106
1107                 /*
1108                  * If we are direct reclaiming for contiguous pages and we do
1109                  * not reclaim everything in the list, try again and wait
1110                  * for IO to complete. This will stall high-order allocations
1111                  * but that should be acceptable to the caller
1112                  */
1113                 if (nr_freed < nr_taken && !current_is_kswapd() &&
1114                                         sc->order > PAGE_ALLOC_COSTLY_ORDER) {
1115                         congestion_wait(WRITE, HZ/10);
1116
1117                         /*
1118                          * The attempt at page out may have made some
1119                          * of the pages active, mark them inactive again.
1120                          */
1121                         nr_active = clear_active_flags(&page_list, count);
1122                         count_vm_events(PGDEACTIVATE, nr_active);
1123
1124                         nr_freed += shrink_page_list(&page_list, sc,
1125                                                         PAGEOUT_IO_SYNC);
1126                 }
1127
1128                 nr_reclaimed += nr_freed;
1129                 local_irq_disable();
1130                 if (current_is_kswapd()) {
1131                         __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan);
1132                         __count_vm_events(KSWAPD_STEAL, nr_freed);
1133                 } else if (scanning_global_lru(sc))
1134                         __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan);
1135
1136                 __count_zone_vm_events(PGSTEAL, zone, nr_freed);
1137
1138                 if (nr_taken == 0)
1139                         goto done;
1140
1141                 spin_lock(&zone->lru_lock);
1142                 /*
1143                  * Put back any unfreeable pages.
1144                  */
1145                 while (!list_empty(&page_list)) {
1146                         int lru;
1147                         page = lru_to_page(&page_list);
1148                         VM_BUG_ON(PageLRU(page));
1149                         list_del(&page->lru);
1150                         if (unlikely(!page_evictable(page, NULL))) {
1151                                 spin_unlock_irq(&zone->lru_lock);
1152                                 putback_lru_page(page);
1153                                 spin_lock_irq(&zone->lru_lock);
1154                                 continue;
1155                         }
1156                         SetPageLRU(page);
1157                         lru = page_lru(page);
1158                         add_page_to_lru_list(zone, page, lru);
1159                         if (PageActive(page)) {
1160                                 int file = !!page_is_file_cache(page);
1161                                 reclaim_stat->recent_rotated[file]++;
1162                         }
1163                         if (!pagevec_add(&pvec, page)) {
1164                                 spin_unlock_irq(&zone->lru_lock);
1165                                 __pagevec_release(&pvec);
1166                                 spin_lock_irq(&zone->lru_lock);
1167                         }
1168                 }
1169         } while (nr_scanned < max_scan);
1170         spin_unlock(&zone->lru_lock);
1171 done:
1172         local_irq_enable();
1173         pagevec_release(&pvec);
1174         return nr_reclaimed;
1175 }
1176
1177 /*
1178  * We are about to scan this zone at a certain priority level.  If that priority
1179  * level is smaller (ie: more urgent) than the previous priority, then note
1180  * that priority level within the zone.  This is done so that when the next
1181  * process comes in to scan this zone, it will immediately start out at this
1182  * priority level rather than having to build up its own scanning priority.
1183  * Here, this priority affects only the reclaim-mapped threshold.
1184  */
1185 static inline void note_zone_scanning_priority(struct zone *zone, int priority)
1186 {
1187         if (priority < zone->prev_priority)
1188                 zone->prev_priority = priority;
1189 }
1190
1191 /*
1192  * This moves pages from the active list to the inactive list.
1193  *
1194  * We move them the other way if the page is referenced by one or more
1195  * processes, from rmap.
1196  *
1197  * If the pages are mostly unmapped, the processing is fast and it is
1198  * appropriate to hold zone->lru_lock across the whole operation.  But if
1199  * the pages are mapped, the processing is slow (page_referenced()) so we
1200  * should drop zone->lru_lock around each page.  It's impossible to balance
1201  * this, so instead we remove the pages from the LRU while processing them.
1202  * It is safe to rely on PG_active against the non-LRU pages in here because
1203  * nobody will play with that bit on a non-LRU page.
1204  *
1205  * The downside is that we have to touch page->_count against each page.
1206  * But we had to alter page->flags anyway.
1207  */
1208
1209
1210 static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
1211                         struct scan_control *sc, int priority, int file)
1212 {
1213         unsigned long pgmoved;
1214         int pgdeactivate = 0;
1215         unsigned long pgscanned;
1216         LIST_HEAD(l_hold);      /* The pages which were snipped off */
1217         LIST_HEAD(l_inactive);
1218         struct page *page;
1219         struct pagevec pvec;
1220         enum lru_list lru;
1221         struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
1222
1223         lru_add_drain();
1224         spin_lock_irq(&zone->lru_lock);
1225         pgmoved = sc->isolate_pages(nr_pages, &l_hold, &pgscanned, sc->order,
1226                                         ISOLATE_ACTIVE, zone,
1227                                         sc->mem_cgroup, 1, file);
1228         /*
1229          * zone->pages_scanned is used for detect zone's oom
1230          * mem_cgroup remembers nr_scan by itself.
1231          */
1232         if (scanning_global_lru(sc)) {
1233                 zone->pages_scanned += pgscanned;
1234         }
1235         reclaim_stat->recent_scanned[!!file] += pgmoved;
1236
1237         if (file)
1238                 __mod_zone_page_state(zone, NR_ACTIVE_FILE, -pgmoved);
1239         else
1240                 __mod_zone_page_state(zone, NR_ACTIVE_ANON, -pgmoved);
1241         spin_unlock_irq(&zone->lru_lock);
1242
1243         pgmoved = 0;
1244         while (!list_empty(&l_hold)) {
1245                 cond_resched();
1246                 page = lru_to_page(&l_hold);
1247                 list_del(&page->lru);
1248
1249                 if (unlikely(!page_evictable(page, NULL))) {
1250                         putback_lru_page(page);
1251                         continue;
1252                 }
1253
1254                 /* page_referenced clears PageReferenced */
1255                 if (page_mapping_inuse(page) &&
1256                     page_referenced(page, 0, sc->mem_cgroup))
1257                         pgmoved++;
1258
1259                 list_add(&page->lru, &l_inactive);
1260         }
1261
1262         /*
1263          * Move the pages to the [file or anon] inactive list.
1264          */
1265         pagevec_init(&pvec, 1);
1266         lru = LRU_BASE + file * LRU_FILE;
1267
1268         spin_lock_irq(&zone->lru_lock);
1269         /*
1270          * Count referenced pages from currently used mappings as
1271          * rotated, even though they are moved to the inactive list.
1272          * This helps balance scan pressure between file and anonymous
1273          * pages in get_scan_ratio.
1274          */
1275         reclaim_stat->recent_rotated[!!file] += pgmoved;
1276
1277         pgmoved = 0;
1278         while (!list_empty(&l_inactive)) {
1279                 page = lru_to_page(&l_inactive);
1280                 prefetchw_prev_lru_page(page, &l_inactive, flags);
1281                 VM_BUG_ON(PageLRU(page));
1282                 SetPageLRU(page);
1283                 VM_BUG_ON(!PageActive(page));
1284                 ClearPageActive(page);
1285
1286                 list_move(&page->lru, &zone->lru[lru].list);
1287                 mem_cgroup_add_lru_list(page, lru);
1288                 pgmoved++;
1289                 if (!pagevec_add(&pvec, page)) {
1290                         __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
1291                         spin_unlock_irq(&zone->lru_lock);
1292                         pgdeactivate += pgmoved;
1293                         pgmoved = 0;
1294                         if (buffer_heads_over_limit)
1295                                 pagevec_strip(&pvec);
1296                         __pagevec_release(&pvec);
1297                         spin_lock_irq(&zone->lru_lock);
1298                 }
1299         }
1300         __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
1301         pgdeactivate += pgmoved;
1302         __count_zone_vm_events(PGREFILL, zone, pgscanned);
1303         __count_vm_events(PGDEACTIVATE, pgdeactivate);
1304         spin_unlock_irq(&zone->lru_lock);
1305         if (buffer_heads_over_limit)
1306                 pagevec_strip(&pvec);
1307         pagevec_release(&pvec);
1308 }
1309
1310 static int inactive_anon_is_low_global(struct zone *zone)
1311 {
1312         unsigned long active, inactive;
1313
1314         active = zone_page_state(zone, NR_ACTIVE_ANON);
1315         inactive = zone_page_state(zone, NR_INACTIVE_ANON);
1316
1317         if (inactive * zone->inactive_ratio < active)
1318                 return 1;
1319
1320         return 0;
1321 }
1322
1323 /**
1324  * inactive_anon_is_low - check if anonymous pages need to be deactivated
1325  * @zone: zone to check
1326  * @sc:   scan control of this context
1327  *
1328  * Returns true if the zone does not have enough inactive anon pages,
1329  * meaning some active anon pages need to be deactivated.
1330  */
1331 static int inactive_anon_is_low(struct zone *zone, struct scan_control *sc)
1332 {
1333         int low;
1334
1335         if (scanning_global_lru(sc))
1336                 low = inactive_anon_is_low_global(zone);
1337         else
1338                 low = mem_cgroup_inactive_anon_is_low(sc->mem_cgroup);
1339         return low;
1340 }
1341
1342 static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
1343         struct zone *zone, struct scan_control *sc, int priority)
1344 {
1345         int file = is_file_lru(lru);
1346
1347         if (lru == LRU_ACTIVE_FILE) {
1348                 shrink_active_list(nr_to_scan, zone, sc, priority, file);
1349                 return 0;
1350         }
1351
1352         if (lru == LRU_ACTIVE_ANON && inactive_anon_is_low(zone, sc)) {
1353                 shrink_active_list(nr_to_scan, zone, sc, priority, file);
1354                 return 0;
1355         }
1356         return shrink_inactive_list(nr_to_scan, zone, sc, priority, file);
1357 }
1358
1359 /*
1360  * Determine how aggressively the anon and file LRU lists should be
1361  * scanned.  The relative value of each set of LRU lists is determined
1362  * by looking at the fraction of the pages scanned we did rotate back
1363  * onto the active list instead of evict.
1364  *
1365  * percent[0] specifies how much pressure to put on ram/swap backed
1366  * memory, while percent[1] determines pressure on the file LRUs.
1367  */
1368 static void get_scan_ratio(struct zone *zone, struct scan_control *sc,
1369                                         unsigned long *percent)
1370 {
1371         unsigned long anon, file, free;
1372         unsigned long anon_prio, file_prio;
1373         unsigned long ap, fp;
1374         struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
1375
1376         /* If we have no swap space, do not bother scanning anon pages. */
1377         if (nr_swap_pages <= 0) {
1378                 percent[0] = 0;
1379                 percent[1] = 100;
1380                 return;
1381         }
1382
1383         anon  = zone_nr_pages(zone, sc, LRU_ACTIVE_ANON) +
1384                 zone_nr_pages(zone, sc, LRU_INACTIVE_ANON);
1385         file  = zone_nr_pages(zone, sc, LRU_ACTIVE_FILE) +
1386                 zone_nr_pages(zone, sc, LRU_INACTIVE_FILE);
1387
1388         if (scanning_global_lru(sc)) {
1389                 free  = zone_page_state(zone, NR_FREE_PAGES);
1390                 /* If we have very few page cache pages,
1391                    force-scan anon pages. */
1392                 if (unlikely(file + free <= zone->pages_high)) {
1393                         percent[0] = 100;
1394                         percent[1] = 0;
1395                         return;
1396                 }
1397         }
1398
1399         /*
1400          * OK, so we have swap space and a fair amount of page cache
1401          * pages.  We use the recently rotated / recently scanned
1402          * ratios to determine how valuable each cache is.
1403          *
1404          * Because workloads change over time (and to avoid overflow)
1405          * we keep these statistics as a floating average, which ends
1406          * up weighing recent references more than old ones.
1407          *
1408          * anon in [0], file in [1]
1409          */
1410         if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
1411                 spin_lock_irq(&zone->lru_lock);
1412                 reclaim_stat->recent_scanned[0] /= 2;
1413                 reclaim_stat->recent_rotated[0] /= 2;
1414                 spin_unlock_irq(&zone->lru_lock);
1415         }
1416
1417         if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
1418                 spin_lock_irq(&zone->lru_lock);
1419                 reclaim_stat->recent_scanned[1] /= 2;
1420                 reclaim_stat->recent_rotated[1] /= 2;
1421                 spin_unlock_irq(&zone->lru_lock);
1422         }
1423
1424         /*
1425          * With swappiness at 100, anonymous and file have the same priority.
1426          * This scanning priority is essentially the inverse of IO cost.
1427          */
1428         anon_prio = sc->swappiness;
1429         file_prio = 200 - sc->swappiness;
1430
1431         /*
1432          * The amount of pressure on anon vs file pages is inversely
1433          * proportional to the fraction of recently scanned pages on
1434          * each list that were recently referenced and in active use.
1435          */
1436         ap = (anon_prio + 1) * (reclaim_stat->recent_scanned[0] + 1);
1437         ap /= reclaim_stat->recent_rotated[0] + 1;
1438
1439         fp = (file_prio + 1) * (reclaim_stat->recent_scanned[1] + 1);
1440         fp /= reclaim_stat->recent_rotated[1] + 1;
1441
1442         /* Normalize to percentages */
1443         percent[0] = 100 * ap / (ap + fp + 1);
1444         percent[1] = 100 - percent[0];
1445 }
1446
1447
1448 /*
1449  * This is a basic per-zone page freer.  Used by both kswapd and direct reclaim.
1450  */
1451 static void shrink_zone(int priority, struct zone *zone,
1452                                 struct scan_control *sc)
1453 {
1454         unsigned long nr[NR_LRU_LISTS];
1455         unsigned long nr_to_scan;
1456         unsigned long percent[2];       /* anon @ 0; file @ 1 */
1457         enum lru_list l;
1458         unsigned long nr_reclaimed = sc->nr_reclaimed;
1459         unsigned long swap_cluster_max = sc->swap_cluster_max;
1460
1461         get_scan_ratio(zone, sc, percent);
1462
1463         for_each_evictable_lru(l) {
1464                 int file = is_file_lru(l);
1465                 int scan;
1466
1467                 scan = zone_nr_pages(zone, sc, l);
1468                 if (priority) {
1469                         scan >>= priority;
1470                         scan = (scan * percent[file]) / 100;
1471                 }
1472                 if (scanning_global_lru(sc)) {
1473                         zone->lru[l].nr_scan += scan;
1474                         nr[l] = zone->lru[l].nr_scan;
1475                         if (nr[l] >= swap_cluster_max)
1476                                 zone->lru[l].nr_scan = 0;
1477                         else
1478                                 nr[l] = 0;
1479                 } else
1480                         nr[l] = scan;
1481         }
1482
1483         while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
1484                                         nr[LRU_INACTIVE_FILE]) {
1485                 for_each_evictable_lru(l) {
1486                         if (nr[l]) {
1487                                 nr_to_scan = min(nr[l], swap_cluster_max);
1488                                 nr[l] -= nr_to_scan;
1489
1490                                 nr_reclaimed += shrink_list(l, nr_to_scan,
1491                                                             zone, sc, priority);
1492                         }
1493                 }
1494                 /*
1495                  * On large memory systems, scan >> priority can become
1496                  * really large. This is fine for the starting priority;
1497                  * we want to put equal scanning pressure on each zone.
1498                  * However, if the VM has a harder time of freeing pages,
1499                  * with multiple processes reclaiming pages, the total
1500                  * freeing target can get unreasonably large.
1501                  */
1502                 if (nr_reclaimed > swap_cluster_max &&
1503                         priority < DEF_PRIORITY && !current_is_kswapd())
1504                         break;
1505         }
1506
1507         sc->nr_reclaimed = nr_reclaimed;
1508
1509         /*
1510          * Even if we did not try to evict anon pages at all, we want to
1511          * rebalance the anon lru active/inactive ratio.
1512          */
1513         if (inactive_anon_is_low(zone, sc))
1514                 shrink_active_list(SWAP_CLUSTER_MAX, zone, sc, priority, 0);
1515
1516         throttle_vm_writeout(sc->gfp_mask);
1517 }
1518
1519 /*
1520  * This is the direct reclaim path, for page-allocating processes.  We only
1521  * try to reclaim pages from zones which will satisfy the caller's allocation
1522  * request.
1523  *
1524  * We reclaim from a zone even if that zone is over pages_high.  Because:
1525  * a) The caller may be trying to free *extra* pages to satisfy a higher-order
1526  *    allocation or
1527  * b) The zones may be over pages_high but they must go *over* pages_high to
1528  *    satisfy the `incremental min' zone defense algorithm.
1529  *
1530  * If a zone is deemed to be full of pinned pages then just give it a light
1531  * scan then give up on it.
1532  */
1533 static void shrink_zones(int priority, struct zonelist *zonelist,
1534                                         struct scan_control *sc)
1535 {
1536         enum zone_type high_zoneidx = gfp_zone(sc->gfp_mask);
1537         struct zoneref *z;
1538         struct zone *zone;
1539
1540         sc->all_unreclaimable = 1;
1541         for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
1542                 if (!populated_zone(zone))
1543                         continue;
1544                 /*
1545                  * Take care memory controller reclaiming has small influence
1546                  * to global LRU.
1547                  */
1548                 if (scanning_global_lru(sc)) {
1549                         if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
1550                                 continue;
1551                         note_zone_scanning_priority(zone, priority);
1552
1553                         if (zone_is_all_unreclaimable(zone) &&
1554                                                 priority != DEF_PRIORITY)
1555                                 continue;       /* Let kswapd poll it */
1556                         sc->all_unreclaimable = 0;
1557                 } else {
1558                         /*
1559                          * Ignore cpuset limitation here. We just want to reduce
1560                          * # of used pages by us regardless of memory shortage.
1561                          */
1562                         sc->all_unreclaimable = 0;
1563                         mem_cgroup_note_reclaim_priority(sc->mem_cgroup,
1564                                                         priority);
1565                 }
1566
1567                 shrink_zone(priority, zone, sc);
1568         }
1569 }
1570
1571 /*
1572  * This is the main entry point to direct page reclaim.
1573  *
1574  * If a full scan of the inactive list fails to free enough memory then we
1575  * are "out of memory" and something needs to be killed.
1576  *
1577  * If the caller is !__GFP_FS then the probability of a failure is reasonably
1578  * high - the zone may be full of dirty or under-writeback pages, which this
1579  * caller can't do much about.  We kick pdflush and take explicit naps in the
1580  * hope that some of these pages can be written.  But if the allocating task
1581  * holds filesystem locks which prevent writeout this might not work, and the
1582  * allocation attempt will fail.
1583  *
1584  * returns:     0, if no pages reclaimed
1585  *              else, the number of pages reclaimed
1586  */
1587 static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
1588                                         struct scan_control *sc)
1589 {
1590         int priority;
1591         unsigned long ret = 0;
1592         unsigned long total_scanned = 0;
1593         struct reclaim_state *reclaim_state = current->reclaim_state;
1594         unsigned long lru_pages = 0;
1595         struct zoneref *z;
1596         struct zone *zone;
1597         enum zone_type high_zoneidx = gfp_zone(sc->gfp_mask);
1598
1599         delayacct_freepages_start();
1600
1601         if (scanning_global_lru(sc))
1602                 count_vm_event(ALLOCSTALL);
1603         /*
1604          * mem_cgroup will not do shrink_slab.
1605          */
1606         if (scanning_global_lru(sc)) {
1607                 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
1608
1609                         if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
1610                                 continue;
1611
1612                         lru_pages += zone_lru_pages(zone);
1613                 }
1614         }
1615
1616         for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1617                 sc->nr_scanned = 0;
1618                 if (!priority)
1619                         disable_swap_token();
1620                 shrink_zones(priority, zonelist, sc);
1621                 /*
1622                  * Don't shrink slabs when reclaiming memory from
1623                  * over limit cgroups
1624                  */
1625                 if (scanning_global_lru(sc)) {
1626                         shrink_slab(sc->nr_scanned, sc->gfp_mask, lru_pages);
1627                         if (reclaim_state) {
1628                                 sc->nr_reclaimed += reclaim_state->reclaimed_slab;
1629                                 reclaim_state->reclaimed_slab = 0;
1630                         }
1631                 }
1632                 total_scanned += sc->nr_scanned;
1633                 if (sc->nr_reclaimed >= sc->swap_cluster_max) {
1634                         ret = sc->nr_reclaimed;
1635                         goto out;
1636                 }
1637
1638                 /*
1639                  * Try to write back as many pages as we just scanned.  This
1640                  * tends to cause slow streaming writers to write data to the
1641                  * disk smoothly, at the dirtying rate, which is nice.   But
1642                  * that's undesirable in laptop mode, where we *want* lumpy
1643                  * writeout.  So in laptop mode, write out the whole world.
1644                  */
1645                 if (total_scanned > sc->swap_cluster_max +
1646                                         sc->swap_cluster_max / 2) {
1647                         wakeup_pdflush(laptop_mode ? 0 : total_scanned);
1648                         sc->may_writepage = 1;
1649                 }
1650
1651                 /* Take a nap, wait for some writeback to complete */
1652                 if (sc->nr_scanned && priority < DEF_PRIORITY - 2)
1653                         congestion_wait(WRITE, HZ/10);
1654         }
1655         /* top priority shrink_zones still had more to do? don't OOM, then */
1656         if (!sc->all_unreclaimable && scanning_global_lru(sc))
1657                 ret = sc->nr_reclaimed;
1658 out:
1659         /*
1660          * Now that we've scanned all the zones at this priority level, note
1661          * that level within the zone so that the next thread which performs
1662          * scanning of this zone will immediately start out at this priority
1663          * level.  This affects only the decision whether or not to bring
1664          * mapped pages onto the inactive list.
1665          */
1666         if (priority < 0)
1667                 priority = 0;
1668
1669         if (scanning_global_lru(sc)) {
1670                 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
1671
1672                         if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
1673                                 continue;
1674
1675                         zone->prev_priority = priority;
1676                 }
1677         } else
1678                 mem_cgroup_record_reclaim_priority(sc->mem_cgroup, priority);
1679
1680         delayacct_freepages_end();
1681
1682         return ret;
1683 }
1684
1685 unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
1686                                                                 gfp_t gfp_mask)
1687 {
1688         struct scan_control sc = {
1689                 .gfp_mask = gfp_mask,
1690                 .may_writepage = !laptop_mode,
1691                 .swap_cluster_max = SWAP_CLUSTER_MAX,
1692                 .may_unmap = 1,
1693                 .swappiness = vm_swappiness,
1694                 .order = order,
1695                 .mem_cgroup = NULL,
1696                 .isolate_pages = isolate_pages_global,
1697         };
1698
1699         return do_try_to_free_pages(zonelist, &sc);
1700 }
1701
1702 #ifdef CONFIG_CGROUP_MEM_RES_CTLR
1703
1704 unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont,
1705                                            gfp_t gfp_mask,
1706                                            bool noswap,
1707                                            unsigned int swappiness)
1708 {
1709         struct scan_control sc = {
1710                 .may_writepage = !laptop_mode,
1711                 .may_unmap = 1,
1712                 .swap_cluster_max = SWAP_CLUSTER_MAX,
1713                 .swappiness = swappiness,
1714                 .order = 0,
1715                 .mem_cgroup = mem_cont,
1716                 .isolate_pages = mem_cgroup_isolate_pages,
1717         };
1718         struct zonelist *zonelist;
1719
1720         if (noswap)
1721                 sc.may_unmap = 0;
1722
1723         sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
1724                         (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
1725         zonelist = NODE_DATA(numa_node_id())->node_zonelists;
1726         return do_try_to_free_pages(zonelist, &sc);
1727 }
1728 #endif
1729
1730 /*
1731  * For kswapd, balance_pgdat() will work across all this node's zones until
1732  * they are all at pages_high.
1733  *
1734  * Returns the number of pages which were actually freed.
1735  *
1736  * There is special handling here for zones which are full of pinned pages.
1737  * This can happen if the pages are all mlocked, or if they are all used by
1738  * device drivers (say, ZONE_DMA).  Or if they are all in use by hugetlb.
1739  * What we do is to detect the case where all pages in the zone have been
1740  * scanned twice and there has been zero successful reclaim.  Mark the zone as
1741  * dead and from now on, only perform a short scan.  Basically we're polling
1742  * the zone for when the problem goes away.
1743  *
1744  * kswapd scans the zones in the highmem->normal->dma direction.  It skips
1745  * zones which have free_pages > pages_high, but once a zone is found to have
1746  * free_pages <= pages_high, we scan that zone and the lower zones regardless
1747  * of the number of free pages in the lower zones.  This interoperates with
1748  * the page allocator fallback scheme to ensure that aging of pages is balanced
1749  * across the zones.
1750  */
1751 static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
1752 {
1753         int all_zones_ok;
1754         int priority;
1755         int i;
1756         unsigned long total_scanned;
1757         struct reclaim_state *reclaim_state = current->reclaim_state;
1758         struct scan_control sc = {
1759                 .gfp_mask = GFP_KERNEL,
1760                 .may_unmap = 1,
1761                 .swap_cluster_max = SWAP_CLUSTER_MAX,
1762                 .swappiness = vm_swappiness,
1763                 .order = order,
1764                 .mem_cgroup = NULL,
1765                 .isolate_pages = isolate_pages_global,
1766         };
1767         /*
1768          * temp_priority is used to remember the scanning priority at which
1769          * this zone was successfully refilled to free_pages == pages_high.
1770          */
1771         int temp_priority[MAX_NR_ZONES];
1772
1773 loop_again:
1774         total_scanned = 0;
1775         sc.nr_reclaimed = 0;
1776         sc.may_writepage = !laptop_mode;
1777         count_vm_event(PAGEOUTRUN);
1778
1779         for (i = 0; i < pgdat->nr_zones; i++)
1780                 temp_priority[i] = DEF_PRIORITY;
1781
1782         for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1783                 int end_zone = 0;       /* Inclusive.  0 = ZONE_DMA */
1784                 unsigned long lru_pages = 0;
1785
1786                 /* The swap token gets in the way of swapout... */
1787                 if (!priority)
1788                         disable_swap_token();
1789
1790                 all_zones_ok = 1;
1791
1792                 /*
1793                  * Scan in the highmem->dma direction for the highest
1794                  * zone which needs scanning
1795                  */
1796                 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1797                         struct zone *zone = pgdat->node_zones + i;
1798
1799                         if (!populated_zone(zone))
1800                                 continue;
1801
1802                         if (zone_is_all_unreclaimable(zone) &&
1803                             priority != DEF_PRIORITY)
1804                                 continue;
1805
1806                         /*
1807                          * Do some background aging of the anon list, to give
1808                          * pages a chance to be referenced before reclaiming.
1809                          */
1810                         if (inactive_anon_is_low(zone, &sc))
1811                                 shrink_active_list(SWAP_CLUSTER_MAX, zone,
1812                                                         &sc, priority, 0);
1813
1814                         if (!zone_watermark_ok(zone, order, zone->pages_high,
1815                                                0, 0)) {
1816                                 end_zone = i;
1817                                 break;
1818                         }
1819                 }
1820                 if (i < 0)
1821                         goto out;
1822
1823                 for (i = 0; i <= end_zone; i++) {
1824                         struct zone *zone = pgdat->node_zones + i;
1825
1826                         lru_pages += zone_lru_pages(zone);
1827                 }
1828
1829                 /*
1830                  * Now scan the zone in the dma->highmem direction, stopping
1831                  * at the last zone which needs scanning.
1832                  *
1833                  * We do this because the page allocator works in the opposite
1834                  * direction.  This prevents the page allocator from allocating
1835                  * pages behind kswapd's direction of progress, which would
1836                  * cause too much scanning of the lower zones.
1837                  */
1838                 for (i = 0; i <= end_zone; i++) {
1839                         struct zone *zone = pgdat->node_zones + i;
1840                         int nr_slab;
1841
1842                         if (!populated_zone(zone))
1843                                 continue;
1844
1845                         if (zone_is_all_unreclaimable(zone) &&
1846                                         priority != DEF_PRIORITY)
1847                                 continue;
1848
1849                         if (!zone_watermark_ok(zone, order, zone->pages_high,
1850                                                end_zone, 0))
1851                                 all_zones_ok = 0;
1852                         temp_priority[i] = priority;
1853                         sc.nr_scanned = 0;
1854                         note_zone_scanning_priority(zone, priority);
1855                         /*
1856                          * We put equal pressure on every zone, unless one
1857                          * zone has way too many pages free already.
1858                          */
1859                         if (!zone_watermark_ok(zone, order, 8*zone->pages_high,
1860                                                 end_zone, 0))
1861                                 shrink_zone(priority, zone, &sc);
1862                         reclaim_state->reclaimed_slab = 0;
1863                         nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1864                                                 lru_pages);
1865                         sc.nr_reclaimed += reclaim_state->reclaimed_slab;
1866                         total_scanned += sc.nr_scanned;
1867                         if (zone_is_all_unreclaimable(zone))
1868                                 continue;
1869                         if (nr_slab == 0 && zone->pages_scanned >=
1870                                                 (zone_lru_pages(zone) * 6))
1871                                         zone_set_flag(zone,
1872                                                       ZONE_ALL_UNRECLAIMABLE);
1873                         /*
1874                          * If we've done a decent amount of scanning and
1875                          * the reclaim ratio is low, start doing writepage
1876                          * even in laptop mode
1877                          */
1878                         if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
1879                             total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2)
1880                                 sc.may_writepage = 1;
1881                 }
1882                 if (all_zones_ok)
1883                         break;          /* kswapd: all done */
1884                 /*
1885                  * OK, kswapd is getting into trouble.  Take a nap, then take
1886                  * another pass across the zones.
1887                  */
1888                 if (total_scanned && priority < DEF_PRIORITY - 2)
1889                         congestion_wait(WRITE, HZ/10);
1890
1891                 /*
1892                  * We do this so kswapd doesn't build up large priorities for
1893                  * example when it is freeing in parallel with allocators. It
1894                  * matches the direct reclaim path behaviour in terms of impact
1895                  * on zone->*_priority.
1896                  */
1897                 if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX)
1898                         break;
1899         }
1900 out:
1901         /*
1902          * Note within each zone the priority level at which this zone was
1903          * brought into a happy state.  So that the next thread which scans this
1904          * zone will start out at that priority level.
1905          */
1906         for (i = 0; i < pgdat->nr_zones; i++) {
1907                 struct zone *zone = pgdat->node_zones + i;
1908
1909                 zone->prev_priority = temp_priority[i];
1910         }
1911         if (!all_zones_ok) {
1912                 cond_resched();
1913
1914                 try_to_freeze();
1915
1916                 /*
1917                  * Fragmentation may mean that the system cannot be
1918                  * rebalanced for high-order allocations in all zones.
1919                  * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX,
1920                  * it means the zones have been fully scanned and are still
1921                  * not balanced. For high-order allocations, there is
1922                  * little point trying all over again as kswapd may
1923                  * infinite loop.
1924                  *
1925                  * Instead, recheck all watermarks at order-0 as they
1926                  * are the most important. If watermarks are ok, kswapd will go
1927                  * back to sleep. High-order users can still perform direct
1928                  * reclaim if they wish.
1929                  */
1930                 if (sc.nr_reclaimed < SWAP_CLUSTER_MAX)
1931                         order = sc.order = 0;
1932
1933                 goto loop_again;
1934         }
1935
1936         return sc.nr_reclaimed;
1937 }
1938
1939 /*
1940  * The background pageout daemon, started as a kernel thread
1941  * from the init process.
1942  *
1943  * This basically trickles out pages so that we have _some_
1944  * free memory available even if there is no other activity
1945  * that frees anything up. This is needed for things like routing
1946  * etc, where we otherwise might have all activity going on in
1947  * asynchronous contexts that cannot page things out.
1948  *
1949  * If there are applications that are active memory-allocators
1950  * (most normal use), this basically shouldn't matter.
1951  */
1952 static int kswapd(void *p)
1953 {
1954         unsigned long order;
1955         pg_data_t *pgdat = (pg_data_t*)p;
1956         struct task_struct *tsk = current;
1957         DEFINE_WAIT(wait);
1958         struct reclaim_state reclaim_state = {
1959                 .reclaimed_slab = 0,
1960         };
1961         node_to_cpumask_ptr(cpumask, pgdat->node_id);
1962
1963         lockdep_set_current_reclaim_state(GFP_KERNEL);
1964
1965         if (!cpumask_empty(cpumask))
1966                 set_cpus_allowed_ptr(tsk, cpumask);
1967         current->reclaim_state = &reclaim_state;
1968
1969         /*
1970          * Tell the memory management that we're a "memory allocator",
1971          * and that if we need more memory we should get access to it
1972          * regardless (see "__alloc_pages()"). "kswapd" should
1973          * never get caught in the normal page freeing logic.
1974          *
1975          * (Kswapd normally doesn't need memory anyway, but sometimes
1976          * you need a small amount of memory in order to be able to
1977          * page out something else, and this flag essentially protects
1978          * us from recursively trying to free more memory as we're
1979          * trying to free the first piece of memory in the first place).
1980          */
1981         tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
1982         set_freezable();
1983
1984         order = 0;
1985         for ( ; ; ) {
1986                 unsigned long new_order;
1987
1988                 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1989                 new_order = pgdat->kswapd_max_order;
1990                 pgdat->kswapd_max_order = 0;
1991                 if (order < new_order) {
1992                         /*
1993                          * Don't sleep if someone wants a larger 'order'
1994                          * allocation
1995                          */
1996                         order = new_order;
1997                 } else {
1998                         if (!freezing(current))
1999                                 schedule();
2000
2001                         order = pgdat->kswapd_max_order;
2002                 }
2003                 finish_wait(&pgdat->kswapd_wait, &wait);
2004
2005                 if (!try_to_freeze()) {
2006                         /* We can speed up thawing tasks if we don't call
2007                          * balance_pgdat after returning from the refrigerator
2008                          */
2009                         balance_pgdat(pgdat, order);
2010                 }
2011         }
2012         return 0;
2013 }
2014
2015 /*
2016  * A zone is low on free memory, so wake its kswapd task to service it.
2017  */
2018 void wakeup_kswapd(struct zone *zone, int order)
2019 {
2020         pg_data_t *pgdat;
2021
2022         if (!populated_zone(zone))
2023                 return;
2024
2025         pgdat = zone->zone_pgdat;
2026         if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
2027                 return;
2028         if (pgdat->kswapd_max_order < order)
2029                 pgdat->kswapd_max_order = order;
2030         if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
2031                 return;
2032         if (!waitqueue_active(&pgdat->kswapd_wait))
2033                 return;
2034         wake_up_interruptible(&pgdat->kswapd_wait);
2035 }
2036
2037 unsigned long global_lru_pages(void)
2038 {
2039         return global_page_state(NR_ACTIVE_ANON)
2040                 + global_page_state(NR_ACTIVE_FILE)
2041                 + global_page_state(NR_INACTIVE_ANON)
2042                 + global_page_state(NR_INACTIVE_FILE);
2043 }
2044
2045 #ifdef CONFIG_PM
2046 /*
2047  * Helper function for shrink_all_memory().  Tries to reclaim 'nr_pages' pages
2048  * from LRU lists system-wide, for given pass and priority.
2049  *
2050  * For pass > 3 we also try to shrink the LRU lists that contain a few pages
2051  */
2052 static void shrink_all_zones(unsigned long nr_pages, int prio,
2053                                       int pass, struct scan_control *sc)
2054 {
2055         struct zone *zone;
2056         unsigned long nr_reclaimed = 0;
2057
2058         for_each_populated_zone(zone) {
2059                 enum lru_list l;
2060
2061                 if (zone_is_all_unreclaimable(zone) && prio != DEF_PRIORITY)
2062                         continue;
2063
2064                 for_each_evictable_lru(l) {
2065                         enum zone_stat_item ls = NR_LRU_BASE + l;
2066                         unsigned long lru_pages = zone_page_state(zone, ls);
2067
2068                         /* For pass = 0, we don't shrink the active list */
2069                         if (pass == 0 && (l == LRU_ACTIVE_ANON ||
2070                                                 l == LRU_ACTIVE_FILE))
2071                                 continue;
2072
2073                         zone->lru[l].nr_scan += (lru_pages >> prio) + 1;
2074                         if (zone->lru[l].nr_scan >= nr_pages || pass > 3) {
2075                                 unsigned long nr_to_scan;
2076
2077                                 zone->lru[l].nr_scan = 0;
2078                                 nr_to_scan = min(nr_pages, lru_pages);
2079                                 nr_reclaimed += shrink_list(l, nr_to_scan, zone,
2080                                                                 sc, prio);
2081                                 if (nr_reclaimed >= nr_pages) {
2082                                         sc->nr_reclaimed = nr_reclaimed;
2083                                         return;
2084                                 }
2085                         }
2086                 }
2087         }
2088         sc->nr_reclaimed = nr_reclaimed;
2089 }
2090
2091 /*
2092  * Try to free `nr_pages' of memory, system-wide, and return the number of
2093  * freed pages.
2094  *
2095  * Rather than trying to age LRUs the aim is to preserve the overall
2096  * LRU order by reclaiming preferentially
2097  * inactive > active > active referenced > active mapped
2098  */
2099 unsigned long shrink_all_memory(unsigned long nr_pages)
2100 {
2101         unsigned long lru_pages, nr_slab;
2102         int pass;
2103         struct reclaim_state reclaim_state;
2104         struct scan_control sc = {
2105                 .gfp_mask = GFP_KERNEL,
2106                 .may_unmap = 0,
2107                 .may_writepage = 1,
2108                 .isolate_pages = isolate_pages_global,
2109         };
2110
2111         current->reclaim_state = &reclaim_state;
2112
2113         lru_pages = global_lru_pages();
2114         nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
2115         /* If slab caches are huge, it's better to hit them first */
2116         while (nr_slab >= lru_pages) {
2117                 reclaim_state.reclaimed_slab = 0;
2118                 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
2119                 if (!reclaim_state.reclaimed_slab)
2120                         break;
2121
2122                 sc.nr_reclaimed += reclaim_state.reclaimed_slab;
2123                 if (sc.nr_reclaimed >= nr_pages)
2124                         goto out;
2125
2126                 nr_slab -= reclaim_state.reclaimed_slab;
2127         }
2128
2129         /*
2130          * We try to shrink LRUs in 5 passes:
2131          * 0 = Reclaim from inactive_list only
2132          * 1 = Reclaim from active list but don't reclaim mapped
2133          * 2 = 2nd pass of type 1
2134          * 3 = Reclaim mapped (normal reclaim)
2135          * 4 = 2nd pass of type 3
2136          */
2137         for (pass = 0; pass < 5; pass++) {
2138                 int prio;
2139
2140                 /* Force reclaiming mapped pages in the passes #3 and #4 */
2141                 if (pass > 2)
2142                         sc.may_unmap = 1;
2143
2144                 for (prio = DEF_PRIORITY; prio >= 0; prio--) {
2145                         unsigned long nr_to_scan = nr_pages - sc.nr_reclaimed;
2146
2147                         sc.nr_scanned = 0;
2148                         sc.swap_cluster_max = nr_to_scan;
2149                         shrink_all_zones(nr_to_scan, prio, pass, &sc);
2150                         if (sc.nr_reclaimed >= nr_pages)
2151                                 goto out;
2152
2153                         reclaim_state.reclaimed_slab = 0;
2154                         shrink_slab(sc.nr_scanned, sc.gfp_mask,
2155                                         global_lru_pages());
2156                         sc.nr_reclaimed += reclaim_state.reclaimed_slab;
2157                         if (sc.nr_reclaimed >= nr_pages)
2158                                 goto out;
2159
2160                         if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
2161                                 congestion_wait(WRITE, HZ / 10);
2162                 }
2163         }
2164
2165         /*
2166          * If sc.nr_reclaimed = 0, we could not shrink LRUs, but there may be
2167          * something in slab caches
2168          */
2169         if (!sc.nr_reclaimed) {
2170                 do {
2171                         reclaim_state.reclaimed_slab = 0;
2172                         shrink_slab(nr_pages, sc.gfp_mask, global_lru_pages());
2173                         sc.nr_reclaimed += reclaim_state.reclaimed_slab;
2174                 } while (sc.nr_reclaimed < nr_pages &&
2175                                 reclaim_state.reclaimed_slab > 0);
2176         }
2177
2178
2179 out:
2180         current->reclaim_state = NULL;
2181
2182         return sc.nr_reclaimed;
2183 }
2184 #endif
2185
2186 /* It's optimal to keep kswapds on the same CPUs as their memory, but
2187    not required for correctness.  So if the last cpu in a node goes
2188    away, we get changed to run anywhere: as the first one comes back,
2189    restore their cpu bindings. */
2190 static int __devinit cpu_callback(struct notifier_block *nfb,
2191                                   unsigned long action, void *hcpu)
2192 {
2193         int nid;
2194
2195         if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
2196                 for_each_node_state(nid, N_HIGH_MEMORY) {
2197                         pg_data_t *pgdat = NODE_DATA(nid);
2198                         node_to_cpumask_ptr(mask, pgdat->node_id);
2199
2200                         if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
2201                                 /* One of our CPUs online: restore mask */
2202                                 set_cpus_allowed_ptr(pgdat->kswapd, mask);
2203                 }
2204         }
2205         return NOTIFY_OK;
2206 }
2207
2208 /*
2209  * This kswapd start function will be called by init and node-hot-add.
2210  * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
2211  */
2212 int kswapd_run(int nid)
2213 {
2214         pg_data_t *pgdat = NODE_DATA(nid);
2215         int ret = 0;
2216
2217         if (pgdat->kswapd)
2218                 return 0;
2219
2220         pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
2221         if (IS_ERR(pgdat->kswapd)) {
2222                 /* failure at boot is fatal */
2223                 BUG_ON(system_state == SYSTEM_BOOTING);
2224                 printk("Failed to start kswapd on node %d\n",nid);
2225                 ret = -1;
2226         }
2227         return ret;
2228 }
2229
2230 static int __init kswapd_init(void)
2231 {
2232         int nid;
2233
2234         swap_setup();
2235         for_each_node_state(nid, N_HIGH_MEMORY)
2236                 kswapd_run(nid);
2237         hotcpu_notifier(cpu_callback, 0);
2238         return 0;
2239 }
2240
2241 module_init(kswapd_init)
2242
2243 #ifdef CONFIG_NUMA
2244 /*
2245  * Zone reclaim mode
2246  *
2247  * If non-zero call zone_reclaim when the number of free pages falls below
2248  * the watermarks.
2249  */
2250 int zone_reclaim_mode __read_mostly;
2251
2252 #define RECLAIM_OFF 0
2253 #define RECLAIM_ZONE (1<<0)     /* Run shrink_inactive_list on the zone */
2254 #define RECLAIM_WRITE (1<<1)    /* Writeout pages during reclaim */
2255 #define RECLAIM_SWAP (1<<2)     /* Swap pages out during reclaim */
2256
2257 /*
2258  * Priority for ZONE_RECLAIM. This determines the fraction of pages
2259  * of a node considered for each zone_reclaim. 4 scans 1/16th of
2260  * a zone.
2261  */
2262 #define ZONE_RECLAIM_PRIORITY 4
2263
2264 /*
2265  * Percentage of pages in a zone that must be unmapped for zone_reclaim to
2266  * occur.
2267  */
2268 int sysctl_min_unmapped_ratio = 1;
2269
2270 /*
2271  * If the number of slab pages in a zone grows beyond this percentage then
2272  * slab reclaim needs to occur.
2273  */
2274 int sysctl_min_slab_ratio = 5;
2275
2276 /*
2277  * Try to free up some pages from this zone through reclaim.
2278  */
2279 static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
2280 {
2281         /* Minimum pages needed in order to stay on node */
2282         const unsigned long nr_pages = 1 << order;
2283         struct task_struct *p = current;
2284         struct reclaim_state reclaim_state;
2285         int priority;
2286         struct scan_control sc = {
2287                 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
2288                 .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP),
2289                 .swap_cluster_max = max_t(unsigned long, nr_pages,
2290                                         SWAP_CLUSTER_MAX),
2291                 .gfp_mask = gfp_mask,
2292                 .swappiness = vm_swappiness,
2293                 .order = order,
2294                 .isolate_pages = isolate_pages_global,
2295         };
2296         unsigned long slab_reclaimable;
2297
2298         disable_swap_token();
2299         cond_resched();
2300         /*
2301          * We need to be able to allocate from the reserves for RECLAIM_SWAP
2302          * and we also need to be able to write out pages for RECLAIM_WRITE
2303          * and RECLAIM_SWAP.
2304          */
2305         p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
2306         reclaim_state.reclaimed_slab = 0;
2307         p->reclaim_state = &reclaim_state;
2308
2309         if (zone_page_state(zone, NR_FILE_PAGES) -
2310                 zone_page_state(zone, NR_FILE_MAPPED) >
2311                 zone->min_unmapped_pages) {
2312                 /*
2313                  * Free memory by calling shrink zone with increasing
2314                  * priorities until we have enough memory freed.
2315                  */
2316                 priority = ZONE_RECLAIM_PRIORITY;
2317                 do {
2318                         note_zone_scanning_priority(zone, priority);
2319                         shrink_zone(priority, zone, &sc);
2320                         priority--;
2321                 } while (priority >= 0 && sc.nr_reclaimed < nr_pages);
2322         }
2323
2324         slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
2325         if (slab_reclaimable > zone->min_slab_pages) {
2326                 /*
2327                  * shrink_slab() does not currently allow us to determine how
2328                  * many pages were freed in this zone. So we take the current
2329                  * number of slab pages and shake the slab until it is reduced
2330                  * by the same nr_pages that we used for reclaiming unmapped
2331                  * pages.
2332                  *
2333                  * Note that shrink_slab will free memory on all zones and may
2334                  * take a long time.
2335                  */
2336                 while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
2337                         zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
2338                                 slab_reclaimable - nr_pages)
2339                         ;
2340
2341                 /*
2342                  * Update nr_reclaimed by the number of slab pages we
2343                  * reclaimed from this zone.
2344                  */
2345                 sc.nr_reclaimed += slab_reclaimable -
2346                         zone_page_state(zone, NR_SLAB_RECLAIMABLE);
2347         }
2348
2349         p->reclaim_state = NULL;
2350         current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
2351         return sc.nr_reclaimed >= nr_pages;
2352 }
2353
2354 int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
2355 {
2356         int node_id;
2357         int ret;
2358
2359         /*
2360          * Zone reclaim reclaims unmapped file backed pages and
2361          * slab pages if we are over the defined limits.
2362          *
2363          * A small portion of unmapped file backed pages is needed for
2364          * file I/O otherwise pages read by file I/O will be immediately
2365          * thrown out if the zone is overallocated. So we do not reclaim
2366          * if less than a specified percentage of the zone is used by
2367          * unmapped file backed pages.
2368          */
2369         if (zone_page_state(zone, NR_FILE_PAGES) -
2370             zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
2371             && zone_page_state(zone, NR_SLAB_RECLAIMABLE)
2372                         <= zone->min_slab_pages)
2373                 return 0;
2374
2375         if (zone_is_all_unreclaimable(zone))
2376                 return 0;
2377
2378         /*
2379          * Do not scan if the allocation should not be delayed.
2380          */
2381         if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
2382                         return 0;
2383
2384         /*
2385          * Only run zone reclaim on the local zone or on zones that do not
2386          * have associated processors. This will favor the local processor
2387          * over remote processors and spread off node memory allocations
2388          * as wide as possible.
2389          */
2390         node_id = zone_to_nid(zone);
2391         if (node_state(node_id, N_CPU) && node_id != numa_node_id())
2392                 return 0;
2393
2394         if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
2395                 return 0;
2396         ret = __zone_reclaim(zone, gfp_mask, order);
2397         zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
2398
2399         return ret;
2400 }
2401 #endif
2402
2403 #ifdef CONFIG_UNEVICTABLE_LRU
2404 /*
2405  * page_evictable - test whether a page is evictable
2406  * @page: the page to test
2407  * @vma: the VMA in which the page is or will be mapped, may be NULL
2408  *
2409  * Test whether page is evictable--i.e., should be placed on active/inactive
2410  * lists vs unevictable list.  The vma argument is !NULL when called from the
2411  * fault path to determine how to instantate a new page.
2412  *
2413  * Reasons page might not be evictable:
2414  * (1) page's mapping marked unevictable
2415  * (2) page is part of an mlocked VMA
2416  *
2417  */
2418 int page_evictable(struct page *page, struct vm_area_struct *vma)
2419 {
2420
2421         if (mapping_unevictable(page_mapping(page)))
2422                 return 0;
2423
2424         if (PageMlocked(page) || (vma && is_mlocked_vma(vma, page)))
2425                 return 0;
2426
2427         return 1;
2428 }
2429
2430 /**
2431  * check_move_unevictable_page - check page for evictability and move to appropriate zone lru list
2432  * @page: page to check evictability and move to appropriate lru list
2433  * @zone: zone page is in
2434  *
2435  * Checks a page for evictability and moves the page to the appropriate
2436  * zone lru list.
2437  *
2438  * Restrictions: zone->lru_lock must be held, page must be on LRU and must
2439  * have PageUnevictable set.
2440  */
2441 static void check_move_unevictable_page(struct page *page, struct zone *zone)
2442 {
2443         VM_BUG_ON(PageActive(page));
2444
2445 retry:
2446         ClearPageUnevictable(page);
2447         if (page_evictable(page, NULL)) {
2448                 enum lru_list l = LRU_INACTIVE_ANON + page_is_file_cache(page);
2449
2450                 __dec_zone_state(zone, NR_UNEVICTABLE);
2451                 list_move(&page->lru, &zone->lru[l].list);
2452                 mem_cgroup_move_lists(page, LRU_UNEVICTABLE, l);
2453                 __inc_zone_state(zone, NR_INACTIVE_ANON + l);
2454                 __count_vm_event(UNEVICTABLE_PGRESCUED);
2455         } else {
2456                 /*
2457                  * rotate unevictable list
2458                  */
2459                 SetPageUnevictable(page);
2460                 list_move(&page->lru, &zone->lru[LRU_UNEVICTABLE].list);
2461                 mem_cgroup_rotate_lru_list(page, LRU_UNEVICTABLE);
2462                 if (page_evictable(page, NULL))
2463                         goto retry;
2464         }
2465 }
2466
2467 /**
2468  * scan_mapping_unevictable_pages - scan an address space for evictable pages
2469  * @mapping: struct address_space to scan for evictable pages
2470  *
2471  * Scan all pages in mapping.  Check unevictable pages for
2472  * evictability and move them to the appropriate zone lru list.
2473  */
2474 void scan_mapping_unevictable_pages(struct address_space *mapping)
2475 {
2476         pgoff_t next = 0;
2477         pgoff_t end   = (i_size_read(mapping->host) + PAGE_CACHE_SIZE - 1) >>
2478                          PAGE_CACHE_SHIFT;
2479         struct zone *zone;
2480         struct pagevec pvec;
2481
2482         if (mapping->nrpages == 0)
2483                 return;
2484
2485         pagevec_init(&pvec, 0);
2486         while (next < end &&
2487                 pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
2488                 int i;
2489                 int pg_scanned = 0;
2490
2491                 zone = NULL;
2492
2493                 for (i = 0; i < pagevec_count(&pvec); i++) {
2494                         struct page *page = pvec.pages[i];
2495                         pgoff_t page_index = page->index;
2496                         struct zone *pagezone = page_zone(page);
2497
2498                         pg_scanned++;
2499                         if (page_index > next)
2500                                 next = page_index;
2501                         next++;
2502
2503                         if (pagezone != zone) {
2504                                 if (zone)
2505                                         spin_unlock_irq(&zone->lru_lock);
2506                                 zone = pagezone;
2507                                 spin_lock_irq(&zone->lru_lock);
2508                         }
2509
2510                         if (PageLRU(page) && PageUnevictable(page))
2511                                 check_move_unevictable_page(page, zone);
2512                 }
2513                 if (zone)
2514                         spin_unlock_irq(&zone->lru_lock);
2515                 pagevec_release(&pvec);
2516
2517                 count_vm_events(UNEVICTABLE_PGSCANNED, pg_scanned);
2518         }
2519
2520 }
2521
2522 /**
2523  * scan_zone_unevictable_pages - check unevictable list for evictable pages
2524  * @zone - zone of which to scan the unevictable list
2525  *
2526  * Scan @zone's unevictable LRU lists to check for pages that have become
2527  * evictable.  Move those that have to @zone's inactive list where they
2528  * become candidates for reclaim, unless shrink_inactive_zone() decides
2529  * to reactivate them.  Pages that are still unevictable are rotated
2530  * back onto @zone's unevictable list.
2531  */
2532 #define SCAN_UNEVICTABLE_BATCH_SIZE 16UL /* arbitrary lock hold batch size */
2533 static void scan_zone_unevictable_pages(struct zone *zone)
2534 {
2535         struct list_head *l_unevictable = &zone->lru[LRU_UNEVICTABLE].list;
2536         unsigned long scan;
2537         unsigned long nr_to_scan = zone_page_state(zone, NR_UNEVICTABLE);
2538
2539         while (nr_to_scan > 0) {
2540                 unsigned long batch_size = min(nr_to_scan,
2541                                                 SCAN_UNEVICTABLE_BATCH_SIZE);
2542
2543                 spin_lock_irq(&zone->lru_lock);
2544                 for (scan = 0;  scan < batch_size; scan++) {
2545                         struct page *page = lru_to_page(l_unevictable);
2546
2547                         if (!trylock_page(page))
2548                                 continue;
2549
2550                         prefetchw_prev_lru_page(page, l_unevictable, flags);
2551
2552                         if (likely(PageLRU(page) && PageUnevictable(page)))
2553                                 check_move_unevictable_page(page, zone);
2554
2555                         unlock_page(page);
2556                 }
2557                 spin_unlock_irq(&zone->lru_lock);
2558
2559                 nr_to_scan -= batch_size;
2560         }
2561 }
2562
2563
2564 /**
2565  * scan_all_zones_unevictable_pages - scan all unevictable lists for evictable pages
2566  *
2567  * A really big hammer:  scan all zones' unevictable LRU lists to check for
2568  * pages that have become evictable.  Move those back to the zones'
2569  * inactive list where they become candidates for reclaim.
2570  * This occurs when, e.g., we have unswappable pages on the unevictable lists,
2571  * and we add swap to the system.  As such, it runs in the context of a task
2572  * that has possibly/probably made some previously unevictable pages
2573  * evictable.
2574  */
2575 static void scan_all_zones_unevictable_pages(void)
2576 {
2577         struct zone *zone;
2578
2579         for_each_zone(zone) {
2580                 scan_zone_unevictable_pages(zone);
2581         }
2582 }
2583
2584 /*
2585  * scan_unevictable_pages [vm] sysctl handler.  On demand re-scan of
2586  * all nodes' unevictable lists for evictable pages
2587  */
2588 unsigned long scan_unevictable_pages;
2589
2590 int scan_unevictable_handler(struct ctl_table *table, int write,
2591                            struct file *file, void __user *buffer,
2592                            size_t *length, loff_t *ppos)
2593 {
2594         proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
2595
2596         if (write && *(unsigned long *)table->data)
2597                 scan_all_zones_unevictable_pages();
2598
2599         scan_unevictable_pages = 0;
2600         return 0;
2601 }
2602
2603 /*
2604  * per node 'scan_unevictable_pages' attribute.  On demand re-scan of
2605  * a specified node's per zone unevictable lists for evictable pages.
2606  */
2607
2608 static ssize_t read_scan_unevictable_node(struct sys_device *dev,
2609                                           struct sysdev_attribute *attr,
2610                                           char *buf)
2611 {
2612         return sprintf(buf, "0\n");     /* always zero; should fit... */
2613 }
2614
2615 static ssize_t write_scan_unevictable_node(struct sys_device *dev,
2616                                            struct sysdev_attribute *attr,
2617                                         const char *buf, size_t count)
2618 {
2619         struct zone *node_zones = NODE_DATA(dev->id)->node_zones;
2620         struct zone *zone;
2621         unsigned long res;
2622         unsigned long req = strict_strtoul(buf, 10, &res);
2623
2624         if (!req)
2625                 return 1;       /* zero is no-op */
2626
2627         for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
2628                 if (!populated_zone(zone))
2629                         continue;
2630                 scan_zone_unevictable_pages(zone);
2631         }
2632         return 1;
2633 }
2634
2635
2636 static SYSDEV_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR,
2637                         read_scan_unevictable_node,
2638                         write_scan_unevictable_node);
2639
2640 int scan_unevictable_register_node(struct node *node)
2641 {
2642         return sysdev_create_file(&node->sysdev, &attr_scan_unevictable_pages);
2643 }
2644
2645 void scan_unevictable_unregister_node(struct node *node)
2646 {
2647         sysdev_remove_file(&node->sysdev, &attr_scan_unevictable_pages);
2648 }
2649
2650 #endif