page-writeback.c 39 KB

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