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