memory cgroup enhancements: fix zone handling in try_to_free_mem_cgroup_page
[safe/jmp/linux-2.6] / 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
42 #include <asm/tlbflush.h>
43 #include <asm/div64.h>
44
45 #include <linux/swapops.h>
46
47 #include "internal.h"
48
49 struct scan_control {
50         /* Incremented by the number of inactive pages that were scanned */
51         unsigned long nr_scanned;
52
53         /* This context's GFP mask */
54         gfp_t gfp_mask;
55
56         int may_writepage;
57
58         /* Can pages be swapped as part of reclaim? */
59         int may_swap;
60
61         /* This context's SWAP_CLUSTER_MAX. If freeing memory for
62          * suspend, we effectively ignore SWAP_CLUSTER_MAX.
63          * In this context, it doesn't matter that we scan the
64          * whole list at once. */
65         int swap_cluster_max;
66
67         int swappiness;
68
69         int all_unreclaimable;
70
71         int order;
72
73         /*
74          * Pages that have (or should have) IO pending.  If we run into
75          * a lot of these, we're better off waiting a little for IO to
76          * finish rather than scanning more pages in the VM.
77          */
78         int nr_io_pages;
79
80         /* Which cgroup do we reclaim from */
81         struct mem_cgroup *mem_cgroup;
82
83         /* Pluggable isolate pages callback */
84         unsigned long (*isolate_pages)(unsigned long nr, struct list_head *dst,
85                         unsigned long *scanned, int order, int mode,
86                         struct zone *z, struct mem_cgroup *mem_cont,
87                         int active);
88 };
89
90 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
91
92 #ifdef ARCH_HAS_PREFETCH
93 #define prefetch_prev_lru_page(_page, _base, _field)                    \
94         do {                                                            \
95                 if ((_page)->lru.prev != _base) {                       \
96                         struct page *prev;                              \
97                                                                         \
98                         prev = lru_to_page(&(_page->lru));              \
99                         prefetch(&prev->_field);                        \
100                 }                                                       \
101         } while (0)
102 #else
103 #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
104 #endif
105
106 #ifdef ARCH_HAS_PREFETCHW
107 #define prefetchw_prev_lru_page(_page, _base, _field)                   \
108         do {                                                            \
109                 if ((_page)->lru.prev != _base) {                       \
110                         struct page *prev;                              \
111                                                                         \
112                         prev = lru_to_page(&(_page->lru));              \
113                         prefetchw(&prev->_field);                       \
114                 }                                                       \
115         } while (0)
116 #else
117 #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
118 #endif
119
120 /*
121  * From 0 .. 100.  Higher means more swappy.
122  */
123 int vm_swappiness = 60;
124 long vm_total_pages;    /* The total number of pages which the VM controls */
125
126 static LIST_HEAD(shrinker_list);
127 static DECLARE_RWSEM(shrinker_rwsem);
128
129 /*
130  * Add a shrinker callback to be called from the vm
131  */
132 void register_shrinker(struct shrinker *shrinker)
133 {
134         shrinker->nr = 0;
135         down_write(&shrinker_rwsem);
136         list_add_tail(&shrinker->list, &shrinker_list);
137         up_write(&shrinker_rwsem);
138 }
139 EXPORT_SYMBOL(register_shrinker);
140
141 /*
142  * Remove one
143  */
144 void unregister_shrinker(struct shrinker *shrinker)
145 {
146         down_write(&shrinker_rwsem);
147         list_del(&shrinker->list);
148         up_write(&shrinker_rwsem);
149 }
150 EXPORT_SYMBOL(unregister_shrinker);
151
152 #define SHRINK_BATCH 128
153 /*
154  * Call the shrink functions to age shrinkable caches
155  *
156  * Here we assume it costs one seek to replace a lru page and that it also
157  * takes a seek to recreate a cache object.  With this in mind we age equal
158  * percentages of the lru and ageable caches.  This should balance the seeks
159  * generated by these structures.
160  *
161  * If the vm encountered mapped pages on the LRU it increase the pressure on
162  * slab to avoid swapping.
163  *
164  * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
165  *
166  * `lru_pages' represents the number of on-LRU pages in all the zones which
167  * are eligible for the caller's allocation attempt.  It is used for balancing
168  * slab reclaim versus page reclaim.
169  *
170  * Returns the number of slab objects which we shrunk.
171  */
172 unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
173                         unsigned long lru_pages)
174 {
175         struct shrinker *shrinker;
176         unsigned long ret = 0;
177
178         if (scanned == 0)
179                 scanned = SWAP_CLUSTER_MAX;
180
181         if (!down_read_trylock(&shrinker_rwsem))
182                 return 1;       /* Assume we'll be able to shrink next time */
183
184         list_for_each_entry(shrinker, &shrinker_list, list) {
185                 unsigned long long delta;
186                 unsigned long total_scan;
187                 unsigned long max_pass = (*shrinker->shrink)(0, gfp_mask);
188
189                 delta = (4 * scanned) / shrinker->seeks;
190                 delta *= max_pass;
191                 do_div(delta, lru_pages + 1);
192                 shrinker->nr += delta;
193                 if (shrinker->nr < 0) {
194                         printk(KERN_ERR "%s: nr=%ld\n",
195                                         __FUNCTION__, shrinker->nr);
196                         shrinker->nr = max_pass;
197                 }
198
199                 /*
200                  * Avoid risking looping forever due to too large nr value:
201                  * never try to free more than twice the estimate number of
202                  * freeable entries.
203                  */
204                 if (shrinker->nr > max_pass * 2)
205                         shrinker->nr = max_pass * 2;
206
207                 total_scan = shrinker->nr;
208                 shrinker->nr = 0;
209
210                 while (total_scan >= SHRINK_BATCH) {
211                         long this_scan = SHRINK_BATCH;
212                         int shrink_ret;
213                         int nr_before;
214
215                         nr_before = (*shrinker->shrink)(0, gfp_mask);
216                         shrink_ret = (*shrinker->shrink)(this_scan, gfp_mask);
217                         if (shrink_ret == -1)
218                                 break;
219                         if (shrink_ret < nr_before)
220                                 ret += nr_before - shrink_ret;
221                         count_vm_events(SLABS_SCANNED, this_scan);
222                         total_scan -= this_scan;
223
224                         cond_resched();
225                 }
226
227                 shrinker->nr += total_scan;
228         }
229         up_read(&shrinker_rwsem);
230         return ret;
231 }
232
233 /* Called without lock on whether page is mapped, so answer is unstable */
234 static inline int page_mapping_inuse(struct page *page)
235 {
236         struct address_space *mapping;
237
238         /* Page is in somebody's page tables. */
239         if (page_mapped(page))
240                 return 1;
241
242         /* Be more reluctant to reclaim swapcache than pagecache */
243         if (PageSwapCache(page))
244                 return 1;
245
246         mapping = page_mapping(page);
247         if (!mapping)
248                 return 0;
249
250         /* File is mmap'd by somebody? */
251         return mapping_mapped(mapping);
252 }
253
254 static inline int is_page_cache_freeable(struct page *page)
255 {
256         return page_count(page) - !!PagePrivate(page) == 2;
257 }
258
259 static int may_write_to_queue(struct backing_dev_info *bdi)
260 {
261         if (current->flags & PF_SWAPWRITE)
262                 return 1;
263         if (!bdi_write_congested(bdi))
264                 return 1;
265         if (bdi == current->backing_dev_info)
266                 return 1;
267         return 0;
268 }
269
270 /*
271  * We detected a synchronous write error writing a page out.  Probably
272  * -ENOSPC.  We need to propagate that into the address_space for a subsequent
273  * fsync(), msync() or close().
274  *
275  * The tricky part is that after writepage we cannot touch the mapping: nothing
276  * prevents it from being freed up.  But we have a ref on the page and once
277  * that page is locked, the mapping is pinned.
278  *
279  * We're allowed to run sleeping lock_page() here because we know the caller has
280  * __GFP_FS.
281  */
282 static void handle_write_error(struct address_space *mapping,
283                                 struct page *page, int error)
284 {
285         lock_page(page);
286         if (page_mapping(page) == mapping)
287                 mapping_set_error(mapping, error);
288         unlock_page(page);
289 }
290
291 /* Request for sync pageout. */
292 enum pageout_io {
293         PAGEOUT_IO_ASYNC,
294         PAGEOUT_IO_SYNC,
295 };
296
297 /* possible outcome of pageout() */
298 typedef enum {
299         /* failed to write page out, page is locked */
300         PAGE_KEEP,
301         /* move page to the active list, page is locked */
302         PAGE_ACTIVATE,
303         /* page has been sent to the disk successfully, page is unlocked */
304         PAGE_SUCCESS,
305         /* page is clean and locked */
306         PAGE_CLEAN,
307 } pageout_t;
308
309 /*
310  * pageout is called by shrink_page_list() for each dirty page.
311  * Calls ->writepage().
312  */
313 static pageout_t pageout(struct page *page, struct address_space *mapping,
314                                                 enum pageout_io sync_writeback)
315 {
316         /*
317          * If the page is dirty, only perform writeback if that write
318          * will be non-blocking.  To prevent this allocation from being
319          * stalled by pagecache activity.  But note that there may be
320          * stalls if we need to run get_block().  We could test
321          * PagePrivate for that.
322          *
323          * If this process is currently in generic_file_write() against
324          * this page's queue, we can perform writeback even if that
325          * will block.
326          *
327          * If the page is swapcache, write it back even if that would
328          * block, for some throttling. This happens by accident, because
329          * swap_backing_dev_info is bust: it doesn't reflect the
330          * congestion state of the swapdevs.  Easy to fix, if needed.
331          * See swapfile.c:page_queue_congested().
332          */
333         if (!is_page_cache_freeable(page))
334                 return PAGE_KEEP;
335         if (!mapping) {
336                 /*
337                  * Some data journaling orphaned pages can have
338                  * page->mapping == NULL while being dirty with clean buffers.
339                  */
340                 if (PagePrivate(page)) {
341                         if (try_to_free_buffers(page)) {
342                                 ClearPageDirty(page);
343                                 printk("%s: orphaned page\n", __FUNCTION__);
344                                 return PAGE_CLEAN;
345                         }
346                 }
347                 return PAGE_KEEP;
348         }
349         if (mapping->a_ops->writepage == NULL)
350                 return PAGE_ACTIVATE;
351         if (!may_write_to_queue(mapping->backing_dev_info))
352                 return PAGE_KEEP;
353
354         if (clear_page_dirty_for_io(page)) {
355                 int res;
356                 struct writeback_control wbc = {
357                         .sync_mode = WB_SYNC_NONE,
358                         .nr_to_write = SWAP_CLUSTER_MAX,
359                         .range_start = 0,
360                         .range_end = LLONG_MAX,
361                         .nonblocking = 1,
362                         .for_reclaim = 1,
363                 };
364
365                 SetPageReclaim(page);
366                 res = mapping->a_ops->writepage(page, &wbc);
367                 if (res < 0)
368                         handle_write_error(mapping, page, res);
369                 if (res == AOP_WRITEPAGE_ACTIVATE) {
370                         ClearPageReclaim(page);
371                         return PAGE_ACTIVATE;
372                 }
373
374                 /*
375                  * Wait on writeback if requested to. This happens when
376                  * direct reclaiming a large contiguous area and the
377                  * first attempt to free a range of pages fails.
378                  */
379                 if (PageWriteback(page) && sync_writeback == PAGEOUT_IO_SYNC)
380                         wait_on_page_writeback(page);
381
382                 if (!PageWriteback(page)) {
383                         /* synchronous write or broken a_ops? */
384                         ClearPageReclaim(page);
385                 }
386                 inc_zone_page_state(page, NR_VMSCAN_WRITE);
387                 return PAGE_SUCCESS;
388         }
389
390         return PAGE_CLEAN;
391 }
392
393 /*
394  * Attempt to detach a locked page from its ->mapping.  If it is dirty or if
395  * someone else has a ref on the page, abort and return 0.  If it was
396  * successfully detached, return 1.  Assumes the caller has a single ref on
397  * this page.
398  */
399 int remove_mapping(struct address_space *mapping, struct page *page)
400 {
401         BUG_ON(!PageLocked(page));
402         BUG_ON(mapping != page_mapping(page));
403
404         write_lock_irq(&mapping->tree_lock);
405         /*
406          * The non racy check for a busy page.
407          *
408          * Must be careful with the order of the tests. When someone has
409          * a ref to the page, it may be possible that they dirty it then
410          * drop the reference. So if PageDirty is tested before page_count
411          * here, then the following race may occur:
412          *
413          * get_user_pages(&page);
414          * [user mapping goes away]
415          * write_to(page);
416          *                              !PageDirty(page)    [good]
417          * SetPageDirty(page);
418          * put_page(page);
419          *                              !page_count(page)   [good, discard it]
420          *
421          * [oops, our write_to data is lost]
422          *
423          * Reversing the order of the tests ensures such a situation cannot
424          * escape unnoticed. The smp_rmb is needed to ensure the page->flags
425          * load is not satisfied before that of page->_count.
426          *
427          * Note that if SetPageDirty is always performed via set_page_dirty,
428          * and thus under tree_lock, then this ordering is not required.
429          */
430         if (unlikely(page_count(page) != 2))
431                 goto cannot_free;
432         smp_rmb();
433         if (unlikely(PageDirty(page)))
434                 goto cannot_free;
435
436         if (PageSwapCache(page)) {
437                 swp_entry_t swap = { .val = page_private(page) };
438                 __delete_from_swap_cache(page);
439                 write_unlock_irq(&mapping->tree_lock);
440                 swap_free(swap);
441                 __put_page(page);       /* The pagecache ref */
442                 return 1;
443         }
444
445         __remove_from_page_cache(page);
446         write_unlock_irq(&mapping->tree_lock);
447         __put_page(page);
448         return 1;
449
450 cannot_free:
451         write_unlock_irq(&mapping->tree_lock);
452         return 0;
453 }
454
455 /*
456  * shrink_page_list() returns the number of reclaimed pages
457  */
458 static unsigned long shrink_page_list(struct list_head *page_list,
459                                         struct scan_control *sc,
460                                         enum pageout_io sync_writeback)
461 {
462         LIST_HEAD(ret_pages);
463         struct pagevec freed_pvec;
464         int pgactivate = 0;
465         unsigned long nr_reclaimed = 0;
466
467         cond_resched();
468
469         pagevec_init(&freed_pvec, 1);
470         while (!list_empty(page_list)) {
471                 struct address_space *mapping;
472                 struct page *page;
473                 int may_enter_fs;
474                 int referenced;
475
476                 cond_resched();
477
478                 page = lru_to_page(page_list);
479                 list_del(&page->lru);
480
481                 if (TestSetPageLocked(page))
482                         goto keep;
483
484                 VM_BUG_ON(PageActive(page));
485
486                 sc->nr_scanned++;
487
488                 if (!sc->may_swap && page_mapped(page))
489                         goto keep_locked;
490
491                 /* Double the slab pressure for mapped and swapcache pages */
492                 if (page_mapped(page) || PageSwapCache(page))
493                         sc->nr_scanned++;
494
495                 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
496                         (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
497
498                 if (PageWriteback(page)) {
499                         /*
500                          * Synchronous reclaim is performed in two passes,
501                          * first an asynchronous pass over the list to
502                          * start parallel writeback, and a second synchronous
503                          * pass to wait for the IO to complete.  Wait here
504                          * for any page for which writeback has already
505                          * started.
506                          */
507                         if (sync_writeback == PAGEOUT_IO_SYNC && may_enter_fs)
508                                 wait_on_page_writeback(page);
509                         else {
510                                 sc->nr_io_pages++;
511                                 goto keep_locked;
512                         }
513                 }
514
515                 referenced = page_referenced(page, 1, sc->mem_cgroup);
516                 /* In active use or really unfreeable?  Activate it. */
517                 if (sc->order <= PAGE_ALLOC_COSTLY_ORDER &&
518                                         referenced && page_mapping_inuse(page))
519                         goto activate_locked;
520
521 #ifdef CONFIG_SWAP
522                 /*
523                  * Anonymous process memory has backing store?
524                  * Try to allocate it some swap space here.
525                  */
526                 if (PageAnon(page) && !PageSwapCache(page))
527                         if (!add_to_swap(page, GFP_ATOMIC))
528                                 goto activate_locked;
529 #endif /* CONFIG_SWAP */
530
531                 mapping = page_mapping(page);
532
533                 /*
534                  * The page is mapped into the page tables of one or more
535                  * processes. Try to unmap it here.
536                  */
537                 if (page_mapped(page) && mapping) {
538                         switch (try_to_unmap(page, 0)) {
539                         case SWAP_FAIL:
540                                 goto activate_locked;
541                         case SWAP_AGAIN:
542                                 goto keep_locked;
543                         case SWAP_SUCCESS:
544                                 ; /* try to free the page below */
545                         }
546                 }
547
548                 if (PageDirty(page)) {
549                         if (sc->order <= PAGE_ALLOC_COSTLY_ORDER && referenced)
550                                 goto keep_locked;
551                         if (!may_enter_fs) {
552                                 sc->nr_io_pages++;
553                                 goto keep_locked;
554                         }
555                         if (!sc->may_writepage)
556                                 goto keep_locked;
557
558                         /* Page is dirty, try to write it out here */
559                         switch (pageout(page, mapping, sync_writeback)) {
560                         case PAGE_KEEP:
561                                 goto keep_locked;
562                         case PAGE_ACTIVATE:
563                                 goto activate_locked;
564                         case PAGE_SUCCESS:
565                                 if (PageWriteback(page) || PageDirty(page)) {
566                                         sc->nr_io_pages++;
567                                         goto keep;
568                                 }
569                                 /*
570                                  * A synchronous write - probably a ramdisk.  Go
571                                  * ahead and try to reclaim the page.
572                                  */
573                                 if (TestSetPageLocked(page))
574                                         goto keep;
575                                 if (PageDirty(page) || PageWriteback(page))
576                                         goto keep_locked;
577                                 mapping = page_mapping(page);
578                         case PAGE_CLEAN:
579                                 ; /* try to free the page below */
580                         }
581                 }
582
583                 /*
584                  * If the page has buffers, try to free the buffer mappings
585                  * associated with this page. If we succeed we try to free
586                  * the page as well.
587                  *
588                  * We do this even if the page is PageDirty().
589                  * try_to_release_page() does not perform I/O, but it is
590                  * possible for a page to have PageDirty set, but it is actually
591                  * clean (all its buffers are clean).  This happens if the
592                  * buffers were written out directly, with submit_bh(). ext3
593                  * will do this, as well as the blockdev mapping. 
594                  * try_to_release_page() will discover that cleanness and will
595                  * drop the buffers and mark the page clean - it can be freed.
596                  *
597                  * Rarely, pages can have buffers and no ->mapping.  These are
598                  * the pages which were not successfully invalidated in
599                  * truncate_complete_page().  We try to drop those buffers here
600                  * and if that worked, and the page is no longer mapped into
601                  * process address space (page_count == 1) it can be freed.
602                  * Otherwise, leave the page on the LRU so it is swappable.
603                  */
604                 if (PagePrivate(page)) {
605                         if (!try_to_release_page(page, sc->gfp_mask))
606                                 goto activate_locked;
607                         if (!mapping && page_count(page) == 1)
608                                 goto free_it;
609                 }
610
611                 if (!mapping || !remove_mapping(mapping, page))
612                         goto keep_locked;
613
614 free_it:
615                 unlock_page(page);
616                 nr_reclaimed++;
617                 if (!pagevec_add(&freed_pvec, page))
618                         __pagevec_release_nonlru(&freed_pvec);
619                 continue;
620
621 activate_locked:
622                 SetPageActive(page);
623                 pgactivate++;
624 keep_locked:
625                 unlock_page(page);
626 keep:
627                 list_add(&page->lru, &ret_pages);
628                 VM_BUG_ON(PageLRU(page));
629         }
630         list_splice(&ret_pages, page_list);
631         if (pagevec_count(&freed_pvec))
632                 __pagevec_release_nonlru(&freed_pvec);
633         count_vm_events(PGACTIVATE, pgactivate);
634         return nr_reclaimed;
635 }
636
637 /* LRU Isolation modes. */
638 #define ISOLATE_INACTIVE 0      /* Isolate inactive pages. */
639 #define ISOLATE_ACTIVE 1        /* Isolate active pages. */
640 #define ISOLATE_BOTH 2          /* Isolate both active and inactive pages. */
641
642 /*
643  * Attempt to remove the specified page from its LRU.  Only take this page
644  * if it is of the appropriate PageActive status.  Pages which are being
645  * freed elsewhere are also ignored.
646  *
647  * page:        page to consider
648  * mode:        one of the LRU isolation modes defined above
649  *
650  * returns 0 on success, -ve errno on failure.
651  */
652 int __isolate_lru_page(struct page *page, int mode)
653 {
654         int ret = -EINVAL;
655
656         /* Only take pages on the LRU. */
657         if (!PageLRU(page))
658                 return ret;
659
660         /*
661          * When checking the active state, we need to be sure we are
662          * dealing with comparible boolean values.  Take the logical not
663          * of each.
664          */
665         if (mode != ISOLATE_BOTH && (!PageActive(page) != !mode))
666                 return ret;
667
668         ret = -EBUSY;
669         if (likely(get_page_unless_zero(page))) {
670                 /*
671                  * Be careful not to clear PageLRU until after we're
672                  * sure the page is not being freed elsewhere -- the
673                  * page release code relies on it.
674                  */
675                 ClearPageLRU(page);
676                 ret = 0;
677         }
678
679         return ret;
680 }
681
682 /*
683  * zone->lru_lock is heavily contended.  Some of the functions that
684  * shrink the lists perform better by taking out a batch of pages
685  * and working on them outside the LRU lock.
686  *
687  * For pagecache intensive workloads, this function is the hottest
688  * spot in the kernel (apart from copy_*_user functions).
689  *
690  * Appropriate locks must be held before calling this function.
691  *
692  * @nr_to_scan: The number of pages to look through on the list.
693  * @src:        The LRU list to pull pages off.
694  * @dst:        The temp list to put pages on to.
695  * @scanned:    The number of pages that were scanned.
696  * @order:      The caller's attempted allocation order
697  * @mode:       One of the LRU isolation modes
698  *
699  * returns how many pages were moved onto *@dst.
700  */
701 static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
702                 struct list_head *src, struct list_head *dst,
703                 unsigned long *scanned, int order, int mode)
704 {
705         unsigned long nr_taken = 0;
706         unsigned long scan;
707
708         for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
709                 struct page *page;
710                 unsigned long pfn;
711                 unsigned long end_pfn;
712                 unsigned long page_pfn;
713                 int zone_id;
714
715                 page = lru_to_page(src);
716                 prefetchw_prev_lru_page(page, src, flags);
717
718                 VM_BUG_ON(!PageLRU(page));
719
720                 switch (__isolate_lru_page(page, mode)) {
721                 case 0:
722                         list_move(&page->lru, dst);
723                         nr_taken++;
724                         break;
725
726                 case -EBUSY:
727                         /* else it is being freed elsewhere */
728                         list_move(&page->lru, src);
729                         continue;
730
731                 default:
732                         BUG();
733                 }
734
735                 if (!order)
736                         continue;
737
738                 /*
739                  * Attempt to take all pages in the order aligned region
740                  * surrounding the tag page.  Only take those pages of
741                  * the same active state as that tag page.  We may safely
742                  * round the target page pfn down to the requested order
743                  * as the mem_map is guarenteed valid out to MAX_ORDER,
744                  * where that page is in a different zone we will detect
745                  * it from its zone id and abort this block scan.
746                  */
747                 zone_id = page_zone_id(page);
748                 page_pfn = page_to_pfn(page);
749                 pfn = page_pfn & ~((1 << order) - 1);
750                 end_pfn = pfn + (1 << order);
751                 for (; pfn < end_pfn; pfn++) {
752                         struct page *cursor_page;
753
754                         /* The target page is in the block, ignore it. */
755                         if (unlikely(pfn == page_pfn))
756                                 continue;
757
758                         /* Avoid holes within the zone. */
759                         if (unlikely(!pfn_valid_within(pfn)))
760                                 break;
761
762                         cursor_page = pfn_to_page(pfn);
763                         /* Check that we have not crossed a zone boundary. */
764                         if (unlikely(page_zone_id(cursor_page) != zone_id))
765                                 continue;
766                         switch (__isolate_lru_page(cursor_page, mode)) {
767                         case 0:
768                                 list_move(&cursor_page->lru, dst);
769                                 nr_taken++;
770                                 scan++;
771                                 break;
772
773                         case -EBUSY:
774                                 /* else it is being freed elsewhere */
775                                 list_move(&cursor_page->lru, src);
776                         default:
777                                 break;
778                         }
779                 }
780         }
781
782         *scanned = scan;
783         return nr_taken;
784 }
785
786 static unsigned long isolate_pages_global(unsigned long nr,
787                                         struct list_head *dst,
788                                         unsigned long *scanned, int order,
789                                         int mode, struct zone *z,
790                                         struct mem_cgroup *mem_cont,
791                                         int active)
792 {
793         if (active)
794                 return isolate_lru_pages(nr, &z->active_list, dst,
795                                                 scanned, order, mode);
796         else
797                 return isolate_lru_pages(nr, &z->inactive_list, dst,
798                                                 scanned, order, mode);
799 }
800
801 /*
802  * clear_active_flags() is a helper for shrink_active_list(), clearing
803  * any active bits from the pages in the list.
804  */
805 static unsigned long clear_active_flags(struct list_head *page_list)
806 {
807         int nr_active = 0;
808         struct page *page;
809
810         list_for_each_entry(page, page_list, lru)
811                 if (PageActive(page)) {
812                         ClearPageActive(page);
813                         nr_active++;
814                 }
815
816         return nr_active;
817 }
818
819 /*
820  * shrink_inactive_list() is a helper for shrink_zone().  It returns the number
821  * of reclaimed pages
822  */
823 static unsigned long shrink_inactive_list(unsigned long max_scan,
824                                 struct zone *zone, struct scan_control *sc)
825 {
826         LIST_HEAD(page_list);
827         struct pagevec pvec;
828         unsigned long nr_scanned = 0;
829         unsigned long nr_reclaimed = 0;
830
831         pagevec_init(&pvec, 1);
832
833         lru_add_drain();
834         spin_lock_irq(&zone->lru_lock);
835         do {
836                 struct page *page;
837                 unsigned long nr_taken;
838                 unsigned long nr_scan;
839                 unsigned long nr_freed;
840                 unsigned long nr_active;
841
842                 nr_taken = sc->isolate_pages(sc->swap_cluster_max,
843                              &page_list, &nr_scan, sc->order,
844                              (sc->order > PAGE_ALLOC_COSTLY_ORDER)?
845                                              ISOLATE_BOTH : ISOLATE_INACTIVE,
846                                 zone, sc->mem_cgroup, 0);
847                 nr_active = clear_active_flags(&page_list);
848                 __count_vm_events(PGDEACTIVATE, nr_active);
849
850                 __mod_zone_page_state(zone, NR_ACTIVE, -nr_active);
851                 __mod_zone_page_state(zone, NR_INACTIVE,
852                                                 -(nr_taken - nr_active));
853                 zone->pages_scanned += nr_scan;
854                 spin_unlock_irq(&zone->lru_lock);
855
856                 nr_scanned += nr_scan;
857                 nr_freed = shrink_page_list(&page_list, sc, PAGEOUT_IO_ASYNC);
858
859                 /*
860                  * If we are direct reclaiming for contiguous pages and we do
861                  * not reclaim everything in the list, try again and wait
862                  * for IO to complete. This will stall high-order allocations
863                  * but that should be acceptable to the caller
864                  */
865                 if (nr_freed < nr_taken && !current_is_kswapd() &&
866                                         sc->order > PAGE_ALLOC_COSTLY_ORDER) {
867                         congestion_wait(WRITE, HZ/10);
868
869                         /*
870                          * The attempt at page out may have made some
871                          * of the pages active, mark them inactive again.
872                          */
873                         nr_active = clear_active_flags(&page_list);
874                         count_vm_events(PGDEACTIVATE, nr_active);
875
876                         nr_freed += shrink_page_list(&page_list, sc,
877                                                         PAGEOUT_IO_SYNC);
878                 }
879
880                 nr_reclaimed += nr_freed;
881                 local_irq_disable();
882                 if (current_is_kswapd()) {
883                         __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan);
884                         __count_vm_events(KSWAPD_STEAL, nr_freed);
885                 } else
886                         __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan);
887                 __count_zone_vm_events(PGSTEAL, zone, nr_freed);
888
889                 if (nr_taken == 0)
890                         goto done;
891
892                 spin_lock(&zone->lru_lock);
893                 /*
894                  * Put back any unfreeable pages.
895                  */
896                 while (!list_empty(&page_list)) {
897                         page = lru_to_page(&page_list);
898                         VM_BUG_ON(PageLRU(page));
899                         SetPageLRU(page);
900                         list_del(&page->lru);
901                         if (PageActive(page))
902                                 add_page_to_active_list(zone, page);
903                         else
904                                 add_page_to_inactive_list(zone, page);
905                         if (!pagevec_add(&pvec, page)) {
906                                 spin_unlock_irq(&zone->lru_lock);
907                                 __pagevec_release(&pvec);
908                                 spin_lock_irq(&zone->lru_lock);
909                         }
910                 }
911         } while (nr_scanned < max_scan);
912         spin_unlock(&zone->lru_lock);
913 done:
914         local_irq_enable();
915         pagevec_release(&pvec);
916         return nr_reclaimed;
917 }
918
919 /*
920  * We are about to scan this zone at a certain priority level.  If that priority
921  * level is smaller (ie: more urgent) than the previous priority, then note
922  * that priority level within the zone.  This is done so that when the next
923  * process comes in to scan this zone, it will immediately start out at this
924  * priority level rather than having to build up its own scanning priority.
925  * Here, this priority affects only the reclaim-mapped threshold.
926  */
927 static inline void note_zone_scanning_priority(struct zone *zone, int priority)
928 {
929         if (priority < zone->prev_priority)
930                 zone->prev_priority = priority;
931 }
932
933 static inline int zone_is_near_oom(struct zone *zone)
934 {
935         return zone->pages_scanned >= (zone_page_state(zone, NR_ACTIVE)
936                                 + zone_page_state(zone, NR_INACTIVE))*3;
937 }
938
939 /*
940  * This moves pages from the active list to the inactive list.
941  *
942  * We move them the other way if the page is referenced by one or more
943  * processes, from rmap.
944  *
945  * If the pages are mostly unmapped, the processing is fast and it is
946  * appropriate to hold zone->lru_lock across the whole operation.  But if
947  * the pages are mapped, the processing is slow (page_referenced()) so we
948  * should drop zone->lru_lock around each page.  It's impossible to balance
949  * this, so instead we remove the pages from the LRU while processing them.
950  * It is safe to rely on PG_active against the non-LRU pages in here because
951  * nobody will play with that bit on a non-LRU page.
952  *
953  * The downside is that we have to touch page->_count against each page.
954  * But we had to alter page->flags anyway.
955  */
956 static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
957                                 struct scan_control *sc, int priority)
958 {
959         unsigned long pgmoved;
960         int pgdeactivate = 0;
961         unsigned long pgscanned;
962         LIST_HEAD(l_hold);      /* The pages which were snipped off */
963         LIST_HEAD(l_inactive);  /* Pages to go onto the inactive_list */
964         LIST_HEAD(l_active);    /* Pages to go onto the active_list */
965         struct page *page;
966         struct pagevec pvec;
967         int reclaim_mapped = 0;
968
969         if (sc->may_swap) {
970                 long mapped_ratio;
971                 long distress;
972                 long swap_tendency;
973                 long imbalance;
974
975                 if (zone_is_near_oom(zone))
976                         goto force_reclaim_mapped;
977
978                 /*
979                  * `distress' is a measure of how much trouble we're having
980                  * reclaiming pages.  0 -> no problems.  100 -> great trouble.
981                  */
982                 distress = 100 >> min(zone->prev_priority, priority);
983
984                 /*
985                  * The point of this algorithm is to decide when to start
986                  * reclaiming mapped memory instead of just pagecache.  Work out
987                  * how much memory
988                  * is mapped.
989                  */
990                 mapped_ratio = ((global_page_state(NR_FILE_MAPPED) +
991                                 global_page_state(NR_ANON_PAGES)) * 100) /
992                                         vm_total_pages;
993
994                 /*
995                  * Now decide how much we really want to unmap some pages.  The
996                  * mapped ratio is downgraded - just because there's a lot of
997                  * mapped memory doesn't necessarily mean that page reclaim
998                  * isn't succeeding.
999                  *
1000                  * The distress ratio is important - we don't want to start
1001                  * going oom.
1002                  *
1003                  * A 100% value of vm_swappiness overrides this algorithm
1004                  * altogether.
1005                  */
1006                 swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
1007
1008                 /*
1009                  * If there's huge imbalance between active and inactive
1010                  * (think active 100 times larger than inactive) we should
1011                  * become more permissive, or the system will take too much
1012                  * cpu before it start swapping during memory pressure.
1013                  * Distress is about avoiding early-oom, this is about
1014                  * making swappiness graceful despite setting it to low
1015                  * values.
1016                  *
1017                  * Avoid div by zero with nr_inactive+1, and max resulting
1018                  * value is vm_total_pages.
1019                  */
1020                 imbalance  = zone_page_state(zone, NR_ACTIVE);
1021                 imbalance /= zone_page_state(zone, NR_INACTIVE) + 1;
1022
1023                 /*
1024                  * Reduce the effect of imbalance if swappiness is low,
1025                  * this means for a swappiness very low, the imbalance
1026                  * must be much higher than 100 for this logic to make
1027                  * the difference.
1028                  *
1029                  * Max temporary value is vm_total_pages*100.
1030                  */
1031                 imbalance *= (vm_swappiness + 1);
1032                 imbalance /= 100;
1033
1034                 /*
1035                  * If not much of the ram is mapped, makes the imbalance
1036                  * less relevant, it's high priority we refill the inactive
1037                  * list with mapped pages only in presence of high ratio of
1038                  * mapped pages.
1039                  *
1040                  * Max temporary value is vm_total_pages*100.
1041                  */
1042                 imbalance *= mapped_ratio;
1043                 imbalance /= 100;
1044
1045                 /* apply imbalance feedback to swap_tendency */
1046                 swap_tendency += imbalance;
1047
1048                 /*
1049                  * Now use this metric to decide whether to start moving mapped
1050                  * memory onto the inactive list.
1051                  */
1052                 if (swap_tendency >= 100)
1053 force_reclaim_mapped:
1054                         reclaim_mapped = 1;
1055         }
1056
1057         lru_add_drain();
1058         spin_lock_irq(&zone->lru_lock);
1059         pgmoved = sc->isolate_pages(nr_pages, &l_hold, &pgscanned, sc->order,
1060                                         ISOLATE_ACTIVE, zone,
1061                                         sc->mem_cgroup, 1);
1062         zone->pages_scanned += pgscanned;
1063         __mod_zone_page_state(zone, NR_ACTIVE, -pgmoved);
1064         spin_unlock_irq(&zone->lru_lock);
1065
1066         while (!list_empty(&l_hold)) {
1067                 cond_resched();
1068                 page = lru_to_page(&l_hold);
1069                 list_del(&page->lru);
1070                 if (page_mapped(page)) {
1071                         if (!reclaim_mapped ||
1072                             (total_swap_pages == 0 && PageAnon(page)) ||
1073                             page_referenced(page, 0, sc->mem_cgroup)) {
1074                                 list_add(&page->lru, &l_active);
1075                                 continue;
1076                         }
1077                 }
1078                 list_add(&page->lru, &l_inactive);
1079         }
1080
1081         pagevec_init(&pvec, 1);
1082         pgmoved = 0;
1083         spin_lock_irq(&zone->lru_lock);
1084         while (!list_empty(&l_inactive)) {
1085                 page = lru_to_page(&l_inactive);
1086                 prefetchw_prev_lru_page(page, &l_inactive, flags);
1087                 VM_BUG_ON(PageLRU(page));
1088                 SetPageLRU(page);
1089                 VM_BUG_ON(!PageActive(page));
1090                 ClearPageActive(page);
1091
1092                 list_move(&page->lru, &zone->inactive_list);
1093                 mem_cgroup_move_lists(page_get_page_cgroup(page), false);
1094                 pgmoved++;
1095                 if (!pagevec_add(&pvec, page)) {
1096                         __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
1097                         spin_unlock_irq(&zone->lru_lock);
1098                         pgdeactivate += pgmoved;
1099                         pgmoved = 0;
1100                         if (buffer_heads_over_limit)
1101                                 pagevec_strip(&pvec);
1102                         __pagevec_release(&pvec);
1103                         spin_lock_irq(&zone->lru_lock);
1104                 }
1105         }
1106         __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
1107         pgdeactivate += pgmoved;
1108         if (buffer_heads_over_limit) {
1109                 spin_unlock_irq(&zone->lru_lock);
1110                 pagevec_strip(&pvec);
1111                 spin_lock_irq(&zone->lru_lock);
1112         }
1113
1114         pgmoved = 0;
1115         while (!list_empty(&l_active)) {
1116                 page = lru_to_page(&l_active);
1117                 prefetchw_prev_lru_page(page, &l_active, flags);
1118                 VM_BUG_ON(PageLRU(page));
1119                 SetPageLRU(page);
1120                 VM_BUG_ON(!PageActive(page));
1121                 list_move(&page->lru, &zone->active_list);
1122                 mem_cgroup_move_lists(page_get_page_cgroup(page), true);
1123                 pgmoved++;
1124                 if (!pagevec_add(&pvec, page)) {
1125                         __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
1126                         pgmoved = 0;
1127                         spin_unlock_irq(&zone->lru_lock);
1128                         __pagevec_release(&pvec);
1129                         spin_lock_irq(&zone->lru_lock);
1130                 }
1131         }
1132         __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
1133
1134         __count_zone_vm_events(PGREFILL, zone, pgscanned);
1135         __count_vm_events(PGDEACTIVATE, pgdeactivate);
1136         spin_unlock_irq(&zone->lru_lock);
1137
1138         pagevec_release(&pvec);
1139 }
1140
1141 /*
1142  * This is a basic per-zone page freer.  Used by both kswapd and direct reclaim.
1143  */
1144 static unsigned long shrink_zone(int priority, struct zone *zone,
1145                                 struct scan_control *sc)
1146 {
1147         unsigned long nr_active;
1148         unsigned long nr_inactive;
1149         unsigned long nr_to_scan;
1150         unsigned long nr_reclaimed = 0;
1151
1152         /*
1153          * Add one to `nr_to_scan' just to make sure that the kernel will
1154          * slowly sift through the active list.
1155          */
1156         zone->nr_scan_active +=
1157                 (zone_page_state(zone, NR_ACTIVE) >> priority) + 1;
1158         nr_active = zone->nr_scan_active;
1159         if (nr_active >= sc->swap_cluster_max)
1160                 zone->nr_scan_active = 0;
1161         else
1162                 nr_active = 0;
1163
1164         zone->nr_scan_inactive +=
1165                 (zone_page_state(zone, NR_INACTIVE) >> priority) + 1;
1166         nr_inactive = zone->nr_scan_inactive;
1167         if (nr_inactive >= sc->swap_cluster_max)
1168                 zone->nr_scan_inactive = 0;
1169         else
1170                 nr_inactive = 0;
1171
1172         while (nr_active || nr_inactive) {
1173                 if (nr_active) {
1174                         nr_to_scan = min(nr_active,
1175                                         (unsigned long)sc->swap_cluster_max);
1176                         nr_active -= nr_to_scan;
1177                         shrink_active_list(nr_to_scan, zone, sc, priority);
1178                 }
1179
1180                 if (nr_inactive) {
1181                         nr_to_scan = min(nr_inactive,
1182                                         (unsigned long)sc->swap_cluster_max);
1183                         nr_inactive -= nr_to_scan;
1184                         nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
1185                                                                 sc);
1186                 }
1187         }
1188
1189         throttle_vm_writeout(sc->gfp_mask);
1190         return nr_reclaimed;
1191 }
1192
1193 /*
1194  * This is the direct reclaim path, for page-allocating processes.  We only
1195  * try to reclaim pages from zones which will satisfy the caller's allocation
1196  * request.
1197  *
1198  * We reclaim from a zone even if that zone is over pages_high.  Because:
1199  * a) The caller may be trying to free *extra* pages to satisfy a higher-order
1200  *    allocation or
1201  * b) The zones may be over pages_high but they must go *over* pages_high to
1202  *    satisfy the `incremental min' zone defense algorithm.
1203  *
1204  * Returns the number of reclaimed pages.
1205  *
1206  * If a zone is deemed to be full of pinned pages then just give it a light
1207  * scan then give up on it.
1208  */
1209 static unsigned long shrink_zones(int priority, struct zone **zones,
1210                                         struct scan_control *sc)
1211 {
1212         unsigned long nr_reclaimed = 0;
1213         int i;
1214
1215         sc->all_unreclaimable = 1;
1216         for (i = 0; zones[i] != NULL; i++) {
1217                 struct zone *zone = zones[i];
1218
1219                 if (!populated_zone(zone))
1220                         continue;
1221
1222                 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
1223                         continue;
1224
1225                 note_zone_scanning_priority(zone, priority);
1226
1227                 if (zone_is_all_unreclaimable(zone) && priority != DEF_PRIORITY)
1228                         continue;       /* Let kswapd poll it */
1229
1230                 sc->all_unreclaimable = 0;
1231
1232                 nr_reclaimed += shrink_zone(priority, zone, sc);
1233         }
1234         return nr_reclaimed;
1235 }
1236  
1237 /*
1238  * This is the main entry point to direct page reclaim.
1239  *
1240  * If a full scan of the inactive list fails to free enough memory then we
1241  * are "out of memory" and something needs to be killed.
1242  *
1243  * If the caller is !__GFP_FS then the probability of a failure is reasonably
1244  * high - the zone may be full of dirty or under-writeback pages, which this
1245  * caller can't do much about.  We kick pdflush and take explicit naps in the
1246  * hope that some of these pages can be written.  But if the allocating task
1247  * holds filesystem locks which prevent writeout this might not work, and the
1248  * allocation attempt will fail.
1249  */
1250 static unsigned long do_try_to_free_pages(struct zone **zones, gfp_t gfp_mask,
1251                                           struct scan_control *sc)
1252 {
1253         int priority;
1254         int ret = 0;
1255         unsigned long total_scanned = 0;
1256         unsigned long nr_reclaimed = 0;
1257         struct reclaim_state *reclaim_state = current->reclaim_state;
1258         unsigned long lru_pages = 0;
1259         int i;
1260
1261         count_vm_event(ALLOCSTALL);
1262
1263         for (i = 0; zones[i] != NULL; i++) {
1264                 struct zone *zone = zones[i];
1265
1266                 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
1267                         continue;
1268
1269                 lru_pages += zone_page_state(zone, NR_ACTIVE)
1270                                 + zone_page_state(zone, NR_INACTIVE);
1271         }
1272
1273         for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1274                 sc->nr_scanned = 0;
1275                 sc->nr_io_pages = 0;
1276                 if (!priority)
1277                         disable_swap_token();
1278                 nr_reclaimed += shrink_zones(priority, zones, sc);
1279                 /*
1280                  * Don't shrink slabs when reclaiming memory from
1281                  * over limit cgroups
1282                  */
1283                 if (sc->mem_cgroup == NULL)
1284                         shrink_slab(sc->nr_scanned, gfp_mask, lru_pages);
1285                 if (reclaim_state) {
1286                         nr_reclaimed += reclaim_state->reclaimed_slab;
1287                         reclaim_state->reclaimed_slab = 0;
1288                 }
1289                 total_scanned += sc->nr_scanned;
1290                 if (nr_reclaimed >= sc->swap_cluster_max) {
1291                         ret = 1;
1292                         goto out;
1293                 }
1294
1295                 /*
1296                  * Try to write back as many pages as we just scanned.  This
1297                  * tends to cause slow streaming writers to write data to the
1298                  * disk smoothly, at the dirtying rate, which is nice.   But
1299                  * that's undesirable in laptop mode, where we *want* lumpy
1300                  * writeout.  So in laptop mode, write out the whole world.
1301                  */
1302                 if (total_scanned > sc->swap_cluster_max +
1303                                         sc->swap_cluster_max / 2) {
1304                         wakeup_pdflush(laptop_mode ? 0 : total_scanned);
1305                         sc->may_writepage = 1;
1306                 }
1307
1308                 /* Take a nap, wait for some writeback to complete */
1309                 if (sc->nr_scanned && priority < DEF_PRIORITY - 2 &&
1310                                 sc->nr_io_pages > sc->swap_cluster_max)
1311                         congestion_wait(WRITE, HZ/10);
1312         }
1313         /* top priority shrink_caches still had more to do? don't OOM, then */
1314         if (!sc->all_unreclaimable && sc->mem_cgroup == NULL)
1315                 ret = 1;
1316 out:
1317         /*
1318          * Now that we've scanned all the zones at this priority level, note
1319          * that level within the zone so that the next thread which performs
1320          * scanning of this zone will immediately start out at this priority
1321          * level.  This affects only the decision whether or not to bring
1322          * mapped pages onto the inactive list.
1323          */
1324         if (priority < 0)
1325                 priority = 0;
1326         for (i = 0; zones[i] != NULL; i++) {
1327                 struct zone *zone = zones[i];
1328
1329                 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
1330                         continue;
1331
1332                 zone->prev_priority = priority;
1333         }
1334         return ret;
1335 }
1336
1337 unsigned long try_to_free_pages(struct zone **zones, int order, gfp_t gfp_mask)
1338 {
1339         struct scan_control sc = {
1340                 .gfp_mask = gfp_mask,
1341                 .may_writepage = !laptop_mode,
1342                 .swap_cluster_max = SWAP_CLUSTER_MAX,
1343                 .may_swap = 1,
1344                 .swappiness = vm_swappiness,
1345                 .order = order,
1346                 .mem_cgroup = NULL,
1347                 .isolate_pages = isolate_pages_global,
1348         };
1349
1350         return do_try_to_free_pages(zones, gfp_mask, &sc);
1351 }
1352
1353 #ifdef CONFIG_CGROUP_MEM_CONT
1354
1355 unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont,
1356                                                 gfp_t gfp_mask)
1357 {
1358         struct scan_control sc = {
1359                 .gfp_mask = gfp_mask,
1360                 .may_writepage = !laptop_mode,
1361                 .may_swap = 1,
1362                 .swap_cluster_max = SWAP_CLUSTER_MAX,
1363                 .swappiness = vm_swappiness,
1364                 .order = 0,
1365                 .mem_cgroup = mem_cont,
1366                 .isolate_pages = mem_cgroup_isolate_pages,
1367         };
1368         struct zone **zones;
1369         int target_zone = gfp_zone(GFP_HIGHUSER_MOVABLE);
1370
1371         zones = NODE_DATA(numa_node_id())->node_zonelists[target_zone].zones;
1372         if (do_try_to_free_pages(zones, sc.gfp_mask, &sc))
1373                 return 1;
1374         return 0;
1375 }
1376 #endif
1377
1378 /*
1379  * For kswapd, balance_pgdat() will work across all this node's zones until
1380  * they are all at pages_high.
1381  *
1382  * Returns the number of pages which were actually freed.
1383  *
1384  * There is special handling here for zones which are full of pinned pages.
1385  * This can happen if the pages are all mlocked, or if they are all used by
1386  * device drivers (say, ZONE_DMA).  Or if they are all in use by hugetlb.
1387  * What we do is to detect the case where all pages in the zone have been
1388  * scanned twice and there has been zero successful reclaim.  Mark the zone as
1389  * dead and from now on, only perform a short scan.  Basically we're polling
1390  * the zone for when the problem goes away.
1391  *
1392  * kswapd scans the zones in the highmem->normal->dma direction.  It skips
1393  * zones which have free_pages > pages_high, but once a zone is found to have
1394  * free_pages <= pages_high, we scan that zone and the lower zones regardless
1395  * of the number of free pages in the lower zones.  This interoperates with
1396  * the page allocator fallback scheme to ensure that aging of pages is balanced
1397  * across the zones.
1398  */
1399 static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
1400 {
1401         int all_zones_ok;
1402         int priority;
1403         int i;
1404         unsigned long total_scanned;
1405         unsigned long nr_reclaimed;
1406         struct reclaim_state *reclaim_state = current->reclaim_state;
1407         struct scan_control sc = {
1408                 .gfp_mask = GFP_KERNEL,
1409                 .may_swap = 1,
1410                 .swap_cluster_max = SWAP_CLUSTER_MAX,
1411                 .swappiness = vm_swappiness,
1412                 .order = order,
1413                 .mem_cgroup = NULL,
1414                 .isolate_pages = isolate_pages_global,
1415         };
1416         /*
1417          * temp_priority is used to remember the scanning priority at which
1418          * this zone was successfully refilled to free_pages == pages_high.
1419          */
1420         int temp_priority[MAX_NR_ZONES];
1421
1422 loop_again:
1423         total_scanned = 0;
1424         nr_reclaimed = 0;
1425         sc.may_writepage = !laptop_mode;
1426         count_vm_event(PAGEOUTRUN);
1427
1428         for (i = 0; i < pgdat->nr_zones; i++)
1429                 temp_priority[i] = DEF_PRIORITY;
1430
1431         for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1432                 int end_zone = 0;       /* Inclusive.  0 = ZONE_DMA */
1433                 unsigned long lru_pages = 0;
1434
1435                 /* The swap token gets in the way of swapout... */
1436                 if (!priority)
1437                         disable_swap_token();
1438
1439                 sc.nr_io_pages = 0;
1440                 all_zones_ok = 1;
1441
1442                 /*
1443                  * Scan in the highmem->dma direction for the highest
1444                  * zone which needs scanning
1445                  */
1446                 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1447                         struct zone *zone = pgdat->node_zones + i;
1448
1449                         if (!populated_zone(zone))
1450                                 continue;
1451
1452                         if (zone_is_all_unreclaimable(zone) &&
1453                             priority != DEF_PRIORITY)
1454                                 continue;
1455
1456                         if (!zone_watermark_ok(zone, order, zone->pages_high,
1457                                                0, 0)) {
1458                                 end_zone = i;
1459                                 break;
1460                         }
1461                 }
1462                 if (i < 0)
1463                         goto out;
1464
1465                 for (i = 0; i <= end_zone; i++) {
1466                         struct zone *zone = pgdat->node_zones + i;
1467
1468                         lru_pages += zone_page_state(zone, NR_ACTIVE)
1469                                         + zone_page_state(zone, NR_INACTIVE);
1470                 }
1471
1472                 /*
1473                  * Now scan the zone in the dma->highmem direction, stopping
1474                  * at the last zone which needs scanning.
1475                  *
1476                  * We do this because the page allocator works in the opposite
1477                  * direction.  This prevents the page allocator from allocating
1478                  * pages behind kswapd's direction of progress, which would
1479                  * cause too much scanning of the lower zones.
1480                  */
1481                 for (i = 0; i <= end_zone; i++) {
1482                         struct zone *zone = pgdat->node_zones + i;
1483                         int nr_slab;
1484
1485                         if (!populated_zone(zone))
1486                                 continue;
1487
1488                         if (zone_is_all_unreclaimable(zone) &&
1489                                         priority != DEF_PRIORITY)
1490                                 continue;
1491
1492                         if (!zone_watermark_ok(zone, order, zone->pages_high,
1493                                                end_zone, 0))
1494                                 all_zones_ok = 0;
1495                         temp_priority[i] = priority;
1496                         sc.nr_scanned = 0;
1497                         note_zone_scanning_priority(zone, priority);
1498                         /*
1499                          * We put equal pressure on every zone, unless one
1500                          * zone has way too many pages free already.
1501                          */
1502                         if (!zone_watermark_ok(zone, order, 8*zone->pages_high,
1503                                                 end_zone, 0))
1504                                 nr_reclaimed += shrink_zone(priority, zone, &sc);
1505                         reclaim_state->reclaimed_slab = 0;
1506                         nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1507                                                 lru_pages);
1508                         nr_reclaimed += reclaim_state->reclaimed_slab;
1509                         total_scanned += sc.nr_scanned;
1510                         if (zone_is_all_unreclaimable(zone))
1511                                 continue;
1512                         if (nr_slab == 0 && zone->pages_scanned >=
1513                                 (zone_page_state(zone, NR_ACTIVE)
1514                                 + zone_page_state(zone, NR_INACTIVE)) * 6)
1515                                         zone_set_flag(zone,
1516                                                       ZONE_ALL_UNRECLAIMABLE);
1517                         /*
1518                          * If we've done a decent amount of scanning and
1519                          * the reclaim ratio is low, start doing writepage
1520                          * even in laptop mode
1521                          */
1522                         if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
1523                             total_scanned > nr_reclaimed + nr_reclaimed / 2)
1524                                 sc.may_writepage = 1;
1525                 }
1526                 if (all_zones_ok)
1527                         break;          /* kswapd: all done */
1528                 /*
1529                  * OK, kswapd is getting into trouble.  Take a nap, then take
1530                  * another pass across the zones.
1531                  */
1532                 if (total_scanned && priority < DEF_PRIORITY - 2 &&
1533                                         sc.nr_io_pages > sc.swap_cluster_max)
1534                         congestion_wait(WRITE, HZ/10);
1535
1536                 /*
1537                  * We do this so kswapd doesn't build up large priorities for
1538                  * example when it is freeing in parallel with allocators. It
1539                  * matches the direct reclaim path behaviour in terms of impact
1540                  * on zone->*_priority.
1541                  */
1542                 if (nr_reclaimed >= SWAP_CLUSTER_MAX)
1543                         break;
1544         }
1545 out:
1546         /*
1547          * Note within each zone the priority level at which this zone was
1548          * brought into a happy state.  So that the next thread which scans this
1549          * zone will start out at that priority level.
1550          */
1551         for (i = 0; i < pgdat->nr_zones; i++) {
1552                 struct zone *zone = pgdat->node_zones + i;
1553
1554                 zone->prev_priority = temp_priority[i];
1555         }
1556         if (!all_zones_ok) {
1557                 cond_resched();
1558
1559                 try_to_freeze();
1560
1561                 goto loop_again;
1562         }
1563
1564         return nr_reclaimed;
1565 }
1566
1567 /*
1568  * The background pageout daemon, started as a kernel thread
1569  * from the init process. 
1570  *
1571  * This basically trickles out pages so that we have _some_
1572  * free memory available even if there is no other activity
1573  * that frees anything up. This is needed for things like routing
1574  * etc, where we otherwise might have all activity going on in
1575  * asynchronous contexts that cannot page things out.
1576  *
1577  * If there are applications that are active memory-allocators
1578  * (most normal use), this basically shouldn't matter.
1579  */
1580 static int kswapd(void *p)
1581 {
1582         unsigned long order;
1583         pg_data_t *pgdat = (pg_data_t*)p;
1584         struct task_struct *tsk = current;
1585         DEFINE_WAIT(wait);
1586         struct reclaim_state reclaim_state = {
1587                 .reclaimed_slab = 0,
1588         };
1589         cpumask_t cpumask;
1590
1591         cpumask = node_to_cpumask(pgdat->node_id);
1592         if (!cpus_empty(cpumask))
1593                 set_cpus_allowed(tsk, cpumask);
1594         current->reclaim_state = &reclaim_state;
1595
1596         /*
1597          * Tell the memory management that we're a "memory allocator",
1598          * and that if we need more memory we should get access to it
1599          * regardless (see "__alloc_pages()"). "kswapd" should
1600          * never get caught in the normal page freeing logic.
1601          *
1602          * (Kswapd normally doesn't need memory anyway, but sometimes
1603          * you need a small amount of memory in order to be able to
1604          * page out something else, and this flag essentially protects
1605          * us from recursively trying to free more memory as we're
1606          * trying to free the first piece of memory in the first place).
1607          */
1608         tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
1609         set_freezable();
1610
1611         order = 0;
1612         for ( ; ; ) {
1613                 unsigned long new_order;
1614
1615                 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1616                 new_order = pgdat->kswapd_max_order;
1617                 pgdat->kswapd_max_order = 0;
1618                 if (order < new_order) {
1619                         /*
1620                          * Don't sleep if someone wants a larger 'order'
1621                          * allocation
1622                          */
1623                         order = new_order;
1624                 } else {
1625                         if (!freezing(current))
1626                                 schedule();
1627
1628                         order = pgdat->kswapd_max_order;
1629                 }
1630                 finish_wait(&pgdat->kswapd_wait, &wait);
1631
1632                 if (!try_to_freeze()) {
1633                         /* We can speed up thawing tasks if we don't call
1634                          * balance_pgdat after returning from the refrigerator
1635                          */
1636                         balance_pgdat(pgdat, order);
1637                 }
1638         }
1639         return 0;
1640 }
1641
1642 /*
1643  * A zone is low on free memory, so wake its kswapd task to service it.
1644  */
1645 void wakeup_kswapd(struct zone *zone, int order)
1646 {
1647         pg_data_t *pgdat;
1648
1649         if (!populated_zone(zone))
1650                 return;
1651
1652         pgdat = zone->zone_pgdat;
1653         if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
1654                 return;
1655         if (pgdat->kswapd_max_order < order)
1656                 pgdat->kswapd_max_order = order;
1657         if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
1658                 return;
1659         if (!waitqueue_active(&pgdat->kswapd_wait))
1660                 return;
1661         wake_up_interruptible(&pgdat->kswapd_wait);
1662 }
1663
1664 #ifdef CONFIG_PM
1665 /*
1666  * Helper function for shrink_all_memory().  Tries to reclaim 'nr_pages' pages
1667  * from LRU lists system-wide, for given pass and priority, and returns the
1668  * number of reclaimed pages
1669  *
1670  * For pass > 3 we also try to shrink the LRU lists that contain a few pages
1671  */
1672 static unsigned long shrink_all_zones(unsigned long nr_pages, int prio,
1673                                       int pass, struct scan_control *sc)
1674 {
1675         struct zone *zone;
1676         unsigned long nr_to_scan, ret = 0;
1677
1678         for_each_zone(zone) {
1679
1680                 if (!populated_zone(zone))
1681                         continue;
1682
1683                 if (zone_is_all_unreclaimable(zone) && prio != DEF_PRIORITY)
1684                         continue;
1685
1686                 /* For pass = 0 we don't shrink the active list */
1687                 if (pass > 0) {
1688                         zone->nr_scan_active +=
1689                                 (zone_page_state(zone, NR_ACTIVE) >> prio) + 1;
1690                         if (zone->nr_scan_active >= nr_pages || pass > 3) {
1691                                 zone->nr_scan_active = 0;
1692                                 nr_to_scan = min(nr_pages,
1693                                         zone_page_state(zone, NR_ACTIVE));
1694                                 shrink_active_list(nr_to_scan, zone, sc, prio);
1695                         }
1696                 }
1697
1698                 zone->nr_scan_inactive +=
1699                         (zone_page_state(zone, NR_INACTIVE) >> prio) + 1;
1700                 if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
1701                         zone->nr_scan_inactive = 0;
1702                         nr_to_scan = min(nr_pages,
1703                                 zone_page_state(zone, NR_INACTIVE));
1704                         ret += shrink_inactive_list(nr_to_scan, zone, sc);
1705                         if (ret >= nr_pages)
1706                                 return ret;
1707                 }
1708         }
1709
1710         return ret;
1711 }
1712
1713 static unsigned long count_lru_pages(void)
1714 {
1715         return global_page_state(NR_ACTIVE) + global_page_state(NR_INACTIVE);
1716 }
1717
1718 /*
1719  * Try to free `nr_pages' of memory, system-wide, and return the number of
1720  * freed pages.
1721  *
1722  * Rather than trying to age LRUs the aim is to preserve the overall
1723  * LRU order by reclaiming preferentially
1724  * inactive > active > active referenced > active mapped
1725  */
1726 unsigned long shrink_all_memory(unsigned long nr_pages)
1727 {
1728         unsigned long lru_pages, nr_slab;
1729         unsigned long ret = 0;
1730         int pass;
1731         struct reclaim_state reclaim_state;
1732         struct scan_control sc = {
1733                 .gfp_mask = GFP_KERNEL,
1734                 .may_swap = 0,
1735                 .swap_cluster_max = nr_pages,
1736                 .may_writepage = 1,
1737                 .swappiness = vm_swappiness,
1738                 .isolate_pages = isolate_pages_global,
1739         };
1740
1741         current->reclaim_state = &reclaim_state;
1742
1743         lru_pages = count_lru_pages();
1744         nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
1745         /* If slab caches are huge, it's better to hit them first */
1746         while (nr_slab >= lru_pages) {
1747                 reclaim_state.reclaimed_slab = 0;
1748                 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1749                 if (!reclaim_state.reclaimed_slab)
1750                         break;
1751
1752                 ret += reclaim_state.reclaimed_slab;
1753                 if (ret >= nr_pages)
1754                         goto out;
1755
1756                 nr_slab -= reclaim_state.reclaimed_slab;
1757         }
1758
1759         /*
1760          * We try to shrink LRUs in 5 passes:
1761          * 0 = Reclaim from inactive_list only
1762          * 1 = Reclaim from active list but don't reclaim mapped
1763          * 2 = 2nd pass of type 1
1764          * 3 = Reclaim mapped (normal reclaim)
1765          * 4 = 2nd pass of type 3
1766          */
1767         for (pass = 0; pass < 5; pass++) {
1768                 int prio;
1769
1770                 /* Force reclaiming mapped pages in the passes #3 and #4 */
1771                 if (pass > 2) {
1772                         sc.may_swap = 1;
1773                         sc.swappiness = 100;
1774                 }
1775
1776                 for (prio = DEF_PRIORITY; prio >= 0; prio--) {
1777                         unsigned long nr_to_scan = nr_pages - ret;
1778
1779                         sc.nr_scanned = 0;
1780                         ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
1781                         if (ret >= nr_pages)
1782                                 goto out;
1783
1784                         reclaim_state.reclaimed_slab = 0;
1785                         shrink_slab(sc.nr_scanned, sc.gfp_mask,
1786                                         count_lru_pages());
1787                         ret += reclaim_state.reclaimed_slab;
1788                         if (ret >= nr_pages)
1789                                 goto out;
1790
1791                         if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
1792                                 congestion_wait(WRITE, HZ / 10);
1793                 }
1794         }
1795
1796         /*
1797          * If ret = 0, we could not shrink LRUs, but there may be something
1798          * in slab caches
1799          */
1800         if (!ret) {
1801                 do {
1802                         reclaim_state.reclaimed_slab = 0;
1803                         shrink_slab(nr_pages, sc.gfp_mask, count_lru_pages());
1804                         ret += reclaim_state.reclaimed_slab;
1805                 } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
1806         }
1807
1808 out:
1809         current->reclaim_state = NULL;
1810
1811         return ret;
1812 }
1813 #endif
1814
1815 /* It's optimal to keep kswapds on the same CPUs as their memory, but
1816    not required for correctness.  So if the last cpu in a node goes
1817    away, we get changed to run anywhere: as the first one comes back,
1818    restore their cpu bindings. */
1819 static int __devinit cpu_callback(struct notifier_block *nfb,
1820                                   unsigned long action, void *hcpu)
1821 {
1822         pg_data_t *pgdat;
1823         cpumask_t mask;
1824         int nid;
1825
1826         if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
1827                 for_each_node_state(nid, N_HIGH_MEMORY) {
1828                         pgdat = NODE_DATA(nid);
1829                         mask = node_to_cpumask(pgdat->node_id);
1830                         if (any_online_cpu(mask) != NR_CPUS)
1831                                 /* One of our CPUs online: restore mask */
1832                                 set_cpus_allowed(pgdat->kswapd, mask);
1833                 }
1834         }
1835         return NOTIFY_OK;
1836 }
1837
1838 /*
1839  * This kswapd start function will be called by init and node-hot-add.
1840  * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
1841  */
1842 int kswapd_run(int nid)
1843 {
1844         pg_data_t *pgdat = NODE_DATA(nid);
1845         int ret = 0;
1846
1847         if (pgdat->kswapd)
1848                 return 0;
1849
1850         pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
1851         if (IS_ERR(pgdat->kswapd)) {
1852                 /* failure at boot is fatal */
1853                 BUG_ON(system_state == SYSTEM_BOOTING);
1854                 printk("Failed to start kswapd on node %d\n",nid);
1855                 ret = -1;
1856         }
1857         return ret;
1858 }
1859
1860 static int __init kswapd_init(void)
1861 {
1862         int nid;
1863
1864         swap_setup();
1865         for_each_node_state(nid, N_HIGH_MEMORY)
1866                 kswapd_run(nid);
1867         hotcpu_notifier(cpu_callback, 0);
1868         return 0;
1869 }
1870
1871 module_init(kswapd_init)
1872
1873 #ifdef CONFIG_NUMA
1874 /*
1875  * Zone reclaim mode
1876  *
1877  * If non-zero call zone_reclaim when the number of free pages falls below
1878  * the watermarks.
1879  */
1880 int zone_reclaim_mode __read_mostly;
1881
1882 #define RECLAIM_OFF 0
1883 #define RECLAIM_ZONE (1<<0)     /* Run shrink_cache on the zone */
1884 #define RECLAIM_WRITE (1<<1)    /* Writeout pages during reclaim */
1885 #define RECLAIM_SWAP (1<<2)     /* Swap pages out during reclaim */
1886
1887 /*
1888  * Priority for ZONE_RECLAIM. This determines the fraction of pages
1889  * of a node considered for each zone_reclaim. 4 scans 1/16th of
1890  * a zone.
1891  */
1892 #define ZONE_RECLAIM_PRIORITY 4
1893
1894 /*
1895  * Percentage of pages in a zone that must be unmapped for zone_reclaim to
1896  * occur.
1897  */
1898 int sysctl_min_unmapped_ratio = 1;
1899
1900 /*
1901  * If the number of slab pages in a zone grows beyond this percentage then
1902  * slab reclaim needs to occur.
1903  */
1904 int sysctl_min_slab_ratio = 5;
1905
1906 /*
1907  * Try to free up some pages from this zone through reclaim.
1908  */
1909 static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1910 {
1911         /* Minimum pages needed in order to stay on node */
1912         const unsigned long nr_pages = 1 << order;
1913         struct task_struct *p = current;
1914         struct reclaim_state reclaim_state;
1915         int priority;
1916         unsigned long nr_reclaimed = 0;
1917         struct scan_control sc = {
1918                 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1919                 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
1920                 .swap_cluster_max = max_t(unsigned long, nr_pages,
1921                                         SWAP_CLUSTER_MAX),
1922                 .gfp_mask = gfp_mask,
1923                 .swappiness = vm_swappiness,
1924                 .isolate_pages = isolate_pages_global,
1925         };
1926         unsigned long slab_reclaimable;
1927
1928         disable_swap_token();
1929         cond_resched();
1930         /*
1931          * We need to be able to allocate from the reserves for RECLAIM_SWAP
1932          * and we also need to be able to write out pages for RECLAIM_WRITE
1933          * and RECLAIM_SWAP.
1934          */
1935         p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
1936         reclaim_state.reclaimed_slab = 0;
1937         p->reclaim_state = &reclaim_state;
1938
1939         if (zone_page_state(zone, NR_FILE_PAGES) -
1940                 zone_page_state(zone, NR_FILE_MAPPED) >
1941                 zone->min_unmapped_pages) {
1942                 /*
1943                  * Free memory by calling shrink zone with increasing
1944                  * priorities until we have enough memory freed.
1945                  */
1946                 priority = ZONE_RECLAIM_PRIORITY;
1947                 do {
1948                         note_zone_scanning_priority(zone, priority);
1949                         nr_reclaimed += shrink_zone(priority, zone, &sc);
1950                         priority--;
1951                 } while (priority >= 0 && nr_reclaimed < nr_pages);
1952         }
1953
1954         slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
1955         if (slab_reclaimable > zone->min_slab_pages) {
1956                 /*
1957                  * shrink_slab() does not currently allow us to determine how
1958                  * many pages were freed in this zone. So we take the current
1959                  * number of slab pages and shake the slab until it is reduced
1960                  * by the same nr_pages that we used for reclaiming unmapped
1961                  * pages.
1962                  *
1963                  * Note that shrink_slab will free memory on all zones and may
1964                  * take a long time.
1965                  */
1966                 while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
1967                         zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
1968                                 slab_reclaimable - nr_pages)
1969                         ;
1970
1971                 /*
1972                  * Update nr_reclaimed by the number of slab pages we
1973                  * reclaimed from this zone.
1974                  */
1975                 nr_reclaimed += slab_reclaimable -
1976                         zone_page_state(zone, NR_SLAB_RECLAIMABLE);
1977         }
1978
1979         p->reclaim_state = NULL;
1980         current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
1981         return nr_reclaimed >= nr_pages;
1982 }
1983
1984 int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1985 {
1986         int node_id;
1987         int ret;
1988
1989         /*
1990          * Zone reclaim reclaims unmapped file backed pages and
1991          * slab pages if we are over the defined limits.
1992          *
1993          * A small portion of unmapped file backed pages is needed for
1994          * file I/O otherwise pages read by file I/O will be immediately
1995          * thrown out if the zone is overallocated. So we do not reclaim
1996          * if less than a specified percentage of the zone is used by
1997          * unmapped file backed pages.
1998          */
1999         if (zone_page_state(zone, NR_FILE_PAGES) -
2000             zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
2001             && zone_page_state(zone, NR_SLAB_RECLAIMABLE)
2002                         <= zone->min_slab_pages)
2003                 return 0;
2004
2005         if (zone_is_all_unreclaimable(zone))
2006                 return 0;
2007
2008         /*
2009          * Do not scan if the allocation should not be delayed.
2010          */
2011         if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
2012                         return 0;
2013
2014         /*
2015          * Only run zone reclaim on the local zone or on zones that do not
2016          * have associated processors. This will favor the local processor
2017          * over remote processors and spread off node memory allocations
2018          * as wide as possible.
2019          */
2020         node_id = zone_to_nid(zone);
2021         if (node_state(node_id, N_CPU) && node_id != numa_node_id())
2022                 return 0;
2023
2024         if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
2025                 return 0;
2026         ret = __zone_reclaim(zone, gfp_mask, order);
2027         zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
2028
2029         return ret;
2030 }
2031 #endif