4 * Copyright (C) 2002, Linus Torvalds.
6 * Contains functions related to writing back dirty pages at the
9 * 10Apr2002 akpm@zip.com.au
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/spinlock.h>
18 #include <linux/swap.h>
19 #include <linux/slab.h>
20 #include <linux/pagemap.h>
21 #include <linux/writeback.h>
22 #include <linux/init.h>
23 #include <linux/backing-dev.h>
24 #include <linux/blkdev.h>
25 #include <linux/mpage.h>
26 #include <linux/percpu.h>
27 #include <linux/notifier.h>
28 #include <linux/smp.h>
29 #include <linux/sysctl.h>
30 #include <linux/cpu.h>
33 * The maximum number of pages to writeout in a single bdflush/kupdate
34 * operation. We do this so we don't hold I_LOCK against an inode for
35 * enormous amounts of time, which would block a userspace task which has
36 * been forced to throttle against that inode. Also, the code reevaluates
37 * the dirty each time it has written this many pages.
39 #define MAX_WRITEBACK_PAGES 1024
42 * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited
43 * will look to see if it needs to force writeback or throttling.
45 static long ratelimit_pages
= 32;
47 static long total_pages
; /* The total number of pages in the machine. */
48 static int dirty_exceeded
; /* Dirty mem may be over limit */
51 * When balance_dirty_pages decides that the caller needs to perform some
52 * non-background writeback, this is how many pages it will attempt to write.
53 * It should be somewhat larger than RATELIMIT_PAGES to ensure that reasonably
54 * large amounts of I/O are submitted.
56 static inline long sync_writeback_pages(void)
58 return ratelimit_pages
+ ratelimit_pages
/ 2;
61 /* The following parameters are exported via /proc/sys/vm */
64 * Start background writeback (via pdflush) at this percentage
66 int dirty_background_ratio
= 10;
69 * The generator of dirty data starts writeback at this percentage
71 int vm_dirty_ratio
= 40;
74 * The interval between `kupdate'-style writebacks, in centiseconds
75 * (hundredths of a second)
77 int dirty_writeback_centisecs
= 5 * 100;
80 * The longest number of centiseconds for which data is allowed to remain dirty
82 int dirty_expire_centisecs
= 30 * 100;
84 /* End of sysctl-exported parameters */
87 static void background_writeout(unsigned long _min_pages
);
90 * Work out the current dirty-memory clamping and background writeout
93 * The main aim here is to lower them aggressively if there is a lot of mapped
94 * memory around. To avoid stressing page reclaim with lots of unreclaimable
95 * pages. It is better to clamp down on writers than to start swapping, and
96 * performing lots of scanning.
98 * We only allow 1/2 of the currently-unmapped memory to be dirtied.
100 * We don't permit the clamping level to fall below 5% - that is getting rather
103 * We make sure that the background writeout level is below the adjusted
107 get_dirty_limits(struct page_state
*ps
, long *pbackground
, long *pdirty
)
109 int background_ratio
; /* Percentages */
114 struct task_struct
*tsk
;
118 unmapped_ratio
= 100 - (ps
->nr_mapped
* 100) / total_pages
;
120 dirty_ratio
= vm_dirty_ratio
;
121 if (dirty_ratio
> unmapped_ratio
/ 2)
122 dirty_ratio
= unmapped_ratio
/ 2;
127 background_ratio
= dirty_background_ratio
;
128 if (background_ratio
>= dirty_ratio
)
129 background_ratio
= dirty_ratio
/ 2;
131 background
= (background_ratio
* total_pages
) / 100;
132 dirty
= (dirty_ratio
* total_pages
) / 100;
134 if (tsk
->flags
& PF_LESS_THROTTLE
|| rt_task(tsk
)) {
135 background
+= background
/ 4;
138 *pbackground
= background
;
143 * balance_dirty_pages() must be called by processes which are generating dirty
144 * data. It looks at the number of dirty pages in the machine and will force
145 * the caller to perform writeback if the system is over `vm_dirty_ratio'.
146 * If we're over `background_thresh' then pdflush is woken to perform some
149 static void balance_dirty_pages(struct address_space
*mapping
)
151 struct page_state ps
;
153 long background_thresh
;
155 unsigned long pages_written
= 0;
156 unsigned long write_chunk
= sync_writeback_pages();
158 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
161 struct writeback_control wbc
= {
163 .sync_mode
= WB_SYNC_NONE
,
164 .older_than_this
= NULL
,
165 .nr_to_write
= write_chunk
,
168 get_dirty_limits(&ps
, &background_thresh
, &dirty_thresh
);
169 nr_reclaimable
= ps
.nr_dirty
+ ps
.nr_unstable
;
170 if (nr_reclaimable
+ ps
.nr_writeback
<= dirty_thresh
)
175 /* Note: nr_reclaimable denotes nr_dirty + nr_unstable.
176 * Unstable writes are a feature of certain networked
177 * filesystems (i.e. NFS) in which data may have been
178 * written to the server's write cache, but has not yet
179 * been flushed to permanent storage.
181 if (nr_reclaimable
) {
182 writeback_inodes(&wbc
);
183 get_dirty_limits(&ps
, &background_thresh
,
185 nr_reclaimable
= ps
.nr_dirty
+ ps
.nr_unstable
;
186 if (nr_reclaimable
+ ps
.nr_writeback
<= dirty_thresh
)
188 pages_written
+= write_chunk
- wbc
.nr_to_write
;
189 if (pages_written
>= write_chunk
)
190 break; /* We've done our duty */
192 blk_congestion_wait(WRITE
, HZ
/10);
195 if (nr_reclaimable
+ ps
.nr_writeback
<= dirty_thresh
)
198 if (!writeback_in_progress(bdi
) && nr_reclaimable
> background_thresh
)
199 pdflush_operation(background_writeout
, 0);
203 * balance_dirty_pages_ratelimited - balance dirty memory state
204 * @mapping - address_space which was dirtied
206 * Processes which are dirtying memory should call in here once for each page
207 * which was newly dirtied. The function will periodically check the system's
208 * dirty state and will initiate writeback if needed.
210 * On really big machines, get_page_state is expensive, so try to avoid calling
211 * it too often (ratelimiting). But once we're over the dirty memory limit we
212 * decrease the ratelimiting by a lot, to prevent individual processes from
213 * overshooting the limit by (ratelimit_pages) each.
215 void balance_dirty_pages_ratelimited(struct address_space
*mapping
)
217 static DEFINE_PER_CPU(int, ratelimits
) = 0;
220 ratelimit
= ratelimit_pages
;
225 * Check the rate limiting. Also, we do not want to throttle real-time
226 * tasks in balance_dirty_pages(). Period.
228 if (get_cpu_var(ratelimits
)++ >= ratelimit
) {
229 __get_cpu_var(ratelimits
) = 0;
230 put_cpu_var(ratelimits
);
231 balance_dirty_pages(mapping
);
234 put_cpu_var(ratelimits
);
236 EXPORT_SYMBOL(balance_dirty_pages_ratelimited
);
239 * writeback at least _min_pages, and keep writing until the amount of dirty
240 * memory is less than the background threshold, or until we're all clean.
242 static void background_writeout(unsigned long _min_pages
)
244 long min_pages
= _min_pages
;
245 struct writeback_control wbc
= {
247 .sync_mode
= WB_SYNC_NONE
,
248 .older_than_this
= NULL
,
254 struct page_state ps
;
255 long background_thresh
;
258 get_dirty_limits(&ps
, &background_thresh
, &dirty_thresh
);
259 if (ps
.nr_dirty
+ ps
.nr_unstable
< background_thresh
262 wbc
.encountered_congestion
= 0;
263 wbc
.nr_to_write
= MAX_WRITEBACK_PAGES
;
264 writeback_inodes(&wbc
);
265 min_pages
-= MAX_WRITEBACK_PAGES
- wbc
.nr_to_write
;
266 if (wbc
.nr_to_write
> 0) {
267 /* Wrote less than expected */
268 if (wbc
.encountered_congestion
)
269 blk_congestion_wait(WRITE
, HZ
/10);
277 * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back
278 * the whole world. Returns 0 if a pdflush thread was dispatched. Returns
279 * -1 if all pdflush threads were busy.
281 int wakeup_bdflush(long nr_pages
)
284 struct page_state ps
;
287 nr_pages
= ps
.nr_dirty
+ ps
.nr_unstable
;
289 return pdflush_operation(background_writeout
, nr_pages
);
292 static struct timer_list wb_timer
;
295 * Periodic writeback of "old" data.
297 * Define "old": the first time one of an inode's pages is dirtied, we mark the
298 * dirtying-time in the inode's address_space. So this periodic writeback code
299 * just walks the superblock inode list, writing back any inodes which are
300 * older than a specific point in time.
302 * Try to run once per dirty_writeback_centisecs. But if a writeback event
303 * takes longer than a dirty_writeback_centisecs interval, then leave a
306 * older_than_this takes precedence over nr_to_write. So we'll only write back
307 * all dirty pages if they are all attached to "old" mappings.
309 static void wb_kupdate(unsigned long arg
)
311 unsigned long oldest_jif
;
312 unsigned long start_jif
;
313 unsigned long next_jif
;
315 struct page_state ps
;
316 struct writeback_control wbc
= {
318 .sync_mode
= WB_SYNC_NONE
,
319 .older_than_this
= &oldest_jif
,
328 oldest_jif
= jiffies
- (dirty_expire_centisecs
* HZ
) / 100;
330 next_jif
= start_jif
+ (dirty_writeback_centisecs
* HZ
) / 100;
331 nr_to_write
= ps
.nr_dirty
+ ps
.nr_unstable
+
332 (inodes_stat
.nr_inodes
- inodes_stat
.nr_unused
);
333 while (nr_to_write
> 0) {
334 wbc
.encountered_congestion
= 0;
335 wbc
.nr_to_write
= MAX_WRITEBACK_PAGES
;
336 writeback_inodes(&wbc
);
337 if (wbc
.nr_to_write
> 0) {
338 if (wbc
.encountered_congestion
)
339 blk_congestion_wait(WRITE
, HZ
/10);
341 break; /* All the old data is written */
343 nr_to_write
-= MAX_WRITEBACK_PAGES
- wbc
.nr_to_write
;
345 if (time_before(next_jif
, jiffies
+ HZ
))
346 next_jif
= jiffies
+ HZ
;
347 if (dirty_writeback_centisecs
)
348 mod_timer(&wb_timer
, next_jif
);
352 * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs
354 int dirty_writeback_centisecs_handler(ctl_table
*table
, int write
,
355 struct file
*file
, void __user
*buffer
, size_t *length
)
357 proc_dointvec(table
, write
, file
, buffer
, length
);
358 if (dirty_writeback_centisecs
) {
360 jiffies
+ (dirty_writeback_centisecs
* HZ
) / 100);
362 del_timer(&wb_timer
);
367 static void wb_timer_fn(unsigned long unused
)
369 if (pdflush_operation(wb_kupdate
, 0) < 0)
370 mod_timer(&wb_timer
, jiffies
+ HZ
); /* delay 1 second */
375 * If ratelimit_pages is too high then we can get into dirty-data overload
376 * if a large number of processes all perform writes at the same time.
377 * If it is too low then SMP machines will call the (expensive) get_page_state
380 * Here we set ratelimit_pages to a level which ensures that when all CPUs are
381 * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
382 * thresholds before writeback cuts in.
384 * But the limit should not be set too high. Because it also controls the
385 * amount of memory which the balance_dirty_pages() caller has to write back.
386 * If this is too large then the caller will block on the IO queue all the
387 * time. So limit it to four megabytes - the balance_dirty_pages() caller
388 * will write six megabyte chunks, max.
391 static void set_ratelimit(void)
393 ratelimit_pages
= total_pages
/ (num_online_cpus() * 32);
394 if (ratelimit_pages
< 16)
395 ratelimit_pages
= 16;
396 if (ratelimit_pages
* PAGE_CACHE_SIZE
> 4096 * 1024)
397 ratelimit_pages
= (4096 * 1024) / PAGE_CACHE_SIZE
;
401 ratelimit_handler(struct notifier_block
*self
, unsigned long u
, void *v
)
407 static struct notifier_block ratelimit_nb
= {
408 .notifier_call
= ratelimit_handler
,
413 * If the machine has a large highmem:lowmem ratio then scale back the default
414 * dirty memory thresholds: allowing too much dirty highmem pins an excessive
415 * number of buffer_heads.
417 void __init
page_writeback_init(void)
419 long buffer_pages
= nr_free_buffer_pages();
422 total_pages
= nr_free_pagecache_pages();
424 correction
= (100 * 4 * buffer_pages
) / total_pages
;
426 if (correction
< 100) {
427 dirty_background_ratio
*= correction
;
428 dirty_background_ratio
/= 100;
429 vm_dirty_ratio
*= correction
;
430 vm_dirty_ratio
/= 100;
433 init_timer(&wb_timer
);
434 wb_timer
.expires
= jiffies
+ (dirty_writeback_centisecs
* HZ
) / 100;
436 wb_timer
.function
= wb_timer_fn
;
437 add_timer(&wb_timer
);
439 register_cpu_notifier(&ratelimit_nb
);
442 int do_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
444 if (mapping
->a_ops
->writepages
)
445 return mapping
->a_ops
->writepages(mapping
, wbc
);
446 return generic_writepages(mapping
, wbc
);
450 * write_one_page - write out a single page and optionally wait on I/O
452 * @page - the page to write
453 * @wait - if true, wait on writeout
455 * The page must be locked by the caller and will be unlocked upon return.
457 * write_one_page() returns a negative error code if I/O failed.
459 int write_one_page(struct page
*page
, int wait
)
461 struct address_space
*mapping
= page
->mapping
;
463 struct writeback_control wbc
= {
464 .sync_mode
= WB_SYNC_ALL
,
468 BUG_ON(!PageLocked(page
));
471 wait_on_page_writeback(page
);
473 spin_lock(&mapping
->page_lock
);
474 list_del(&page
->list
);
475 if (test_clear_page_dirty(page
)) {
476 list_add(&page
->list
, &mapping
->locked_pages
);
477 page_cache_get(page
);
478 spin_unlock(&mapping
->page_lock
);
479 ret
= mapping
->a_ops
->writepage(page
, &wbc
);
480 if (ret
== 0 && wait
) {
481 wait_on_page_writeback(page
);
485 page_cache_release(page
);
487 list_add(&page
->list
, &mapping
->clean_pages
);
488 spin_unlock(&mapping
->page_lock
);
493 EXPORT_SYMBOL(write_one_page
);
496 * For address_spaces which do not use buffers. Just set the page's dirty bit
497 * and move it to the dirty_pages list. Also perform space reservation if
500 * __set_page_dirty_nobuffers() may return -ENOSPC. But if it does, the page
501 * is still safe, as long as it actually manages to find some blocks at
504 * This is also used when a single buffer is being dirtied: we want to set the
505 * page dirty in that case, but not all the buffers. This is a "bottom-up"
506 * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
508 int __set_page_dirty_nobuffers(struct page
*page
)
512 if (!TestSetPageDirty(page
)) {
513 struct address_space
*mapping
= page
->mapping
;
516 spin_lock(&mapping
->page_lock
);
517 if (page
->mapping
) { /* Race with truncate? */
518 BUG_ON(page
->mapping
!= mapping
);
519 if (!mapping
->backing_dev_info
->memory_backed
)
520 inc_page_state(nr_dirty
);
521 list_del(&page
->list
);
522 list_add(&page
->list
, &mapping
->dirty_pages
);
524 spin_unlock(&mapping
->page_lock
);
525 if (!PageSwapCache(page
))
526 __mark_inode_dirty(mapping
->host
,
532 EXPORT_SYMBOL(__set_page_dirty_nobuffers
);
535 * set_page_dirty() is racy if the caller has no reference against
536 * page->mapping->host, and if the page is unlocked. This is because another
537 * CPU could truncate the page off the mapping and then free the mapping.
539 * Usually, the page _is_ locked, or the caller is a user-space process which
540 * holds a reference on the inode by having an open file.
542 * In other cases, the page should be locked before running set_page_dirty().
544 int set_page_dirty_lock(struct page
*page
)
549 ret
= set_page_dirty(page
);
553 EXPORT_SYMBOL(set_page_dirty_lock
);
556 * Clear a page's dirty flag, while caring for dirty memory accounting.
557 * Returns true if the page was previously dirty.
559 int test_clear_page_dirty(struct page
*page
)
561 if (TestClearPageDirty(page
)) {
562 struct address_space
*mapping
= page
->mapping
;
564 if (mapping
&& !mapping
->backing_dev_info
->memory_backed
)
565 dec_page_state(nr_dirty
);
570 EXPORT_SYMBOL(test_clear_page_dirty
);