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