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