ia64/linux-2.6.18-xen.hg

annotate mm/vmscan.c @ 871:9cbcc9008446

xen/x86: don't initialize cpu_data[]'s apicid field on generic code

Afaict, this is not only redundant with the intialization done in
drivers/xen/core/smpboot.c, but actually results - at least for
secondary CPUs - in the Xen-specific value written to be later
overwritten with whatever the generic code determines (with no
guarantee that the two values are identical).

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu May 14 10:09:15 2009 +0100 (2009-05-14)
parents 3e8752eb6d9c
children
rev   line source
ian@0 1 /*
ian@0 2 * linux/mm/vmscan.c
ian@0 3 *
ian@0 4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
ian@0 5 *
ian@0 6 * Swap reorganised 29.12.95, Stephen Tweedie.
ian@0 7 * kswapd added: 7.1.96 sct
ian@0 8 * Removed kswapd_ctl limits, and swap out as many pages as needed
ian@0 9 * to bring the system back to freepages.high: 2.4.97, Rik van Riel.
ian@0 10 * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com).
ian@0 11 * Multiqueue VM started 5.8.00, Rik van Riel.
ian@0 12 */
ian@0 13
ian@0 14 #include <linux/mm.h>
ian@0 15 #include <linux/module.h>
ian@0 16 #include <linux/slab.h>
ian@0 17 #include <linux/kernel_stat.h>
ian@0 18 #include <linux/swap.h>
ian@0 19 #include <linux/pagemap.h>
ian@0 20 #include <linux/init.h>
ian@0 21 #include <linux/highmem.h>
ian@0 22 #include <linux/file.h>
ian@0 23 #include <linux/writeback.h>
ian@0 24 #include <linux/blkdev.h>
ian@0 25 #include <linux/buffer_head.h> /* for try_to_release_page(),
ian@0 26 buffer_heads_over_limit */
ian@0 27 #include <linux/mm_inline.h>
ian@0 28 #include <linux/pagevec.h>
ian@0 29 #include <linux/backing-dev.h>
ian@0 30 #include <linux/rmap.h>
ian@0 31 #include <linux/topology.h>
ian@0 32 #include <linux/cpu.h>
ian@0 33 #include <linux/cpuset.h>
ian@0 34 #include <linux/notifier.h>
ian@0 35 #include <linux/rwsem.h>
ian@0 36 #include <linux/delay.h>
ian@0 37 #include <linux/kthread.h>
ian@0 38
ian@0 39 #include <asm/tlbflush.h>
ian@0 40 #include <asm/div64.h>
ian@0 41
ian@0 42 #include <linux/swapops.h>
ian@0 43
ian@0 44 #include "internal.h"
ian@0 45
ian@0 46 struct scan_control {
ian@0 47 /* Incremented by the number of inactive pages that were scanned */
ian@0 48 unsigned long nr_scanned;
ian@0 49
ian@0 50 /* This context's GFP mask */
ian@0 51 gfp_t gfp_mask;
ian@0 52
ian@0 53 int may_writepage;
ian@0 54
ian@0 55 /* Can pages be swapped as part of reclaim? */
ian@0 56 int may_swap;
ian@0 57
ian@0 58 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
ian@0 59 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
ian@0 60 * In this context, it doesn't matter that we scan the
ian@0 61 * whole list at once. */
ian@0 62 int swap_cluster_max;
ian@0 63
ian@0 64 int swappiness;
ian@0 65 };
ian@0 66
ian@0 67 /*
ian@0 68 * The list of shrinker callbacks used by to apply pressure to
ian@0 69 * ageable caches.
ian@0 70 */
ian@0 71 struct shrinker {
ian@0 72 shrinker_t shrinker;
ian@0 73 struct list_head list;
ian@0 74 int seeks; /* seeks to recreate an obj */
ian@0 75 long nr; /* objs pending delete */
ian@0 76 };
ian@0 77
ian@0 78 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
ian@0 79
ian@0 80 #ifdef ARCH_HAS_PREFETCH
ian@0 81 #define prefetch_prev_lru_page(_page, _base, _field) \
ian@0 82 do { \
ian@0 83 if ((_page)->lru.prev != _base) { \
ian@0 84 struct page *prev; \
ian@0 85 \
ian@0 86 prev = lru_to_page(&(_page->lru)); \
ian@0 87 prefetch(&prev->_field); \
ian@0 88 } \
ian@0 89 } while (0)
ian@0 90 #else
ian@0 91 #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
ian@0 92 #endif
ian@0 93
ian@0 94 #ifdef ARCH_HAS_PREFETCHW
ian@0 95 #define prefetchw_prev_lru_page(_page, _base, _field) \
ian@0 96 do { \
ian@0 97 if ((_page)->lru.prev != _base) { \
ian@0 98 struct page *prev; \
ian@0 99 \
ian@0 100 prev = lru_to_page(&(_page->lru)); \
ian@0 101 prefetchw(&prev->_field); \
ian@0 102 } \
ian@0 103 } while (0)
ian@0 104 #else
ian@0 105 #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
ian@0 106 #endif
ian@0 107
ian@0 108 /*
ian@0 109 * From 0 .. 100. Higher means more swappy.
ian@0 110 */
ian@0 111 int vm_swappiness = 60;
ian@0 112 long vm_total_pages; /* The total number of pages which the VM controls */
ian@0 113
ian@0 114 static LIST_HEAD(shrinker_list);
ian@0 115 static DECLARE_RWSEM(shrinker_rwsem);
ian@0 116
ian@0 117 /*
ian@0 118 * Add a shrinker callback to be called from the vm
ian@0 119 */
ian@0 120 struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
ian@0 121 {
ian@0 122 struct shrinker *shrinker;
ian@0 123
ian@0 124 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
ian@0 125 if (shrinker) {
ian@0 126 shrinker->shrinker = theshrinker;
ian@0 127 shrinker->seeks = seeks;
ian@0 128 shrinker->nr = 0;
ian@0 129 down_write(&shrinker_rwsem);
ian@0 130 list_add_tail(&shrinker->list, &shrinker_list);
ian@0 131 up_write(&shrinker_rwsem);
ian@0 132 }
ian@0 133 return shrinker;
ian@0 134 }
ian@0 135 EXPORT_SYMBOL(set_shrinker);
ian@0 136
ian@0 137 /*
ian@0 138 * Remove one
ian@0 139 */
ian@0 140 void remove_shrinker(struct shrinker *shrinker)
ian@0 141 {
ian@0 142 down_write(&shrinker_rwsem);
ian@0 143 list_del(&shrinker->list);
ian@0 144 up_write(&shrinker_rwsem);
ian@0 145 kfree(shrinker);
ian@0 146 }
ian@0 147 EXPORT_SYMBOL(remove_shrinker);
ian@0 148
ian@0 149 #define SHRINK_BATCH 128
ian@0 150 /*
ian@0 151 * Call the shrink functions to age shrinkable caches
ian@0 152 *
ian@0 153 * Here we assume it costs one seek to replace a lru page and that it also
ian@0 154 * takes a seek to recreate a cache object. With this in mind we age equal
ian@0 155 * percentages of the lru and ageable caches. This should balance the seeks
ian@0 156 * generated by these structures.
ian@0 157 *
ian@0 158 * If the vm encounted mapped pages on the LRU it increase the pressure on
ian@0 159 * slab to avoid swapping.
ian@0 160 *
ian@0 161 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
ian@0 162 *
ian@0 163 * `lru_pages' represents the number of on-LRU pages in all the zones which
ian@0 164 * are eligible for the caller's allocation attempt. It is used for balancing
ian@0 165 * slab reclaim versus page reclaim.
ian@0 166 *
ian@0 167 * Returns the number of slab objects which we shrunk.
ian@0 168 */
ian@0 169 unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
ian@0 170 unsigned long lru_pages)
ian@0 171 {
ian@0 172 struct shrinker *shrinker;
ian@0 173 unsigned long ret = 0;
ian@0 174
ian@0 175 if (scanned == 0)
ian@0 176 scanned = SWAP_CLUSTER_MAX;
ian@0 177
ian@0 178 if (!down_read_trylock(&shrinker_rwsem))
ian@0 179 return 1; /* Assume we'll be able to shrink next time */
ian@0 180
ian@0 181 list_for_each_entry(shrinker, &shrinker_list, list) {
ian@0 182 unsigned long long delta;
ian@0 183 unsigned long total_scan;
ian@0 184 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
ian@0 185
ian@0 186 delta = (4 * scanned) / shrinker->seeks;
ian@0 187 delta *= max_pass;
ian@0 188 do_div(delta, lru_pages + 1);
ian@0 189 shrinker->nr += delta;
ian@0 190 if (shrinker->nr < 0) {
ian@0 191 printk(KERN_ERR "%s: nr=%ld\n",
ian@0 192 __FUNCTION__, shrinker->nr);
ian@0 193 shrinker->nr = max_pass;
ian@0 194 }
ian@0 195
ian@0 196 /*
ian@0 197 * Avoid risking looping forever due to too large nr value:
ian@0 198 * never try to free more than twice the estimate number of
ian@0 199 * freeable entries.
ian@0 200 */
ian@0 201 if (shrinker->nr > max_pass * 2)
ian@0 202 shrinker->nr = max_pass * 2;
ian@0 203
ian@0 204 total_scan = shrinker->nr;
ian@0 205 shrinker->nr = 0;
ian@0 206
ian@0 207 while (total_scan >= SHRINK_BATCH) {
ian@0 208 long this_scan = SHRINK_BATCH;
ian@0 209 int shrink_ret;
ian@0 210 int nr_before;
ian@0 211
ian@0 212 nr_before = (*shrinker->shrinker)(0, gfp_mask);
ian@0 213 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
ian@0 214 if (shrink_ret == -1)
ian@0 215 break;
ian@0 216 if (shrink_ret < nr_before)
ian@0 217 ret += nr_before - shrink_ret;
ian@0 218 count_vm_events(SLABS_SCANNED, this_scan);
ian@0 219 total_scan -= this_scan;
ian@0 220
ian@0 221 cond_resched();
ian@0 222 }
ian@0 223
ian@0 224 shrinker->nr += total_scan;
ian@0 225 }
ian@0 226 up_read(&shrinker_rwsem);
ian@0 227 return ret;
ian@0 228 }
ian@0 229
ian@0 230 /* Called without lock on whether page is mapped, so answer is unstable */
ian@0 231 static inline int page_mapping_inuse(struct page *page)
ian@0 232 {
ian@0 233 struct address_space *mapping;
ian@0 234
ian@0 235 /* Page is in somebody's page tables. */
ian@0 236 if (page_mapped(page))
ian@0 237 return 1;
ian@0 238
ian@0 239 /* Be more reluctant to reclaim swapcache than pagecache */
ian@0 240 if (PageSwapCache(page))
ian@0 241 return 1;
ian@0 242
ian@0 243 mapping = page_mapping(page);
ian@0 244 if (!mapping)
ian@0 245 return 0;
ian@0 246
ian@0 247 /* File is mmap'd by somebody? */
ian@0 248 return mapping_mapped(mapping);
ian@0 249 }
ian@0 250
ian@0 251 static inline int is_page_cache_freeable(struct page *page)
ian@0 252 {
ian@0 253 return page_count(page) - !!PagePrivate(page) == 2;
ian@0 254 }
ian@0 255
ian@0 256 static int may_write_to_queue(struct backing_dev_info *bdi)
ian@0 257 {
ian@0 258 if (current->flags & PF_SWAPWRITE)
ian@0 259 return 1;
ian@0 260 if (!bdi_write_congested(bdi))
ian@0 261 return 1;
ian@0 262 if (bdi == current->backing_dev_info)
ian@0 263 return 1;
ian@0 264 return 0;
ian@0 265 }
ian@0 266
ian@0 267 /*
ian@0 268 * We detected a synchronous write error writing a page out. Probably
ian@0 269 * -ENOSPC. We need to propagate that into the address_space for a subsequent
ian@0 270 * fsync(), msync() or close().
ian@0 271 *
ian@0 272 * The tricky part is that after writepage we cannot touch the mapping: nothing
ian@0 273 * prevents it from being freed up. But we have a ref on the page and once
ian@0 274 * that page is locked, the mapping is pinned.
ian@0 275 *
ian@0 276 * We're allowed to run sleeping lock_page() here because we know the caller has
ian@0 277 * __GFP_FS.
ian@0 278 */
ian@0 279 static void handle_write_error(struct address_space *mapping,
ian@0 280 struct page *page, int error)
ian@0 281 {
ian@0 282 lock_page(page);
ian@0 283 if (page_mapping(page) == mapping) {
ian@0 284 if (error == -ENOSPC)
ian@0 285 set_bit(AS_ENOSPC, &mapping->flags);
ian@0 286 else
ian@0 287 set_bit(AS_EIO, &mapping->flags);
ian@0 288 }
ian@0 289 unlock_page(page);
ian@0 290 }
ian@0 291
ian@0 292 /* possible outcome of pageout() */
ian@0 293 typedef enum {
ian@0 294 /* failed to write page out, page is locked */
ian@0 295 PAGE_KEEP,
ian@0 296 /* move page to the active list, page is locked */
ian@0 297 PAGE_ACTIVATE,
ian@0 298 /* page has been sent to the disk successfully, page is unlocked */
ian@0 299 PAGE_SUCCESS,
ian@0 300 /* page is clean and locked */
ian@0 301 PAGE_CLEAN,
ian@0 302 } pageout_t;
ian@0 303
ian@0 304 /*
ian@0 305 * pageout is called by shrink_page_list() for each dirty page.
ian@0 306 * Calls ->writepage().
ian@0 307 */
ian@0 308 static pageout_t pageout(struct page *page, struct address_space *mapping)
ian@0 309 {
ian@0 310 /*
ian@0 311 * If the page is dirty, only perform writeback if that write
ian@0 312 * will be non-blocking. To prevent this allocation from being
ian@0 313 * stalled by pagecache activity. But note that there may be
ian@0 314 * stalls if we need to run get_block(). We could test
ian@0 315 * PagePrivate for that.
ian@0 316 *
ian@0 317 * If this process is currently in generic_file_write() against
ian@0 318 * this page's queue, we can perform writeback even if that
ian@0 319 * will block.
ian@0 320 *
ian@0 321 * If the page is swapcache, write it back even if that would
ian@0 322 * block, for some throttling. This happens by accident, because
ian@0 323 * swap_backing_dev_info is bust: it doesn't reflect the
ian@0 324 * congestion state of the swapdevs. Easy to fix, if needed.
ian@0 325 * See swapfile.c:page_queue_congested().
ian@0 326 */
ian@0 327 if (!is_page_cache_freeable(page))
ian@0 328 return PAGE_KEEP;
ian@0 329 if (!mapping) {
ian@0 330 /*
ian@0 331 * Some data journaling orphaned pages can have
ian@0 332 * page->mapping == NULL while being dirty with clean buffers.
ian@0 333 */
ian@0 334 if (PagePrivate(page)) {
ian@0 335 if (try_to_free_buffers(page)) {
ian@0 336 ClearPageDirty(page);
ian@0 337 printk("%s: orphaned page\n", __FUNCTION__);
ian@0 338 return PAGE_CLEAN;
ian@0 339 }
ian@0 340 }
ian@0 341 return PAGE_KEEP;
ian@0 342 }
ian@0 343 if (mapping->a_ops->writepage == NULL)
ian@0 344 return PAGE_ACTIVATE;
ian@0 345 if (!may_write_to_queue(mapping->backing_dev_info))
ian@0 346 return PAGE_KEEP;
ian@0 347
ian@0 348 if (clear_page_dirty_for_io(page)) {
ian@0 349 int res;
ian@0 350 struct writeback_control wbc = {
ian@0 351 .sync_mode = WB_SYNC_NONE,
ian@0 352 .nr_to_write = SWAP_CLUSTER_MAX,
ian@0 353 .range_start = 0,
ian@0 354 .range_end = LLONG_MAX,
ian@0 355 .nonblocking = 1,
ian@0 356 .for_reclaim = 1,
ian@0 357 };
ian@0 358
ian@0 359 SetPageReclaim(page);
ian@0 360 res = mapping->a_ops->writepage(page, &wbc);
ian@0 361 if (res < 0)
ian@0 362 handle_write_error(mapping, page, res);
ian@0 363 if (res == AOP_WRITEPAGE_ACTIVATE) {
ian@0 364 ClearPageReclaim(page);
ian@0 365 return PAGE_ACTIVATE;
ian@0 366 }
ian@0 367 if (!PageWriteback(page)) {
ian@0 368 /* synchronous write or broken a_ops? */
ian@0 369 ClearPageReclaim(page);
ian@0 370 }
ian@0 371
ian@0 372 return PAGE_SUCCESS;
ian@0 373 }
ian@0 374
ian@0 375 return PAGE_CLEAN;
ian@0 376 }
ian@0 377
ian@0 378 int remove_mapping(struct address_space *mapping, struct page *page)
ian@0 379 {
ian@0 380 if (!mapping)
ian@0 381 return 0; /* truncate got there first */
ian@0 382
ian@0 383 write_lock_irq(&mapping->tree_lock);
ian@0 384
ian@0 385 /*
ian@0 386 * The non-racy check for busy page. It is critical to check
ian@0 387 * PageDirty _after_ making sure that the page is freeable and
ian@0 388 * not in use by anybody. (pagecache + us == 2)
ian@0 389 */
ian@0 390 if (unlikely(page_count(page) != 2))
ian@0 391 goto cannot_free;
ian@0 392 smp_rmb();
ian@0 393 if (unlikely(PageDirty(page)))
ian@0 394 goto cannot_free;
ian@0 395
ian@0 396 if (PageSwapCache(page)) {
ian@0 397 swp_entry_t swap = { .val = page_private(page) };
ian@0 398 __delete_from_swap_cache(page);
ian@0 399 write_unlock_irq(&mapping->tree_lock);
ian@0 400 swap_free(swap);
ian@0 401 __put_page(page); /* The pagecache ref */
ian@0 402 return 1;
ian@0 403 }
ian@0 404
ian@0 405 __remove_from_page_cache(page);
ian@0 406 write_unlock_irq(&mapping->tree_lock);
ian@0 407 __put_page(page);
ian@0 408 return 1;
ian@0 409
ian@0 410 cannot_free:
ian@0 411 write_unlock_irq(&mapping->tree_lock);
ian@0 412 return 0;
ian@0 413 }
ian@0 414
ian@0 415 /*
ian@0 416 * shrink_page_list() returns the number of reclaimed pages
ian@0 417 */
ian@0 418 static unsigned long shrink_page_list(struct list_head *page_list,
ian@0 419 struct scan_control *sc)
ian@0 420 {
ian@0 421 LIST_HEAD(ret_pages);
ian@0 422 struct pagevec freed_pvec;
ian@0 423 int pgactivate = 0;
ian@0 424 unsigned long nr_reclaimed = 0;
ian@0 425
ian@0 426 cond_resched();
ian@0 427
ian@0 428 pagevec_init(&freed_pvec, 1);
ian@0 429 while (!list_empty(page_list)) {
ian@0 430 struct address_space *mapping;
ian@0 431 struct page *page;
ian@0 432 int may_enter_fs;
ian@0 433 int referenced;
ian@0 434
ian@0 435 cond_resched();
ian@0 436
ian@0 437 page = lru_to_page(page_list);
ian@0 438 list_del(&page->lru);
ian@0 439
ian@0 440 if (TestSetPageLocked(page))
ian@0 441 goto keep;
ian@0 442
ian@0 443 BUG_ON(PageActive(page));
ian@0 444
ian@0 445 sc->nr_scanned++;
ian@0 446
ian@0 447 if (!sc->may_swap && page_mapped(page))
ian@0 448 goto keep_locked;
ian@0 449
ian@0 450 /* Double the slab pressure for mapped and swapcache pages */
ian@0 451 if (page_mapped(page) || PageSwapCache(page))
ian@0 452 sc->nr_scanned++;
ian@0 453
ian@0 454 if (PageWriteback(page))
ian@0 455 goto keep_locked;
ian@0 456
ian@0 457 referenced = page_referenced(page, 1);
ian@0 458 /* In active use or really unfreeable? Activate it. */
ian@0 459 if (referenced && page_mapping_inuse(page))
ian@0 460 goto activate_locked;
ian@0 461
ian@0 462 #ifdef CONFIG_SWAP
ian@0 463 /*
ian@0 464 * Anonymous process memory has backing store?
ian@0 465 * Try to allocate it some swap space here.
ian@0 466 */
ian@0 467 if (PageAnon(page) && !PageSwapCache(page))
ian@0 468 if (!add_to_swap(page, GFP_ATOMIC))
ian@0 469 goto activate_locked;
ian@0 470 #endif /* CONFIG_SWAP */
ian@0 471
ian@0 472 mapping = page_mapping(page);
ian@0 473 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
ian@0 474 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
ian@0 475
ian@0 476 /*
ian@0 477 * The page is mapped into the page tables of one or more
ian@0 478 * processes. Try to unmap it here.
ian@0 479 */
ian@0 480 if (page_mapped(page) && mapping) {
ian@0 481 switch (try_to_unmap(page, 0)) {
ian@0 482 case SWAP_FAIL:
ian@0 483 goto activate_locked;
ian@0 484 case SWAP_AGAIN:
ian@0 485 goto keep_locked;
ian@0 486 case SWAP_SUCCESS:
ian@0 487 ; /* try to free the page below */
ian@0 488 }
ian@0 489 }
ian@0 490
ian@0 491 if (PageDirty(page)) {
ian@0 492 if (referenced)
ian@0 493 goto keep_locked;
ian@0 494 if (!may_enter_fs)
ian@0 495 goto keep_locked;
ian@0 496 if (!sc->may_writepage)
ian@0 497 goto keep_locked;
ian@0 498
ian@0 499 /* Page is dirty, try to write it out here */
ian@0 500 switch(pageout(page, mapping)) {
ian@0 501 case PAGE_KEEP:
ian@0 502 goto keep_locked;
ian@0 503 case PAGE_ACTIVATE:
ian@0 504 goto activate_locked;
ian@0 505 case PAGE_SUCCESS:
ian@0 506 if (PageWriteback(page) || PageDirty(page))
ian@0 507 goto keep;
ian@0 508 /*
ian@0 509 * A synchronous write - probably a ramdisk. Go
ian@0 510 * ahead and try to reclaim the page.
ian@0 511 */
ian@0 512 if (TestSetPageLocked(page))
ian@0 513 goto keep;
ian@0 514 if (PageDirty(page) || PageWriteback(page))
ian@0 515 goto keep_locked;
ian@0 516 mapping = page_mapping(page);
ian@0 517 case PAGE_CLEAN:
ian@0 518 ; /* try to free the page below */
ian@0 519 }
ian@0 520 }
ian@0 521
ian@0 522 /*
ian@0 523 * If the page has buffers, try to free the buffer mappings
ian@0 524 * associated with this page. If we succeed we try to free
ian@0 525 * the page as well.
ian@0 526 *
ian@0 527 * We do this even if the page is PageDirty().
ian@0 528 * try_to_release_page() does not perform I/O, but it is
ian@0 529 * possible for a page to have PageDirty set, but it is actually
ian@0 530 * clean (all its buffers are clean). This happens if the
ian@0 531 * buffers were written out directly, with submit_bh(). ext3
ian@0 532 * will do this, as well as the blockdev mapping.
ian@0 533 * try_to_release_page() will discover that cleanness and will
ian@0 534 * drop the buffers and mark the page clean - it can be freed.
ian@0 535 *
ian@0 536 * Rarely, pages can have buffers and no ->mapping. These are
ian@0 537 * the pages which were not successfully invalidated in
ian@0 538 * truncate_complete_page(). We try to drop those buffers here
ian@0 539 * and if that worked, and the page is no longer mapped into
ian@0 540 * process address space (page_count == 1) it can be freed.
ian@0 541 * Otherwise, leave the page on the LRU so it is swappable.
ian@0 542 */
ian@0 543 if (PagePrivate(page)) {
ian@0 544 if (!try_to_release_page(page, sc->gfp_mask))
ian@0 545 goto activate_locked;
ian@0 546 if (!mapping && page_count(page) == 1)
ian@0 547 goto free_it;
ian@0 548 }
ian@0 549
ian@0 550 if (!remove_mapping(mapping, page))
ian@0 551 goto keep_locked;
ian@0 552
ian@0 553 free_it:
ian@0 554 unlock_page(page);
ian@0 555 nr_reclaimed++;
ian@0 556 if (!pagevec_add(&freed_pvec, page))
ian@0 557 __pagevec_release_nonlru(&freed_pvec);
ian@0 558 continue;
ian@0 559
ian@0 560 activate_locked:
ian@0 561 SetPageActive(page);
ian@0 562 pgactivate++;
ian@0 563 keep_locked:
ian@0 564 unlock_page(page);
ian@0 565 keep:
ian@0 566 list_add(&page->lru, &ret_pages);
ian@0 567 BUG_ON(PageLRU(page));
ian@0 568 }
ian@0 569 list_splice(&ret_pages, page_list);
ian@0 570 if (pagevec_count(&freed_pvec))
ian@0 571 __pagevec_release_nonlru(&freed_pvec);
ian@0 572 count_vm_events(PGACTIVATE, pgactivate);
ian@0 573 return nr_reclaimed;
ian@0 574 }
ian@0 575
ian@0 576 /*
ian@0 577 * zone->lru_lock is heavily contended. Some of the functions that
ian@0 578 * shrink the lists perform better by taking out a batch of pages
ian@0 579 * and working on them outside the LRU lock.
ian@0 580 *
ian@0 581 * For pagecache intensive workloads, this function is the hottest
ian@0 582 * spot in the kernel (apart from copy_*_user functions).
ian@0 583 *
ian@0 584 * Appropriate locks must be held before calling this function.
ian@0 585 *
ian@0 586 * @nr_to_scan: The number of pages to look through on the list.
ian@0 587 * @src: The LRU list to pull pages off.
ian@0 588 * @dst: The temp list to put pages on to.
ian@0 589 * @scanned: The number of pages that were scanned.
ian@0 590 *
ian@0 591 * returns how many pages were moved onto *@dst.
ian@0 592 */
ian@0 593 static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
ian@0 594 struct list_head *src, struct list_head *dst,
ian@0 595 unsigned long *scanned)
ian@0 596 {
ian@0 597 unsigned long nr_taken = 0;
ian@0 598 struct page *page;
ian@0 599 unsigned long scan;
ian@0 600
ian@0 601 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
ian@0 602 struct list_head *target;
ian@0 603 page = lru_to_page(src);
ian@0 604 prefetchw_prev_lru_page(page, src, flags);
ian@0 605
ian@0 606 BUG_ON(!PageLRU(page));
ian@0 607
ian@0 608 list_del(&page->lru);
ian@0 609 target = src;
ian@0 610 if (likely(get_page_unless_zero(page))) {
ian@0 611 /*
ian@0 612 * Be careful not to clear PageLRU until after we're
ian@0 613 * sure the page is not being freed elsewhere -- the
ian@0 614 * page release code relies on it.
ian@0 615 */
ian@0 616 ClearPageLRU(page);
ian@0 617 target = dst;
ian@0 618 nr_taken++;
ian@0 619 } /* else it is being freed elsewhere */
ian@0 620
ian@0 621 list_add(&page->lru, target);
ian@0 622 }
ian@0 623
ian@0 624 *scanned = scan;
ian@0 625 return nr_taken;
ian@0 626 }
ian@0 627
ian@0 628 /*
ian@0 629 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
ian@0 630 * of reclaimed pages
ian@0 631 */
ian@0 632 static unsigned long shrink_inactive_list(unsigned long max_scan,
ian@0 633 struct zone *zone, struct scan_control *sc)
ian@0 634 {
ian@0 635 LIST_HEAD(page_list);
ian@0 636 struct pagevec pvec;
ian@0 637 unsigned long nr_scanned = 0;
ian@0 638 unsigned long nr_reclaimed = 0;
ian@0 639
ian@0 640 pagevec_init(&pvec, 1);
ian@0 641
ian@0 642 lru_add_drain();
ian@0 643 spin_lock_irq(&zone->lru_lock);
ian@0 644 do {
ian@0 645 struct page *page;
ian@0 646 unsigned long nr_taken;
ian@0 647 unsigned long nr_scan;
ian@0 648 unsigned long nr_freed;
ian@0 649
ian@0 650 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
ian@0 651 &zone->inactive_list,
ian@0 652 &page_list, &nr_scan);
ian@0 653 zone->nr_inactive -= nr_taken;
ian@0 654 zone->pages_scanned += nr_scan;
ian@0 655 spin_unlock_irq(&zone->lru_lock);
ian@0 656
ian@0 657 nr_scanned += nr_scan;
ian@0 658 nr_freed = shrink_page_list(&page_list, sc);
ian@0 659 nr_reclaimed += nr_freed;
ian@0 660 local_irq_disable();
ian@0 661 if (current_is_kswapd()) {
ian@0 662 __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan);
ian@0 663 __count_vm_events(KSWAPD_STEAL, nr_freed);
ian@0 664 } else
ian@0 665 __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan);
ian@0 666 __count_vm_events(PGACTIVATE, nr_freed);
ian@0 667
ian@0 668 if (nr_taken == 0)
ian@0 669 goto done;
ian@0 670
ian@0 671 spin_lock(&zone->lru_lock);
ian@0 672 /*
ian@0 673 * Put back any unfreeable pages.
ian@0 674 */
ian@0 675 while (!list_empty(&page_list)) {
ian@0 676 page = lru_to_page(&page_list);
ian@0 677 BUG_ON(PageLRU(page));
ian@0 678 SetPageLRU(page);
ian@0 679 list_del(&page->lru);
ian@0 680 if (PageActive(page))
ian@0 681 add_page_to_active_list(zone, page);
ian@0 682 else
ian@0 683 add_page_to_inactive_list(zone, page);
ian@0 684 if (!pagevec_add(&pvec, page)) {
ian@0 685 spin_unlock_irq(&zone->lru_lock);
ian@0 686 __pagevec_release(&pvec);
ian@0 687 spin_lock_irq(&zone->lru_lock);
ian@0 688 }
ian@0 689 }
ian@0 690 } while (nr_scanned < max_scan);
ian@0 691 spin_unlock(&zone->lru_lock);
ian@0 692 done:
ian@0 693 local_irq_enable();
ian@0 694 pagevec_release(&pvec);
ian@0 695 return nr_reclaimed;
ian@0 696 }
ian@0 697
ian@0 698 /*
ian@240 699 * We are about to scan this zone at a certain priority level. If that priority
ian@240 700 * level is smaller (ie: more urgent) than the previous priority, then note
ian@240 701 * that priority level within the zone. This is done so that when the next
ian@240 702 * process comes in to scan this zone, it will immediately start out at this
ian@240 703 * priority level rather than having to build up its own scanning priority.
ian@240 704 * Here, this priority affects only the reclaim-mapped threshold.
ian@240 705 */
ian@240 706 static inline void note_zone_scanning_priority(struct zone *zone, int priority)
ian@240 707 {
ian@240 708 if (priority < zone->prev_priority)
ian@240 709 zone->prev_priority = priority;
ian@240 710 }
ian@240 711
ian@240 712 /*
ian@0 713 * This moves pages from the active list to the inactive list.
ian@0 714 *
ian@0 715 * We move them the other way if the page is referenced by one or more
ian@0 716 * processes, from rmap.
ian@0 717 *
ian@0 718 * If the pages are mostly unmapped, the processing is fast and it is
ian@0 719 * appropriate to hold zone->lru_lock across the whole operation. But if
ian@0 720 * the pages are mapped, the processing is slow (page_referenced()) so we
ian@0 721 * should drop zone->lru_lock around each page. It's impossible to balance
ian@0 722 * this, so instead we remove the pages from the LRU while processing them.
ian@0 723 * It is safe to rely on PG_active against the non-LRU pages in here because
ian@0 724 * nobody will play with that bit on a non-LRU page.
ian@0 725 *
ian@0 726 * The downside is that we have to touch page->_count against each page.
ian@0 727 * But we had to alter page->flags anyway.
ian@0 728 */
ian@0 729 static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
ian@240 730 struct scan_control *sc, int priority)
ian@0 731 {
ian@0 732 unsigned long pgmoved;
ian@0 733 int pgdeactivate = 0;
ian@0 734 unsigned long pgscanned;
ian@0 735 LIST_HEAD(l_hold); /* The pages which were snipped off */
ian@0 736 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
ian@0 737 LIST_HEAD(l_active); /* Pages to go onto the active_list */
ian@0 738 struct page *page;
ian@0 739 struct pagevec pvec;
ian@0 740 int reclaim_mapped = 0;
ian@0 741
ian@0 742 if (sc->may_swap) {
ian@0 743 long mapped_ratio;
ian@0 744 long distress;
ian@0 745 long swap_tendency;
ian@0 746
ian@0 747 /*
ian@0 748 * `distress' is a measure of how much trouble we're having
ian@0 749 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
ian@0 750 */
ian@240 751 distress = 100 >> min(zone->prev_priority, priority);
ian@0 752
ian@0 753 /*
ian@0 754 * The point of this algorithm is to decide when to start
ian@0 755 * reclaiming mapped memory instead of just pagecache. Work out
ian@0 756 * how much memory
ian@0 757 * is mapped.
ian@0 758 */
ian@0 759 mapped_ratio = ((global_page_state(NR_FILE_MAPPED) +
ian@0 760 global_page_state(NR_ANON_PAGES)) * 100) /
ian@0 761 vm_total_pages;
ian@0 762
ian@0 763 /*
ian@0 764 * Now decide how much we really want to unmap some pages. The
ian@0 765 * mapped ratio is downgraded - just because there's a lot of
ian@0 766 * mapped memory doesn't necessarily mean that page reclaim
ian@0 767 * isn't succeeding.
ian@0 768 *
ian@0 769 * The distress ratio is important - we don't want to start
ian@0 770 * going oom.
ian@0 771 *
ian@0 772 * A 100% value of vm_swappiness overrides this algorithm
ian@0 773 * altogether.
ian@0 774 */
ian@0 775 swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
ian@0 776
ian@0 777 /*
ian@0 778 * Now use this metric to decide whether to start moving mapped
ian@0 779 * memory onto the inactive list.
ian@0 780 */
ian@0 781 if (swap_tendency >= 100)
ian@0 782 reclaim_mapped = 1;
ian@0 783 }
ian@0 784
ian@0 785 lru_add_drain();
ian@0 786 spin_lock_irq(&zone->lru_lock);
ian@0 787 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
ian@0 788 &l_hold, &pgscanned);
ian@0 789 zone->pages_scanned += pgscanned;
ian@0 790 zone->nr_active -= pgmoved;
ian@0 791 spin_unlock_irq(&zone->lru_lock);
ian@0 792
ian@0 793 while (!list_empty(&l_hold)) {
ian@0 794 cond_resched();
ian@0 795 page = lru_to_page(&l_hold);
ian@0 796 list_del(&page->lru);
ian@0 797 if (page_mapped(page)) {
ian@0 798 if (!reclaim_mapped ||
ian@0 799 (total_swap_pages == 0 && PageAnon(page)) ||
ian@0 800 page_referenced(page, 0)) {
ian@0 801 list_add(&page->lru, &l_active);
ian@0 802 continue;
ian@0 803 }
ian@0 804 }
ian@0 805 list_add(&page->lru, &l_inactive);
ian@0 806 }
ian@0 807
ian@0 808 pagevec_init(&pvec, 1);
ian@0 809 pgmoved = 0;
ian@0 810 spin_lock_irq(&zone->lru_lock);
ian@0 811 while (!list_empty(&l_inactive)) {
ian@0 812 page = lru_to_page(&l_inactive);
ian@0 813 prefetchw_prev_lru_page(page, &l_inactive, flags);
ian@0 814 BUG_ON(PageLRU(page));
ian@0 815 SetPageLRU(page);
ian@0 816 BUG_ON(!PageActive(page));
ian@0 817 ClearPageActive(page);
ian@0 818
ian@0 819 list_move(&page->lru, &zone->inactive_list);
ian@0 820 pgmoved++;
ian@0 821 if (!pagevec_add(&pvec, page)) {
ian@0 822 zone->nr_inactive += pgmoved;
ian@0 823 spin_unlock_irq(&zone->lru_lock);
ian@0 824 pgdeactivate += pgmoved;
ian@0 825 pgmoved = 0;
ian@0 826 if (buffer_heads_over_limit)
ian@0 827 pagevec_strip(&pvec);
ian@0 828 __pagevec_release(&pvec);
ian@0 829 spin_lock_irq(&zone->lru_lock);
ian@0 830 }
ian@0 831 }
ian@0 832 zone->nr_inactive += pgmoved;
ian@0 833 pgdeactivate += pgmoved;
ian@0 834 if (buffer_heads_over_limit) {
ian@0 835 spin_unlock_irq(&zone->lru_lock);
ian@0 836 pagevec_strip(&pvec);
ian@0 837 spin_lock_irq(&zone->lru_lock);
ian@0 838 }
ian@0 839
ian@0 840 pgmoved = 0;
ian@0 841 while (!list_empty(&l_active)) {
ian@0 842 page = lru_to_page(&l_active);
ian@0 843 prefetchw_prev_lru_page(page, &l_active, flags);
ian@0 844 BUG_ON(PageLRU(page));
ian@0 845 SetPageLRU(page);
ian@0 846 BUG_ON(!PageActive(page));
ian@0 847 list_move(&page->lru, &zone->active_list);
ian@0 848 pgmoved++;
ian@0 849 if (!pagevec_add(&pvec, page)) {
ian@0 850 zone->nr_active += pgmoved;
ian@0 851 pgmoved = 0;
ian@0 852 spin_unlock_irq(&zone->lru_lock);
ian@0 853 __pagevec_release(&pvec);
ian@0 854 spin_lock_irq(&zone->lru_lock);
ian@0 855 }
ian@0 856 }
ian@0 857 zone->nr_active += pgmoved;
ian@0 858
ian@0 859 __count_zone_vm_events(PGREFILL, zone, pgscanned);
ian@0 860 __count_vm_events(PGDEACTIVATE, pgdeactivate);
ian@0 861 spin_unlock_irq(&zone->lru_lock);
ian@0 862
ian@0 863 pagevec_release(&pvec);
ian@0 864 }
ian@0 865
ian@0 866 /*
ian@0 867 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
ian@0 868 */
ian@0 869 static unsigned long shrink_zone(int priority, struct zone *zone,
ian@0 870 struct scan_control *sc)
ian@0 871 {
ian@0 872 unsigned long nr_active;
ian@0 873 unsigned long nr_inactive;
ian@0 874 unsigned long nr_to_scan;
ian@0 875 unsigned long nr_reclaimed = 0;
ian@0 876
ian@0 877 atomic_inc(&zone->reclaim_in_progress);
ian@0 878
ian@0 879 /*
ian@0 880 * Add one to `nr_to_scan' just to make sure that the kernel will
ian@0 881 * slowly sift through the active list.
ian@0 882 */
ian@0 883 zone->nr_scan_active += (zone->nr_active >> priority) + 1;
ian@0 884 nr_active = zone->nr_scan_active;
ian@0 885 if (nr_active >= sc->swap_cluster_max)
ian@0 886 zone->nr_scan_active = 0;
ian@0 887 else
ian@0 888 nr_active = 0;
ian@0 889
ian@0 890 zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1;
ian@0 891 nr_inactive = zone->nr_scan_inactive;
ian@0 892 if (nr_inactive >= sc->swap_cluster_max)
ian@0 893 zone->nr_scan_inactive = 0;
ian@0 894 else
ian@0 895 nr_inactive = 0;
ian@0 896
ian@0 897 while (nr_active || nr_inactive) {
ian@0 898 if (nr_active) {
ian@0 899 nr_to_scan = min(nr_active,
ian@0 900 (unsigned long)sc->swap_cluster_max);
ian@0 901 nr_active -= nr_to_scan;
ian@240 902 shrink_active_list(nr_to_scan, zone, sc, priority);
ian@0 903 }
ian@0 904
ian@0 905 if (nr_inactive) {
ian@0 906 nr_to_scan = min(nr_inactive,
ian@0 907 (unsigned long)sc->swap_cluster_max);
ian@0 908 nr_inactive -= nr_to_scan;
ian@0 909 nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
ian@0 910 sc);
ian@0 911 }
ian@0 912 }
ian@0 913
ian@0 914 throttle_vm_writeout();
ian@0 915
ian@0 916 atomic_dec(&zone->reclaim_in_progress);
ian@0 917 return nr_reclaimed;
ian@0 918 }
ian@0 919
ian@0 920 /*
ian@0 921 * This is the direct reclaim path, for page-allocating processes. We only
ian@0 922 * try to reclaim pages from zones which will satisfy the caller's allocation
ian@0 923 * request.
ian@0 924 *
ian@0 925 * We reclaim from a zone even if that zone is over pages_high. Because:
ian@0 926 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
ian@0 927 * allocation or
ian@0 928 * b) The zones may be over pages_high but they must go *over* pages_high to
ian@0 929 * satisfy the `incremental min' zone defense algorithm.
ian@0 930 *
ian@0 931 * Returns the number of reclaimed pages.
ian@0 932 *
ian@0 933 * If a zone is deemed to be full of pinned pages then just give it a light
ian@0 934 * scan then give up on it.
ian@0 935 */
ian@0 936 static unsigned long shrink_zones(int priority, struct zone **zones,
ian@0 937 struct scan_control *sc)
ian@0 938 {
ian@0 939 unsigned long nr_reclaimed = 0;
ian@0 940 int i;
ian@0 941
ian@0 942 for (i = 0; zones[i] != NULL; i++) {
ian@0 943 struct zone *zone = zones[i];
ian@0 944
ian@0 945 if (!populated_zone(zone))
ian@0 946 continue;
ian@0 947
ian@0 948 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
ian@0 949 continue;
ian@0 950
ian@240 951 note_zone_scanning_priority(zone, priority);
ian@0 952
ian@0 953 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
ian@0 954 continue; /* Let kswapd poll it */
ian@0 955
ian@0 956 nr_reclaimed += shrink_zone(priority, zone, sc);
ian@0 957 }
ian@0 958 return nr_reclaimed;
ian@0 959 }
ian@0 960
ian@0 961 /*
ian@0 962 * This is the main entry point to direct page reclaim.
ian@0 963 *
ian@0 964 * If a full scan of the inactive list fails to free enough memory then we
ian@0 965 * are "out of memory" and something needs to be killed.
ian@0 966 *
ian@0 967 * If the caller is !__GFP_FS then the probability of a failure is reasonably
ian@0 968 * high - the zone may be full of dirty or under-writeback pages, which this
ian@0 969 * caller can't do much about. We kick pdflush and take explicit naps in the
ian@0 970 * hope that some of these pages can be written. But if the allocating task
ian@0 971 * holds filesystem locks which prevent writeout this might not work, and the
ian@0 972 * allocation attempt will fail.
ian@0 973 */
ian@0 974 unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
ian@0 975 {
ian@0 976 int priority;
ian@0 977 int ret = 0;
ian@0 978 unsigned long total_scanned = 0;
ian@0 979 unsigned long nr_reclaimed = 0;
ian@0 980 struct reclaim_state *reclaim_state = current->reclaim_state;
ian@0 981 unsigned long lru_pages = 0;
ian@0 982 int i;
ian@0 983 struct scan_control sc = {
ian@0 984 .gfp_mask = gfp_mask,
ian@0 985 .may_writepage = !laptop_mode,
ian@0 986 .swap_cluster_max = SWAP_CLUSTER_MAX,
ian@0 987 .may_swap = 1,
ian@0 988 .swappiness = vm_swappiness,
ian@0 989 };
ian@0 990
ian@0 991 count_vm_event(ALLOCSTALL);
ian@0 992
ian@0 993 for (i = 0; zones[i] != NULL; i++) {
ian@0 994 struct zone *zone = zones[i];
ian@0 995
ian@0 996 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
ian@0 997 continue;
ian@0 998
ian@0 999 lru_pages += zone->nr_active + zone->nr_inactive;
ian@0 1000 }
ian@0 1001
ian@0 1002 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
ian@0 1003 sc.nr_scanned = 0;
ian@0 1004 if (!priority)
ian@0 1005 disable_swap_token();
ian@0 1006 nr_reclaimed += shrink_zones(priority, zones, &sc);
ian@0 1007 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
ian@0 1008 if (reclaim_state) {
ian@0 1009 nr_reclaimed += reclaim_state->reclaimed_slab;
ian@0 1010 reclaim_state->reclaimed_slab = 0;
ian@0 1011 }
ian@0 1012 total_scanned += sc.nr_scanned;
ian@0 1013 if (nr_reclaimed >= sc.swap_cluster_max) {
ian@0 1014 ret = 1;
ian@0 1015 goto out;
ian@0 1016 }
ian@0 1017
ian@0 1018 /*
ian@0 1019 * Try to write back as many pages as we just scanned. This
ian@0 1020 * tends to cause slow streaming writers to write data to the
ian@0 1021 * disk smoothly, at the dirtying rate, which is nice. But
ian@0 1022 * that's undesirable in laptop mode, where we *want* lumpy
ian@0 1023 * writeout. So in laptop mode, write out the whole world.
ian@0 1024 */
ian@0 1025 if (total_scanned > sc.swap_cluster_max +
ian@0 1026 sc.swap_cluster_max / 2) {
ian@0 1027 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
ian@0 1028 sc.may_writepage = 1;
ian@0 1029 }
ian@0 1030
ian@0 1031 /* Take a nap, wait for some writeback to complete */
ian@0 1032 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
ian@0 1033 blk_congestion_wait(WRITE, HZ/10);
ian@0 1034 }
ian@0 1035 out:
ian@240 1036 /*
ian@240 1037 * Now that we've scanned all the zones at this priority level, note
ian@240 1038 * that level within the zone so that the next thread which performs
ian@240 1039 * scanning of this zone will immediately start out at this priority
ian@240 1040 * level. This affects only the decision whether or not to bring
ian@240 1041 * mapped pages onto the inactive list.
ian@240 1042 */
ian@240 1043 if (priority < 0)
ian@240 1044 priority = 0;
ian@0 1045 for (i = 0; zones[i] != 0; i++) {
ian@0 1046 struct zone *zone = zones[i];
ian@0 1047
ian@0 1048 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
ian@0 1049 continue;
ian@0 1050
ian@240 1051 zone->prev_priority = priority;
ian@0 1052 }
ian@0 1053 return ret;
ian@0 1054 }
ian@0 1055
ian@0 1056 /*
ian@0 1057 * For kswapd, balance_pgdat() will work across all this node's zones until
ian@0 1058 * they are all at pages_high.
ian@0 1059 *
ian@0 1060 * Returns the number of pages which were actually freed.
ian@0 1061 *
ian@0 1062 * There is special handling here for zones which are full of pinned pages.
ian@0 1063 * This can happen if the pages are all mlocked, or if they are all used by
ian@0 1064 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
ian@0 1065 * What we do is to detect the case where all pages in the zone have been
ian@0 1066 * scanned twice and there has been zero successful reclaim. Mark the zone as
ian@0 1067 * dead and from now on, only perform a short scan. Basically we're polling
ian@0 1068 * the zone for when the problem goes away.
ian@0 1069 *
ian@0 1070 * kswapd scans the zones in the highmem->normal->dma direction. It skips
ian@0 1071 * zones which have free_pages > pages_high, but once a zone is found to have
ian@0 1072 * free_pages <= pages_high, we scan that zone and the lower zones regardless
ian@0 1073 * of the number of free pages in the lower zones. This interoperates with
ian@0 1074 * the page allocator fallback scheme to ensure that aging of pages is balanced
ian@0 1075 * across the zones.
ian@0 1076 */
ian@0 1077 static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
ian@0 1078 {
ian@0 1079 int all_zones_ok;
ian@0 1080 int priority;
ian@0 1081 int i;
ian@0 1082 unsigned long total_scanned;
ian@0 1083 unsigned long nr_reclaimed;
ian@0 1084 struct reclaim_state *reclaim_state = current->reclaim_state;
ian@0 1085 struct scan_control sc = {
ian@0 1086 .gfp_mask = GFP_KERNEL,
ian@0 1087 .may_swap = 1,
ian@0 1088 .swap_cluster_max = SWAP_CLUSTER_MAX,
ian@0 1089 .swappiness = vm_swappiness,
ian@0 1090 };
ian@240 1091 /*
ian@240 1092 * temp_priority is used to remember the scanning priority at which
ian@240 1093 * this zone was successfully refilled to free_pages == pages_high.
ian@240 1094 */
ian@240 1095 int temp_priority[MAX_NR_ZONES];
ian@0 1096
ian@0 1097 loop_again:
ian@0 1098 total_scanned = 0;
ian@0 1099 nr_reclaimed = 0;
ian@0 1100 sc.may_writepage = !laptop_mode;
ian@0 1101 count_vm_event(PAGEOUTRUN);
ian@0 1102
ian@240 1103 for (i = 0; i < pgdat->nr_zones; i++)
ian@240 1104 temp_priority[i] = DEF_PRIORITY;
ian@0 1105
ian@0 1106 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
ian@0 1107 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
ian@0 1108 unsigned long lru_pages = 0;
ian@0 1109
ian@0 1110 /* The swap token gets in the way of swapout... */
ian@0 1111 if (!priority)
ian@0 1112 disable_swap_token();
ian@0 1113
ian@0 1114 all_zones_ok = 1;
ian@0 1115
ian@0 1116 /*
ian@0 1117 * Scan in the highmem->dma direction for the highest
ian@0 1118 * zone which needs scanning
ian@0 1119 */
ian@0 1120 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
ian@0 1121 struct zone *zone = pgdat->node_zones + i;
ian@0 1122
ian@0 1123 if (!populated_zone(zone))
ian@0 1124 continue;
ian@0 1125
ian@0 1126 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
ian@0 1127 continue;
ian@0 1128
ian@0 1129 if (!zone_watermark_ok(zone, order, zone->pages_high,
ian@0 1130 0, 0)) {
ian@0 1131 end_zone = i;
ian@0 1132 goto scan;
ian@0 1133 }
ian@0 1134 }
ian@0 1135 goto out;
ian@0 1136 scan:
ian@0 1137 for (i = 0; i <= end_zone; i++) {
ian@0 1138 struct zone *zone = pgdat->node_zones + i;
ian@0 1139
ian@0 1140 lru_pages += zone->nr_active + zone->nr_inactive;
ian@0 1141 }
ian@0 1142
ian@0 1143 /*
ian@0 1144 * Now scan the zone in the dma->highmem direction, stopping
ian@0 1145 * at the last zone which needs scanning.
ian@0 1146 *
ian@0 1147 * We do this because the page allocator works in the opposite
ian@0 1148 * direction. This prevents the page allocator from allocating
ian@0 1149 * pages behind kswapd's direction of progress, which would
ian@0 1150 * cause too much scanning of the lower zones.
ian@0 1151 */
ian@0 1152 for (i = 0; i <= end_zone; i++) {
ian@0 1153 struct zone *zone = pgdat->node_zones + i;
ian@0 1154 int nr_slab;
ian@0 1155
ian@0 1156 if (!populated_zone(zone))
ian@0 1157 continue;
ian@0 1158
ian@0 1159 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
ian@0 1160 continue;
ian@0 1161
ian@0 1162 if (!zone_watermark_ok(zone, order, zone->pages_high,
ian@0 1163 end_zone, 0))
ian@0 1164 all_zones_ok = 0;
ian@240 1165 temp_priority[i] = priority;
ian@0 1166 sc.nr_scanned = 0;
ian@240 1167 note_zone_scanning_priority(zone, priority);
ian@0 1168 nr_reclaimed += shrink_zone(priority, zone, &sc);
ian@0 1169 reclaim_state->reclaimed_slab = 0;
ian@0 1170 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
ian@0 1171 lru_pages);
ian@0 1172 nr_reclaimed += reclaim_state->reclaimed_slab;
ian@0 1173 total_scanned += sc.nr_scanned;
ian@0 1174 if (zone->all_unreclaimable)
ian@0 1175 continue;
ian@0 1176 if (nr_slab == 0 && zone->pages_scanned >=
ian@0 1177 (zone->nr_active + zone->nr_inactive) * 4)
ian@0 1178 zone->all_unreclaimable = 1;
ian@0 1179 /*
ian@0 1180 * If we've done a decent amount of scanning and
ian@0 1181 * the reclaim ratio is low, start doing writepage
ian@0 1182 * even in laptop mode
ian@0 1183 */
ian@0 1184 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
ian@0 1185 total_scanned > nr_reclaimed + nr_reclaimed / 2)
ian@0 1186 sc.may_writepage = 1;
ian@0 1187 }
ian@0 1188 if (all_zones_ok)
ian@0 1189 break; /* kswapd: all done */
ian@0 1190 /*
ian@0 1191 * OK, kswapd is getting into trouble. Take a nap, then take
ian@0 1192 * another pass across the zones.
ian@0 1193 */
ian@0 1194 if (total_scanned && priority < DEF_PRIORITY - 2)
ian@0 1195 blk_congestion_wait(WRITE, HZ/10);
ian@0 1196
ian@0 1197 /*
ian@0 1198 * We do this so kswapd doesn't build up large priorities for
ian@0 1199 * example when it is freeing in parallel with allocators. It
ian@0 1200 * matches the direct reclaim path behaviour in terms of impact
ian@0 1201 * on zone->*_priority.
ian@0 1202 */
ian@0 1203 if (nr_reclaimed >= SWAP_CLUSTER_MAX)
ian@0 1204 break;
ian@0 1205 }
ian@0 1206 out:
ian@240 1207 /*
ian@240 1208 * Note within each zone the priority level at which this zone was
ian@240 1209 * brought into a happy state. So that the next thread which scans this
ian@240 1210 * zone will start out at that priority level.
ian@240 1211 */
ian@0 1212 for (i = 0; i < pgdat->nr_zones; i++) {
ian@0 1213 struct zone *zone = pgdat->node_zones + i;
ian@0 1214
ian@240 1215 zone->prev_priority = temp_priority[i];
ian@0 1216 }
ian@0 1217 if (!all_zones_ok) {
ian@0 1218 cond_resched();
ian@0 1219 goto loop_again;
ian@0 1220 }
ian@0 1221
ian@0 1222 return nr_reclaimed;
ian@0 1223 }
ian@0 1224
ian@0 1225 /*
ian@0 1226 * The background pageout daemon, started as a kernel thread
ian@0 1227 * from the init process.
ian@0 1228 *
ian@0 1229 * This basically trickles out pages so that we have _some_
ian@0 1230 * free memory available even if there is no other activity
ian@0 1231 * that frees anything up. This is needed for things like routing
ian@0 1232 * etc, where we otherwise might have all activity going on in
ian@0 1233 * asynchronous contexts that cannot page things out.
ian@0 1234 *
ian@0 1235 * If there are applications that are active memory-allocators
ian@0 1236 * (most normal use), this basically shouldn't matter.
ian@0 1237 */
ian@0 1238 static int kswapd(void *p)
ian@0 1239 {
ian@0 1240 unsigned long order;
ian@0 1241 pg_data_t *pgdat = (pg_data_t*)p;
ian@0 1242 struct task_struct *tsk = current;
ian@0 1243 DEFINE_WAIT(wait);
ian@0 1244 struct reclaim_state reclaim_state = {
ian@0 1245 .reclaimed_slab = 0,
ian@0 1246 };
ian@0 1247 cpumask_t cpumask;
ian@0 1248
ian@0 1249 cpumask = node_to_cpumask(pgdat->node_id);
ian@0 1250 if (!cpus_empty(cpumask))
ian@0 1251 set_cpus_allowed(tsk, cpumask);
ian@0 1252 current->reclaim_state = &reclaim_state;
ian@0 1253
ian@0 1254 /*
ian@0 1255 * Tell the memory management that we're a "memory allocator",
ian@0 1256 * and that if we need more memory we should get access to it
ian@0 1257 * regardless (see "__alloc_pages()"). "kswapd" should
ian@0 1258 * never get caught in the normal page freeing logic.
ian@0 1259 *
ian@0 1260 * (Kswapd normally doesn't need memory anyway, but sometimes
ian@0 1261 * you need a small amount of memory in order to be able to
ian@0 1262 * page out something else, and this flag essentially protects
ian@0 1263 * us from recursively trying to free more memory as we're
ian@0 1264 * trying to free the first piece of memory in the first place).
ian@0 1265 */
ian@0 1266 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
ian@0 1267
ian@0 1268 order = 0;
ian@0 1269 for ( ; ; ) {
ian@0 1270 unsigned long new_order;
ian@0 1271
ian@0 1272 try_to_freeze();
ian@0 1273
ian@0 1274 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
ian@0 1275 new_order = pgdat->kswapd_max_order;
ian@0 1276 pgdat->kswapd_max_order = 0;
ian@0 1277 if (order < new_order) {
ian@0 1278 /*
ian@0 1279 * Don't sleep if someone wants a larger 'order'
ian@0 1280 * allocation
ian@0 1281 */
ian@0 1282 order = new_order;
ian@0 1283 } else {
ian@0 1284 schedule();
ian@0 1285 order = pgdat->kswapd_max_order;
ian@0 1286 }
ian@0 1287 finish_wait(&pgdat->kswapd_wait, &wait);
ian@0 1288
ian@0 1289 balance_pgdat(pgdat, order);
ian@0 1290 }
ian@0 1291 return 0;
ian@0 1292 }
ian@0 1293
ian@0 1294 /*
ian@0 1295 * A zone is low on free memory, so wake its kswapd task to service it.
ian@0 1296 */
ian@0 1297 void wakeup_kswapd(struct zone *zone, int order)
ian@0 1298 {
ian@0 1299 pg_data_t *pgdat;
ian@0 1300
ian@0 1301 if (!populated_zone(zone))
ian@0 1302 return;
ian@0 1303
ian@0 1304 pgdat = zone->zone_pgdat;
ian@0 1305 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
ian@0 1306 return;
ian@0 1307 if (pgdat->kswapd_max_order < order)
ian@0 1308 pgdat->kswapd_max_order = order;
ian@0 1309 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
ian@0 1310 return;
ian@0 1311 if (!waitqueue_active(&pgdat->kswapd_wait))
ian@0 1312 return;
ian@0 1313 wake_up_interruptible(&pgdat->kswapd_wait);
ian@0 1314 }
ian@0 1315
ian@0 1316 #ifdef CONFIG_PM
ian@0 1317 /*
ian@0 1318 * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
ian@0 1319 * from LRU lists system-wide, for given pass and priority, and returns the
ian@0 1320 * number of reclaimed pages
ian@0 1321 *
ian@0 1322 * For pass > 3 we also try to shrink the LRU lists that contain a few pages
ian@0 1323 */
ian@0 1324 static unsigned long shrink_all_zones(unsigned long nr_pages, int pass,
ian@0 1325 int prio, struct scan_control *sc)
ian@0 1326 {
ian@0 1327 struct zone *zone;
ian@0 1328 unsigned long nr_to_scan, ret = 0;
ian@0 1329
ian@0 1330 for_each_zone(zone) {
ian@0 1331
ian@0 1332 if (!populated_zone(zone))
ian@0 1333 continue;
ian@0 1334
ian@0 1335 if (zone->all_unreclaimable && prio != DEF_PRIORITY)
ian@0 1336 continue;
ian@0 1337
ian@0 1338 /* For pass = 0 we don't shrink the active list */
ian@0 1339 if (pass > 0) {
ian@0 1340 zone->nr_scan_active += (zone->nr_active >> prio) + 1;
ian@0 1341 if (zone->nr_scan_active >= nr_pages || pass > 3) {
ian@0 1342 zone->nr_scan_active = 0;
ian@0 1343 nr_to_scan = min(nr_pages, zone->nr_active);
ian@240 1344 shrink_active_list(nr_to_scan, zone, sc, prio);
ian@0 1345 }
ian@0 1346 }
ian@0 1347
ian@0 1348 zone->nr_scan_inactive += (zone->nr_inactive >> prio) + 1;
ian@0 1349 if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
ian@0 1350 zone->nr_scan_inactive = 0;
ian@0 1351 nr_to_scan = min(nr_pages, zone->nr_inactive);
ian@0 1352 ret += shrink_inactive_list(nr_to_scan, zone, sc);
ian@0 1353 if (ret >= nr_pages)
ian@0 1354 return ret;
ian@0 1355 }
ian@0 1356 }
ian@0 1357
ian@0 1358 return ret;
ian@0 1359 }
ian@0 1360
ian@0 1361 /*
ian@0 1362 * Try to free `nr_pages' of memory, system-wide, and return the number of
ian@0 1363 * freed pages.
ian@0 1364 *
ian@0 1365 * Rather than trying to age LRUs the aim is to preserve the overall
ian@0 1366 * LRU order by reclaiming preferentially
ian@0 1367 * inactive > active > active referenced > active mapped
ian@0 1368 */
ian@0 1369 unsigned long shrink_all_memory(unsigned long nr_pages)
ian@0 1370 {
ian@0 1371 unsigned long lru_pages, nr_slab;
ian@0 1372 unsigned long ret = 0;
ian@0 1373 int pass;
ian@0 1374 struct reclaim_state reclaim_state;
ian@0 1375 struct zone *zone;
ian@0 1376 struct scan_control sc = {
ian@0 1377 .gfp_mask = GFP_KERNEL,
ian@0 1378 .may_swap = 0,
ian@0 1379 .swap_cluster_max = nr_pages,
ian@0 1380 .may_writepage = 1,
ian@0 1381 .swappiness = vm_swappiness,
ian@0 1382 };
ian@0 1383
ian@0 1384 current->reclaim_state = &reclaim_state;
ian@0 1385
ian@0 1386 lru_pages = 0;
ian@0 1387 for_each_zone(zone)
ian@0 1388 lru_pages += zone->nr_active + zone->nr_inactive;
ian@0 1389
ian@0 1390 nr_slab = global_page_state(NR_SLAB);
ian@0 1391 /* If slab caches are huge, it's better to hit them first */
ian@0 1392 while (nr_slab >= lru_pages) {
ian@0 1393 reclaim_state.reclaimed_slab = 0;
ian@0 1394 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
ian@0 1395 if (!reclaim_state.reclaimed_slab)
ian@0 1396 break;
ian@0 1397
ian@0 1398 ret += reclaim_state.reclaimed_slab;
ian@0 1399 if (ret >= nr_pages)
ian@0 1400 goto out;
ian@0 1401
ian@0 1402 nr_slab -= reclaim_state.reclaimed_slab;
ian@0 1403 }
ian@0 1404
ian@0 1405 /*
ian@0 1406 * We try to shrink LRUs in 5 passes:
ian@0 1407 * 0 = Reclaim from inactive_list only
ian@0 1408 * 1 = Reclaim from active list but don't reclaim mapped
ian@0 1409 * 2 = 2nd pass of type 1
ian@0 1410 * 3 = Reclaim mapped (normal reclaim)
ian@0 1411 * 4 = 2nd pass of type 3
ian@0 1412 */
ian@0 1413 for (pass = 0; pass < 5; pass++) {
ian@0 1414 int prio;
ian@0 1415
ian@0 1416 /* Needed for shrinking slab caches later on */
ian@0 1417 if (!lru_pages)
ian@0 1418 for_each_zone(zone) {
ian@0 1419 lru_pages += zone->nr_active;
ian@0 1420 lru_pages += zone->nr_inactive;
ian@0 1421 }
ian@0 1422
ian@0 1423 /* Force reclaiming mapped pages in the passes #3 and #4 */
ian@0 1424 if (pass > 2) {
ian@0 1425 sc.may_swap = 1;
ian@0 1426 sc.swappiness = 100;
ian@0 1427 }
ian@0 1428
ian@0 1429 for (prio = DEF_PRIORITY; prio >= 0; prio--) {
ian@0 1430 unsigned long nr_to_scan = nr_pages - ret;
ian@0 1431
ian@0 1432 sc.nr_scanned = 0;
ian@0 1433 ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
ian@0 1434 if (ret >= nr_pages)
ian@0 1435 goto out;
ian@0 1436
ian@0 1437 reclaim_state.reclaimed_slab = 0;
ian@0 1438 shrink_slab(sc.nr_scanned, sc.gfp_mask, lru_pages);
ian@0 1439 ret += reclaim_state.reclaimed_slab;
ian@0 1440 if (ret >= nr_pages)
ian@0 1441 goto out;
ian@0 1442
ian@0 1443 if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
ian@0 1444 blk_congestion_wait(WRITE, HZ / 10);
ian@0 1445 }
ian@0 1446
ian@0 1447 lru_pages = 0;
ian@0 1448 }
ian@0 1449
ian@0 1450 /*
ian@0 1451 * If ret = 0, we could not shrink LRUs, but there may be something
ian@0 1452 * in slab caches
ian@0 1453 */
ian@0 1454 if (!ret)
ian@0 1455 do {
ian@0 1456 reclaim_state.reclaimed_slab = 0;
ian@0 1457 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
ian@0 1458 ret += reclaim_state.reclaimed_slab;
ian@0 1459 } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
ian@0 1460
ian@0 1461 out:
ian@0 1462 current->reclaim_state = NULL;
ian@0 1463
ian@0 1464 return ret;
ian@0 1465 }
ian@0 1466 #endif
ian@0 1467
ian@0 1468 #ifdef CONFIG_HOTPLUG_CPU
ian@0 1469 /* It's optimal to keep kswapds on the same CPUs as their memory, but
ian@0 1470 not required for correctness. So if the last cpu in a node goes
ian@0 1471 away, we get changed to run anywhere: as the first one comes back,
ian@0 1472 restore their cpu bindings. */
ian@0 1473 static int __devinit cpu_callback(struct notifier_block *nfb,
ian@0 1474 unsigned long action, void *hcpu)
ian@0 1475 {
ian@0 1476 pg_data_t *pgdat;
ian@0 1477 cpumask_t mask;
ian@0 1478
ian@0 1479 if (action == CPU_ONLINE) {
ian@0 1480 for_each_online_pgdat(pgdat) {
ian@0 1481 mask = node_to_cpumask(pgdat->node_id);
ian@0 1482 if (any_online_cpu(mask) != NR_CPUS)
ian@0 1483 /* One of our CPUs online: restore mask */
ian@0 1484 set_cpus_allowed(pgdat->kswapd, mask);
ian@0 1485 }
ian@0 1486 }
ian@0 1487 return NOTIFY_OK;
ian@0 1488 }
ian@0 1489 #endif /* CONFIG_HOTPLUG_CPU */
ian@0 1490
ian@0 1491 /*
ian@0 1492 * This kswapd start function will be called by init and node-hot-add.
ian@0 1493 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
ian@0 1494 */
ian@0 1495 int kswapd_run(int nid)
ian@0 1496 {
ian@0 1497 pg_data_t *pgdat = NODE_DATA(nid);
ian@0 1498 int ret = 0;
ian@0 1499
ian@0 1500 if (pgdat->kswapd)
ian@0 1501 return 0;
ian@0 1502
ian@0 1503 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
ian@0 1504 if (IS_ERR(pgdat->kswapd)) {
ian@0 1505 /* failure at boot is fatal */
ian@0 1506 BUG_ON(system_state == SYSTEM_BOOTING);
ian@0 1507 printk("Failed to start kswapd on node %d\n",nid);
ian@0 1508 ret = -1;
ian@0 1509 }
ian@0 1510 return ret;
ian@0 1511 }
ian@0 1512
ian@0 1513 static int __init kswapd_init(void)
ian@0 1514 {
ian@0 1515 int nid;
ian@0 1516
ian@0 1517 swap_setup();
ian@0 1518 for_each_online_node(nid)
ian@0 1519 kswapd_run(nid);
ian@0 1520 hotcpu_notifier(cpu_callback, 0);
ian@0 1521 return 0;
ian@0 1522 }
ian@0 1523
ian@0 1524 module_init(kswapd_init)
ian@0 1525
ian@0 1526 #ifdef CONFIG_NUMA
ian@0 1527 /*
ian@0 1528 * Zone reclaim mode
ian@0 1529 *
ian@0 1530 * If non-zero call zone_reclaim when the number of free pages falls below
ian@0 1531 * the watermarks.
ian@0 1532 */
ian@0 1533 int zone_reclaim_mode __read_mostly;
ian@0 1534
ian@0 1535 #define RECLAIM_OFF 0
ian@0 1536 #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
ian@0 1537 #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
ian@0 1538 #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
ian@0 1539
ian@0 1540 /*
ian@0 1541 * Priority for ZONE_RECLAIM. This determines the fraction of pages
ian@0 1542 * of a node considered for each zone_reclaim. 4 scans 1/16th of
ian@0 1543 * a zone.
ian@0 1544 */
ian@0 1545 #define ZONE_RECLAIM_PRIORITY 4
ian@0 1546
ian@0 1547 /*
ian@0 1548 * Percentage of pages in a zone that must be unmapped for zone_reclaim to
ian@0 1549 * occur.
ian@0 1550 */
ian@0 1551 int sysctl_min_unmapped_ratio = 1;
ian@0 1552
ian@0 1553 /*
ian@240 1554 * If the number of slab pages in a zone grows beyond this percentage then
ian@240 1555 * slab reclaim needs to occur.
ian@240 1556 */
ian@240 1557 int sysctl_min_slab_ratio = 5;
ian@240 1558
ian@240 1559 /*
ian@0 1560 * Try to free up some pages from this zone through reclaim.
ian@0 1561 */
ian@0 1562 static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
ian@0 1563 {
ian@0 1564 /* Minimum pages needed in order to stay on node */
ian@0 1565 const unsigned long nr_pages = 1 << order;
ian@0 1566 struct task_struct *p = current;
ian@0 1567 struct reclaim_state reclaim_state;
ian@0 1568 int priority;
ian@0 1569 unsigned long nr_reclaimed = 0;
ian@0 1570 struct scan_control sc = {
ian@0 1571 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
ian@0 1572 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
ian@0 1573 .swap_cluster_max = max_t(unsigned long, nr_pages,
ian@0 1574 SWAP_CLUSTER_MAX),
ian@0 1575 .gfp_mask = gfp_mask,
ian@0 1576 .swappiness = vm_swappiness,
ian@0 1577 };
ian@0 1578
ian@0 1579 disable_swap_token();
ian@0 1580 cond_resched();
ian@0 1581 /*
ian@0 1582 * We need to be able to allocate from the reserves for RECLAIM_SWAP
ian@0 1583 * and we also need to be able to write out pages for RECLAIM_WRITE
ian@0 1584 * and RECLAIM_SWAP.
ian@0 1585 */
ian@0 1586 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
ian@0 1587 reclaim_state.reclaimed_slab = 0;
ian@0 1588 p->reclaim_state = &reclaim_state;
ian@0 1589
ian@240 1590 if (zone_page_state(zone, NR_FILE_PAGES) -
ian@240 1591 zone_page_state(zone, NR_FILE_MAPPED) >
ian@240 1592 zone->min_unmapped_ratio) {
ian@240 1593 /*
ian@240 1594 * Free memory by calling shrink zone with increasing
ian@240 1595 * priorities until we have enough memory freed.
ian@240 1596 */
ian@240 1597 priority = ZONE_RECLAIM_PRIORITY;
ian@240 1598 do {
ian@240 1599 note_zone_scanning_priority(zone, priority);
ian@240 1600 nr_reclaimed += shrink_zone(priority, zone, &sc);
ian@240 1601 priority--;
ian@240 1602 } while (priority >= 0 && nr_reclaimed < nr_pages);
ian@240 1603 }
ian@0 1604
ian@240 1605 if (zone_page_state(zone, NR_SLAB) > zone->min_slab_pages) {
ian@0 1606 /*
ian@0 1607 * shrink_slab() does not currently allow us to determine how
ian@240 1608 * many pages were freed in this zone. So we take the current
ian@240 1609 * number of slab pages and shake the slab until it is reduced
ian@240 1610 * by the same nr_pages that we used for reclaiming unmapped
ian@240 1611 * pages.
ian@0 1612 *
ian@240 1613 * Note that shrink_slab will free memory on all zones and may
ian@240 1614 * take a long time.
ian@0 1615 */
ian@240 1616 unsigned long limit = zone_page_state(zone,
ian@240 1617 NR_SLAB) - nr_pages;
ian@240 1618
ian@240 1619 while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
ian@240 1620 zone_page_state(zone, NR_SLAB) > limit)
ian@240 1621 ;
ian@0 1622 }
ian@0 1623
ian@0 1624 p->reclaim_state = NULL;
ian@0 1625 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
ian@0 1626 return nr_reclaimed >= nr_pages;
ian@0 1627 }
ian@0 1628
ian@0 1629 int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
ian@0 1630 {
ian@0 1631 cpumask_t mask;
ian@0 1632 int node_id;
ian@0 1633
ian@0 1634 /*
ian@240 1635 * Zone reclaim reclaims unmapped file backed pages and
ian@240 1636 * slab pages if we are over the defined limits.
ian@0 1637 *
ian@0 1638 * A small portion of unmapped file backed pages is needed for
ian@0 1639 * file I/O otherwise pages read by file I/O will be immediately
ian@0 1640 * thrown out if the zone is overallocated. So we do not reclaim
ian@0 1641 * if less than a specified percentage of the zone is used by
ian@0 1642 * unmapped file backed pages.
ian@0 1643 */
ian@0 1644 if (zone_page_state(zone, NR_FILE_PAGES) -
ian@240 1645 zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_ratio
ian@240 1646 && zone_page_state(zone, NR_SLAB)
ian@240 1647 <= zone->min_slab_pages)
ian@0 1648 return 0;
ian@0 1649
ian@0 1650 /*
ian@0 1651 * Avoid concurrent zone reclaims, do not reclaim in a zone that does
ian@0 1652 * not have reclaimable pages and if we should not delay the allocation
ian@0 1653 * then do not scan.
ian@0 1654 */
ian@0 1655 if (!(gfp_mask & __GFP_WAIT) ||
ian@0 1656 zone->all_unreclaimable ||
ian@0 1657 atomic_read(&zone->reclaim_in_progress) > 0 ||
ian@0 1658 (current->flags & PF_MEMALLOC))
ian@0 1659 return 0;
ian@0 1660
ian@0 1661 /*
ian@0 1662 * Only run zone reclaim on the local zone or on zones that do not
ian@0 1663 * have associated processors. This will favor the local processor
ian@0 1664 * over remote processors and spread off node memory allocations
ian@0 1665 * as wide as possible.
ian@0 1666 */
ian@0 1667 node_id = zone->zone_pgdat->node_id;
ian@0 1668 mask = node_to_cpumask(node_id);
ian@0 1669 if (!cpus_empty(mask) && node_id != numa_node_id())
ian@0 1670 return 0;
ian@0 1671 return __zone_reclaim(zone, gfp_mask, order);
ian@0 1672 }
ian@0 1673 #endif