page-writeback.c 36 KB

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