page-writeback.c 35 KB

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