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