page-writeback.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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. .bdi = bdi,
  426. .sync_mode = WB_SYNC_NONE,
  427. .older_than_this = NULL,
  428. .nr_to_write = write_chunk,
  429. .range_cyclic = 1,
  430. };
  431. get_dirty_limits(&background_thresh, &dirty_thresh,
  432. &bdi_thresh, bdi);
  433. nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
  434. global_page_state(NR_UNSTABLE_NFS);
  435. nr_writeback = global_page_state(NR_WRITEBACK);
  436. bdi_nr_reclaimable = bdi_stat(bdi, BDI_RECLAIMABLE);
  437. bdi_nr_writeback = bdi_stat(bdi, BDI_WRITEBACK);
  438. if (bdi_nr_reclaimable + bdi_nr_writeback <= bdi_thresh)
  439. break;
  440. /*
  441. * Throttle it only when the background writeback cannot
  442. * catch-up. This avoids (excessively) small writeouts
  443. * when the bdi limits are ramping up.
  444. */
  445. if (nr_reclaimable + nr_writeback <
  446. (background_thresh + dirty_thresh) / 2)
  447. break;
  448. if (!bdi->dirty_exceeded)
  449. bdi->dirty_exceeded = 1;
  450. /* Note: nr_reclaimable denotes nr_dirty + nr_unstable.
  451. * Unstable writes are a feature of certain networked
  452. * filesystems (i.e. NFS) in which data may have been
  453. * written to the server's write cache, but has not yet
  454. * been flushed to permanent storage.
  455. * Only move pages to writeback if this bdi is over its
  456. * threshold otherwise wait until the disk writes catch
  457. * up.
  458. */
  459. if (bdi_nr_reclaimable > bdi_thresh) {
  460. writeback_inodes_wbc(&wbc);
  461. pages_written += write_chunk - wbc.nr_to_write;
  462. get_dirty_limits(&background_thresh, &dirty_thresh,
  463. &bdi_thresh, bdi);
  464. }
  465. /*
  466. * In order to avoid the stacked BDI deadlock we need
  467. * to ensure we accurately count the 'dirty' pages when
  468. * the threshold is low.
  469. *
  470. * Otherwise it would be possible to get thresh+n pages
  471. * reported dirty, even though there are thresh-m pages
  472. * actually dirty; with m+n sitting in the percpu
  473. * deltas.
  474. */
  475. if (bdi_thresh < 2*bdi_stat_error(bdi)) {
  476. bdi_nr_reclaimable = bdi_stat_sum(bdi, BDI_RECLAIMABLE);
  477. bdi_nr_writeback = bdi_stat_sum(bdi, BDI_WRITEBACK);
  478. } else if (bdi_nr_reclaimable) {
  479. bdi_nr_reclaimable = bdi_stat(bdi, BDI_RECLAIMABLE);
  480. bdi_nr_writeback = bdi_stat(bdi, BDI_WRITEBACK);
  481. }
  482. if (bdi_nr_reclaimable + bdi_nr_writeback <= bdi_thresh)
  483. break;
  484. if (pages_written >= write_chunk)
  485. break; /* We've done our duty */
  486. __set_current_state(TASK_INTERRUPTIBLE);
  487. io_schedule_timeout(pause);
  488. /*
  489. * Increase the delay for each loop, up to our previous
  490. * default of taking a 100ms nap.
  491. */
  492. pause <<= 1;
  493. if (pause > HZ / 10)
  494. pause = HZ / 10;
  495. }
  496. if (bdi_nr_reclaimable + bdi_nr_writeback < bdi_thresh &&
  497. bdi->dirty_exceeded)
  498. bdi->dirty_exceeded = 0;
  499. if (writeback_in_progress(bdi))
  500. return;
  501. /*
  502. * In laptop mode, we wait until hitting the higher threshold before
  503. * starting background writeout, and then write out all the way down
  504. * to the lower threshold. So slow writers cause minimal disk activity.
  505. *
  506. * In normal mode, we start background writeout at the lower
  507. * background_thresh, to keep the amount of dirty memory low.
  508. */
  509. if ((laptop_mode && pages_written) ||
  510. (!laptop_mode && ((global_page_state(NR_FILE_DIRTY)
  511. + global_page_state(NR_UNSTABLE_NFS))
  512. > background_thresh)))
  513. bdi_start_writeback(bdi, NULL, 0, 0);
  514. }
  515. void set_page_dirty_balance(struct page *page, int page_mkwrite)
  516. {
  517. if (set_page_dirty(page) || page_mkwrite) {
  518. struct address_space *mapping = page_mapping(page);
  519. if (mapping)
  520. balance_dirty_pages_ratelimited(mapping);
  521. }
  522. }
  523. static DEFINE_PER_CPU(unsigned long, bdp_ratelimits) = 0;
  524. /**
  525. * balance_dirty_pages_ratelimited_nr - balance dirty memory state
  526. * @mapping: address_space which was dirtied
  527. * @nr_pages_dirtied: number of pages which the caller has just dirtied
  528. *
  529. * Processes which are dirtying memory should call in here once for each page
  530. * which was newly dirtied. The function will periodically check the system's
  531. * dirty state and will initiate writeback if needed.
  532. *
  533. * On really big machines, get_writeback_state is expensive, so try to avoid
  534. * calling it too often (ratelimiting). But once we're over the dirty memory
  535. * limit we decrease the ratelimiting by a lot, to prevent individual processes
  536. * from overshooting the limit by (ratelimit_pages) each.
  537. */
  538. void balance_dirty_pages_ratelimited_nr(struct address_space *mapping,
  539. unsigned long nr_pages_dirtied)
  540. {
  541. unsigned long ratelimit;
  542. unsigned long *p;
  543. ratelimit = ratelimit_pages;
  544. if (mapping->backing_dev_info->dirty_exceeded)
  545. ratelimit = 8;
  546. /*
  547. * Check the rate limiting. Also, we do not want to throttle real-time
  548. * tasks in balance_dirty_pages(). Period.
  549. */
  550. preempt_disable();
  551. p = &__get_cpu_var(bdp_ratelimits);
  552. *p += nr_pages_dirtied;
  553. if (unlikely(*p >= ratelimit)) {
  554. ratelimit = sync_writeback_pages(*p);
  555. *p = 0;
  556. preempt_enable();
  557. balance_dirty_pages(mapping, ratelimit);
  558. return;
  559. }
  560. preempt_enable();
  561. }
  562. EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr);
  563. void throttle_vm_writeout(gfp_t gfp_mask)
  564. {
  565. unsigned long background_thresh;
  566. unsigned long dirty_thresh;
  567. for ( ; ; ) {
  568. get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL);
  569. /*
  570. * Boost the allowable dirty threshold a bit for page
  571. * allocators so they don't get DoS'ed by heavy writers
  572. */
  573. dirty_thresh += dirty_thresh / 10; /* wheeee... */
  574. if (global_page_state(NR_UNSTABLE_NFS) +
  575. global_page_state(NR_WRITEBACK) <= dirty_thresh)
  576. break;
  577. congestion_wait(BLK_RW_ASYNC, HZ/10);
  578. /*
  579. * The caller might hold locks which can prevent IO completion
  580. * or progress in the filesystem. So we cannot just sit here
  581. * waiting for IO to complete.
  582. */
  583. if ((gfp_mask & (__GFP_FS|__GFP_IO)) != (__GFP_FS|__GFP_IO))
  584. break;
  585. }
  586. }
  587. /*
  588. * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs
  589. */
  590. int dirty_writeback_centisecs_handler(ctl_table *table, int write,
  591. void __user *buffer, size_t *length, loff_t *ppos)
  592. {
  593. proc_dointvec(table, write, buffer, length, ppos);
  594. bdi_arm_supers_timer();
  595. return 0;
  596. }
  597. #ifdef CONFIG_BLOCK
  598. void laptop_mode_timer_fn(unsigned long data)
  599. {
  600. struct request_queue *q = (struct request_queue *)data;
  601. int nr_pages = global_page_state(NR_FILE_DIRTY) +
  602. global_page_state(NR_UNSTABLE_NFS);
  603. /*
  604. * We want to write everything out, not just down to the dirty
  605. * threshold
  606. */
  607. if (bdi_has_dirty_io(&q->backing_dev_info))
  608. bdi_start_writeback(&q->backing_dev_info, NULL, nr_pages, 0);
  609. }
  610. /*
  611. * We've spun up the disk and we're in laptop mode: schedule writeback
  612. * of all dirty data a few seconds from now. If the flush is already scheduled
  613. * then push it back - the user is still using the disk.
  614. */
  615. void laptop_io_completion(struct backing_dev_info *info)
  616. {
  617. mod_timer(&info->laptop_mode_wb_timer, jiffies + laptop_mode);
  618. }
  619. /*
  620. * We're in laptop mode and we've just synced. The sync's writes will have
  621. * caused another writeback to be scheduled by laptop_io_completion.
  622. * Nothing needs to be written back anymore, so we unschedule the writeback.
  623. */
  624. void laptop_sync_completion(void)
  625. {
  626. struct backing_dev_info *bdi;
  627. rcu_read_lock();
  628. list_for_each_entry_rcu(bdi, &bdi_list, bdi_list)
  629. del_timer(&bdi->laptop_mode_wb_timer);
  630. rcu_read_unlock();
  631. }
  632. #endif
  633. /*
  634. * If ratelimit_pages is too high then we can get into dirty-data overload
  635. * if a large number of processes all perform writes at the same time.
  636. * If it is too low then SMP machines will call the (expensive)
  637. * get_writeback_state too often.
  638. *
  639. * Here we set ratelimit_pages to a level which ensures that when all CPUs are
  640. * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
  641. * thresholds before writeback cuts in.
  642. *
  643. * But the limit should not be set too high. Because it also controls the
  644. * amount of memory which the balance_dirty_pages() caller has to write back.
  645. * If this is too large then the caller will block on the IO queue all the
  646. * time. So limit it to four megabytes - the balance_dirty_pages() caller
  647. * will write six megabyte chunks, max.
  648. */
  649. void writeback_set_ratelimit(void)
  650. {
  651. ratelimit_pages = vm_total_pages / (num_online_cpus() * 32);
  652. if (ratelimit_pages < 16)
  653. ratelimit_pages = 16;
  654. if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024)
  655. ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE;
  656. }
  657. static int __cpuinit
  658. ratelimit_handler(struct notifier_block *self, unsigned long u, void *v)
  659. {
  660. writeback_set_ratelimit();
  661. return NOTIFY_DONE;
  662. }
  663. static struct notifier_block __cpuinitdata ratelimit_nb = {
  664. .notifier_call = ratelimit_handler,
  665. .next = NULL,
  666. };
  667. /*
  668. * Called early on to tune the page writeback dirty limits.
  669. *
  670. * We used to scale dirty pages according to how total memory
  671. * related to pages that could be allocated for buffers (by
  672. * comparing nr_free_buffer_pages() to vm_total_pages.
  673. *
  674. * However, that was when we used "dirty_ratio" to scale with
  675. * all memory, and we don't do that any more. "dirty_ratio"
  676. * is now applied to total non-HIGHPAGE memory (by subtracting
  677. * totalhigh_pages from vm_total_pages), and as such we can't
  678. * get into the old insane situation any more where we had
  679. * large amounts of dirty pages compared to a small amount of
  680. * non-HIGHMEM memory.
  681. *
  682. * But we might still want to scale the dirty_ratio by how
  683. * much memory the box has..
  684. */
  685. void __init page_writeback_init(void)
  686. {
  687. int shift;
  688. writeback_set_ratelimit();
  689. register_cpu_notifier(&ratelimit_nb);
  690. shift = calc_period_shift();
  691. prop_descriptor_init(&vm_completions, shift);
  692. prop_descriptor_init(&vm_dirties, shift);
  693. }
  694. /**
  695. * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
  696. * @mapping: address space structure to write
  697. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  698. * @writepage: function called for each page
  699. * @data: data passed to writepage function
  700. *
  701. * If a page is already under I/O, write_cache_pages() skips it, even
  702. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  703. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  704. * and msync() need to guarantee that all the data which was dirty at the time
  705. * the call was made get new I/O started against them. If wbc->sync_mode is
  706. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  707. * existing IO to complete.
  708. */
  709. int write_cache_pages(struct address_space *mapping,
  710. struct writeback_control *wbc, writepage_t writepage,
  711. void *data)
  712. {
  713. int ret = 0;
  714. int done = 0;
  715. struct pagevec pvec;
  716. int nr_pages;
  717. pgoff_t uninitialized_var(writeback_index);
  718. pgoff_t index;
  719. pgoff_t end; /* Inclusive */
  720. pgoff_t done_index;
  721. int cycled;
  722. int range_whole = 0;
  723. long nr_to_write = wbc->nr_to_write;
  724. pagevec_init(&pvec, 0);
  725. if (wbc->range_cyclic) {
  726. writeback_index = mapping->writeback_index; /* prev offset */
  727. index = writeback_index;
  728. if (index == 0)
  729. cycled = 1;
  730. else
  731. cycled = 0;
  732. end = -1;
  733. } else {
  734. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  735. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  736. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  737. range_whole = 1;
  738. cycled = 1; /* ignore range_cyclic tests */
  739. }
  740. retry:
  741. done_index = index;
  742. while (!done && (index <= end)) {
  743. int i;
  744. nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  745. PAGECACHE_TAG_DIRTY,
  746. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  747. if (nr_pages == 0)
  748. break;
  749. for (i = 0; i < nr_pages; i++) {
  750. struct page *page = pvec.pages[i];
  751. /*
  752. * At this point, the page may be truncated or
  753. * invalidated (changing page->mapping to NULL), or
  754. * even swizzled back from swapper_space to tmpfs file
  755. * mapping. However, page->index will not change
  756. * because we have a reference on the page.
  757. */
  758. if (page->index > end) {
  759. /*
  760. * can't be range_cyclic (1st pass) because
  761. * end == -1 in that case.
  762. */
  763. done = 1;
  764. break;
  765. }
  766. done_index = page->index + 1;
  767. lock_page(page);
  768. /*
  769. * Page truncated or invalidated. We can freely skip it
  770. * then, even for data integrity operations: the page
  771. * has disappeared concurrently, so there could be no
  772. * real expectation of this data interity operation
  773. * even if there is now a new, dirty page at the same
  774. * pagecache address.
  775. */
  776. if (unlikely(page->mapping != mapping)) {
  777. continue_unlock:
  778. unlock_page(page);
  779. continue;
  780. }
  781. if (!PageDirty(page)) {
  782. /* someone wrote it for us */
  783. goto continue_unlock;
  784. }
  785. if (PageWriteback(page)) {
  786. if (wbc->sync_mode != WB_SYNC_NONE)
  787. wait_on_page_writeback(page);
  788. else
  789. goto continue_unlock;
  790. }
  791. BUG_ON(PageWriteback(page));
  792. if (!clear_page_dirty_for_io(page))
  793. goto continue_unlock;
  794. ret = (*writepage)(page, wbc, data);
  795. if (unlikely(ret)) {
  796. if (ret == AOP_WRITEPAGE_ACTIVATE) {
  797. unlock_page(page);
  798. ret = 0;
  799. } else {
  800. /*
  801. * done_index is set past this page,
  802. * so media errors will not choke
  803. * background writeout for the entire
  804. * file. This has consequences for
  805. * range_cyclic semantics (ie. it may
  806. * not be suitable for data integrity
  807. * writeout).
  808. */
  809. done = 1;
  810. break;
  811. }
  812. }
  813. if (nr_to_write > 0) {
  814. nr_to_write--;
  815. if (nr_to_write == 0 &&
  816. wbc->sync_mode == WB_SYNC_NONE) {
  817. /*
  818. * We stop writing back only if we are
  819. * not doing integrity sync. In case of
  820. * integrity sync we have to keep going
  821. * because someone may be concurrently
  822. * dirtying pages, and we might have
  823. * synced a lot of newly appeared dirty
  824. * pages, but have not synced all of the
  825. * old dirty pages.
  826. */
  827. done = 1;
  828. break;
  829. }
  830. }
  831. }
  832. pagevec_release(&pvec);
  833. cond_resched();
  834. }
  835. if (!cycled && !done) {
  836. /*
  837. * range_cyclic:
  838. * We hit the last page and there is more work to be done: wrap
  839. * back to the start of the file
  840. */
  841. cycled = 1;
  842. index = 0;
  843. end = writeback_index - 1;
  844. goto retry;
  845. }
  846. if (!wbc->no_nrwrite_index_update) {
  847. if (wbc->range_cyclic || (range_whole && nr_to_write > 0))
  848. mapping->writeback_index = done_index;
  849. wbc->nr_to_write = nr_to_write;
  850. }
  851. return ret;
  852. }
  853. EXPORT_SYMBOL(write_cache_pages);
  854. /*
  855. * Function used by generic_writepages to call the real writepage
  856. * function and set the mapping flags on error
  857. */
  858. static int __writepage(struct page *page, struct writeback_control *wbc,
  859. void *data)
  860. {
  861. struct address_space *mapping = data;
  862. int ret = mapping->a_ops->writepage(page, wbc);
  863. mapping_set_error(mapping, ret);
  864. return ret;
  865. }
  866. /**
  867. * generic_writepages - walk the list of dirty pages of the given address space and writepage() all of them.
  868. * @mapping: address space structure to write
  869. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  870. *
  871. * This is a library function, which implements the writepages()
  872. * address_space_operation.
  873. */
  874. int generic_writepages(struct address_space *mapping,
  875. struct writeback_control *wbc)
  876. {
  877. /* deal with chardevs and other special file */
  878. if (!mapping->a_ops->writepage)
  879. return 0;
  880. return write_cache_pages(mapping, wbc, __writepage, mapping);
  881. }
  882. EXPORT_SYMBOL(generic_writepages);
  883. int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
  884. {
  885. int ret;
  886. if (wbc->nr_to_write <= 0)
  887. return 0;
  888. if (mapping->a_ops->writepages)
  889. ret = mapping->a_ops->writepages(mapping, wbc);
  890. else
  891. ret = generic_writepages(mapping, wbc);
  892. return ret;
  893. }
  894. /**
  895. * write_one_page - write out a single page and optionally wait on I/O
  896. * @page: the page to write
  897. * @wait: if true, wait on writeout
  898. *
  899. * The page must be locked by the caller and will be unlocked upon return.
  900. *
  901. * write_one_page() returns a negative error code if I/O failed.
  902. */
  903. int write_one_page(struct page *page, int wait)
  904. {
  905. struct address_space *mapping = page->mapping;
  906. int ret = 0;
  907. struct writeback_control wbc = {
  908. .sync_mode = WB_SYNC_ALL,
  909. .nr_to_write = 1,
  910. };
  911. BUG_ON(!PageLocked(page));
  912. if (wait)
  913. wait_on_page_writeback(page);
  914. if (clear_page_dirty_for_io(page)) {
  915. page_cache_get(page);
  916. ret = mapping->a_ops->writepage(page, &wbc);
  917. if (ret == 0 && wait) {
  918. wait_on_page_writeback(page);
  919. if (PageError(page))
  920. ret = -EIO;
  921. }
  922. page_cache_release(page);
  923. } else {
  924. unlock_page(page);
  925. }
  926. return ret;
  927. }
  928. EXPORT_SYMBOL(write_one_page);
  929. /*
  930. * For address_spaces which do not use buffers nor write back.
  931. */
  932. int __set_page_dirty_no_writeback(struct page *page)
  933. {
  934. if (!PageDirty(page))
  935. SetPageDirty(page);
  936. return 0;
  937. }
  938. /*
  939. * Helper function for set_page_dirty family.
  940. * NOTE: This relies on being atomic wrt interrupts.
  941. */
  942. void account_page_dirtied(struct page *page, struct address_space *mapping)
  943. {
  944. if (mapping_cap_account_dirty(mapping)) {
  945. __inc_zone_page_state(page, NR_FILE_DIRTY);
  946. __inc_bdi_stat(mapping->backing_dev_info, BDI_RECLAIMABLE);
  947. task_dirty_inc(current);
  948. task_io_account_write(PAGE_CACHE_SIZE);
  949. }
  950. }
  951. /*
  952. * For address_spaces which do not use buffers. Just tag the page as dirty in
  953. * its radix tree.
  954. *
  955. * This is also used when a single buffer is being dirtied: we want to set the
  956. * page dirty in that case, but not all the buffers. This is a "bottom-up"
  957. * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
  958. *
  959. * Most callers have locked the page, which pins the address_space in memory.
  960. * But zap_pte_range() does not lock the page, however in that case the
  961. * mapping is pinned by the vma's ->vm_file reference.
  962. *
  963. * We take care to handle the case where the page was truncated from the
  964. * mapping by re-checking page_mapping() inside tree_lock.
  965. */
  966. int __set_page_dirty_nobuffers(struct page *page)
  967. {
  968. if (!TestSetPageDirty(page)) {
  969. struct address_space *mapping = page_mapping(page);
  970. struct address_space *mapping2;
  971. if (!mapping)
  972. return 1;
  973. spin_lock_irq(&mapping->tree_lock);
  974. mapping2 = page_mapping(page);
  975. if (mapping2) { /* Race with truncate? */
  976. BUG_ON(mapping2 != mapping);
  977. WARN_ON_ONCE(!PagePrivate(page) && !PageUptodate(page));
  978. account_page_dirtied(page, mapping);
  979. radix_tree_tag_set(&mapping->page_tree,
  980. page_index(page), PAGECACHE_TAG_DIRTY);
  981. }
  982. spin_unlock_irq(&mapping->tree_lock);
  983. if (mapping->host) {
  984. /* !PageAnon && !swapper_space */
  985. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  986. }
  987. return 1;
  988. }
  989. return 0;
  990. }
  991. EXPORT_SYMBOL(__set_page_dirty_nobuffers);
  992. /*
  993. * When a writepage implementation decides that it doesn't want to write this
  994. * page for some reason, it should redirty the locked page via
  995. * redirty_page_for_writepage() and it should then unlock the page and return 0
  996. */
  997. int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page)
  998. {
  999. wbc->pages_skipped++;
  1000. return __set_page_dirty_nobuffers(page);
  1001. }
  1002. EXPORT_SYMBOL(redirty_page_for_writepage);
  1003. /*
  1004. * Dirty a page.
  1005. *
  1006. * For pages with a mapping this should be done under the page lock
  1007. * for the benefit of asynchronous memory errors who prefer a consistent
  1008. * dirty state. This rule can be broken in some special cases,
  1009. * but should be better not to.
  1010. *
  1011. * If the mapping doesn't provide a set_page_dirty a_op, then
  1012. * just fall through and assume that it wants buffer_heads.
  1013. */
  1014. int set_page_dirty(struct page *page)
  1015. {
  1016. struct address_space *mapping = page_mapping(page);
  1017. if (likely(mapping)) {
  1018. int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
  1019. #ifdef CONFIG_BLOCK
  1020. if (!spd)
  1021. spd = __set_page_dirty_buffers;
  1022. #endif
  1023. return (*spd)(page);
  1024. }
  1025. if (!PageDirty(page)) {
  1026. if (!TestSetPageDirty(page))
  1027. return 1;
  1028. }
  1029. return 0;
  1030. }
  1031. EXPORT_SYMBOL(set_page_dirty);
  1032. /*
  1033. * set_page_dirty() is racy if the caller has no reference against
  1034. * page->mapping->host, and if the page is unlocked. This is because another
  1035. * CPU could truncate the page off the mapping and then free the mapping.
  1036. *
  1037. * Usually, the page _is_ locked, or the caller is a user-space process which
  1038. * holds a reference on the inode by having an open file.
  1039. *
  1040. * In other cases, the page should be locked before running set_page_dirty().
  1041. */
  1042. int set_page_dirty_lock(struct page *page)
  1043. {
  1044. int ret;
  1045. lock_page_nosync(page);
  1046. ret = set_page_dirty(page);
  1047. unlock_page(page);
  1048. return ret;
  1049. }
  1050. EXPORT_SYMBOL(set_page_dirty_lock);
  1051. /*
  1052. * Clear a page's dirty flag, while caring for dirty memory accounting.
  1053. * Returns true if the page was previously dirty.
  1054. *
  1055. * This is for preparing to put the page under writeout. We leave the page
  1056. * tagged as dirty in the radix tree so that a concurrent write-for-sync
  1057. * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage
  1058. * implementation will run either set_page_writeback() or set_page_dirty(),
  1059. * at which stage we bring the page's dirty flag and radix-tree dirty tag
  1060. * back into sync.
  1061. *
  1062. * This incoherency between the page's dirty flag and radix-tree tag is
  1063. * unfortunate, but it only exists while the page is locked.
  1064. */
  1065. int clear_page_dirty_for_io(struct page *page)
  1066. {
  1067. struct address_space *mapping = page_mapping(page);
  1068. BUG_ON(!PageLocked(page));
  1069. ClearPageReclaim(page);
  1070. if (mapping && mapping_cap_account_dirty(mapping)) {
  1071. /*
  1072. * Yes, Virginia, this is indeed insane.
  1073. *
  1074. * We use this sequence to make sure that
  1075. * (a) we account for dirty stats properly
  1076. * (b) we tell the low-level filesystem to
  1077. * mark the whole page dirty if it was
  1078. * dirty in a pagetable. Only to then
  1079. * (c) clean the page again and return 1 to
  1080. * cause the writeback.
  1081. *
  1082. * This way we avoid all nasty races with the
  1083. * dirty bit in multiple places and clearing
  1084. * them concurrently from different threads.
  1085. *
  1086. * Note! Normally the "set_page_dirty(page)"
  1087. * has no effect on the actual dirty bit - since
  1088. * that will already usually be set. But we
  1089. * need the side effects, and it can help us
  1090. * avoid races.
  1091. *
  1092. * We basically use the page "master dirty bit"
  1093. * as a serialization point for all the different
  1094. * threads doing their things.
  1095. */
  1096. if (page_mkclean(page))
  1097. set_page_dirty(page);
  1098. /*
  1099. * We carefully synchronise fault handlers against
  1100. * installing a dirty pte and marking the page dirty
  1101. * at this point. We do this by having them hold the
  1102. * page lock at some point after installing their
  1103. * pte, but before marking the page dirty.
  1104. * Pages are always locked coming in here, so we get
  1105. * the desired exclusion. See mm/memory.c:do_wp_page()
  1106. * for more comments.
  1107. */
  1108. if (TestClearPageDirty(page)) {
  1109. dec_zone_page_state(page, NR_FILE_DIRTY);
  1110. dec_bdi_stat(mapping->backing_dev_info,
  1111. BDI_RECLAIMABLE);
  1112. return 1;
  1113. }
  1114. return 0;
  1115. }
  1116. return TestClearPageDirty(page);
  1117. }
  1118. EXPORT_SYMBOL(clear_page_dirty_for_io);
  1119. int test_clear_page_writeback(struct page *page)
  1120. {
  1121. struct address_space *mapping = page_mapping(page);
  1122. int ret;
  1123. if (mapping) {
  1124. struct backing_dev_info *bdi = mapping->backing_dev_info;
  1125. unsigned long flags;
  1126. spin_lock_irqsave(&mapping->tree_lock, flags);
  1127. ret = TestClearPageWriteback(page);
  1128. if (ret) {
  1129. radix_tree_tag_clear(&mapping->page_tree,
  1130. page_index(page),
  1131. PAGECACHE_TAG_WRITEBACK);
  1132. if (bdi_cap_account_writeback(bdi)) {
  1133. __dec_bdi_stat(bdi, BDI_WRITEBACK);
  1134. __bdi_writeout_inc(bdi);
  1135. }
  1136. }
  1137. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  1138. } else {
  1139. ret = TestClearPageWriteback(page);
  1140. }
  1141. if (ret)
  1142. dec_zone_page_state(page, NR_WRITEBACK);
  1143. return ret;
  1144. }
  1145. int test_set_page_writeback(struct page *page)
  1146. {
  1147. struct address_space *mapping = page_mapping(page);
  1148. int ret;
  1149. if (mapping) {
  1150. struct backing_dev_info *bdi = mapping->backing_dev_info;
  1151. unsigned long flags;
  1152. spin_lock_irqsave(&mapping->tree_lock, flags);
  1153. ret = TestSetPageWriteback(page);
  1154. if (!ret) {
  1155. radix_tree_tag_set(&mapping->page_tree,
  1156. page_index(page),
  1157. PAGECACHE_TAG_WRITEBACK);
  1158. if (bdi_cap_account_writeback(bdi))
  1159. __inc_bdi_stat(bdi, BDI_WRITEBACK);
  1160. }
  1161. if (!PageDirty(page))
  1162. radix_tree_tag_clear(&mapping->page_tree,
  1163. page_index(page),
  1164. PAGECACHE_TAG_DIRTY);
  1165. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  1166. } else {
  1167. ret = TestSetPageWriteback(page);
  1168. }
  1169. if (!ret)
  1170. inc_zone_page_state(page, NR_WRITEBACK);
  1171. return ret;
  1172. }
  1173. EXPORT_SYMBOL(test_set_page_writeback);
  1174. /*
  1175. * Return true if any of the pages in the mapping are marked with the
  1176. * passed tag.
  1177. */
  1178. int mapping_tagged(struct address_space *mapping, int tag)
  1179. {
  1180. int ret;
  1181. rcu_read_lock();
  1182. ret = radix_tree_tagged(&mapping->page_tree, tag);
  1183. rcu_read_unlock();
  1184. return ret;
  1185. }
  1186. EXPORT_SYMBOL(mapping_tagged);