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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Keika Kobayashi | 873b477 | 2008-07-25 01:48:52 -0700 | [diff] [blame] | 41 | #include <linux/delayacct.h> |
Lee Schermerhorn | af936a1 | 2008-10-18 20:26:53 -0700 | [diff] [blame] | 42 | #include <linux/sysctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #include <asm/tlbflush.h> |
| 45 | #include <asm/div64.h> |
| 46 | |
| 47 | #include <linux/swapops.h> |
| 48 | |
Nick Piggin | 0f8053a | 2006-03-22 00:08:33 -0800 | [diff] [blame] | 49 | #include "internal.h" |
| 50 | |
Mel Gorman | 33906bc | 2010-08-09 17:19:16 -0700 | [diff] [blame] | 51 | #define CREATE_TRACE_POINTS |
| 52 | #include <trace/events/vmscan.h> |
| 53 | |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 54 | enum lumpy_mode { |
| 55 | LUMPY_MODE_NONE, |
| 56 | LUMPY_MODE_ASYNC, |
| 57 | LUMPY_MODE_SYNC, |
| 58 | }; |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | struct scan_control { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | /* Incremented by the number of inactive pages that were scanned */ |
| 62 | unsigned long nr_scanned; |
| 63 | |
Rik van Riel | a79311c | 2009-01-06 14:40:01 -0800 | [diff] [blame] | 64 | /* Number of pages freed so far during a call to shrink_zones() */ |
| 65 | unsigned long nr_reclaimed; |
| 66 | |
KOSAKI Motohiro | 22fba33 | 2009-12-14 17:59:10 -0800 | [diff] [blame] | 67 | /* How many pages shrink_list() should reclaim */ |
| 68 | unsigned long nr_to_reclaim; |
| 69 | |
KOSAKI Motohiro | 7b51755 | 2009-12-14 17:59:12 -0800 | [diff] [blame] | 70 | unsigned long hibernation_mode; |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* This context's GFP mask */ |
Al Viro | 6daa0e2 | 2005-10-21 03:18:50 -0400 | [diff] [blame] | 73 | gfp_t gfp_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | int may_writepage; |
| 76 | |
Johannes Weiner | a6dc60f8 | 2009-03-31 15:19:30 -0700 | [diff] [blame] | 77 | /* Can mapped pages be reclaimed? */ |
| 78 | int may_unmap; |
Christoph Lameter | f1fd106 | 2006-01-18 17:42:30 -0800 | [diff] [blame] | 79 | |
KOSAKI Motohiro | 2e2e425 | 2009-04-21 12:24:57 -0700 | [diff] [blame] | 80 | /* Can pages be swapped as part of reclaim? */ |
| 81 | int may_swap; |
| 82 | |
Rafael J. Wysocki | d6277db | 2006-06-23 02:03:18 -0700 | [diff] [blame] | 83 | int swappiness; |
Nick Piggin | 408d854 | 2006-09-25 23:31:27 -0700 | [diff] [blame] | 84 | |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 85 | int order; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 86 | |
KOSAKI Motohiro | 5f53e76 | 2010-05-24 14:32:37 -0700 | [diff] [blame] | 87 | /* |
Nikanth Karthikesan | 415b54e | 2010-08-17 15:39:09 +0530 | [diff] [blame] | 88 | * Intend to reclaim enough continuous memory rather than reclaim |
| 89 | * enough amount of memory. i.e, mode for high order allocation. |
KOSAKI Motohiro | 5f53e76 | 2010-05-24 14:32:37 -0700 | [diff] [blame] | 90 | */ |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 91 | enum lumpy_mode lumpy_reclaim_mode; |
KOSAKI Motohiro | 5f53e76 | 2010-05-24 14:32:37 -0700 | [diff] [blame] | 92 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 93 | /* Which cgroup do we reclaim from */ |
| 94 | struct mem_cgroup *mem_cgroup; |
| 95 | |
KAMEZAWA Hiroyuki | 327c0e9 | 2009-03-31 15:23:31 -0700 | [diff] [blame] | 96 | /* |
| 97 | * Nodemask of nodes allowed by the caller. If NULL, all nodes |
| 98 | * are scanned. |
| 99 | */ |
| 100 | nodemask_t *nodemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru)) |
| 104 | |
| 105 | #ifdef ARCH_HAS_PREFETCH |
| 106 | #define prefetch_prev_lru_page(_page, _base, _field) \ |
| 107 | do { \ |
| 108 | if ((_page)->lru.prev != _base) { \ |
| 109 | struct page *prev; \ |
| 110 | \ |
| 111 | prev = lru_to_page(&(_page->lru)); \ |
| 112 | prefetch(&prev->_field); \ |
| 113 | } \ |
| 114 | } while (0) |
| 115 | #else |
| 116 | #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) |
| 117 | #endif |
| 118 | |
| 119 | #ifdef ARCH_HAS_PREFETCHW |
| 120 | #define prefetchw_prev_lru_page(_page, _base, _field) \ |
| 121 | do { \ |
| 122 | if ((_page)->lru.prev != _base) { \ |
| 123 | struct page *prev; \ |
| 124 | \ |
| 125 | prev = lru_to_page(&(_page->lru)); \ |
| 126 | prefetchw(&prev->_field); \ |
| 127 | } \ |
| 128 | } while (0) |
| 129 | #else |
| 130 | #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) |
| 131 | #endif |
| 132 | |
| 133 | /* |
| 134 | * From 0 .. 100. Higher means more swappy. |
| 135 | */ |
| 136 | int vm_swappiness = 60; |
Andrew Morton | bd1e22b | 2006-06-23 02:03:47 -0700 | [diff] [blame] | 137 | 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] | 138 | |
| 139 | static LIST_HEAD(shrinker_list); |
| 140 | static DECLARE_RWSEM(shrinker_rwsem); |
| 141 | |
Balbir Singh | 00f0b82 | 2008-03-04 14:28:39 -0800 | [diff] [blame] | 142 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR |
KAMEZAWA Hiroyuki | e72e2bd | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 143 | #define scanning_global_lru(sc) (!(sc)->mem_cgroup) |
KAMEZAWA Hiroyuki | 91a4547 | 2008-02-07 00:14:29 -0800 | [diff] [blame] | 144 | #else |
KAMEZAWA Hiroyuki | e72e2bd | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 145 | #define scanning_global_lru(sc) (1) |
KAMEZAWA Hiroyuki | 91a4547 | 2008-02-07 00:14:29 -0800 | [diff] [blame] | 146 | #endif |
| 147 | |
KOSAKI Motohiro | 6e90157 | 2009-01-07 18:08:15 -0800 | [diff] [blame] | 148 | static struct zone_reclaim_stat *get_reclaim_stat(struct zone *zone, |
| 149 | struct scan_control *sc) |
| 150 | { |
KAMEZAWA Hiroyuki | e72e2bd | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 151 | if (!scanning_global_lru(sc)) |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 152 | return mem_cgroup_get_reclaim_stat(sc->mem_cgroup, zone); |
| 153 | |
KOSAKI Motohiro | 6e90157 | 2009-01-07 18:08:15 -0800 | [diff] [blame] | 154 | return &zone->reclaim_stat; |
| 155 | } |
| 156 | |
Vincent Li | 0b21767 | 2009-09-21 17:03:09 -0700 | [diff] [blame] | 157 | static unsigned long zone_nr_lru_pages(struct zone *zone, |
| 158 | struct scan_control *sc, enum lru_list lru) |
KOSAKI Motohiro | c9f299d | 2009-01-07 18:08:16 -0800 | [diff] [blame] | 159 | { |
KAMEZAWA Hiroyuki | e72e2bd | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 160 | if (!scanning_global_lru(sc)) |
KOSAKI Motohiro | a3d8e05 | 2009-01-07 18:08:19 -0800 | [diff] [blame] | 161 | return mem_cgroup_zone_nr_pages(sc->mem_cgroup, zone, lru); |
| 162 | |
KOSAKI Motohiro | c9f299d | 2009-01-07 18:08:16 -0800 | [diff] [blame] | 163 | return zone_page_state(zone, NR_LRU_BASE + lru); |
| 164 | } |
| 165 | |
| 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | /* |
| 168 | * Add a shrinker callback to be called from the vm |
| 169 | */ |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 170 | void register_shrinker(struct shrinker *shrinker) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 172 | shrinker->nr = 0; |
| 173 | down_write(&shrinker_rwsem); |
| 174 | list_add_tail(&shrinker->list, &shrinker_list); |
| 175 | up_write(&shrinker_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 177 | EXPORT_SYMBOL(register_shrinker); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * Remove one |
| 181 | */ |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 182 | void unregister_shrinker(struct shrinker *shrinker) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
| 184 | down_write(&shrinker_rwsem); |
| 185 | list_del(&shrinker->list); |
| 186 | up_write(&shrinker_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 188 | EXPORT_SYMBOL(unregister_shrinker); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
| 190 | #define SHRINK_BATCH 128 |
| 191 | /* |
| 192 | * Call the shrink functions to age shrinkable caches |
| 193 | * |
| 194 | * Here we assume it costs one seek to replace a lru page and that it also |
| 195 | * takes a seek to recreate a cache object. With this in mind we age equal |
| 196 | * percentages of the lru and ageable caches. This should balance the seeks |
| 197 | * generated by these structures. |
| 198 | * |
Simon Arlott | 183ff22 | 2007-10-20 01:27:18 +0200 | [diff] [blame] | 199 | * 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] | 200 | * slab to avoid swapping. |
| 201 | * |
| 202 | * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits. |
| 203 | * |
| 204 | * `lru_pages' represents the number of on-LRU pages in all the zones which |
| 205 | * are eligible for the caller's allocation attempt. It is used for balancing |
| 206 | * slab reclaim versus page reclaim. |
akpm@osdl.org | b15e090 | 2005-06-21 17:14:35 -0700 | [diff] [blame] | 207 | * |
| 208 | * Returns the number of slab objects which we shrunk. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | */ |
Andrew Morton | 69e0594 | 2006-03-22 00:08:19 -0800 | [diff] [blame] | 210 | unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask, |
| 211 | unsigned long lru_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
| 213 | struct shrinker *shrinker; |
Andrew Morton | 69e0594 | 2006-03-22 00:08:19 -0800 | [diff] [blame] | 214 | unsigned long ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
| 216 | if (scanned == 0) |
| 217 | scanned = SWAP_CLUSTER_MAX; |
| 218 | |
| 219 | if (!down_read_trylock(&shrinker_rwsem)) |
akpm@osdl.org | b15e090 | 2005-06-21 17:14:35 -0700 | [diff] [blame] | 220 | return 1; /* Assume we'll be able to shrink next time */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
| 222 | list_for_each_entry(shrinker, &shrinker_list, list) { |
| 223 | unsigned long long delta; |
| 224 | unsigned long total_scan; |
Dave Chinner | 7f8275d | 2010-07-19 14:56:17 +1000 | [diff] [blame] | 225 | unsigned long max_pass; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Dave Chinner | 7f8275d | 2010-07-19 14:56:17 +1000 | [diff] [blame] | 227 | max_pass = (*shrinker->shrink)(shrinker, 0, gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | delta = (4 * scanned) / shrinker->seeks; |
Andrea Arcangeli | ea164d7 | 2005-11-28 13:44:15 -0800 | [diff] [blame] | 229 | delta *= max_pass; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | do_div(delta, lru_pages + 1); |
| 231 | shrinker->nr += delta; |
Andrea Arcangeli | ea164d7 | 2005-11-28 13:44:15 -0800 | [diff] [blame] | 232 | if (shrinker->nr < 0) { |
David Rientjes | 88c3bd7 | 2009-03-31 15:23:29 -0700 | [diff] [blame] | 233 | printk(KERN_ERR "shrink_slab: %pF negative objects to " |
| 234 | "delete nr=%ld\n", |
| 235 | shrinker->shrink, shrinker->nr); |
Andrea Arcangeli | ea164d7 | 2005-11-28 13:44:15 -0800 | [diff] [blame] | 236 | shrinker->nr = max_pass; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Avoid risking looping forever due to too large nr value: |
| 241 | * never try to free more than twice the estimate number of |
| 242 | * freeable entries. |
| 243 | */ |
| 244 | if (shrinker->nr > max_pass * 2) |
| 245 | shrinker->nr = max_pass * 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
| 247 | total_scan = shrinker->nr; |
| 248 | shrinker->nr = 0; |
| 249 | |
| 250 | while (total_scan >= SHRINK_BATCH) { |
| 251 | long this_scan = SHRINK_BATCH; |
| 252 | int shrink_ret; |
akpm@osdl.org | b15e090 | 2005-06-21 17:14:35 -0700 | [diff] [blame] | 253 | int nr_before; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
Dave Chinner | 7f8275d | 2010-07-19 14:56:17 +1000 | [diff] [blame] | 255 | nr_before = (*shrinker->shrink)(shrinker, 0, gfp_mask); |
| 256 | shrink_ret = (*shrinker->shrink)(shrinker, this_scan, |
| 257 | gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | if (shrink_ret == -1) |
| 259 | break; |
akpm@osdl.org | b15e090 | 2005-06-21 17:14:35 -0700 | [diff] [blame] | 260 | if (shrink_ret < nr_before) |
| 261 | ret += nr_before - shrink_ret; |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 262 | count_vm_events(SLABS_SCANNED, this_scan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | total_scan -= this_scan; |
| 264 | |
| 265 | cond_resched(); |
| 266 | } |
| 267 | |
| 268 | shrinker->nr += total_scan; |
| 269 | } |
| 270 | up_read(&shrinker_rwsem); |
akpm@osdl.org | b15e090 | 2005-06-21 17:14:35 -0700 | [diff] [blame] | 271 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 274 | static void set_lumpy_reclaim_mode(int priority, struct scan_control *sc, |
| 275 | bool sync) |
| 276 | { |
| 277 | enum lumpy_mode mode = sync ? LUMPY_MODE_SYNC : LUMPY_MODE_ASYNC; |
| 278 | |
| 279 | /* |
| 280 | * Some reclaim have alredy been failed. No worth to try synchronous |
| 281 | * lumpy reclaim. |
| 282 | */ |
| 283 | if (sync && sc->lumpy_reclaim_mode == LUMPY_MODE_NONE) |
| 284 | return; |
| 285 | |
| 286 | /* |
| 287 | * If we need a large contiguous chunk of memory, or have |
| 288 | * trouble getting a small set of contiguous pages, we |
| 289 | * will reclaim both active and inactive pages. |
| 290 | */ |
| 291 | if (sc->order > PAGE_ALLOC_COSTLY_ORDER) |
| 292 | sc->lumpy_reclaim_mode = mode; |
| 293 | else if (sc->order && priority < DEF_PRIORITY - 2) |
| 294 | sc->lumpy_reclaim_mode = mode; |
| 295 | else |
| 296 | sc->lumpy_reclaim_mode = LUMPY_MODE_NONE; |
| 297 | } |
| 298 | |
| 299 | static void disable_lumpy_reclaim_mode(struct scan_control *sc) |
| 300 | { |
| 301 | sc->lumpy_reclaim_mode = LUMPY_MODE_NONE; |
| 302 | } |
| 303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | static inline int is_page_cache_freeable(struct page *page) |
| 305 | { |
Johannes Weiner | ceddc3a | 2009-09-21 17:03:00 -0700 | [diff] [blame] | 306 | /* |
| 307 | * A freeable page cache page is referenced only by the caller |
| 308 | * that isolated the page, the page cache radix tree and |
| 309 | * optional buffer heads at page->private. |
| 310 | */ |
Johannes Weiner | edcf474 | 2009-09-21 17:02:59 -0700 | [diff] [blame] | 311 | return page_count(page) - page_has_private(page) == 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 314 | static int may_write_to_queue(struct backing_dev_info *bdi, |
| 315 | struct scan_control *sc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | { |
Christoph Lameter | 930d915 | 2006-01-08 01:00:47 -0800 | [diff] [blame] | 317 | if (current->flags & PF_SWAPWRITE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | return 1; |
| 319 | if (!bdi_write_congested(bdi)) |
| 320 | return 1; |
| 321 | if (bdi == current->backing_dev_info) |
| 322 | return 1; |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 323 | |
| 324 | /* lumpy reclaim for hugepage often need a lot of write */ |
| 325 | if (sc->order > PAGE_ALLOC_COSTLY_ORDER) |
| 326 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * We detected a synchronous write error writing a page out. Probably |
| 332 | * -ENOSPC. We need to propagate that into the address_space for a subsequent |
| 333 | * fsync(), msync() or close(). |
| 334 | * |
| 335 | * The tricky part is that after writepage we cannot touch the mapping: nothing |
| 336 | * prevents it from being freed up. But we have a ref on the page and once |
| 337 | * that page is locked, the mapping is pinned. |
| 338 | * |
| 339 | * We're allowed to run sleeping lock_page() here because we know the caller has |
| 340 | * __GFP_FS. |
| 341 | */ |
| 342 | static void handle_write_error(struct address_space *mapping, |
| 343 | struct page *page, int error) |
| 344 | { |
Nick Piggin | a6aa62a | 2010-07-20 13:24:25 -0700 | [diff] [blame] | 345 | lock_page_nosync(page); |
Guillaume Chazarain | 3e9f45b | 2007-05-08 00:23:25 -0700 | [diff] [blame] | 346 | if (page_mapping(page) == mapping) |
| 347 | mapping_set_error(mapping, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | unlock_page(page); |
| 349 | } |
| 350 | |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 351 | /* possible outcome of pageout() */ |
| 352 | typedef enum { |
| 353 | /* failed to write page out, page is locked */ |
| 354 | PAGE_KEEP, |
| 355 | /* move page to the active list, page is locked */ |
| 356 | PAGE_ACTIVATE, |
| 357 | /* page has been sent to the disk successfully, page is unlocked */ |
| 358 | PAGE_SUCCESS, |
| 359 | /* page is clean and locked */ |
| 360 | PAGE_CLEAN, |
| 361 | } pageout_t; |
| 362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | /* |
Andrew Morton | 1742f19 | 2006-03-22 00:08:21 -0800 | [diff] [blame] | 364 | * pageout is called by shrink_page_list() for each dirty page. |
| 365 | * Calls ->writepage(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | */ |
Andy Whitcroft | c661b07 | 2007-08-22 14:01:26 -0700 | [diff] [blame] | 367 | static pageout_t pageout(struct page *page, struct address_space *mapping, |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 368 | struct scan_control *sc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | { |
| 370 | /* |
| 371 | * If the page is dirty, only perform writeback if that write |
| 372 | * will be non-blocking. To prevent this allocation from being |
| 373 | * stalled by pagecache activity. But note that there may be |
| 374 | * stalls if we need to run get_block(). We could test |
| 375 | * PagePrivate for that. |
| 376 | * |
Vincent Li | 6aceb53 | 2009-12-14 17:58:49 -0800 | [diff] [blame] | 377 | * If this process is currently in __generic_file_aio_write() against |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | * this page's queue, we can perform writeback even if that |
| 379 | * will block. |
| 380 | * |
| 381 | * If the page is swapcache, write it back even if that would |
| 382 | * block, for some throttling. This happens by accident, because |
| 383 | * swap_backing_dev_info is bust: it doesn't reflect the |
| 384 | * congestion state of the swapdevs. Easy to fix, if needed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | */ |
| 386 | if (!is_page_cache_freeable(page)) |
| 387 | return PAGE_KEEP; |
| 388 | if (!mapping) { |
| 389 | /* |
| 390 | * Some data journaling orphaned pages can have |
| 391 | * page->mapping == NULL while being dirty with clean buffers. |
| 392 | */ |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 393 | if (page_has_private(page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | if (try_to_free_buffers(page)) { |
| 395 | ClearPageDirty(page); |
Harvey Harrison | d40cee2 | 2008-04-30 00:55:07 -0700 | [diff] [blame] | 396 | printk("%s: orphaned page\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | return PAGE_CLEAN; |
| 398 | } |
| 399 | } |
| 400 | return PAGE_KEEP; |
| 401 | } |
| 402 | if (mapping->a_ops->writepage == NULL) |
| 403 | return PAGE_ACTIVATE; |
Mel Gorman | 0e093d99 | 2010-10-26 14:21:45 -0700 | [diff] [blame] | 404 | if (!may_write_to_queue(mapping->backing_dev_info, sc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | return PAGE_KEEP; |
| 406 | |
| 407 | if (clear_page_dirty_for_io(page)) { |
| 408 | int res; |
| 409 | struct writeback_control wbc = { |
| 410 | .sync_mode = WB_SYNC_NONE, |
| 411 | .nr_to_write = SWAP_CLUSTER_MAX, |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 412 | .range_start = 0, |
| 413 | .range_end = LLONG_MAX, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | .for_reclaim = 1, |
| 415 | }; |
| 416 | |
| 417 | SetPageReclaim(page); |
| 418 | res = mapping->a_ops->writepage(page, &wbc); |
| 419 | if (res < 0) |
| 420 | handle_write_error(mapping, page, res); |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 421 | if (res == AOP_WRITEPAGE_ACTIVATE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | ClearPageReclaim(page); |
| 423 | return PAGE_ACTIVATE; |
| 424 | } |
Andy Whitcroft | c661b07 | 2007-08-22 14:01:26 -0700 | [diff] [blame] | 425 | |
| 426 | /* |
| 427 | * Wait on writeback if requested to. This happens when |
| 428 | * direct reclaiming a large contiguous area and the |
| 429 | * first attempt to free a range of pages fails. |
| 430 | */ |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 431 | if (PageWriteback(page) && |
| 432 | sc->lumpy_reclaim_mode == LUMPY_MODE_SYNC) |
Andy Whitcroft | c661b07 | 2007-08-22 14:01:26 -0700 | [diff] [blame] | 433 | wait_on_page_writeback(page); |
| 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | if (!PageWriteback(page)) { |
| 436 | /* synchronous write or broken a_ops? */ |
| 437 | ClearPageReclaim(page); |
| 438 | } |
Mel Gorman | 755f022 | 2010-08-09 17:19:18 -0700 | [diff] [blame] | 439 | trace_mm_vmscan_writepage(page, |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 440 | trace_reclaim_flags(page, sc->lumpy_reclaim_mode)); |
Andrew Morton | e129b5c | 2006-09-27 01:50:00 -0700 | [diff] [blame] | 441 | inc_zone_page_state(page, NR_VMSCAN_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | return PAGE_SUCCESS; |
| 443 | } |
| 444 | |
| 445 | return PAGE_CLEAN; |
| 446 | } |
| 447 | |
Andrew Morton | a649fd9 | 2006-10-17 00:09:36 -0700 | [diff] [blame] | 448 | /* |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 449 | * Same as remove_mapping, but if the page is removed from the mapping, it |
| 450 | * gets returned with a refcount of 0. |
Andrew Morton | a649fd9 | 2006-10-17 00:09:36 -0700 | [diff] [blame] | 451 | */ |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 452 | static int __remove_mapping(struct address_space *mapping, struct page *page) |
Christoph Lameter | 49d2e9c | 2006-01-08 01:00:48 -0800 | [diff] [blame] | 453 | { |
Nick Piggin | 28e4d96 | 2006-09-25 23:31:23 -0700 | [diff] [blame] | 454 | BUG_ON(!PageLocked(page)); |
| 455 | BUG_ON(mapping != page_mapping(page)); |
Christoph Lameter | 49d2e9c | 2006-01-08 01:00:48 -0800 | [diff] [blame] | 456 | |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 457 | spin_lock_irq(&mapping->tree_lock); |
Christoph Lameter | 49d2e9c | 2006-01-08 01:00:48 -0800 | [diff] [blame] | 458 | /* |
Nick Piggin | 0fd0e6b | 2006-09-27 01:50:02 -0700 | [diff] [blame] | 459 | * The non racy check for a busy page. |
| 460 | * |
| 461 | * Must be careful with the order of the tests. When someone has |
| 462 | * a ref to the page, it may be possible that they dirty it then |
| 463 | * drop the reference. So if PageDirty is tested before page_count |
| 464 | * here, then the following race may occur: |
| 465 | * |
| 466 | * get_user_pages(&page); |
| 467 | * [user mapping goes away] |
| 468 | * write_to(page); |
| 469 | * !PageDirty(page) [good] |
| 470 | * SetPageDirty(page); |
| 471 | * put_page(page); |
| 472 | * !page_count(page) [good, discard it] |
| 473 | * |
| 474 | * [oops, our write_to data is lost] |
| 475 | * |
| 476 | * Reversing the order of the tests ensures such a situation cannot |
| 477 | * escape unnoticed. The smp_rmb is needed to ensure the page->flags |
| 478 | * load is not satisfied before that of page->_count. |
| 479 | * |
| 480 | * Note that if SetPageDirty is always performed via set_page_dirty, |
| 481 | * and thus under tree_lock, then this ordering is not required. |
Christoph Lameter | 49d2e9c | 2006-01-08 01:00:48 -0800 | [diff] [blame] | 482 | */ |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 483 | if (!page_freeze_refs(page, 2)) |
Christoph Lameter | 49d2e9c | 2006-01-08 01:00:48 -0800 | [diff] [blame] | 484 | goto cannot_free; |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 485 | /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ |
| 486 | if (unlikely(PageDirty(page))) { |
| 487 | page_unfreeze_refs(page, 2); |
Christoph Lameter | 49d2e9c | 2006-01-08 01:00:48 -0800 | [diff] [blame] | 488 | goto cannot_free; |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 489 | } |
Christoph Lameter | 49d2e9c | 2006-01-08 01:00:48 -0800 | [diff] [blame] | 490 | |
| 491 | if (PageSwapCache(page)) { |
| 492 | swp_entry_t swap = { .val = page_private(page) }; |
| 493 | __delete_from_swap_cache(page); |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 494 | spin_unlock_irq(&mapping->tree_lock); |
KAMEZAWA Hiroyuki | cb4b86b | 2009-06-16 15:32:52 -0700 | [diff] [blame] | 495 | swapcache_free(swap, page); |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 496 | } else { |
Linus Torvalds | 6072d13 | 2010-12-01 13:35:19 -0500 | [diff] [blame] | 497 | void (*freepage)(struct page *); |
| 498 | |
| 499 | freepage = mapping->a_ops->freepage; |
| 500 | |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 501 | __remove_from_page_cache(page); |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 502 | spin_unlock_irq(&mapping->tree_lock); |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 503 | mem_cgroup_uncharge_cache_page(page); |
Linus Torvalds | 6072d13 | 2010-12-01 13:35:19 -0500 | [diff] [blame] | 504 | |
| 505 | if (freepage != NULL) |
| 506 | freepage(page); |
Christoph Lameter | 49d2e9c | 2006-01-08 01:00:48 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Christoph Lameter | 49d2e9c | 2006-01-08 01:00:48 -0800 | [diff] [blame] | 509 | return 1; |
| 510 | |
| 511 | cannot_free: |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 512 | spin_unlock_irq(&mapping->tree_lock); |
Christoph Lameter | 49d2e9c | 2006-01-08 01:00:48 -0800 | [diff] [blame] | 513 | return 0; |
| 514 | } |
| 515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | /* |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 517 | * Attempt to detach a locked page from its ->mapping. If it is dirty or if |
| 518 | * someone else has a ref on the page, abort and return 0. If it was |
| 519 | * successfully detached, return 1. Assumes the caller has a single ref on |
| 520 | * this page. |
| 521 | */ |
| 522 | int remove_mapping(struct address_space *mapping, struct page *page) |
| 523 | { |
| 524 | if (__remove_mapping(mapping, page)) { |
| 525 | /* |
| 526 | * Unfreezing the refcount with 1 rather than 2 effectively |
| 527 | * drops the pagecache ref for us without requiring another |
| 528 | * atomic operation. |
| 529 | */ |
| 530 | page_unfreeze_refs(page, 1); |
| 531 | return 1; |
| 532 | } |
| 533 | return 0; |
| 534 | } |
| 535 | |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 536 | /** |
| 537 | * putback_lru_page - put previously isolated page onto appropriate LRU list |
| 538 | * @page: page to be put back to appropriate lru list |
| 539 | * |
| 540 | * Add previously isolated @page to appropriate LRU list. |
| 541 | * Page may still be unevictable for other reasons. |
| 542 | * |
| 543 | * lru_lock must not be held, interrupts must be enabled. |
| 544 | */ |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 545 | void putback_lru_page(struct page *page) |
| 546 | { |
| 547 | int lru; |
| 548 | int active = !!TestClearPageActive(page); |
Lee Schermerhorn | bbfd28e | 2008-10-18 20:26:40 -0700 | [diff] [blame] | 549 | int was_unevictable = PageUnevictable(page); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 550 | |
| 551 | VM_BUG_ON(PageLRU(page)); |
| 552 | |
| 553 | redo: |
| 554 | ClearPageUnevictable(page); |
| 555 | |
| 556 | if (page_evictable(page, NULL)) { |
| 557 | /* |
| 558 | * For evictable pages, we can use the cache. |
| 559 | * In event of a race, worst case is we end up with an |
| 560 | * unevictable page on [in]active list. |
| 561 | * We know how to handle that. |
| 562 | */ |
Johannes Weiner | 401a8e1 | 2009-09-21 17:02:58 -0700 | [diff] [blame] | 563 | lru = active + page_lru_base_type(page); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 564 | lru_cache_add_lru(page, lru); |
| 565 | } else { |
| 566 | /* |
| 567 | * Put unevictable pages directly on zone's unevictable |
| 568 | * list. |
| 569 | */ |
| 570 | lru = LRU_UNEVICTABLE; |
| 571 | add_page_to_unevictable_list(page); |
Johannes Weiner | 6a7b954 | 2009-10-26 16:50:00 -0700 | [diff] [blame] | 572 | /* |
| 573 | * When racing with an mlock clearing (page is |
| 574 | * unlocked), make sure that if the other thread does |
| 575 | * not observe our setting of PG_lru and fails |
| 576 | * isolation, we see PG_mlocked cleared below and move |
| 577 | * the page back to the evictable list. |
| 578 | * |
| 579 | * The other side is TestClearPageMlocked(). |
| 580 | */ |
| 581 | smp_mb(); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 582 | } |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 583 | |
| 584 | /* |
| 585 | * page's status can change while we move it among lru. If an evictable |
| 586 | * page is on unevictable list, it never be freed. To avoid that, |
| 587 | * check after we added it to the list, again. |
| 588 | */ |
| 589 | if (lru == LRU_UNEVICTABLE && page_evictable(page, NULL)) { |
| 590 | if (!isolate_lru_page(page)) { |
| 591 | put_page(page); |
| 592 | goto redo; |
| 593 | } |
| 594 | /* This means someone else dropped this page from LRU |
| 595 | * So, it will be freed or putback to LRU again. There is |
| 596 | * nothing to do here. |
| 597 | */ |
| 598 | } |
| 599 | |
Lee Schermerhorn | bbfd28e | 2008-10-18 20:26:40 -0700 | [diff] [blame] | 600 | if (was_unevictable && lru != LRU_UNEVICTABLE) |
| 601 | count_vm_event(UNEVICTABLE_PGRESCUED); |
| 602 | else if (!was_unevictable && lru == LRU_UNEVICTABLE) |
| 603 | count_vm_event(UNEVICTABLE_PGCULLED); |
| 604 | |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 605 | put_page(page); /* drop ref from isolate */ |
| 606 | } |
| 607 | |
Johannes Weiner | dfc8d63 | 2010-03-05 13:42:19 -0800 | [diff] [blame] | 608 | enum page_references { |
| 609 | PAGEREF_RECLAIM, |
| 610 | PAGEREF_RECLAIM_CLEAN, |
Johannes Weiner | 64574746 | 2010-03-05 13:42:22 -0800 | [diff] [blame] | 611 | PAGEREF_KEEP, |
Johannes Weiner | dfc8d63 | 2010-03-05 13:42:19 -0800 | [diff] [blame] | 612 | PAGEREF_ACTIVATE, |
| 613 | }; |
| 614 | |
| 615 | static enum page_references page_check_references(struct page *page, |
| 616 | struct scan_control *sc) |
| 617 | { |
Johannes Weiner | 64574746 | 2010-03-05 13:42:22 -0800 | [diff] [blame] | 618 | int referenced_ptes, referenced_page; |
Johannes Weiner | dfc8d63 | 2010-03-05 13:42:19 -0800 | [diff] [blame] | 619 | unsigned long vm_flags; |
Johannes Weiner | dfc8d63 | 2010-03-05 13:42:19 -0800 | [diff] [blame] | 620 | |
Johannes Weiner | 64574746 | 2010-03-05 13:42:22 -0800 | [diff] [blame] | 621 | referenced_ptes = page_referenced(page, 1, sc->mem_cgroup, &vm_flags); |
| 622 | referenced_page = TestClearPageReferenced(page); |
Johannes Weiner | dfc8d63 | 2010-03-05 13:42:19 -0800 | [diff] [blame] | 623 | |
| 624 | /* Lumpy reclaim - ignore references */ |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 625 | if (sc->lumpy_reclaim_mode != LUMPY_MODE_NONE) |
Johannes Weiner | dfc8d63 | 2010-03-05 13:42:19 -0800 | [diff] [blame] | 626 | return PAGEREF_RECLAIM; |
| 627 | |
| 628 | /* |
| 629 | * Mlock lost the isolation race with us. Let try_to_unmap() |
| 630 | * move the page to the unevictable list. |
| 631 | */ |
| 632 | if (vm_flags & VM_LOCKED) |
| 633 | return PAGEREF_RECLAIM; |
| 634 | |
Johannes Weiner | 64574746 | 2010-03-05 13:42:22 -0800 | [diff] [blame] | 635 | if (referenced_ptes) { |
| 636 | if (PageAnon(page)) |
| 637 | return PAGEREF_ACTIVATE; |
| 638 | /* |
| 639 | * All mapped pages start out with page table |
| 640 | * references from the instantiating fault, so we need |
| 641 | * to look twice if a mapped file page is used more |
| 642 | * than once. |
| 643 | * |
| 644 | * Mark it and spare it for another trip around the |
| 645 | * inactive list. Another page table reference will |
| 646 | * lead to its activation. |
| 647 | * |
| 648 | * Note: the mark is set for activated pages as well |
| 649 | * so that recently deactivated but used pages are |
| 650 | * quickly recovered. |
| 651 | */ |
| 652 | SetPageReferenced(page); |
| 653 | |
| 654 | if (referenced_page) |
| 655 | return PAGEREF_ACTIVATE; |
| 656 | |
| 657 | return PAGEREF_KEEP; |
| 658 | } |
Johannes Weiner | dfc8d63 | 2010-03-05 13:42:19 -0800 | [diff] [blame] | 659 | |
| 660 | /* Reclaim if clean, defer dirty pages to writeback */ |
KOSAKI Motohiro | 2e30244 | 2010-10-26 14:21:46 -0700 | [diff] [blame] | 661 | if (referenced_page && !PageSwapBacked(page)) |
Johannes Weiner | 64574746 | 2010-03-05 13:42:22 -0800 | [diff] [blame] | 662 | return PAGEREF_RECLAIM_CLEAN; |
| 663 | |
| 664 | return PAGEREF_RECLAIM; |
Johannes Weiner | dfc8d63 | 2010-03-05 13:42:19 -0800 | [diff] [blame] | 665 | } |
| 666 | |
Mel Gorman | abe4c3b | 2010-08-09 17:19:31 -0700 | [diff] [blame] | 667 | static noinline_for_stack void free_page_list(struct list_head *free_pages) |
| 668 | { |
| 669 | struct pagevec freed_pvec; |
| 670 | struct page *page, *tmp; |
| 671 | |
| 672 | pagevec_init(&freed_pvec, 1); |
| 673 | |
| 674 | list_for_each_entry_safe(page, tmp, free_pages, lru) { |
| 675 | list_del(&page->lru); |
| 676 | if (!pagevec_add(&freed_pvec, page)) { |
| 677 | __pagevec_free(&freed_pvec); |
| 678 | pagevec_reinit(&freed_pvec); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | pagevec_free(&freed_pvec); |
| 683 | } |
| 684 | |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 685 | /* |
Andrew Morton | 1742f19 | 2006-03-22 00:08:21 -0800 | [diff] [blame] | 686 | * shrink_page_list() returns the number of reclaimed pages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | */ |
Andrew Morton | 1742f19 | 2006-03-22 00:08:21 -0800 | [diff] [blame] | 688 | static unsigned long shrink_page_list(struct list_head *page_list, |
Mel Gorman | 0e093d99 | 2010-10-26 14:21:45 -0700 | [diff] [blame] | 689 | struct zone *zone, |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 690 | struct scan_control *sc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { |
| 692 | LIST_HEAD(ret_pages); |
Mel Gorman | abe4c3b | 2010-08-09 17:19:31 -0700 | [diff] [blame] | 693 | LIST_HEAD(free_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | int pgactivate = 0; |
Mel Gorman | 0e093d99 | 2010-10-26 14:21:45 -0700 | [diff] [blame] | 695 | unsigned long nr_dirty = 0; |
| 696 | unsigned long nr_congested = 0; |
Andrew Morton | 05ff513 | 2006-03-22 00:08:20 -0800 | [diff] [blame] | 697 | unsigned long nr_reclaimed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
| 699 | cond_resched(); |
| 700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | while (!list_empty(page_list)) { |
Johannes Weiner | dfc8d63 | 2010-03-05 13:42:19 -0800 | [diff] [blame] | 702 | enum page_references references; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | struct address_space *mapping; |
| 704 | struct page *page; |
| 705 | int may_enter_fs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
| 707 | cond_resched(); |
| 708 | |
| 709 | page = lru_to_page(page_list); |
| 710 | list_del(&page->lru); |
| 711 | |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 712 | if (!trylock_page(page)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | goto keep; |
| 714 | |
Nick Piggin | 725d704 | 2006-09-25 23:30:55 -0700 | [diff] [blame] | 715 | VM_BUG_ON(PageActive(page)); |
Mel Gorman | 0e093d99 | 2010-10-26 14:21:45 -0700 | [diff] [blame] | 716 | VM_BUG_ON(page_zone(page) != zone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
| 718 | sc->nr_scanned++; |
Christoph Lameter | 80e4342 | 2006-02-11 17:55:53 -0800 | [diff] [blame] | 719 | |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 720 | if (unlikely(!page_evictable(page, NULL))) |
| 721 | goto cull_mlocked; |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 722 | |
Johannes Weiner | a6dc60f8 | 2009-03-31 15:19:30 -0700 | [diff] [blame] | 723 | if (!sc->may_unmap && page_mapped(page)) |
Christoph Lameter | 80e4342 | 2006-02-11 17:55:53 -0800 | [diff] [blame] | 724 | goto keep_locked; |
| 725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | /* Double the slab pressure for mapped and swapcache pages */ |
| 727 | if (page_mapped(page) || PageSwapCache(page)) |
| 728 | sc->nr_scanned++; |
| 729 | |
Andy Whitcroft | c661b07 | 2007-08-22 14:01:26 -0700 | [diff] [blame] | 730 | may_enter_fs = (sc->gfp_mask & __GFP_FS) || |
| 731 | (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); |
| 732 | |
| 733 | if (PageWriteback(page)) { |
| 734 | /* |
| 735 | * Synchronous reclaim is performed in two passes, |
| 736 | * first an asynchronous pass over the list to |
| 737 | * start parallel writeback, and a second synchronous |
| 738 | * pass to wait for the IO to complete. Wait here |
| 739 | * for any page for which writeback has already |
| 740 | * started. |
| 741 | */ |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 742 | if (sc->lumpy_reclaim_mode == LUMPY_MODE_SYNC && |
| 743 | may_enter_fs) |
Andy Whitcroft | c661b07 | 2007-08-22 14:01:26 -0700 | [diff] [blame] | 744 | wait_on_page_writeback(page); |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 745 | else { |
| 746 | unlock_page(page); |
| 747 | goto keep_lumpy; |
| 748 | } |
Andy Whitcroft | c661b07 | 2007-08-22 14:01:26 -0700 | [diff] [blame] | 749 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
Johannes Weiner | dfc8d63 | 2010-03-05 13:42:19 -0800 | [diff] [blame] | 751 | references = page_check_references(page, sc); |
| 752 | switch (references) { |
| 753 | case PAGEREF_ACTIVATE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | goto activate_locked; |
Johannes Weiner | 64574746 | 2010-03-05 13:42:22 -0800 | [diff] [blame] | 755 | case PAGEREF_KEEP: |
| 756 | goto keep_locked; |
Johannes Weiner | dfc8d63 | 2010-03-05 13:42:19 -0800 | [diff] [blame] | 757 | case PAGEREF_RECLAIM: |
| 758 | case PAGEREF_RECLAIM_CLEAN: |
| 759 | ; /* try to reclaim the page below */ |
| 760 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | /* |
| 763 | * Anonymous process memory has backing store? |
| 764 | * Try to allocate it some swap space here. |
| 765 | */ |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 766 | if (PageAnon(page) && !PageSwapCache(page)) { |
Hugh Dickins | 63eb6b9 | 2008-11-19 15:36:37 -0800 | [diff] [blame] | 767 | if (!(sc->gfp_mask & __GFP_IO)) |
| 768 | goto keep_locked; |
Hugh Dickins | ac47b00 | 2009-01-06 14:39:39 -0800 | [diff] [blame] | 769 | if (!add_to_swap(page)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | goto activate_locked; |
Hugh Dickins | 63eb6b9 | 2008-11-19 15:36:37 -0800 | [diff] [blame] | 771 | may_enter_fs = 1; |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | |
| 774 | mapping = page_mapping(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
| 776 | /* |
| 777 | * The page is mapped into the page tables of one or more |
| 778 | * processes. Try to unmap it here. |
| 779 | */ |
| 780 | if (page_mapped(page) && mapping) { |
Andi Kleen | 14fa31b | 2009-09-16 11:50:10 +0200 | [diff] [blame] | 781 | switch (try_to_unmap(page, TTU_UNMAP)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | case SWAP_FAIL: |
| 783 | goto activate_locked; |
| 784 | case SWAP_AGAIN: |
| 785 | goto keep_locked; |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 786 | case SWAP_MLOCK: |
| 787 | goto cull_mlocked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | case SWAP_SUCCESS: |
| 789 | ; /* try to free the page below */ |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | if (PageDirty(page)) { |
Mel Gorman | 0e093d99 | 2010-10-26 14:21:45 -0700 | [diff] [blame] | 794 | nr_dirty++; |
| 795 | |
Johannes Weiner | dfc8d63 | 2010-03-05 13:42:19 -0800 | [diff] [blame] | 796 | if (references == PAGEREF_RECLAIM_CLEAN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | goto keep_locked; |
Andrew Morton | 4dd4b92 | 2008-03-24 12:29:52 -0700 | [diff] [blame] | 798 | if (!may_enter_fs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | goto keep_locked; |
Christoph Lameter | 52a8363 | 2006-02-01 03:05:28 -0800 | [diff] [blame] | 800 | if (!sc->may_writepage) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | goto keep_locked; |
| 802 | |
| 803 | /* Page is dirty, try to write it out here */ |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 804 | switch (pageout(page, mapping, sc)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | case PAGE_KEEP: |
Mel Gorman | 0e093d99 | 2010-10-26 14:21:45 -0700 | [diff] [blame] | 806 | nr_congested++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | goto keep_locked; |
| 808 | case PAGE_ACTIVATE: |
| 809 | goto activate_locked; |
| 810 | case PAGE_SUCCESS: |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 811 | if (PageWriteback(page)) |
| 812 | goto keep_lumpy; |
| 813 | if (PageDirty(page)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | goto keep; |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | /* |
| 817 | * A synchronous write - probably a ramdisk. Go |
| 818 | * ahead and try to reclaim the page. |
| 819 | */ |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 820 | if (!trylock_page(page)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | goto keep; |
| 822 | if (PageDirty(page) || PageWriteback(page)) |
| 823 | goto keep_locked; |
| 824 | mapping = page_mapping(page); |
| 825 | case PAGE_CLEAN: |
| 826 | ; /* try to free the page below */ |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | /* |
| 831 | * If the page has buffers, try to free the buffer mappings |
| 832 | * associated with this page. If we succeed we try to free |
| 833 | * the page as well. |
| 834 | * |
| 835 | * We do this even if the page is PageDirty(). |
| 836 | * try_to_release_page() does not perform I/O, but it is |
| 837 | * possible for a page to have PageDirty set, but it is actually |
| 838 | * clean (all its buffers are clean). This happens if the |
| 839 | * buffers were written out directly, with submit_bh(). ext3 |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 840 | * will do this, as well as the blockdev mapping. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | * try_to_release_page() will discover that cleanness and will |
| 842 | * drop the buffers and mark the page clean - it can be freed. |
| 843 | * |
| 844 | * Rarely, pages can have buffers and no ->mapping. These are |
| 845 | * the pages which were not successfully invalidated in |
| 846 | * truncate_complete_page(). We try to drop those buffers here |
| 847 | * and if that worked, and the page is no longer mapped into |
| 848 | * process address space (page_count == 1) it can be freed. |
| 849 | * Otherwise, leave the page on the LRU so it is swappable. |
| 850 | */ |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 851 | if (page_has_private(page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | if (!try_to_release_page(page, sc->gfp_mask)) |
| 853 | goto activate_locked; |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 854 | if (!mapping && page_count(page) == 1) { |
| 855 | unlock_page(page); |
| 856 | if (put_page_testzero(page)) |
| 857 | goto free_it; |
| 858 | else { |
| 859 | /* |
| 860 | * rare race with speculative reference. |
| 861 | * the speculative reference will free |
| 862 | * this page shortly, so we may |
| 863 | * increment nr_reclaimed here (and |
| 864 | * leave it off the LRU). |
| 865 | */ |
| 866 | nr_reclaimed++; |
| 867 | continue; |
| 868 | } |
| 869 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 872 | if (!mapping || !__remove_mapping(mapping, page)) |
Christoph Lameter | 49d2e9c | 2006-01-08 01:00:48 -0800 | [diff] [blame] | 873 | goto keep_locked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | |
Nick Piggin | a978d6f | 2008-10-18 20:26:58 -0700 | [diff] [blame] | 875 | /* |
| 876 | * At this point, we have no other references and there is |
| 877 | * no way to pick any more up (removed from LRU, removed |
| 878 | * from pagecache). Can use non-atomic bitops now (and |
| 879 | * we obviously don't have to worry about waking up a process |
| 880 | * waiting on the page lock, because there are no references. |
| 881 | */ |
| 882 | __clear_page_locked(page); |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 883 | free_it: |
Andrew Morton | 05ff513 | 2006-03-22 00:08:20 -0800 | [diff] [blame] | 884 | nr_reclaimed++; |
Mel Gorman | abe4c3b | 2010-08-09 17:19:31 -0700 | [diff] [blame] | 885 | |
| 886 | /* |
| 887 | * Is there need to periodically free_page_list? It would |
| 888 | * appear not as the counts should be low |
| 889 | */ |
| 890 | list_add(&page->lru, &free_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | continue; |
| 892 | |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 893 | cull_mlocked: |
Hugh Dickins | 63d6c5a | 2009-01-06 14:39:38 -0800 | [diff] [blame] | 894 | if (PageSwapCache(page)) |
| 895 | try_to_free_swap(page); |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 896 | unlock_page(page); |
| 897 | putback_lru_page(page); |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 898 | disable_lumpy_reclaim_mode(sc); |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 899 | continue; |
| 900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | activate_locked: |
Rik van Riel | 68a22394 | 2008-10-18 20:26:23 -0700 | [diff] [blame] | 902 | /* Not a candidate for swapping, so reclaim swap space. */ |
| 903 | if (PageSwapCache(page) && vm_swap_full()) |
Hugh Dickins | a2c43ee | 2009-01-06 14:39:36 -0800 | [diff] [blame] | 904 | try_to_free_swap(page); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 905 | VM_BUG_ON(PageActive(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | SetPageActive(page); |
| 907 | pgactivate++; |
| 908 | keep_locked: |
| 909 | unlock_page(page); |
| 910 | keep: |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 911 | disable_lumpy_reclaim_mode(sc); |
| 912 | keep_lumpy: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | list_add(&page->lru, &ret_pages); |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 914 | VM_BUG_ON(PageLRU(page) || PageUnevictable(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | } |
Mel Gorman | abe4c3b | 2010-08-09 17:19:31 -0700 | [diff] [blame] | 916 | |
Mel Gorman | 0e093d99 | 2010-10-26 14:21:45 -0700 | [diff] [blame] | 917 | /* |
| 918 | * Tag a zone as congested if all the dirty pages encountered were |
| 919 | * backed by a congested BDI. In this case, reclaimers should just |
| 920 | * back off and wait for congestion to clear because further reclaim |
| 921 | * will encounter the same problem |
| 922 | */ |
Shaohua Li | 1dce071e | 2010-11-11 14:05:17 -0800 | [diff] [blame] | 923 | if (nr_dirty == nr_congested && nr_dirty != 0) |
Mel Gorman | 0e093d99 | 2010-10-26 14:21:45 -0700 | [diff] [blame] | 924 | zone_set_flag(zone, ZONE_CONGESTED); |
| 925 | |
Mel Gorman | abe4c3b | 2010-08-09 17:19:31 -0700 | [diff] [blame] | 926 | free_page_list(&free_pages); |
| 927 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | list_splice(&ret_pages, page_list); |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 929 | count_vm_events(PGACTIVATE, pgactivate); |
Andrew Morton | 05ff513 | 2006-03-22 00:08:20 -0800 | [diff] [blame] | 930 | return nr_reclaimed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 933 | /* |
| 934 | * Attempt to remove the specified page from its LRU. Only take this page |
| 935 | * if it is of the appropriate PageActive status. Pages which are being |
| 936 | * freed elsewhere are also ignored. |
| 937 | * |
| 938 | * page: page to consider |
| 939 | * mode: one of the LRU isolation modes defined above |
| 940 | * |
| 941 | * returns 0 on success, -ve errno on failure. |
| 942 | */ |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 943 | int __isolate_lru_page(struct page *page, int mode, int file) |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 944 | { |
| 945 | int ret = -EINVAL; |
| 946 | |
| 947 | /* Only take pages on the LRU. */ |
| 948 | if (!PageLRU(page)) |
| 949 | return ret; |
| 950 | |
| 951 | /* |
| 952 | * When checking the active state, we need to be sure we are |
| 953 | * dealing with comparible boolean values. Take the logical not |
| 954 | * of each. |
| 955 | */ |
| 956 | if (mode != ISOLATE_BOTH && (!PageActive(page) != !mode)) |
| 957 | return ret; |
| 958 | |
Johannes Weiner | 6c0b135 | 2009-09-21 17:02:59 -0700 | [diff] [blame] | 959 | if (mode != ISOLATE_BOTH && page_is_file_cache(page) != file) |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 960 | return ret; |
| 961 | |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 962 | /* |
| 963 | * When this function is being called for lumpy reclaim, we |
| 964 | * initially look into all LRU pages, active, inactive and |
| 965 | * unevictable; only give shrink_page_list evictable pages. |
| 966 | */ |
| 967 | if (PageUnevictable(page)) |
| 968 | return ret; |
| 969 | |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 970 | ret = -EBUSY; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 971 | |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 972 | if (likely(get_page_unless_zero(page))) { |
| 973 | /* |
| 974 | * Be careful not to clear PageLRU until after we're |
| 975 | * sure the page is not being freed elsewhere -- the |
| 976 | * page release code relies on it. |
| 977 | */ |
| 978 | ClearPageLRU(page); |
| 979 | ret = 0; |
| 980 | } |
| 981 | |
| 982 | return ret; |
| 983 | } |
| 984 | |
Christoph Lameter | 49d2e9c | 2006-01-08 01:00:48 -0800 | [diff] [blame] | 985 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | * zone->lru_lock is heavily contended. Some of the functions that |
| 987 | * shrink the lists perform better by taking out a batch of pages |
| 988 | * and working on them outside the LRU lock. |
| 989 | * |
| 990 | * For pagecache intensive workloads, this function is the hottest |
| 991 | * spot in the kernel (apart from copy_*_user functions). |
| 992 | * |
| 993 | * Appropriate locks must be held before calling this function. |
| 994 | * |
| 995 | * @nr_to_scan: The number of pages to look through on the list. |
| 996 | * @src: The LRU list to pull pages off. |
| 997 | * @dst: The temp list to put pages on to. |
| 998 | * @scanned: The number of pages that were scanned. |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 999 | * @order: The caller's attempted allocation order |
| 1000 | * @mode: One of the LRU isolation modes |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1001 | * @file: True [1] if isolating file [!anon] pages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | * |
| 1003 | * returns how many pages were moved onto *@dst. |
| 1004 | */ |
Andrew Morton | 69e0594 | 2006-03-22 00:08:19 -0800 | [diff] [blame] | 1005 | static unsigned long isolate_lru_pages(unsigned long nr_to_scan, |
| 1006 | struct list_head *src, struct list_head *dst, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1007 | unsigned long *scanned, int order, int mode, int file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | { |
Andrew Morton | 69e0594 | 2006-03-22 00:08:19 -0800 | [diff] [blame] | 1009 | unsigned long nr_taken = 0; |
Mel Gorman | a8a94d1 | 2010-08-09 17:19:17 -0700 | [diff] [blame] | 1010 | unsigned long nr_lumpy_taken = 0; |
| 1011 | unsigned long nr_lumpy_dirty = 0; |
| 1012 | unsigned long nr_lumpy_failed = 0; |
Wu Fengguang | c9b02d9 | 2006-03-22 00:08:23 -0800 | [diff] [blame] | 1013 | unsigned long scan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | |
Wu Fengguang | c9b02d9 | 2006-03-22 00:08:23 -0800 | [diff] [blame] | 1015 | for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) { |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 1016 | struct page *page; |
| 1017 | unsigned long pfn; |
| 1018 | unsigned long end_pfn; |
| 1019 | unsigned long page_pfn; |
| 1020 | int zone_id; |
| 1021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | page = lru_to_page(src); |
| 1023 | prefetchw_prev_lru_page(page, src, flags); |
| 1024 | |
Nick Piggin | 725d704 | 2006-09-25 23:30:55 -0700 | [diff] [blame] | 1025 | VM_BUG_ON(!PageLRU(page)); |
Nick Piggin | 8d438f9 | 2006-03-22 00:07:59 -0800 | [diff] [blame] | 1026 | |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1027 | switch (__isolate_lru_page(page, mode, file)) { |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 1028 | case 0: |
| 1029 | list_move(&page->lru, dst); |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1030 | mem_cgroup_del_lru(page); |
Nick Piggin | 7c8ee9a | 2006-03-22 00:08:03 -0800 | [diff] [blame] | 1031 | nr_taken++; |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 1032 | break; |
Nick Piggin | 46453a6 | 2006-03-22 00:07:58 -0800 | [diff] [blame] | 1033 | |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 1034 | case -EBUSY: |
| 1035 | /* else it is being freed elsewhere */ |
| 1036 | list_move(&page->lru, src); |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 1037 | mem_cgroup_rotate_lru_list(page, page_lru(page)); |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 1038 | continue; |
| 1039 | |
| 1040 | default: |
| 1041 | BUG(); |
| 1042 | } |
| 1043 | |
| 1044 | if (!order) |
| 1045 | continue; |
| 1046 | |
| 1047 | /* |
| 1048 | * Attempt to take all pages in the order aligned region |
| 1049 | * surrounding the tag page. Only take those pages of |
| 1050 | * the same active state as that tag page. We may safely |
| 1051 | * round the target page pfn down to the requested order |
| 1052 | * as the mem_map is guarenteed valid out to MAX_ORDER, |
| 1053 | * where that page is in a different zone we will detect |
| 1054 | * it from its zone id and abort this block scan. |
| 1055 | */ |
| 1056 | zone_id = page_zone_id(page); |
| 1057 | page_pfn = page_to_pfn(page); |
| 1058 | pfn = page_pfn & ~((1 << order) - 1); |
| 1059 | end_pfn = pfn + (1 << order); |
| 1060 | for (; pfn < end_pfn; pfn++) { |
| 1061 | struct page *cursor_page; |
| 1062 | |
| 1063 | /* The target page is in the block, ignore it. */ |
| 1064 | if (unlikely(pfn == page_pfn)) |
| 1065 | continue; |
| 1066 | |
| 1067 | /* Avoid holes within the zone. */ |
| 1068 | if (unlikely(!pfn_valid_within(pfn))) |
| 1069 | break; |
| 1070 | |
| 1071 | cursor_page = pfn_to_page(pfn); |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1072 | |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 1073 | /* Check that we have not crossed a zone boundary. */ |
| 1074 | if (unlikely(page_zone_id(cursor_page) != zone_id)) |
KOSAKI Motohiro | 08fc468 | 2010-10-26 14:21:44 -0700 | [diff] [blame] | 1075 | break; |
Minchan Kim | de2e756 | 2009-09-21 17:01:43 -0700 | [diff] [blame] | 1076 | |
| 1077 | /* |
| 1078 | * If we don't have enough swap space, reclaiming of |
| 1079 | * anon page which don't already have a swap slot is |
| 1080 | * pointless. |
| 1081 | */ |
| 1082 | if (nr_swap_pages <= 0 && PageAnon(cursor_page) && |
KOSAKI Motohiro | 08fc468 | 2010-10-26 14:21:44 -0700 | [diff] [blame] | 1083 | !PageSwapCache(cursor_page)) |
| 1084 | break; |
Minchan Kim | de2e756 | 2009-09-21 17:01:43 -0700 | [diff] [blame] | 1085 | |
KAMEZAWA Hiroyuki | ee993b1 | 2009-06-16 15:33:24 -0700 | [diff] [blame] | 1086 | if (__isolate_lru_page(cursor_page, mode, file) == 0) { |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 1087 | list_move(&cursor_page->lru, dst); |
KAMEZAWA Hiroyuki | cb4cbcf | 2009-06-23 08:57:55 +0900 | [diff] [blame] | 1088 | mem_cgroup_del_lru(cursor_page); |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 1089 | nr_taken++; |
Mel Gorman | a8a94d1 | 2010-08-09 17:19:17 -0700 | [diff] [blame] | 1090 | nr_lumpy_taken++; |
| 1091 | if (PageDirty(cursor_page)) |
| 1092 | nr_lumpy_dirty++; |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 1093 | scan++; |
Mel Gorman | a8a94d1 | 2010-08-09 17:19:17 -0700 | [diff] [blame] | 1094 | } else { |
KOSAKI Motohiro | 08fc468 | 2010-10-26 14:21:44 -0700 | [diff] [blame] | 1095 | /* the page is freed already. */ |
| 1096 | if (!page_count(cursor_page)) |
| 1097 | continue; |
| 1098 | break; |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 1099 | } |
| 1100 | } |
KOSAKI Motohiro | 08fc468 | 2010-10-26 14:21:44 -0700 | [diff] [blame] | 1101 | |
| 1102 | /* If we break out of the loop above, lumpy reclaim failed */ |
| 1103 | if (pfn < end_pfn) |
| 1104 | nr_lumpy_failed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | *scanned = scan; |
Mel Gorman | a8a94d1 | 2010-08-09 17:19:17 -0700 | [diff] [blame] | 1108 | |
| 1109 | trace_mm_vmscan_lru_isolate(order, |
| 1110 | nr_to_scan, scan, |
| 1111 | nr_taken, |
| 1112 | nr_lumpy_taken, nr_lumpy_dirty, nr_lumpy_failed, |
| 1113 | mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | return nr_taken; |
| 1115 | } |
| 1116 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1117 | static unsigned long isolate_pages_global(unsigned long nr, |
| 1118 | struct list_head *dst, |
| 1119 | unsigned long *scanned, int order, |
| 1120 | int mode, struct zone *z, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1121 | int active, int file) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1122 | { |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1123 | int lru = LRU_BASE; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1124 | if (active) |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1125 | lru += LRU_ACTIVE; |
| 1126 | if (file) |
| 1127 | lru += LRU_FILE; |
| 1128 | return isolate_lru_pages(nr, &z->lru[lru].list, dst, scanned, order, |
Johannes Weiner | b7c46d1 | 2009-09-21 17:02:56 -0700 | [diff] [blame] | 1129 | mode, file); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | /* |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 1133 | * clear_active_flags() is a helper for shrink_active_list(), clearing |
| 1134 | * any active bits from the pages in the list. |
| 1135 | */ |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1136 | static unsigned long clear_active_flags(struct list_head *page_list, |
| 1137 | unsigned int *count) |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 1138 | { |
| 1139 | int nr_active = 0; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1140 | int lru; |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 1141 | struct page *page; |
| 1142 | |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1143 | list_for_each_entry(page, page_list, lru) { |
Johannes Weiner | 401a8e1 | 2009-09-21 17:02:58 -0700 | [diff] [blame] | 1144 | lru = page_lru_base_type(page); |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 1145 | if (PageActive(page)) { |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1146 | lru += LRU_ACTIVE; |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 1147 | ClearPageActive(page); |
| 1148 | nr_active++; |
| 1149 | } |
Mel Gorman | 1489fa1 | 2010-08-09 17:19:33 -0700 | [diff] [blame] | 1150 | if (count) |
| 1151 | count[lru]++; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1152 | } |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 1153 | |
| 1154 | return nr_active; |
| 1155 | } |
| 1156 | |
Nick Piggin | 62695a8 | 2008-10-18 20:26:09 -0700 | [diff] [blame] | 1157 | /** |
| 1158 | * isolate_lru_page - tries to isolate a page from its LRU list |
| 1159 | * @page: page to isolate from its LRU list |
| 1160 | * |
| 1161 | * Isolates a @page from an LRU list, clears PageLRU and adjusts the |
| 1162 | * vmstat statistic corresponding to whatever LRU list the page was on. |
| 1163 | * |
| 1164 | * Returns 0 if the page was removed from an LRU list. |
| 1165 | * Returns -EBUSY if the page was not on an LRU list. |
| 1166 | * |
| 1167 | * The returned page will have PageLRU() cleared. If it was found on |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 1168 | * the active list, it will have PageActive set. If it was found on |
| 1169 | * the unevictable list, it will have the PageUnevictable bit set. That flag |
| 1170 | * may need to be cleared by the caller before letting the page go. |
Nick Piggin | 62695a8 | 2008-10-18 20:26:09 -0700 | [diff] [blame] | 1171 | * |
| 1172 | * The vmstat statistic corresponding to the list on which the page was |
| 1173 | * found will be decremented. |
| 1174 | * |
| 1175 | * Restrictions: |
| 1176 | * (1) Must be called with an elevated refcount on the page. This is a |
| 1177 | * fundamentnal difference from isolate_lru_pages (which is called |
| 1178 | * without a stable reference). |
| 1179 | * (2) the lru_lock must not be held. |
| 1180 | * (3) interrupts must be enabled. |
| 1181 | */ |
| 1182 | int isolate_lru_page(struct page *page) |
| 1183 | { |
| 1184 | int ret = -EBUSY; |
| 1185 | |
| 1186 | if (PageLRU(page)) { |
| 1187 | struct zone *zone = page_zone(page); |
| 1188 | |
| 1189 | spin_lock_irq(&zone->lru_lock); |
| 1190 | if (PageLRU(page) && get_page_unless_zero(page)) { |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 1191 | int lru = page_lru(page); |
Nick Piggin | 62695a8 | 2008-10-18 20:26:09 -0700 | [diff] [blame] | 1192 | ret = 0; |
| 1193 | ClearPageLRU(page); |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1194 | |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1195 | del_page_from_lru_list(zone, page, lru); |
Nick Piggin | 62695a8 | 2008-10-18 20:26:09 -0700 | [diff] [blame] | 1196 | } |
| 1197 | spin_unlock_irq(&zone->lru_lock); |
| 1198 | } |
| 1199 | return ret; |
| 1200 | } |
| 1201 | |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 1202 | /* |
Rik van Riel | 35cd781 | 2009-09-21 17:01:38 -0700 | [diff] [blame] | 1203 | * Are there way too many processes in the direct reclaim path already? |
| 1204 | */ |
| 1205 | static int too_many_isolated(struct zone *zone, int file, |
| 1206 | struct scan_control *sc) |
| 1207 | { |
| 1208 | unsigned long inactive, isolated; |
| 1209 | |
| 1210 | if (current_is_kswapd()) |
| 1211 | return 0; |
| 1212 | |
| 1213 | if (!scanning_global_lru(sc)) |
| 1214 | return 0; |
| 1215 | |
| 1216 | if (file) { |
| 1217 | inactive = zone_page_state(zone, NR_INACTIVE_FILE); |
| 1218 | isolated = zone_page_state(zone, NR_ISOLATED_FILE); |
| 1219 | } else { |
| 1220 | inactive = zone_page_state(zone, NR_INACTIVE_ANON); |
| 1221 | isolated = zone_page_state(zone, NR_ISOLATED_ANON); |
| 1222 | } |
| 1223 | |
| 1224 | return isolated > inactive; |
| 1225 | } |
| 1226 | |
| 1227 | /* |
Mel Gorman | 6663562 | 2010-08-09 17:19:30 -0700 | [diff] [blame] | 1228 | * TODO: Try merging with migrations version of putback_lru_pages |
| 1229 | */ |
| 1230 | static noinline_for_stack void |
Mel Gorman | 1489fa1 | 2010-08-09 17:19:33 -0700 | [diff] [blame] | 1231 | putback_lru_pages(struct zone *zone, struct scan_control *sc, |
Mel Gorman | 6663562 | 2010-08-09 17:19:30 -0700 | [diff] [blame] | 1232 | unsigned long nr_anon, unsigned long nr_file, |
| 1233 | struct list_head *page_list) |
| 1234 | { |
| 1235 | struct page *page; |
| 1236 | struct pagevec pvec; |
Mel Gorman | 1489fa1 | 2010-08-09 17:19:33 -0700 | [diff] [blame] | 1237 | struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc); |
Mel Gorman | 6663562 | 2010-08-09 17:19:30 -0700 | [diff] [blame] | 1238 | |
| 1239 | pagevec_init(&pvec, 1); |
| 1240 | |
| 1241 | /* |
| 1242 | * Put back any unfreeable pages. |
| 1243 | */ |
| 1244 | spin_lock(&zone->lru_lock); |
| 1245 | while (!list_empty(page_list)) { |
| 1246 | int lru; |
| 1247 | page = lru_to_page(page_list); |
| 1248 | VM_BUG_ON(PageLRU(page)); |
| 1249 | list_del(&page->lru); |
| 1250 | if (unlikely(!page_evictable(page, NULL))) { |
| 1251 | spin_unlock_irq(&zone->lru_lock); |
| 1252 | putback_lru_page(page); |
| 1253 | spin_lock_irq(&zone->lru_lock); |
| 1254 | continue; |
| 1255 | } |
| 1256 | SetPageLRU(page); |
| 1257 | lru = page_lru(page); |
| 1258 | add_page_to_lru_list(zone, page, lru); |
| 1259 | if (is_active_lru(lru)) { |
| 1260 | int file = is_file_lru(lru); |
| 1261 | reclaim_stat->recent_rotated[file]++; |
| 1262 | } |
| 1263 | if (!pagevec_add(&pvec, page)) { |
| 1264 | spin_unlock_irq(&zone->lru_lock); |
| 1265 | __pagevec_release(&pvec); |
| 1266 | spin_lock_irq(&zone->lru_lock); |
| 1267 | } |
| 1268 | } |
| 1269 | __mod_zone_page_state(zone, NR_ISOLATED_ANON, -nr_anon); |
| 1270 | __mod_zone_page_state(zone, NR_ISOLATED_FILE, -nr_file); |
| 1271 | |
| 1272 | spin_unlock_irq(&zone->lru_lock); |
| 1273 | pagevec_release(&pvec); |
| 1274 | } |
| 1275 | |
Mel Gorman | 1489fa1 | 2010-08-09 17:19:33 -0700 | [diff] [blame] | 1276 | static noinline_for_stack void update_isolated_counts(struct zone *zone, |
| 1277 | struct scan_control *sc, |
| 1278 | unsigned long *nr_anon, |
| 1279 | unsigned long *nr_file, |
| 1280 | struct list_head *isolated_list) |
| 1281 | { |
| 1282 | unsigned long nr_active; |
| 1283 | unsigned int count[NR_LRU_LISTS] = { 0, }; |
| 1284 | struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc); |
| 1285 | |
| 1286 | nr_active = clear_active_flags(isolated_list, count); |
| 1287 | __count_vm_events(PGDEACTIVATE, nr_active); |
| 1288 | |
| 1289 | __mod_zone_page_state(zone, NR_ACTIVE_FILE, |
| 1290 | -count[LRU_ACTIVE_FILE]); |
| 1291 | __mod_zone_page_state(zone, NR_INACTIVE_FILE, |
| 1292 | -count[LRU_INACTIVE_FILE]); |
| 1293 | __mod_zone_page_state(zone, NR_ACTIVE_ANON, |
| 1294 | -count[LRU_ACTIVE_ANON]); |
| 1295 | __mod_zone_page_state(zone, NR_INACTIVE_ANON, |
| 1296 | -count[LRU_INACTIVE_ANON]); |
| 1297 | |
| 1298 | *nr_anon = count[LRU_ACTIVE_ANON] + count[LRU_INACTIVE_ANON]; |
| 1299 | *nr_file = count[LRU_ACTIVE_FILE] + count[LRU_INACTIVE_FILE]; |
| 1300 | __mod_zone_page_state(zone, NR_ISOLATED_ANON, *nr_anon); |
| 1301 | __mod_zone_page_state(zone, NR_ISOLATED_FILE, *nr_file); |
| 1302 | |
| 1303 | reclaim_stat->recent_scanned[0] += *nr_anon; |
| 1304 | reclaim_stat->recent_scanned[1] += *nr_file; |
| 1305 | } |
| 1306 | |
Mel Gorman | 6663562 | 2010-08-09 17:19:30 -0700 | [diff] [blame] | 1307 | /* |
Wu Fengguang | e31f3698 | 2010-08-09 17:20:01 -0700 | [diff] [blame] | 1308 | * Returns true if the caller should wait to clean dirty/writeback pages. |
| 1309 | * |
| 1310 | * If we are direct reclaiming for contiguous pages and we do not reclaim |
| 1311 | * everything in the list, try again and wait for writeback IO to complete. |
| 1312 | * This will stall high-order allocations noticeably. Only do that when really |
| 1313 | * need to free the pages under high memory pressure. |
| 1314 | */ |
| 1315 | static inline bool should_reclaim_stall(unsigned long nr_taken, |
| 1316 | unsigned long nr_freed, |
| 1317 | int priority, |
| 1318 | struct scan_control *sc) |
| 1319 | { |
| 1320 | int lumpy_stall_priority; |
| 1321 | |
| 1322 | /* kswapd should not stall on sync IO */ |
| 1323 | if (current_is_kswapd()) |
| 1324 | return false; |
| 1325 | |
| 1326 | /* Only stall on lumpy reclaim */ |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 1327 | if (sc->lumpy_reclaim_mode == LUMPY_MODE_NONE) |
Wu Fengguang | e31f3698 | 2010-08-09 17:20:01 -0700 | [diff] [blame] | 1328 | return false; |
| 1329 | |
| 1330 | /* If we have relaimed everything on the isolated list, no stall */ |
| 1331 | if (nr_freed == nr_taken) |
| 1332 | return false; |
| 1333 | |
| 1334 | /* |
| 1335 | * For high-order allocations, there are two stall thresholds. |
| 1336 | * High-cost allocations stall immediately where as lower |
| 1337 | * order allocations such as stacks require the scanning |
| 1338 | * priority to be much higher before stalling. |
| 1339 | */ |
| 1340 | if (sc->order > PAGE_ALLOC_COSTLY_ORDER) |
| 1341 | lumpy_stall_priority = DEF_PRIORITY; |
| 1342 | else |
| 1343 | lumpy_stall_priority = DEF_PRIORITY / 3; |
| 1344 | |
| 1345 | return priority <= lumpy_stall_priority; |
| 1346 | } |
| 1347 | |
| 1348 | /* |
Andrew Morton | 1742f19 | 2006-03-22 00:08:21 -0800 | [diff] [blame] | 1349 | * shrink_inactive_list() is a helper for shrink_zone(). It returns the number |
| 1350 | * of reclaimed pages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | */ |
Mel Gorman | 6663562 | 2010-08-09 17:19:30 -0700 | [diff] [blame] | 1352 | static noinline_for_stack unsigned long |
| 1353 | shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone, |
| 1354 | struct scan_control *sc, int priority, int file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | { |
| 1356 | LIST_HEAD(page_list); |
KOSAKI Motohiro | e247dbc | 2010-08-09 17:19:28 -0700 | [diff] [blame] | 1357 | unsigned long nr_scanned; |
Andrew Morton | 05ff513 | 2006-03-22 00:08:20 -0800 | [diff] [blame] | 1358 | unsigned long nr_reclaimed = 0; |
KOSAKI Motohiro | e247dbc | 2010-08-09 17:19:28 -0700 | [diff] [blame] | 1359 | unsigned long nr_taken; |
KOSAKI Motohiro | e247dbc | 2010-08-09 17:19:28 -0700 | [diff] [blame] | 1360 | unsigned long nr_anon; |
| 1361 | unsigned long nr_file; |
KOSAKI Motohiro | 78dc583 | 2009-06-16 15:31:40 -0700 | [diff] [blame] | 1362 | |
Rik van Riel | 35cd781 | 2009-09-21 17:01:38 -0700 | [diff] [blame] | 1363 | while (unlikely(too_many_isolated(zone, file, sc))) { |
KOSAKI Motohiro | 58355c7 | 2009-10-26 16:49:35 -0700 | [diff] [blame] | 1364 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
Rik van Riel | 35cd781 | 2009-09-21 17:01:38 -0700 | [diff] [blame] | 1365 | |
| 1366 | /* We are about to die and free our memory. Return now. */ |
| 1367 | if (fatal_signal_pending(current)) |
| 1368 | return SWAP_CLUSTER_MAX; |
| 1369 | } |
| 1370 | |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 1371 | set_lumpy_reclaim_mode(priority, sc, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | lru_add_drain(); |
| 1373 | spin_lock_irq(&zone->lru_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | |
KOSAKI Motohiro | e247dbc | 2010-08-09 17:19:28 -0700 | [diff] [blame] | 1375 | if (scanning_global_lru(sc)) { |
| 1376 | nr_taken = isolate_pages_global(nr_to_scan, |
| 1377 | &page_list, &nr_scanned, sc->order, |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 1378 | sc->lumpy_reclaim_mode == LUMPY_MODE_NONE ? |
| 1379 | ISOLATE_INACTIVE : ISOLATE_BOTH, |
KOSAKI Motohiro | e247dbc | 2010-08-09 17:19:28 -0700 | [diff] [blame] | 1380 | zone, 0, file); |
| 1381 | zone->pages_scanned += nr_scanned; |
KOSAKI Motohiro | b35ea17 | 2009-09-21 17:01:36 -0700 | [diff] [blame] | 1382 | if (current_is_kswapd()) |
KOSAKI Motohiro | e247dbc | 2010-08-09 17:19:28 -0700 | [diff] [blame] | 1383 | __count_zone_vm_events(PGSCAN_KSWAPD, zone, |
| 1384 | nr_scanned); |
| 1385 | else |
| 1386 | __count_zone_vm_events(PGSCAN_DIRECT, zone, |
| 1387 | nr_scanned); |
| 1388 | } else { |
| 1389 | nr_taken = mem_cgroup_isolate_pages(nr_to_scan, |
| 1390 | &page_list, &nr_scanned, sc->order, |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 1391 | sc->lumpy_reclaim_mode == LUMPY_MODE_NONE ? |
| 1392 | ISOLATE_INACTIVE : ISOLATE_BOTH, |
KOSAKI Motohiro | e247dbc | 2010-08-09 17:19:28 -0700 | [diff] [blame] | 1393 | zone, sc->mem_cgroup, |
| 1394 | 0, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | /* |
KOSAKI Motohiro | e247dbc | 2010-08-09 17:19:28 -0700 | [diff] [blame] | 1396 | * mem_cgroup_isolate_pages() keeps track of |
| 1397 | * scanned pages on its own. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | */ |
KOSAKI Motohiro | e247dbc | 2010-08-09 17:19:28 -0700 | [diff] [blame] | 1399 | } |
KOSAKI Motohiro | a731286 | 2009-09-21 17:01:37 -0700 | [diff] [blame] | 1400 | |
Mel Gorman | 6663562 | 2010-08-09 17:19:30 -0700 | [diff] [blame] | 1401 | if (nr_taken == 0) { |
| 1402 | spin_unlock_irq(&zone->lru_lock); |
| 1403 | return 0; |
| 1404 | } |
KOSAKI Motohiro | e247dbc | 2010-08-09 17:19:28 -0700 | [diff] [blame] | 1405 | |
Mel Gorman | 1489fa1 | 2010-08-09 17:19:33 -0700 | [diff] [blame] | 1406 | update_isolated_counts(zone, sc, &nr_anon, &nr_file, &page_list); |
KOSAKI Motohiro | e247dbc | 2010-08-09 17:19:28 -0700 | [diff] [blame] | 1407 | |
| 1408 | spin_unlock_irq(&zone->lru_lock); |
| 1409 | |
Mel Gorman | 0e093d99 | 2010-10-26 14:21:45 -0700 | [diff] [blame] | 1410 | nr_reclaimed = shrink_page_list(&page_list, zone, sc); |
KOSAKI Motohiro | e247dbc | 2010-08-09 17:19:28 -0700 | [diff] [blame] | 1411 | |
Wu Fengguang | e31f3698 | 2010-08-09 17:20:01 -0700 | [diff] [blame] | 1412 | /* Check if we should syncronously wait for writeback */ |
| 1413 | if (should_reclaim_stall(nr_taken, nr_reclaimed, priority, sc)) { |
KOSAKI Motohiro | 7d3579e | 2010-10-26 14:21:42 -0700 | [diff] [blame] | 1414 | set_lumpy_reclaim_mode(priority, sc, true); |
Mel Gorman | 0e093d99 | 2010-10-26 14:21:45 -0700 | [diff] [blame] | 1415 | nr_reclaimed += shrink_page_list(&page_list, zone, sc); |
KOSAKI Motohiro | e247dbc | 2010-08-09 17:19:28 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | local_irq_disable(); |
| 1419 | if (current_is_kswapd()) |
| 1420 | __count_vm_events(KSWAPD_STEAL, nr_reclaimed); |
| 1421 | __count_zone_vm_events(PGSTEAL, zone, nr_reclaimed); |
| 1422 | |
Mel Gorman | 1489fa1 | 2010-08-09 17:19:33 -0700 | [diff] [blame] | 1423 | putback_lru_pages(zone, sc, nr_anon, nr_file, &page_list); |
Mel Gorman | e11da5b | 2010-10-26 14:21:40 -0700 | [diff] [blame] | 1424 | |
| 1425 | trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id, |
| 1426 | zone_idx(zone), |
| 1427 | nr_scanned, nr_reclaimed, |
| 1428 | priority, |
| 1429 | trace_shrink_flags(file, sc->lumpy_reclaim_mode)); |
Andrew Morton | 05ff513 | 2006-03-22 00:08:20 -0800 | [diff] [blame] | 1430 | return nr_reclaimed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Martin Bligh | 3bb1a852 | 2006-10-28 10:38:24 -0700 | [diff] [blame] | 1433 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | * This moves pages from the active list to the inactive list. |
| 1435 | * |
| 1436 | * We move them the other way if the page is referenced by one or more |
| 1437 | * processes, from rmap. |
| 1438 | * |
| 1439 | * If the pages are mostly unmapped, the processing is fast and it is |
| 1440 | * appropriate to hold zone->lru_lock across the whole operation. But if |
| 1441 | * the pages are mapped, the processing is slow (page_referenced()) so we |
| 1442 | * should drop zone->lru_lock around each page. It's impossible to balance |
| 1443 | * this, so instead we remove the pages from the LRU while processing them. |
| 1444 | * It is safe to rely on PG_active against the non-LRU pages in here because |
| 1445 | * nobody will play with that bit on a non-LRU page. |
| 1446 | * |
| 1447 | * The downside is that we have to touch page->_count against each page. |
| 1448 | * But we had to alter page->flags anyway. |
| 1449 | */ |
KAMEZAWA Hiroyuki | 1cfb419 | 2008-02-07 00:14:37 -0800 | [diff] [blame] | 1450 | |
Wu Fengguang | 3eb4140 | 2009-06-16 15:33:13 -0700 | [diff] [blame] | 1451 | static void move_active_pages_to_lru(struct zone *zone, |
| 1452 | struct list_head *list, |
| 1453 | enum lru_list lru) |
| 1454 | { |
| 1455 | unsigned long pgmoved = 0; |
| 1456 | struct pagevec pvec; |
| 1457 | struct page *page; |
| 1458 | |
| 1459 | pagevec_init(&pvec, 1); |
| 1460 | |
| 1461 | while (!list_empty(list)) { |
| 1462 | page = lru_to_page(list); |
Wu Fengguang | 3eb4140 | 2009-06-16 15:33:13 -0700 | [diff] [blame] | 1463 | |
| 1464 | VM_BUG_ON(PageLRU(page)); |
| 1465 | SetPageLRU(page); |
| 1466 | |
Wu Fengguang | 3eb4140 | 2009-06-16 15:33:13 -0700 | [diff] [blame] | 1467 | list_move(&page->lru, &zone->lru[lru].list); |
| 1468 | mem_cgroup_add_lru_list(page, lru); |
| 1469 | pgmoved++; |
| 1470 | |
| 1471 | if (!pagevec_add(&pvec, page) || list_empty(list)) { |
| 1472 | spin_unlock_irq(&zone->lru_lock); |
| 1473 | if (buffer_heads_over_limit) |
| 1474 | pagevec_strip(&pvec); |
| 1475 | __pagevec_release(&pvec); |
| 1476 | spin_lock_irq(&zone->lru_lock); |
| 1477 | } |
| 1478 | } |
| 1479 | __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved); |
| 1480 | if (!is_active_lru(lru)) |
| 1481 | __count_vm_events(PGDEACTIVATE, pgmoved); |
| 1482 | } |
KAMEZAWA Hiroyuki | 1cfb419 | 2008-02-07 00:14:37 -0800 | [diff] [blame] | 1483 | |
Andrew Morton | 1742f19 | 2006-03-22 00:08:21 -0800 | [diff] [blame] | 1484 | static void shrink_active_list(unsigned long nr_pages, struct zone *zone, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1485 | struct scan_control *sc, int priority, int file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | { |
KOSAKI Motohiro | 44c241f | 2009-09-21 17:01:35 -0700 | [diff] [blame] | 1487 | unsigned long nr_taken; |
Andrew Morton | 69e0594 | 2006-03-22 00:08:19 -0800 | [diff] [blame] | 1488 | unsigned long pgscanned; |
Wu Fengguang | 6fe6b7e | 2009-06-16 15:33:05 -0700 | [diff] [blame] | 1489 | unsigned long vm_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | LIST_HEAD(l_hold); /* The pages which were snipped off */ |
Wu Fengguang | 8cab475 | 2009-06-16 15:33:12 -0700 | [diff] [blame] | 1491 | LIST_HEAD(l_active); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1492 | LIST_HEAD(l_inactive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | struct page *page; |
KOSAKI Motohiro | 6e90157 | 2009-01-07 18:08:15 -0800 | [diff] [blame] | 1494 | struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc); |
KOSAKI Motohiro | 44c241f | 2009-09-21 17:01:35 -0700 | [diff] [blame] | 1495 | unsigned long nr_rotated = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | |
| 1497 | lru_add_drain(); |
| 1498 | spin_lock_irq(&zone->lru_lock); |
KAMEZAWA Hiroyuki | e72e2bd | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 1499 | if (scanning_global_lru(sc)) { |
Johannes Weiner | 8b25c6d | 2010-05-24 14:32:40 -0700 | [diff] [blame] | 1500 | nr_taken = isolate_pages_global(nr_pages, &l_hold, |
| 1501 | &pgscanned, sc->order, |
| 1502 | ISOLATE_ACTIVE, zone, |
| 1503 | 1, file); |
KAMEZAWA Hiroyuki | 1cfb419 | 2008-02-07 00:14:37 -0800 | [diff] [blame] | 1504 | zone->pages_scanned += pgscanned; |
Johannes Weiner | 8b25c6d | 2010-05-24 14:32:40 -0700 | [diff] [blame] | 1505 | } else { |
| 1506 | nr_taken = mem_cgroup_isolate_pages(nr_pages, &l_hold, |
| 1507 | &pgscanned, sc->order, |
| 1508 | ISOLATE_ACTIVE, zone, |
| 1509 | sc->mem_cgroup, 1, file); |
| 1510 | /* |
| 1511 | * mem_cgroup_isolate_pages() keeps track of |
| 1512 | * scanned pages on its own. |
| 1513 | */ |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1514 | } |
Johannes Weiner | 8b25c6d | 2010-05-24 14:32:40 -0700 | [diff] [blame] | 1515 | |
Johannes Weiner | b7c46d1 | 2009-09-21 17:02:56 -0700 | [diff] [blame] | 1516 | reclaim_stat->recent_scanned[file] += nr_taken; |
KAMEZAWA Hiroyuki | 1cfb419 | 2008-02-07 00:14:37 -0800 | [diff] [blame] | 1517 | |
Wu Fengguang | 3eb4140 | 2009-06-16 15:33:13 -0700 | [diff] [blame] | 1518 | __count_zone_vm_events(PGREFILL, zone, pgscanned); |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1519 | if (file) |
KOSAKI Motohiro | 44c241f | 2009-09-21 17:01:35 -0700 | [diff] [blame] | 1520 | __mod_zone_page_state(zone, NR_ACTIVE_FILE, -nr_taken); |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1521 | else |
KOSAKI Motohiro | 44c241f | 2009-09-21 17:01:35 -0700 | [diff] [blame] | 1522 | __mod_zone_page_state(zone, NR_ACTIVE_ANON, -nr_taken); |
KOSAKI Motohiro | a731286 | 2009-09-21 17:01:37 -0700 | [diff] [blame] | 1523 | __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | spin_unlock_irq(&zone->lru_lock); |
| 1525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | while (!list_empty(&l_hold)) { |
| 1527 | cond_resched(); |
| 1528 | page = lru_to_page(&l_hold); |
| 1529 | list_del(&page->lru); |
Rik van Riel | 7e9cd48 | 2008-10-18 20:26:35 -0700 | [diff] [blame] | 1530 | |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 1531 | if (unlikely(!page_evictable(page, NULL))) { |
| 1532 | putback_lru_page(page); |
| 1533 | continue; |
| 1534 | } |
| 1535 | |
Johannes Weiner | 64574746 | 2010-03-05 13:42:22 -0800 | [diff] [blame] | 1536 | if (page_referenced(page, 0, sc->mem_cgroup, &vm_flags)) { |
KOSAKI Motohiro | 44c241f | 2009-09-21 17:01:35 -0700 | [diff] [blame] | 1537 | nr_rotated++; |
Wu Fengguang | 8cab475 | 2009-06-16 15:33:12 -0700 | [diff] [blame] | 1538 | /* |
| 1539 | * Identify referenced, file-backed active pages and |
| 1540 | * give them one more trip around the active list. So |
| 1541 | * that executable code get better chances to stay in |
| 1542 | * memory under moderate memory pressure. Anon pages |
| 1543 | * are not likely to be evicted by use-once streaming |
| 1544 | * IO, plus JVM can create lots of anon VM_EXEC pages, |
| 1545 | * so we ignore them here. |
| 1546 | */ |
Wu Fengguang | 41e2098 | 2009-10-26 16:49:53 -0700 | [diff] [blame] | 1547 | if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { |
Wu Fengguang | 8cab475 | 2009-06-16 15:33:12 -0700 | [diff] [blame] | 1548 | list_add(&page->lru, &l_active); |
| 1549 | continue; |
| 1550 | } |
| 1551 | } |
Rik van Riel | 7e9cd48 | 2008-10-18 20:26:35 -0700 | [diff] [blame] | 1552 | |
KOSAKI Motohiro | 5205e56 | 2009-09-21 17:01:44 -0700 | [diff] [blame] | 1553 | ClearPageActive(page); /* we are de-activating */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | list_add(&page->lru, &l_inactive); |
| 1555 | } |
| 1556 | |
Andrew Morton | b555749 | 2009-01-06 14:40:13 -0800 | [diff] [blame] | 1557 | /* |
Wu Fengguang | 8cab475 | 2009-06-16 15:33:12 -0700 | [diff] [blame] | 1558 | * Move pages back to the lru list. |
Andrew Morton | b555749 | 2009-01-06 14:40:13 -0800 | [diff] [blame] | 1559 | */ |
Johannes Weiner | 2a1dc50 | 2008-12-01 03:00:35 +0100 | [diff] [blame] | 1560 | spin_lock_irq(&zone->lru_lock); |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1561 | /* |
Wu Fengguang | 8cab475 | 2009-06-16 15:33:12 -0700 | [diff] [blame] | 1562 | * Count referenced pages from currently used mappings as rotated, |
| 1563 | * even though only some of them are actually re-activated. This |
| 1564 | * helps balance scan pressure between file and anonymous pages in |
| 1565 | * get_scan_ratio. |
Rik van Riel | 7e9cd48 | 2008-10-18 20:26:35 -0700 | [diff] [blame] | 1566 | */ |
Johannes Weiner | b7c46d1 | 2009-09-21 17:02:56 -0700 | [diff] [blame] | 1567 | reclaim_stat->recent_rotated[file] += nr_rotated; |
Rik van Riel | 556adec | 2008-10-18 20:26:34 -0700 | [diff] [blame] | 1568 | |
Wu Fengguang | 3eb4140 | 2009-06-16 15:33:13 -0700 | [diff] [blame] | 1569 | move_active_pages_to_lru(zone, &l_active, |
| 1570 | LRU_ACTIVE + file * LRU_FILE); |
| 1571 | move_active_pages_to_lru(zone, &l_inactive, |
| 1572 | LRU_BASE + file * LRU_FILE); |
KOSAKI Motohiro | a731286 | 2009-09-21 17:01:37 -0700 | [diff] [blame] | 1573 | __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 1574 | spin_unlock_irq(&zone->lru_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | } |
| 1576 | |
Minchan Kim | 74e3f3c | 2010-10-26 14:21:31 -0700 | [diff] [blame] | 1577 | #ifdef CONFIG_SWAP |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1578 | static int inactive_anon_is_low_global(struct zone *zone) |
KOSAKI Motohiro | f89eb90 | 2009-01-07 18:08:14 -0800 | [diff] [blame] | 1579 | { |
| 1580 | unsigned long active, inactive; |
| 1581 | |
| 1582 | active = zone_page_state(zone, NR_ACTIVE_ANON); |
| 1583 | inactive = zone_page_state(zone, NR_INACTIVE_ANON); |
| 1584 | |
| 1585 | if (inactive * zone->inactive_ratio < active) |
| 1586 | return 1; |
| 1587 | |
| 1588 | return 0; |
| 1589 | } |
| 1590 | |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1591 | /** |
| 1592 | * inactive_anon_is_low - check if anonymous pages need to be deactivated |
| 1593 | * @zone: zone to check |
| 1594 | * @sc: scan control of this context |
| 1595 | * |
| 1596 | * Returns true if the zone does not have enough inactive anon pages, |
| 1597 | * meaning some active anon pages need to be deactivated. |
| 1598 | */ |
| 1599 | static int inactive_anon_is_low(struct zone *zone, struct scan_control *sc) |
| 1600 | { |
| 1601 | int low; |
| 1602 | |
Minchan Kim | 74e3f3c | 2010-10-26 14:21:31 -0700 | [diff] [blame] | 1603 | /* |
| 1604 | * If we don't have swap space, anonymous page deactivation |
| 1605 | * is pointless. |
| 1606 | */ |
| 1607 | if (!total_swap_pages) |
| 1608 | return 0; |
| 1609 | |
KAMEZAWA Hiroyuki | e72e2bd | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 1610 | if (scanning_global_lru(sc)) |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1611 | low = inactive_anon_is_low_global(zone); |
| 1612 | else |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 1613 | low = mem_cgroup_inactive_anon_is_low(sc->mem_cgroup); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1614 | return low; |
| 1615 | } |
Minchan Kim | 74e3f3c | 2010-10-26 14:21:31 -0700 | [diff] [blame] | 1616 | #else |
| 1617 | static inline int inactive_anon_is_low(struct zone *zone, |
| 1618 | struct scan_control *sc) |
| 1619 | { |
| 1620 | return 0; |
| 1621 | } |
| 1622 | #endif |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1623 | |
Rik van Riel | 56e49d2 | 2009-06-16 15:32:28 -0700 | [diff] [blame] | 1624 | static int inactive_file_is_low_global(struct zone *zone) |
| 1625 | { |
| 1626 | unsigned long active, inactive; |
| 1627 | |
| 1628 | active = zone_page_state(zone, NR_ACTIVE_FILE); |
| 1629 | inactive = zone_page_state(zone, NR_INACTIVE_FILE); |
| 1630 | |
| 1631 | return (active > inactive); |
| 1632 | } |
| 1633 | |
| 1634 | /** |
| 1635 | * inactive_file_is_low - check if file pages need to be deactivated |
| 1636 | * @zone: zone to check |
| 1637 | * @sc: scan control of this context |
| 1638 | * |
| 1639 | * When the system is doing streaming IO, memory pressure here |
| 1640 | * ensures that active file pages get deactivated, until more |
| 1641 | * than half of the file pages are on the inactive list. |
| 1642 | * |
| 1643 | * Once we get to that situation, protect the system's working |
| 1644 | * set from being evicted by disabling active file page aging. |
| 1645 | * |
| 1646 | * This uses a different ratio than the anonymous pages, because |
| 1647 | * the page cache uses a use-once replacement algorithm. |
| 1648 | */ |
| 1649 | static int inactive_file_is_low(struct zone *zone, struct scan_control *sc) |
| 1650 | { |
| 1651 | int low; |
| 1652 | |
| 1653 | if (scanning_global_lru(sc)) |
| 1654 | low = inactive_file_is_low_global(zone); |
| 1655 | else |
| 1656 | low = mem_cgroup_inactive_file_is_low(sc->mem_cgroup); |
| 1657 | return low; |
| 1658 | } |
| 1659 | |
Rik van Riel | b39415b | 2009-12-14 17:59:48 -0800 | [diff] [blame] | 1660 | static int inactive_list_is_low(struct zone *zone, struct scan_control *sc, |
| 1661 | int file) |
| 1662 | { |
| 1663 | if (file) |
| 1664 | return inactive_file_is_low(zone, sc); |
| 1665 | else |
| 1666 | return inactive_anon_is_low(zone, sc); |
| 1667 | } |
| 1668 | |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1669 | static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1670 | struct zone *zone, struct scan_control *sc, int priority) |
| 1671 | { |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1672 | int file = is_file_lru(lru); |
| 1673 | |
Rik van Riel | b39415b | 2009-12-14 17:59:48 -0800 | [diff] [blame] | 1674 | if (is_active_lru(lru)) { |
| 1675 | if (inactive_list_is_low(zone, sc, file)) |
| 1676 | shrink_active_list(nr_to_scan, zone, sc, priority, file); |
Rik van Riel | 556adec | 2008-10-18 20:26:34 -0700 | [diff] [blame] | 1677 | return 0; |
| 1678 | } |
| 1679 | |
Rik van Riel | 33c120e | 2008-10-18 20:26:36 -0700 | [diff] [blame] | 1680 | return shrink_inactive_list(nr_to_scan, zone, sc, priority, file); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | /* |
Shaohua Li | 76a33fc | 2010-05-24 14:32:36 -0700 | [diff] [blame] | 1684 | * Smallish @nr_to_scan's are deposited in @nr_saved_scan, |
| 1685 | * until we collected @swap_cluster_max pages to scan. |
| 1686 | */ |
| 1687 | static unsigned long nr_scan_try_batch(unsigned long nr_to_scan, |
| 1688 | unsigned long *nr_saved_scan) |
| 1689 | { |
| 1690 | unsigned long nr; |
| 1691 | |
| 1692 | *nr_saved_scan += nr_to_scan; |
| 1693 | nr = *nr_saved_scan; |
| 1694 | |
| 1695 | if (nr >= SWAP_CLUSTER_MAX) |
| 1696 | *nr_saved_scan = 0; |
| 1697 | else |
| 1698 | nr = 0; |
| 1699 | |
| 1700 | return nr; |
| 1701 | } |
| 1702 | |
| 1703 | /* |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1704 | * Determine how aggressively the anon and file LRU lists should be |
| 1705 | * scanned. The relative value of each set of LRU lists is determined |
| 1706 | * by looking at the fraction of the pages scanned we did rotate back |
| 1707 | * onto the active list instead of evict. |
| 1708 | * |
Shaohua Li | 76a33fc | 2010-05-24 14:32:36 -0700 | [diff] [blame] | 1709 | * nr[0] = anon pages to scan; nr[1] = file pages to scan |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1710 | */ |
Shaohua Li | 76a33fc | 2010-05-24 14:32:36 -0700 | [diff] [blame] | 1711 | static void get_scan_count(struct zone *zone, struct scan_control *sc, |
| 1712 | unsigned long *nr, int priority) |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1713 | { |
| 1714 | unsigned long anon, file, free; |
| 1715 | unsigned long anon_prio, file_prio; |
| 1716 | unsigned long ap, fp; |
KOSAKI Motohiro | 6e90157 | 2009-01-07 18:08:15 -0800 | [diff] [blame] | 1717 | struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc); |
Shaohua Li | 76a33fc | 2010-05-24 14:32:36 -0700 | [diff] [blame] | 1718 | u64 fraction[2], denominator; |
| 1719 | enum lru_list l; |
| 1720 | int noswap = 0; |
| 1721 | |
| 1722 | /* If we have no swap space, do not bother scanning anon pages. */ |
| 1723 | if (!sc->may_swap || (nr_swap_pages <= 0)) { |
| 1724 | noswap = 1; |
| 1725 | fraction[0] = 0; |
| 1726 | fraction[1] = 1; |
| 1727 | denominator = 1; |
| 1728 | goto out; |
| 1729 | } |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1730 | |
Vincent Li | 0b21767 | 2009-09-21 17:03:09 -0700 | [diff] [blame] | 1731 | anon = zone_nr_lru_pages(zone, sc, LRU_ACTIVE_ANON) + |
| 1732 | zone_nr_lru_pages(zone, sc, LRU_INACTIVE_ANON); |
| 1733 | file = zone_nr_lru_pages(zone, sc, LRU_ACTIVE_FILE) + |
| 1734 | zone_nr_lru_pages(zone, sc, LRU_INACTIVE_FILE); |
Hugh Dickins | b962716 | 2009-01-06 14:39:41 -0800 | [diff] [blame] | 1735 | |
KAMEZAWA Hiroyuki | e72e2bd | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 1736 | if (scanning_global_lru(sc)) { |
KOSAKI Motohiro | eeee9a8 | 2009-01-07 18:08:17 -0800 | [diff] [blame] | 1737 | free = zone_page_state(zone, NR_FREE_PAGES); |
| 1738 | /* If we have very few page cache pages, |
| 1739 | force-scan anon pages. */ |
Mel Gorman | 4185896 | 2009-06-16 15:32:12 -0700 | [diff] [blame] | 1740 | if (unlikely(file + free <= high_wmark_pages(zone))) { |
Shaohua Li | 76a33fc | 2010-05-24 14:32:36 -0700 | [diff] [blame] | 1741 | fraction[0] = 1; |
| 1742 | fraction[1] = 0; |
| 1743 | denominator = 1; |
| 1744 | goto out; |
KOSAKI Motohiro | eeee9a8 | 2009-01-07 18:08:17 -0800 | [diff] [blame] | 1745 | } |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | /* |
KOSAKI Motohiro | 58c37f6 | 2010-08-09 17:19:51 -0700 | [diff] [blame] | 1749 | * With swappiness at 100, anonymous and file have the same priority. |
| 1750 | * This scanning priority is essentially the inverse of IO cost. |
| 1751 | */ |
| 1752 | anon_prio = sc->swappiness; |
| 1753 | file_prio = 200 - sc->swappiness; |
| 1754 | |
| 1755 | /* |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1756 | * OK, so we have swap space and a fair amount of page cache |
| 1757 | * pages. We use the recently rotated / recently scanned |
| 1758 | * ratios to determine how valuable each cache is. |
| 1759 | * |
| 1760 | * Because workloads change over time (and to avoid overflow) |
| 1761 | * we keep these statistics as a floating average, which ends |
| 1762 | * up weighing recent references more than old ones. |
| 1763 | * |
| 1764 | * anon in [0], file in [1] |
| 1765 | */ |
KOSAKI Motohiro | 58c37f6 | 2010-08-09 17:19:51 -0700 | [diff] [blame] | 1766 | spin_lock_irq(&zone->lru_lock); |
KOSAKI Motohiro | 6e90157 | 2009-01-07 18:08:15 -0800 | [diff] [blame] | 1767 | if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { |
KOSAKI Motohiro | 6e90157 | 2009-01-07 18:08:15 -0800 | [diff] [blame] | 1768 | reclaim_stat->recent_scanned[0] /= 2; |
| 1769 | reclaim_stat->recent_rotated[0] /= 2; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
KOSAKI Motohiro | 6e90157 | 2009-01-07 18:08:15 -0800 | [diff] [blame] | 1772 | if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { |
KOSAKI Motohiro | 6e90157 | 2009-01-07 18:08:15 -0800 | [diff] [blame] | 1773 | reclaim_stat->recent_scanned[1] /= 2; |
| 1774 | reclaim_stat->recent_rotated[1] /= 2; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | /* |
Rik van Riel | 00d8089 | 2008-11-19 15:36:44 -0800 | [diff] [blame] | 1778 | * The amount of pressure on anon vs file pages is inversely |
| 1779 | * proportional to the fraction of recently scanned pages on |
| 1780 | * each list that were recently referenced and in active use. |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1781 | */ |
KOSAKI Motohiro | 6e90157 | 2009-01-07 18:08:15 -0800 | [diff] [blame] | 1782 | ap = (anon_prio + 1) * (reclaim_stat->recent_scanned[0] + 1); |
| 1783 | ap /= reclaim_stat->recent_rotated[0] + 1; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1784 | |
KOSAKI Motohiro | 6e90157 | 2009-01-07 18:08:15 -0800 | [diff] [blame] | 1785 | fp = (file_prio + 1) * (reclaim_stat->recent_scanned[1] + 1); |
| 1786 | fp /= reclaim_stat->recent_rotated[1] + 1; |
KOSAKI Motohiro | 58c37f6 | 2010-08-09 17:19:51 -0700 | [diff] [blame] | 1787 | spin_unlock_irq(&zone->lru_lock); |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1788 | |
Shaohua Li | 76a33fc | 2010-05-24 14:32:36 -0700 | [diff] [blame] | 1789 | fraction[0] = ap; |
| 1790 | fraction[1] = fp; |
| 1791 | denominator = ap + fp + 1; |
| 1792 | out: |
| 1793 | for_each_evictable_lru(l) { |
| 1794 | int file = is_file_lru(l); |
| 1795 | unsigned long scan; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1796 | |
Shaohua Li | 76a33fc | 2010-05-24 14:32:36 -0700 | [diff] [blame] | 1797 | scan = zone_nr_lru_pages(zone, sc, l); |
| 1798 | if (priority || noswap) { |
| 1799 | scan >>= priority; |
| 1800 | scan = div64_u64(scan * fraction[file], denominator); |
| 1801 | } |
| 1802 | nr[l] = nr_scan_try_batch(scan, |
| 1803 | &reclaim_stat->nr_saved_scan[l]); |
| 1804 | } |
Wu Fengguang | 6e08a36 | 2009-06-16 15:32:29 -0700 | [diff] [blame] | 1805 | } |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1806 | |
| 1807 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | * This is a basic per-zone page freer. Used by both kswapd and direct reclaim. |
| 1809 | */ |
Rik van Riel | a79311c | 2009-01-06 14:40:01 -0800 | [diff] [blame] | 1810 | static void shrink_zone(int priority, struct zone *zone, |
Andrew Morton | 05ff513 | 2006-03-22 00:08:20 -0800 | [diff] [blame] | 1811 | struct scan_control *sc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | { |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1813 | unsigned long nr[NR_LRU_LISTS]; |
Christoph Lameter | 8695949 | 2006-03-22 00:08:18 -0800 | [diff] [blame] | 1814 | unsigned long nr_to_scan; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1815 | enum lru_list l; |
KOSAKI Motohiro | 01dbe5c | 2009-01-06 14:40:02 -0800 | [diff] [blame] | 1816 | unsigned long nr_reclaimed = sc->nr_reclaimed; |
KOSAKI Motohiro | 22fba33 | 2009-12-14 17:59:10 -0800 | [diff] [blame] | 1817 | unsigned long nr_to_reclaim = sc->nr_to_reclaim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | |
Shaohua Li | 76a33fc | 2010-05-24 14:32:36 -0700 | [diff] [blame] | 1819 | get_scan_count(zone, sc, nr, priority); |
KAMEZAWA Hiroyuki | 1cfb419 | 2008-02-07 00:14:37 -0800 | [diff] [blame] | 1820 | |
Rik van Riel | 556adec | 2008-10-18 20:26:34 -0700 | [diff] [blame] | 1821 | while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || |
| 1822 | nr[LRU_INACTIVE_FILE]) { |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 1823 | for_each_evictable_lru(l) { |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1824 | if (nr[l]) { |
KOSAKI Motohiro | ece74b2 | 2009-12-14 17:59:14 -0800 | [diff] [blame] | 1825 | nr_to_scan = min_t(unsigned long, |
| 1826 | nr[l], SWAP_CLUSTER_MAX); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1827 | nr[l] -= nr_to_scan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | |
KOSAKI Motohiro | 01dbe5c | 2009-01-06 14:40:02 -0800 | [diff] [blame] | 1829 | nr_reclaimed += shrink_list(l, nr_to_scan, |
| 1830 | zone, sc, priority); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1831 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | } |
Rik van Riel | a79311c | 2009-01-06 14:40:01 -0800 | [diff] [blame] | 1833 | /* |
| 1834 | * On large memory systems, scan >> priority can become |
| 1835 | * really large. This is fine for the starting priority; |
| 1836 | * we want to put equal scanning pressure on each zone. |
| 1837 | * However, if the VM has a harder time of freeing pages, |
| 1838 | * with multiple processes reclaiming pages, the total |
| 1839 | * freeing target can get unreasonably large. |
| 1840 | */ |
KOSAKI Motohiro | 338fde9 | 2009-12-14 17:59:15 -0800 | [diff] [blame] | 1841 | if (nr_reclaimed >= nr_to_reclaim && priority < DEF_PRIORITY) |
Rik van Riel | a79311c | 2009-01-06 14:40:01 -0800 | [diff] [blame] | 1842 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | } |
| 1844 | |
KOSAKI Motohiro | 01dbe5c | 2009-01-06 14:40:02 -0800 | [diff] [blame] | 1845 | sc->nr_reclaimed = nr_reclaimed; |
| 1846 | |
Rik van Riel | 556adec | 2008-10-18 20:26:34 -0700 | [diff] [blame] | 1847 | /* |
| 1848 | * Even if we did not try to evict anon pages at all, we want to |
| 1849 | * rebalance the anon lru active/inactive ratio. |
| 1850 | */ |
Minchan Kim | 74e3f3c | 2010-10-26 14:21:31 -0700 | [diff] [blame] | 1851 | if (inactive_anon_is_low(zone, sc)) |
Rik van Riel | 556adec | 2008-10-18 20:26:34 -0700 | [diff] [blame] | 1852 | shrink_active_list(SWAP_CLUSTER_MAX, zone, sc, priority, 0); |
| 1853 | |
Andrew Morton | 232ea4d | 2007-02-28 20:13:21 -0800 | [diff] [blame] | 1854 | throttle_vm_writeout(sc->gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | /* |
| 1858 | * This is the direct reclaim path, for page-allocating processes. We only |
| 1859 | * try to reclaim pages from zones which will satisfy the caller's allocation |
| 1860 | * request. |
| 1861 | * |
Mel Gorman | 4185896 | 2009-06-16 15:32:12 -0700 | [diff] [blame] | 1862 | * We reclaim from a zone even if that zone is over high_wmark_pages(zone). |
| 1863 | * Because: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | * a) The caller may be trying to free *extra* pages to satisfy a higher-order |
| 1865 | * allocation or |
Mel Gorman | 4185896 | 2009-06-16 15:32:12 -0700 | [diff] [blame] | 1866 | * b) The target zone may be at high_wmark_pages(zone) but the lower zones |
| 1867 | * must go *over* high_wmark_pages(zone) to satisfy the `incremental min' |
| 1868 | * zone defense algorithm. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | * If a zone is deemed to be full of pinned pages then just give it a light |
| 1871 | * scan then give up on it. |
| 1872 | */ |
Minchan Kim | d190836 | 2010-09-22 13:05:01 -0700 | [diff] [blame] | 1873 | static void shrink_zones(int priority, struct zonelist *zonelist, |
Andrew Morton | 05ff513 | 2006-03-22 00:08:20 -0800 | [diff] [blame] | 1874 | struct scan_control *sc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | { |
Mel Gorman | dd1a239 | 2008-04-28 02:12:17 -0700 | [diff] [blame] | 1876 | struct zoneref *z; |
Mel Gorman | 54a6eb5 | 2008-04-28 02:12:16 -0700 | [diff] [blame] | 1877 | struct zone *zone; |
KAMEZAWA Hiroyuki | 1cfb419 | 2008-02-07 00:14:37 -0800 | [diff] [blame] | 1878 | |
Mel Gorman | d4debc6 | 2010-08-09 17:19:29 -0700 | [diff] [blame] | 1879 | for_each_zone_zonelist_nodemask(zone, z, zonelist, |
| 1880 | gfp_zone(sc->gfp_mask), sc->nodemask) { |
Con Kolivas | f3fe651 | 2006-01-06 00:11:15 -0800 | [diff] [blame] | 1881 | if (!populated_zone(zone)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | continue; |
KAMEZAWA Hiroyuki | 1cfb419 | 2008-02-07 00:14:37 -0800 | [diff] [blame] | 1883 | /* |
| 1884 | * Take care memory controller reclaiming has small influence |
| 1885 | * to global LRU. |
| 1886 | */ |
KAMEZAWA Hiroyuki | e72e2bd | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 1887 | if (scanning_global_lru(sc)) { |
KAMEZAWA Hiroyuki | 1cfb419 | 2008-02-07 00:14:37 -0800 | [diff] [blame] | 1888 | if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) |
| 1889 | continue; |
KOSAKI Motohiro | 93e4a89 | 2010-03-05 13:41:55 -0800 | [diff] [blame] | 1890 | if (zone->all_unreclaimable && priority != DEF_PRIORITY) |
KAMEZAWA Hiroyuki | 1cfb419 | 2008-02-07 00:14:37 -0800 | [diff] [blame] | 1891 | continue; /* Let kswapd poll it */ |
KAMEZAWA Hiroyuki | 1cfb419 | 2008-02-07 00:14:37 -0800 | [diff] [blame] | 1892 | } |
Nick Piggin | 408d854 | 2006-09-25 23:31:27 -0700 | [diff] [blame] | 1893 | |
Rik van Riel | a79311c | 2009-01-06 14:40:01 -0800 | [diff] [blame] | 1894 | shrink_zone(priority, zone, sc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | } |
Minchan Kim | d190836 | 2010-09-22 13:05:01 -0700 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | static bool zone_reclaimable(struct zone *zone) |
| 1899 | { |
| 1900 | return zone->pages_scanned < zone_reclaimable_pages(zone) * 6; |
| 1901 | } |
| 1902 | |
| 1903 | /* |
| 1904 | * As hibernation is going on, kswapd is freezed so that it can't mark |
| 1905 | * the zone into all_unreclaimable. It can't handle OOM during hibernation. |
| 1906 | * So let's check zone's unreclaimable in direct reclaim as well as kswapd. |
| 1907 | */ |
| 1908 | static bool all_unreclaimable(struct zonelist *zonelist, |
| 1909 | struct scan_control *sc) |
| 1910 | { |
| 1911 | struct zoneref *z; |
| 1912 | struct zone *zone; |
| 1913 | bool all_unreclaimable = true; |
| 1914 | |
| 1915 | for_each_zone_zonelist_nodemask(zone, z, zonelist, |
| 1916 | gfp_zone(sc->gfp_mask), sc->nodemask) { |
| 1917 | if (!populated_zone(zone)) |
| 1918 | continue; |
| 1919 | if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) |
| 1920 | continue; |
| 1921 | if (zone_reclaimable(zone)) { |
| 1922 | all_unreclaimable = false; |
| 1923 | break; |
| 1924 | } |
| 1925 | } |
| 1926 | |
KOSAKI Motohiro | bb21c7c | 2010-06-04 14:15:05 -0700 | [diff] [blame] | 1927 | return all_unreclaimable; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | } |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | /* |
| 1931 | * This is the main entry point to direct page reclaim. |
| 1932 | * |
| 1933 | * If a full scan of the inactive list fails to free enough memory then we |
| 1934 | * are "out of memory" and something needs to be killed. |
| 1935 | * |
| 1936 | * If the caller is !__GFP_FS then the probability of a failure is reasonably |
| 1937 | * high - the zone may be full of dirty or under-writeback pages, which this |
Jens Axboe | 5b0830c | 2009-09-23 19:37:09 +0200 | [diff] [blame] | 1938 | * caller can't do much about. We kick the writeback threads and take explicit |
| 1939 | * naps in the hope that some of these pages can be written. But if the |
| 1940 | * allocating task holds filesystem locks which prevent writeout this might not |
| 1941 | * work, and the allocation attempt will fail. |
Nishanth Aravamudan | a41f24e | 2008-04-29 00:58:25 -0700 | [diff] [blame] | 1942 | * |
| 1943 | * returns: 0, if no pages reclaimed |
| 1944 | * else, the number of pages reclaimed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | */ |
Mel Gorman | dac1d27 | 2008-04-28 02:12:12 -0700 | [diff] [blame] | 1946 | static unsigned long do_try_to_free_pages(struct zonelist *zonelist, |
Mel Gorman | dd1a239 | 2008-04-28 02:12:17 -0700 | [diff] [blame] | 1947 | struct scan_control *sc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | { |
| 1949 | int priority; |
Andrew Morton | 69e0594 | 2006-03-22 00:08:19 -0800 | [diff] [blame] | 1950 | unsigned long total_scanned = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | struct reclaim_state *reclaim_state = current->reclaim_state; |
Mel Gorman | dd1a239 | 2008-04-28 02:12:17 -0700 | [diff] [blame] | 1952 | struct zoneref *z; |
Mel Gorman | 54a6eb5 | 2008-04-28 02:12:16 -0700 | [diff] [blame] | 1953 | struct zone *zone; |
KOSAKI Motohiro | 22fba33 | 2009-12-14 17:59:10 -0800 | [diff] [blame] | 1954 | unsigned long writeback_threshold; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | |
Miao Xie | c0ff745 | 2010-05-24 14:32:08 -0700 | [diff] [blame] | 1956 | get_mems_allowed(); |
Keika Kobayashi | 873b477 | 2008-07-25 01:48:52 -0700 | [diff] [blame] | 1957 | delayacct_freepages_start(); |
| 1958 | |
KAMEZAWA Hiroyuki | e72e2bd | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 1959 | if (scanning_global_lru(sc)) |
KAMEZAWA Hiroyuki | 1cfb419 | 2008-02-07 00:14:37 -0800 | [diff] [blame] | 1960 | count_vm_event(ALLOCSTALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | |
| 1962 | for (priority = DEF_PRIORITY; priority >= 0; priority--) { |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1963 | sc->nr_scanned = 0; |
Rik van Riel | f7b7fd8 | 2005-11-28 13:44:07 -0800 | [diff] [blame] | 1964 | if (!priority) |
| 1965 | disable_swap_token(); |
Minchan Kim | d190836 | 2010-09-22 13:05:01 -0700 | [diff] [blame] | 1966 | shrink_zones(priority, zonelist, sc); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1967 | /* |
| 1968 | * Don't shrink slabs when reclaiming memory from |
| 1969 | * over limit cgroups |
| 1970 | */ |
KAMEZAWA Hiroyuki | e72e2bd | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 1971 | if (scanning_global_lru(sc)) { |
KOSAKI Motohiro | c6a8a8c | 2010-08-09 17:19:14 -0700 | [diff] [blame] | 1972 | unsigned long lru_pages = 0; |
Mel Gorman | d4debc6 | 2010-08-09 17:19:29 -0700 | [diff] [blame] | 1973 | for_each_zone_zonelist(zone, z, zonelist, |
| 1974 | gfp_zone(sc->gfp_mask)) { |
KOSAKI Motohiro | c6a8a8c | 2010-08-09 17:19:14 -0700 | [diff] [blame] | 1975 | if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) |
| 1976 | continue; |
| 1977 | |
| 1978 | lru_pages += zone_reclaimable_pages(zone); |
| 1979 | } |
| 1980 | |
Mel Gorman | dd1a239 | 2008-04-28 02:12:17 -0700 | [diff] [blame] | 1981 | shrink_slab(sc->nr_scanned, sc->gfp_mask, lru_pages); |
KAMEZAWA Hiroyuki | 91a4547 | 2008-02-07 00:14:29 -0800 | [diff] [blame] | 1982 | if (reclaim_state) { |
Rik van Riel | a79311c | 2009-01-06 14:40:01 -0800 | [diff] [blame] | 1983 | sc->nr_reclaimed += reclaim_state->reclaimed_slab; |
KAMEZAWA Hiroyuki | 91a4547 | 2008-02-07 00:14:29 -0800 | [diff] [blame] | 1984 | reclaim_state->reclaimed_slab = 0; |
| 1985 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | } |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1987 | total_scanned += sc->nr_scanned; |
KOSAKI Motohiro | bb21c7c | 2010-06-04 14:15:05 -0700 | [diff] [blame] | 1988 | if (sc->nr_reclaimed >= sc->nr_to_reclaim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | |
| 1991 | /* |
| 1992 | * Try to write back as many pages as we just scanned. This |
| 1993 | * tends to cause slow streaming writers to write data to the |
| 1994 | * disk smoothly, at the dirtying rate, which is nice. But |
| 1995 | * that's undesirable in laptop mode, where we *want* lumpy |
| 1996 | * writeout. So in laptop mode, write out the whole world. |
| 1997 | */ |
KOSAKI Motohiro | 22fba33 | 2009-12-14 17:59:10 -0800 | [diff] [blame] | 1998 | writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2; |
| 1999 | if (total_scanned > writeback_threshold) { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 2000 | wakeup_flusher_threads(laptop_mode ? 0 : total_scanned); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2001 | sc->may_writepage = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | /* Take a nap, wait for some writeback to complete */ |
KOSAKI Motohiro | 7b51755 | 2009-12-14 17:59:12 -0800 | [diff] [blame] | 2005 | if (!sc->hibernation_mode && sc->nr_scanned && |
Mel Gorman | 0e093d99 | 2010-10-26 14:21:45 -0700 | [diff] [blame] | 2006 | priority < DEF_PRIORITY - 2) { |
| 2007 | struct zone *preferred_zone; |
| 2008 | |
| 2009 | first_zones_zonelist(zonelist, gfp_zone(sc->gfp_mask), |
| 2010 | NULL, &preferred_zone); |
| 2011 | wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/10); |
| 2012 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | } |
KOSAKI Motohiro | bb21c7c | 2010-06-04 14:15:05 -0700 | [diff] [blame] | 2014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | out: |
Keika Kobayashi | 873b477 | 2008-07-25 01:48:52 -0700 | [diff] [blame] | 2016 | delayacct_freepages_end(); |
Miao Xie | c0ff745 | 2010-05-24 14:32:08 -0700 | [diff] [blame] | 2017 | put_mems_allowed(); |
Keika Kobayashi | 873b477 | 2008-07-25 01:48:52 -0700 | [diff] [blame] | 2018 | |
KOSAKI Motohiro | bb21c7c | 2010-06-04 14:15:05 -0700 | [diff] [blame] | 2019 | if (sc->nr_reclaimed) |
| 2020 | return sc->nr_reclaimed; |
| 2021 | |
| 2022 | /* top priority shrink_zones still had more to do? don't OOM, then */ |
Minchan Kim | d190836 | 2010-09-22 13:05:01 -0700 | [diff] [blame] | 2023 | if (scanning_global_lru(sc) && !all_unreclaimable(zonelist, sc)) |
KOSAKI Motohiro | bb21c7c | 2010-06-04 14:15:05 -0700 | [diff] [blame] | 2024 | return 1; |
| 2025 | |
| 2026 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | } |
| 2028 | |
Mel Gorman | dac1d27 | 2008-04-28 02:12:12 -0700 | [diff] [blame] | 2029 | unsigned long try_to_free_pages(struct zonelist *zonelist, int order, |
KAMEZAWA Hiroyuki | 327c0e9 | 2009-03-31 15:23:31 -0700 | [diff] [blame] | 2030 | gfp_t gfp_mask, nodemask_t *nodemask) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2031 | { |
Mel Gorman | 33906bc | 2010-08-09 17:19:16 -0700 | [diff] [blame] | 2032 | unsigned long nr_reclaimed; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2033 | struct scan_control sc = { |
| 2034 | .gfp_mask = gfp_mask, |
| 2035 | .may_writepage = !laptop_mode, |
KOSAKI Motohiro | 22fba33 | 2009-12-14 17:59:10 -0800 | [diff] [blame] | 2036 | .nr_to_reclaim = SWAP_CLUSTER_MAX, |
Johannes Weiner | a6dc60f8 | 2009-03-31 15:19:30 -0700 | [diff] [blame] | 2037 | .may_unmap = 1, |
KOSAKI Motohiro | 2e2e425 | 2009-04-21 12:24:57 -0700 | [diff] [blame] | 2038 | .may_swap = 1, |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2039 | .swappiness = vm_swappiness, |
| 2040 | .order = order, |
| 2041 | .mem_cgroup = NULL, |
KAMEZAWA Hiroyuki | 327c0e9 | 2009-03-31 15:23:31 -0700 | [diff] [blame] | 2042 | .nodemask = nodemask, |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2043 | }; |
| 2044 | |
Mel Gorman | 33906bc | 2010-08-09 17:19:16 -0700 | [diff] [blame] | 2045 | trace_mm_vmscan_direct_reclaim_begin(order, |
| 2046 | sc.may_writepage, |
| 2047 | gfp_mask); |
| 2048 | |
| 2049 | nr_reclaimed = do_try_to_free_pages(zonelist, &sc); |
| 2050 | |
| 2051 | trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); |
| 2052 | |
| 2053 | return nr_reclaimed; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2054 | } |
| 2055 | |
Balbir Singh | 00f0b82 | 2008-03-04 14:28:39 -0800 | [diff] [blame] | 2056 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2057 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 2058 | unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *mem, |
| 2059 | gfp_t gfp_mask, bool noswap, |
| 2060 | unsigned int swappiness, |
KOSAKI Motohiro | 14fec79 | 2010-08-10 18:03:05 -0700 | [diff] [blame] | 2061 | struct zone *zone) |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 2062 | { |
| 2063 | struct scan_control sc = { |
KOSAKI Motohiro | b8f5c56 | 2010-08-10 18:03:02 -0700 | [diff] [blame] | 2064 | .nr_to_reclaim = SWAP_CLUSTER_MAX, |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 2065 | .may_writepage = !laptop_mode, |
| 2066 | .may_unmap = 1, |
| 2067 | .may_swap = !noswap, |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 2068 | .swappiness = swappiness, |
| 2069 | .order = 0, |
| 2070 | .mem_cgroup = mem, |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 2071 | }; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 2072 | sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | |
| 2073 | (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); |
KOSAKI Motohiro | bdce6d9 | 2010-08-09 17:19:56 -0700 | [diff] [blame] | 2074 | |
| 2075 | trace_mm_vmscan_memcg_softlimit_reclaim_begin(0, |
| 2076 | sc.may_writepage, |
| 2077 | sc.gfp_mask); |
| 2078 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 2079 | /* |
| 2080 | * NOTE: Although we can get the priority field, using it |
| 2081 | * here is not a good idea, since it limits the pages we can scan. |
| 2082 | * if we don't reclaim here, the shrink_zone from balance_pgdat |
| 2083 | * will pick up pages from other mem cgroup's as well. We hack |
| 2084 | * the priority and make it zero. |
| 2085 | */ |
| 2086 | shrink_zone(0, zone, &sc); |
KOSAKI Motohiro | bdce6d9 | 2010-08-09 17:19:56 -0700 | [diff] [blame] | 2087 | |
| 2088 | trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); |
| 2089 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 2090 | return sc.nr_reclaimed; |
| 2091 | } |
| 2092 | |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 2093 | unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont, |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2094 | gfp_t gfp_mask, |
| 2095 | bool noswap, |
| 2096 | unsigned int swappiness) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2097 | { |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 2098 | struct zonelist *zonelist; |
KOSAKI Motohiro | bdce6d9 | 2010-08-09 17:19:56 -0700 | [diff] [blame] | 2099 | unsigned long nr_reclaimed; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2100 | struct scan_control sc = { |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2101 | .may_writepage = !laptop_mode, |
Johannes Weiner | a6dc60f8 | 2009-03-31 15:19:30 -0700 | [diff] [blame] | 2102 | .may_unmap = 1, |
KOSAKI Motohiro | 2e2e425 | 2009-04-21 12:24:57 -0700 | [diff] [blame] | 2103 | .may_swap = !noswap, |
KOSAKI Motohiro | 22fba33 | 2009-12-14 17:59:10 -0800 | [diff] [blame] | 2104 | .nr_to_reclaim = SWAP_CLUSTER_MAX, |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2105 | .swappiness = swappiness, |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2106 | .order = 0, |
| 2107 | .mem_cgroup = mem_cont, |
KAMEZAWA Hiroyuki | 327c0e9 | 2009-03-31 15:23:31 -0700 | [diff] [blame] | 2108 | .nodemask = NULL, /* we don't care the placement */ |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2109 | }; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2110 | |
Mel Gorman | dd1a239 | 2008-04-28 02:12:17 -0700 | [diff] [blame] | 2111 | sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | |
| 2112 | (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); |
| 2113 | zonelist = NODE_DATA(numa_node_id())->node_zonelists; |
KOSAKI Motohiro | bdce6d9 | 2010-08-09 17:19:56 -0700 | [diff] [blame] | 2114 | |
| 2115 | trace_mm_vmscan_memcg_reclaim_begin(0, |
| 2116 | sc.may_writepage, |
| 2117 | sc.gfp_mask); |
| 2118 | |
| 2119 | nr_reclaimed = do_try_to_free_pages(zonelist, &sc); |
| 2120 | |
| 2121 | trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); |
| 2122 | |
| 2123 | return nr_reclaimed; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2124 | } |
| 2125 | #endif |
| 2126 | |
Mel Gorman | f50de2d | 2009-12-14 17:58:53 -0800 | [diff] [blame] | 2127 | /* is kswapd sleeping prematurely? */ |
KOSAKI Motohiro | bb3ab59 | 2009-12-14 17:58:55 -0800 | [diff] [blame] | 2128 | static int sleeping_prematurely(pg_data_t *pgdat, int order, long remaining) |
Mel Gorman | f50de2d | 2009-12-14 17:58:53 -0800 | [diff] [blame] | 2129 | { |
KOSAKI Motohiro | bb3ab59 | 2009-12-14 17:58:55 -0800 | [diff] [blame] | 2130 | int i; |
Mel Gorman | f50de2d | 2009-12-14 17:58:53 -0800 | [diff] [blame] | 2131 | |
| 2132 | /* If a direct reclaimer woke kswapd within HZ/10, it's premature */ |
| 2133 | if (remaining) |
| 2134 | return 1; |
| 2135 | |
| 2136 | /* If after HZ/10, a zone is below the high mark, it's premature */ |
KOSAKI Motohiro | bb3ab59 | 2009-12-14 17:58:55 -0800 | [diff] [blame] | 2137 | for (i = 0; i < pgdat->nr_zones; i++) { |
| 2138 | struct zone *zone = pgdat->node_zones + i; |
| 2139 | |
| 2140 | if (!populated_zone(zone)) |
| 2141 | continue; |
| 2142 | |
KOSAKI Motohiro | 93e4a89 | 2010-03-05 13:41:55 -0800 | [diff] [blame] | 2143 | if (zone->all_unreclaimable) |
KOSAKI Motohiro | de3fab3 | 2010-01-15 17:01:25 -0800 | [diff] [blame] | 2144 | continue; |
| 2145 | |
Mel Gorman | f50de2d | 2009-12-14 17:58:53 -0800 | [diff] [blame] | 2146 | if (!zone_watermark_ok(zone, order, high_wmark_pages(zone), |
| 2147 | 0, 0)) |
| 2148 | return 1; |
KOSAKI Motohiro | bb3ab59 | 2009-12-14 17:58:55 -0800 | [diff] [blame] | 2149 | } |
Mel Gorman | f50de2d | 2009-12-14 17:58:53 -0800 | [diff] [blame] | 2150 | |
| 2151 | return 0; |
| 2152 | } |
| 2153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | /* |
| 2155 | * For kswapd, balance_pgdat() will work across all this node's zones until |
Mel Gorman | 4185896 | 2009-06-16 15:32:12 -0700 | [diff] [blame] | 2156 | * they are all at high_wmark_pages(zone). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | * Returns the number of pages which were actually freed. |
| 2159 | * |
| 2160 | * There is special handling here for zones which are full of pinned pages. |
| 2161 | * This can happen if the pages are all mlocked, or if they are all used by |
| 2162 | * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb. |
| 2163 | * What we do is to detect the case where all pages in the zone have been |
| 2164 | * scanned twice and there has been zero successful reclaim. Mark the zone as |
| 2165 | * dead and from now on, only perform a short scan. Basically we're polling |
| 2166 | * the zone for when the problem goes away. |
| 2167 | * |
| 2168 | * kswapd scans the zones in the highmem->normal->dma direction. It skips |
Mel Gorman | 4185896 | 2009-06-16 15:32:12 -0700 | [diff] [blame] | 2169 | * zones which have free_pages > high_wmark_pages(zone), but once a zone is |
| 2170 | * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the |
| 2171 | * lower zones regardless of the number of free pages in the lower zones. This |
| 2172 | * interoperates with the page allocator fallback scheme to ensure that aging |
| 2173 | * of pages is balanced across the zones. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | */ |
Rafael J. Wysocki | d6277db | 2006-06-23 02:03:18 -0700 | [diff] [blame] | 2175 | static unsigned long balance_pgdat(pg_data_t *pgdat, int order) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | int all_zones_ok; |
| 2178 | int priority; |
| 2179 | int i; |
Andrew Morton | 69e0594 | 2006-03-22 00:08:19 -0800 | [diff] [blame] | 2180 | unsigned long total_scanned; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | struct reclaim_state *reclaim_state = current->reclaim_state; |
Andrew Morton | 179e963 | 2006-03-22 00:08:18 -0800 | [diff] [blame] | 2182 | struct scan_control sc = { |
| 2183 | .gfp_mask = GFP_KERNEL, |
Johannes Weiner | a6dc60f8 | 2009-03-31 15:19:30 -0700 | [diff] [blame] | 2184 | .may_unmap = 1, |
KOSAKI Motohiro | 2e2e425 | 2009-04-21 12:24:57 -0700 | [diff] [blame] | 2185 | .may_swap = 1, |
KOSAKI Motohiro | 22fba33 | 2009-12-14 17:59:10 -0800 | [diff] [blame] | 2186 | /* |
| 2187 | * kswapd doesn't want to be bailed out while reclaim. because |
| 2188 | * we want to put equal scanning pressure on each zone. |
| 2189 | */ |
| 2190 | .nr_to_reclaim = ULONG_MAX, |
Rafael J. Wysocki | d6277db | 2006-06-23 02:03:18 -0700 | [diff] [blame] | 2191 | .swappiness = vm_swappiness, |
Andy Whitcroft | 5ad333e | 2007-07-17 04:03:16 -0700 | [diff] [blame] | 2192 | .order = order, |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 2193 | .mem_cgroup = NULL, |
Andrew Morton | 179e963 | 2006-03-22 00:08:18 -0800 | [diff] [blame] | 2194 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | loop_again: |
| 2196 | total_scanned = 0; |
Rik van Riel | a79311c | 2009-01-06 14:40:01 -0800 | [diff] [blame] | 2197 | sc.nr_reclaimed = 0; |
Christoph Lameter | c0bbbc7 | 2006-06-11 15:22:26 -0700 | [diff] [blame] | 2198 | sc.may_writepage = !laptop_mode; |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 2199 | count_vm_event(PAGEOUTRUN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | for (priority = DEF_PRIORITY; priority >= 0; priority--) { |
| 2202 | int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */ |
| 2203 | unsigned long lru_pages = 0; |
KOSAKI Motohiro | bb3ab59 | 2009-12-14 17:58:55 -0800 | [diff] [blame] | 2204 | int has_under_min_watermark_zone = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | |
Rik van Riel | f7b7fd8 | 2005-11-28 13:44:07 -0800 | [diff] [blame] | 2206 | /* The swap token gets in the way of swapout... */ |
| 2207 | if (!priority) |
| 2208 | disable_swap_token(); |
| 2209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | all_zones_ok = 1; |
| 2211 | |
Rafael J. Wysocki | d6277db | 2006-06-23 02:03:18 -0700 | [diff] [blame] | 2212 | /* |
| 2213 | * Scan in the highmem->dma direction for the highest |
| 2214 | * zone which needs scanning |
| 2215 | */ |
| 2216 | for (i = pgdat->nr_zones - 1; i >= 0; i--) { |
| 2217 | struct zone *zone = pgdat->node_zones + i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | |
Rafael J. Wysocki | d6277db | 2006-06-23 02:03:18 -0700 | [diff] [blame] | 2219 | if (!populated_zone(zone)) |
| 2220 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | |
KOSAKI Motohiro | 93e4a89 | 2010-03-05 13:41:55 -0800 | [diff] [blame] | 2222 | if (zone->all_unreclaimable && priority != DEF_PRIORITY) |
Rafael J. Wysocki | d6277db | 2006-06-23 02:03:18 -0700 | [diff] [blame] | 2223 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | |
Rik van Riel | 556adec | 2008-10-18 20:26:34 -0700 | [diff] [blame] | 2225 | /* |
| 2226 | * Do some background aging of the anon list, to give |
| 2227 | * pages a chance to be referenced before reclaiming. |
| 2228 | */ |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 2229 | if (inactive_anon_is_low(zone, &sc)) |
Rik van Riel | 556adec | 2008-10-18 20:26:34 -0700 | [diff] [blame] | 2230 | shrink_active_list(SWAP_CLUSTER_MAX, zone, |
| 2231 | &sc, priority, 0); |
| 2232 | |
Mel Gorman | 4185896 | 2009-06-16 15:32:12 -0700 | [diff] [blame] | 2233 | if (!zone_watermark_ok(zone, order, |
| 2234 | high_wmark_pages(zone), 0, 0)) { |
Rafael J. Wysocki | d6277db | 2006-06-23 02:03:18 -0700 | [diff] [blame] | 2235 | end_zone = i; |
Andrew Morton | e1dbeda | 2006-12-06 20:32:01 -0800 | [diff] [blame] | 2236 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | } |
Andrew Morton | e1dbeda | 2006-12-06 20:32:01 -0800 | [diff] [blame] | 2239 | if (i < 0) |
| 2240 | goto out; |
| 2241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | for (i = 0; i <= end_zone; i++) { |
| 2243 | struct zone *zone = pgdat->node_zones + i; |
| 2244 | |
Wu Fengguang | adea02a | 2009-09-21 17:01:42 -0700 | [diff] [blame] | 2245 | lru_pages += zone_reclaimable_pages(zone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | /* |
| 2249 | * Now scan the zone in the dma->highmem direction, stopping |
| 2250 | * at the last zone which needs scanning. |
| 2251 | * |
| 2252 | * We do this because the page allocator works in the opposite |
| 2253 | * direction. This prevents the page allocator from allocating |
| 2254 | * pages behind kswapd's direction of progress, which would |
| 2255 | * cause too much scanning of the lower zones. |
| 2256 | */ |
| 2257 | for (i = 0; i <= end_zone; i++) { |
| 2258 | struct zone *zone = pgdat->node_zones + i; |
akpm@osdl.org | b15e090 | 2005-06-21 17:14:35 -0700 | [diff] [blame] | 2259 | int nr_slab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | |
Con Kolivas | f3fe651 | 2006-01-06 00:11:15 -0800 | [diff] [blame] | 2261 | if (!populated_zone(zone)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | continue; |
| 2263 | |
KOSAKI Motohiro | 93e4a89 | 2010-03-05 13:41:55 -0800 | [diff] [blame] | 2264 | if (zone->all_unreclaimable && priority != DEF_PRIORITY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | continue; |
| 2266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | sc.nr_scanned = 0; |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 2268 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 2269 | /* |
| 2270 | * Call soft limit reclaim before calling shrink_zone. |
| 2271 | * For now we ignore the return value |
| 2272 | */ |
KOSAKI Motohiro | 00918b6 | 2010-08-10 18:03:05 -0700 | [diff] [blame] | 2273 | mem_cgroup_soft_limit_reclaim(zone, order, sc.gfp_mask); |
| 2274 | |
Rik van Riel | 32a4330 | 2007-10-16 01:24:50 -0700 | [diff] [blame] | 2275 | /* |
| 2276 | * We put equal pressure on every zone, unless one |
| 2277 | * zone has way too many pages free already. |
| 2278 | */ |
Mel Gorman | 4185896 | 2009-06-16 15:32:12 -0700 | [diff] [blame] | 2279 | if (!zone_watermark_ok(zone, order, |
| 2280 | 8*high_wmark_pages(zone), end_zone, 0)) |
Rik van Riel | a79311c | 2009-01-06 14:40:01 -0800 | [diff] [blame] | 2281 | shrink_zone(priority, zone, &sc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | reclaim_state->reclaimed_slab = 0; |
akpm@osdl.org | b15e090 | 2005-06-21 17:14:35 -0700 | [diff] [blame] | 2283 | nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL, |
| 2284 | lru_pages); |
Rik van Riel | a79311c | 2009-01-06 14:40:01 -0800 | [diff] [blame] | 2285 | sc.nr_reclaimed += reclaim_state->reclaimed_slab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | total_scanned += sc.nr_scanned; |
KOSAKI Motohiro | 93e4a89 | 2010-03-05 13:41:55 -0800 | [diff] [blame] | 2287 | if (zone->all_unreclaimable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | continue; |
Minchan Kim | d190836 | 2010-09-22 13:05:01 -0700 | [diff] [blame] | 2289 | if (nr_slab == 0 && !zone_reclaimable(zone)) |
KOSAKI Motohiro | 93e4a89 | 2010-03-05 13:41:55 -0800 | [diff] [blame] | 2290 | zone->all_unreclaimable = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | /* |
| 2292 | * If we've done a decent amount of scanning and |
| 2293 | * the reclaim ratio is low, start doing writepage |
| 2294 | * even in laptop mode |
| 2295 | */ |
| 2296 | if (total_scanned > SWAP_CLUSTER_MAX * 2 && |
Rik van Riel | a79311c | 2009-01-06 14:40:01 -0800 | [diff] [blame] | 2297 | total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2298 | sc.may_writepage = 1; |
KOSAKI Motohiro | bb3ab59 | 2009-12-14 17:58:55 -0800 | [diff] [blame] | 2299 | |
Minchan Kim | 45973d7 | 2010-03-05 13:41:45 -0800 | [diff] [blame] | 2300 | if (!zone_watermark_ok(zone, order, |
| 2301 | high_wmark_pages(zone), end_zone, 0)) { |
| 2302 | all_zones_ok = 0; |
| 2303 | /* |
| 2304 | * We are still under min water mark. This |
| 2305 | * means that we have a GFP_ATOMIC allocation |
| 2306 | * failure risk. Hurry up! |
| 2307 | */ |
| 2308 | if (!zone_watermark_ok(zone, order, |
| 2309 | min_wmark_pages(zone), end_zone, 0)) |
| 2310 | has_under_min_watermark_zone = 1; |
Mel Gorman | 0e093d99 | 2010-10-26 14:21:45 -0700 | [diff] [blame] | 2311 | } else { |
| 2312 | /* |
| 2313 | * If a zone reaches its high watermark, |
| 2314 | * consider it to be no longer congested. It's |
| 2315 | * possible there are dirty pages backed by |
| 2316 | * congested BDIs but as pressure is relieved, |
| 2317 | * spectulatively avoid congestion waits |
| 2318 | */ |
| 2319 | zone_clear_flag(zone, ZONE_CONGESTED); |
Minchan Kim | 45973d7 | 2010-03-05 13:41:45 -0800 | [diff] [blame] | 2320 | } |
KOSAKI Motohiro | bb3ab59 | 2009-12-14 17:58:55 -0800 | [diff] [blame] | 2321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | if (all_zones_ok) |
| 2324 | break; /* kswapd: all done */ |
| 2325 | /* |
| 2326 | * OK, kswapd is getting into trouble. Take a nap, then take |
| 2327 | * another pass across the zones. |
| 2328 | */ |
KOSAKI Motohiro | bb3ab59 | 2009-12-14 17:58:55 -0800 | [diff] [blame] | 2329 | if (total_scanned && (priority < DEF_PRIORITY - 2)) { |
| 2330 | if (has_under_min_watermark_zone) |
| 2331 | count_vm_event(KSWAPD_SKIP_CONGESTION_WAIT); |
| 2332 | else |
| 2333 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
| 2334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | |
| 2336 | /* |
| 2337 | * We do this so kswapd doesn't build up large priorities for |
| 2338 | * example when it is freeing in parallel with allocators. It |
| 2339 | * matches the direct reclaim path behaviour in terms of impact |
| 2340 | * on zone->*_priority. |
| 2341 | */ |
Rik van Riel | a79311c | 2009-01-06 14:40:01 -0800 | [diff] [blame] | 2342 | if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2343 | break; |
| 2344 | } |
| 2345 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | if (!all_zones_ok) { |
| 2347 | cond_resched(); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2348 | |
| 2349 | try_to_freeze(); |
| 2350 | |
KOSAKI Motohiro | 73ce02e | 2009-01-06 14:40:33 -0800 | [diff] [blame] | 2351 | /* |
| 2352 | * Fragmentation may mean that the system cannot be |
| 2353 | * rebalanced for high-order allocations in all zones. |
| 2354 | * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX, |
| 2355 | * it means the zones have been fully scanned and are still |
| 2356 | * not balanced. For high-order allocations, there is |
| 2357 | * little point trying all over again as kswapd may |
| 2358 | * infinite loop. |
| 2359 | * |
| 2360 | * Instead, recheck all watermarks at order-0 as they |
| 2361 | * are the most important. If watermarks are ok, kswapd will go |
| 2362 | * back to sleep. High-order users can still perform direct |
| 2363 | * reclaim if they wish. |
| 2364 | */ |
| 2365 | if (sc.nr_reclaimed < SWAP_CLUSTER_MAX) |
| 2366 | order = sc.order = 0; |
| 2367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | goto loop_again; |
| 2369 | } |
| 2370 | |
Rik van Riel | a79311c | 2009-01-06 14:40:01 -0800 | [diff] [blame] | 2371 | return sc.nr_reclaimed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | /* |
| 2375 | * The background pageout daemon, started as a kernel thread |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 2376 | * from the init process. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | * |
| 2378 | * This basically trickles out pages so that we have _some_ |
| 2379 | * free memory available even if there is no other activity |
| 2380 | * that frees anything up. This is needed for things like routing |
| 2381 | * etc, where we otherwise might have all activity going on in |
| 2382 | * asynchronous contexts that cannot page things out. |
| 2383 | * |
| 2384 | * If there are applications that are active memory-allocators |
| 2385 | * (most normal use), this basically shouldn't matter. |
| 2386 | */ |
| 2387 | static int kswapd(void *p) |
| 2388 | { |
| 2389 | unsigned long order; |
| 2390 | pg_data_t *pgdat = (pg_data_t*)p; |
| 2391 | struct task_struct *tsk = current; |
| 2392 | DEFINE_WAIT(wait); |
| 2393 | struct reclaim_state reclaim_state = { |
| 2394 | .reclaimed_slab = 0, |
| 2395 | }; |
Rusty Russell | a70f730 | 2009-03-13 14:49:46 +1030 | [diff] [blame] | 2396 | const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 2398 | lockdep_set_current_reclaim_state(GFP_KERNEL); |
| 2399 | |
Rusty Russell | 174596a | 2009-01-01 10:12:29 +1030 | [diff] [blame] | 2400 | if (!cpumask_empty(cpumask)) |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 2401 | set_cpus_allowed_ptr(tsk, cpumask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | current->reclaim_state = &reclaim_state; |
| 2403 | |
| 2404 | /* |
| 2405 | * Tell the memory management that we're a "memory allocator", |
| 2406 | * and that if we need more memory we should get access to it |
| 2407 | * regardless (see "__alloc_pages()"). "kswapd" should |
| 2408 | * never get caught in the normal page freeing logic. |
| 2409 | * |
| 2410 | * (Kswapd normally doesn't need memory anyway, but sometimes |
| 2411 | * you need a small amount of memory in order to be able to |
| 2412 | * page out something else, and this flag essentially protects |
| 2413 | * us from recursively trying to free more memory as we're |
| 2414 | * trying to free the first piece of memory in the first place). |
| 2415 | */ |
Christoph Lameter | 930d915 | 2006-01-08 01:00:47 -0800 | [diff] [blame] | 2416 | tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 2417 | set_freezable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | |
| 2419 | order = 0; |
| 2420 | for ( ; ; ) { |
| 2421 | unsigned long new_order; |
David Rientjes | 8fe23e0 | 2009-12-14 17:58:33 -0800 | [diff] [blame] | 2422 | int ret; |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 2423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); |
| 2425 | new_order = pgdat->kswapd_max_order; |
| 2426 | pgdat->kswapd_max_order = 0; |
| 2427 | if (order < new_order) { |
| 2428 | /* |
| 2429 | * Don't sleep if someone wants a larger 'order' |
| 2430 | * allocation |
| 2431 | */ |
| 2432 | order = new_order; |
| 2433 | } else { |
Mel Gorman | f50de2d | 2009-12-14 17:58:53 -0800 | [diff] [blame] | 2434 | if (!freezing(current) && !kthread_should_stop()) { |
| 2435 | long remaining = 0; |
| 2436 | |
| 2437 | /* Try to sleep for a short interval */ |
KOSAKI Motohiro | bb3ab59 | 2009-12-14 17:58:55 -0800 | [diff] [blame] | 2438 | if (!sleeping_prematurely(pgdat, order, remaining)) { |
Mel Gorman | f50de2d | 2009-12-14 17:58:53 -0800 | [diff] [blame] | 2439 | remaining = schedule_timeout(HZ/10); |
| 2440 | finish_wait(&pgdat->kswapd_wait, &wait); |
| 2441 | prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); |
| 2442 | } |
| 2443 | |
| 2444 | /* |
| 2445 | * After a short sleep, check if it was a |
| 2446 | * premature sleep. If not, then go fully |
| 2447 | * to sleep until explicitly woken up |
| 2448 | */ |
Mel Gorman | 33906bc | 2010-08-09 17:19:16 -0700 | [diff] [blame] | 2449 | if (!sleeping_prematurely(pgdat, order, remaining)) { |
| 2450 | trace_mm_vmscan_kswapd_sleep(pgdat->node_id); |
Mel Gorman | f50de2d | 2009-12-14 17:58:53 -0800 | [diff] [blame] | 2451 | schedule(); |
Mel Gorman | 33906bc | 2010-08-09 17:19:16 -0700 | [diff] [blame] | 2452 | } else { |
Mel Gorman | f50de2d | 2009-12-14 17:58:53 -0800 | [diff] [blame] | 2453 | if (remaining) |
KOSAKI Motohiro | bb3ab59 | 2009-12-14 17:58:55 -0800 | [diff] [blame] | 2454 | count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); |
Mel Gorman | f50de2d | 2009-12-14 17:58:53 -0800 | [diff] [blame] | 2455 | else |
KOSAKI Motohiro | bb3ab59 | 2009-12-14 17:58:55 -0800 | [diff] [blame] | 2456 | count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); |
Mel Gorman | f50de2d | 2009-12-14 17:58:53 -0800 | [diff] [blame] | 2457 | } |
| 2458 | } |
Rafael J. Wysocki | b1296cc | 2007-05-06 14:50:48 -0700 | [diff] [blame] | 2459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | order = pgdat->kswapd_max_order; |
| 2461 | } |
| 2462 | finish_wait(&pgdat->kswapd_wait, &wait); |
| 2463 | |
David Rientjes | 8fe23e0 | 2009-12-14 17:58:33 -0800 | [diff] [blame] | 2464 | ret = try_to_freeze(); |
| 2465 | if (kthread_should_stop()) |
| 2466 | break; |
| 2467 | |
| 2468 | /* |
| 2469 | * We can speed up thawing tasks if we don't call balance_pgdat |
| 2470 | * after returning from the refrigerator |
| 2471 | */ |
Mel Gorman | 33906bc | 2010-08-09 17:19:16 -0700 | [diff] [blame] | 2472 | if (!ret) { |
| 2473 | trace_mm_vmscan_kswapd_wake(pgdat->node_id, order); |
Rafael J. Wysocki | b1296cc | 2007-05-06 14:50:48 -0700 | [diff] [blame] | 2474 | balance_pgdat(pgdat, order); |
Mel Gorman | 33906bc | 2010-08-09 17:19:16 -0700 | [diff] [blame] | 2475 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | } |
| 2477 | return 0; |
| 2478 | } |
| 2479 | |
| 2480 | /* |
| 2481 | * A zone is low on free memory, so wake its kswapd task to service it. |
| 2482 | */ |
| 2483 | void wakeup_kswapd(struct zone *zone, int order) |
| 2484 | { |
| 2485 | pg_data_t *pgdat; |
| 2486 | |
Con Kolivas | f3fe651 | 2006-01-06 00:11:15 -0800 | [diff] [blame] | 2487 | if (!populated_zone(zone)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | return; |
| 2489 | |
| 2490 | pgdat = zone->zone_pgdat; |
Mel Gorman | 4185896 | 2009-06-16 15:32:12 -0700 | [diff] [blame] | 2491 | if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | return; |
| 2493 | if (pgdat->kswapd_max_order < order) |
| 2494 | pgdat->kswapd_max_order = order; |
Mel Gorman | 33906bc | 2010-08-09 17:19:16 -0700 | [diff] [blame] | 2495 | trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order); |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 2496 | if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | return; |
Con Kolivas | 8d0986e | 2005-09-13 01:25:07 -0700 | [diff] [blame] | 2498 | if (!waitqueue_active(&pgdat->kswapd_wait)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2499 | return; |
Con Kolivas | 8d0986e | 2005-09-13 01:25:07 -0700 | [diff] [blame] | 2500 | wake_up_interruptible(&pgdat->kswapd_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | } |
| 2502 | |
Wu Fengguang | adea02a | 2009-09-21 17:01:42 -0700 | [diff] [blame] | 2503 | /* |
| 2504 | * The reclaimable count would be mostly accurate. |
| 2505 | * The less reclaimable pages may be |
| 2506 | * - mlocked pages, which will be moved to unevictable list when encountered |
| 2507 | * - mapped pages, which may require several travels to be reclaimed |
| 2508 | * - dirty pages, which is not "instantly" reclaimable |
| 2509 | */ |
| 2510 | unsigned long global_reclaimable_pages(void) |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 2511 | { |
Wu Fengguang | adea02a | 2009-09-21 17:01:42 -0700 | [diff] [blame] | 2512 | int nr; |
| 2513 | |
| 2514 | nr = global_page_state(NR_ACTIVE_FILE) + |
| 2515 | global_page_state(NR_INACTIVE_FILE); |
| 2516 | |
| 2517 | if (nr_swap_pages > 0) |
| 2518 | nr += global_page_state(NR_ACTIVE_ANON) + |
| 2519 | global_page_state(NR_INACTIVE_ANON); |
| 2520 | |
| 2521 | return nr; |
| 2522 | } |
| 2523 | |
| 2524 | unsigned long zone_reclaimable_pages(struct zone *zone) |
| 2525 | { |
| 2526 | int nr; |
| 2527 | |
| 2528 | nr = zone_page_state(zone, NR_ACTIVE_FILE) + |
| 2529 | zone_page_state(zone, NR_INACTIVE_FILE); |
| 2530 | |
| 2531 | if (nr_swap_pages > 0) |
| 2532 | nr += zone_page_state(zone, NR_ACTIVE_ANON) + |
| 2533 | zone_page_state(zone, NR_INACTIVE_ANON); |
| 2534 | |
| 2535 | return nr; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 2536 | } |
| 2537 | |
Rafael J. Wysocki | c6f37f1 | 2009-05-24 22:16:31 +0200 | [diff] [blame] | 2538 | #ifdef CONFIG_HIBERNATION |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2539 | /* |
KOSAKI Motohiro | 7b51755 | 2009-12-14 17:59:12 -0800 | [diff] [blame] | 2540 | * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of |
Rafael J. Wysocki | d6277db | 2006-06-23 02:03:18 -0700 | [diff] [blame] | 2541 | * freed pages. |
| 2542 | * |
| 2543 | * Rather than trying to age LRUs the aim is to preserve the overall |
| 2544 | * LRU order by reclaiming preferentially |
| 2545 | * inactive > active > active referenced > active mapped |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2546 | */ |
KOSAKI Motohiro | 7b51755 | 2009-12-14 17:59:12 -0800 | [diff] [blame] | 2547 | unsigned long shrink_all_memory(unsigned long nr_to_reclaim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | { |
Rafael J. Wysocki | d6277db | 2006-06-23 02:03:18 -0700 | [diff] [blame] | 2549 | struct reclaim_state reclaim_state; |
Rafael J. Wysocki | d6277db | 2006-06-23 02:03:18 -0700 | [diff] [blame] | 2550 | struct scan_control sc = { |
KOSAKI Motohiro | 7b51755 | 2009-12-14 17:59:12 -0800 | [diff] [blame] | 2551 | .gfp_mask = GFP_HIGHUSER_MOVABLE, |
| 2552 | .may_swap = 1, |
| 2553 | .may_unmap = 1, |
Rafael J. Wysocki | d6277db | 2006-06-23 02:03:18 -0700 | [diff] [blame] | 2554 | .may_writepage = 1, |
KOSAKI Motohiro | 7b51755 | 2009-12-14 17:59:12 -0800 | [diff] [blame] | 2555 | .nr_to_reclaim = nr_to_reclaim, |
| 2556 | .hibernation_mode = 1, |
| 2557 | .swappiness = vm_swappiness, |
| 2558 | .order = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2559 | }; |
KOSAKI Motohiro | 7b51755 | 2009-12-14 17:59:12 -0800 | [diff] [blame] | 2560 | struct zonelist * zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); |
| 2561 | struct task_struct *p = current; |
| 2562 | unsigned long nr_reclaimed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | |
KOSAKI Motohiro | 7b51755 | 2009-12-14 17:59:12 -0800 | [diff] [blame] | 2564 | p->flags |= PF_MEMALLOC; |
| 2565 | lockdep_set_current_reclaim_state(sc.gfp_mask); |
| 2566 | reclaim_state.reclaimed_slab = 0; |
| 2567 | p->reclaim_state = &reclaim_state; |
Andrew Morton | 69e0594 | 2006-03-22 00:08:19 -0800 | [diff] [blame] | 2568 | |
KOSAKI Motohiro | 7b51755 | 2009-12-14 17:59:12 -0800 | [diff] [blame] | 2569 | nr_reclaimed = do_try_to_free_pages(zonelist, &sc); |
Rafael J. Wysocki | d6277db | 2006-06-23 02:03:18 -0700 | [diff] [blame] | 2570 | |
KOSAKI Motohiro | 7b51755 | 2009-12-14 17:59:12 -0800 | [diff] [blame] | 2571 | p->reclaim_state = NULL; |
| 2572 | lockdep_clear_current_reclaim_state(); |
| 2573 | p->flags &= ~PF_MEMALLOC; |
Rafael J. Wysocki | d6277db | 2006-06-23 02:03:18 -0700 | [diff] [blame] | 2574 | |
KOSAKI Motohiro | 7b51755 | 2009-12-14 17:59:12 -0800 | [diff] [blame] | 2575 | return nr_reclaimed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2576 | } |
Rafael J. Wysocki | c6f37f1 | 2009-05-24 22:16:31 +0200 | [diff] [blame] | 2577 | #endif /* CONFIG_HIBERNATION */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | /* It's optimal to keep kswapds on the same CPUs as their memory, but |
| 2580 | not required for correctness. So if the last cpu in a node goes |
| 2581 | away, we get changed to run anywhere: as the first one comes back, |
| 2582 | restore their cpu bindings. */ |
Chandra Seetharaman | 9c7b216 | 2006-06-27 02:54:07 -0700 | [diff] [blame] | 2583 | static int __devinit cpu_callback(struct notifier_block *nfb, |
Andrew Morton | 69e0594 | 2006-03-22 00:08:19 -0800 | [diff] [blame] | 2584 | unsigned long action, void *hcpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | { |
Yasunori Goto | 58c0a4a | 2007-10-16 01:25:40 -0700 | [diff] [blame] | 2586 | int nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 2588 | if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { |
Yasunori Goto | 58c0a4a | 2007-10-16 01:25:40 -0700 | [diff] [blame] | 2589 | for_each_node_state(nid, N_HIGH_MEMORY) { |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 2590 | pg_data_t *pgdat = NODE_DATA(nid); |
Rusty Russell | a70f730 | 2009-03-13 14:49:46 +1030 | [diff] [blame] | 2591 | const struct cpumask *mask; |
| 2592 | |
| 2593 | mask = cpumask_of_node(pgdat->node_id); |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 2594 | |
Rusty Russell | 3e59794 | 2009-01-01 10:12:24 +1030 | [diff] [blame] | 2595 | if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | /* One of our CPUs online: restore mask */ |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 2597 | set_cpus_allowed_ptr(pgdat->kswapd, mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | } |
| 2599 | } |
| 2600 | return NOTIFY_OK; |
| 2601 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | |
Yasunori Goto | 3218ae1 | 2006-06-27 02:53:33 -0700 | [diff] [blame] | 2603 | /* |
| 2604 | * This kswapd start function will be called by init and node-hot-add. |
| 2605 | * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. |
| 2606 | */ |
| 2607 | int kswapd_run(int nid) |
| 2608 | { |
| 2609 | pg_data_t *pgdat = NODE_DATA(nid); |
| 2610 | int ret = 0; |
| 2611 | |
| 2612 | if (pgdat->kswapd) |
| 2613 | return 0; |
| 2614 | |
| 2615 | pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); |
| 2616 | if (IS_ERR(pgdat->kswapd)) { |
| 2617 | /* failure at boot is fatal */ |
| 2618 | BUG_ON(system_state == SYSTEM_BOOTING); |
| 2619 | printk("Failed to start kswapd on node %d\n",nid); |
| 2620 | ret = -1; |
| 2621 | } |
| 2622 | return ret; |
| 2623 | } |
| 2624 | |
David Rientjes | 8fe23e0 | 2009-12-14 17:58:33 -0800 | [diff] [blame] | 2625 | /* |
| 2626 | * Called by memory hotplug when all memory in a node is offlined. |
| 2627 | */ |
| 2628 | void kswapd_stop(int nid) |
| 2629 | { |
| 2630 | struct task_struct *kswapd = NODE_DATA(nid)->kswapd; |
| 2631 | |
| 2632 | if (kswapd) |
| 2633 | kthread_stop(kswapd); |
| 2634 | } |
| 2635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | static int __init kswapd_init(void) |
| 2637 | { |
Yasunori Goto | 3218ae1 | 2006-06-27 02:53:33 -0700 | [diff] [blame] | 2638 | int nid; |
Andrew Morton | 69e0594 | 2006-03-22 00:08:19 -0800 | [diff] [blame] | 2639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | swap_setup(); |
Christoph Lameter | 9422ffb | 2007-10-16 01:25:31 -0700 | [diff] [blame] | 2641 | for_each_node_state(nid, N_HIGH_MEMORY) |
Yasunori Goto | 3218ae1 | 2006-06-27 02:53:33 -0700 | [diff] [blame] | 2642 | kswapd_run(nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | hotcpu_notifier(cpu_callback, 0); |
| 2644 | return 0; |
| 2645 | } |
| 2646 | |
| 2647 | module_init(kswapd_init) |
Christoph Lameter | 9eeff23 | 2006-01-18 17:42:31 -0800 | [diff] [blame] | 2648 | |
| 2649 | #ifdef CONFIG_NUMA |
| 2650 | /* |
| 2651 | * Zone reclaim mode |
| 2652 | * |
| 2653 | * If non-zero call zone_reclaim when the number of free pages falls below |
| 2654 | * the watermarks. |
Christoph Lameter | 9eeff23 | 2006-01-18 17:42:31 -0800 | [diff] [blame] | 2655 | */ |
| 2656 | int zone_reclaim_mode __read_mostly; |
| 2657 | |
Christoph Lameter | 1b2ffb7 | 2006-02-01 03:05:34 -0800 | [diff] [blame] | 2658 | #define RECLAIM_OFF 0 |
Fernando Luis Vazquez Cao | 7d03431 | 2008-07-29 22:33:41 -0700 | [diff] [blame] | 2659 | #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ |
Christoph Lameter | 1b2ffb7 | 2006-02-01 03:05:34 -0800 | [diff] [blame] | 2660 | #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ |
| 2661 | #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */ |
| 2662 | |
Christoph Lameter | 9eeff23 | 2006-01-18 17:42:31 -0800 | [diff] [blame] | 2663 | /* |
Christoph Lameter | a92f712 | 2006-02-01 03:05:32 -0800 | [diff] [blame] | 2664 | * Priority for ZONE_RECLAIM. This determines the fraction of pages |
| 2665 | * of a node considered for each zone_reclaim. 4 scans 1/16th of |
| 2666 | * a zone. |
| 2667 | */ |
| 2668 | #define ZONE_RECLAIM_PRIORITY 4 |
| 2669 | |
Christoph Lameter | 9eeff23 | 2006-01-18 17:42:31 -0800 | [diff] [blame] | 2670 | /* |
Christoph Lameter | 9614634 | 2006-07-03 00:24:13 -0700 | [diff] [blame] | 2671 | * Percentage of pages in a zone that must be unmapped for zone_reclaim to |
| 2672 | * occur. |
| 2673 | */ |
| 2674 | int sysctl_min_unmapped_ratio = 1; |
| 2675 | |
| 2676 | /* |
Christoph Lameter | 0ff3849 | 2006-09-25 23:31:52 -0700 | [diff] [blame] | 2677 | * If the number of slab pages in a zone grows beyond this percentage then |
| 2678 | * slab reclaim needs to occur. |
| 2679 | */ |
| 2680 | int sysctl_min_slab_ratio = 5; |
| 2681 | |
Mel Gorman | 90afa5d | 2009-06-16 15:33:20 -0700 | [diff] [blame] | 2682 | static inline unsigned long zone_unmapped_file_pages(struct zone *zone) |
| 2683 | { |
| 2684 | unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED); |
| 2685 | unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) + |
| 2686 | zone_page_state(zone, NR_ACTIVE_FILE); |
| 2687 | |
| 2688 | /* |
| 2689 | * It's possible for there to be more file mapped pages than |
| 2690 | * accounted for by the pages on the file LRU lists because |
| 2691 | * tmpfs pages accounted for as ANON can also be FILE_MAPPED |
| 2692 | */ |
| 2693 | return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; |
| 2694 | } |
| 2695 | |
| 2696 | /* Work out how many page cache pages we can reclaim in this reclaim_mode */ |
| 2697 | static long zone_pagecache_reclaimable(struct zone *zone) |
| 2698 | { |
| 2699 | long nr_pagecache_reclaimable; |
| 2700 | long delta = 0; |
| 2701 | |
| 2702 | /* |
| 2703 | * If RECLAIM_SWAP is set, then all file pages are considered |
| 2704 | * potentially reclaimable. Otherwise, we have to worry about |
| 2705 | * pages like swapcache and zone_unmapped_file_pages() provides |
| 2706 | * a better estimate |
| 2707 | */ |
| 2708 | if (zone_reclaim_mode & RECLAIM_SWAP) |
| 2709 | nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES); |
| 2710 | else |
| 2711 | nr_pagecache_reclaimable = zone_unmapped_file_pages(zone); |
| 2712 | |
| 2713 | /* If we can't clean pages, remove dirty pages from consideration */ |
| 2714 | if (!(zone_reclaim_mode & RECLAIM_WRITE)) |
| 2715 | delta += zone_page_state(zone, NR_FILE_DIRTY); |
| 2716 | |
| 2717 | /* Watch for any possible underflows due to delta */ |
| 2718 | if (unlikely(delta > nr_pagecache_reclaimable)) |
| 2719 | delta = nr_pagecache_reclaimable; |
| 2720 | |
| 2721 | return nr_pagecache_reclaimable - delta; |
| 2722 | } |
| 2723 | |
Christoph Lameter | 0ff3849 | 2006-09-25 23:31:52 -0700 | [diff] [blame] | 2724 | /* |
Christoph Lameter | 9eeff23 | 2006-01-18 17:42:31 -0800 | [diff] [blame] | 2725 | * Try to free up some pages from this zone through reclaim. |
| 2726 | */ |
Andrew Morton | 179e963 | 2006-03-22 00:08:18 -0800 | [diff] [blame] | 2727 | 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] | 2728 | { |
Christoph Lameter | 7fb2d46 | 2006-03-22 00:08:22 -0800 | [diff] [blame] | 2729 | /* Minimum pages needed in order to stay on node */ |
Andrew Morton | 69e0594 | 2006-03-22 00:08:19 -0800 | [diff] [blame] | 2730 | const unsigned long nr_pages = 1 << order; |
Christoph Lameter | 9eeff23 | 2006-01-18 17:42:31 -0800 | [diff] [blame] | 2731 | struct task_struct *p = current; |
| 2732 | struct reclaim_state reclaim_state; |
Christoph Lameter | 8695949 | 2006-03-22 00:08:18 -0800 | [diff] [blame] | 2733 | int priority; |
Andrew Morton | 179e963 | 2006-03-22 00:08:18 -0800 | [diff] [blame] | 2734 | struct scan_control sc = { |
| 2735 | .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE), |
Johannes Weiner | a6dc60f8 | 2009-03-31 15:19:30 -0700 | [diff] [blame] | 2736 | .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP), |
KOSAKI Motohiro | 2e2e425 | 2009-04-21 12:24:57 -0700 | [diff] [blame] | 2737 | .may_swap = 1, |
KOSAKI Motohiro | 22fba33 | 2009-12-14 17:59:10 -0800 | [diff] [blame] | 2738 | .nr_to_reclaim = max_t(unsigned long, nr_pages, |
| 2739 | SWAP_CLUSTER_MAX), |
Andrew Morton | 179e963 | 2006-03-22 00:08:18 -0800 | [diff] [blame] | 2740 | .gfp_mask = gfp_mask, |
Rafael J. Wysocki | d6277db | 2006-06-23 02:03:18 -0700 | [diff] [blame] | 2741 | .swappiness = vm_swappiness, |
Johannes Weiner | bd2f619 | 2009-03-31 15:19:38 -0700 | [diff] [blame] | 2742 | .order = order, |
Andrew Morton | 179e963 | 2006-03-22 00:08:18 -0800 | [diff] [blame] | 2743 | }; |
KOSAKI Motohiro | 1574804 | 2010-08-09 17:19:50 -0700 | [diff] [blame] | 2744 | unsigned long nr_slab_pages0, nr_slab_pages1; |
Christoph Lameter | 9eeff23 | 2006-01-18 17:42:31 -0800 | [diff] [blame] | 2745 | |
Christoph Lameter | 9eeff23 | 2006-01-18 17:42:31 -0800 | [diff] [blame] | 2746 | cond_resched(); |
Christoph Lameter | d4f7796 | 2006-02-24 13:04:22 -0800 | [diff] [blame] | 2747 | /* |
| 2748 | * We need to be able to allocate from the reserves for RECLAIM_SWAP |
| 2749 | * and we also need to be able to write out pages for RECLAIM_WRITE |
| 2750 | * and RECLAIM_SWAP. |
| 2751 | */ |
| 2752 | p->flags |= PF_MEMALLOC | PF_SWAPWRITE; |
KOSAKI Motohiro | 76ca542 | 2010-03-05 13:41:47 -0800 | [diff] [blame] | 2753 | lockdep_set_current_reclaim_state(gfp_mask); |
Christoph Lameter | 9eeff23 | 2006-01-18 17:42:31 -0800 | [diff] [blame] | 2754 | reclaim_state.reclaimed_slab = 0; |
| 2755 | p->reclaim_state = &reclaim_state; |
Christoph Lameter | c84db23 | 2006-02-01 03:05:29 -0800 | [diff] [blame] | 2756 | |
Mel Gorman | 90afa5d | 2009-06-16 15:33:20 -0700 | [diff] [blame] | 2757 | if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) { |
Christoph Lameter | 0ff3849 | 2006-09-25 23:31:52 -0700 | [diff] [blame] | 2758 | /* |
| 2759 | * Free memory by calling shrink zone with increasing |
| 2760 | * priorities until we have enough memory freed. |
| 2761 | */ |
| 2762 | priority = ZONE_RECLAIM_PRIORITY; |
| 2763 | do { |
Rik van Riel | a79311c | 2009-01-06 14:40:01 -0800 | [diff] [blame] | 2764 | shrink_zone(priority, zone, &sc); |
Christoph Lameter | 0ff3849 | 2006-09-25 23:31:52 -0700 | [diff] [blame] | 2765 | priority--; |
Rik van Riel | a79311c | 2009-01-06 14:40:01 -0800 | [diff] [blame] | 2766 | } while (priority >= 0 && sc.nr_reclaimed < nr_pages); |
Christoph Lameter | 0ff3849 | 2006-09-25 23:31:52 -0700 | [diff] [blame] | 2767 | } |
Christoph Lameter | c84db23 | 2006-02-01 03:05:29 -0800 | [diff] [blame] | 2768 | |
KOSAKI Motohiro | 1574804 | 2010-08-09 17:19:50 -0700 | [diff] [blame] | 2769 | nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); |
| 2770 | if (nr_slab_pages0 > zone->min_slab_pages) { |
Christoph Lameter | 2a16e3f | 2006-02-01 03:05:35 -0800 | [diff] [blame] | 2771 | /* |
Christoph Lameter | 7fb2d46 | 2006-03-22 00:08:22 -0800 | [diff] [blame] | 2772 | * shrink_slab() does not currently allow us to determine how |
Christoph Lameter | 0ff3849 | 2006-09-25 23:31:52 -0700 | [diff] [blame] | 2773 | * many pages were freed in this zone. So we take the current |
| 2774 | * number of slab pages and shake the slab until it is reduced |
| 2775 | * by the same nr_pages that we used for reclaiming unmapped |
| 2776 | * pages. |
Christoph Lameter | 2a16e3f | 2006-02-01 03:05:35 -0800 | [diff] [blame] | 2777 | * |
Christoph Lameter | 0ff3849 | 2006-09-25 23:31:52 -0700 | [diff] [blame] | 2778 | * Note that shrink_slab will free memory on all zones and may |
| 2779 | * take a long time. |
Christoph Lameter | 2a16e3f | 2006-02-01 03:05:35 -0800 | [diff] [blame] | 2780 | */ |
KOSAKI Motohiro | 4dc4b3d | 2010-08-09 17:19:54 -0700 | [diff] [blame] | 2781 | for (;;) { |
| 2782 | unsigned long lru_pages = zone_reclaimable_pages(zone); |
| 2783 | |
| 2784 | /* No reclaimable slab or very low memory pressure */ |
| 2785 | if (!shrink_slab(sc.nr_scanned, gfp_mask, lru_pages)) |
| 2786 | break; |
| 2787 | |
| 2788 | /* Freed enough memory */ |
| 2789 | nr_slab_pages1 = zone_page_state(zone, |
| 2790 | NR_SLAB_RECLAIMABLE); |
| 2791 | if (nr_slab_pages1 + nr_pages <= nr_slab_pages0) |
| 2792 | break; |
| 2793 | } |
Christoph Lameter | 83e33a4 | 2006-09-25 23:31:53 -0700 | [diff] [blame] | 2794 | |
| 2795 | /* |
| 2796 | * Update nr_reclaimed by the number of slab pages we |
| 2797 | * reclaimed from this zone. |
| 2798 | */ |
KOSAKI Motohiro | 1574804 | 2010-08-09 17:19:50 -0700 | [diff] [blame] | 2799 | nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); |
| 2800 | if (nr_slab_pages1 < nr_slab_pages0) |
| 2801 | sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1; |
Christoph Lameter | 2a16e3f | 2006-02-01 03:05:35 -0800 | [diff] [blame] | 2802 | } |
| 2803 | |
Christoph Lameter | 9eeff23 | 2006-01-18 17:42:31 -0800 | [diff] [blame] | 2804 | p->reclaim_state = NULL; |
Christoph Lameter | d4f7796 | 2006-02-24 13:04:22 -0800 | [diff] [blame] | 2805 | current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); |
KOSAKI Motohiro | 76ca542 | 2010-03-05 13:41:47 -0800 | [diff] [blame] | 2806 | lockdep_clear_current_reclaim_state(); |
Rik van Riel | a79311c | 2009-01-06 14:40:01 -0800 | [diff] [blame] | 2807 | return sc.nr_reclaimed >= nr_pages; |
Christoph Lameter | 9eeff23 | 2006-01-18 17:42:31 -0800 | [diff] [blame] | 2808 | } |
Andrew Morton | 179e963 | 2006-03-22 00:08:18 -0800 | [diff] [blame] | 2809 | |
| 2810 | int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) |
| 2811 | { |
Andrew Morton | 179e963 | 2006-03-22 00:08:18 -0800 | [diff] [blame] | 2812 | int node_id; |
David Rientjes | d773ed6 | 2007-10-16 23:26:01 -0700 | [diff] [blame] | 2813 | int ret; |
Andrew Morton | 179e963 | 2006-03-22 00:08:18 -0800 | [diff] [blame] | 2814 | |
| 2815 | /* |
Christoph Lameter | 0ff3849 | 2006-09-25 23:31:52 -0700 | [diff] [blame] | 2816 | * Zone reclaim reclaims unmapped file backed pages and |
| 2817 | * slab pages if we are over the defined limits. |
Christoph Lameter | 34aa133 | 2006-06-30 01:55:37 -0700 | [diff] [blame] | 2818 | * |
Christoph Lameter | 9614634 | 2006-07-03 00:24:13 -0700 | [diff] [blame] | 2819 | * A small portion of unmapped file backed pages is needed for |
| 2820 | * file I/O otherwise pages read by file I/O will be immediately |
| 2821 | * thrown out if the zone is overallocated. So we do not reclaim |
| 2822 | * if less than a specified percentage of the zone is used by |
| 2823 | * unmapped file backed pages. |
Andrew Morton | 179e963 | 2006-03-22 00:08:18 -0800 | [diff] [blame] | 2824 | */ |
Mel Gorman | 90afa5d | 2009-06-16 15:33:20 -0700 | [diff] [blame] | 2825 | if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages && |
| 2826 | zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages) |
Mel Gorman | fa5e084 | 2009-06-16 15:33:22 -0700 | [diff] [blame] | 2827 | return ZONE_RECLAIM_FULL; |
Andrew Morton | 179e963 | 2006-03-22 00:08:18 -0800 | [diff] [blame] | 2828 | |
KOSAKI Motohiro | 93e4a89 | 2010-03-05 13:41:55 -0800 | [diff] [blame] | 2829 | if (zone->all_unreclaimable) |
Mel Gorman | fa5e084 | 2009-06-16 15:33:22 -0700 | [diff] [blame] | 2830 | return ZONE_RECLAIM_FULL; |
David Rientjes | d773ed6 | 2007-10-16 23:26:01 -0700 | [diff] [blame] | 2831 | |
Andrew Morton | 179e963 | 2006-03-22 00:08:18 -0800 | [diff] [blame] | 2832 | /* |
David Rientjes | d773ed6 | 2007-10-16 23:26:01 -0700 | [diff] [blame] | 2833 | * Do not scan if the allocation should not be delayed. |
Andrew Morton | 179e963 | 2006-03-22 00:08:18 -0800 | [diff] [blame] | 2834 | */ |
David Rientjes | d773ed6 | 2007-10-16 23:26:01 -0700 | [diff] [blame] | 2835 | if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC)) |
Mel Gorman | fa5e084 | 2009-06-16 15:33:22 -0700 | [diff] [blame] | 2836 | return ZONE_RECLAIM_NOSCAN; |
Andrew Morton | 179e963 | 2006-03-22 00:08:18 -0800 | [diff] [blame] | 2837 | |
| 2838 | /* |
| 2839 | * Only run zone reclaim on the local zone or on zones that do not |
| 2840 | * have associated processors. This will favor the local processor |
| 2841 | * over remote processors and spread off node memory allocations |
| 2842 | * as wide as possible. |
| 2843 | */ |
Christoph Lameter | 89fa302 | 2006-09-25 23:31:55 -0700 | [diff] [blame] | 2844 | node_id = zone_to_nid(zone); |
Christoph Lameter | 37c0708 | 2007-10-16 01:25:36 -0700 | [diff] [blame] | 2845 | if (node_state(node_id, N_CPU) && node_id != numa_node_id()) |
Mel Gorman | fa5e084 | 2009-06-16 15:33:22 -0700 | [diff] [blame] | 2846 | return ZONE_RECLAIM_NOSCAN; |
David Rientjes | d773ed6 | 2007-10-16 23:26:01 -0700 | [diff] [blame] | 2847 | |
| 2848 | if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED)) |
Mel Gorman | fa5e084 | 2009-06-16 15:33:22 -0700 | [diff] [blame] | 2849 | return ZONE_RECLAIM_NOSCAN; |
| 2850 | |
David Rientjes | d773ed6 | 2007-10-16 23:26:01 -0700 | [diff] [blame] | 2851 | ret = __zone_reclaim(zone, gfp_mask, order); |
| 2852 | zone_clear_flag(zone, ZONE_RECLAIM_LOCKED); |
| 2853 | |
Mel Gorman | 24cf72518 | 2009-06-16 15:33:23 -0700 | [diff] [blame] | 2854 | if (!ret) |
| 2855 | count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); |
| 2856 | |
David Rientjes | d773ed6 | 2007-10-16 23:26:01 -0700 | [diff] [blame] | 2857 | return ret; |
Andrew Morton | 179e963 | 2006-03-22 00:08:18 -0800 | [diff] [blame] | 2858 | } |
Christoph Lameter | 9eeff23 | 2006-01-18 17:42:31 -0800 | [diff] [blame] | 2859 | #endif |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 2860 | |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 2861 | /* |
| 2862 | * page_evictable - test whether a page is evictable |
| 2863 | * @page: the page to test |
| 2864 | * @vma: the VMA in which the page is or will be mapped, may be NULL |
| 2865 | * |
| 2866 | * Test whether page is evictable--i.e., should be placed on active/inactive |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 2867 | * lists vs unevictable list. The vma argument is !NULL when called from the |
| 2868 | * fault path to determine how to instantate a new page. |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 2869 | * |
| 2870 | * Reasons page might not be evictable: |
Lee Schermerhorn | ba9ddf4 | 2008-10-18 20:26:42 -0700 | [diff] [blame] | 2871 | * (1) page's mapping marked unevictable |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 2872 | * (2) page is part of an mlocked VMA |
Lee Schermerhorn | ba9ddf4 | 2008-10-18 20:26:42 -0700 | [diff] [blame] | 2873 | * |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 2874 | */ |
| 2875 | int page_evictable(struct page *page, struct vm_area_struct *vma) |
| 2876 | { |
| 2877 | |
Lee Schermerhorn | ba9ddf4 | 2008-10-18 20:26:42 -0700 | [diff] [blame] | 2878 | if (mapping_unevictable(page_mapping(page))) |
| 2879 | return 0; |
| 2880 | |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 2881 | if (PageMlocked(page) || (vma && is_mlocked_vma(vma, page))) |
| 2882 | return 0; |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 2883 | |
| 2884 | return 1; |
| 2885 | } |
Lee Schermerhorn | 89e004ea | 2008-10-18 20:26:43 -0700 | [diff] [blame] | 2886 | |
| 2887 | /** |
| 2888 | * check_move_unevictable_page - check page for evictability and move to appropriate zone lru list |
| 2889 | * @page: page to check evictability and move to appropriate lru list |
| 2890 | * @zone: zone page is in |
| 2891 | * |
| 2892 | * Checks a page for evictability and moves the page to the appropriate |
| 2893 | * zone lru list. |
| 2894 | * |
| 2895 | * Restrictions: zone->lru_lock must be held, page must be on LRU and must |
| 2896 | * have PageUnevictable set. |
| 2897 | */ |
| 2898 | static void check_move_unevictable_page(struct page *page, struct zone *zone) |
| 2899 | { |
| 2900 | VM_BUG_ON(PageActive(page)); |
| 2901 | |
| 2902 | retry: |
| 2903 | ClearPageUnevictable(page); |
| 2904 | if (page_evictable(page, NULL)) { |
Johannes Weiner | 401a8e1 | 2009-09-21 17:02:58 -0700 | [diff] [blame] | 2905 | enum lru_list l = page_lru_base_type(page); |
Lee Schermerhorn | af936a1 | 2008-10-18 20:26:53 -0700 | [diff] [blame] | 2906 | |
Lee Schermerhorn | 89e004ea | 2008-10-18 20:26:43 -0700 | [diff] [blame] | 2907 | __dec_zone_state(zone, NR_UNEVICTABLE); |
| 2908 | list_move(&page->lru, &zone->lru[l].list); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2909 | mem_cgroup_move_lists(page, LRU_UNEVICTABLE, l); |
Lee Schermerhorn | 89e004ea | 2008-10-18 20:26:43 -0700 | [diff] [blame] | 2910 | __inc_zone_state(zone, NR_INACTIVE_ANON + l); |
| 2911 | __count_vm_event(UNEVICTABLE_PGRESCUED); |
| 2912 | } else { |
| 2913 | /* |
| 2914 | * rotate unevictable list |
| 2915 | */ |
| 2916 | SetPageUnevictable(page); |
| 2917 | list_move(&page->lru, &zone->lru[LRU_UNEVICTABLE].list); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2918 | mem_cgroup_rotate_lru_list(page, LRU_UNEVICTABLE); |
Lee Schermerhorn | 89e004ea | 2008-10-18 20:26:43 -0700 | [diff] [blame] | 2919 | if (page_evictable(page, NULL)) |
| 2920 | goto retry; |
| 2921 | } |
| 2922 | } |
| 2923 | |
| 2924 | /** |
| 2925 | * scan_mapping_unevictable_pages - scan an address space for evictable pages |
| 2926 | * @mapping: struct address_space to scan for evictable pages |
| 2927 | * |
| 2928 | * Scan all pages in mapping. Check unevictable pages for |
| 2929 | * evictability and move them to the appropriate zone lru list. |
| 2930 | */ |
| 2931 | void scan_mapping_unevictable_pages(struct address_space *mapping) |
| 2932 | { |
| 2933 | pgoff_t next = 0; |
| 2934 | pgoff_t end = (i_size_read(mapping->host) + PAGE_CACHE_SIZE - 1) >> |
| 2935 | PAGE_CACHE_SHIFT; |
| 2936 | struct zone *zone; |
| 2937 | struct pagevec pvec; |
| 2938 | |
| 2939 | if (mapping->nrpages == 0) |
| 2940 | return; |
| 2941 | |
| 2942 | pagevec_init(&pvec, 0); |
| 2943 | while (next < end && |
| 2944 | pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) { |
| 2945 | int i; |
| 2946 | int pg_scanned = 0; |
| 2947 | |
| 2948 | zone = NULL; |
| 2949 | |
| 2950 | for (i = 0; i < pagevec_count(&pvec); i++) { |
| 2951 | struct page *page = pvec.pages[i]; |
| 2952 | pgoff_t page_index = page->index; |
| 2953 | struct zone *pagezone = page_zone(page); |
| 2954 | |
| 2955 | pg_scanned++; |
| 2956 | if (page_index > next) |
| 2957 | next = page_index; |
| 2958 | next++; |
| 2959 | |
| 2960 | if (pagezone != zone) { |
| 2961 | if (zone) |
| 2962 | spin_unlock_irq(&zone->lru_lock); |
| 2963 | zone = pagezone; |
| 2964 | spin_lock_irq(&zone->lru_lock); |
| 2965 | } |
| 2966 | |
| 2967 | if (PageLRU(page) && PageUnevictable(page)) |
| 2968 | check_move_unevictable_page(page, zone); |
| 2969 | } |
| 2970 | if (zone) |
| 2971 | spin_unlock_irq(&zone->lru_lock); |
| 2972 | pagevec_release(&pvec); |
| 2973 | |
| 2974 | count_vm_events(UNEVICTABLE_PGSCANNED, pg_scanned); |
| 2975 | } |
| 2976 | |
| 2977 | } |
Lee Schermerhorn | af936a1 | 2008-10-18 20:26:53 -0700 | [diff] [blame] | 2978 | |
| 2979 | /** |
| 2980 | * scan_zone_unevictable_pages - check unevictable list for evictable pages |
| 2981 | * @zone - zone of which to scan the unevictable list |
| 2982 | * |
| 2983 | * Scan @zone's unevictable LRU lists to check for pages that have become |
| 2984 | * evictable. Move those that have to @zone's inactive list where they |
| 2985 | * become candidates for reclaim, unless shrink_inactive_zone() decides |
| 2986 | * to reactivate them. Pages that are still unevictable are rotated |
| 2987 | * back onto @zone's unevictable list. |
| 2988 | */ |
| 2989 | #define SCAN_UNEVICTABLE_BATCH_SIZE 16UL /* arbitrary lock hold batch size */ |
KOSAKI Motohiro | 14b90b2 | 2009-01-06 14:39:45 -0800 | [diff] [blame] | 2990 | static void scan_zone_unevictable_pages(struct zone *zone) |
Lee Schermerhorn | af936a1 | 2008-10-18 20:26:53 -0700 | [diff] [blame] | 2991 | { |
| 2992 | struct list_head *l_unevictable = &zone->lru[LRU_UNEVICTABLE].list; |
| 2993 | unsigned long scan; |
| 2994 | unsigned long nr_to_scan = zone_page_state(zone, NR_UNEVICTABLE); |
| 2995 | |
| 2996 | while (nr_to_scan > 0) { |
| 2997 | unsigned long batch_size = min(nr_to_scan, |
| 2998 | SCAN_UNEVICTABLE_BATCH_SIZE); |
| 2999 | |
| 3000 | spin_lock_irq(&zone->lru_lock); |
| 3001 | for (scan = 0; scan < batch_size; scan++) { |
| 3002 | struct page *page = lru_to_page(l_unevictable); |
| 3003 | |
| 3004 | if (!trylock_page(page)) |
| 3005 | continue; |
| 3006 | |
| 3007 | prefetchw_prev_lru_page(page, l_unevictable, flags); |
| 3008 | |
| 3009 | if (likely(PageLRU(page) && PageUnevictable(page))) |
| 3010 | check_move_unevictable_page(page, zone); |
| 3011 | |
| 3012 | unlock_page(page); |
| 3013 | } |
| 3014 | spin_unlock_irq(&zone->lru_lock); |
| 3015 | |
| 3016 | nr_to_scan -= batch_size; |
| 3017 | } |
| 3018 | } |
| 3019 | |
| 3020 | |
| 3021 | /** |
| 3022 | * scan_all_zones_unevictable_pages - scan all unevictable lists for evictable pages |
| 3023 | * |
| 3024 | * A really big hammer: scan all zones' unevictable LRU lists to check for |
| 3025 | * pages that have become evictable. Move those back to the zones' |
| 3026 | * inactive list where they become candidates for reclaim. |
| 3027 | * This occurs when, e.g., we have unswappable pages on the unevictable lists, |
| 3028 | * and we add swap to the system. As such, it runs in the context of a task |
| 3029 | * that has possibly/probably made some previously unevictable pages |
| 3030 | * evictable. |
| 3031 | */ |
KOSAKI Motohiro | ff30153 | 2009-01-06 14:39:44 -0800 | [diff] [blame] | 3032 | static void scan_all_zones_unevictable_pages(void) |
Lee Schermerhorn | af936a1 | 2008-10-18 20:26:53 -0700 | [diff] [blame] | 3033 | { |
| 3034 | struct zone *zone; |
| 3035 | |
| 3036 | for_each_zone(zone) { |
| 3037 | scan_zone_unevictable_pages(zone); |
| 3038 | } |
| 3039 | } |
| 3040 | |
| 3041 | /* |
| 3042 | * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of |
| 3043 | * all nodes' unevictable lists for evictable pages |
| 3044 | */ |
| 3045 | unsigned long scan_unevictable_pages; |
| 3046 | |
| 3047 | int scan_unevictable_handler(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 3048 | void __user *buffer, |
Lee Schermerhorn | af936a1 | 2008-10-18 20:26:53 -0700 | [diff] [blame] | 3049 | size_t *length, loff_t *ppos) |
| 3050 | { |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 3051 | proc_doulongvec_minmax(table, write, buffer, length, ppos); |
Lee Schermerhorn | af936a1 | 2008-10-18 20:26:53 -0700 | [diff] [blame] | 3052 | |
| 3053 | if (write && *(unsigned long *)table->data) |
| 3054 | scan_all_zones_unevictable_pages(); |
| 3055 | |
| 3056 | scan_unevictable_pages = 0; |
| 3057 | return 0; |
| 3058 | } |
| 3059 | |
Thadeu Lima de Souza Cascardo | e4455ab | 2010-10-26 14:21:28 -0700 | [diff] [blame] | 3060 | #ifdef CONFIG_NUMA |
Lee Schermerhorn | af936a1 | 2008-10-18 20:26:53 -0700 | [diff] [blame] | 3061 | /* |
| 3062 | * per node 'scan_unevictable_pages' attribute. On demand re-scan of |
| 3063 | * a specified node's per zone unevictable lists for evictable pages. |
| 3064 | */ |
| 3065 | |
| 3066 | static ssize_t read_scan_unevictable_node(struct sys_device *dev, |
| 3067 | struct sysdev_attribute *attr, |
| 3068 | char *buf) |
| 3069 | { |
| 3070 | return sprintf(buf, "0\n"); /* always zero; should fit... */ |
| 3071 | } |
| 3072 | |
| 3073 | static ssize_t write_scan_unevictable_node(struct sys_device *dev, |
| 3074 | struct sysdev_attribute *attr, |
| 3075 | const char *buf, size_t count) |
| 3076 | { |
| 3077 | struct zone *node_zones = NODE_DATA(dev->id)->node_zones; |
| 3078 | struct zone *zone; |
| 3079 | unsigned long res; |
| 3080 | unsigned long req = strict_strtoul(buf, 10, &res); |
| 3081 | |
| 3082 | if (!req) |
| 3083 | return 1; /* zero is no-op */ |
| 3084 | |
| 3085 | for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) { |
| 3086 | if (!populated_zone(zone)) |
| 3087 | continue; |
| 3088 | scan_zone_unevictable_pages(zone); |
| 3089 | } |
| 3090 | return 1; |
| 3091 | } |
| 3092 | |
| 3093 | |
| 3094 | static SYSDEV_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR, |
| 3095 | read_scan_unevictable_node, |
| 3096 | write_scan_unevictable_node); |
| 3097 | |
| 3098 | int scan_unevictable_register_node(struct node *node) |
| 3099 | { |
| 3100 | return sysdev_create_file(&node->sysdev, &attr_scan_unevictable_pages); |
| 3101 | } |
| 3102 | |
| 3103 | void scan_unevictable_unregister_node(struct node *node) |
| 3104 | { |
| 3105 | sysdev_remove_file(&node->sysdev, &attr_scan_unevictable_pages); |
| 3106 | } |
Thadeu Lima de Souza Cascardo | e4455ab | 2010-10-26 14:21:28 -0700 | [diff] [blame] | 3107 | #endif |