page-writeback.c 36 KB

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