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