page-writeback.c 38 KB

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