page-writeback.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  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);
  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. static void laptop_timer_fn(unsigned long unused);
  588. static DEFINE_TIMER(laptop_mode_wb_timer, laptop_timer_fn, 0, 0);
  589. /*
  590. * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs
  591. */
  592. int dirty_writeback_centisecs_handler(ctl_table *table, int write,
  593. void __user *buffer, size_t *length, loff_t *ppos)
  594. {
  595. proc_dointvec(table, write, buffer, length, ppos);
  596. return 0;
  597. }
  598. static void do_laptop_sync(struct work_struct *work)
  599. {
  600. wakeup_flusher_threads(0);
  601. kfree(work);
  602. }
  603. static void laptop_timer_fn(unsigned long unused)
  604. {
  605. struct work_struct *work;
  606. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  607. if (work) {
  608. INIT_WORK(work, do_laptop_sync);
  609. schedule_work(work);
  610. }
  611. }
  612. /*
  613. * We've spun up the disk and we're in laptop mode: schedule writeback
  614. * of all dirty data a few seconds from now. If the flush is already scheduled
  615. * then push it back - the user is still using the disk.
  616. */
  617. void laptop_io_completion(void)
  618. {
  619. mod_timer(&laptop_mode_wb_timer, jiffies + laptop_mode);
  620. }
  621. /*
  622. * We're in laptop mode and we've just synced. The sync's writes will have
  623. * caused another writeback to be scheduled by laptop_io_completion.
  624. * Nothing needs to be written back anymore, so we unschedule the writeback.
  625. */
  626. void laptop_sync_completion(void)
  627. {
  628. del_timer(&laptop_mode_wb_timer);
  629. }
  630. /*
  631. * If ratelimit_pages is too high then we can get into dirty-data overload
  632. * if a large number of processes all perform writes at the same time.
  633. * If it is too low then SMP machines will call the (expensive)
  634. * get_writeback_state too often.
  635. *
  636. * Here we set ratelimit_pages to a level which ensures that when all CPUs are
  637. * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
  638. * thresholds before writeback cuts in.
  639. *
  640. * But the limit should not be set too high. Because it also controls the
  641. * amount of memory which the balance_dirty_pages() caller has to write back.
  642. * If this is too large then the caller will block on the IO queue all the
  643. * time. So limit it to four megabytes - the balance_dirty_pages() caller
  644. * will write six megabyte chunks, max.
  645. */
  646. void writeback_set_ratelimit(void)
  647. {
  648. ratelimit_pages = vm_total_pages / (num_online_cpus() * 32);
  649. if (ratelimit_pages < 16)
  650. ratelimit_pages = 16;
  651. if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024)
  652. ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE;
  653. }
  654. static int __cpuinit
  655. ratelimit_handler(struct notifier_block *self, unsigned long u, void *v)
  656. {
  657. writeback_set_ratelimit();
  658. return NOTIFY_DONE;
  659. }
  660. static struct notifier_block __cpuinitdata ratelimit_nb = {
  661. .notifier_call = ratelimit_handler,
  662. .next = NULL,
  663. };
  664. /*
  665. * Called early on to tune the page writeback dirty limits.
  666. *
  667. * We used to scale dirty pages according to how total memory
  668. * related to pages that could be allocated for buffers (by
  669. * comparing nr_free_buffer_pages() to vm_total_pages.
  670. *
  671. * However, that was when we used "dirty_ratio" to scale with
  672. * all memory, and we don't do that any more. "dirty_ratio"
  673. * is now applied to total non-HIGHPAGE memory (by subtracting
  674. * totalhigh_pages from vm_total_pages), and as such we can't
  675. * get into the old insane situation any more where we had
  676. * large amounts of dirty pages compared to a small amount of
  677. * non-HIGHMEM memory.
  678. *
  679. * But we might still want to scale the dirty_ratio by how
  680. * much memory the box has..
  681. */
  682. void __init page_writeback_init(void)
  683. {
  684. int shift;
  685. writeback_set_ratelimit();
  686. register_cpu_notifier(&ratelimit_nb);
  687. shift = calc_period_shift();
  688. prop_descriptor_init(&vm_completions, shift);
  689. prop_descriptor_init(&vm_dirties, shift);
  690. }
  691. /**
  692. * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
  693. * @mapping: address space structure to write
  694. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  695. * @writepage: function called for each page
  696. * @data: data passed to writepage function
  697. *
  698. * If a page is already under I/O, write_cache_pages() skips it, even
  699. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  700. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  701. * and msync() need to guarantee that all the data which was dirty at the time
  702. * the call was made get new I/O started against them. If wbc->sync_mode is
  703. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  704. * existing IO to complete.
  705. */
  706. int write_cache_pages(struct address_space *mapping,
  707. struct writeback_control *wbc, writepage_t writepage,
  708. void *data)
  709. {
  710. struct backing_dev_info *bdi = mapping->backing_dev_info;
  711. int ret = 0;
  712. int done = 0;
  713. struct pagevec pvec;
  714. int nr_pages;
  715. pgoff_t uninitialized_var(writeback_index);
  716. pgoff_t index;
  717. pgoff_t end; /* Inclusive */
  718. pgoff_t done_index;
  719. int cycled;
  720. int range_whole = 0;
  721. long nr_to_write = wbc->nr_to_write;
  722. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  723. wbc->encountered_congestion = 1;
  724. return 0;
  725. }
  726. pagevec_init(&pvec, 0);
  727. if (wbc->range_cyclic) {
  728. writeback_index = mapping->writeback_index; /* prev offset */
  729. index = writeback_index;
  730. if (index == 0)
  731. cycled = 1;
  732. else
  733. cycled = 0;
  734. end = -1;
  735. } else {
  736. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  737. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  738. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  739. range_whole = 1;
  740. cycled = 1; /* ignore range_cyclic tests */
  741. }
  742. retry:
  743. done_index = index;
  744. while (!done && (index <= end)) {
  745. int i;
  746. nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  747. PAGECACHE_TAG_DIRTY,
  748. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  749. if (nr_pages == 0)
  750. break;
  751. for (i = 0; i < nr_pages; i++) {
  752. struct page *page = pvec.pages[i];
  753. /*
  754. * At this point, the page may be truncated or
  755. * invalidated (changing page->mapping to NULL), or
  756. * even swizzled back from swapper_space to tmpfs file
  757. * mapping. However, page->index will not change
  758. * because we have a reference on the page.
  759. */
  760. if (page->index > end) {
  761. /*
  762. * can't be range_cyclic (1st pass) because
  763. * end == -1 in that case.
  764. */
  765. done = 1;
  766. break;
  767. }
  768. done_index = page->index + 1;
  769. lock_page(page);
  770. /*
  771. * Page truncated or invalidated. We can freely skip it
  772. * then, even for data integrity operations: the page
  773. * has disappeared concurrently, so there could be no
  774. * real expectation of this data interity operation
  775. * even if there is now a new, dirty page at the same
  776. * pagecache address.
  777. */
  778. if (unlikely(page->mapping != mapping)) {
  779. continue_unlock:
  780. unlock_page(page);
  781. continue;
  782. }
  783. if (!PageDirty(page)) {
  784. /* someone wrote it for us */
  785. goto continue_unlock;
  786. }
  787. if (PageWriteback(page)) {
  788. if (wbc->sync_mode != WB_SYNC_NONE)
  789. wait_on_page_writeback(page);
  790. else
  791. goto continue_unlock;
  792. }
  793. BUG_ON(PageWriteback(page));
  794. if (!clear_page_dirty_for_io(page))
  795. goto continue_unlock;
  796. ret = (*writepage)(page, wbc, data);
  797. if (unlikely(ret)) {
  798. if (ret == AOP_WRITEPAGE_ACTIVATE) {
  799. unlock_page(page);
  800. ret = 0;
  801. } else {
  802. /*
  803. * done_index is set past this page,
  804. * so media errors will not choke
  805. * background writeout for the entire
  806. * file. This has consequences for
  807. * range_cyclic semantics (ie. it may
  808. * not be suitable for data integrity
  809. * writeout).
  810. */
  811. done = 1;
  812. break;
  813. }
  814. }
  815. if (nr_to_write > 0) {
  816. nr_to_write--;
  817. if (nr_to_write == 0 &&
  818. wbc->sync_mode == WB_SYNC_NONE) {
  819. /*
  820. * We stop writing back only if we are
  821. * not doing integrity sync. In case of
  822. * integrity sync we have to keep going
  823. * because someone may be concurrently
  824. * dirtying pages, and we might have
  825. * synced a lot of newly appeared dirty
  826. * pages, but have not synced all of the
  827. * old dirty pages.
  828. */
  829. done = 1;
  830. break;
  831. }
  832. }
  833. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  834. wbc->encountered_congestion = 1;
  835. done = 1;
  836. break;
  837. }
  838. }
  839. pagevec_release(&pvec);
  840. cond_resched();
  841. }
  842. if (!cycled && !done) {
  843. /*
  844. * range_cyclic:
  845. * We hit the last page and there is more work to be done: wrap
  846. * back to the start of the file
  847. */
  848. cycled = 1;
  849. index = 0;
  850. end = writeback_index - 1;
  851. goto retry;
  852. }
  853. if (!wbc->no_nrwrite_index_update) {
  854. if (wbc->range_cyclic || (range_whole && nr_to_write > 0))
  855. mapping->writeback_index = done_index;
  856. wbc->nr_to_write = nr_to_write;
  857. }
  858. return ret;
  859. }
  860. EXPORT_SYMBOL(write_cache_pages);
  861. /*
  862. * Function used by generic_writepages to call the real writepage
  863. * function and set the mapping flags on error
  864. */
  865. static int __writepage(struct page *page, struct writeback_control *wbc,
  866. void *data)
  867. {
  868. struct address_space *mapping = data;
  869. int ret = mapping->a_ops->writepage(page, wbc);
  870. mapping_set_error(mapping, ret);
  871. return ret;
  872. }
  873. /**
  874. * generic_writepages - walk the list of dirty pages of the given address space and writepage() all of them.
  875. * @mapping: address space structure to write
  876. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  877. *
  878. * This is a library function, which implements the writepages()
  879. * address_space_operation.
  880. */
  881. int generic_writepages(struct address_space *mapping,
  882. struct writeback_control *wbc)
  883. {
  884. /* deal with chardevs and other special file */
  885. if (!mapping->a_ops->writepage)
  886. return 0;
  887. return write_cache_pages(mapping, wbc, __writepage, mapping);
  888. }
  889. EXPORT_SYMBOL(generic_writepages);
  890. int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
  891. {
  892. int ret;
  893. if (wbc->nr_to_write <= 0)
  894. return 0;
  895. if (mapping->a_ops->writepages)
  896. ret = mapping->a_ops->writepages(mapping, wbc);
  897. else
  898. ret = generic_writepages(mapping, wbc);
  899. return ret;
  900. }
  901. /**
  902. * write_one_page - write out a single page and optionally wait on I/O
  903. * @page: the page to write
  904. * @wait: if true, wait on writeout
  905. *
  906. * The page must be locked by the caller and will be unlocked upon return.
  907. *
  908. * write_one_page() returns a negative error code if I/O failed.
  909. */
  910. int write_one_page(struct page *page, int wait)
  911. {
  912. struct address_space *mapping = page->mapping;
  913. int ret = 0;
  914. struct writeback_control wbc = {
  915. .sync_mode = WB_SYNC_ALL,
  916. .nr_to_write = 1,
  917. };
  918. BUG_ON(!PageLocked(page));
  919. if (wait)
  920. wait_on_page_writeback(page);
  921. if (clear_page_dirty_for_io(page)) {
  922. page_cache_get(page);
  923. ret = mapping->a_ops->writepage(page, &wbc);
  924. if (ret == 0 && wait) {
  925. wait_on_page_writeback(page);
  926. if (PageError(page))
  927. ret = -EIO;
  928. }
  929. page_cache_release(page);
  930. } else {
  931. unlock_page(page);
  932. }
  933. return ret;
  934. }
  935. EXPORT_SYMBOL(write_one_page);
  936. /*
  937. * For address_spaces which do not use buffers nor write back.
  938. */
  939. int __set_page_dirty_no_writeback(struct page *page)
  940. {
  941. if (!PageDirty(page))
  942. SetPageDirty(page);
  943. return 0;
  944. }
  945. /*
  946. * Helper function for set_page_dirty family.
  947. * NOTE: This relies on being atomic wrt interrupts.
  948. */
  949. void account_page_dirtied(struct page *page, struct address_space *mapping)
  950. {
  951. if (mapping_cap_account_dirty(mapping)) {
  952. __inc_zone_page_state(page, NR_FILE_DIRTY);
  953. __inc_bdi_stat(mapping->backing_dev_info, BDI_RECLAIMABLE);
  954. task_dirty_inc(current);
  955. task_io_account_write(PAGE_CACHE_SIZE);
  956. }
  957. }
  958. /*
  959. * For address_spaces which do not use buffers. Just tag the page as dirty in
  960. * its radix tree.
  961. *
  962. * This is also used when a single buffer is being dirtied: we want to set the
  963. * page dirty in that case, but not all the buffers. This is a "bottom-up"
  964. * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
  965. *
  966. * Most callers have locked the page, which pins the address_space in memory.
  967. * But zap_pte_range() does not lock the page, however in that case the
  968. * mapping is pinned by the vma's ->vm_file reference.
  969. *
  970. * We take care to handle the case where the page was truncated from the
  971. * mapping by re-checking page_mapping() inside tree_lock.
  972. */
  973. int __set_page_dirty_nobuffers(struct page *page)
  974. {
  975. if (!TestSetPageDirty(page)) {
  976. struct address_space *mapping = page_mapping(page);
  977. struct address_space *mapping2;
  978. if (!mapping)
  979. return 1;
  980. spin_lock_irq(&mapping->tree_lock);
  981. mapping2 = page_mapping(page);
  982. if (mapping2) { /* Race with truncate? */
  983. BUG_ON(mapping2 != mapping);
  984. WARN_ON_ONCE(!PagePrivate(page) && !PageUptodate(page));
  985. account_page_dirtied(page, mapping);
  986. radix_tree_tag_set(&mapping->page_tree,
  987. page_index(page), PAGECACHE_TAG_DIRTY);
  988. }
  989. spin_unlock_irq(&mapping->tree_lock);
  990. if (mapping->host) {
  991. /* !PageAnon && !swapper_space */
  992. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  993. }
  994. return 1;
  995. }
  996. return 0;
  997. }
  998. EXPORT_SYMBOL(__set_page_dirty_nobuffers);
  999. /*
  1000. * When a writepage implementation decides that it doesn't want to write this
  1001. * page for some reason, it should redirty the locked page via
  1002. * redirty_page_for_writepage() and it should then unlock the page and return 0
  1003. */
  1004. int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page)
  1005. {
  1006. wbc->pages_skipped++;
  1007. return __set_page_dirty_nobuffers(page);
  1008. }
  1009. EXPORT_SYMBOL(redirty_page_for_writepage);
  1010. /*
  1011. * Dirty a page.
  1012. *
  1013. * For pages with a mapping this should be done under the page lock
  1014. * for the benefit of asynchronous memory errors who prefer a consistent
  1015. * dirty state. This rule can be broken in some special cases,
  1016. * but should be better not to.
  1017. *
  1018. * If the mapping doesn't provide a set_page_dirty a_op, then
  1019. * just fall through and assume that it wants buffer_heads.
  1020. */
  1021. int set_page_dirty(struct page *page)
  1022. {
  1023. struct address_space *mapping = page_mapping(page);
  1024. if (likely(mapping)) {
  1025. int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
  1026. #ifdef CONFIG_BLOCK
  1027. if (!spd)
  1028. spd = __set_page_dirty_buffers;
  1029. #endif
  1030. return (*spd)(page);
  1031. }
  1032. if (!PageDirty(page)) {
  1033. if (!TestSetPageDirty(page))
  1034. return 1;
  1035. }
  1036. return 0;
  1037. }
  1038. EXPORT_SYMBOL(set_page_dirty);
  1039. /*
  1040. * set_page_dirty() is racy if the caller has no reference against
  1041. * page->mapping->host, and if the page is unlocked. This is because another
  1042. * CPU could truncate the page off the mapping and then free the mapping.
  1043. *
  1044. * Usually, the page _is_ locked, or the caller is a user-space process which
  1045. * holds a reference on the inode by having an open file.
  1046. *
  1047. * In other cases, the page should be locked before running set_page_dirty().
  1048. */
  1049. int set_page_dirty_lock(struct page *page)
  1050. {
  1051. int ret;
  1052. lock_page_nosync(page);
  1053. ret = set_page_dirty(page);
  1054. unlock_page(page);
  1055. return ret;
  1056. }
  1057. EXPORT_SYMBOL(set_page_dirty_lock);
  1058. /*
  1059. * Clear a page's dirty flag, while caring for dirty memory accounting.
  1060. * Returns true if the page was previously dirty.
  1061. *
  1062. * This is for preparing to put the page under writeout. We leave the page
  1063. * tagged as dirty in the radix tree so that a concurrent write-for-sync
  1064. * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage
  1065. * implementation will run either set_page_writeback() or set_page_dirty(),
  1066. * at which stage we bring the page's dirty flag and radix-tree dirty tag
  1067. * back into sync.
  1068. *
  1069. * This incoherency between the page's dirty flag and radix-tree tag is
  1070. * unfortunate, but it only exists while the page is locked.
  1071. */
  1072. int clear_page_dirty_for_io(struct page *page)
  1073. {
  1074. struct address_space *mapping = page_mapping(page);
  1075. BUG_ON(!PageLocked(page));
  1076. ClearPageReclaim(page);
  1077. if (mapping && mapping_cap_account_dirty(mapping)) {
  1078. /*
  1079. * Yes, Virginia, this is indeed insane.
  1080. *
  1081. * We use this sequence to make sure that
  1082. * (a) we account for dirty stats properly
  1083. * (b) we tell the low-level filesystem to
  1084. * mark the whole page dirty if it was
  1085. * dirty in a pagetable. Only to then
  1086. * (c) clean the page again and return 1 to
  1087. * cause the writeback.
  1088. *
  1089. * This way we avoid all nasty races with the
  1090. * dirty bit in multiple places and clearing
  1091. * them concurrently from different threads.
  1092. *
  1093. * Note! Normally the "set_page_dirty(page)"
  1094. * has no effect on the actual dirty bit - since
  1095. * that will already usually be set. But we
  1096. * need the side effects, and it can help us
  1097. * avoid races.
  1098. *
  1099. * We basically use the page "master dirty bit"
  1100. * as a serialization point for all the different
  1101. * threads doing their things.
  1102. */
  1103. if (page_mkclean(page))
  1104. set_page_dirty(page);
  1105. /*
  1106. * We carefully synchronise fault handlers against
  1107. * installing a dirty pte and marking the page dirty
  1108. * at this point. We do this by having them hold the
  1109. * page lock at some point after installing their
  1110. * pte, but before marking the page dirty.
  1111. * Pages are always locked coming in here, so we get
  1112. * the desired exclusion. See mm/memory.c:do_wp_page()
  1113. * for more comments.
  1114. */
  1115. if (TestClearPageDirty(page)) {
  1116. dec_zone_page_state(page, NR_FILE_DIRTY);
  1117. dec_bdi_stat(mapping->backing_dev_info,
  1118. BDI_RECLAIMABLE);
  1119. return 1;
  1120. }
  1121. return 0;
  1122. }
  1123. return TestClearPageDirty(page);
  1124. }
  1125. EXPORT_SYMBOL(clear_page_dirty_for_io);
  1126. int test_clear_page_writeback(struct page *page)
  1127. {
  1128. struct address_space *mapping = page_mapping(page);
  1129. int ret;
  1130. if (mapping) {
  1131. struct backing_dev_info *bdi = mapping->backing_dev_info;
  1132. unsigned long flags;
  1133. spin_lock_irqsave(&mapping->tree_lock, flags);
  1134. ret = TestClearPageWriteback(page);
  1135. if (ret) {
  1136. radix_tree_tag_clear(&mapping->page_tree,
  1137. page_index(page),
  1138. PAGECACHE_TAG_WRITEBACK);
  1139. if (bdi_cap_account_writeback(bdi)) {
  1140. __dec_bdi_stat(bdi, BDI_WRITEBACK);
  1141. __bdi_writeout_inc(bdi);
  1142. }
  1143. }
  1144. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  1145. } else {
  1146. ret = TestClearPageWriteback(page);
  1147. }
  1148. if (ret)
  1149. dec_zone_page_state(page, NR_WRITEBACK);
  1150. return ret;
  1151. }
  1152. int test_set_page_writeback(struct page *page)
  1153. {
  1154. struct address_space *mapping = page_mapping(page);
  1155. int ret;
  1156. if (mapping) {
  1157. struct backing_dev_info *bdi = mapping->backing_dev_info;
  1158. unsigned long flags;
  1159. spin_lock_irqsave(&mapping->tree_lock, flags);
  1160. ret = TestSetPageWriteback(page);
  1161. if (!ret) {
  1162. radix_tree_tag_set(&mapping->page_tree,
  1163. page_index(page),
  1164. PAGECACHE_TAG_WRITEBACK);
  1165. if (bdi_cap_account_writeback(bdi))
  1166. __inc_bdi_stat(bdi, BDI_WRITEBACK);
  1167. }
  1168. if (!PageDirty(page))
  1169. radix_tree_tag_clear(&mapping->page_tree,
  1170. page_index(page),
  1171. PAGECACHE_TAG_DIRTY);
  1172. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  1173. } else {
  1174. ret = TestSetPageWriteback(page);
  1175. }
  1176. if (!ret)
  1177. inc_zone_page_state(page, NR_WRITEBACK);
  1178. return ret;
  1179. }
  1180. EXPORT_SYMBOL(test_set_page_writeback);
  1181. /*
  1182. * Return true if any of the pages in the mapping are marked with the
  1183. * passed tag.
  1184. */
  1185. int mapping_tagged(struct address_space *mapping, int tag)
  1186. {
  1187. int ret;
  1188. rcu_read_lock();
  1189. ret = radix_tree_tagged(&mapping->page_tree, tag);
  1190. rcu_read_unlock();
  1191. return ret;
  1192. }
  1193. EXPORT_SYMBOL(mapping_tagged);