page-writeback.c 38 KB

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