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