kswapd should only wait on IO if there is IO
[deliverable/linux.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
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 int node;
1369 struct zone **zones;
1370 int target_zone = gfp_zone(GFP_HIGHUSER_MOVABLE);
1371
1372 for_each_online_node(node) {
1373 zones = NODE_DATA(node)->node_zonelists[target_zone].zones;
1374 if (do_try_to_free_pages(zones, sc.gfp_mask, &sc))
1375 return 1;
1376 }
1377 return 0;
1378 }
1379 #endif
1380
1381 /*
1382 * For kswapd, balance_pgdat() will work across all this node's zones until
1383 * they are all at pages_high.
1384 *
1385 * Returns the number of pages which were actually freed.
1386 *
1387 * There is special handling here for zones which are full of pinned pages.
1388 * This can happen if the pages are all mlocked, or if they are all used by
1389 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1390 * What we do is to detect the case where all pages in the zone have been
1391 * scanned twice and there has been zero successful reclaim. Mark the zone as
1392 * dead and from now on, only perform a short scan. Basically we're polling
1393 * the zone for when the problem goes away.
1394 *
1395 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1396 * zones which have free_pages > pages_high, but once a zone is found to have
1397 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1398 * of the number of free pages in the lower zones. This interoperates with
1399 * the page allocator fallback scheme to ensure that aging of pages is balanced
1400 * across the zones.
1401 */
1402 static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
1403 {
1404 int all_zones_ok;
1405 int priority;
1406 int i;
1407 unsigned long total_scanned;
1408 unsigned long nr_reclaimed;
1409 struct reclaim_state *reclaim_state = current->reclaim_state;
1410 struct scan_control sc = {
1411 .gfp_mask = GFP_KERNEL,
1412 .may_swap = 1,
1413 .swap_cluster_max = SWAP_CLUSTER_MAX,
1414 .swappiness = vm_swappiness,
1415 .order = order,
1416 .mem_cgroup = NULL,
1417 .isolate_pages = isolate_pages_global,
1418 };
1419 /*
1420 * temp_priority is used to remember the scanning priority at which
1421 * this zone was successfully refilled to free_pages == pages_high.
1422 */
1423 int temp_priority[MAX_NR_ZONES];
1424
1425 loop_again:
1426 total_scanned = 0;
1427 nr_reclaimed = 0;
1428 sc.may_writepage = !laptop_mode;
1429 count_vm_event(PAGEOUTRUN);
1430
1431 for (i = 0; i < pgdat->nr_zones; i++)
1432 temp_priority[i] = DEF_PRIORITY;
1433
1434 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1435 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1436 unsigned long lru_pages = 0;
1437
1438 /* The swap token gets in the way of swapout... */
1439 if (!priority)
1440 disable_swap_token();
1441
1442 sc.nr_io_pages = 0;
1443 all_zones_ok = 1;
1444
1445 /*
1446 * Scan in the highmem->dma direction for the highest
1447 * zone which needs scanning
1448 */
1449 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1450 struct zone *zone = pgdat->node_zones + i;
1451
1452 if (!populated_zone(zone))
1453 continue;
1454
1455 if (zone_is_all_unreclaimable(zone) &&
1456 priority != DEF_PRIORITY)
1457 continue;
1458
1459 if (!zone_watermark_ok(zone, order, zone->pages_high,
1460 0, 0)) {
1461 end_zone = i;
1462 break;
1463 }
1464 }
1465 if (i < 0)
1466 goto out;
1467
1468 for (i = 0; i <= end_zone; i++) {
1469 struct zone *zone = pgdat->node_zones + i;
1470
1471 lru_pages += zone_page_state(zone, NR_ACTIVE)
1472 + zone_page_state(zone, NR_INACTIVE);
1473 }
1474
1475 /*
1476 * Now scan the zone in the dma->highmem direction, stopping
1477 * at the last zone which needs scanning.
1478 *
1479 * We do this because the page allocator works in the opposite
1480 * direction. This prevents the page allocator from allocating
1481 * pages behind kswapd's direction of progress, which would
1482 * cause too much scanning of the lower zones.
1483 */
1484 for (i = 0; i <= end_zone; i++) {
1485 struct zone *zone = pgdat->node_zones + i;
1486 int nr_slab;
1487
1488 if (!populated_zone(zone))
1489 continue;
1490
1491 if (zone_is_all_unreclaimable(zone) &&
1492 priority != DEF_PRIORITY)
1493 continue;
1494
1495 if (!zone_watermark_ok(zone, order, zone->pages_high,
1496 end_zone, 0))
1497 all_zones_ok = 0;
1498 temp_priority[i] = priority;
1499 sc.nr_scanned = 0;
1500 note_zone_scanning_priority(zone, priority);
1501 /*
1502 * We put equal pressure on every zone, unless one
1503 * zone has way too many pages free already.
1504 */
1505 if (!zone_watermark_ok(zone, order, 8*zone->pages_high,
1506 end_zone, 0))
1507 nr_reclaimed += shrink_zone(priority, zone, &sc);
1508 reclaim_state->reclaimed_slab = 0;
1509 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1510 lru_pages);
1511 nr_reclaimed += reclaim_state->reclaimed_slab;
1512 total_scanned += sc.nr_scanned;
1513 if (zone_is_all_unreclaimable(zone))
1514 continue;
1515 if (nr_slab == 0 && zone->pages_scanned >=
1516 (zone_page_state(zone, NR_ACTIVE)
1517 + zone_page_state(zone, NR_INACTIVE)) * 6)
1518 zone_set_flag(zone,
1519 ZONE_ALL_UNRECLAIMABLE);
1520 /*
1521 * If we've done a decent amount of scanning and
1522 * the reclaim ratio is low, start doing writepage
1523 * even in laptop mode
1524 */
1525 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
1526 total_scanned > nr_reclaimed + nr_reclaimed / 2)
1527 sc.may_writepage = 1;
1528 }
1529 if (all_zones_ok)
1530 break; /* kswapd: all done */
1531 /*
1532 * OK, kswapd is getting into trouble. Take a nap, then take
1533 * another pass across the zones.
1534 */
1535 if (total_scanned && priority < DEF_PRIORITY - 2 &&
1536 sc.nr_io_pages > sc.swap_cluster_max)
1537 congestion_wait(WRITE, HZ/10);
1538
1539 /*
1540 * We do this so kswapd doesn't build up large priorities for
1541 * example when it is freeing in parallel with allocators. It
1542 * matches the direct reclaim path behaviour in terms of impact
1543 * on zone->*_priority.
1544 */
1545 if (nr_reclaimed >= SWAP_CLUSTER_MAX)
1546 break;
1547 }
1548 out:
1549 /*
1550 * Note within each zone the priority level at which this zone was
1551 * brought into a happy state. So that the next thread which scans this
1552 * zone will start out at that priority level.
1553 */
1554 for (i = 0; i < pgdat->nr_zones; i++) {
1555 struct zone *zone = pgdat->node_zones + i;
1556
1557 zone->prev_priority = temp_priority[i];
1558 }
1559 if (!all_zones_ok) {
1560 cond_resched();
1561
1562 try_to_freeze();
1563
1564 goto loop_again;
1565 }
1566
1567 return nr_reclaimed;
1568 }
1569
1570 /*
1571 * The background pageout daemon, started as a kernel thread
1572 * from the init process.
1573 *
1574 * This basically trickles out pages so that we have _some_
1575 * free memory available even if there is no other activity
1576 * that frees anything up. This is needed for things like routing
1577 * etc, where we otherwise might have all activity going on in
1578 * asynchronous contexts that cannot page things out.
1579 *
1580 * If there are applications that are active memory-allocators
1581 * (most normal use), this basically shouldn't matter.
1582 */
1583 static int kswapd(void *p)
1584 {
1585 unsigned long order;
1586 pg_data_t *pgdat = (pg_data_t*)p;
1587 struct task_struct *tsk = current;
1588 DEFINE_WAIT(wait);
1589 struct reclaim_state reclaim_state = {
1590 .reclaimed_slab = 0,
1591 };
1592 cpumask_t cpumask;
1593
1594 cpumask = node_to_cpumask(pgdat->node_id);
1595 if (!cpus_empty(cpumask))
1596 set_cpus_allowed(tsk, cpumask);
1597 current->reclaim_state = &reclaim_state;
1598
1599 /*
1600 * Tell the memory management that we're a "memory allocator",
1601 * and that if we need more memory we should get access to it
1602 * regardless (see "__alloc_pages()"). "kswapd" should
1603 * never get caught in the normal page freeing logic.
1604 *
1605 * (Kswapd normally doesn't need memory anyway, but sometimes
1606 * you need a small amount of memory in order to be able to
1607 * page out something else, and this flag essentially protects
1608 * us from recursively trying to free more memory as we're
1609 * trying to free the first piece of memory in the first place).
1610 */
1611 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
1612 set_freezable();
1613
1614 order = 0;
1615 for ( ; ; ) {
1616 unsigned long new_order;
1617
1618 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1619 new_order = pgdat->kswapd_max_order;
1620 pgdat->kswapd_max_order = 0;
1621 if (order < new_order) {
1622 /*
1623 * Don't sleep if someone wants a larger 'order'
1624 * allocation
1625 */
1626 order = new_order;
1627 } else {
1628 if (!freezing(current))
1629 schedule();
1630
1631 order = pgdat->kswapd_max_order;
1632 }
1633 finish_wait(&pgdat->kswapd_wait, &wait);
1634
1635 if (!try_to_freeze()) {
1636 /* We can speed up thawing tasks if we don't call
1637 * balance_pgdat after returning from the refrigerator
1638 */
1639 balance_pgdat(pgdat, order);
1640 }
1641 }
1642 return 0;
1643 }
1644
1645 /*
1646 * A zone is low on free memory, so wake its kswapd task to service it.
1647 */
1648 void wakeup_kswapd(struct zone *zone, int order)
1649 {
1650 pg_data_t *pgdat;
1651
1652 if (!populated_zone(zone))
1653 return;
1654
1655 pgdat = zone->zone_pgdat;
1656 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
1657 return;
1658 if (pgdat->kswapd_max_order < order)
1659 pgdat->kswapd_max_order = order;
1660 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
1661 return;
1662 if (!waitqueue_active(&pgdat->kswapd_wait))
1663 return;
1664 wake_up_interruptible(&pgdat->kswapd_wait);
1665 }
1666
1667 #ifdef CONFIG_PM
1668 /*
1669 * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
1670 * from LRU lists system-wide, for given pass and priority, and returns the
1671 * number of reclaimed pages
1672 *
1673 * For pass > 3 we also try to shrink the LRU lists that contain a few pages
1674 */
1675 static unsigned long shrink_all_zones(unsigned long nr_pages, int prio,
1676 int pass, struct scan_control *sc)
1677 {
1678 struct zone *zone;
1679 unsigned long nr_to_scan, ret = 0;
1680
1681 for_each_zone(zone) {
1682
1683 if (!populated_zone(zone))
1684 continue;
1685
1686 if (zone_is_all_unreclaimable(zone) && prio != DEF_PRIORITY)
1687 continue;
1688
1689 /* For pass = 0 we don't shrink the active list */
1690 if (pass > 0) {
1691 zone->nr_scan_active +=
1692 (zone_page_state(zone, NR_ACTIVE) >> prio) + 1;
1693 if (zone->nr_scan_active >= nr_pages || pass > 3) {
1694 zone->nr_scan_active = 0;
1695 nr_to_scan = min(nr_pages,
1696 zone_page_state(zone, NR_ACTIVE));
1697 shrink_active_list(nr_to_scan, zone, sc, prio);
1698 }
1699 }
1700
1701 zone->nr_scan_inactive +=
1702 (zone_page_state(zone, NR_INACTIVE) >> prio) + 1;
1703 if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
1704 zone->nr_scan_inactive = 0;
1705 nr_to_scan = min(nr_pages,
1706 zone_page_state(zone, NR_INACTIVE));
1707 ret += shrink_inactive_list(nr_to_scan, zone, sc);
1708 if (ret >= nr_pages)
1709 return ret;
1710 }
1711 }
1712
1713 return ret;
1714 }
1715
1716 static unsigned long count_lru_pages(void)
1717 {
1718 return global_page_state(NR_ACTIVE) + global_page_state(NR_INACTIVE);
1719 }
1720
1721 /*
1722 * Try to free `nr_pages' of memory, system-wide, and return the number of
1723 * freed pages.
1724 *
1725 * Rather than trying to age LRUs the aim is to preserve the overall
1726 * LRU order by reclaiming preferentially
1727 * inactive > active > active referenced > active mapped
1728 */
1729 unsigned long shrink_all_memory(unsigned long nr_pages)
1730 {
1731 unsigned long lru_pages, nr_slab;
1732 unsigned long ret = 0;
1733 int pass;
1734 struct reclaim_state reclaim_state;
1735 struct scan_control sc = {
1736 .gfp_mask = GFP_KERNEL,
1737 .may_swap = 0,
1738 .swap_cluster_max = nr_pages,
1739 .may_writepage = 1,
1740 .swappiness = vm_swappiness,
1741 .isolate_pages = isolate_pages_global,
1742 };
1743
1744 current->reclaim_state = &reclaim_state;
1745
1746 lru_pages = count_lru_pages();
1747 nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
1748 /* If slab caches are huge, it's better to hit them first */
1749 while (nr_slab >= lru_pages) {
1750 reclaim_state.reclaimed_slab = 0;
1751 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1752 if (!reclaim_state.reclaimed_slab)
1753 break;
1754
1755 ret += reclaim_state.reclaimed_slab;
1756 if (ret >= nr_pages)
1757 goto out;
1758
1759 nr_slab -= reclaim_state.reclaimed_slab;
1760 }
1761
1762 /*
1763 * We try to shrink LRUs in 5 passes:
1764 * 0 = Reclaim from inactive_list only
1765 * 1 = Reclaim from active list but don't reclaim mapped
1766 * 2 = 2nd pass of type 1
1767 * 3 = Reclaim mapped (normal reclaim)
1768 * 4 = 2nd pass of type 3
1769 */
1770 for (pass = 0; pass < 5; pass++) {
1771 int prio;
1772
1773 /* Force reclaiming mapped pages in the passes #3 and #4 */
1774 if (pass > 2) {
1775 sc.may_swap = 1;
1776 sc.swappiness = 100;
1777 }
1778
1779 for (prio = DEF_PRIORITY; prio >= 0; prio--) {
1780 unsigned long nr_to_scan = nr_pages - ret;
1781
1782 sc.nr_scanned = 0;
1783 ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
1784 if (ret >= nr_pages)
1785 goto out;
1786
1787 reclaim_state.reclaimed_slab = 0;
1788 shrink_slab(sc.nr_scanned, sc.gfp_mask,
1789 count_lru_pages());
1790 ret += reclaim_state.reclaimed_slab;
1791 if (ret >= nr_pages)
1792 goto out;
1793
1794 if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
1795 congestion_wait(WRITE, HZ / 10);
1796 }
1797 }
1798
1799 /*
1800 * If ret = 0, we could not shrink LRUs, but there may be something
1801 * in slab caches
1802 */
1803 if (!ret) {
1804 do {
1805 reclaim_state.reclaimed_slab = 0;
1806 shrink_slab(nr_pages, sc.gfp_mask, count_lru_pages());
1807 ret += reclaim_state.reclaimed_slab;
1808 } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
1809 }
1810
1811 out:
1812 current->reclaim_state = NULL;
1813
1814 return ret;
1815 }
1816 #endif
1817
1818 /* It's optimal to keep kswapds on the same CPUs as their memory, but
1819 not required for correctness. So if the last cpu in a node goes
1820 away, we get changed to run anywhere: as the first one comes back,
1821 restore their cpu bindings. */
1822 static int __devinit cpu_callback(struct notifier_block *nfb,
1823 unsigned long action, void *hcpu)
1824 {
1825 pg_data_t *pgdat;
1826 cpumask_t mask;
1827 int nid;
1828
1829 if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
1830 for_each_node_state(nid, N_HIGH_MEMORY) {
1831 pgdat = NODE_DATA(nid);
1832 mask = node_to_cpumask(pgdat->node_id);
1833 if (any_online_cpu(mask) != NR_CPUS)
1834 /* One of our CPUs online: restore mask */
1835 set_cpus_allowed(pgdat->kswapd, mask);
1836 }
1837 }
1838 return NOTIFY_OK;
1839 }
1840
1841 /*
1842 * This kswapd start function will be called by init and node-hot-add.
1843 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
1844 */
1845 int kswapd_run(int nid)
1846 {
1847 pg_data_t *pgdat = NODE_DATA(nid);
1848 int ret = 0;
1849
1850 if (pgdat->kswapd)
1851 return 0;
1852
1853 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
1854 if (IS_ERR(pgdat->kswapd)) {
1855 /* failure at boot is fatal */
1856 BUG_ON(system_state == SYSTEM_BOOTING);
1857 printk("Failed to start kswapd on node %d\n",nid);
1858 ret = -1;
1859 }
1860 return ret;
1861 }
1862
1863 static int __init kswapd_init(void)
1864 {
1865 int nid;
1866
1867 swap_setup();
1868 for_each_node_state(nid, N_HIGH_MEMORY)
1869 kswapd_run(nid);
1870 hotcpu_notifier(cpu_callback, 0);
1871 return 0;
1872 }
1873
1874 module_init(kswapd_init)
1875
1876 #ifdef CONFIG_NUMA
1877 /*
1878 * Zone reclaim mode
1879 *
1880 * If non-zero call zone_reclaim when the number of free pages falls below
1881 * the watermarks.
1882 */
1883 int zone_reclaim_mode __read_mostly;
1884
1885 #define RECLAIM_OFF 0
1886 #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1887 #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1888 #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
1889
1890 /*
1891 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1892 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1893 * a zone.
1894 */
1895 #define ZONE_RECLAIM_PRIORITY 4
1896
1897 /*
1898 * Percentage of pages in a zone that must be unmapped for zone_reclaim to
1899 * occur.
1900 */
1901 int sysctl_min_unmapped_ratio = 1;
1902
1903 /*
1904 * If the number of slab pages in a zone grows beyond this percentage then
1905 * slab reclaim needs to occur.
1906 */
1907 int sysctl_min_slab_ratio = 5;
1908
1909 /*
1910 * Try to free up some pages from this zone through reclaim.
1911 */
1912 static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1913 {
1914 /* Minimum pages needed in order to stay on node */
1915 const unsigned long nr_pages = 1 << order;
1916 struct task_struct *p = current;
1917 struct reclaim_state reclaim_state;
1918 int priority;
1919 unsigned long nr_reclaimed = 0;
1920 struct scan_control sc = {
1921 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1922 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
1923 .swap_cluster_max = max_t(unsigned long, nr_pages,
1924 SWAP_CLUSTER_MAX),
1925 .gfp_mask = gfp_mask,
1926 .swappiness = vm_swappiness,
1927 .isolate_pages = isolate_pages_global,
1928 };
1929 unsigned long slab_reclaimable;
1930
1931 disable_swap_token();
1932 cond_resched();
1933 /*
1934 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1935 * and we also need to be able to write out pages for RECLAIM_WRITE
1936 * and RECLAIM_SWAP.
1937 */
1938 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
1939 reclaim_state.reclaimed_slab = 0;
1940 p->reclaim_state = &reclaim_state;
1941
1942 if (zone_page_state(zone, NR_FILE_PAGES) -
1943 zone_page_state(zone, NR_FILE_MAPPED) >
1944 zone->min_unmapped_pages) {
1945 /*
1946 * Free memory by calling shrink zone with increasing
1947 * priorities until we have enough memory freed.
1948 */
1949 priority = ZONE_RECLAIM_PRIORITY;
1950 do {
1951 note_zone_scanning_priority(zone, priority);
1952 nr_reclaimed += shrink_zone(priority, zone, &sc);
1953 priority--;
1954 } while (priority >= 0 && nr_reclaimed < nr_pages);
1955 }
1956
1957 slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
1958 if (slab_reclaimable > zone->min_slab_pages) {
1959 /*
1960 * shrink_slab() does not currently allow us to determine how
1961 * many pages were freed in this zone. So we take the current
1962 * number of slab pages and shake the slab until it is reduced
1963 * by the same nr_pages that we used for reclaiming unmapped
1964 * pages.
1965 *
1966 * Note that shrink_slab will free memory on all zones and may
1967 * take a long time.
1968 */
1969 while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
1970 zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
1971 slab_reclaimable - nr_pages)
1972 ;
1973
1974 /*
1975 * Update nr_reclaimed by the number of slab pages we
1976 * reclaimed from this zone.
1977 */
1978 nr_reclaimed += slab_reclaimable -
1979 zone_page_state(zone, NR_SLAB_RECLAIMABLE);
1980 }
1981
1982 p->reclaim_state = NULL;
1983 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
1984 return nr_reclaimed >= nr_pages;
1985 }
1986
1987 int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1988 {
1989 int node_id;
1990 int ret;
1991
1992 /*
1993 * Zone reclaim reclaims unmapped file backed pages and
1994 * slab pages if we are over the defined limits.
1995 *
1996 * A small portion of unmapped file backed pages is needed for
1997 * file I/O otherwise pages read by file I/O will be immediately
1998 * thrown out if the zone is overallocated. So we do not reclaim
1999 * if less than a specified percentage of the zone is used by
2000 * unmapped file backed pages.
2001 */
2002 if (zone_page_state(zone, NR_FILE_PAGES) -
2003 zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
2004 && zone_page_state(zone, NR_SLAB_RECLAIMABLE)
2005 <= zone->min_slab_pages)
2006 return 0;
2007
2008 if (zone_is_all_unreclaimable(zone))
2009 return 0;
2010
2011 /*
2012 * Do not scan if the allocation should not be delayed.
2013 */
2014 if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
2015 return 0;
2016
2017 /*
2018 * Only run zone reclaim on the local zone or on zones that do not
2019 * have associated processors. This will favor the local processor
2020 * over remote processors and spread off node memory allocations
2021 * as wide as possible.
2022 */
2023 node_id = zone_to_nid(zone);
2024 if (node_state(node_id, N_CPU) && node_id != numa_node_id())
2025 return 0;
2026
2027 if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
2028 return 0;
2029 ret = __zone_reclaim(zone, gfp_mask, order);
2030 zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
2031
2032 return ret;
2033 }
2034 #endif
This page took 0.134821 seconds and 6 git commands to generate.