page-writeback.c 35 KB

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