page-writeback.c 38 KB

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