~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/mm/page-writeback.c

Version: ~ [ linux-5.12-rc6 ] ~ [ linux-5.11.13 ] ~ [ linux-5.10.29 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.111 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.186 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.230 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.266 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.266 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * mm/page-writeback.c.
  3  *
  4  * Copyright (C) 2002, Linus Torvalds.
  5  *
  6  * Contains functions related to writing back dirty pages at the
  7  * address_space level.
  8  *
  9  * 10Apr2002    akpm@zip.com.au
 10  *              Initial version
 11  */
 12 
 13 #include <linux/kernel.h>
 14 #include <linux/module.h>
 15 #include <linux/spinlock.h>
 16 #include <linux/fs.h>
 17 #include <linux/mm.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>
 31 
 32 /*
 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.
 38  */
 39 #define MAX_WRITEBACK_PAGES     1024
 40 
 41 /*
 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.
 44  */
 45 static long ratelimit_pages = 32;
 46 
 47 static long total_pages;        /* The total number of pages in the machine. */
 48 static int dirty_exceeded;      /* Dirty mem may be over limit */
 49 
 50 /*
 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.
 55  */
 56 static inline long sync_writeback_pages(void)
 57 {
 58         return ratelimit_pages + ratelimit_pages / 2;
 59 }
 60 
 61 /* The following parameters are exported via /proc/sys/vm */
 62 
 63 /*
 64  * Start background writeback (via pdflush) at this percentage
 65  */
 66 int dirty_background_ratio = 10;
 67 
 68 /*
 69  * The generator of dirty data starts writeback at this percentage
 70  */
 71 int vm_dirty_ratio = 40;
 72 
 73 /*
 74  * The interval between `kupdate'-style writebacks, in centiseconds
 75  * (hundredths of a second)
 76  */
 77 int dirty_writeback_centisecs = 5 * 100;
 78 
 79 /*
 80  * The longest number of centiseconds for which data is allowed to remain dirty
 81  */
 82 int dirty_expire_centisecs = 30 * 100;
 83 
 84 /* End of sysctl-exported parameters */
 85 
 86 
 87 static void background_writeout(unsigned long _min_pages);
 88 
 89 /*
 90  * Work out the current dirty-memory clamping and background writeout
 91  * thresholds.
 92  *
 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.
 97  *
 98  * We only allow 1/2 of the currently-unmapped memory to be dirtied.
 99  *
100  * We don't permit the clamping level to fall below 5% - that is getting rather
101  * excessive.
102  *
103  * We make sure that the background writeout level is below the adjusted
104  * clamping level.
105  */
106 static void
107 get_dirty_limits(struct page_state *ps, long *pbackground, long *pdirty)
108 {
109         int background_ratio;           /* Percentages */
110         int dirty_ratio;
111         int unmapped_ratio;
112         long background;
113         long dirty;
114         struct task_struct *tsk;
115 
116         get_page_state(ps);
117 
118         unmapped_ratio = 100 - (ps->nr_mapped * 100) / total_pages;
119 
120         dirty_ratio = vm_dirty_ratio;
121         if (dirty_ratio > unmapped_ratio / 2)
122                 dirty_ratio = unmapped_ratio / 2;
123 
124         if (dirty_ratio < 5)
125                 dirty_ratio = 5;
126 
127         background_ratio = dirty_background_ratio;
128         if (background_ratio >= dirty_ratio)
129                 background_ratio = dirty_ratio / 2;
130 
131         background = (background_ratio * total_pages) / 100;
132         dirty = (dirty_ratio * total_pages) / 100;
133         tsk = current;
134         if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) {
135                 background += background / 4;
136                 dirty += dirty / 4;
137         }
138         *pbackground = background;
139         *pdirty = dirty;
140 }
141 
142 /*
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
147  * writeout.
148  */
149 static void balance_dirty_pages(struct address_space *mapping)
150 {
151         struct page_state ps;
152         long nr_reclaimable;
153         long background_thresh;
154         long dirty_thresh;
155         unsigned long pages_written = 0;
156         unsigned long write_chunk = sync_writeback_pages();
157 
158         struct backing_dev_info *bdi = mapping->backing_dev_info;
159 
160         for (;;) {
161                 struct writeback_control wbc = {
162                         .bdi            = bdi,
163                         .sync_mode      = WB_SYNC_NONE,
164                         .older_than_this = NULL,
165                         .nr_to_write    = write_chunk,
166                 };
167 
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)
171                         break;
172 
173                 dirty_exceeded = 1;
174 
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.
180                  */
181                 if (nr_reclaimable) {
182                         writeback_inodes(&wbc);
183                         get_dirty_limits(&ps, &background_thresh,
184                                         &dirty_thresh);
185                         nr_reclaimable = ps.nr_dirty + ps.nr_unstable;
186                         if (nr_reclaimable + ps.nr_writeback <= dirty_thresh)
187                                 break;
188                         pages_written += write_chunk - wbc.nr_to_write;
189                         if (pages_written >= write_chunk)
190                                 break;          /* We've done our duty */
191                 }
192                 blk_congestion_wait(WRITE, HZ/10);
193         }
194 
195         if (nr_reclaimable + ps.nr_writeback <= dirty_thresh)
196                 dirty_exceeded = 0;
197 
198         if (!writeback_in_progress(bdi) && nr_reclaimable > background_thresh)
199                 pdflush_operation(background_writeout, 0);
200 }
201 
202 /**
203  * balance_dirty_pages_ratelimited - balance dirty memory state
204  * @mapping - address_space which was dirtied
205  *
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.
209  *
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.
214  */
215 void balance_dirty_pages_ratelimited(struct address_space *mapping)
216 {
217         static DEFINE_PER_CPU(int, ratelimits) = 0;
218         long ratelimit;
219 
220         ratelimit = ratelimit_pages;
221         if (dirty_exceeded)
222                 ratelimit = 8;
223 
224         /*
225          * Check the rate limiting. Also, we do not want to throttle real-time
226          * tasks in balance_dirty_pages(). Period.
227          */
228         if (get_cpu_var(ratelimits)++ >= ratelimit) {
229                 __get_cpu_var(ratelimits) = 0;
230                 put_cpu_var(ratelimits);
231                 balance_dirty_pages(mapping);
232                 return;
233         }
234         put_cpu_var(ratelimits);
235 }
236 
237 /*
238  * writeback at least _min_pages, and keep writing until the amount of dirty
239  * memory is less than the background threshold, or until we're all clean.
240  */
241 static void background_writeout(unsigned long _min_pages)
242 {
243         long min_pages = _min_pages;
244         struct writeback_control wbc = {
245                 .bdi            = NULL,
246                 .sync_mode      = WB_SYNC_NONE,
247                 .older_than_this = NULL,
248                 .nr_to_write    = 0,
249                 .nonblocking    = 1,
250         };
251 
252         for ( ; ; ) {
253                 struct page_state ps;
254                 long background_thresh;
255                 long dirty_thresh;
256 
257                 get_dirty_limits(&ps, &background_thresh, &dirty_thresh);
258                 if (ps.nr_dirty + ps.nr_unstable < background_thresh
259                                 && min_pages <= 0)
260                         break;
261                 wbc.encountered_congestion = 0;
262                 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
263                 writeback_inodes(&wbc);
264                 min_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
265                 if (wbc.nr_to_write > 0) {
266                         /* Wrote less than expected */
267                         if (wbc.encountered_congestion)
268                                 blk_congestion_wait(WRITE, HZ/10);
269                         else
270                                 break;
271                 }
272         }
273 }
274 
275 /*
276  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
277  * the whole world.  Returns 0 if a pdflush thread was dispatched.  Returns
278  * -1 if all pdflush threads were busy.
279  */
280 int wakeup_bdflush(long nr_pages)
281 {
282         if (nr_pages == 0) {
283                 struct page_state ps;
284 
285                 get_page_state(&ps);
286                 nr_pages = ps.nr_dirty + ps.nr_unstable;
287         }
288         return pdflush_operation(background_writeout, nr_pages);
289 }
290 
291 static struct timer_list wb_timer;
292 
293 /*
294  * Periodic writeback of "old" data.
295  *
296  * Define "old": the first time one of an inode's pages is dirtied, we mark the
297  * dirtying-time in the inode's address_space.  So this periodic writeback code
298  * just walks the superblock inode list, writing back any inodes which are
299  * older than a specific point in time.
300  *
301  * Try to run once per dirty_writeback_centisecs.  But if a writeback event
302  * takes longer than a dirty_writeback_centisecs interval, then leave a
303  * one-second gap.
304  *
305  * older_than_this takes precedence over nr_to_write.  So we'll only write back
306  * all dirty pages if they are all attached to "old" mappings.
307  */
308 static void wb_kupdate(unsigned long arg)
309 {
310         unsigned long oldest_jif;
311         unsigned long start_jif;
312         unsigned long next_jif;
313         long nr_to_write;
314         struct page_state ps;
315         struct writeback_control wbc = {
316                 .bdi            = NULL,
317                 .sync_mode      = WB_SYNC_NONE,
318                 .older_than_this = &oldest_jif,
319                 .nr_to_write    = 0,
320                 .nonblocking    = 1,
321                 .for_kupdate    = 1,
322         };
323 
324         sync_supers();
325 
326         get_page_state(&ps);
327         oldest_jif = jiffies - (dirty_expire_centisecs * HZ) / 100;
328         start_jif = jiffies;
329         next_jif = start_jif + (dirty_writeback_centisecs * HZ) / 100;
330         nr_to_write = ps.nr_dirty + ps.nr_unstable +
331                         (inodes_stat.nr_inodes - inodes_stat.nr_unused);
332         while (nr_to_write > 0) {
333                 wbc.encountered_congestion = 0;
334                 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
335                 writeback_inodes(&wbc);
336                 if (wbc.nr_to_write > 0) {
337                         if (wbc.encountered_congestion)
338                                 blk_congestion_wait(WRITE, HZ/10);
339                         else
340                                 break;  /* All the old data is written */
341                 }
342                 nr_to_write -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
343         }
344         if (time_before(next_jif, jiffies + HZ))
345                 next_jif = jiffies + HZ;
346         if (dirty_writeback_centisecs)
347                 mod_timer(&wb_timer, next_jif);
348 }
349 
350 /*
351  * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs
352  */
353 int dirty_writeback_centisecs_handler(ctl_table *table, int write,
354                 struct file *file, void __user *buffer, size_t *length)
355 {
356         proc_dointvec(table, write, file, buffer, length);
357         if (dirty_writeback_centisecs) {
358                 mod_timer(&wb_timer,
359                         jiffies + (dirty_writeback_centisecs * HZ) / 100);
360         } else {
361                 del_timer(&wb_timer);
362         }
363         return 0;
364 }
365 
366 static void wb_timer_fn(unsigned long unused)
367 {
368         if (pdflush_operation(wb_kupdate, 0) < 0)
369                 mod_timer(&wb_timer, jiffies + HZ); /* delay 1 second */
370 
371 }
372 
373 /*
374  * If ratelimit_pages is too high then we can get into dirty-data overload
375  * if a large number of processes all perform writes at the same time.
376  * If it is too low then SMP machines will call the (expensive) get_page_state
377  * too often.
378  *
379  * Here we set ratelimit_pages to a level which ensures that when all CPUs are
380  * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
381  * thresholds before writeback cuts in.
382  *
383  * But the limit should not be set too high.  Because it also controls the
384  * amount of memory which the balance_dirty_pages() caller has to write back.
385  * If this is too large then the caller will block on the IO queue all the
386  * time.  So limit it to four megabytes - the balance_dirty_pages() caller
387  * will write six megabyte chunks, max.
388  */
389 
390 static void set_ratelimit(void)
391 {
392         ratelimit_pages = total_pages / (num_online_cpus() * 32);
393         if (ratelimit_pages < 16)
394                 ratelimit_pages = 16;
395         if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024)
396                 ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE;
397 }
398 
399 static int
400 ratelimit_handler(struct notifier_block *self, unsigned long u, void *v)
401 {
402         set_ratelimit();
403         return 0;
404 }
405 
406 static struct notifier_block ratelimit_nb = {
407         .notifier_call  = ratelimit_handler,
408         .next           = NULL,
409 };
410 
411 /*
412  * If the machine has a large highmem:lowmem ratio then scale back the default
413  * dirty memory thresholds: allowing too much dirty highmem pins an excessive
414  * number of buffer_heads.
415  */
416 void __init page_writeback_init(void)
417 {
418         long buffer_pages = nr_free_buffer_pages();
419         long correction;
420 
421         total_pages = nr_free_pagecache_pages();
422 
423         correction = (100 * 4 * buffer_pages) / total_pages;
424 
425         if (correction < 100) {
426                 dirty_background_ratio *= correction;
427                 dirty_background_ratio /= 100;
428                 vm_dirty_ratio *= correction;
429                 vm_dirty_ratio /= 100;
430         }
431 
432         init_timer(&wb_timer);
433         wb_timer.expires = jiffies + (dirty_writeback_centisecs * HZ) / 100;
434         wb_timer.data = 0;
435         wb_timer.function = wb_timer_fn;
436         add_timer(&wb_timer);
437         set_ratelimit();
438         register_cpu_notifier(&ratelimit_nb);
439 }
440 
441 int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
442 {
443         if (mapping->a_ops->writepages)
444                 return mapping->a_ops->writepages(mapping, wbc);
445         return generic_writepages(mapping, wbc);
446 }
447 
448 /**
449  * write_one_page - write out a single page and optionally wait on I/O
450  *
451  * @page - the page to write
452  * @wait - if true, wait on writeout
453  *
454  * The page must be locked by the caller and will be unlocked upon return.
455  *
456  * write_one_page() returns a negative error code if I/O failed.
457  */
458 int write_one_page(struct page *page, int wait)
459 {
460         struct address_space *mapping = page->mapping;
461         int ret = 0;
462         struct writeback_control wbc = {
463                 .sync_mode = WB_SYNC_ALL,
464                 .nr_to_write = 1,
465         };
466 
467         BUG_ON(!PageLocked(page));
468 
469         if (wait)
470                 wait_on_page_writeback(page);
471 
472         spin_lock(&mapping->page_lock);
473         list_del(&page->list);
474         if (test_clear_page_dirty(page)) {
475                 list_add(&page->list, &mapping->locked_pages);
476                 page_cache_get(page);
477                 spin_unlock(&mapping->page_lock);
478                 ret = mapping->a_ops->writepage(page, &wbc);
479                 if (ret == 0 && wait) {
480                         wait_on_page_writeback(page);
481                         if (PageError(page))
482                                 ret = -EIO;
483                 }
484                 page_cache_release(page);
485         } else {
486                 list_add(&page->list, &mapping->clean_pages);
487                 spin_unlock(&mapping->page_lock);
488                 unlock_page(page);
489         }
490         return ret;
491 }
492 EXPORT_SYMBOL(write_one_page);
493 
494 /*
495  * For address_spaces which do not use buffers.  Just set the page's dirty bit
496  * and move it to the dirty_pages list.  Also perform space reservation if
497  * required.
498  *
499  * __set_page_dirty_nobuffers() may return -ENOSPC.  But if it does, the page
500  * is still safe, as long as it actually manages to find some blocks at
501  * writeback time.
502  *
503  * This is also used when a single buffer is being dirtied: we want to set the
504  * page dirty in that case, but not all the buffers.  This is a "bottom-up"
505  * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
506  */
507 int __set_page_dirty_nobuffers(struct page *page)
508 {
509         int ret = 0;
510 
511         if (!TestSetPageDirty(page)) {
512                 struct address_space *mapping = page->mapping;
513 
514                 if (mapping) {
515                         spin_lock(&mapping->page_lock);
516                         if (page->mapping) {    /* Race with truncate? */
517                                 BUG_ON(page->mapping != mapping);
518                                 if (!mapping->backing_dev_info->memory_backed)
519                                         inc_page_state(nr_dirty);
520                                 list_del(&page->list);
521                                 list_add(&page->list, &mapping->dirty_pages);
522                         }
523                         spin_unlock(&mapping->page_lock);
524                         if (!PageSwapCache(page))
525                                 __mark_inode_dirty(mapping->host,
526                                                         I_DIRTY_PAGES);
527                 }
528         }
529         return ret;
530 }
531 EXPORT_SYMBOL(__set_page_dirty_nobuffers);
532 
533 /*
534  * set_page_dirty() is racy if the caller has no reference against
535  * page->mapping->host, and if the page is unlocked.  This is because another
536  * CPU could truncate the page off the mapping and then free the mapping.
537  *
538  * Usually, the page _is_ locked, or the caller is a user-space process which
539  * holds a reference on the inode by having an open file.
540  *
541  * In other cases, the page should be locked before running set_page_dirty().
542  */
543 int set_page_dirty_lock(struct page *page)
544 {
545         int ret;
546 
547         lock_page(page);
548         ret = set_page_dirty(page);
549         unlock_page(page);
550         return ret;
551 }
552 EXPORT_SYMBOL(set_page_dirty_lock);
553 
554 /*
555  * Clear a page's dirty flag, while caring for dirty memory accounting. 
556  * Returns true if the page was previously dirty.
557  */
558 int test_clear_page_dirty(struct page *page)
559 {
560         if (TestClearPageDirty(page)) {
561                 struct address_space *mapping = page->mapping;
562 
563                 if (mapping && !mapping->backing_dev_info->memory_backed)
564                         dec_page_state(nr_dirty);
565                 return 1;
566         }
567         return 0;
568 }
569 EXPORT_SYMBOL(test_clear_page_dirty);
570 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp