Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 2 | * mm/page-writeback.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2002, Linus Torvalds. |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 5 | * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Contains functions related to writing back dirty pages at the |
| 8 | * address_space level. |
| 9 | * |
| 10 | * 10Apr2002 akpm@zip.com.au |
| 11 | * Initial version |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/fs.h> |
| 18 | #include <linux/mm.h> |
| 19 | #include <linux/swap.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/pagemap.h> |
| 22 | #include <linux/writeback.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/backing-dev.h> |
Andrew Morton | 55e829a | 2006-12-10 02:19:27 -0800 | [diff] [blame] | 25 | #include <linux/task_io_accounting_ops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/blkdev.h> |
| 27 | #include <linux/mpage.h> |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 28 | #include <linux/rmap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/percpu.h> |
| 30 | #include <linux/notifier.h> |
| 31 | #include <linux/smp.h> |
| 32 | #include <linux/sysctl.h> |
| 33 | #include <linux/cpu.h> |
| 34 | #include <linux/syscalls.h> |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 35 | #include <linux/buffer_head.h> |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 36 | #include <linux/pagevec.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * The maximum number of pages to writeout in a single bdflush/kupdate |
Joern Engel | 1c0eeaf | 2007-10-16 23:30:44 -0700 | [diff] [blame] | 40 | * operation. We do this so we don't hold I_SYNC against an inode for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | * enormous amounts of time, which would block a userspace task which has |
| 42 | * been forced to throttle against that inode. Also, the code reevaluates |
| 43 | * the dirty each time it has written this many pages. |
| 44 | */ |
| 45 | #define MAX_WRITEBACK_PAGES 1024 |
| 46 | |
| 47 | /* |
| 48 | * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited |
| 49 | * will look to see if it needs to force writeback or throttling. |
| 50 | */ |
| 51 | static long ratelimit_pages = 32; |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | /* |
| 54 | * When balance_dirty_pages decides that the caller needs to perform some |
| 55 | * non-background writeback, this is how many pages it will attempt to write. |
| 56 | * It should be somewhat larger than RATELIMIT_PAGES to ensure that reasonably |
| 57 | * large amounts of I/O are submitted. |
| 58 | */ |
| 59 | static inline long sync_writeback_pages(void) |
| 60 | { |
| 61 | return ratelimit_pages + ratelimit_pages / 2; |
| 62 | } |
| 63 | |
| 64 | /* The following parameters are exported via /proc/sys/vm */ |
| 65 | |
| 66 | /* |
| 67 | * Start background writeback (via pdflush) at this percentage |
| 68 | */ |
Linus Torvalds | 07db59b | 2007-04-27 09:10:47 -0700 | [diff] [blame] | 69 | int dirty_background_ratio = 5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * The generator of dirty data starts writeback at this percentage |
| 73 | */ |
Linus Torvalds | 07db59b | 2007-04-27 09:10:47 -0700 | [diff] [blame] | 74 | int vm_dirty_ratio = 10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | /* |
Coywolf Qi Hunt | fd5403c | 2006-04-10 22:54:35 -0700 | [diff] [blame] | 77 | * The interval between `kupdate'-style writebacks, in jiffies |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | */ |
Bart Samwel | f6ef943 | 2006-03-24 03:15:48 -0800 | [diff] [blame] | 79 | int dirty_writeback_interval = 5 * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | /* |
Coywolf Qi Hunt | fd5403c | 2006-04-10 22:54:35 -0700 | [diff] [blame] | 82 | * The longest number of jiffies for which data is allowed to remain dirty |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | */ |
Bart Samwel | f6ef943 | 2006-03-24 03:15:48 -0800 | [diff] [blame] | 84 | int dirty_expire_interval = 30 * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | /* |
| 87 | * Flag that makes the machine dump writes/reads and block dirtyings. |
| 88 | */ |
| 89 | int block_dump; |
| 90 | |
| 91 | /* |
Bart Samwel | ed5b43f | 2006-03-24 03:15:49 -0800 | [diff] [blame] | 92 | * Flag that puts the machine in "laptop mode". Doubles as a timeout in jiffies: |
| 93 | * a full sync is triggered after this time elapses without any disk activity. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | */ |
| 95 | int laptop_mode; |
| 96 | |
| 97 | EXPORT_SYMBOL(laptop_mode); |
| 98 | |
| 99 | /* End of sysctl-exported parameters */ |
| 100 | |
| 101 | |
| 102 | static void background_writeout(unsigned long _min_pages); |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | /* |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 105 | * Scale the writeback cache size proportional to the relative writeout speeds. |
| 106 | * |
| 107 | * We do this by keeping a floating proportion between BDIs, based on page |
| 108 | * writeback completions [end_page_writeback()]. Those devices that write out |
| 109 | * pages fastest will get the larger share, while the slower will get a smaller |
| 110 | * share. |
| 111 | * |
| 112 | * We use page writeout completions because we are interested in getting rid of |
| 113 | * dirty pages. Having them written out is the primary goal. |
| 114 | * |
| 115 | * We introduce a concept of time, a period over which we measure these events, |
| 116 | * because demand can/will vary over time. The length of this period itself is |
| 117 | * measured in page writeback completions. |
| 118 | * |
| 119 | */ |
| 120 | static struct prop_descriptor vm_completions; |
Peter Zijlstra | 3e26c14 | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 121 | static struct prop_descriptor vm_dirties; |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 122 | |
| 123 | static unsigned long determine_dirtyable_memory(void); |
| 124 | |
| 125 | /* |
| 126 | * couple the period to the dirty_ratio: |
| 127 | * |
| 128 | * period/2 ~ roundup_pow_of_two(dirty limit) |
| 129 | */ |
| 130 | static int calc_period_shift(void) |
| 131 | { |
| 132 | unsigned long dirty_total; |
| 133 | |
| 134 | dirty_total = (vm_dirty_ratio * determine_dirtyable_memory()) / 100; |
| 135 | return 2 + ilog2(dirty_total - 1); |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * update the period when the dirty ratio changes. |
| 140 | */ |
| 141 | int dirty_ratio_handler(struct ctl_table *table, int write, |
| 142 | struct file *filp, void __user *buffer, size_t *lenp, |
| 143 | loff_t *ppos) |
| 144 | { |
| 145 | int old_ratio = vm_dirty_ratio; |
| 146 | int ret = proc_dointvec_minmax(table, write, filp, buffer, lenp, ppos); |
| 147 | if (ret == 0 && write && vm_dirty_ratio != old_ratio) { |
| 148 | int shift = calc_period_shift(); |
| 149 | prop_change_shift(&vm_completions, shift); |
Peter Zijlstra | 3e26c14 | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 150 | prop_change_shift(&vm_dirties, shift); |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 151 | } |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Increment the BDI's writeout completion count and the global writeout |
| 157 | * completion count. Called from test_clear_page_writeback(). |
| 158 | */ |
| 159 | static inline void __bdi_writeout_inc(struct backing_dev_info *bdi) |
| 160 | { |
| 161 | __prop_inc_percpu(&vm_completions, &bdi->completions); |
| 162 | } |
| 163 | |
Peter Zijlstra | 3e26c14 | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 164 | static inline void task_dirty_inc(struct task_struct *tsk) |
| 165 | { |
| 166 | prop_inc_single(&vm_dirties, &tsk->dirties); |
| 167 | } |
| 168 | |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 169 | /* |
| 170 | * Obtain an accurate fraction of the BDI's portion. |
| 171 | */ |
| 172 | static void bdi_writeout_fraction(struct backing_dev_info *bdi, |
| 173 | long *numerator, long *denominator) |
| 174 | { |
| 175 | if (bdi_cap_writeback_dirty(bdi)) { |
| 176 | prop_fraction_percpu(&vm_completions, &bdi->completions, |
| 177 | numerator, denominator); |
| 178 | } else { |
| 179 | *numerator = 0; |
| 180 | *denominator = 1; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Clip the earned share of dirty pages to that which is actually available. |
| 186 | * This avoids exceeding the total dirty_limit when the floating averages |
| 187 | * fluctuate too quickly. |
| 188 | */ |
| 189 | static void |
| 190 | clip_bdi_dirty_limit(struct backing_dev_info *bdi, long dirty, long *pbdi_dirty) |
| 191 | { |
| 192 | long avail_dirty; |
| 193 | |
| 194 | avail_dirty = dirty - |
| 195 | (global_page_state(NR_FILE_DIRTY) + |
| 196 | global_page_state(NR_WRITEBACK) + |
| 197 | global_page_state(NR_UNSTABLE_NFS)); |
| 198 | |
| 199 | if (avail_dirty < 0) |
| 200 | avail_dirty = 0; |
| 201 | |
| 202 | avail_dirty += bdi_stat(bdi, BDI_RECLAIMABLE) + |
| 203 | bdi_stat(bdi, BDI_WRITEBACK); |
| 204 | |
| 205 | *pbdi_dirty = min(*pbdi_dirty, avail_dirty); |
| 206 | } |
| 207 | |
Peter Zijlstra | 3e26c14 | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 208 | static inline void task_dirties_fraction(struct task_struct *tsk, |
| 209 | long *numerator, long *denominator) |
| 210 | { |
| 211 | prop_fraction_single(&vm_dirties, &tsk->dirties, |
| 212 | numerator, denominator); |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * scale the dirty limit |
| 217 | * |
| 218 | * task specific dirty limit: |
| 219 | * |
| 220 | * dirty -= (dirty/8) * p_{t} |
| 221 | */ |
| 222 | void task_dirty_limit(struct task_struct *tsk, long *pdirty) |
| 223 | { |
| 224 | long numerator, denominator; |
| 225 | long dirty = *pdirty; |
| 226 | u64 inv = dirty >> 3; |
| 227 | |
| 228 | task_dirties_fraction(tsk, &numerator, &denominator); |
| 229 | inv *= numerator; |
| 230 | do_div(inv, denominator); |
| 231 | |
| 232 | dirty -= inv; |
| 233 | if (dirty < *pdirty/2) |
| 234 | dirty = *pdirty/2; |
| 235 | |
| 236 | *pdirty = dirty; |
| 237 | } |
| 238 | |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 239 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | * Work out the current dirty-memory clamping and background writeout |
| 241 | * thresholds. |
| 242 | * |
| 243 | * The main aim here is to lower them aggressively if there is a lot of mapped |
| 244 | * memory around. To avoid stressing page reclaim with lots of unreclaimable |
| 245 | * pages. It is better to clamp down on writers than to start swapping, and |
| 246 | * performing lots of scanning. |
| 247 | * |
| 248 | * We only allow 1/2 of the currently-unmapped memory to be dirtied. |
| 249 | * |
| 250 | * We don't permit the clamping level to fall below 5% - that is getting rather |
| 251 | * excessive. |
| 252 | * |
| 253 | * We make sure that the background writeout level is below the adjusted |
| 254 | * clamping level. |
| 255 | */ |
Christoph Lameter | 1b42446 | 2007-05-06 14:48:59 -0700 | [diff] [blame] | 256 | |
| 257 | static unsigned long highmem_dirtyable_memory(unsigned long total) |
| 258 | { |
| 259 | #ifdef CONFIG_HIGHMEM |
| 260 | int node; |
| 261 | unsigned long x = 0; |
| 262 | |
Lee Schermerhorn | 37b07e4 | 2007-10-16 01:25:39 -0700 | [diff] [blame] | 263 | for_each_node_state(node, N_HIGH_MEMORY) { |
Christoph Lameter | 1b42446 | 2007-05-06 14:48:59 -0700 | [diff] [blame] | 264 | struct zone *z = |
| 265 | &NODE_DATA(node)->node_zones[ZONE_HIGHMEM]; |
| 266 | |
| 267 | x += zone_page_state(z, NR_FREE_PAGES) |
| 268 | + zone_page_state(z, NR_INACTIVE) |
| 269 | + zone_page_state(z, NR_ACTIVE); |
| 270 | } |
| 271 | /* |
| 272 | * Make sure that the number of highmem pages is never larger |
| 273 | * than the number of the total dirtyable memory. This can only |
| 274 | * occur in very strange VM situations but we want to make sure |
| 275 | * that this does not occur. |
| 276 | */ |
| 277 | return min(x, total); |
| 278 | #else |
| 279 | return 0; |
| 280 | #endif |
| 281 | } |
| 282 | |
| 283 | static unsigned long determine_dirtyable_memory(void) |
| 284 | { |
| 285 | unsigned long x; |
| 286 | |
| 287 | x = global_page_state(NR_FREE_PAGES) |
| 288 | + global_page_state(NR_INACTIVE) |
| 289 | + global_page_state(NR_ACTIVE); |
| 290 | x -= highmem_dirtyable_memory(x); |
| 291 | return x + 1; /* Ensure that we never return 0 */ |
| 292 | } |
| 293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | static void |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 295 | get_dirty_limits(long *pbackground, long *pdirty, long *pbdi_dirty, |
| 296 | struct backing_dev_info *bdi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | { |
| 298 | int background_ratio; /* Percentages */ |
| 299 | int dirty_ratio; |
| 300 | int unmapped_ratio; |
| 301 | long background; |
| 302 | long dirty; |
Christoph Lameter | 1b42446 | 2007-05-06 14:48:59 -0700 | [diff] [blame] | 303 | unsigned long available_memory = determine_dirtyable_memory(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | struct task_struct *tsk; |
| 305 | |
Christoph Lameter | c24f21b | 2006-06-30 01:55:42 -0700 | [diff] [blame] | 306 | unmapped_ratio = 100 - ((global_page_state(NR_FILE_MAPPED) + |
| 307 | global_page_state(NR_ANON_PAGES)) * 100) / |
Christoph Lameter | 1b42446 | 2007-05-06 14:48:59 -0700 | [diff] [blame] | 308 | available_memory; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
| 310 | dirty_ratio = vm_dirty_ratio; |
| 311 | if (dirty_ratio > unmapped_ratio / 2) |
| 312 | dirty_ratio = unmapped_ratio / 2; |
| 313 | |
| 314 | if (dirty_ratio < 5) |
| 315 | dirty_ratio = 5; |
| 316 | |
| 317 | background_ratio = dirty_background_ratio; |
| 318 | if (background_ratio >= dirty_ratio) |
| 319 | background_ratio = dirty_ratio / 2; |
| 320 | |
| 321 | background = (background_ratio * available_memory) / 100; |
| 322 | dirty = (dirty_ratio * available_memory) / 100; |
| 323 | tsk = current; |
| 324 | if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) { |
| 325 | background += background / 4; |
| 326 | dirty += dirty / 4; |
| 327 | } |
| 328 | *pbackground = background; |
| 329 | *pdirty = dirty; |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 330 | |
| 331 | if (bdi) { |
| 332 | u64 bdi_dirty = dirty; |
| 333 | long numerator, denominator; |
| 334 | |
| 335 | /* |
| 336 | * Calculate this BDI's share of the dirty ratio. |
| 337 | */ |
| 338 | bdi_writeout_fraction(bdi, &numerator, &denominator); |
| 339 | |
| 340 | bdi_dirty *= numerator; |
| 341 | do_div(bdi_dirty, denominator); |
| 342 | |
| 343 | *pbdi_dirty = bdi_dirty; |
| 344 | clip_bdi_dirty_limit(bdi, dirty, pbdi_dirty); |
Peter Zijlstra | 3e26c14 | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 345 | task_dirty_limit(current, pbdi_dirty); |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 346 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* |
| 350 | * balance_dirty_pages() must be called by processes which are generating dirty |
| 351 | * data. It looks at the number of dirty pages in the machine and will force |
| 352 | * the caller to perform writeback if the system is over `vm_dirty_ratio'. |
| 353 | * If we're over `background_thresh' then pdflush is woken to perform some |
| 354 | * writeout. |
| 355 | */ |
| 356 | static void balance_dirty_pages(struct address_space *mapping) |
| 357 | { |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 358 | long bdi_nr_reclaimable; |
| 359 | long bdi_nr_writeback; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | long background_thresh; |
| 361 | long dirty_thresh; |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 362 | long bdi_thresh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | unsigned long pages_written = 0; |
| 364 | unsigned long write_chunk = sync_writeback_pages(); |
| 365 | |
| 366 | struct backing_dev_info *bdi = mapping->backing_dev_info; |
| 367 | |
| 368 | for (;;) { |
| 369 | struct writeback_control wbc = { |
| 370 | .bdi = bdi, |
| 371 | .sync_mode = WB_SYNC_NONE, |
| 372 | .older_than_this = NULL, |
| 373 | .nr_to_write = write_chunk, |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 374 | .range_cyclic = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | }; |
| 376 | |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 377 | get_dirty_limits(&background_thresh, &dirty_thresh, |
| 378 | &bdi_thresh, bdi); |
| 379 | bdi_nr_reclaimable = bdi_stat(bdi, BDI_RECLAIMABLE); |
| 380 | bdi_nr_writeback = bdi_stat(bdi, BDI_WRITEBACK); |
| 381 | if (bdi_nr_reclaimable + bdi_nr_writeback <= bdi_thresh) |
| 382 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 384 | if (!bdi->dirty_exceeded) |
| 385 | bdi->dirty_exceeded = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
| 387 | /* Note: nr_reclaimable denotes nr_dirty + nr_unstable. |
| 388 | * Unstable writes are a feature of certain networked |
| 389 | * filesystems (i.e. NFS) in which data may have been |
| 390 | * written to the server's write cache, but has not yet |
| 391 | * been flushed to permanent storage. |
| 392 | */ |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 393 | if (bdi_nr_reclaimable) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | writeback_inodes(&wbc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | pages_written += write_chunk - wbc.nr_to_write; |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 396 | get_dirty_limits(&background_thresh, &dirty_thresh, |
| 397 | &bdi_thresh, bdi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 399 | |
| 400 | /* |
| 401 | * In order to avoid the stacked BDI deadlock we need |
| 402 | * to ensure we accurately count the 'dirty' pages when |
| 403 | * the threshold is low. |
| 404 | * |
| 405 | * Otherwise it would be possible to get thresh+n pages |
| 406 | * reported dirty, even though there are thresh-m pages |
| 407 | * actually dirty; with m+n sitting in the percpu |
| 408 | * deltas. |
| 409 | */ |
| 410 | if (bdi_thresh < 2*bdi_stat_error(bdi)) { |
| 411 | bdi_nr_reclaimable = bdi_stat_sum(bdi, BDI_RECLAIMABLE); |
| 412 | bdi_nr_writeback = bdi_stat_sum(bdi, BDI_WRITEBACK); |
| 413 | } else if (bdi_nr_reclaimable) { |
| 414 | bdi_nr_reclaimable = bdi_stat(bdi, BDI_RECLAIMABLE); |
| 415 | bdi_nr_writeback = bdi_stat(bdi, BDI_WRITEBACK); |
| 416 | } |
| 417 | |
| 418 | if (bdi_nr_reclaimable + bdi_nr_writeback <= bdi_thresh) |
| 419 | break; |
| 420 | if (pages_written >= write_chunk) |
| 421 | break; /* We've done our duty */ |
| 422 | |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 423 | congestion_wait(WRITE, HZ/10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 426 | if (bdi_nr_reclaimable + bdi_nr_writeback < bdi_thresh && |
| 427 | bdi->dirty_exceeded) |
| 428 | bdi->dirty_exceeded = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | if (writeback_in_progress(bdi)) |
| 431 | return; /* pdflush is already working this queue */ |
| 432 | |
| 433 | /* |
| 434 | * In laptop mode, we wait until hitting the higher threshold before |
| 435 | * starting background writeout, and then write out all the way down |
| 436 | * to the lower threshold. So slow writers cause minimal disk activity. |
| 437 | * |
| 438 | * In normal mode, we start background writeout at the lower |
| 439 | * background_thresh, to keep the amount of dirty memory low. |
| 440 | */ |
| 441 | if ((laptop_mode && pages_written) || |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 442 | (!laptop_mode && (global_page_state(NR_FILE_DIRTY) |
| 443 | + global_page_state(NR_UNSTABLE_NFS) |
| 444 | > background_thresh))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | pdflush_operation(background_writeout, 0); |
| 446 | } |
| 447 | |
Peter Zijlstra | a200ee1 | 2007-10-08 18:54:37 +0200 | [diff] [blame] | 448 | void set_page_dirty_balance(struct page *page, int page_mkwrite) |
Peter Zijlstra | edc79b2 | 2006-09-25 23:30:58 -0700 | [diff] [blame] | 449 | { |
Peter Zijlstra | a200ee1 | 2007-10-08 18:54:37 +0200 | [diff] [blame] | 450 | if (set_page_dirty(page) || page_mkwrite) { |
Peter Zijlstra | edc79b2 | 2006-09-25 23:30:58 -0700 | [diff] [blame] | 451 | struct address_space *mapping = page_mapping(page); |
| 452 | |
| 453 | if (mapping) |
| 454 | balance_dirty_pages_ratelimited(mapping); |
| 455 | } |
| 456 | } |
| 457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | /** |
Andrew Morton | fa5a734 | 2006-03-24 03:18:10 -0800 | [diff] [blame] | 459 | * balance_dirty_pages_ratelimited_nr - balance dirty memory state |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 460 | * @mapping: address_space which was dirtied |
Martin Waitz | a580290 | 2006-04-02 13:59:55 +0200 | [diff] [blame] | 461 | * @nr_pages_dirtied: number of pages which the caller has just dirtied |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | * |
| 463 | * Processes which are dirtying memory should call in here once for each page |
| 464 | * which was newly dirtied. The function will periodically check the system's |
| 465 | * dirty state and will initiate writeback if needed. |
| 466 | * |
| 467 | * On really big machines, get_writeback_state is expensive, so try to avoid |
| 468 | * calling it too often (ratelimiting). But once we're over the dirty memory |
| 469 | * limit we decrease the ratelimiting by a lot, to prevent individual processes |
| 470 | * from overshooting the limit by (ratelimit_pages) each. |
| 471 | */ |
Andrew Morton | fa5a734 | 2006-03-24 03:18:10 -0800 | [diff] [blame] | 472 | void balance_dirty_pages_ratelimited_nr(struct address_space *mapping, |
| 473 | unsigned long nr_pages_dirtied) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | { |
Andrew Morton | fa5a734 | 2006-03-24 03:18:10 -0800 | [diff] [blame] | 475 | static DEFINE_PER_CPU(unsigned long, ratelimits) = 0; |
| 476 | unsigned long ratelimit; |
| 477 | unsigned long *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
| 479 | ratelimit = ratelimit_pages; |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 480 | if (mapping->backing_dev_info->dirty_exceeded) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | ratelimit = 8; |
| 482 | |
| 483 | /* |
| 484 | * Check the rate limiting. Also, we do not want to throttle real-time |
| 485 | * tasks in balance_dirty_pages(). Period. |
| 486 | */ |
Andrew Morton | fa5a734 | 2006-03-24 03:18:10 -0800 | [diff] [blame] | 487 | preempt_disable(); |
| 488 | p = &__get_cpu_var(ratelimits); |
| 489 | *p += nr_pages_dirtied; |
| 490 | if (unlikely(*p >= ratelimit)) { |
| 491 | *p = 0; |
| 492 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | balance_dirty_pages(mapping); |
| 494 | return; |
| 495 | } |
Andrew Morton | fa5a734 | 2006-03-24 03:18:10 -0800 | [diff] [blame] | 496 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } |
Andrew Morton | fa5a734 | 2006-03-24 03:18:10 -0800 | [diff] [blame] | 498 | EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
Andrew Morton | 232ea4d | 2007-02-28 20:13:21 -0800 | [diff] [blame] | 500 | void throttle_vm_writeout(gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | long background_thresh; |
| 503 | long dirty_thresh; |
| 504 | |
| 505 | for ( ; ; ) { |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 506 | get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
| 508 | /* |
| 509 | * Boost the allowable dirty threshold a bit for page |
| 510 | * allocators so they don't get DoS'ed by heavy writers |
| 511 | */ |
| 512 | dirty_thresh += dirty_thresh / 10; /* wheeee... */ |
| 513 | |
Christoph Lameter | c24f21b | 2006-06-30 01:55:42 -0700 | [diff] [blame] | 514 | if (global_page_state(NR_UNSTABLE_NFS) + |
| 515 | global_page_state(NR_WRITEBACK) <= dirty_thresh) |
| 516 | break; |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 517 | congestion_wait(WRITE, HZ/10); |
Fengguang Wu | 369f238 | 2007-10-16 23:30:45 -0700 | [diff] [blame] | 518 | |
| 519 | /* |
| 520 | * The caller might hold locks which can prevent IO completion |
| 521 | * or progress in the filesystem. So we cannot just sit here |
| 522 | * waiting for IO to complete. |
| 523 | */ |
| 524 | if ((gfp_mask & (__GFP_FS|__GFP_IO)) != (__GFP_FS|__GFP_IO)) |
| 525 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | /* |
| 530 | * writeback at least _min_pages, and keep writing until the amount of dirty |
| 531 | * memory is less than the background threshold, or until we're all clean. |
| 532 | */ |
| 533 | static void background_writeout(unsigned long _min_pages) |
| 534 | { |
| 535 | long min_pages = _min_pages; |
| 536 | struct writeback_control wbc = { |
| 537 | .bdi = NULL, |
| 538 | .sync_mode = WB_SYNC_NONE, |
| 539 | .older_than_this = NULL, |
| 540 | .nr_to_write = 0, |
| 541 | .nonblocking = 1, |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 542 | .range_cyclic = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | }; |
| 544 | |
| 545 | for ( ; ; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | long background_thresh; |
| 547 | long dirty_thresh; |
| 548 | |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 549 | get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL); |
Christoph Lameter | c24f21b | 2006-06-30 01:55:42 -0700 | [diff] [blame] | 550 | if (global_page_state(NR_FILE_DIRTY) + |
| 551 | global_page_state(NR_UNSTABLE_NFS) < background_thresh |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | && min_pages <= 0) |
| 553 | break; |
Fengguang Wu | 2e6883b | 2007-10-16 23:30:43 -0700 | [diff] [blame] | 554 | wbc.more_io = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | wbc.encountered_congestion = 0; |
| 556 | wbc.nr_to_write = MAX_WRITEBACK_PAGES; |
| 557 | wbc.pages_skipped = 0; |
| 558 | writeback_inodes(&wbc); |
| 559 | min_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write; |
| 560 | if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) { |
| 561 | /* Wrote less than expected */ |
Fengguang Wu | 2e6883b | 2007-10-16 23:30:43 -0700 | [diff] [blame] | 562 | if (wbc.encountered_congestion || wbc.more_io) |
| 563 | congestion_wait(WRITE, HZ/10); |
| 564 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | break; |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back |
| 572 | * the whole world. Returns 0 if a pdflush thread was dispatched. Returns |
| 573 | * -1 if all pdflush threads were busy. |
| 574 | */ |
Pekka J Enberg | 687a21c | 2005-06-28 20:44:55 -0700 | [diff] [blame] | 575 | int wakeup_pdflush(long nr_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | { |
Christoph Lameter | c24f21b | 2006-06-30 01:55:42 -0700 | [diff] [blame] | 577 | if (nr_pages == 0) |
| 578 | nr_pages = global_page_state(NR_FILE_DIRTY) + |
| 579 | global_page_state(NR_UNSTABLE_NFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | return pdflush_operation(background_writeout, nr_pages); |
| 581 | } |
| 582 | |
| 583 | static void wb_timer_fn(unsigned long unused); |
| 584 | static void laptop_timer_fn(unsigned long unused); |
| 585 | |
Ingo Molnar | 8d06afa | 2005-09-09 13:10:40 -0700 | [diff] [blame] | 586 | static DEFINE_TIMER(wb_timer, wb_timer_fn, 0, 0); |
| 587 | static DEFINE_TIMER(laptop_mode_wb_timer, laptop_timer_fn, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
| 589 | /* |
| 590 | * Periodic writeback of "old" data. |
| 591 | * |
| 592 | * Define "old": the first time one of an inode's pages is dirtied, we mark the |
| 593 | * dirtying-time in the inode's address_space. So this periodic writeback code |
| 594 | * just walks the superblock inode list, writing back any inodes which are |
| 595 | * older than a specific point in time. |
| 596 | * |
Bart Samwel | f6ef943 | 2006-03-24 03:15:48 -0800 | [diff] [blame] | 597 | * Try to run once per dirty_writeback_interval. But if a writeback event |
| 598 | * takes longer than a dirty_writeback_interval interval, then leave a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | * one-second gap. |
| 600 | * |
| 601 | * older_than_this takes precedence over nr_to_write. So we'll only write back |
| 602 | * all dirty pages if they are all attached to "old" mappings. |
| 603 | */ |
| 604 | static void wb_kupdate(unsigned long arg) |
| 605 | { |
| 606 | unsigned long oldest_jif; |
| 607 | unsigned long start_jif; |
| 608 | unsigned long next_jif; |
| 609 | long nr_to_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | struct writeback_control wbc = { |
| 611 | .bdi = NULL, |
| 612 | .sync_mode = WB_SYNC_NONE, |
| 613 | .older_than_this = &oldest_jif, |
| 614 | .nr_to_write = 0, |
| 615 | .nonblocking = 1, |
| 616 | .for_kupdate = 1, |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 617 | .range_cyclic = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | }; |
| 619 | |
| 620 | sync_supers(); |
| 621 | |
Bart Samwel | f6ef943 | 2006-03-24 03:15:48 -0800 | [diff] [blame] | 622 | oldest_jif = jiffies - dirty_expire_interval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | start_jif = jiffies; |
Bart Samwel | f6ef943 | 2006-03-24 03:15:48 -0800 | [diff] [blame] | 624 | next_jif = start_jif + dirty_writeback_interval; |
Christoph Lameter | c24f21b | 2006-06-30 01:55:42 -0700 | [diff] [blame] | 625 | nr_to_write = global_page_state(NR_FILE_DIRTY) + |
| 626 | global_page_state(NR_UNSTABLE_NFS) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | (inodes_stat.nr_inodes - inodes_stat.nr_unused); |
| 628 | while (nr_to_write > 0) { |
Fengguang Wu | 2e6883b | 2007-10-16 23:30:43 -0700 | [diff] [blame] | 629 | wbc.more_io = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | wbc.encountered_congestion = 0; |
| 631 | wbc.nr_to_write = MAX_WRITEBACK_PAGES; |
| 632 | writeback_inodes(&wbc); |
| 633 | if (wbc.nr_to_write > 0) { |
Fengguang Wu | 2e6883b | 2007-10-16 23:30:43 -0700 | [diff] [blame] | 634 | if (wbc.encountered_congestion || wbc.more_io) |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 635 | congestion_wait(WRITE, HZ/10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | else |
| 637 | break; /* All the old data is written */ |
| 638 | } |
| 639 | nr_to_write -= MAX_WRITEBACK_PAGES - wbc.nr_to_write; |
| 640 | } |
| 641 | if (time_before(next_jif, jiffies + HZ)) |
| 642 | next_jif = jiffies + HZ; |
Bart Samwel | f6ef943 | 2006-03-24 03:15:48 -0800 | [diff] [blame] | 643 | if (dirty_writeback_interval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | mod_timer(&wb_timer, next_jif); |
| 645 | } |
| 646 | |
| 647 | /* |
| 648 | * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs |
| 649 | */ |
| 650 | int dirty_writeback_centisecs_handler(ctl_table *table, int write, |
Andrew Morton | 3e733f0 | 2007-07-15 23:41:05 -0700 | [diff] [blame] | 651 | struct file *file, void __user *buffer, size_t *length, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | { |
Bart Samwel | f6ef943 | 2006-03-24 03:15:48 -0800 | [diff] [blame] | 653 | proc_dointvec_userhz_jiffies(table, write, file, buffer, length, ppos); |
Andrew Morton | 3e733f0 | 2007-07-15 23:41:05 -0700 | [diff] [blame] | 654 | if (dirty_writeback_interval) |
| 655 | mod_timer(&wb_timer, jiffies + dirty_writeback_interval); |
| 656 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | del_timer(&wb_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | static void wb_timer_fn(unsigned long unused) |
| 662 | { |
| 663 | if (pdflush_operation(wb_kupdate, 0) < 0) |
| 664 | mod_timer(&wb_timer, jiffies + HZ); /* delay 1 second */ |
| 665 | } |
| 666 | |
| 667 | static void laptop_flush(unsigned long unused) |
| 668 | { |
| 669 | sys_sync(); |
| 670 | } |
| 671 | |
| 672 | static void laptop_timer_fn(unsigned long unused) |
| 673 | { |
| 674 | pdflush_operation(laptop_flush, 0); |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | * We've spun up the disk and we're in laptop mode: schedule writeback |
| 679 | * of all dirty data a few seconds from now. If the flush is already scheduled |
| 680 | * then push it back - the user is still using the disk. |
| 681 | */ |
| 682 | void laptop_io_completion(void) |
| 683 | { |
Bart Samwel | ed5b43f | 2006-03-24 03:15:49 -0800 | [diff] [blame] | 684 | mod_timer(&laptop_mode_wb_timer, jiffies + laptop_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /* |
| 688 | * We're in laptop mode and we've just synced. The sync's writes will have |
| 689 | * caused another writeback to be scheduled by laptop_io_completion. |
| 690 | * Nothing needs to be written back anymore, so we unschedule the writeback. |
| 691 | */ |
| 692 | void laptop_sync_completion(void) |
| 693 | { |
| 694 | del_timer(&laptop_mode_wb_timer); |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * If ratelimit_pages is too high then we can get into dirty-data overload |
| 699 | * if a large number of processes all perform writes at the same time. |
| 700 | * If it is too low then SMP machines will call the (expensive) |
| 701 | * get_writeback_state too often. |
| 702 | * |
| 703 | * Here we set ratelimit_pages to a level which ensures that when all CPUs are |
| 704 | * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory |
| 705 | * thresholds before writeback cuts in. |
| 706 | * |
| 707 | * But the limit should not be set too high. Because it also controls the |
| 708 | * amount of memory which the balance_dirty_pages() caller has to write back. |
| 709 | * If this is too large then the caller will block on the IO queue all the |
| 710 | * time. So limit it to four megabytes - the balance_dirty_pages() caller |
| 711 | * will write six megabyte chunks, max. |
| 712 | */ |
| 713 | |
Chandra Seetharaman | 2d1d43f | 2006-09-29 02:01:25 -0700 | [diff] [blame] | 714 | void writeback_set_ratelimit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | { |
Chandra Seetharaman | 40c99aa | 2006-09-29 02:01:24 -0700 | [diff] [blame] | 716 | ratelimit_pages = vm_total_pages / (num_online_cpus() * 32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | if (ratelimit_pages < 16) |
| 718 | ratelimit_pages = 16; |
| 719 | if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024) |
| 720 | ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE; |
| 721 | } |
| 722 | |
Chandra Seetharaman | 26c2143 | 2006-06-27 02:54:10 -0700 | [diff] [blame] | 723 | static int __cpuinit |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | ratelimit_handler(struct notifier_block *self, unsigned long u, void *v) |
| 725 | { |
Chandra Seetharaman | 2d1d43f | 2006-09-29 02:01:25 -0700 | [diff] [blame] | 726 | writeback_set_ratelimit(); |
Paul E. McKenney | aa0f030 | 2007-02-10 01:46:37 -0800 | [diff] [blame] | 727 | return NOTIFY_DONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Chandra Seetharaman | 74b85f3 | 2006-06-27 02:54:09 -0700 | [diff] [blame] | 730 | static struct notifier_block __cpuinitdata ratelimit_nb = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | .notifier_call = ratelimit_handler, |
| 732 | .next = NULL, |
| 733 | }; |
| 734 | |
| 735 | /* |
Linus Torvalds | dc6e29d | 2007-01-29 16:37:38 -0800 | [diff] [blame] | 736 | * Called early on to tune the page writeback dirty limits. |
| 737 | * |
| 738 | * We used to scale dirty pages according to how total memory |
| 739 | * related to pages that could be allocated for buffers (by |
| 740 | * comparing nr_free_buffer_pages() to vm_total_pages. |
| 741 | * |
| 742 | * However, that was when we used "dirty_ratio" to scale with |
| 743 | * all memory, and we don't do that any more. "dirty_ratio" |
| 744 | * is now applied to total non-HIGHPAGE memory (by subtracting |
| 745 | * totalhigh_pages from vm_total_pages), and as such we can't |
| 746 | * get into the old insane situation any more where we had |
| 747 | * large amounts of dirty pages compared to a small amount of |
| 748 | * non-HIGHMEM memory. |
| 749 | * |
| 750 | * But we might still want to scale the dirty_ratio by how |
| 751 | * much memory the box has.. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | */ |
| 753 | void __init page_writeback_init(void) |
| 754 | { |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 755 | int shift; |
| 756 | |
Bart Samwel | f6ef943 | 2006-03-24 03:15:48 -0800 | [diff] [blame] | 757 | mod_timer(&wb_timer, jiffies + dirty_writeback_interval); |
Chandra Seetharaman | 2d1d43f | 2006-09-29 02:01:25 -0700 | [diff] [blame] | 758 | writeback_set_ratelimit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | register_cpu_notifier(&ratelimit_nb); |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 760 | |
| 761 | shift = calc_period_shift(); |
| 762 | prop_descriptor_init(&vm_completions, shift); |
Peter Zijlstra | 3e26c14 | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 763 | prop_descriptor_init(&vm_dirties, shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | } |
| 765 | |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 766 | /** |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 767 | * write_cache_pages - walk the list of dirty pages of the given address space and write all of them. |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 768 | * @mapping: address space structure to write |
| 769 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 770 | * @writepage: function called for each page |
| 771 | * @data: data passed to writepage function |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 772 | * |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 773 | * If a page is already under I/O, write_cache_pages() skips it, even |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 774 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, |
| 775 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() |
| 776 | * and msync() need to guarantee that all the data which was dirty at the time |
| 777 | * the call was made get new I/O started against them. If wbc->sync_mode is |
| 778 | * WB_SYNC_ALL then we were called for data integrity and we must wait for |
| 779 | * existing IO to complete. |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 780 | */ |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 781 | int write_cache_pages(struct address_space *mapping, |
| 782 | struct writeback_control *wbc, writepage_t writepage, |
| 783 | void *data) |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 784 | { |
| 785 | struct backing_dev_info *bdi = mapping->backing_dev_info; |
| 786 | int ret = 0; |
| 787 | int done = 0; |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 788 | struct pagevec pvec; |
| 789 | int nr_pages; |
| 790 | pgoff_t index; |
| 791 | pgoff_t end; /* Inclusive */ |
| 792 | int scanned = 0; |
| 793 | int range_whole = 0; |
| 794 | |
| 795 | if (wbc->nonblocking && bdi_write_congested(bdi)) { |
| 796 | wbc->encountered_congestion = 1; |
| 797 | return 0; |
| 798 | } |
| 799 | |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 800 | pagevec_init(&pvec, 0); |
| 801 | if (wbc->range_cyclic) { |
| 802 | index = mapping->writeback_index; /* Start from prev offset */ |
| 803 | end = -1; |
| 804 | } else { |
| 805 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 806 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
| 807 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 808 | range_whole = 1; |
| 809 | scanned = 1; |
| 810 | } |
| 811 | retry: |
| 812 | while (!done && (index <= end) && |
| 813 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, |
| 814 | PAGECACHE_TAG_DIRTY, |
| 815 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
| 816 | unsigned i; |
| 817 | |
| 818 | scanned = 1; |
| 819 | for (i = 0; i < nr_pages; i++) { |
| 820 | struct page *page = pvec.pages[i]; |
| 821 | |
| 822 | /* |
| 823 | * At this point we hold neither mapping->tree_lock nor |
| 824 | * lock on the page itself: the page may be truncated or |
| 825 | * invalidated (changing page->mapping to NULL), or even |
| 826 | * swizzled back from swapper_space to tmpfs file |
| 827 | * mapping |
| 828 | */ |
| 829 | lock_page(page); |
| 830 | |
| 831 | if (unlikely(page->mapping != mapping)) { |
| 832 | unlock_page(page); |
| 833 | continue; |
| 834 | } |
| 835 | |
| 836 | if (!wbc->range_cyclic && page->index > end) { |
| 837 | done = 1; |
| 838 | unlock_page(page); |
| 839 | continue; |
| 840 | } |
| 841 | |
| 842 | if (wbc->sync_mode != WB_SYNC_NONE) |
| 843 | wait_on_page_writeback(page); |
| 844 | |
| 845 | if (PageWriteback(page) || |
| 846 | !clear_page_dirty_for_io(page)) { |
| 847 | unlock_page(page); |
| 848 | continue; |
| 849 | } |
| 850 | |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 851 | ret = (*writepage)(page, wbc, data); |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 852 | |
Andrew Morton | e423003 | 2007-10-16 23:26:02 -0700 | [diff] [blame] | 853 | if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) { |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 854 | unlock_page(page); |
Andrew Morton | e423003 | 2007-10-16 23:26:02 -0700 | [diff] [blame] | 855 | ret = 0; |
| 856 | } |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 857 | if (ret || (--(wbc->nr_to_write) <= 0)) |
| 858 | done = 1; |
| 859 | if (wbc->nonblocking && bdi_write_congested(bdi)) { |
| 860 | wbc->encountered_congestion = 1; |
| 861 | done = 1; |
| 862 | } |
| 863 | } |
| 864 | pagevec_release(&pvec); |
| 865 | cond_resched(); |
| 866 | } |
| 867 | if (!scanned && !done) { |
| 868 | /* |
| 869 | * We hit the last page and there is more work to be done: wrap |
| 870 | * back to the start of the file |
| 871 | */ |
| 872 | scanned = 1; |
| 873 | index = 0; |
| 874 | goto retry; |
| 875 | } |
| 876 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
| 877 | mapping->writeback_index = index; |
| 878 | return ret; |
| 879 | } |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 880 | EXPORT_SYMBOL(write_cache_pages); |
| 881 | |
| 882 | /* |
| 883 | * Function used by generic_writepages to call the real writepage |
| 884 | * function and set the mapping flags on error |
| 885 | */ |
| 886 | static int __writepage(struct page *page, struct writeback_control *wbc, |
| 887 | void *data) |
| 888 | { |
| 889 | struct address_space *mapping = data; |
| 890 | int ret = mapping->a_ops->writepage(page, wbc); |
| 891 | mapping_set_error(mapping, ret); |
| 892 | return ret; |
| 893 | } |
| 894 | |
| 895 | /** |
| 896 | * generic_writepages - walk the list of dirty pages of the given address space and writepage() all of them. |
| 897 | * @mapping: address space structure to write |
| 898 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
| 899 | * |
| 900 | * This is a library function, which implements the writepages() |
| 901 | * address_space_operation. |
| 902 | */ |
| 903 | int generic_writepages(struct address_space *mapping, |
| 904 | struct writeback_control *wbc) |
| 905 | { |
| 906 | /* deal with chardevs and other special file */ |
| 907 | if (!mapping->a_ops->writepage) |
| 908 | return 0; |
| 909 | |
| 910 | return write_cache_pages(mapping, wbc, __writepage, mapping); |
| 911 | } |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 912 | |
| 913 | EXPORT_SYMBOL(generic_writepages); |
| 914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | int do_writepages(struct address_space *mapping, struct writeback_control *wbc) |
| 916 | { |
Andrew Morton | 22905f7 | 2005-11-16 15:07:01 -0800 | [diff] [blame] | 917 | int ret; |
| 918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | if (wbc->nr_to_write <= 0) |
| 920 | return 0; |
Andrew Morton | 22905f7 | 2005-11-16 15:07:01 -0800 | [diff] [blame] | 921 | wbc->for_writepages = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | if (mapping->a_ops->writepages) |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 923 | ret = mapping->a_ops->writepages(mapping, wbc); |
Andrew Morton | 22905f7 | 2005-11-16 15:07:01 -0800 | [diff] [blame] | 924 | else |
| 925 | ret = generic_writepages(mapping, wbc); |
| 926 | wbc->for_writepages = 0; |
| 927 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | /** |
| 931 | * write_one_page - write out a single page and optionally wait on I/O |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 932 | * @page: the page to write |
| 933 | * @wait: if true, wait on writeout |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | * |
| 935 | * The page must be locked by the caller and will be unlocked upon return. |
| 936 | * |
| 937 | * write_one_page() returns a negative error code if I/O failed. |
| 938 | */ |
| 939 | int write_one_page(struct page *page, int wait) |
| 940 | { |
| 941 | struct address_space *mapping = page->mapping; |
| 942 | int ret = 0; |
| 943 | struct writeback_control wbc = { |
| 944 | .sync_mode = WB_SYNC_ALL, |
| 945 | .nr_to_write = 1, |
| 946 | }; |
| 947 | |
| 948 | BUG_ON(!PageLocked(page)); |
| 949 | |
| 950 | if (wait) |
| 951 | wait_on_page_writeback(page); |
| 952 | |
| 953 | if (clear_page_dirty_for_io(page)) { |
| 954 | page_cache_get(page); |
| 955 | ret = mapping->a_ops->writepage(page, &wbc); |
| 956 | if (ret == 0 && wait) { |
| 957 | wait_on_page_writeback(page); |
| 958 | if (PageError(page)) |
| 959 | ret = -EIO; |
| 960 | } |
| 961 | page_cache_release(page); |
| 962 | } else { |
| 963 | unlock_page(page); |
| 964 | } |
| 965 | return ret; |
| 966 | } |
| 967 | EXPORT_SYMBOL(write_one_page); |
| 968 | |
| 969 | /* |
Ken Chen | 7671932 | 2007-02-10 01:43:15 -0800 | [diff] [blame] | 970 | * For address_spaces which do not use buffers nor write back. |
| 971 | */ |
| 972 | int __set_page_dirty_no_writeback(struct page *page) |
| 973 | { |
| 974 | if (!PageDirty(page)) |
| 975 | SetPageDirty(page); |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | * For address_spaces which do not use buffers. Just tag the page as dirty in |
| 981 | * its radix tree. |
| 982 | * |
| 983 | * This is also used when a single buffer is being dirtied: we want to set the |
| 984 | * page dirty in that case, but not all the buffers. This is a "bottom-up" |
| 985 | * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying. |
| 986 | * |
| 987 | * Most callers have locked the page, which pins the address_space in memory. |
| 988 | * But zap_pte_range() does not lock the page, however in that case the |
| 989 | * mapping is pinned by the vma's ->vm_file reference. |
| 990 | * |
| 991 | * We take care to handle the case where the page was truncated from the |
| 992 | * mapping by re-checking page_mapping() insode tree_lock. |
| 993 | */ |
| 994 | int __set_page_dirty_nobuffers(struct page *page) |
| 995 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | if (!TestSetPageDirty(page)) { |
| 997 | struct address_space *mapping = page_mapping(page); |
| 998 | struct address_space *mapping2; |
| 999 | |
Andrew Morton | 8c08540 | 2006-12-10 02:19:24 -0800 | [diff] [blame] | 1000 | if (!mapping) |
| 1001 | return 1; |
| 1002 | |
| 1003 | write_lock_irq(&mapping->tree_lock); |
| 1004 | mapping2 = page_mapping(page); |
| 1005 | if (mapping2) { /* Race with truncate? */ |
| 1006 | BUG_ON(mapping2 != mapping); |
Nick Piggin | 787d221 | 2007-07-17 04:03:34 -0700 | [diff] [blame] | 1007 | WARN_ON_ONCE(!PagePrivate(page) && !PageUptodate(page)); |
Andrew Morton | 55e829a | 2006-12-10 02:19:27 -0800 | [diff] [blame] | 1008 | if (mapping_cap_account_dirty(mapping)) { |
Andrew Morton | 8c08540 | 2006-12-10 02:19:24 -0800 | [diff] [blame] | 1009 | __inc_zone_page_state(page, NR_FILE_DIRTY); |
Peter Zijlstra | c9e51e4 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 1010 | __inc_bdi_stat(mapping->backing_dev_info, |
| 1011 | BDI_RECLAIMABLE); |
Andrew Morton | 55e829a | 2006-12-10 02:19:27 -0800 | [diff] [blame] | 1012 | task_io_account_write(PAGE_CACHE_SIZE); |
| 1013 | } |
Andrew Morton | 8c08540 | 2006-12-10 02:19:24 -0800 | [diff] [blame] | 1014 | radix_tree_tag_set(&mapping->page_tree, |
| 1015 | page_index(page), PAGECACHE_TAG_DIRTY); |
| 1016 | } |
| 1017 | write_unlock_irq(&mapping->tree_lock); |
| 1018 | if (mapping->host) { |
| 1019 | /* !PageAnon && !swapper_space */ |
| 1020 | __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | } |
Andrew Morton | 4741c9f | 2006-03-24 03:18:11 -0800 | [diff] [blame] | 1022 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | } |
Andrew Morton | 4741c9f | 2006-03-24 03:18:11 -0800 | [diff] [blame] | 1024 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | } |
| 1026 | EXPORT_SYMBOL(__set_page_dirty_nobuffers); |
| 1027 | |
| 1028 | /* |
| 1029 | * When a writepage implementation decides that it doesn't want to write this |
| 1030 | * page for some reason, it should redirty the locked page via |
| 1031 | * redirty_page_for_writepage() and it should then unlock the page and return 0 |
| 1032 | */ |
| 1033 | int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page) |
| 1034 | { |
| 1035 | wbc->pages_skipped++; |
| 1036 | return __set_page_dirty_nobuffers(page); |
| 1037 | } |
| 1038 | EXPORT_SYMBOL(redirty_page_for_writepage); |
| 1039 | |
| 1040 | /* |
| 1041 | * If the mapping doesn't provide a set_page_dirty a_op, then |
| 1042 | * just fall through and assume that it wants buffer_heads. |
| 1043 | */ |
Peter Zijlstra | 3e26c14 | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 1044 | static int __set_page_dirty(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | { |
| 1046 | struct address_space *mapping = page_mapping(page); |
| 1047 | |
| 1048 | if (likely(mapping)) { |
| 1049 | int (*spd)(struct page *) = mapping->a_ops->set_page_dirty; |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 1050 | #ifdef CONFIG_BLOCK |
| 1051 | if (!spd) |
| 1052 | spd = __set_page_dirty_buffers; |
| 1053 | #endif |
| 1054 | return (*spd)(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | } |
Andrew Morton | 4741c9f | 2006-03-24 03:18:11 -0800 | [diff] [blame] | 1056 | if (!PageDirty(page)) { |
| 1057 | if (!TestSetPageDirty(page)) |
| 1058 | return 1; |
| 1059 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | return 0; |
| 1061 | } |
Peter Zijlstra | 3e26c14 | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 1062 | |
| 1063 | int fastcall set_page_dirty(struct page *page) |
| 1064 | { |
| 1065 | int ret = __set_page_dirty(page); |
| 1066 | if (ret) |
| 1067 | task_dirty_inc(current); |
| 1068 | return ret; |
| 1069 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | EXPORT_SYMBOL(set_page_dirty); |
| 1071 | |
| 1072 | /* |
| 1073 | * set_page_dirty() is racy if the caller has no reference against |
| 1074 | * page->mapping->host, and if the page is unlocked. This is because another |
| 1075 | * CPU could truncate the page off the mapping and then free the mapping. |
| 1076 | * |
| 1077 | * Usually, the page _is_ locked, or the caller is a user-space process which |
| 1078 | * holds a reference on the inode by having an open file. |
| 1079 | * |
| 1080 | * In other cases, the page should be locked before running set_page_dirty(). |
| 1081 | */ |
| 1082 | int set_page_dirty_lock(struct page *page) |
| 1083 | { |
| 1084 | int ret; |
| 1085 | |
Nick Piggin | db37648 | 2006-09-25 23:31:24 -0700 | [diff] [blame] | 1086 | lock_page_nosync(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | ret = set_page_dirty(page); |
| 1088 | unlock_page(page); |
| 1089 | return ret; |
| 1090 | } |
| 1091 | EXPORT_SYMBOL(set_page_dirty_lock); |
| 1092 | |
| 1093 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | * Clear a page's dirty flag, while caring for dirty memory accounting. |
| 1095 | * Returns true if the page was previously dirty. |
| 1096 | * |
| 1097 | * This is for preparing to put the page under writeout. We leave the page |
| 1098 | * tagged as dirty in the radix tree so that a concurrent write-for-sync |
| 1099 | * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage |
| 1100 | * implementation will run either set_page_writeback() or set_page_dirty(), |
| 1101 | * at which stage we bring the page's dirty flag and radix-tree dirty tag |
| 1102 | * back into sync. |
| 1103 | * |
| 1104 | * This incoherency between the page's dirty flag and radix-tree tag is |
| 1105 | * unfortunate, but it only exists while the page is locked. |
| 1106 | */ |
| 1107 | int clear_page_dirty_for_io(struct page *page) |
| 1108 | { |
| 1109 | struct address_space *mapping = page_mapping(page); |
| 1110 | |
Nick Piggin | 7935289 | 2007-07-19 01:47:22 -0700 | [diff] [blame] | 1111 | BUG_ON(!PageLocked(page)); |
| 1112 | |
Fengguang Wu | fe3cba1 | 2007-07-19 01:48:07 -0700 | [diff] [blame] | 1113 | ClearPageReclaim(page); |
Linus Torvalds | 7658cc2 | 2006-12-29 10:00:58 -0800 | [diff] [blame] | 1114 | if (mapping && mapping_cap_account_dirty(mapping)) { |
| 1115 | /* |
| 1116 | * Yes, Virginia, this is indeed insane. |
| 1117 | * |
| 1118 | * We use this sequence to make sure that |
| 1119 | * (a) we account for dirty stats properly |
| 1120 | * (b) we tell the low-level filesystem to |
| 1121 | * mark the whole page dirty if it was |
| 1122 | * dirty in a pagetable. Only to then |
| 1123 | * (c) clean the page again and return 1 to |
| 1124 | * cause the writeback. |
| 1125 | * |
| 1126 | * This way we avoid all nasty races with the |
| 1127 | * dirty bit in multiple places and clearing |
| 1128 | * them concurrently from different threads. |
| 1129 | * |
| 1130 | * Note! Normally the "set_page_dirty(page)" |
| 1131 | * has no effect on the actual dirty bit - since |
| 1132 | * that will already usually be set. But we |
| 1133 | * need the side effects, and it can help us |
| 1134 | * avoid races. |
| 1135 | * |
| 1136 | * We basically use the page "master dirty bit" |
| 1137 | * as a serialization point for all the different |
| 1138 | * threads doing their things. |
Linus Torvalds | 7658cc2 | 2006-12-29 10:00:58 -0800 | [diff] [blame] | 1139 | */ |
| 1140 | if (page_mkclean(page)) |
| 1141 | set_page_dirty(page); |
Nick Piggin | 7935289 | 2007-07-19 01:47:22 -0700 | [diff] [blame] | 1142 | /* |
| 1143 | * We carefully synchronise fault handlers against |
| 1144 | * installing a dirty pte and marking the page dirty |
| 1145 | * at this point. We do this by having them hold the |
| 1146 | * page lock at some point after installing their |
| 1147 | * pte, but before marking the page dirty. |
| 1148 | * Pages are always locked coming in here, so we get |
| 1149 | * the desired exclusion. See mm/memory.c:do_wp_page() |
| 1150 | * for more comments. |
| 1151 | */ |
Linus Torvalds | 7658cc2 | 2006-12-29 10:00:58 -0800 | [diff] [blame] | 1152 | if (TestClearPageDirty(page)) { |
Andrew Morton | 8c08540 | 2006-12-10 02:19:24 -0800 | [diff] [blame] | 1153 | dec_zone_page_state(page, NR_FILE_DIRTY); |
Peter Zijlstra | c9e51e4 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 1154 | dec_bdi_stat(mapping->backing_dev_info, |
| 1155 | BDI_RECLAIMABLE); |
Linus Torvalds | 7658cc2 | 2006-12-29 10:00:58 -0800 | [diff] [blame] | 1156 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | } |
Linus Torvalds | 7658cc2 | 2006-12-29 10:00:58 -0800 | [diff] [blame] | 1158 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | } |
Linus Torvalds | 7658cc2 | 2006-12-29 10:00:58 -0800 | [diff] [blame] | 1160 | return TestClearPageDirty(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | } |
Hans Reiser | 58bb01a | 2005-11-18 01:10:53 -0800 | [diff] [blame] | 1162 | EXPORT_SYMBOL(clear_page_dirty_for_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | |
| 1164 | int test_clear_page_writeback(struct page *page) |
| 1165 | { |
| 1166 | struct address_space *mapping = page_mapping(page); |
| 1167 | int ret; |
| 1168 | |
| 1169 | if (mapping) { |
Peter Zijlstra | 69cb51d | 2007-10-16 23:25:48 -0700 | [diff] [blame] | 1170 | struct backing_dev_info *bdi = mapping->backing_dev_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | unsigned long flags; |
| 1172 | |
| 1173 | write_lock_irqsave(&mapping->tree_lock, flags); |
| 1174 | ret = TestClearPageWriteback(page); |
Peter Zijlstra | 69cb51d | 2007-10-16 23:25:48 -0700 | [diff] [blame] | 1175 | if (ret) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | radix_tree_tag_clear(&mapping->page_tree, |
| 1177 | page_index(page), |
| 1178 | PAGECACHE_TAG_WRITEBACK); |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 1179 | if (bdi_cap_writeback_dirty(bdi)) { |
Peter Zijlstra | 69cb51d | 2007-10-16 23:25:48 -0700 | [diff] [blame] | 1180 | __dec_bdi_stat(bdi, BDI_WRITEBACK); |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 1181 | __bdi_writeout_inc(bdi); |
| 1182 | } |
Peter Zijlstra | 69cb51d | 2007-10-16 23:25:48 -0700 | [diff] [blame] | 1183 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | write_unlock_irqrestore(&mapping->tree_lock, flags); |
| 1185 | } else { |
| 1186 | ret = TestClearPageWriteback(page); |
| 1187 | } |
Andrew Morton | d688abf | 2007-07-19 01:49:17 -0700 | [diff] [blame] | 1188 | if (ret) |
| 1189 | dec_zone_page_state(page, NR_WRITEBACK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | return ret; |
| 1191 | } |
| 1192 | |
| 1193 | int test_set_page_writeback(struct page *page) |
| 1194 | { |
| 1195 | struct address_space *mapping = page_mapping(page); |
| 1196 | int ret; |
| 1197 | |
| 1198 | if (mapping) { |
Peter Zijlstra | 69cb51d | 2007-10-16 23:25:48 -0700 | [diff] [blame] | 1199 | struct backing_dev_info *bdi = mapping->backing_dev_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | unsigned long flags; |
| 1201 | |
| 1202 | write_lock_irqsave(&mapping->tree_lock, flags); |
| 1203 | ret = TestSetPageWriteback(page); |
Peter Zijlstra | 69cb51d | 2007-10-16 23:25:48 -0700 | [diff] [blame] | 1204 | if (!ret) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | radix_tree_tag_set(&mapping->page_tree, |
| 1206 | page_index(page), |
| 1207 | PAGECACHE_TAG_WRITEBACK); |
Peter Zijlstra | 69cb51d | 2007-10-16 23:25:48 -0700 | [diff] [blame] | 1208 | if (bdi_cap_writeback_dirty(bdi)) |
| 1209 | __inc_bdi_stat(bdi, BDI_WRITEBACK); |
| 1210 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | if (!PageDirty(page)) |
| 1212 | radix_tree_tag_clear(&mapping->page_tree, |
| 1213 | page_index(page), |
| 1214 | PAGECACHE_TAG_DIRTY); |
| 1215 | write_unlock_irqrestore(&mapping->tree_lock, flags); |
| 1216 | } else { |
| 1217 | ret = TestSetPageWriteback(page); |
| 1218 | } |
Andrew Morton | d688abf | 2007-07-19 01:49:17 -0700 | [diff] [blame] | 1219 | if (!ret) |
| 1220 | inc_zone_page_state(page, NR_WRITEBACK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | return ret; |
| 1222 | |
| 1223 | } |
| 1224 | EXPORT_SYMBOL(test_set_page_writeback); |
| 1225 | |
| 1226 | /* |
Nick Piggin | 0012818 | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 1227 | * Return true if any of the pages in the mapping are marked with the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | * passed tag. |
| 1229 | */ |
| 1230 | int mapping_tagged(struct address_space *mapping, int tag) |
| 1231 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | int ret; |
Nick Piggin | 0012818 | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 1233 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | ret = radix_tree_tagged(&mapping->page_tree, tag); |
Nick Piggin | 0012818 | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 1235 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | return ret; |
| 1237 | } |
| 1238 | EXPORT_SYMBOL(mapping_tagged); |