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