page-writeback.c 34 KB

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