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