page-writeback.c 40 KB

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