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