blob: 165f921ab68ad9ea71ba775146cafac683043b85 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * mm/page-writeback.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2002, Linus Torvalds.
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -07005 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Contains functions related to writing back dirty pages at the
8 * address_space level.
9 *
Francois Camie1f8e872008-10-15 22:01:59 -070010 * 10Apr2002 Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Initial version
12 */
13
14#include <linux/kernel.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040015#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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 Morton55e829a2006-12-10 02:19:27 -080025#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/blkdev.h>
27#include <linux/mpage.h>
Peter Zijlstrad08b3852006-09-25 23:30:57 -070028#include <linux/rmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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>
Al Viroff01bb42011-09-16 02:31:11 -040035#include <linux/buffer_head.h> /* __set_page_dirty_buffers */
David Howells811d7362006-08-29 19:06:09 +010036#include <linux/pagevec.h>
Lisa Due0935212013-09-11 14:22:36 -070037#include <linux/mm_inline.h>
Dave Chinner028c2dd2010-07-07 13:24:07 +100038#include <trace/events/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Lisa Due0935212013-09-11 14:22:36 -070040#include "internal.h"
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
Wu Fengguangffd1f602011-06-19 22:18:42 -060043 * Sleep at most 200ms at a time in balance_dirty_pages().
44 */
45#define MAX_PAUSE max(HZ/5, 1)
46
47/*
Wu Fengguang5b9b3572011-12-06 13:17:17 -060048 * Try to keep balance_dirty_pages() call intervals higher than this many pages
49 * by raising pause time to max_pause when falls below it.
50 */
51#define DIRTY_POLL_THRESH (128 >> (PAGE_SHIFT - 10))
52
53/*
Wu Fengguange98be2d2010-08-29 11:22:30 -060054 * Estimate write bandwidth at 200ms intervals.
55 */
56#define BANDWIDTH_INTERVAL max(HZ/5, 1)
57
Wu Fengguang6c14ae12011-03-02 16:04:18 -060058#define RATELIMIT_CALC_SHIFT 10
59
Wu Fengguange98be2d2010-08-29 11:22:30 -060060/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited
62 * will look to see if it needs to force writeback or throttling.
63 */
64static long ratelimit_pages = 32;
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/* The following parameters are exported via /proc/sys/vm */
67
68/*
Jens Axboe5b0830c2009-09-23 19:37:09 +020069 * Start background writeback (via writeback threads) at this percentage
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 */
Wu Fengguang1b5e62b2009-03-23 08:57:38 +080071int dirty_background_ratio = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
David Rientjes2da02992009-01-06 14:39:31 -080074 * dirty_background_bytes starts at 0 (disabled) so that it is a function of
75 * dirty_background_ratio * the amount of dirtyable memory
76 */
77unsigned long dirty_background_bytes;
78
79/*
Bron Gondwana195cf452008-02-04 22:29:20 -080080 * free highmem will not be subtracted from the total free memory
81 * for calculating free ratios if vm_highmem_is_dirtyable is true
82 */
83int vm_highmem_is_dirtyable;
84
85/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * The generator of dirty data starts writeback at this percentage
87 */
Wu Fengguang1b5e62b2009-03-23 08:57:38 +080088int vm_dirty_ratio = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/*
David Rientjes2da02992009-01-06 14:39:31 -080091 * vm_dirty_bytes starts at 0 (disabled) so that it is a function of
92 * vm_dirty_ratio * the amount of dirtyable memory
93 */
94unsigned long vm_dirty_bytes;
95
96/*
Alexey Dobriyan704503d2009-03-31 15:23:18 -070097 * The interval between `kupdate'-style writebacks
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 */
Toshiyuki Okajima22ef37e2009-05-16 22:56:28 -070099unsigned int dirty_writeback_interval = 5 * 100; /* centiseconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Artem Bityutskiy91913a22012-03-21 22:33:00 -0400101EXPORT_SYMBOL_GPL(dirty_writeback_interval);
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
Alexey Dobriyan704503d2009-03-31 15:23:18 -0700104 * The longest time for which data is allowed to remain dirty
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 */
Toshiyuki Okajima22ef37e2009-05-16 22:56:28 -0700106unsigned int dirty_expire_interval = 30 * 100; /* centiseconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*
109 * Flag that makes the machine dump writes/reads and block dirtyings.
110 */
111int block_dump;
112
113/*
Bart Samweled5b43f2006-03-24 03:15:49 -0800114 * Flag that puts the machine in "laptop mode". Doubles as a timeout in jiffies:
115 * a full sync is triggered after this time elapses without any disk activity.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
117int laptop_mode;
118
119EXPORT_SYMBOL(laptop_mode);
120
121/* End of sysctl-exported parameters */
122
Wu Fengguangc42843f2011-03-02 15:54:09 -0600123unsigned long global_dirty_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/*
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700126 * Scale the writeback cache size proportional to the relative writeout speeds.
127 *
128 * We do this by keeping a floating proportion between BDIs, based on page
129 * writeback completions [end_page_writeback()]. Those devices that write out
130 * pages fastest will get the larger share, while the slower will get a smaller
131 * share.
132 *
133 * We use page writeout completions because we are interested in getting rid of
134 * dirty pages. Having them written out is the primary goal.
135 *
136 * We introduce a concept of time, a period over which we measure these events,
137 * because demand can/will vary over time. The length of this period itself is
138 * measured in page writeback completions.
139 *
140 */
141static struct prop_descriptor vm_completions;
142
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700143/*
Johannes Weiner1edf2232012-01-10 15:06:57 -0800144 * Work out the current dirty-memory clamping and background writeout
145 * thresholds.
146 *
147 * The main aim here is to lower them aggressively if there is a lot of mapped
148 * memory around. To avoid stressing page reclaim with lots of unreclaimable
149 * pages. It is better to clamp down on writers than to start swapping, and
150 * performing lots of scanning.
151 *
152 * We only allow 1/2 of the currently-unmapped memory to be dirtied.
153 *
154 * We don't permit the clamping level to fall below 5% - that is getting rather
155 * excessive.
156 *
157 * We make sure that the background writeout level is below the adjusted
158 * clamping level.
159 */
Johannes Weinerccafa282012-01-10 15:07:44 -0800160
Johannes Weinera756cf52012-01-10 15:07:49 -0800161/*
162 * In a memory zone, there is a certain amount of pages we consider
163 * available for the page cache, which is essentially the number of
164 * free and reclaimable pages, minus some zone reserves to protect
165 * lowmem and the ability to uphold the zone's watermarks without
166 * requiring writeback.
167 *
168 * This number of dirtyable pages is the base value of which the
169 * user-configurable dirty ratio is the effictive number of pages that
170 * are allowed to be actually dirtied. Per individual zone, or
171 * globally by using the sum of dirtyable pages over all zones.
172 *
173 * Because the user is allowed to specify the dirty limit globally as
174 * absolute number of bytes, calculating the per-zone dirty limit can
175 * require translating the configured limit into a percentage of
176 * global dirtyable memory first.
177 */
178
Johannes Weiner1edf2232012-01-10 15:06:57 -0800179static unsigned long highmem_dirtyable_memory(unsigned long total)
180{
181#ifdef CONFIG_HIGHMEM
182 int node;
183 unsigned long x = 0;
184
185 for_each_node_state(node, N_HIGH_MEMORY) {
186 struct zone *z =
187 &NODE_DATA(node)->node_zones[ZONE_HIGHMEM];
188
189 x += zone_page_state(z, NR_FREE_PAGES) +
Johannes Weinerab8fabd2012-01-10 15:07:42 -0800190 zone_reclaimable_pages(z) - z->dirty_balance_reserve;
Johannes Weiner1edf2232012-01-10 15:06:57 -0800191 }
192 /*
Sonny Rao64a552f2012-12-20 15:05:07 -0800193 * Unreclaimable memory (kernel memory or anonymous memory
194 * without swap) can bring down the dirtyable pages below
195 * the zone's dirty balance reserve and the above calculation
196 * will underflow. However we still want to add in nodes
197 * which are below threshold (negative values) to get a more
198 * accurate calculation but make sure that the total never
199 * underflows.
200 */
201 if ((long)x < 0)
202 x = 0;
203
204 /*
Johannes Weiner1edf2232012-01-10 15:06:57 -0800205 * Make sure that the number of highmem pages is never larger
206 * than the number of the total dirtyable memory. This can only
207 * occur in very strange VM situations but we want to make sure
208 * that this does not occur.
209 */
210 return min(x, total);
211#else
212 return 0;
213#endif
214}
215
216/**
Johannes Weinerccafa282012-01-10 15:07:44 -0800217 * global_dirtyable_memory - number of globally dirtyable pages
Johannes Weiner1edf2232012-01-10 15:06:57 -0800218 *
Johannes Weinerccafa282012-01-10 15:07:44 -0800219 * Returns the global number of pages potentially available for dirty
220 * page cache. This is the base value for the global dirty limits.
Johannes Weiner1edf2232012-01-10 15:06:57 -0800221 */
Johannes Weinerccafa282012-01-10 15:07:44 -0800222unsigned long global_dirtyable_memory(void)
Johannes Weiner1edf2232012-01-10 15:06:57 -0800223{
224 unsigned long x;
225
Sonny Rao64a552f2012-12-20 15:05:07 -0800226 x = global_page_state(NR_FREE_PAGES) + global_reclaimable_pages();
227 x -= min(x, dirty_balance_reserve);
Johannes Weiner1edf2232012-01-10 15:06:57 -0800228
229 if (!vm_highmem_is_dirtyable)
230 x -= highmem_dirtyable_memory(x);
231
232 return x + 1; /* Ensure that we never return 0 */
233}
234
235/*
Johannes Weinerccafa282012-01-10 15:07:44 -0800236 * global_dirty_limits - background-writeback and dirty-throttling thresholds
237 *
238 * Calculate the dirty thresholds based on sysctl parameters
239 * - vm.dirty_background_ratio or vm.dirty_background_bytes
240 * - vm.dirty_ratio or vm.dirty_bytes
241 * The dirty limits will be lifted by 1/4 for PF_LESS_THROTTLE (ie. nfsd) and
242 * real-time tasks.
243 */
244void global_dirty_limits(unsigned long *pbackground, unsigned long *pdirty)
245{
246 unsigned long background;
247 unsigned long dirty;
248 unsigned long uninitialized_var(available_memory);
249 struct task_struct *tsk;
250
251 if (!vm_dirty_bytes || !dirty_background_bytes)
252 available_memory = global_dirtyable_memory();
253
254 if (vm_dirty_bytes)
255 dirty = DIV_ROUND_UP(vm_dirty_bytes, PAGE_SIZE);
256 else
257 dirty = (vm_dirty_ratio * available_memory) / 100;
258
259 if (dirty_background_bytes)
260 background = DIV_ROUND_UP(dirty_background_bytes, PAGE_SIZE);
261 else
262 background = (dirty_background_ratio * available_memory) / 100;
263
264 if (background >= dirty)
265 background = dirty / 2;
266 tsk = current;
267 if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) {
268 background += background / 4;
269 dirty += dirty / 4;
270 }
271 *pbackground = background;
272 *pdirty = dirty;
273 trace_global_dirty_state(background, dirty);
274}
275
Johannes Weinera756cf52012-01-10 15:07:49 -0800276/**
277 * zone_dirtyable_memory - number of dirtyable pages in a zone
278 * @zone: the zone
279 *
280 * Returns the zone's number of pages potentially available for dirty
281 * page cache. This is the base value for the per-zone dirty limits.
282 */
283static unsigned long zone_dirtyable_memory(struct zone *zone)
284{
285 /*
286 * The effective global number of dirtyable pages may exclude
287 * highmem as a big-picture measure to keep the ratio between
288 * dirty memory and lowmem reasonable.
289 *
290 * But this function is purely about the individual zone and a
291 * highmem zone can hold its share of dirty pages, so we don't
292 * care about vm_highmem_is_dirtyable here.
293 */
Sonny Rao64a552f2012-12-20 15:05:07 -0800294 unsigned long nr_pages = zone_page_state(zone, NR_FREE_PAGES) +
295 zone_reclaimable_pages(zone);
296
297 /* don't allow this to underflow */
298 nr_pages -= min(nr_pages, zone->dirty_balance_reserve);
299 return nr_pages;
Johannes Weinera756cf52012-01-10 15:07:49 -0800300}
301
302/**
303 * zone_dirty_limit - maximum number of dirty pages allowed in a zone
304 * @zone: the zone
305 *
306 * Returns the maximum number of dirty pages allowed in a zone, based
307 * on the zone's dirtyable memory.
308 */
309static unsigned long zone_dirty_limit(struct zone *zone)
310{
311 unsigned long zone_memory = zone_dirtyable_memory(zone);
312 struct task_struct *tsk = current;
313 unsigned long dirty;
314
315 if (vm_dirty_bytes)
316 dirty = DIV_ROUND_UP(vm_dirty_bytes, PAGE_SIZE) *
317 zone_memory / global_dirtyable_memory();
318 else
319 dirty = vm_dirty_ratio * zone_memory / 100;
320
321 if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk))
322 dirty += dirty / 4;
323
324 return dirty;
325}
326
327/**
328 * zone_dirty_ok - tells whether a zone is within its dirty limits
329 * @zone: the zone to check
330 *
331 * Returns %true when the dirty pages in @zone are within the zone's
332 * dirty limit, %false if the limit is exceeded.
333 */
334bool zone_dirty_ok(struct zone *zone)
335{
336 unsigned long limit = zone_dirty_limit(zone);
337
338 return zone_page_state(zone, NR_FILE_DIRTY) +
339 zone_page_state(zone, NR_UNSTABLE_NFS) +
340 zone_page_state(zone, NR_WRITEBACK) <= limit;
341}
342
Johannes Weinerccafa282012-01-10 15:07:44 -0800343/*
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700344 * couple the period to the dirty_ratio:
345 *
346 * period/2 ~ roundup_pow_of_two(dirty limit)
347 */
348static int calc_period_shift(void)
349{
350 unsigned long dirty_total;
351
David Rientjes2da02992009-01-06 14:39:31 -0800352 if (vm_dirty_bytes)
353 dirty_total = vm_dirty_bytes / PAGE_SIZE;
354 else
Johannes Weinerccafa282012-01-10 15:07:44 -0800355 dirty_total = (vm_dirty_ratio * global_dirtyable_memory()) /
David Rientjes2da02992009-01-06 14:39:31 -0800356 100;
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700357 return 2 + ilog2(dirty_total - 1);
358}
359
360/*
David Rientjes2da02992009-01-06 14:39:31 -0800361 * update the period when the dirty threshold changes.
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700362 */
David Rientjes2da02992009-01-06 14:39:31 -0800363static void update_completion_period(void)
364{
365 int shift = calc_period_shift();
366 prop_change_shift(&vm_completions, shift);
Wu Fengguang9d823e82011-06-11 18:10:12 -0600367
368 writeback_set_ratelimit();
David Rientjes2da02992009-01-06 14:39:31 -0800369}
370
371int dirty_background_ratio_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700372 void __user *buffer, size_t *lenp,
David Rientjes2da02992009-01-06 14:39:31 -0800373 loff_t *ppos)
374{
375 int ret;
376
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700377 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
David Rientjes2da02992009-01-06 14:39:31 -0800378 if (ret == 0 && write)
379 dirty_background_bytes = 0;
380 return ret;
381}
382
383int dirty_background_bytes_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700384 void __user *buffer, size_t *lenp,
David Rientjes2da02992009-01-06 14:39:31 -0800385 loff_t *ppos)
386{
387 int ret;
388
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700389 ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
David Rientjes2da02992009-01-06 14:39:31 -0800390 if (ret == 0 && write)
391 dirty_background_ratio = 0;
392 return ret;
393}
394
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700395int dirty_ratio_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700396 void __user *buffer, size_t *lenp,
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700397 loff_t *ppos)
398{
399 int old_ratio = vm_dirty_ratio;
David Rientjes2da02992009-01-06 14:39:31 -0800400 int ret;
401
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700402 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700403 if (ret == 0 && write && vm_dirty_ratio != old_ratio) {
David Rientjes2da02992009-01-06 14:39:31 -0800404 update_completion_period();
405 vm_dirty_bytes = 0;
406 }
407 return ret;
408}
409
David Rientjes2da02992009-01-06 14:39:31 -0800410int dirty_bytes_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700411 void __user *buffer, size_t *lenp,
David Rientjes2da02992009-01-06 14:39:31 -0800412 loff_t *ppos)
413{
Sven Wegenerfc3501d2009-02-11 13:04:23 -0800414 unsigned long old_bytes = vm_dirty_bytes;
David Rientjes2da02992009-01-06 14:39:31 -0800415 int ret;
416
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700417 ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
David Rientjes2da02992009-01-06 14:39:31 -0800418 if (ret == 0 && write && vm_dirty_bytes != old_bytes) {
419 update_completion_period();
420 vm_dirty_ratio = 0;
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700421 }
422 return ret;
423}
424
425/*
426 * Increment the BDI's writeout completion count and the global writeout
427 * completion count. Called from test_clear_page_writeback().
428 */
429static inline void __bdi_writeout_inc(struct backing_dev_info *bdi)
430{
Jan Karaf7d2b1e2010-12-08 22:44:24 -0600431 __inc_bdi_stat(bdi, BDI_WRITTEN);
Peter Zijlstraa42dde02008-04-30 00:54:36 -0700432 __prop_inc_percpu_max(&vm_completions, &bdi->completions,
433 bdi->max_prop_frac);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700434}
435
Miklos Szeredidd5656e2008-04-30 00:54:37 -0700436void bdi_writeout_inc(struct backing_dev_info *bdi)
437{
438 unsigned long flags;
439
440 local_irq_save(flags);
441 __bdi_writeout_inc(bdi);
442 local_irq_restore(flags);
443}
444EXPORT_SYMBOL_GPL(bdi_writeout_inc);
445
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700446/*
447 * Obtain an accurate fraction of the BDI's portion.
448 */
449static void bdi_writeout_fraction(struct backing_dev_info *bdi,
450 long *numerator, long *denominator)
451{
Wu Fengguang3efaf0f2010-12-16 22:22:00 -0600452 prop_fraction_percpu(&vm_completions, &bdi->completions,
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700453 numerator, denominator);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700454}
455
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700456/*
Johannes Weinerd08c4292011-10-31 17:07:05 -0700457 * bdi_min_ratio keeps the sum of the minimum dirty shares of all
458 * registered backing devices, which, for obvious reasons, can not
459 * exceed 100%.
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700460 */
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700461static unsigned int bdi_min_ratio;
462
463int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio)
464{
465 int ret = 0;
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700466
Jens Axboecfc4ba52009-09-14 13:12:40 +0200467 spin_lock_bh(&bdi_lock);
Peter Zijlstraa42dde02008-04-30 00:54:36 -0700468 if (min_ratio > bdi->max_ratio) {
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700469 ret = -EINVAL;
Peter Zijlstraa42dde02008-04-30 00:54:36 -0700470 } else {
471 min_ratio -= bdi->min_ratio;
472 if (bdi_min_ratio + min_ratio < 100) {
473 bdi_min_ratio += min_ratio;
474 bdi->min_ratio += min_ratio;
475 } else {
476 ret = -EINVAL;
477 }
478 }
Jens Axboecfc4ba52009-09-14 13:12:40 +0200479 spin_unlock_bh(&bdi_lock);
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700480
481 return ret;
482}
483
Peter Zijlstraa42dde02008-04-30 00:54:36 -0700484int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned max_ratio)
485{
Peter Zijlstraa42dde02008-04-30 00:54:36 -0700486 int ret = 0;
487
488 if (max_ratio > 100)
489 return -EINVAL;
490
Jens Axboecfc4ba52009-09-14 13:12:40 +0200491 spin_lock_bh(&bdi_lock);
Peter Zijlstraa42dde02008-04-30 00:54:36 -0700492 if (bdi->min_ratio > max_ratio) {
493 ret = -EINVAL;
494 } else {
495 bdi->max_ratio = max_ratio;
496 bdi->max_prop_frac = (PROP_FRAC_BASE * max_ratio) / 100;
497 }
Jens Axboecfc4ba52009-09-14 13:12:40 +0200498 spin_unlock_bh(&bdi_lock);
Peter Zijlstraa42dde02008-04-30 00:54:36 -0700499
500 return ret;
501}
502EXPORT_SYMBOL(bdi_set_max_ratio);
503
Wu Fengguang6c14ae12011-03-02 16:04:18 -0600504static unsigned long dirty_freerun_ceiling(unsigned long thresh,
505 unsigned long bg_thresh)
506{
507 return (thresh + bg_thresh) / 2;
508}
509
Wu Fengguangffd1f602011-06-19 22:18:42 -0600510static unsigned long hard_dirty_limit(unsigned long thresh)
511{
512 return max(thresh, global_dirty_limit);
513}
514
Wu Fengguang6f718652011-03-02 17:14:34 -0600515/**
Wu Fengguang1babe182010-08-11 14:17:40 -0700516 * bdi_dirty_limit - @bdi's share of dirty throttling threshold
Wu Fengguang6f718652011-03-02 17:14:34 -0600517 * @bdi: the backing_dev_info to query
518 * @dirty: global dirty limit in pages
Wu Fengguang1babe182010-08-11 14:17:40 -0700519 *
Wu Fengguang6f718652011-03-02 17:14:34 -0600520 * Returns @bdi's dirty limit in pages. The term "dirty" in the context of
521 * dirty balancing includes all PG_dirty, PG_writeback and NFS unstable pages.
Wu Fengguangaed21ad2011-11-23 11:44:41 -0600522 *
523 * Note that balance_dirty_pages() will only seriously take it as a hard limit
524 * when sleeping max_pause per page is not enough to keep the dirty pages under
525 * control. For example, when the device is completely stalled due to some error
526 * conditions, or when there are 1000 dd tasks writing to a slow 10MB/s USB key.
527 * In the other normal situations, it acts more gently by throttling the tasks
528 * more (rather than completely block them) when the bdi dirty pages go high.
Wu Fengguang6f718652011-03-02 17:14:34 -0600529 *
530 * It allocates high/low dirty limits to fast/slow devices, in order to prevent
Wu Fengguang1babe182010-08-11 14:17:40 -0700531 * - starving fast devices
532 * - piling up dirty pages (that will take long time to sync) on slow devices
533 *
534 * The bdi's share of dirty limit will be adapting to its throughput and
535 * bounded by the bdi->min_ratio and/or bdi->max_ratio parameters, if set.
536 */
537unsigned long bdi_dirty_limit(struct backing_dev_info *bdi, unsigned long dirty)
Wu Fengguang16c40422010-08-11 14:17:39 -0700538{
539 u64 bdi_dirty;
540 long numerator, denominator;
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700541
Wu Fengguang16c40422010-08-11 14:17:39 -0700542 /*
543 * Calculate this BDI's share of the dirty ratio.
544 */
545 bdi_writeout_fraction(bdi, &numerator, &denominator);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700546
Wu Fengguang16c40422010-08-11 14:17:39 -0700547 bdi_dirty = (dirty * (100 - bdi_min_ratio)) / 100;
548 bdi_dirty *= numerator;
549 do_div(bdi_dirty, denominator);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700550
Wu Fengguang16c40422010-08-11 14:17:39 -0700551 bdi_dirty += (dirty * bdi->min_ratio) / 100;
552 if (bdi_dirty > (dirty * bdi->max_ratio) / 100)
553 bdi_dirty = dirty * bdi->max_ratio / 100;
554
555 return bdi_dirty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Wu Fengguang6c14ae12011-03-02 16:04:18 -0600558/*
Maxim Patlasov459b18d2013-09-11 14:22:46 -0700559 * setpoint - dirty 3
560 * f(dirty) := 1.0 + (----------------)
561 * limit - setpoint
562 *
563 * it's a 3rd order polynomial that subjects to
564 *
565 * (1) f(freerun) = 2.0 => rampup dirty_ratelimit reasonably fast
566 * (2) f(setpoint) = 1.0 => the balance point
567 * (3) f(limit) = 0 => the hard limit
568 * (4) df/dx <= 0 => negative feedback control
569 * (5) the closer to setpoint, the smaller |df/dx| (and the reverse)
570 * => fast response on large errors; small oscillation near setpoint
571 */
572static inline long long pos_ratio_polynom(unsigned long setpoint,
573 unsigned long dirty,
574 unsigned long limit)
575{
576 long long pos_ratio;
577 long x;
578
579 x = div_s64(((s64)setpoint - (s64)dirty) << RATELIMIT_CALC_SHIFT,
580 limit - setpoint + 1);
581 pos_ratio = x;
582 pos_ratio = pos_ratio * x >> RATELIMIT_CALC_SHIFT;
583 pos_ratio = pos_ratio * x >> RATELIMIT_CALC_SHIFT;
584 pos_ratio += 1 << RATELIMIT_CALC_SHIFT;
585
586 return clamp(pos_ratio, 0LL, 2LL << RATELIMIT_CALC_SHIFT);
587}
588
589/*
Wu Fengguang6c14ae12011-03-02 16:04:18 -0600590 * Dirty position control.
591 *
592 * (o) global/bdi setpoints
593 *
594 * We want the dirty pages be balanced around the global/bdi setpoints.
595 * When the number of dirty pages is higher/lower than the setpoint, the
596 * dirty position control ratio (and hence task dirty ratelimit) will be
597 * decreased/increased to bring the dirty pages back to the setpoint.
598 *
599 * pos_ratio = 1 << RATELIMIT_CALC_SHIFT
600 *
601 * if (dirty < setpoint) scale up pos_ratio
602 * if (dirty > setpoint) scale down pos_ratio
603 *
604 * if (bdi_dirty < bdi_setpoint) scale up pos_ratio
605 * if (bdi_dirty > bdi_setpoint) scale down pos_ratio
606 *
607 * task_ratelimit = dirty_ratelimit * pos_ratio >> RATELIMIT_CALC_SHIFT
608 *
609 * (o) global control line
610 *
611 * ^ pos_ratio
612 * |
613 * | |<===== global dirty control scope ======>|
614 * 2.0 .............*
615 * | .*
616 * | . *
617 * | . *
618 * | . *
619 * | . *
620 * | . *
621 * 1.0 ................................*
622 * | . . *
623 * | . . *
624 * | . . *
625 * | . . *
626 * | . . *
627 * 0 +------------.------------------.----------------------*------------->
628 * freerun^ setpoint^ limit^ dirty pages
629 *
630 * (o) bdi control line
631 *
632 * ^ pos_ratio
633 * |
634 * | *
635 * | *
636 * | *
637 * | *
638 * | * |<=========== span ============>|
639 * 1.0 .......................*
640 * | . *
641 * | . *
642 * | . *
643 * | . *
644 * | . *
645 * | . *
646 * | . *
647 * | . *
648 * | . *
649 * | . *
650 * | . *
651 * 1/4 ...............................................* * * * * * * * * * * *
652 * | . .
653 * | . .
654 * | . .
655 * 0 +----------------------.-------------------------------.------------->
656 * bdi_setpoint^ x_intercept^
657 *
658 * The bdi control line won't drop below pos_ratio=1/4, so that bdi_dirty can
659 * be smoothly throttled down to normal if it starts high in situations like
660 * - start writing to a slow SD card and a fast disk at the same time. The SD
661 * card's bdi_dirty may rush to many times higher than bdi_setpoint.
662 * - the bdi dirty thresh drops quickly due to change of JBOD workload
663 */
664static unsigned long bdi_position_ratio(struct backing_dev_info *bdi,
665 unsigned long thresh,
666 unsigned long bg_thresh,
667 unsigned long dirty,
668 unsigned long bdi_thresh,
669 unsigned long bdi_dirty)
670{
671 unsigned long write_bw = bdi->avg_write_bandwidth;
672 unsigned long freerun = dirty_freerun_ceiling(thresh, bg_thresh);
673 unsigned long limit = hard_dirty_limit(thresh);
674 unsigned long x_intercept;
675 unsigned long setpoint; /* dirty pages' target balance point */
676 unsigned long bdi_setpoint;
677 unsigned long span;
678 long long pos_ratio; /* for scaling up/down the rate limit */
679 long x;
680
681 if (unlikely(dirty >= limit))
682 return 0;
683
684 /*
685 * global setpoint
686 *
Maxim Patlasov459b18d2013-09-11 14:22:46 -0700687 * See comment for pos_ratio_polynom().
Wu Fengguang6c14ae12011-03-02 16:04:18 -0600688 */
689 setpoint = (freerun + limit) / 2;
Maxim Patlasov459b18d2013-09-11 14:22:46 -0700690 pos_ratio = pos_ratio_polynom(setpoint, dirty, limit);
691
692 /*
693 * The strictlimit feature is a tool preventing mistrusted filesystems
694 * from growing a large number of dirty pages before throttling. For
695 * such filesystems balance_dirty_pages always checks bdi counters
696 * against bdi limits. Even if global "nr_dirty" is under "freerun".
697 * This is especially important for fuse which sets bdi->max_ratio to
698 * 1% by default. Without strictlimit feature, fuse writeback may
699 * consume arbitrary amount of RAM because it is accounted in
700 * NR_WRITEBACK_TEMP which is not involved in calculating "nr_dirty".
701 *
702 * Here, in bdi_position_ratio(), we calculate pos_ratio based on
703 * two values: bdi_dirty and bdi_thresh. Let's consider an example:
704 * total amount of RAM is 16GB, bdi->max_ratio is equal to 1%, global
705 * limits are set by default to 10% and 20% (background and throttle).
706 * Then bdi_thresh is 1% of 20% of 16GB. This amounts to ~8K pages.
707 * bdi_dirty_limit(bdi, bg_thresh) is about ~4K pages. bdi_setpoint is
708 * about ~6K pages (as the average of background and throttle bdi
709 * limits). The 3rd order polynomial will provide positive feedback if
710 * bdi_dirty is under bdi_setpoint and vice versa.
711 *
712 * Note, that we cannot use global counters in these calculations
713 * because we want to throttle process writing to a strictlimit BDI
714 * much earlier than global "freerun" is reached (~23MB vs. ~2.3GB
715 * in the example above).
716 */
717 if (unlikely(bdi->capabilities & BDI_CAP_STRICTLIMIT)) {
718 long long bdi_pos_ratio;
719 unsigned long bdi_bg_thresh;
720
721 if (bdi_dirty < 8)
722 return min_t(long long, pos_ratio * 2,
723 2 << RATELIMIT_CALC_SHIFT);
724
725 if (bdi_dirty >= bdi_thresh)
726 return 0;
727
728 bdi_bg_thresh = div_u64((u64)bdi_thresh * bg_thresh, thresh);
729 bdi_setpoint = dirty_freerun_ceiling(bdi_thresh,
730 bdi_bg_thresh);
731
732 if (bdi_setpoint == 0 || bdi_setpoint == bdi_thresh)
733 return 0;
734
735 bdi_pos_ratio = pos_ratio_polynom(bdi_setpoint, bdi_dirty,
736 bdi_thresh);
737
738 /*
739 * Typically, for strictlimit case, bdi_setpoint << setpoint
740 * and pos_ratio >> bdi_pos_ratio. In the other words global
741 * state ("dirty") is not limiting factor and we have to
742 * make decision based on bdi counters. But there is an
743 * important case when global pos_ratio should get precedence:
744 * global limits are exceeded (e.g. due to activities on other
745 * BDIs) while given strictlimit BDI is below limit.
746 *
747 * "pos_ratio * bdi_pos_ratio" would work for the case above,
748 * but it would look too non-natural for the case of all
749 * activity in the system coming from a single strictlimit BDI
750 * with bdi->max_ratio == 100%.
751 *
752 * Note that min() below somewhat changes the dynamics of the
753 * control system. Normally, pos_ratio value can be well over 3
754 * (when globally we are at freerun and bdi is well below bdi
755 * setpoint). Now the maximum pos_ratio in the same situation
756 * is 2. We might want to tweak this if we observe the control
757 * system is too slow to adapt.
758 */
759 return min(pos_ratio, bdi_pos_ratio);
760 }
Wu Fengguang6c14ae12011-03-02 16:04:18 -0600761
762 /*
763 * We have computed basic pos_ratio above based on global situation. If
764 * the bdi is over/under its share of dirty pages, we want to scale
765 * pos_ratio further down/up. That is done by the following mechanism.
766 */
767
768 /*
769 * bdi setpoint
770 *
771 * f(bdi_dirty) := 1.0 + k * (bdi_dirty - bdi_setpoint)
772 *
773 * x_intercept - bdi_dirty
774 * := --------------------------
775 * x_intercept - bdi_setpoint
776 *
777 * The main bdi control line is a linear function that subjects to
778 *
779 * (1) f(bdi_setpoint) = 1.0
780 * (2) k = - 1 / (8 * write_bw) (in single bdi case)
781 * or equally: x_intercept = bdi_setpoint + 8 * write_bw
782 *
783 * For single bdi case, the dirty pages are observed to fluctuate
784 * regularly within range
785 * [bdi_setpoint - write_bw/2, bdi_setpoint + write_bw/2]
786 * for various filesystems, where (2) can yield in a reasonable 12.5%
787 * fluctuation range for pos_ratio.
788 *
789 * For JBOD case, bdi_thresh (not bdi_dirty!) could fluctuate up to its
790 * own size, so move the slope over accordingly and choose a slope that
791 * yields 100% pos_ratio fluctuation on suddenly doubled bdi_thresh.
792 */
793 if (unlikely(bdi_thresh > thresh))
794 bdi_thresh = thresh;
Wu Fengguangaed21ad2011-11-23 11:44:41 -0600795 /*
796 * It's very possible that bdi_thresh is close to 0 not because the
797 * device is slow, but that it has remained inactive for long time.
798 * Honour such devices a reasonable good (hopefully IO efficient)
799 * threshold, so that the occasional writes won't be blocked and active
800 * writes can rampup the threshold quickly.
801 */
Wu Fengguang8927f662011-08-04 22:16:46 -0600802 bdi_thresh = max(bdi_thresh, (limit - dirty) / 8);
Wu Fengguang6c14ae12011-03-02 16:04:18 -0600803 /*
804 * scale global setpoint to bdi's:
805 * bdi_setpoint = setpoint * bdi_thresh / thresh
806 */
807 x = div_u64((u64)bdi_thresh << 16, thresh + 1);
808 bdi_setpoint = setpoint * (u64)x >> 16;
809 /*
810 * Use span=(8*write_bw) in single bdi case as indicated by
811 * (thresh - bdi_thresh ~= 0) and transit to bdi_thresh in JBOD case.
812 *
813 * bdi_thresh thresh - bdi_thresh
814 * span = ---------- * (8 * write_bw) + ------------------- * bdi_thresh
815 * thresh thresh
816 */
817 span = (thresh - bdi_thresh + 8 * write_bw) * (u64)x >> 16;
818 x_intercept = bdi_setpoint + span;
819
820 if (bdi_dirty < x_intercept - span / 4) {
Wu Fengguang50657fc2011-10-11 17:06:33 -0600821 pos_ratio = div_u64(pos_ratio * (x_intercept - bdi_dirty),
822 x_intercept - bdi_setpoint + 1);
Wu Fengguang6c14ae12011-03-02 16:04:18 -0600823 } else
824 pos_ratio /= 4;
825
Wu Fengguang8927f662011-08-04 22:16:46 -0600826 /*
827 * bdi reserve area, safeguard against dirty pool underrun and disk idle
828 * It may push the desired control point of global dirty pages higher
829 * than setpoint.
830 */
831 x_intercept = bdi_thresh / 2;
832 if (bdi_dirty < x_intercept) {
Wu Fengguang50657fc2011-10-11 17:06:33 -0600833 if (bdi_dirty > x_intercept / 8)
834 pos_ratio = div_u64(pos_ratio * x_intercept, bdi_dirty);
835 else
Wu Fengguang8927f662011-08-04 22:16:46 -0600836 pos_ratio *= 8;
837 }
838
Wu Fengguang6c14ae12011-03-02 16:04:18 -0600839 return pos_ratio;
840}
841
Wu Fengguange98be2d2010-08-29 11:22:30 -0600842static void bdi_update_write_bandwidth(struct backing_dev_info *bdi,
843 unsigned long elapsed,
844 unsigned long written)
845{
846 const unsigned long period = roundup_pow_of_two(3 * HZ);
847 unsigned long avg = bdi->avg_write_bandwidth;
848 unsigned long old = bdi->write_bandwidth;
849 u64 bw;
850
851 /*
852 * bw = written * HZ / elapsed
853 *
854 * bw * elapsed + write_bandwidth * (period - elapsed)
855 * write_bandwidth = ---------------------------------------------------
856 * period
857 */
858 bw = written - bdi->written_stamp;
859 bw *= HZ;
860 if (unlikely(elapsed > period)) {
861 do_div(bw, elapsed);
862 avg = bw;
863 goto out;
864 }
865 bw += (u64)bdi->write_bandwidth * (period - elapsed);
866 bw >>= ilog2(period);
867
868 /*
869 * one more level of smoothing, for filtering out sudden spikes
870 */
871 if (avg > old && old >= (unsigned long)bw)
872 avg -= (avg - old) >> 3;
873
874 if (avg < old && old <= (unsigned long)bw)
875 avg += (old - avg) >> 3;
876
877out:
878 bdi->write_bandwidth = bw;
879 bdi->avg_write_bandwidth = avg;
880}
881
Wu Fengguangc42843f2011-03-02 15:54:09 -0600882/*
883 * The global dirtyable memory and dirty threshold could be suddenly knocked
884 * down by a large amount (eg. on the startup of KVM in a swapless system).
885 * This may throw the system into deep dirty exceeded state and throttle
886 * heavy/light dirtiers alike. To retain good responsiveness, maintain
887 * global_dirty_limit for tracking slowly down to the knocked down dirty
888 * threshold.
889 */
890static void update_dirty_limit(unsigned long thresh, unsigned long dirty)
891{
892 unsigned long limit = global_dirty_limit;
893
894 /*
895 * Follow up in one step.
896 */
897 if (limit < thresh) {
898 limit = thresh;
899 goto update;
900 }
901
902 /*
903 * Follow down slowly. Use the higher one as the target, because thresh
904 * may drop below dirty. This is exactly the reason to introduce
905 * global_dirty_limit which is guaranteed to lie above the dirty pages.
906 */
907 thresh = max(thresh, dirty);
908 if (limit > thresh) {
909 limit -= (limit - thresh) >> 5;
910 goto update;
911 }
912 return;
913update:
914 global_dirty_limit = limit;
915}
916
917static void global_update_bandwidth(unsigned long thresh,
918 unsigned long dirty,
919 unsigned long now)
920{
921 static DEFINE_SPINLOCK(dirty_lock);
922 static unsigned long update_time;
923
924 /*
925 * check locklessly first to optimize away locking for the most time
926 */
927 if (time_before(now, update_time + BANDWIDTH_INTERVAL))
928 return;
929
930 spin_lock(&dirty_lock);
931 if (time_after_eq(now, update_time + BANDWIDTH_INTERVAL)) {
932 update_dirty_limit(thresh, dirty);
933 update_time = now;
934 }
935 spin_unlock(&dirty_lock);
936}
937
Wu Fengguangbe3ffa22011-06-12 10:51:31 -0600938/*
939 * Maintain bdi->dirty_ratelimit, the base dirty throttle rate.
940 *
941 * Normal bdi tasks will be curbed at or below it in long term.
942 * Obviously it should be around (write_bw / N) when there are N dd tasks.
943 */
944static void bdi_update_dirty_ratelimit(struct backing_dev_info *bdi,
945 unsigned long thresh,
946 unsigned long bg_thresh,
947 unsigned long dirty,
948 unsigned long bdi_thresh,
949 unsigned long bdi_dirty,
950 unsigned long dirtied,
951 unsigned long elapsed)
952{
Wu Fengguang73811312011-08-26 15:53:24 -0600953 unsigned long freerun = dirty_freerun_ceiling(thresh, bg_thresh);
954 unsigned long limit = hard_dirty_limit(thresh);
955 unsigned long setpoint = (freerun + limit) / 2;
Wu Fengguangbe3ffa22011-06-12 10:51:31 -0600956 unsigned long write_bw = bdi->avg_write_bandwidth;
957 unsigned long dirty_ratelimit = bdi->dirty_ratelimit;
958 unsigned long dirty_rate;
959 unsigned long task_ratelimit;
960 unsigned long balanced_dirty_ratelimit;
961 unsigned long pos_ratio;
Wu Fengguang73811312011-08-26 15:53:24 -0600962 unsigned long step;
963 unsigned long x;
Wu Fengguangbe3ffa22011-06-12 10:51:31 -0600964
965 /*
966 * The dirty rate will match the writeout rate in long term, except
967 * when dirty pages are truncated by userspace or re-dirtied by FS.
968 */
969 dirty_rate = (dirtied - bdi->dirtied_stamp) * HZ / elapsed;
970
971 pos_ratio = bdi_position_ratio(bdi, thresh, bg_thresh, dirty,
972 bdi_thresh, bdi_dirty);
973 /*
974 * task_ratelimit reflects each dd's dirty rate for the past 200ms.
975 */
976 task_ratelimit = (u64)dirty_ratelimit *
977 pos_ratio >> RATELIMIT_CALC_SHIFT;
978 task_ratelimit++; /* it helps rampup dirty_ratelimit from tiny values */
979
980 /*
981 * A linear estimation of the "balanced" throttle rate. The theory is,
982 * if there are N dd tasks, each throttled at task_ratelimit, the bdi's
983 * dirty_rate will be measured to be (N * task_ratelimit). So the below
984 * formula will yield the balanced rate limit (write_bw / N).
985 *
986 * Note that the expanded form is not a pure rate feedback:
987 * rate_(i+1) = rate_(i) * (write_bw / dirty_rate) (1)
988 * but also takes pos_ratio into account:
989 * rate_(i+1) = rate_(i) * (write_bw / dirty_rate) * pos_ratio (2)
990 *
991 * (1) is not realistic because pos_ratio also takes part in balancing
992 * the dirty rate. Consider the state
993 * pos_ratio = 0.5 (3)
994 * rate = 2 * (write_bw / N) (4)
995 * If (1) is used, it will stuck in that state! Because each dd will
996 * be throttled at
997 * task_ratelimit = pos_ratio * rate = (write_bw / N) (5)
998 * yielding
999 * dirty_rate = N * task_ratelimit = write_bw (6)
1000 * put (6) into (1) we get
1001 * rate_(i+1) = rate_(i) (7)
1002 *
1003 * So we end up using (2) to always keep
1004 * rate_(i+1) ~= (write_bw / N) (8)
1005 * regardless of the value of pos_ratio. As long as (8) is satisfied,
1006 * pos_ratio is able to drive itself to 1.0, which is not only where
1007 * the dirty count meet the setpoint, but also where the slope of
1008 * pos_ratio is most flat and hence task_ratelimit is least fluctuated.
1009 */
1010 balanced_dirty_ratelimit = div_u64((u64)task_ratelimit * write_bw,
1011 dirty_rate | 1);
Wu Fengguangbdaac492011-08-03 14:30:36 -06001012 /*
1013 * balanced_dirty_ratelimit ~= (write_bw / N) <= write_bw
1014 */
1015 if (unlikely(balanced_dirty_ratelimit > write_bw))
1016 balanced_dirty_ratelimit = write_bw;
Wu Fengguangbe3ffa22011-06-12 10:51:31 -06001017
Wu Fengguang73811312011-08-26 15:53:24 -06001018 /*
1019 * We could safely do this and return immediately:
1020 *
1021 * bdi->dirty_ratelimit = balanced_dirty_ratelimit;
1022 *
1023 * However to get a more stable dirty_ratelimit, the below elaborated
1024 * code makes use of task_ratelimit to filter out sigular points and
1025 * limit the step size.
1026 *
1027 * The below code essentially only uses the relative value of
1028 *
1029 * task_ratelimit - dirty_ratelimit
1030 * = (pos_ratio - 1) * dirty_ratelimit
1031 *
1032 * which reflects the direction and size of dirty position error.
1033 */
1034
1035 /*
1036 * dirty_ratelimit will follow balanced_dirty_ratelimit iff
1037 * task_ratelimit is on the same side of dirty_ratelimit, too.
1038 * For example, when
1039 * - dirty_ratelimit > balanced_dirty_ratelimit
1040 * - dirty_ratelimit > task_ratelimit (dirty pages are above setpoint)
1041 * lowering dirty_ratelimit will help meet both the position and rate
1042 * control targets. Otherwise, don't update dirty_ratelimit if it will
1043 * only help meet the rate target. After all, what the users ultimately
1044 * feel and care are stable dirty rate and small position error.
1045 *
1046 * |task_ratelimit - dirty_ratelimit| is used to limit the step size
1047 * and filter out the sigular points of balanced_dirty_ratelimit. Which
1048 * keeps jumping around randomly and can even leap far away at times
1049 * due to the small 200ms estimation period of dirty_rate (we want to
1050 * keep that period small to reduce time lags).
1051 */
1052 step = 0;
Maxim Patlasov459b18d2013-09-11 14:22:46 -07001053
1054 /*
1055 * For strictlimit case, calculations above were based on bdi counters
1056 * and limits (starting from pos_ratio = bdi_position_ratio() and up to
1057 * balanced_dirty_ratelimit = task_ratelimit * write_bw / dirty_rate).
1058 * Hence, to calculate "step" properly, we have to use bdi_dirty as
1059 * "dirty" and bdi_setpoint as "setpoint".
1060 *
1061 * We rampup dirty_ratelimit forcibly if bdi_dirty is low because
1062 * it's possible that bdi_thresh is close to zero due to inactivity
1063 * of backing device (see the implementation of bdi_dirty_limit()).
1064 */
1065 if (unlikely(bdi->capabilities & BDI_CAP_STRICTLIMIT)) {
1066 dirty = bdi_dirty;
1067 if (bdi_dirty < 8)
1068 setpoint = bdi_dirty + 1;
1069 else
1070 setpoint = (bdi_thresh +
1071 bdi_dirty_limit(bdi, bg_thresh)) / 2;
1072 }
1073
Wu Fengguang73811312011-08-26 15:53:24 -06001074 if (dirty < setpoint) {
1075 x = min(bdi->balanced_dirty_ratelimit,
1076 min(balanced_dirty_ratelimit, task_ratelimit));
1077 if (dirty_ratelimit < x)
1078 step = x - dirty_ratelimit;
1079 } else {
1080 x = max(bdi->balanced_dirty_ratelimit,
1081 max(balanced_dirty_ratelimit, task_ratelimit));
1082 if (dirty_ratelimit > x)
1083 step = dirty_ratelimit - x;
1084 }
1085
1086 /*
1087 * Don't pursue 100% rate matching. It's impossible since the balanced
1088 * rate itself is constantly fluctuating. So decrease the track speed
1089 * when it gets close to the target. Helps eliminate pointless tremors.
1090 */
1091 step >>= dirty_ratelimit / (2 * step + 1);
1092 /*
1093 * Limit the tracking speed to avoid overshooting.
1094 */
1095 step = (step + 7) / 8;
1096
1097 if (dirty_ratelimit < balanced_dirty_ratelimit)
1098 dirty_ratelimit += step;
1099 else
1100 dirty_ratelimit -= step;
1101
1102 bdi->dirty_ratelimit = max(dirty_ratelimit, 1UL);
1103 bdi->balanced_dirty_ratelimit = balanced_dirty_ratelimit;
Wu Fengguangb48c1042011-03-02 17:22:49 -06001104
1105 trace_bdi_dirty_ratelimit(bdi, dirty_rate, task_ratelimit);
Wu Fengguangbe3ffa22011-06-12 10:51:31 -06001106}
1107
Wu Fengguange98be2d2010-08-29 11:22:30 -06001108void __bdi_update_bandwidth(struct backing_dev_info *bdi,
Wu Fengguangc42843f2011-03-02 15:54:09 -06001109 unsigned long thresh,
Wu Fengguangaf6a3112011-10-03 20:46:17 -06001110 unsigned long bg_thresh,
Wu Fengguangc42843f2011-03-02 15:54:09 -06001111 unsigned long dirty,
1112 unsigned long bdi_thresh,
1113 unsigned long bdi_dirty,
Wu Fengguange98be2d2010-08-29 11:22:30 -06001114 unsigned long start_time)
1115{
1116 unsigned long now = jiffies;
1117 unsigned long elapsed = now - bdi->bw_time_stamp;
Wu Fengguangbe3ffa22011-06-12 10:51:31 -06001118 unsigned long dirtied;
Wu Fengguange98be2d2010-08-29 11:22:30 -06001119 unsigned long written;
1120
1121 /*
1122 * rate-limit, only update once every 200ms.
1123 */
1124 if (elapsed < BANDWIDTH_INTERVAL)
1125 return;
1126
Wu Fengguangbe3ffa22011-06-12 10:51:31 -06001127 dirtied = percpu_counter_read(&bdi->bdi_stat[BDI_DIRTIED]);
Wu Fengguange98be2d2010-08-29 11:22:30 -06001128 written = percpu_counter_read(&bdi->bdi_stat[BDI_WRITTEN]);
1129
1130 /*
1131 * Skip quiet periods when disk bandwidth is under-utilized.
1132 * (at least 1s idle time between two flusher runs)
1133 */
1134 if (elapsed > HZ && time_before(bdi->bw_time_stamp, start_time))
1135 goto snapshot;
1136
Wu Fengguangbe3ffa22011-06-12 10:51:31 -06001137 if (thresh) {
Wu Fengguangc42843f2011-03-02 15:54:09 -06001138 global_update_bandwidth(thresh, dirty, now);
Wu Fengguangbe3ffa22011-06-12 10:51:31 -06001139 bdi_update_dirty_ratelimit(bdi, thresh, bg_thresh, dirty,
1140 bdi_thresh, bdi_dirty,
1141 dirtied, elapsed);
1142 }
Wu Fengguange98be2d2010-08-29 11:22:30 -06001143 bdi_update_write_bandwidth(bdi, elapsed, written);
1144
1145snapshot:
Wu Fengguangbe3ffa22011-06-12 10:51:31 -06001146 bdi->dirtied_stamp = dirtied;
Wu Fengguange98be2d2010-08-29 11:22:30 -06001147 bdi->written_stamp = written;
1148 bdi->bw_time_stamp = now;
1149}
1150
1151static void bdi_update_bandwidth(struct backing_dev_info *bdi,
Wu Fengguangc42843f2011-03-02 15:54:09 -06001152 unsigned long thresh,
Wu Fengguangaf6a3112011-10-03 20:46:17 -06001153 unsigned long bg_thresh,
Wu Fengguangc42843f2011-03-02 15:54:09 -06001154 unsigned long dirty,
1155 unsigned long bdi_thresh,
1156 unsigned long bdi_dirty,
Wu Fengguange98be2d2010-08-29 11:22:30 -06001157 unsigned long start_time)
1158{
1159 if (time_is_after_eq_jiffies(bdi->bw_time_stamp + BANDWIDTH_INTERVAL))
1160 return;
1161 spin_lock(&bdi->wb.list_lock);
Wu Fengguangaf6a3112011-10-03 20:46:17 -06001162 __bdi_update_bandwidth(bdi, thresh, bg_thresh, dirty,
1163 bdi_thresh, bdi_dirty, start_time);
Wu Fengguange98be2d2010-08-29 11:22:30 -06001164 spin_unlock(&bdi->wb.list_lock);
1165}
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167/*
Wu Fengguang9d823e82011-06-11 18:10:12 -06001168 * After a task dirtied this many pages, balance_dirty_pages_ratelimited_nr()
1169 * will look to see if it needs to start dirty throttling.
1170 *
1171 * If dirty_poll_interval is too low, big NUMA machines will call the expensive
1172 * global_page_state() too often. So scale it near-sqrt to the safety margin
1173 * (the number of pages we may dirty without exceeding the dirty limits).
1174 */
1175static unsigned long dirty_poll_interval(unsigned long dirty,
1176 unsigned long thresh)
1177{
1178 if (thresh > dirty)
1179 return 1UL << (ilog2(thresh - dirty) >> 1);
1180
1181 return 1;
1182}
1183
Wu Fengguang7ccb9ad2011-11-30 11:08:55 -06001184static long bdi_max_pause(struct backing_dev_info *bdi,
1185 unsigned long bdi_dirty)
Wu Fengguangc8462cc2011-06-11 19:21:43 -06001186{
Wu Fengguang7ccb9ad2011-11-30 11:08:55 -06001187 long bw = bdi->avg_write_bandwidth;
1188 long t;
Wu Fengguangc8462cc2011-06-11 19:21:43 -06001189
1190 /*
1191 * Limit pause time for small memory systems. If sleeping for too long
1192 * time, a small pool of dirty/writeback pages may go empty and disk go
1193 * idle.
1194 *
1195 * 8 serves as the safety ratio.
1196 */
Wu Fengguang7ccb9ad2011-11-30 11:08:55 -06001197 t = bdi_dirty / (1 + bw / roundup_pow_of_two(1 + HZ / 8));
1198 t++;
1199
1200 return min_t(long, t, MAX_PAUSE);
1201}
1202
1203static long bdi_min_pause(struct backing_dev_info *bdi,
1204 long max_pause,
1205 unsigned long task_ratelimit,
1206 unsigned long dirty_ratelimit,
1207 int *nr_dirtied_pause)
1208{
1209 long hi = ilog2(bdi->avg_write_bandwidth);
1210 long lo = ilog2(bdi->dirty_ratelimit);
1211 long t; /* target pause */
1212 long pause; /* estimated next pause */
1213 int pages; /* target nr_dirtied_pause */
1214
1215 /* target for 10ms pause on 1-dd case */
1216 t = max(1, HZ / 100);
Wu Fengguangc8462cc2011-06-11 19:21:43 -06001217
1218 /*
Wu Fengguang7ccb9ad2011-11-30 11:08:55 -06001219 * Scale up pause time for concurrent dirtiers in order to reduce CPU
1220 * overheads.
1221 *
1222 * (N * 10ms) on 2^N concurrent tasks.
Wu Fengguangc8462cc2011-06-11 19:21:43 -06001223 */
Wu Fengguang7ccb9ad2011-11-30 11:08:55 -06001224 if (hi > lo)
1225 t += (hi - lo) * (10 * HZ) / 1024;
1226
1227 /*
1228 * This is a bit convoluted. We try to base the next nr_dirtied_pause
1229 * on the much more stable dirty_ratelimit. However the next pause time
1230 * will be computed based on task_ratelimit and the two rate limits may
1231 * depart considerably at some time. Especially if task_ratelimit goes
1232 * below dirty_ratelimit/2 and the target pause is max_pause, the next
1233 * pause time will be max_pause*2 _trimmed down_ to max_pause. As a
1234 * result task_ratelimit won't be executed faithfully, which could
1235 * eventually bring down dirty_ratelimit.
1236 *
1237 * We apply two rules to fix it up:
1238 * 1) try to estimate the next pause time and if necessary, use a lower
1239 * nr_dirtied_pause so as not to exceed max_pause. When this happens,
1240 * nr_dirtied_pause will be "dancing" with task_ratelimit.
1241 * 2) limit the target pause time to max_pause/2, so that the normal
1242 * small fluctuations of task_ratelimit won't trigger rule (1) and
1243 * nr_dirtied_pause will remain as stable as dirty_ratelimit.
1244 */
1245 t = min(t, 1 + max_pause / 2);
1246 pages = dirty_ratelimit * t / roundup_pow_of_two(HZ);
1247
Wu Fengguang5b9b3572011-12-06 13:17:17 -06001248 /*
1249 * Tiny nr_dirtied_pause is found to hurt I/O performance in the test
1250 * case fio-mmap-randwrite-64k, which does 16*{sync read, async write}.
1251 * When the 16 consecutive reads are often interrupted by some dirty
1252 * throttling pause during the async writes, cfq will go into idles
1253 * (deadline is fine). So push nr_dirtied_pause as high as possible
1254 * until reaches DIRTY_POLL_THRESH=32 pages.
1255 */
1256 if (pages < DIRTY_POLL_THRESH) {
1257 t = max_pause;
1258 pages = dirty_ratelimit * t / roundup_pow_of_two(HZ);
1259 if (pages > DIRTY_POLL_THRESH) {
1260 pages = DIRTY_POLL_THRESH;
1261 t = HZ * DIRTY_POLL_THRESH / dirty_ratelimit;
1262 }
1263 }
1264
Wu Fengguang7ccb9ad2011-11-30 11:08:55 -06001265 pause = HZ * pages / (task_ratelimit + 1);
1266 if (pause > max_pause) {
1267 t = max_pause;
1268 pages = task_ratelimit * t / roundup_pow_of_two(HZ);
1269 }
1270
1271 *nr_dirtied_pause = pages;
1272 /*
1273 * The minimal pause time will normally be half the target pause time.
1274 */
Wu Fengguang5b9b3572011-12-06 13:17:17 -06001275 return pages >= DIRTY_POLL_THRESH ? 1 + t / 2 : t;
Wu Fengguangc8462cc2011-06-11 19:21:43 -06001276}
1277
Maxim Patlasov459b18d2013-09-11 14:22:46 -07001278static inline void bdi_dirty_limits(struct backing_dev_info *bdi,
1279 unsigned long dirty_thresh,
1280 unsigned long background_thresh,
1281 unsigned long *bdi_dirty,
1282 unsigned long *bdi_thresh,
1283 unsigned long *bdi_bg_thresh)
1284{
1285 unsigned long bdi_reclaimable;
1286
1287 /*
1288 * bdi_thresh is not treated as some limiting factor as
1289 * dirty_thresh, due to reasons
1290 * - in JBOD setup, bdi_thresh can fluctuate a lot
1291 * - in a system with HDD and USB key, the USB key may somehow
1292 * go into state (bdi_dirty >> bdi_thresh) either because
1293 * bdi_dirty starts high, or because bdi_thresh drops low.
1294 * In this case we don't want to hard throttle the USB key
1295 * dirtiers for 100 seconds until bdi_dirty drops under
1296 * bdi_thresh. Instead the auxiliary bdi control line in
1297 * bdi_position_ratio() will let the dirtier task progress
1298 * at some rate <= (write_bw / 2) for bringing down bdi_dirty.
1299 */
1300 *bdi_thresh = bdi_dirty_limit(bdi, dirty_thresh);
1301
1302 if (bdi_bg_thresh)
1303 *bdi_bg_thresh = div_u64((u64)*bdi_thresh *
1304 background_thresh,
1305 dirty_thresh);
1306
1307 /*
1308 * In order to avoid the stacked BDI deadlock we need
1309 * to ensure we accurately count the 'dirty' pages when
1310 * the threshold is low.
1311 *
1312 * Otherwise it would be possible to get thresh+n pages
1313 * reported dirty, even though there are thresh-m pages
1314 * actually dirty; with m+n sitting in the percpu
1315 * deltas.
1316 */
1317 if (*bdi_thresh < 2 * bdi_stat_error(bdi)) {
1318 bdi_reclaimable = bdi_stat_sum(bdi, BDI_RECLAIMABLE);
1319 *bdi_dirty = bdi_reclaimable +
1320 bdi_stat_sum(bdi, BDI_WRITEBACK);
1321 } else {
1322 bdi_reclaimable = bdi_stat(bdi, BDI_RECLAIMABLE);
1323 *bdi_dirty = bdi_reclaimable +
1324 bdi_stat(bdi, BDI_WRITEBACK);
1325 }
1326}
1327
Wu Fengguang9d823e82011-06-11 18:10:12 -06001328/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 * balance_dirty_pages() must be called by processes which are generating dirty
1330 * data. It looks at the number of dirty pages in the machine and will force
Wu Fengguang143dfe82010-08-27 18:45:12 -06001331 * the caller to wait once crossing the (background_thresh + dirty_thresh) / 2.
Jens Axboe5b0830c2009-09-23 19:37:09 +02001332 * If we're over `background_thresh' then the writeback threads are woken to
1333 * perform some writeout.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 */
Wu Fengguang3a2e9a52009-09-23 21:56:00 +08001335static void balance_dirty_pages(struct address_space *mapping,
Wu Fengguang143dfe82010-08-27 18:45:12 -06001336 unsigned long pages_dirtied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Wu Fengguang143dfe82010-08-27 18:45:12 -06001338 unsigned long nr_reclaimable; /* = file_dirty + unstable_nfs */
Wu Fengguang77627412010-09-12 13:34:05 -06001339 unsigned long nr_dirty; /* = file_dirty + writeback + unstable_nfs */
David Rientjes364aeb22009-01-06 14:39:29 -08001340 unsigned long background_thresh;
1341 unsigned long dirty_thresh;
Wu Fengguang83712352011-06-11 19:25:42 -06001342 long period;
Wu Fengguang7ccb9ad2011-11-30 11:08:55 -06001343 long pause;
1344 long max_pause;
1345 long min_pause;
1346 int nr_dirtied_pause;
Wu Fengguange50e3722010-08-11 14:17:37 -07001347 bool dirty_exceeded = false;
Wu Fengguang143dfe82010-08-27 18:45:12 -06001348 unsigned long task_ratelimit;
Wu Fengguang7ccb9ad2011-11-30 11:08:55 -06001349 unsigned long dirty_ratelimit;
Wu Fengguang143dfe82010-08-27 18:45:12 -06001350 unsigned long pos_ratio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 struct backing_dev_info *bdi = mapping->backing_dev_info;
Maxim Patlasov459b18d2013-09-11 14:22:46 -07001352 bool strictlimit = bdi->capabilities & BDI_CAP_STRICTLIMIT;
Wu Fengguange98be2d2010-08-29 11:22:30 -06001353 unsigned long start_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 for (;;) {
Wu Fengguang83712352011-06-11 19:25:42 -06001356 unsigned long now = jiffies;
Maxim Patlasov459b18d2013-09-11 14:22:46 -07001357 unsigned long uninitialized_var(bdi_thresh);
1358 unsigned long thresh;
1359 unsigned long uninitialized_var(bdi_dirty);
1360 unsigned long dirty;
1361 unsigned long bg_thresh;
Wu Fengguang83712352011-06-11 19:25:42 -06001362
Wu Fengguang143dfe82010-08-27 18:45:12 -06001363 /*
1364 * Unstable writes are a feature of certain networked
1365 * filesystems (i.e. NFS) in which data may have been
1366 * written to the server's write cache, but has not yet
1367 * been flushed to permanent storage.
1368 */
Peter Zijlstra5fce25a2007-11-14 16:59:15 -08001369 nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
1370 global_page_state(NR_UNSTABLE_NFS);
Wu Fengguang77627412010-09-12 13:34:05 -06001371 nr_dirty = nr_reclaimable + global_page_state(NR_WRITEBACK);
Peter Zijlstra5fce25a2007-11-14 16:59:15 -08001372
Wu Fengguang16c40422010-08-11 14:17:39 -07001373 global_dirty_limits(&background_thresh, &dirty_thresh);
1374
Maxim Patlasov459b18d2013-09-11 14:22:46 -07001375 if (unlikely(strictlimit)) {
1376 bdi_dirty_limits(bdi, dirty_thresh, background_thresh,
1377 &bdi_dirty, &bdi_thresh, &bg_thresh);
1378
1379 dirty = bdi_dirty;
1380 thresh = bdi_thresh;
1381 } else {
1382 dirty = nr_dirty;
1383 thresh = dirty_thresh;
1384 bg_thresh = background_thresh;
1385 }
1386
Wu Fengguang16c40422010-08-11 14:17:39 -07001387 /*
1388 * Throttle it only when the background writeback cannot
1389 * catch-up. This avoids (excessively) small writeouts
Maxim Patlasov459b18d2013-09-11 14:22:46 -07001390 * when the bdi limits are ramping up in case of !strictlimit.
1391 *
1392 * In strictlimit case make decision based on the bdi counters
1393 * and limits. Small writeouts when the bdi limits are ramping
1394 * up are the price we consciously pay for strictlimit-ing.
Wu Fengguang16c40422010-08-11 14:17:39 -07001395 */
Maxim Patlasov459b18d2013-09-11 14:22:46 -07001396 if (dirty <= dirty_freerun_ceiling(thresh, bg_thresh)) {
Wu Fengguang83712352011-06-11 19:25:42 -06001397 current->dirty_paused_when = now;
1398 current->nr_dirtied = 0;
Wu Fengguang7ccb9ad2011-11-30 11:08:55 -06001399 current->nr_dirtied_pause =
Maxim Patlasov459b18d2013-09-11 14:22:46 -07001400 dirty_poll_interval(dirty, thresh);
Wu Fengguang16c40422010-08-11 14:17:39 -07001401 break;
Wu Fengguang83712352011-06-11 19:25:42 -06001402 }
Wu Fengguang16c40422010-08-11 14:17:39 -07001403
Wu Fengguang143dfe82010-08-27 18:45:12 -06001404 if (unlikely(!writeback_in_progress(bdi)))
1405 bdi_start_background_writeback(bdi);
1406
Maxim Patlasov459b18d2013-09-11 14:22:46 -07001407 if (!strictlimit)
1408 bdi_dirty_limits(bdi, dirty_thresh, background_thresh,
1409 &bdi_dirty, &bdi_thresh, NULL);
Peter Zijlstra5fce25a2007-11-14 16:59:15 -08001410
Wu Fengguang82791942011-12-03 21:26:01 -06001411 dirty_exceeded = (bdi_dirty > bdi_thresh) &&
Maxim Patlasov459b18d2013-09-11 14:22:46 -07001412 ((nr_dirty > dirty_thresh) || strictlimit);
Wu Fengguang143dfe82010-08-27 18:45:12 -06001413 if (dirty_exceeded && !bdi->dirty_exceeded)
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -07001414 bdi->dirty_exceeded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Wu Fengguangaf6a3112011-10-03 20:46:17 -06001416 bdi_update_bandwidth(bdi, dirty_thresh, background_thresh,
1417 nr_dirty, bdi_thresh, bdi_dirty,
1418 start_time);
Wu Fengguange98be2d2010-08-29 11:22:30 -06001419
Wu Fengguang143dfe82010-08-27 18:45:12 -06001420 dirty_ratelimit = bdi->dirty_ratelimit;
1421 pos_ratio = bdi_position_ratio(bdi, dirty_thresh,
1422 background_thresh, nr_dirty,
1423 bdi_thresh, bdi_dirty);
Wu Fengguang3a73dbb2011-11-07 19:19:28 +08001424 task_ratelimit = ((u64)dirty_ratelimit * pos_ratio) >>
1425 RATELIMIT_CALC_SHIFT;
Wu Fengguang7ccb9ad2011-11-30 11:08:55 -06001426 max_pause = bdi_max_pause(bdi, bdi_dirty);
1427 min_pause = bdi_min_pause(bdi, max_pause,
1428 task_ratelimit, dirty_ratelimit,
1429 &nr_dirtied_pause);
1430
Wu Fengguang3a73dbb2011-11-07 19:19:28 +08001431 if (unlikely(task_ratelimit == 0)) {
Wu Fengguang83712352011-06-11 19:25:42 -06001432 period = max_pause;
Wu Fengguangc8462cc2011-06-11 19:21:43 -06001433 pause = max_pause;
Wu Fengguang143dfe82010-08-27 18:45:12 -06001434 goto pause;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
Wu Fengguang83712352011-06-11 19:25:42 -06001436 period = HZ * pages_dirtied / task_ratelimit;
1437 pause = period;
1438 if (current->dirty_paused_when)
1439 pause -= now - current->dirty_paused_when;
1440 /*
1441 * For less than 1s think time (ext3/4 may block the dirtier
1442 * for up to 800ms from time to time on 1-HDD; so does xfs,
1443 * however at much less frequency), try to compensate it in
1444 * future periods by updating the virtual time; otherwise just
1445 * do a reset, as it may be a light dirtier.
1446 */
Wu Fengguang7ccb9ad2011-11-30 11:08:55 -06001447 if (pause < min_pause) {
Wu Fengguangece13ac2010-08-29 23:33:20 -06001448 trace_balance_dirty_pages(bdi,
1449 dirty_thresh,
1450 background_thresh,
1451 nr_dirty,
1452 bdi_thresh,
1453 bdi_dirty,
1454 dirty_ratelimit,
1455 task_ratelimit,
1456 pages_dirtied,
Wu Fengguang83712352011-06-11 19:25:42 -06001457 period,
Wu Fengguang7ccb9ad2011-11-30 11:08:55 -06001458 min(pause, 0L),
Wu Fengguangece13ac2010-08-29 23:33:20 -06001459 start_time);
Wu Fengguang83712352011-06-11 19:25:42 -06001460 if (pause < -HZ) {
1461 current->dirty_paused_when = now;
1462 current->nr_dirtied = 0;
1463 } else if (period) {
1464 current->dirty_paused_when += period;
1465 current->nr_dirtied = 0;
Wu Fengguang7ccb9ad2011-11-30 11:08:55 -06001466 } else if (current->nr_dirtied_pause <= pages_dirtied)
1467 current->nr_dirtied_pause += pages_dirtied;
Wu Fengguang57fc9782011-06-11 19:32:32 -06001468 break;
1469 }
Wu Fengguang7ccb9ad2011-11-30 11:08:55 -06001470 if (unlikely(pause > max_pause)) {
1471 /* for occasional dropped task_ratelimit */
1472 now += min(pause - max_pause, max_pause);
1473 pause = max_pause;
1474 }
Wu Fengguang143dfe82010-08-27 18:45:12 -06001475
1476pause:
Wu Fengguangece13ac2010-08-29 23:33:20 -06001477 trace_balance_dirty_pages(bdi,
1478 dirty_thresh,
1479 background_thresh,
1480 nr_dirty,
1481 bdi_thresh,
1482 bdi_dirty,
1483 dirty_ratelimit,
1484 task_ratelimit,
1485 pages_dirtied,
Wu Fengguang83712352011-06-11 19:25:42 -06001486 period,
Wu Fengguangece13ac2010-08-29 23:33:20 -06001487 pause,
1488 start_time);
Jan Kara499d05e2011-11-16 19:34:48 +08001489 __set_current_state(TASK_KILLABLE);
Wu Fengguangd25105e2009-10-09 12:40:42 +02001490 io_schedule_timeout(pause);
Jens Axboe87c6a9b2009-09-17 19:59:14 +02001491
Wu Fengguang83712352011-06-11 19:25:42 -06001492 current->dirty_paused_when = now + pause;
1493 current->nr_dirtied = 0;
Wu Fengguang7ccb9ad2011-11-30 11:08:55 -06001494 current->nr_dirtied_pause = nr_dirtied_pause;
Wu Fengguang83712352011-06-11 19:25:42 -06001495
Wu Fengguangffd1f602011-06-19 22:18:42 -06001496 /*
Wu Fengguang1df64712011-11-13 19:47:32 -06001497 * This is typically equal to (nr_dirty < dirty_thresh) and can
1498 * also keep "1000+ dd on a slow USB stick" under control.
Wu Fengguangffd1f602011-06-19 22:18:42 -06001499 */
Wu Fengguang1df64712011-11-13 19:47:32 -06001500 if (task_ratelimit)
Wu Fengguangffd1f602011-06-19 22:18:42 -06001501 break;
Jan Kara499d05e2011-11-16 19:34:48 +08001502
Wu Fengguangc5c63432011-12-02 10:21:33 -06001503 /*
1504 * In the case of an unresponding NFS server and the NFS dirty
1505 * pages exceeds dirty_thresh, give the other good bdi's a pipe
1506 * to go through, so that tasks on them still remain responsive.
1507 *
1508 * In theory 1 page is enough to keep the comsumer-producer
1509 * pipe going: the flusher cleans 1 page => the task dirties 1
1510 * more page. However bdi_dirty has accounting errors. So use
1511 * the larger and more IO friendly bdi_stat_error.
1512 */
1513 if (bdi_dirty <= bdi_stat_error(bdi))
1514 break;
1515
Jan Kara499d05e2011-11-16 19:34:48 +08001516 if (fatal_signal_pending(current))
1517 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
1519
Wu Fengguang143dfe82010-08-27 18:45:12 -06001520 if (!dirty_exceeded && bdi->dirty_exceeded)
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -07001521 bdi->dirty_exceeded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 if (writeback_in_progress(bdi))
Jens Axboe5b0830c2009-09-23 19:37:09 +02001524 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 /*
1527 * In laptop mode, we wait until hitting the higher threshold before
1528 * starting background writeout, and then write out all the way down
1529 * to the lower threshold. So slow writers cause minimal disk activity.
1530 *
1531 * In normal mode, we start background writeout at the lower
1532 * background_thresh, to keep the amount of dirty memory low.
1533 */
Wu Fengguang143dfe82010-08-27 18:45:12 -06001534 if (laptop_mode)
1535 return;
1536
1537 if (nr_reclaimable > background_thresh)
Christoph Hellwigc5444192010-06-08 18:15:15 +02001538 bdi_start_background_writeback(bdi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
Peter Zijlstraa200ee12007-10-08 18:54:37 +02001541void set_page_dirty_balance(struct page *page, int page_mkwrite)
Peter Zijlstraedc79b22006-09-25 23:30:58 -07001542{
Peter Zijlstraa200ee12007-10-08 18:54:37 +02001543 if (set_page_dirty(page) || page_mkwrite) {
Peter Zijlstraedc79b22006-09-25 23:30:58 -07001544 struct address_space *mapping = page_mapping(page);
1545
1546 if (mapping)
1547 balance_dirty_pages_ratelimited(mapping);
1548 }
1549}
1550
Wu Fengguang9d823e82011-06-11 18:10:12 -06001551static DEFINE_PER_CPU(int, bdp_ratelimits);
Tejun Heo245b2e72009-06-24 15:13:48 +09001552
Wu Fengguang54848d72011-04-05 13:21:19 -06001553/*
1554 * Normal tasks are throttled by
1555 * loop {
1556 * dirty tsk->nr_dirtied_pause pages;
1557 * take a snap in balance_dirty_pages();
1558 * }
1559 * However there is a worst case. If every task exit immediately when dirtied
1560 * (tsk->nr_dirtied_pause - 1) pages, balance_dirty_pages() will never be
1561 * called to throttle the page dirties. The solution is to save the not yet
1562 * throttled page dirties in dirty_throttle_leaks on task exit and charge them
1563 * randomly into the running tasks. This works well for the above worst case,
1564 * as the new task will pick up and accumulate the old task's leaked dirty
1565 * count and eventually get throttled.
1566 */
1567DEFINE_PER_CPU(int, dirty_throttle_leaks) = 0;
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569/**
Andrew Mortonfa5a7342006-03-24 03:18:10 -08001570 * balance_dirty_pages_ratelimited_nr - balance dirty memory state
Martin Waitz67be2dd2005-05-01 08:59:26 -07001571 * @mapping: address_space which was dirtied
Martin Waitza5802902006-04-02 13:59:55 +02001572 * @nr_pages_dirtied: number of pages which the caller has just dirtied
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 *
1574 * Processes which are dirtying memory should call in here once for each page
1575 * which was newly dirtied. The function will periodically check the system's
1576 * dirty state and will initiate writeback if needed.
1577 *
1578 * On really big machines, get_writeback_state is expensive, so try to avoid
1579 * calling it too often (ratelimiting). But once we're over the dirty memory
1580 * limit we decrease the ratelimiting by a lot, to prevent individual processes
1581 * from overshooting the limit by (ratelimit_pages) each.
1582 */
Andrew Mortonfa5a7342006-03-24 03:18:10 -08001583void balance_dirty_pages_ratelimited_nr(struct address_space *mapping,
1584 unsigned long nr_pages_dirtied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Wu Fengguang36715ce2011-06-11 17:53:57 -06001586 struct backing_dev_info *bdi = mapping->backing_dev_info;
Wu Fengguang9d823e82011-06-11 18:10:12 -06001587 int ratelimit;
1588 int *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Wu Fengguang36715ce2011-06-11 17:53:57 -06001590 if (!bdi_cap_account_dirty(bdi))
1591 return;
1592
Wu Fengguang9d823e82011-06-11 18:10:12 -06001593 ratelimit = current->nr_dirtied_pause;
1594 if (bdi->dirty_exceeded)
1595 ratelimit = min(ratelimit, 32 >> (PAGE_SHIFT - 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Andrew Mortonfa5a7342006-03-24 03:18:10 -08001597 preempt_disable();
Wu Fengguang9d823e82011-06-11 18:10:12 -06001598 /*
1599 * This prevents one CPU to accumulate too many dirtied pages without
1600 * calling into balance_dirty_pages(), which can happen when there are
1601 * 1000+ tasks, all of them start dirtying pages at exactly the same
1602 * time, hence all honoured too large initial task->nr_dirtied_pause.
1603 */
Tejun Heo245b2e72009-06-24 15:13:48 +09001604 p = &__get_cpu_var(bdp_ratelimits);
Wu Fengguang9d823e82011-06-11 18:10:12 -06001605 if (unlikely(current->nr_dirtied >= ratelimit))
Andrew Mortonfa5a7342006-03-24 03:18:10 -08001606 *p = 0;
Wu Fengguangd3bc1fe2011-04-14 07:52:37 -06001607 else if (unlikely(*p >= ratelimit_pages)) {
1608 *p = 0;
1609 ratelimit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
Wu Fengguang54848d72011-04-05 13:21:19 -06001611 /*
1612 * Pick up the dirtied pages by the exited tasks. This avoids lots of
1613 * short-lived tasks (eg. gcc invocations in a kernel build) escaping
1614 * the dirty throttling and livelock other long-run dirtiers.
1615 */
1616 p = &__get_cpu_var(dirty_throttle_leaks);
1617 if (*p > 0 && current->nr_dirtied < ratelimit) {
1618 nr_pages_dirtied = min(*p, ratelimit - current->nr_dirtied);
1619 *p -= nr_pages_dirtied;
1620 current->nr_dirtied += nr_pages_dirtied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
Andrew Mortonfa5a7342006-03-24 03:18:10 -08001622 preempt_enable();
Wu Fengguang9d823e82011-06-11 18:10:12 -06001623
1624 if (unlikely(current->nr_dirtied >= ratelimit))
1625 balance_dirty_pages(mapping, current->nr_dirtied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
Andrew Mortonfa5a7342006-03-24 03:18:10 -08001627EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Andrew Morton232ea4d2007-02-28 20:13:21 -08001629void throttle_vm_writeout(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
David Rientjes364aeb22009-01-06 14:39:29 -08001631 unsigned long background_thresh;
1632 unsigned long dirty_thresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 for ( ; ; ) {
Wu Fengguang16c40422010-08-11 14:17:39 -07001635 global_dirty_limits(&background_thresh, &dirty_thresh);
Fengguang Wu47a13332012-03-21 16:34:09 -07001636 dirty_thresh = hard_dirty_limit(dirty_thresh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 /*
1639 * Boost the allowable dirty threshold a bit for page
1640 * allocators so they don't get DoS'ed by heavy writers
1641 */
1642 dirty_thresh += dirty_thresh / 10; /* wheeee... */
1643
Christoph Lameterc24f21b2006-06-30 01:55:42 -07001644 if (global_page_state(NR_UNSTABLE_NFS) +
1645 global_page_state(NR_WRITEBACK) <= dirty_thresh)
1646 break;
Jens Axboe8aa7e842009-07-09 14:52:32 +02001647 congestion_wait(BLK_RW_ASYNC, HZ/10);
Fengguang Wu369f2382007-10-16 23:30:45 -07001648
1649 /*
1650 * The caller might hold locks which can prevent IO completion
1651 * or progress in the filesystem. So we cannot just sit here
1652 * waiting for IO to complete.
1653 */
1654 if ((gfp_mask & (__GFP_FS|__GFP_IO)) != (__GFP_FS|__GFP_IO))
1655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
1657}
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs
1661 */
1662int dirty_writeback_centisecs_handler(ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001663 void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001665 proc_dointvec(table, write, buffer, length, ppos);
Jens Axboe64231042010-05-21 20:00:35 +02001666 bdi_arm_supers_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 return 0;
1668}
1669
Jens Axboec2c49862010-05-20 09:18:47 +02001670#ifdef CONFIG_BLOCK
Matthew Garrett31373d02010-04-06 14:25:14 +02001671void laptop_mode_timer_fn(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
Matthew Garrett31373d02010-04-06 14:25:14 +02001673 struct request_queue *q = (struct request_queue *)data;
1674 int nr_pages = global_page_state(NR_FILE_DIRTY) +
1675 global_page_state(NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Matthew Garrett31373d02010-04-06 14:25:14 +02001677 /*
1678 * We want to write everything out, not just down to the dirty
1679 * threshold
1680 */
Matthew Garrett31373d02010-04-06 14:25:14 +02001681 if (bdi_has_dirty_io(&q->backing_dev_info))
Curt Wohlgemuth0e175a12011-10-07 21:54:10 -06001682 bdi_start_writeback(&q->backing_dev_info, nr_pages,
1683 WB_REASON_LAPTOP_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
1686/*
1687 * We've spun up the disk and we're in laptop mode: schedule writeback
1688 * of all dirty data a few seconds from now. If the flush is already scheduled
1689 * then push it back - the user is still using the disk.
1690 */
Matthew Garrett31373d02010-04-06 14:25:14 +02001691void laptop_io_completion(struct backing_dev_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Matthew Garrett31373d02010-04-06 14:25:14 +02001693 mod_timer(&info->laptop_mode_wb_timer, jiffies + laptop_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
1696/*
1697 * We're in laptop mode and we've just synced. The sync's writes will have
1698 * caused another writeback to be scheduled by laptop_io_completion.
1699 * Nothing needs to be written back anymore, so we unschedule the writeback.
1700 */
1701void laptop_sync_completion(void)
1702{
Matthew Garrett31373d02010-04-06 14:25:14 +02001703 struct backing_dev_info *bdi;
1704
1705 rcu_read_lock();
1706
1707 list_for_each_entry_rcu(bdi, &bdi_list, bdi_list)
1708 del_timer(&bdi->laptop_mode_wb_timer);
1709
1710 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
Jens Axboec2c49862010-05-20 09:18:47 +02001712#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714/*
1715 * If ratelimit_pages is too high then we can get into dirty-data overload
1716 * if a large number of processes all perform writes at the same time.
1717 * If it is too low then SMP machines will call the (expensive)
1718 * get_writeback_state too often.
1719 *
1720 * Here we set ratelimit_pages to a level which ensures that when all CPUs are
1721 * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
Wu Fengguang9d823e82011-06-11 18:10:12 -06001722 * thresholds.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 */
1724
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -07001725void writeback_set_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Wu Fengguang9d823e82011-06-11 18:10:12 -06001727 unsigned long background_thresh;
1728 unsigned long dirty_thresh;
1729 global_dirty_limits(&background_thresh, &dirty_thresh);
1730 ratelimit_pages = dirty_thresh / (num_online_cpus() * 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (ratelimit_pages < 16)
1732 ratelimit_pages = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
Chandra Seetharaman26c21432006-06-27 02:54:10 -07001735static int __cpuinit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736ratelimit_handler(struct notifier_block *self, unsigned long u, void *v)
1737{
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -07001738 writeback_set_ratelimit();
Paul E. McKenneyaa0f0302007-02-10 01:46:37 -08001739 return NOTIFY_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
1741
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001742static struct notifier_block __cpuinitdata ratelimit_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 .notifier_call = ratelimit_handler,
1744 .next = NULL,
1745};
1746
1747/*
Linus Torvaldsdc6e29d2007-01-29 16:37:38 -08001748 * Called early on to tune the page writeback dirty limits.
1749 *
1750 * We used to scale dirty pages according to how total memory
1751 * related to pages that could be allocated for buffers (by
1752 * comparing nr_free_buffer_pages() to vm_total_pages.
1753 *
1754 * However, that was when we used "dirty_ratio" to scale with
1755 * all memory, and we don't do that any more. "dirty_ratio"
1756 * is now applied to total non-HIGHPAGE memory (by subtracting
1757 * totalhigh_pages from vm_total_pages), and as such we can't
1758 * get into the old insane situation any more where we had
1759 * large amounts of dirty pages compared to a small amount of
1760 * non-HIGHMEM memory.
1761 *
1762 * But we might still want to scale the dirty_ratio by how
1763 * much memory the box has..
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 */
1765void __init page_writeback_init(void)
1766{
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -07001767 int shift;
1768
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -07001769 writeback_set_ratelimit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 register_cpu_notifier(&ratelimit_nb);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -07001771
1772 shift = calc_period_shift();
1773 prop_descriptor_init(&vm_completions, shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
David Howells811d7362006-08-29 19:06:09 +01001776/**
Jan Karaf446daa2010-08-09 17:19:12 -07001777 * tag_pages_for_writeback - tag pages to be written by write_cache_pages
1778 * @mapping: address space structure to write
1779 * @start: starting page index
1780 * @end: ending page index (inclusive)
1781 *
1782 * This function scans the page range from @start to @end (inclusive) and tags
1783 * all pages that have DIRTY tag set with a special TOWRITE tag. The idea is
1784 * that write_cache_pages (or whoever calls this function) will then use
1785 * TOWRITE tag to identify pages eligible for writeback. This mechanism is
1786 * used to avoid livelocking of writeback by a process steadily creating new
1787 * dirty pages in the file (thus it is important for this function to be quick
1788 * so that it can tag pages faster than a dirtying process can create them).
1789 */
1790/*
1791 * We tag pages in batches of WRITEBACK_TAG_BATCH to reduce tree_lock latency.
1792 */
Jan Karaf446daa2010-08-09 17:19:12 -07001793void tag_pages_for_writeback(struct address_space *mapping,
1794 pgoff_t start, pgoff_t end)
1795{
Randy Dunlap3c111a02010-08-11 14:17:30 -07001796#define WRITEBACK_TAG_BATCH 4096
Jan Karaf446daa2010-08-09 17:19:12 -07001797 unsigned long tagged;
1798
1799 do {
1800 spin_lock_irq(&mapping->tree_lock);
1801 tagged = radix_tree_range_tag_if_tagged(&mapping->page_tree,
1802 &start, end, WRITEBACK_TAG_BATCH,
1803 PAGECACHE_TAG_DIRTY, PAGECACHE_TAG_TOWRITE);
1804 spin_unlock_irq(&mapping->tree_lock);
1805 WARN_ON_ONCE(tagged > WRITEBACK_TAG_BATCH);
1806 cond_resched();
Jan Karad5ed3a42010-08-19 14:13:33 -07001807 /* We check 'start' to handle wrapping when end == ~0UL */
1808 } while (tagged >= WRITEBACK_TAG_BATCH && start);
Jan Karaf446daa2010-08-09 17:19:12 -07001809}
1810EXPORT_SYMBOL(tag_pages_for_writeback);
1811
1812/**
Miklos Szeredi0ea97182007-05-10 22:22:51 -07001813 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
David Howells811d7362006-08-29 19:06:09 +01001814 * @mapping: address space structure to write
1815 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
Miklos Szeredi0ea97182007-05-10 22:22:51 -07001816 * @writepage: function called for each page
1817 * @data: data passed to writepage function
David Howells811d7362006-08-29 19:06:09 +01001818 *
Miklos Szeredi0ea97182007-05-10 22:22:51 -07001819 * If a page is already under I/O, write_cache_pages() skips it, even
David Howells811d7362006-08-29 19:06:09 +01001820 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
1821 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
1822 * and msync() need to guarantee that all the data which was dirty at the time
1823 * the call was made get new I/O started against them. If wbc->sync_mode is
1824 * WB_SYNC_ALL then we were called for data integrity and we must wait for
1825 * existing IO to complete.
Jan Karaf446daa2010-08-09 17:19:12 -07001826 *
1827 * To avoid livelocks (when other process dirties new pages), we first tag
1828 * pages which should be written back with TOWRITE tag and only then start
1829 * writing them. For data-integrity sync we have to be careful so that we do
1830 * not miss some pages (e.g., because some other process has cleared TOWRITE
1831 * tag we set). The rule we follow is that TOWRITE tag can be cleared only
1832 * by the process clearing the DIRTY tag (and submitting the page for IO).
David Howells811d7362006-08-29 19:06:09 +01001833 */
Miklos Szeredi0ea97182007-05-10 22:22:51 -07001834int write_cache_pages(struct address_space *mapping,
1835 struct writeback_control *wbc, writepage_t writepage,
1836 void *data)
David Howells811d7362006-08-29 19:06:09 +01001837{
David Howells811d7362006-08-29 19:06:09 +01001838 int ret = 0;
1839 int done = 0;
David Howells811d7362006-08-29 19:06:09 +01001840 struct pagevec pvec;
1841 int nr_pages;
Nick Piggin31a12662009-01-06 14:39:04 -08001842 pgoff_t uninitialized_var(writeback_index);
David Howells811d7362006-08-29 19:06:09 +01001843 pgoff_t index;
1844 pgoff_t end; /* Inclusive */
Nick Pigginbd19e012009-01-06 14:39:06 -08001845 pgoff_t done_index;
Nick Piggin31a12662009-01-06 14:39:04 -08001846 int cycled;
David Howells811d7362006-08-29 19:06:09 +01001847 int range_whole = 0;
Jan Karaf446daa2010-08-09 17:19:12 -07001848 int tag;
David Howells811d7362006-08-29 19:06:09 +01001849
David Howells811d7362006-08-29 19:06:09 +01001850 pagevec_init(&pvec, 0);
1851 if (wbc->range_cyclic) {
Nick Piggin31a12662009-01-06 14:39:04 -08001852 writeback_index = mapping->writeback_index; /* prev offset */
1853 index = writeback_index;
1854 if (index == 0)
1855 cycled = 1;
1856 else
1857 cycled = 0;
David Howells811d7362006-08-29 19:06:09 +01001858 end = -1;
1859 } else {
1860 index = wbc->range_start >> PAGE_CACHE_SHIFT;
1861 end = wbc->range_end >> PAGE_CACHE_SHIFT;
1862 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1863 range_whole = 1;
Nick Piggin31a12662009-01-06 14:39:04 -08001864 cycled = 1; /* ignore range_cyclic tests */
David Howells811d7362006-08-29 19:06:09 +01001865 }
Wu Fengguang6e6938b2010-06-06 10:38:15 -06001866 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Jan Karaf446daa2010-08-09 17:19:12 -07001867 tag = PAGECACHE_TAG_TOWRITE;
1868 else
1869 tag = PAGECACHE_TAG_DIRTY;
David Howells811d7362006-08-29 19:06:09 +01001870retry:
Wu Fengguang6e6938b2010-06-06 10:38:15 -06001871 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Jan Karaf446daa2010-08-09 17:19:12 -07001872 tag_pages_for_writeback(mapping, index, end);
Nick Pigginbd19e012009-01-06 14:39:06 -08001873 done_index = index;
Nick Piggin5a3d5c92009-01-06 14:39:09 -08001874 while (!done && (index <= end)) {
1875 int i;
1876
Jan Karaf446daa2010-08-09 17:19:12 -07001877 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
Nick Piggin5a3d5c92009-01-06 14:39:09 -08001878 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1879 if (nr_pages == 0)
1880 break;
David Howells811d7362006-08-29 19:06:09 +01001881
David Howells811d7362006-08-29 19:06:09 +01001882 for (i = 0; i < nr_pages; i++) {
1883 struct page *page = pvec.pages[i];
1884
Nick Piggind5482cd2009-01-06 14:39:11 -08001885 /*
1886 * At this point, the page may be truncated or
1887 * invalidated (changing page->mapping to NULL), or
1888 * even swizzled back from swapper_space to tmpfs file
1889 * mapping. However, page->index will not change
1890 * because we have a reference on the page.
1891 */
1892 if (page->index > end) {
1893 /*
1894 * can't be range_cyclic (1st pass) because
1895 * end == -1 in that case.
1896 */
1897 done = 1;
1898 break;
1899 }
1900
Jun'ichi Nomuracf15b072011-03-22 16:33:40 -07001901 done_index = page->index;
Nick Pigginbd19e012009-01-06 14:39:06 -08001902
David Howells811d7362006-08-29 19:06:09 +01001903 lock_page(page);
1904
Nick Piggin5a3d5c92009-01-06 14:39:09 -08001905 /*
1906 * Page truncated or invalidated. We can freely skip it
1907 * then, even for data integrity operations: the page
1908 * has disappeared concurrently, so there could be no
1909 * real expectation of this data interity operation
1910 * even if there is now a new, dirty page at the same
1911 * pagecache address.
1912 */
David Howells811d7362006-08-29 19:06:09 +01001913 if (unlikely(page->mapping != mapping)) {
Nick Piggin5a3d5c92009-01-06 14:39:09 -08001914continue_unlock:
David Howells811d7362006-08-29 19:06:09 +01001915 unlock_page(page);
1916 continue;
1917 }
1918
Nick Piggin515f4a02009-01-06 14:39:10 -08001919 if (!PageDirty(page)) {
1920 /* someone wrote it for us */
1921 goto continue_unlock;
1922 }
David Howells811d7362006-08-29 19:06:09 +01001923
Nick Piggin515f4a02009-01-06 14:39:10 -08001924 if (PageWriteback(page)) {
1925 if (wbc->sync_mode != WB_SYNC_NONE)
1926 wait_on_page_writeback(page);
1927 else
1928 goto continue_unlock;
1929 }
1930
1931 BUG_ON(PageWriteback(page));
1932 if (!clear_page_dirty_for_io(page))
Nick Piggin5a3d5c92009-01-06 14:39:09 -08001933 goto continue_unlock;
David Howells811d7362006-08-29 19:06:09 +01001934
Dave Chinner9e094382010-07-07 13:24:08 +10001935 trace_wbc_writepage(wbc, mapping->backing_dev_info);
Miklos Szeredi0ea97182007-05-10 22:22:51 -07001936 ret = (*writepage)(page, wbc, data);
Nick Piggin00266772009-01-06 14:39:06 -08001937 if (unlikely(ret)) {
1938 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1939 unlock_page(page);
1940 ret = 0;
1941 } else {
1942 /*
1943 * done_index is set past this page,
1944 * so media errors will not choke
1945 * background writeout for the entire
1946 * file. This has consequences for
1947 * range_cyclic semantics (ie. it may
1948 * not be suitable for data integrity
1949 * writeout).
1950 */
Jun'ichi Nomuracf15b072011-03-22 16:33:40 -07001951 done_index = page->index + 1;
Nick Piggin00266772009-01-06 14:39:06 -08001952 done = 1;
1953 break;
1954 }
Dave Chinner0b564922010-06-09 10:37:18 +10001955 }
David Howells811d7362006-08-29 19:06:09 +01001956
Dave Chinner546a1922010-08-24 11:44:34 +10001957 /*
1958 * We stop writing back only if we are not doing
1959 * integrity sync. In case of integrity sync we have to
1960 * keep going until we have written all the pages
1961 * we tagged for writeback prior to entering this loop.
1962 */
1963 if (--wbc->nr_to_write <= 0 &&
1964 wbc->sync_mode == WB_SYNC_NONE) {
1965 done = 1;
1966 break;
Nick Piggin05fe4782009-01-06 14:39:08 -08001967 }
David Howells811d7362006-08-29 19:06:09 +01001968 }
1969 pagevec_release(&pvec);
1970 cond_resched();
1971 }
Nick Piggin3a4c6802009-02-12 04:34:23 +01001972 if (!cycled && !done) {
David Howells811d7362006-08-29 19:06:09 +01001973 /*
Nick Piggin31a12662009-01-06 14:39:04 -08001974 * range_cyclic:
David Howells811d7362006-08-29 19:06:09 +01001975 * We hit the last page and there is more work to be done: wrap
1976 * back to the start of the file
1977 */
Nick Piggin31a12662009-01-06 14:39:04 -08001978 cycled = 1;
David Howells811d7362006-08-29 19:06:09 +01001979 index = 0;
Nick Piggin31a12662009-01-06 14:39:04 -08001980 end = writeback_index - 1;
David Howells811d7362006-08-29 19:06:09 +01001981 goto retry;
1982 }
Dave Chinner0b564922010-06-09 10:37:18 +10001983 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1984 mapping->writeback_index = done_index;
Aneesh Kumar K.V06d6cf62008-07-11 19:27:31 -04001985
David Howells811d7362006-08-29 19:06:09 +01001986 return ret;
1987}
Miklos Szeredi0ea97182007-05-10 22:22:51 -07001988EXPORT_SYMBOL(write_cache_pages);
1989
1990/*
1991 * Function used by generic_writepages to call the real writepage
1992 * function and set the mapping flags on error
1993 */
1994static int __writepage(struct page *page, struct writeback_control *wbc,
1995 void *data)
1996{
1997 struct address_space *mapping = data;
1998 int ret = mapping->a_ops->writepage(page, wbc);
1999 mapping_set_error(mapping, ret);
2000 return ret;
2001}
2002
2003/**
2004 * generic_writepages - walk the list of dirty pages of the given address space and writepage() all of them.
2005 * @mapping: address space structure to write
2006 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2007 *
2008 * This is a library function, which implements the writepages()
2009 * address_space_operation.
2010 */
2011int generic_writepages(struct address_space *mapping,
2012 struct writeback_control *wbc)
2013{
Shaohua Li9b6096a2011-03-17 10:47:06 +01002014 struct blk_plug plug;
2015 int ret;
2016
Miklos Szeredi0ea97182007-05-10 22:22:51 -07002017 /* deal with chardevs and other special file */
2018 if (!mapping->a_ops->writepage)
2019 return 0;
2020
Shaohua Li9b6096a2011-03-17 10:47:06 +01002021 blk_start_plug(&plug);
2022 ret = write_cache_pages(mapping, wbc, __writepage, mapping);
2023 blk_finish_plug(&plug);
2024 return ret;
Miklos Szeredi0ea97182007-05-10 22:22:51 -07002025}
David Howells811d7362006-08-29 19:06:09 +01002026
2027EXPORT_SYMBOL(generic_writepages);
2028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
2030{
Andrew Morton22905f72005-11-16 15:07:01 -08002031 int ret;
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (wbc->nr_to_write <= 0)
2034 return 0;
2035 if (mapping->a_ops->writepages)
Peter Zijlstrad08b3852006-09-25 23:30:57 -07002036 ret = mapping->a_ops->writepages(mapping, wbc);
Andrew Morton22905f72005-11-16 15:07:01 -08002037 else
2038 ret = generic_writepages(mapping, wbc);
Andrew Morton22905f72005-11-16 15:07:01 -08002039 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
2042/**
2043 * write_one_page - write out a single page and optionally wait on I/O
Martin Waitz67be2dd2005-05-01 08:59:26 -07002044 * @page: the page to write
2045 * @wait: if true, wait on writeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 *
2047 * The page must be locked by the caller and will be unlocked upon return.
2048 *
2049 * write_one_page() returns a negative error code if I/O failed.
2050 */
2051int write_one_page(struct page *page, int wait)
2052{
2053 struct address_space *mapping = page->mapping;
2054 int ret = 0;
2055 struct writeback_control wbc = {
2056 .sync_mode = WB_SYNC_ALL,
2057 .nr_to_write = 1,
2058 };
2059
2060 BUG_ON(!PageLocked(page));
2061
2062 if (wait)
2063 wait_on_page_writeback(page);
2064
2065 if (clear_page_dirty_for_io(page)) {
2066 page_cache_get(page);
2067 ret = mapping->a_ops->writepage(page, &wbc);
2068 if (ret == 0 && wait) {
2069 wait_on_page_writeback(page);
2070 if (PageError(page))
2071 ret = -EIO;
2072 }
2073 page_cache_release(page);
2074 } else {
2075 unlock_page(page);
2076 }
2077 return ret;
2078}
2079EXPORT_SYMBOL(write_one_page);
2080
2081/*
Ken Chen76719322007-02-10 01:43:15 -08002082 * For address_spaces which do not use buffers nor write back.
2083 */
2084int __set_page_dirty_no_writeback(struct page *page)
2085{
2086 if (!PageDirty(page))
Bob Liuc3f0da62011-01-13 15:45:49 -08002087 return !TestSetPageDirty(page);
Ken Chen76719322007-02-10 01:43:15 -08002088 return 0;
2089}
2090
2091/*
Edward Shishkine3a7cca2009-03-31 15:19:39 -07002092 * Helper function for set_page_dirty family.
2093 * NOTE: This relies on being atomic wrt interrupts.
2094 */
2095void account_page_dirtied(struct page *page, struct address_space *mapping)
2096{
2097 if (mapping_cap_account_dirty(mapping)) {
2098 __inc_zone_page_state(page, NR_FILE_DIRTY);
Michael Rubinea941f02010-10-26 14:21:35 -07002099 __inc_zone_page_state(page, NR_DIRTIED);
Edward Shishkine3a7cca2009-03-31 15:19:39 -07002100 __inc_bdi_stat(mapping->backing_dev_info, BDI_RECLAIMABLE);
Wu Fengguangc8e28ce2011-01-23 10:07:47 -06002101 __inc_bdi_stat(mapping->backing_dev_info, BDI_DIRTIED);
Edward Shishkine3a7cca2009-03-31 15:19:39 -07002102 task_io_account_write(PAGE_CACHE_SIZE);
Wu Fengguangd3bc1fe2011-04-14 07:52:37 -06002103 current->nr_dirtied++;
2104 this_cpu_inc(bdp_ratelimits);
Edward Shishkine3a7cca2009-03-31 15:19:39 -07002105 }
2106}
Michael Rubin679ceac2010-08-20 02:31:26 -07002107EXPORT_SYMBOL(account_page_dirtied);
Edward Shishkine3a7cca2009-03-31 15:19:39 -07002108
2109/*
Michael Rubinf629d1c2010-10-26 14:21:33 -07002110 * Helper function for set_page_writeback family.
2111 * NOTE: Unlike account_page_dirtied this does not rely on being atomic
2112 * wrt interrupts.
2113 */
2114void account_page_writeback(struct page *page)
2115{
2116 inc_zone_page_state(page, NR_WRITEBACK);
2117}
2118EXPORT_SYMBOL(account_page_writeback);
2119
2120/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 * For address_spaces which do not use buffers. Just tag the page as dirty in
2122 * its radix tree.
2123 *
2124 * This is also used when a single buffer is being dirtied: we want to set the
2125 * page dirty in that case, but not all the buffers. This is a "bottom-up"
2126 * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
2127 *
2128 * Most callers have locked the page, which pins the address_space in memory.
2129 * But zap_pte_range() does not lock the page, however in that case the
2130 * mapping is pinned by the vma's ->vm_file reference.
2131 *
2132 * We take care to handle the case where the page was truncated from the
Simon Arlott183ff222007-10-20 01:27:18 +02002133 * mapping by re-checking page_mapping() inside tree_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 */
2135int __set_page_dirty_nobuffers(struct page *page)
2136{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (!TestSetPageDirty(page)) {
2138 struct address_space *mapping = page_mapping(page);
2139 struct address_space *mapping2;
2140
Andrew Morton8c085402006-12-10 02:19:24 -08002141 if (!mapping)
2142 return 1;
2143
Nick Piggin19fd6232008-07-25 19:45:32 -07002144 spin_lock_irq(&mapping->tree_lock);
Andrew Morton8c085402006-12-10 02:19:24 -08002145 mapping2 = page_mapping(page);
2146 if (mapping2) { /* Race with truncate? */
2147 BUG_ON(mapping2 != mapping);
Nick Piggin787d2212007-07-17 04:03:34 -07002148 WARN_ON_ONCE(!PagePrivate(page) && !PageUptodate(page));
Edward Shishkine3a7cca2009-03-31 15:19:39 -07002149 account_page_dirtied(page, mapping);
Andrew Morton8c085402006-12-10 02:19:24 -08002150 radix_tree_tag_set(&mapping->page_tree,
2151 page_index(page), PAGECACHE_TAG_DIRTY);
2152 }
Nick Piggin19fd6232008-07-25 19:45:32 -07002153 spin_unlock_irq(&mapping->tree_lock);
Andrew Morton8c085402006-12-10 02:19:24 -08002154 if (mapping->host) {
2155 /* !PageAnon && !swapper_space */
2156 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
Andrew Morton4741c9f2006-03-24 03:18:11 -08002158 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 }
Andrew Morton4741c9f2006-03-24 03:18:11 -08002160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162EXPORT_SYMBOL(__set_page_dirty_nobuffers);
2163
2164/*
Wu Fengguang2f800fb2011-08-08 15:22:00 -06002165 * Call this whenever redirtying a page, to de-account the dirty counters
2166 * (NR_DIRTIED, BDI_DIRTIED, tsk->nr_dirtied), so that they match the written
2167 * counters (NR_WRITTEN, BDI_WRITTEN) in long term. The mismatches will lead to
2168 * systematic errors in balanced_dirty_ratelimit and the dirty pages position
2169 * control.
2170 */
2171void account_page_redirty(struct page *page)
2172{
2173 struct address_space *mapping = page->mapping;
2174 if (mapping && mapping_cap_account_dirty(mapping)) {
2175 current->nr_dirtied--;
2176 dec_zone_page_state(page, NR_DIRTIED);
2177 dec_bdi_stat(mapping->backing_dev_info, BDI_DIRTIED);
2178 }
2179}
2180EXPORT_SYMBOL(account_page_redirty);
2181
2182/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 * When a writepage implementation decides that it doesn't want to write this
2184 * page for some reason, it should redirty the locked page via
2185 * redirty_page_for_writepage() and it should then unlock the page and return 0
2186 */
2187int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page)
2188{
2189 wbc->pages_skipped++;
Wu Fengguang2f800fb2011-08-08 15:22:00 -06002190 account_page_redirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 return __set_page_dirty_nobuffers(page);
2192}
2193EXPORT_SYMBOL(redirty_page_for_writepage);
2194
2195/*
Wu Fengguang6746aff2009-09-16 11:50:14 +02002196 * Dirty a page.
2197 *
2198 * For pages with a mapping this should be done under the page lock
2199 * for the benefit of asynchronous memory errors who prefer a consistent
2200 * dirty state. This rule can be broken in some special cases,
2201 * but should be better not to.
2202 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 * If the mapping doesn't provide a set_page_dirty a_op, then
2204 * just fall through and assume that it wants buffer_heads.
2205 */
Nick Piggin1cf6e7d2009-02-18 14:48:18 -08002206int set_page_dirty(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
2208 struct address_space *mapping = page_mapping(page);
2209
2210 if (likely(mapping)) {
2211 int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
Minchan Kim278df9f2011-03-22 16:32:54 -07002212 /*
2213 * readahead/lru_deactivate_page could remain
2214 * PG_readahead/PG_reclaim due to race with end_page_writeback
2215 * About readahead, if the page is written, the flags would be
2216 * reset. So no problem.
2217 * About lru_deactivate_page, if the page is redirty, the flag
2218 * will be reset. So no problem. but if the page is used by readahead
2219 * it will confuse readahead and make it restart the size rampup
2220 * process. But it's a trivial problem.
2221 */
2222 ClearPageReclaim(page);
David Howells93614012006-09-30 20:45:40 +02002223#ifdef CONFIG_BLOCK
2224 if (!spd)
2225 spd = __set_page_dirty_buffers;
2226#endif
2227 return (*spd)(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 }
Andrew Morton4741c9f2006-03-24 03:18:11 -08002229 if (!PageDirty(page)) {
2230 if (!TestSetPageDirty(page))
2231 return 1;
2232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 return 0;
2234}
2235EXPORT_SYMBOL(set_page_dirty);
2236
2237/*
2238 * set_page_dirty() is racy if the caller has no reference against
2239 * page->mapping->host, and if the page is unlocked. This is because another
2240 * CPU could truncate the page off the mapping and then free the mapping.
2241 *
2242 * Usually, the page _is_ locked, or the caller is a user-space process which
2243 * holds a reference on the inode by having an open file.
2244 *
2245 * In other cases, the page should be locked before running set_page_dirty().
2246 */
2247int set_page_dirty_lock(struct page *page)
2248{
2249 int ret;
2250
Jens Axboe7eaceac2011-03-10 08:52:07 +01002251 lock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 ret = set_page_dirty(page);
2253 unlock_page(page);
2254 return ret;
2255}
2256EXPORT_SYMBOL(set_page_dirty_lock);
2257
2258/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 * Clear a page's dirty flag, while caring for dirty memory accounting.
2260 * Returns true if the page was previously dirty.
2261 *
2262 * This is for preparing to put the page under writeout. We leave the page
2263 * tagged as dirty in the radix tree so that a concurrent write-for-sync
2264 * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage
2265 * implementation will run either set_page_writeback() or set_page_dirty(),
2266 * at which stage we bring the page's dirty flag and radix-tree dirty tag
2267 * back into sync.
2268 *
2269 * This incoherency between the page's dirty flag and radix-tree tag is
2270 * unfortunate, but it only exists while the page is locked.
2271 */
2272int clear_page_dirty_for_io(struct page *page)
2273{
2274 struct address_space *mapping = page_mapping(page);
2275
Nick Piggin79352892007-07-19 01:47:22 -07002276 BUG_ON(!PageLocked(page));
2277
Linus Torvalds7658cc22006-12-29 10:00:58 -08002278 if (mapping && mapping_cap_account_dirty(mapping)) {
2279 /*
2280 * Yes, Virginia, this is indeed insane.
2281 *
2282 * We use this sequence to make sure that
2283 * (a) we account for dirty stats properly
2284 * (b) we tell the low-level filesystem to
2285 * mark the whole page dirty if it was
2286 * dirty in a pagetable. Only to then
2287 * (c) clean the page again and return 1 to
2288 * cause the writeback.
2289 *
2290 * This way we avoid all nasty races with the
2291 * dirty bit in multiple places and clearing
2292 * them concurrently from different threads.
2293 *
2294 * Note! Normally the "set_page_dirty(page)"
2295 * has no effect on the actual dirty bit - since
2296 * that will already usually be set. But we
2297 * need the side effects, and it can help us
2298 * avoid races.
2299 *
2300 * We basically use the page "master dirty bit"
2301 * as a serialization point for all the different
2302 * threads doing their things.
Linus Torvalds7658cc22006-12-29 10:00:58 -08002303 */
2304 if (page_mkclean(page))
2305 set_page_dirty(page);
Nick Piggin79352892007-07-19 01:47:22 -07002306 /*
2307 * We carefully synchronise fault handlers against
2308 * installing a dirty pte and marking the page dirty
2309 * at this point. We do this by having them hold the
2310 * page lock at some point after installing their
2311 * pte, but before marking the page dirty.
2312 * Pages are always locked coming in here, so we get
2313 * the desired exclusion. See mm/memory.c:do_wp_page()
2314 * for more comments.
2315 */
Linus Torvalds7658cc22006-12-29 10:00:58 -08002316 if (TestClearPageDirty(page)) {
Andrew Morton8c085402006-12-10 02:19:24 -08002317 dec_zone_page_state(page, NR_FILE_DIRTY);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -07002318 dec_bdi_stat(mapping->backing_dev_info,
2319 BDI_RECLAIMABLE);
Linus Torvalds7658cc22006-12-29 10:00:58 -08002320 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 }
Linus Torvalds7658cc22006-12-29 10:00:58 -08002322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
Linus Torvalds7658cc22006-12-29 10:00:58 -08002324 return TestClearPageDirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
Hans Reiser58bb01a2005-11-18 01:10:53 -08002326EXPORT_SYMBOL(clear_page_dirty_for_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328int test_clear_page_writeback(struct page *page)
2329{
2330 struct address_space *mapping = page_mapping(page);
2331 int ret;
2332
2333 if (mapping) {
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07002334 struct backing_dev_info *bdi = mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 unsigned long flags;
2336
Nick Piggin19fd6232008-07-25 19:45:32 -07002337 spin_lock_irqsave(&mapping->tree_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 ret = TestClearPageWriteback(page);
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07002339 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 radix_tree_tag_clear(&mapping->page_tree,
2341 page_index(page),
2342 PAGECACHE_TAG_WRITEBACK);
Miklos Szeredie4ad08f2008-04-30 00:54:37 -07002343 if (bdi_cap_account_writeback(bdi)) {
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07002344 __dec_bdi_stat(bdi, BDI_WRITEBACK);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -07002345 __bdi_writeout_inc(bdi);
2346 }
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07002347 }
Nick Piggin19fd6232008-07-25 19:45:32 -07002348 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 } else {
2350 ret = TestClearPageWriteback(page);
2351 }
Wu Fengguang99b12e32011-07-25 17:12:37 -07002352 if (ret) {
Andrew Mortond688abf2007-07-19 01:49:17 -07002353 dec_zone_page_state(page, NR_WRITEBACK);
Wu Fengguang99b12e32011-07-25 17:12:37 -07002354 inc_zone_page_state(page, NR_WRITTEN);
2355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 return ret;
2357}
2358
2359int test_set_page_writeback(struct page *page)
2360{
2361 struct address_space *mapping = page_mapping(page);
2362 int ret;
2363
2364 if (mapping) {
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07002365 struct backing_dev_info *bdi = mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 unsigned long flags;
2367
Nick Piggin19fd6232008-07-25 19:45:32 -07002368 spin_lock_irqsave(&mapping->tree_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 ret = TestSetPageWriteback(page);
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07002370 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 radix_tree_tag_set(&mapping->page_tree,
2372 page_index(page),
2373 PAGECACHE_TAG_WRITEBACK);
Miklos Szeredie4ad08f2008-04-30 00:54:37 -07002374 if (bdi_cap_account_writeback(bdi))
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07002375 __inc_bdi_stat(bdi, BDI_WRITEBACK);
2376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if (!PageDirty(page))
2378 radix_tree_tag_clear(&mapping->page_tree,
2379 page_index(page),
2380 PAGECACHE_TAG_DIRTY);
Jan Karaf446daa2010-08-09 17:19:12 -07002381 radix_tree_tag_clear(&mapping->page_tree,
2382 page_index(page),
2383 PAGECACHE_TAG_TOWRITE);
Nick Piggin19fd6232008-07-25 19:45:32 -07002384 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 } else {
2386 ret = TestSetPageWriteback(page);
2387 }
Andrew Mortond688abf2007-07-19 01:49:17 -07002388 if (!ret)
Michael Rubinf629d1c2010-10-26 14:21:33 -07002389 account_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 return ret;
2391
2392}
2393EXPORT_SYMBOL(test_set_page_writeback);
2394
2395/*
Nick Piggin00128182007-10-16 01:24:40 -07002396 * Return true if any of the pages in the mapping are marked with the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 * passed tag.
2398 */
2399int mapping_tagged(struct address_space *mapping, int tag)
2400{
Konstantin Khlebnikov72c47832011-07-25 17:12:31 -07002401 return radix_tree_tagged(&mapping->page_tree, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402}
2403EXPORT_SYMBOL(mapping_tagged);