ia64/linux-2.6.18-xen.hg

annotate mm/page-writeback.c @ 749:2892ca2b9c17

linux/x86: cleanup IO-APIC code

- get 32-bit code in sync with 64-bit wrt ExtINT pin detection being
unnecessary
- eliminate build warnings resulting from c/s 725

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Nov 28 13:31:21 2008 +0000 (2008-11-28)
parents 831230e53067
children
rev   line source
ian@0 1 /*
ian@0 2 * mm/page-writeback.c.
ian@0 3 *
ian@0 4 * Copyright (C) 2002, Linus Torvalds.
ian@0 5 *
ian@0 6 * Contains functions related to writing back dirty pages at the
ian@0 7 * address_space level.
ian@0 8 *
ian@0 9 * 10Apr2002 akpm@zip.com.au
ian@0 10 * Initial version
ian@0 11 */
ian@0 12
ian@0 13 #include <linux/kernel.h>
ian@0 14 #include <linux/module.h>
ian@0 15 #include <linux/spinlock.h>
ian@0 16 #include <linux/fs.h>
ian@0 17 #include <linux/mm.h>
ian@0 18 #include <linux/swap.h>
ian@0 19 #include <linux/slab.h>
ian@0 20 #include <linux/pagemap.h>
ian@0 21 #include <linux/writeback.h>
ian@0 22 #include <linux/init.h>
ian@0 23 #include <linux/backing-dev.h>
ian@0 24 #include <linux/blkdev.h>
ian@0 25 #include <linux/mpage.h>
ian@0 26 #include <linux/percpu.h>
ian@0 27 #include <linux/notifier.h>
ian@0 28 #include <linux/smp.h>
ian@0 29 #include <linux/sysctl.h>
ian@0 30 #include <linux/cpu.h>
ian@0 31 #include <linux/syscalls.h>
ian@0 32
ian@0 33 /*
ian@0 34 * The maximum number of pages to writeout in a single bdflush/kupdate
ian@0 35 * operation. We do this so we don't hold I_LOCK against an inode for
ian@0 36 * enormous amounts of time, which would block a userspace task which has
ian@0 37 * been forced to throttle against that inode. Also, the code reevaluates
ian@0 38 * the dirty each time it has written this many pages.
ian@0 39 */
ian@0 40 #define MAX_WRITEBACK_PAGES 1024
ian@0 41
ian@0 42 /*
ian@0 43 * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited
ian@0 44 * will look to see if it needs to force writeback or throttling.
ian@0 45 */
ian@0 46 static long ratelimit_pages = 32;
ian@0 47
ian@0 48 static long total_pages; /* The total number of pages in the machine. */
ian@0 49 static int dirty_exceeded __cacheline_aligned_in_smp; /* Dirty mem may be over limit */
ian@0 50
ian@0 51 /*
ian@0 52 * When balance_dirty_pages decides that the caller needs to perform some
ian@0 53 * non-background writeback, this is how many pages it will attempt to write.
ian@0 54 * It should be somewhat larger than RATELIMIT_PAGES to ensure that reasonably
ian@0 55 * large amounts of I/O are submitted.
ian@0 56 */
ian@0 57 static inline long sync_writeback_pages(void)
ian@0 58 {
ian@0 59 return ratelimit_pages + ratelimit_pages / 2;
ian@0 60 }
ian@0 61
ian@0 62 /* The following parameters are exported via /proc/sys/vm */
ian@0 63
ian@0 64 /*
ian@0 65 * Start background writeback (via pdflush) at this percentage
ian@0 66 */
ian@0 67 int dirty_background_ratio = 10;
ian@0 68
ian@0 69 /*
ian@0 70 * The generator of dirty data starts writeback at this percentage
ian@0 71 */
ian@0 72 int vm_dirty_ratio = 40;
ian@0 73
ian@0 74 /*
ian@0 75 * The interval between `kupdate'-style writebacks, in jiffies
ian@0 76 */
ian@0 77 int dirty_writeback_interval = 5 * HZ;
ian@0 78
ian@0 79 /*
ian@0 80 * The longest number of jiffies for which data is allowed to remain dirty
ian@0 81 */
ian@0 82 int dirty_expire_interval = 30 * HZ;
ian@0 83
ian@0 84 /*
ian@0 85 * Flag that makes the machine dump writes/reads and block dirtyings.
ian@0 86 */
ian@0 87 int block_dump;
ian@0 88
ian@0 89 /*
ian@0 90 * Flag that puts the machine in "laptop mode". Doubles as a timeout in jiffies:
ian@0 91 * a full sync is triggered after this time elapses without any disk activity.
ian@0 92 */
ian@0 93 int laptop_mode;
ian@0 94
ian@0 95 EXPORT_SYMBOL(laptop_mode);
ian@0 96
ian@0 97 /* End of sysctl-exported parameters */
ian@0 98
ian@0 99
ian@0 100 static void background_writeout(unsigned long _min_pages);
ian@0 101
ian@0 102 /*
ian@0 103 * Work out the current dirty-memory clamping and background writeout
ian@0 104 * thresholds.
ian@0 105 *
ian@0 106 * The main aim here is to lower them aggressively if there is a lot of mapped
ian@0 107 * memory around. To avoid stressing page reclaim with lots of unreclaimable
ian@0 108 * pages. It is better to clamp down on writers than to start swapping, and
ian@0 109 * performing lots of scanning.
ian@0 110 *
ian@0 111 * We only allow 1/2 of the currently-unmapped memory to be dirtied.
ian@0 112 *
ian@0 113 * We don't permit the clamping level to fall below 5% - that is getting rather
ian@0 114 * excessive.
ian@0 115 *
ian@0 116 * We make sure that the background writeout level is below the adjusted
ian@0 117 * clamping level.
ian@0 118 */
ian@0 119 static void
ian@0 120 get_dirty_limits(long *pbackground, long *pdirty,
ian@0 121 struct address_space *mapping)
ian@0 122 {
ian@0 123 int background_ratio; /* Percentages */
ian@0 124 int dirty_ratio;
ian@0 125 int unmapped_ratio;
ian@0 126 long background;
ian@0 127 long dirty;
ian@0 128 unsigned long available_memory = total_pages;
ian@0 129 struct task_struct *tsk;
ian@0 130
ian@0 131 #ifdef CONFIG_HIGHMEM
ian@0 132 /*
ian@0 133 * If this mapping can only allocate from low memory,
ian@0 134 * we exclude high memory from our count.
ian@0 135 */
ian@0 136 if (mapping && !(mapping_gfp_mask(mapping) & __GFP_HIGHMEM))
ian@0 137 available_memory -= totalhigh_pages;
ian@0 138 #endif
ian@0 139
ian@0 140
ian@0 141 unmapped_ratio = 100 - ((global_page_state(NR_FILE_MAPPED) +
ian@0 142 global_page_state(NR_ANON_PAGES)) * 100) /
ian@0 143 total_pages;
ian@0 144
ian@0 145 dirty_ratio = vm_dirty_ratio;
ian@0 146 if (dirty_ratio > unmapped_ratio / 2)
ian@0 147 dirty_ratio = unmapped_ratio / 2;
ian@0 148
ian@0 149 if (dirty_ratio < 5)
ian@0 150 dirty_ratio = 5;
ian@0 151
ian@0 152 background_ratio = dirty_background_ratio;
ian@0 153 if (background_ratio >= dirty_ratio)
ian@0 154 background_ratio = dirty_ratio / 2;
ian@0 155
ian@0 156 background = (background_ratio * available_memory) / 100;
ian@0 157 dirty = (dirty_ratio * available_memory) / 100;
ian@0 158 tsk = current;
ian@0 159 if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) {
ian@0 160 background += background / 4;
ian@0 161 dirty += dirty / 4;
ian@0 162 }
ian@0 163 *pbackground = background;
ian@0 164 *pdirty = dirty;
ian@0 165 }
ian@0 166
ian@0 167 /*
ian@0 168 * balance_dirty_pages() must be called by processes which are generating dirty
ian@0 169 * data. It looks at the number of dirty pages in the machine and will force
ian@0 170 * the caller to perform writeback if the system is over `vm_dirty_ratio'.
ian@0 171 * If we're over `background_thresh' then pdflush is woken to perform some
ian@0 172 * writeout.
ian@0 173 */
ian@0 174 static void balance_dirty_pages(struct address_space *mapping)
ian@0 175 {
ian@0 176 long nr_reclaimable;
ian@0 177 long background_thresh;
ian@0 178 long dirty_thresh;
ian@0 179 unsigned long pages_written = 0;
ian@0 180 unsigned long write_chunk = sync_writeback_pages();
ian@0 181
ian@0 182 struct backing_dev_info *bdi = mapping->backing_dev_info;
ian@0 183
ian@0 184 for (;;) {
ian@0 185 struct writeback_control wbc = {
ian@0 186 .bdi = bdi,
ian@0 187 .sync_mode = WB_SYNC_NONE,
ian@0 188 .older_than_this = NULL,
ian@0 189 .nr_to_write = write_chunk,
ian@0 190 .range_cyclic = 1,
ian@0 191 };
ian@0 192
ian@0 193 get_dirty_limits(&background_thresh, &dirty_thresh, mapping);
ian@0 194 nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
ian@0 195 global_page_state(NR_UNSTABLE_NFS);
ian@0 196 if (nr_reclaimable + global_page_state(NR_WRITEBACK) <=
ian@0 197 dirty_thresh)
ian@0 198 break;
ian@0 199
ian@0 200 if (!dirty_exceeded)
ian@0 201 dirty_exceeded = 1;
ian@0 202
ian@0 203 /* Note: nr_reclaimable denotes nr_dirty + nr_unstable.
ian@0 204 * Unstable writes are a feature of certain networked
ian@0 205 * filesystems (i.e. NFS) in which data may have been
ian@0 206 * written to the server's write cache, but has not yet
ian@0 207 * been flushed to permanent storage.
ian@0 208 */
ian@0 209 if (nr_reclaimable) {
ian@0 210 writeback_inodes(&wbc);
ian@0 211 get_dirty_limits(&background_thresh,
ian@0 212 &dirty_thresh, mapping);
ian@0 213 nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
ian@0 214 global_page_state(NR_UNSTABLE_NFS);
ian@0 215 if (nr_reclaimable +
ian@0 216 global_page_state(NR_WRITEBACK)
ian@0 217 <= dirty_thresh)
ian@0 218 break;
ian@0 219 pages_written += write_chunk - wbc.nr_to_write;
ian@0 220 if (pages_written >= write_chunk)
ian@0 221 break; /* We've done our duty */
ian@0 222 }
ian@0 223 blk_congestion_wait(WRITE, HZ/10);
ian@0 224 }
ian@0 225
ian@0 226 if (nr_reclaimable + global_page_state(NR_WRITEBACK)
ian@0 227 <= dirty_thresh && dirty_exceeded)
ian@0 228 dirty_exceeded = 0;
ian@0 229
ian@0 230 if (writeback_in_progress(bdi))
ian@0 231 return; /* pdflush is already working this queue */
ian@0 232
ian@0 233 /*
ian@0 234 * In laptop mode, we wait until hitting the higher threshold before
ian@0 235 * starting background writeout, and then write out all the way down
ian@0 236 * to the lower threshold. So slow writers cause minimal disk activity.
ian@0 237 *
ian@0 238 * In normal mode, we start background writeout at the lower
ian@0 239 * background_thresh, to keep the amount of dirty memory low.
ian@0 240 */
ian@0 241 if ((laptop_mode && pages_written) ||
ian@0 242 (!laptop_mode && (nr_reclaimable > background_thresh)))
ian@0 243 pdflush_operation(background_writeout, 0);
ian@0 244 }
ian@0 245
ian@0 246 /**
ian@0 247 * balance_dirty_pages_ratelimited_nr - balance dirty memory state
ian@0 248 * @mapping: address_space which was dirtied
ian@0 249 * @nr_pages_dirtied: number of pages which the caller has just dirtied
ian@0 250 *
ian@0 251 * Processes which are dirtying memory should call in here once for each page
ian@0 252 * which was newly dirtied. The function will periodically check the system's
ian@0 253 * dirty state and will initiate writeback if needed.
ian@0 254 *
ian@0 255 * On really big machines, get_writeback_state is expensive, so try to avoid
ian@0 256 * calling it too often (ratelimiting). But once we're over the dirty memory
ian@0 257 * limit we decrease the ratelimiting by a lot, to prevent individual processes
ian@0 258 * from overshooting the limit by (ratelimit_pages) each.
ian@0 259 */
ian@0 260 void balance_dirty_pages_ratelimited_nr(struct address_space *mapping,
ian@0 261 unsigned long nr_pages_dirtied)
ian@0 262 {
ian@0 263 static DEFINE_PER_CPU(unsigned long, ratelimits) = 0;
ian@0 264 unsigned long ratelimit;
ian@0 265 unsigned long *p;
ian@0 266
ian@0 267 ratelimit = ratelimit_pages;
ian@0 268 if (dirty_exceeded)
ian@0 269 ratelimit = 8;
ian@0 270
ian@0 271 /*
ian@0 272 * Check the rate limiting. Also, we do not want to throttle real-time
ian@0 273 * tasks in balance_dirty_pages(). Period.
ian@0 274 */
ian@0 275 preempt_disable();
ian@0 276 p = &__get_cpu_var(ratelimits);
ian@0 277 *p += nr_pages_dirtied;
ian@0 278 if (unlikely(*p >= ratelimit)) {
ian@0 279 *p = 0;
ian@0 280 preempt_enable();
ian@0 281 balance_dirty_pages(mapping);
ian@0 282 return;
ian@0 283 }
ian@0 284 preempt_enable();
ian@0 285 }
ian@0 286 EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr);
ian@0 287
ian@0 288 void throttle_vm_writeout(void)
ian@0 289 {
ian@0 290 long background_thresh;
ian@0 291 long dirty_thresh;
ian@0 292
ian@0 293 for ( ; ; ) {
ian@0 294 get_dirty_limits(&background_thresh, &dirty_thresh, NULL);
ian@0 295
ian@0 296 /*
ian@0 297 * Boost the allowable dirty threshold a bit for page
ian@0 298 * allocators so they don't get DoS'ed by heavy writers
ian@0 299 */
ian@0 300 dirty_thresh += dirty_thresh / 10; /* wheeee... */
ian@0 301
ian@0 302 if (global_page_state(NR_UNSTABLE_NFS) +
ian@0 303 global_page_state(NR_WRITEBACK) <= dirty_thresh)
ian@0 304 break;
ian@0 305 blk_congestion_wait(WRITE, HZ/10);
ian@0 306 }
ian@0 307 }
ian@0 308
ian@0 309
ian@0 310 /*
ian@0 311 * writeback at least _min_pages, and keep writing until the amount of dirty
ian@0 312 * memory is less than the background threshold, or until we're all clean.
ian@0 313 */
ian@0 314 static void background_writeout(unsigned long _min_pages)
ian@0 315 {
ian@0 316 long min_pages = _min_pages;
ian@0 317 struct writeback_control wbc = {
ian@0 318 .bdi = NULL,
ian@0 319 .sync_mode = WB_SYNC_NONE,
ian@0 320 .older_than_this = NULL,
ian@0 321 .nr_to_write = 0,
ian@0 322 .nonblocking = 1,
ian@0 323 .range_cyclic = 1,
ian@0 324 };
ian@0 325
ian@0 326 for ( ; ; ) {
ian@0 327 long background_thresh;
ian@0 328 long dirty_thresh;
ian@0 329
ian@0 330 get_dirty_limits(&background_thresh, &dirty_thresh, NULL);
ian@0 331 if (global_page_state(NR_FILE_DIRTY) +
ian@0 332 global_page_state(NR_UNSTABLE_NFS) < background_thresh
ian@0 333 && min_pages <= 0)
ian@0 334 break;
ian@0 335 wbc.encountered_congestion = 0;
ian@0 336 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
ian@0 337 wbc.pages_skipped = 0;
ian@0 338 writeback_inodes(&wbc);
ian@0 339 min_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
ian@0 340 if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) {
ian@0 341 /* Wrote less than expected */
ian@0 342 blk_congestion_wait(WRITE, HZ/10);
ian@0 343 if (!wbc.encountered_congestion)
ian@0 344 break;
ian@0 345 }
ian@0 346 }
ian@0 347 }
ian@0 348
ian@0 349 /*
ian@0 350 * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back
ian@0 351 * the whole world. Returns 0 if a pdflush thread was dispatched. Returns
ian@0 352 * -1 if all pdflush threads were busy.
ian@0 353 */
ian@0 354 int wakeup_pdflush(long nr_pages)
ian@0 355 {
ian@0 356 if (nr_pages == 0)
ian@0 357 nr_pages = global_page_state(NR_FILE_DIRTY) +
ian@0 358 global_page_state(NR_UNSTABLE_NFS);
ian@0 359 return pdflush_operation(background_writeout, nr_pages);
ian@0 360 }
ian@0 361
ian@0 362 static void wb_timer_fn(unsigned long unused);
ian@0 363 static void laptop_timer_fn(unsigned long unused);
ian@0 364
ian@0 365 static DEFINE_TIMER(wb_timer, wb_timer_fn, 0, 0);
ian@0 366 static DEFINE_TIMER(laptop_mode_wb_timer, laptop_timer_fn, 0, 0);
ian@0 367
ian@0 368 /*
ian@0 369 * Periodic writeback of "old" data.
ian@0 370 *
ian@0 371 * Define "old": the first time one of an inode's pages is dirtied, we mark the
ian@0 372 * dirtying-time in the inode's address_space. So this periodic writeback code
ian@0 373 * just walks the superblock inode list, writing back any inodes which are
ian@0 374 * older than a specific point in time.
ian@0 375 *
ian@0 376 * Try to run once per dirty_writeback_interval. But if a writeback event
ian@0 377 * takes longer than a dirty_writeback_interval interval, then leave a
ian@0 378 * one-second gap.
ian@0 379 *
ian@0 380 * older_than_this takes precedence over nr_to_write. So we'll only write back
ian@0 381 * all dirty pages if they are all attached to "old" mappings.
ian@0 382 */
ian@0 383 static void wb_kupdate(unsigned long arg)
ian@0 384 {
ian@0 385 unsigned long oldest_jif;
ian@0 386 unsigned long start_jif;
ian@0 387 unsigned long next_jif;
ian@0 388 long nr_to_write;
ian@0 389 struct writeback_control wbc = {
ian@0 390 .bdi = NULL,
ian@0 391 .sync_mode = WB_SYNC_NONE,
ian@0 392 .older_than_this = &oldest_jif,
ian@0 393 .nr_to_write = 0,
ian@0 394 .nonblocking = 1,
ian@0 395 .for_kupdate = 1,
ian@0 396 .range_cyclic = 1,
ian@0 397 };
ian@0 398
ian@0 399 sync_supers();
ian@0 400
ian@0 401 oldest_jif = jiffies - dirty_expire_interval;
ian@0 402 start_jif = jiffies;
ian@0 403 next_jif = start_jif + dirty_writeback_interval;
ian@0 404 nr_to_write = global_page_state(NR_FILE_DIRTY) +
ian@0 405 global_page_state(NR_UNSTABLE_NFS) +
ian@0 406 (inodes_stat.nr_inodes - inodes_stat.nr_unused);
ian@0 407 while (nr_to_write > 0) {
ian@0 408 wbc.encountered_congestion = 0;
ian@0 409 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
ian@0 410 writeback_inodes(&wbc);
ian@0 411 if (wbc.nr_to_write > 0) {
ian@0 412 if (wbc.encountered_congestion)
ian@0 413 blk_congestion_wait(WRITE, HZ/10);
ian@0 414 else
ian@0 415 break; /* All the old data is written */
ian@0 416 }
ian@0 417 nr_to_write -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
ian@0 418 }
ian@0 419 if (time_before(next_jif, jiffies + HZ))
ian@0 420 next_jif = jiffies + HZ;
ian@0 421 if (dirty_writeback_interval)
ian@0 422 mod_timer(&wb_timer, next_jif);
ian@0 423 }
ian@0 424
ian@0 425 /*
ian@0 426 * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs
ian@0 427 */
ian@0 428 int dirty_writeback_centisecs_handler(ctl_table *table, int write,
ian@0 429 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
ian@0 430 {
ian@0 431 proc_dointvec_userhz_jiffies(table, write, file, buffer, length, ppos);
ian@0 432 if (dirty_writeback_interval) {
ian@0 433 mod_timer(&wb_timer,
ian@0 434 jiffies + dirty_writeback_interval);
ian@0 435 } else {
ian@0 436 del_timer(&wb_timer);
ian@0 437 }
ian@0 438 return 0;
ian@0 439 }
ian@0 440
ian@0 441 static void wb_timer_fn(unsigned long unused)
ian@0 442 {
ian@0 443 if (pdflush_operation(wb_kupdate, 0) < 0)
ian@0 444 mod_timer(&wb_timer, jiffies + HZ); /* delay 1 second */
ian@0 445 }
ian@0 446
ian@0 447 static void laptop_flush(unsigned long unused)
ian@0 448 {
ian@0 449 sys_sync();
ian@0 450 }
ian@0 451
ian@0 452 static void laptop_timer_fn(unsigned long unused)
ian@0 453 {
ian@0 454 pdflush_operation(laptop_flush, 0);
ian@0 455 }
ian@0 456
ian@0 457 /*
ian@0 458 * We've spun up the disk and we're in laptop mode: schedule writeback
ian@0 459 * of all dirty data a few seconds from now. If the flush is already scheduled
ian@0 460 * then push it back - the user is still using the disk.
ian@0 461 */
ian@0 462 void laptop_io_completion(void)
ian@0 463 {
ian@0 464 mod_timer(&laptop_mode_wb_timer, jiffies + laptop_mode);
ian@0 465 }
ian@0 466
ian@0 467 /*
ian@0 468 * We're in laptop mode and we've just synced. The sync's writes will have
ian@0 469 * caused another writeback to be scheduled by laptop_io_completion.
ian@0 470 * Nothing needs to be written back anymore, so we unschedule the writeback.
ian@0 471 */
ian@0 472 void laptop_sync_completion(void)
ian@0 473 {
ian@0 474 del_timer(&laptop_mode_wb_timer);
ian@0 475 }
ian@0 476
ian@0 477 /*
ian@0 478 * If ratelimit_pages is too high then we can get into dirty-data overload
ian@0 479 * if a large number of processes all perform writes at the same time.
ian@0 480 * If it is too low then SMP machines will call the (expensive)
ian@0 481 * get_writeback_state too often.
ian@0 482 *
ian@0 483 * Here we set ratelimit_pages to a level which ensures that when all CPUs are
ian@0 484 * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
ian@0 485 * thresholds before writeback cuts in.
ian@0 486 *
ian@0 487 * But the limit should not be set too high. Because it also controls the
ian@0 488 * amount of memory which the balance_dirty_pages() caller has to write back.
ian@0 489 * If this is too large then the caller will block on the IO queue all the
ian@0 490 * time. So limit it to four megabytes - the balance_dirty_pages() caller
ian@0 491 * will write six megabyte chunks, max.
ian@0 492 */
ian@0 493
ian@0 494 static void set_ratelimit(void)
ian@0 495 {
ian@0 496 ratelimit_pages = total_pages / (num_online_cpus() * 32);
ian@0 497 if (ratelimit_pages < 16)
ian@0 498 ratelimit_pages = 16;
ian@0 499 if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024)
ian@0 500 ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE;
ian@0 501 }
ian@0 502
ian@0 503 static int __cpuinit
ian@0 504 ratelimit_handler(struct notifier_block *self, unsigned long u, void *v)
ian@0 505 {
ian@0 506 set_ratelimit();
ian@0 507 return 0;
ian@0 508 }
ian@0 509
ian@0 510 static struct notifier_block __cpuinitdata ratelimit_nb = {
ian@0 511 .notifier_call = ratelimit_handler,
ian@0 512 .next = NULL,
ian@0 513 };
ian@0 514
ian@0 515 /*
ian@0 516 * If the machine has a large highmem:lowmem ratio then scale back the default
ian@0 517 * dirty memory thresholds: allowing too much dirty highmem pins an excessive
ian@0 518 * number of buffer_heads.
ian@0 519 */
ian@0 520 void __init page_writeback_init(void)
ian@0 521 {
ian@0 522 long buffer_pages = nr_free_buffer_pages();
ian@0 523 long correction;
ian@0 524
ian@0 525 total_pages = nr_free_pagecache_pages();
ian@0 526
ian@0 527 correction = (100 * 4 * buffer_pages) / total_pages;
ian@0 528
ian@0 529 if (correction < 100) {
ian@0 530 dirty_background_ratio *= correction;
ian@0 531 dirty_background_ratio /= 100;
ian@0 532 vm_dirty_ratio *= correction;
ian@0 533 vm_dirty_ratio /= 100;
ian@0 534
ian@0 535 if (dirty_background_ratio <= 0)
ian@0 536 dirty_background_ratio = 1;
ian@0 537 if (vm_dirty_ratio <= 0)
ian@0 538 vm_dirty_ratio = 1;
ian@0 539 }
ian@0 540 mod_timer(&wb_timer, jiffies + dirty_writeback_interval);
ian@0 541 set_ratelimit();
ian@0 542 register_cpu_notifier(&ratelimit_nb);
ian@0 543 }
ian@0 544
ian@0 545 int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
ian@0 546 {
ian@0 547 int ret;
ian@0 548
ian@0 549 if (wbc->nr_to_write <= 0)
ian@0 550 return 0;
ian@0 551 wbc->for_writepages = 1;
ian@0 552 if (mapping->a_ops->writepages)
ian@0 553 ret = mapping->a_ops->writepages(mapping, wbc);
ian@0 554 else
ian@0 555 ret = generic_writepages(mapping, wbc);
ian@0 556 wbc->for_writepages = 0;
ian@0 557 return ret;
ian@0 558 }
ian@0 559
ian@0 560 /**
ian@0 561 * write_one_page - write out a single page and optionally wait on I/O
ian@0 562 *
ian@0 563 * @page: the page to write
ian@0 564 * @wait: if true, wait on writeout
ian@0 565 *
ian@0 566 * The page must be locked by the caller and will be unlocked upon return.
ian@0 567 *
ian@0 568 * write_one_page() returns a negative error code if I/O failed.
ian@0 569 */
ian@0 570 int write_one_page(struct page *page, int wait)
ian@0 571 {
ian@0 572 struct address_space *mapping = page->mapping;
ian@0 573 int ret = 0;
ian@0 574 struct writeback_control wbc = {
ian@0 575 .sync_mode = WB_SYNC_ALL,
ian@0 576 .nr_to_write = 1,
ian@0 577 };
ian@0 578
ian@0 579 BUG_ON(!PageLocked(page));
ian@0 580
ian@0 581 if (wait)
ian@0 582 wait_on_page_writeback(page);
ian@0 583
ian@0 584 if (clear_page_dirty_for_io(page)) {
ian@0 585 page_cache_get(page);
ian@0 586 ret = mapping->a_ops->writepage(page, &wbc);
ian@0 587 if (ret == 0 && wait) {
ian@0 588 wait_on_page_writeback(page);
ian@0 589 if (PageError(page))
ian@0 590 ret = -EIO;
ian@0 591 }
ian@0 592 page_cache_release(page);
ian@0 593 } else {
ian@0 594 unlock_page(page);
ian@0 595 }
ian@0 596 return ret;
ian@0 597 }
ian@0 598 EXPORT_SYMBOL(write_one_page);
ian@0 599
ian@0 600 /*
ian@0 601 * For address_spaces which do not use buffers. Just tag the page as dirty in
ian@0 602 * its radix tree.
ian@0 603 *
ian@0 604 * This is also used when a single buffer is being dirtied: we want to set the
ian@0 605 * page dirty in that case, but not all the buffers. This is a "bottom-up"
ian@0 606 * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
ian@0 607 *
ian@0 608 * Most callers have locked the page, which pins the address_space in memory.
ian@0 609 * But zap_pte_range() does not lock the page, however in that case the
ian@0 610 * mapping is pinned by the vma's ->vm_file reference.
ian@0 611 *
ian@0 612 * We take care to handle the case where the page was truncated from the
ian@0 613 * mapping by re-checking page_mapping() insode tree_lock.
ian@0 614 */
ian@0 615 int __set_page_dirty_nobuffers(struct page *page)
ian@0 616 {
ian@0 617 if (!TestSetPageDirty(page)) {
ian@0 618 struct address_space *mapping = page_mapping(page);
ian@0 619 struct address_space *mapping2;
ian@0 620
ian@0 621 if (mapping) {
ian@0 622 write_lock_irq(&mapping->tree_lock);
ian@0 623 mapping2 = page_mapping(page);
ian@0 624 if (mapping2) { /* Race with truncate? */
ian@0 625 BUG_ON(mapping2 != mapping);
ian@0 626 if (mapping_cap_account_dirty(mapping))
ian@0 627 __inc_zone_page_state(page,
ian@0 628 NR_FILE_DIRTY);
ian@0 629 radix_tree_tag_set(&mapping->page_tree,
ian@0 630 page_index(page), PAGECACHE_TAG_DIRTY);
ian@0 631 }
ian@0 632 write_unlock_irq(&mapping->tree_lock);
ian@0 633 if (mapping->host) {
ian@0 634 /* !PageAnon && !swapper_space */
ian@0 635 __mark_inode_dirty(mapping->host,
ian@0 636 I_DIRTY_PAGES);
ian@0 637 }
ian@0 638 }
ian@0 639 return 1;
ian@0 640 }
ian@0 641 return 0;
ian@0 642 }
ian@0 643 EXPORT_SYMBOL(__set_page_dirty_nobuffers);
ian@0 644
ian@0 645 /*
ian@0 646 * When a writepage implementation decides that it doesn't want to write this
ian@0 647 * page for some reason, it should redirty the locked page via
ian@0 648 * redirty_page_for_writepage() and it should then unlock the page and return 0
ian@0 649 */
ian@0 650 int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page)
ian@0 651 {
ian@0 652 wbc->pages_skipped++;
ian@0 653 return __set_page_dirty_nobuffers(page);
ian@0 654 }
ian@0 655 EXPORT_SYMBOL(redirty_page_for_writepage);
ian@0 656
ian@0 657 /*
ian@0 658 * If the mapping doesn't provide a set_page_dirty a_op, then
ian@0 659 * just fall through and assume that it wants buffer_heads.
ian@0 660 */
ian@0 661 int fastcall set_page_dirty(struct page *page)
ian@0 662 {
ian@0 663 struct address_space *mapping = page_mapping(page);
ian@0 664
ian@0 665 if (likely(mapping)) {
ian@0 666 int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
ian@0 667 if (spd)
ian@0 668 return (*spd)(page);
ian@0 669 return __set_page_dirty_buffers(page);
ian@0 670 }
ian@0 671 if (!PageDirty(page)) {
ian@0 672 if (!TestSetPageDirty(page))
ian@0 673 return 1;
ian@0 674 }
ian@0 675 return 0;
ian@0 676 }
ian@0 677 EXPORT_SYMBOL(set_page_dirty);
ian@0 678
ian@0 679 /*
ian@0 680 * set_page_dirty() is racy if the caller has no reference against
ian@0 681 * page->mapping->host, and if the page is unlocked. This is because another
ian@0 682 * CPU could truncate the page off the mapping and then free the mapping.
ian@0 683 *
ian@0 684 * Usually, the page _is_ locked, or the caller is a user-space process which
ian@0 685 * holds a reference on the inode by having an open file.
ian@0 686 *
ian@0 687 * In other cases, the page should be locked before running set_page_dirty().
ian@0 688 */
ian@0 689 int set_page_dirty_lock(struct page *page)
ian@0 690 {
ian@0 691 int ret;
ian@0 692
ian@0 693 lock_page(page);
ian@0 694 ret = set_page_dirty(page);
ian@0 695 unlock_page(page);
ian@0 696 return ret;
ian@0 697 }
ian@0 698 EXPORT_SYMBOL(set_page_dirty_lock);
ian@0 699
ian@0 700 /*
ian@0 701 * Clear a page's dirty flag, while caring for dirty memory accounting.
ian@0 702 * Returns true if the page was previously dirty.
ian@0 703 */
ian@0 704 int test_clear_page_dirty(struct page *page)
ian@0 705 {
ian@0 706 struct address_space *mapping = page_mapping(page);
ian@0 707 unsigned long flags;
ian@0 708
ian@0 709 if (mapping) {
ian@0 710 write_lock_irqsave(&mapping->tree_lock, flags);
ian@0 711 if (TestClearPageDirty(page)) {
ian@0 712 radix_tree_tag_clear(&mapping->page_tree,
ian@0 713 page_index(page),
ian@0 714 PAGECACHE_TAG_DIRTY);
ian@0 715 if (mapping_cap_account_dirty(mapping))
ian@0 716 __dec_zone_page_state(page, NR_FILE_DIRTY);
ian@0 717 write_unlock_irqrestore(&mapping->tree_lock, flags);
ian@0 718 return 1;
ian@0 719 }
ian@0 720 write_unlock_irqrestore(&mapping->tree_lock, flags);
ian@0 721 return 0;
ian@0 722 }
ian@0 723 return TestClearPageDirty(page);
ian@0 724 }
ian@0 725 EXPORT_SYMBOL(test_clear_page_dirty);
ian@0 726
ian@0 727 /*
ian@0 728 * Clear a page's dirty flag, while caring for dirty memory accounting.
ian@0 729 * Returns true if the page was previously dirty.
ian@0 730 *
ian@0 731 * This is for preparing to put the page under writeout. We leave the page
ian@0 732 * tagged as dirty in the radix tree so that a concurrent write-for-sync
ian@0 733 * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage
ian@0 734 * implementation will run either set_page_writeback() or set_page_dirty(),
ian@0 735 * at which stage we bring the page's dirty flag and radix-tree dirty tag
ian@0 736 * back into sync.
ian@0 737 *
ian@0 738 * This incoherency between the page's dirty flag and radix-tree tag is
ian@0 739 * unfortunate, but it only exists while the page is locked.
ian@0 740 */
ian@0 741 int clear_page_dirty_for_io(struct page *page)
ian@0 742 {
ian@0 743 struct address_space *mapping = page_mapping(page);
ian@0 744
ian@0 745 if (mapping) {
ian@0 746 if (TestClearPageDirty(page)) {
ian@0 747 if (mapping_cap_account_dirty(mapping))
ian@0 748 dec_zone_page_state(page, NR_FILE_DIRTY);
ian@0 749 return 1;
ian@0 750 }
ian@0 751 return 0;
ian@0 752 }
ian@0 753 return TestClearPageDirty(page);
ian@0 754 }
ian@0 755 EXPORT_SYMBOL(clear_page_dirty_for_io);
ian@0 756
ian@0 757 int test_clear_page_writeback(struct page *page)
ian@0 758 {
ian@0 759 struct address_space *mapping = page_mapping(page);
ian@0 760 int ret;
ian@0 761
ian@0 762 if (mapping) {
ian@0 763 unsigned long flags;
ian@0 764
ian@0 765 write_lock_irqsave(&mapping->tree_lock, flags);
ian@0 766 ret = TestClearPageWriteback(page);
ian@0 767 if (ret)
ian@0 768 radix_tree_tag_clear(&mapping->page_tree,
ian@0 769 page_index(page),
ian@0 770 PAGECACHE_TAG_WRITEBACK);
ian@0 771 write_unlock_irqrestore(&mapping->tree_lock, flags);
ian@0 772 } else {
ian@0 773 ret = TestClearPageWriteback(page);
ian@0 774 }
ian@0 775 return ret;
ian@0 776 }
ian@0 777
ian@0 778 int test_set_page_writeback(struct page *page)
ian@0 779 {
ian@0 780 struct address_space *mapping = page_mapping(page);
ian@0 781 int ret;
ian@0 782
ian@0 783 if (mapping) {
ian@0 784 unsigned long flags;
ian@0 785
ian@0 786 write_lock_irqsave(&mapping->tree_lock, flags);
ian@0 787 ret = TestSetPageWriteback(page);
ian@0 788 if (!ret)
ian@0 789 radix_tree_tag_set(&mapping->page_tree,
ian@0 790 page_index(page),
ian@0 791 PAGECACHE_TAG_WRITEBACK);
ian@0 792 if (!PageDirty(page))
ian@0 793 radix_tree_tag_clear(&mapping->page_tree,
ian@0 794 page_index(page),
ian@0 795 PAGECACHE_TAG_DIRTY);
ian@0 796 write_unlock_irqrestore(&mapping->tree_lock, flags);
ian@0 797 } else {
ian@0 798 ret = TestSetPageWriteback(page);
ian@0 799 }
ian@0 800 return ret;
ian@0 801
ian@0 802 }
ian@0 803 EXPORT_SYMBOL(test_set_page_writeback);
ian@0 804
ian@0 805 /*
ian@0 806 * Return true if any of the pages in the mapping are marged with the
ian@0 807 * passed tag.
ian@0 808 */
ian@0 809 int mapping_tagged(struct address_space *mapping, int tag)
ian@0 810 {
ian@0 811 unsigned long flags;
ian@0 812 int ret;
ian@0 813
ian@0 814 read_lock_irqsave(&mapping->tree_lock, flags);
ian@0 815 ret = radix_tree_tagged(&mapping->page_tree, tag);
ian@0 816 read_unlock_irqrestore(&mapping->tree_lock, flags);
ian@0 817 return ret;
ian@0 818 }
ian@0 819 EXPORT_SYMBOL(mapping_tagged);