writeback.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * include/linux/writeback.h
  3. */
  4. #ifndef WRITEBACK_H
  5. #define WRITEBACK_H
  6. #include <linux/sched.h>
  7. #include <linux/workqueue.h>
  8. #include <linux/fs.h>
  9. #include <linux/flex_proportions.h>
  10. #include <linux/backing-dev-defs.h>
  11. #include <linux/blk_types.h>
  12. struct bio;
  13. DECLARE_PER_CPU(int, dirty_throttle_leaks);
  14. /*
  15. * The 1/4 region under the global dirty thresh is for smooth dirty throttling:
  16. *
  17. * (thresh - thresh/DIRTY_FULL_SCOPE, thresh)
  18. *
  19. * Further beyond, all dirtier tasks will enter a loop waiting (possibly long
  20. * time) for the dirty pages to drop, unless written enough pages.
  21. *
  22. * The global dirty threshold is normally equal to the global dirty limit,
  23. * except when the system suddenly allocates a lot of anonymous memory and
  24. * knocks down the global dirty threshold quickly, in which case the global
  25. * dirty limit will follow down slowly to prevent livelocking all dirtier tasks.
  26. */
  27. #define DIRTY_SCOPE 8
  28. #define DIRTY_FULL_SCOPE (DIRTY_SCOPE / 2)
  29. struct backing_dev_info;
  30. /*
  31. * fs/fs-writeback.c
  32. */
  33. enum writeback_sync_modes {
  34. WB_SYNC_NONE, /* Don't wait on anything */
  35. WB_SYNC_ALL, /* Wait on every mapping */
  36. };
  37. /*
  38. * why some writeback work was initiated
  39. */
  40. enum wb_reason {
  41. WB_REASON_BACKGROUND,
  42. WB_REASON_TRY_TO_FREE_PAGES,
  43. WB_REASON_SYNC,
  44. WB_REASON_PERIODIC,
  45. WB_REASON_LAPTOP_TIMER,
  46. WB_REASON_FREE_MORE_MEM,
  47. WB_REASON_FS_FREE_SPACE,
  48. /*
  49. * There is no bdi forker thread any more and works are done
  50. * by emergency worker, however, this is TPs userland visible
  51. * and we'll be exposing exactly the same information,
  52. * so it has a mismatch name.
  53. */
  54. WB_REASON_FORKER_THREAD,
  55. WB_REASON_MAX,
  56. };
  57. /*
  58. * A control structure which tells the writeback code what to do. These are
  59. * always on the stack, and hence need no locking. They are always initialised
  60. * in a manner such that unspecified fields are set to zero.
  61. */
  62. struct writeback_control {
  63. long nr_to_write; /* Write this many pages, and decrement
  64. this for each page written */
  65. long pages_skipped; /* Pages which were not written */
  66. /*
  67. * For a_ops->writepages(): if start or end are non-zero then this is
  68. * a hint that the filesystem need only write out the pages inside that
  69. * byterange. The byte at `end' is included in the writeout request.
  70. */
  71. loff_t range_start;
  72. loff_t range_end;
  73. enum writeback_sync_modes sync_mode;
  74. unsigned for_kupdate:1; /* A kupdate writeback */
  75. unsigned for_background:1; /* A background writeback */
  76. unsigned tagged_writepages:1; /* tag-and-write to avoid livelock */
  77. unsigned for_reclaim:1; /* Invoked from the page allocator */
  78. unsigned range_cyclic:1; /* range_start is cyclic */
  79. unsigned for_sync:1; /* sync(2) WB_SYNC_ALL writeback */
  80. #ifdef CONFIG_CGROUP_WRITEBACK
  81. struct bdi_writeback *wb; /* wb this writeback is issued under */
  82. struct inode *inode; /* inode being written out */
  83. /* foreign inode detection, see wbc_detach_inode() */
  84. int wb_id; /* current wb id */
  85. int wb_lcand_id; /* last foreign candidate wb id */
  86. int wb_tcand_id; /* this foreign candidate wb id */
  87. size_t wb_bytes; /* bytes written by current wb */
  88. size_t wb_lcand_bytes; /* bytes written by last candidate */
  89. size_t wb_tcand_bytes; /* bytes written by this candidate */
  90. #endif
  91. };
  92. static inline int wbc_to_write_flags(struct writeback_control *wbc)
  93. {
  94. if (wbc->sync_mode == WB_SYNC_ALL)
  95. return REQ_SYNC;
  96. else if (wbc->for_kupdate || wbc->for_background)
  97. return REQ_BACKGROUND;
  98. return 0;
  99. }
  100. /*
  101. * A wb_domain represents a domain that wb's (bdi_writeback's) belong to
  102. * and are measured against each other in. There always is one global
  103. * domain, global_wb_domain, that every wb in the system is a member of.
  104. * This allows measuring the relative bandwidth of each wb to distribute
  105. * dirtyable memory accordingly.
  106. */
  107. struct wb_domain {
  108. spinlock_t lock;
  109. /*
  110. * Scale the writeback cache size proportional to the relative
  111. * writeout speed.
  112. *
  113. * We do this by keeping a floating proportion between BDIs, based
  114. * on page writeback completions [end_page_writeback()]. Those
  115. * devices that write out pages fastest will get the larger share,
  116. * while the slower will get a smaller share.
  117. *
  118. * We use page writeout completions because we are interested in
  119. * getting rid of dirty pages. Having them written out is the
  120. * primary goal.
  121. *
  122. * We introduce a concept of time, a period over which we measure
  123. * these events, because demand can/will vary over time. The length
  124. * of this period itself is measured in page writeback completions.
  125. */
  126. struct fprop_global completions;
  127. struct timer_list period_timer; /* timer for aging of completions */
  128. unsigned long period_time;
  129. /*
  130. * The dirtyable memory and dirty threshold could be suddenly
  131. * knocked down by a large amount (eg. on the startup of KVM in a
  132. * swapless system). This may throw the system into deep dirty
  133. * exceeded state and throttle heavy/light dirtiers alike. To
  134. * retain good responsiveness, maintain global_dirty_limit for
  135. * tracking slowly down to the knocked down dirty threshold.
  136. *
  137. * Both fields are protected by ->lock.
  138. */
  139. unsigned long dirty_limit_tstamp;
  140. unsigned long dirty_limit;
  141. };
  142. /**
  143. * wb_domain_size_changed - memory available to a wb_domain has changed
  144. * @dom: wb_domain of interest
  145. *
  146. * This function should be called when the amount of memory available to
  147. * @dom has changed. It resets @dom's dirty limit parameters to prevent
  148. * the past values which don't match the current configuration from skewing
  149. * dirty throttling. Without this, when memory size of a wb_domain is
  150. * greatly reduced, the dirty throttling logic may allow too many pages to
  151. * be dirtied leading to consecutive unnecessary OOMs and may get stuck in
  152. * that situation.
  153. */
  154. static inline void wb_domain_size_changed(struct wb_domain *dom)
  155. {
  156. spin_lock(&dom->lock);
  157. dom->dirty_limit_tstamp = jiffies;
  158. dom->dirty_limit = 0;
  159. spin_unlock(&dom->lock);
  160. }
  161. /*
  162. * fs/fs-writeback.c
  163. */
  164. struct bdi_writeback;
  165. void writeback_inodes_sb(struct super_block *, enum wb_reason reason);
  166. void writeback_inodes_sb_nr(struct super_block *, unsigned long nr,
  167. enum wb_reason reason);
  168. bool try_to_writeback_inodes_sb(struct super_block *, enum wb_reason reason);
  169. bool try_to_writeback_inodes_sb_nr(struct super_block *, unsigned long nr,
  170. enum wb_reason reason);
  171. void sync_inodes_sb(struct super_block *);
  172. void wakeup_flusher_threads(long nr_pages, enum wb_reason reason);
  173. void inode_wait_for_writeback(struct inode *inode);
  174. /* writeback.h requires fs.h; it, too, is not included from here. */
  175. static inline void wait_on_inode(struct inode *inode)
  176. {
  177. might_sleep();
  178. wait_on_bit(&inode->i_state, __I_NEW, TASK_UNINTERRUPTIBLE);
  179. }
  180. #ifdef CONFIG_CGROUP_WRITEBACK
  181. #include <linux/cgroup.h>
  182. #include <linux/bio.h>
  183. void __inode_attach_wb(struct inode *inode, struct page *page);
  184. void wbc_attach_and_unlock_inode(struct writeback_control *wbc,
  185. struct inode *inode)
  186. __releases(&inode->i_lock);
  187. void wbc_detach_inode(struct writeback_control *wbc);
  188. void wbc_account_io(struct writeback_control *wbc, struct page *page,
  189. size_t bytes);
  190. void cgroup_writeback_umount(void);
  191. /**
  192. * inode_attach_wb - associate an inode with its wb
  193. * @inode: inode of interest
  194. * @page: page being dirtied (may be NULL)
  195. *
  196. * If @inode doesn't have its wb, associate it with the wb matching the
  197. * memcg of @page or, if @page is NULL, %current. May be called w/ or w/o
  198. * @inode->i_lock.
  199. */
  200. static inline void inode_attach_wb(struct inode *inode, struct page *page)
  201. {
  202. if (!inode->i_wb)
  203. __inode_attach_wb(inode, page);
  204. }
  205. /**
  206. * inode_detach_wb - disassociate an inode from its wb
  207. * @inode: inode of interest
  208. *
  209. * @inode is being freed. Detach from its wb.
  210. */
  211. static inline void inode_detach_wb(struct inode *inode)
  212. {
  213. if (inode->i_wb) {
  214. wb_put(inode->i_wb);
  215. inode->i_wb = NULL;
  216. }
  217. }
  218. /**
  219. * wbc_attach_fdatawrite_inode - associate wbc and inode for fdatawrite
  220. * @wbc: writeback_control of interest
  221. * @inode: target inode
  222. *
  223. * This function is to be used by __filemap_fdatawrite_range(), which is an
  224. * alternative entry point into writeback code, and first ensures @inode is
  225. * associated with a bdi_writeback and attaches it to @wbc.
  226. */
  227. static inline void wbc_attach_fdatawrite_inode(struct writeback_control *wbc,
  228. struct inode *inode)
  229. {
  230. spin_lock(&inode->i_lock);
  231. inode_attach_wb(inode, NULL);
  232. wbc_attach_and_unlock_inode(wbc, inode);
  233. }
  234. /**
  235. * wbc_init_bio - writeback specific initializtion of bio
  236. * @wbc: writeback_control for the writeback in progress
  237. * @bio: bio to be initialized
  238. *
  239. * @bio is a part of the writeback in progress controlled by @wbc. Perform
  240. * writeback specific initialization. This is used to apply the cgroup
  241. * writeback context.
  242. */
  243. static inline void wbc_init_bio(struct writeback_control *wbc, struct bio *bio)
  244. {
  245. /*
  246. * pageout() path doesn't attach @wbc to the inode being written
  247. * out. This is intentional as we don't want the function to block
  248. * behind a slow cgroup. Ultimately, we want pageout() to kick off
  249. * regular writeback instead of writing things out itself.
  250. */
  251. if (wbc->wb)
  252. bio_associate_blkcg(bio, wbc->wb->blkcg_css);
  253. }
  254. #else /* CONFIG_CGROUP_WRITEBACK */
  255. static inline void inode_attach_wb(struct inode *inode, struct page *page)
  256. {
  257. }
  258. static inline void inode_detach_wb(struct inode *inode)
  259. {
  260. }
  261. static inline void wbc_attach_and_unlock_inode(struct writeback_control *wbc,
  262. struct inode *inode)
  263. __releases(&inode->i_lock)
  264. {
  265. spin_unlock(&inode->i_lock);
  266. }
  267. static inline void wbc_attach_fdatawrite_inode(struct writeback_control *wbc,
  268. struct inode *inode)
  269. {
  270. }
  271. static inline void wbc_detach_inode(struct writeback_control *wbc)
  272. {
  273. }
  274. static inline void wbc_init_bio(struct writeback_control *wbc, struct bio *bio)
  275. {
  276. }
  277. static inline void wbc_account_io(struct writeback_control *wbc,
  278. struct page *page, size_t bytes)
  279. {
  280. }
  281. static inline void cgroup_writeback_umount(void)
  282. {
  283. }
  284. #endif /* CONFIG_CGROUP_WRITEBACK */
  285. /*
  286. * mm/page-writeback.c
  287. */
  288. #ifdef CONFIG_BLOCK
  289. void laptop_io_completion(struct backing_dev_info *info);
  290. void laptop_sync_completion(void);
  291. void laptop_mode_sync(struct work_struct *work);
  292. void laptop_mode_timer_fn(unsigned long data);
  293. #else
  294. static inline void laptop_sync_completion(void) { }
  295. #endif
  296. bool node_dirty_ok(struct pglist_data *pgdat);
  297. int wb_domain_init(struct wb_domain *dom, gfp_t gfp);
  298. #ifdef CONFIG_CGROUP_WRITEBACK
  299. void wb_domain_exit(struct wb_domain *dom);
  300. #endif
  301. extern struct wb_domain global_wb_domain;
  302. /* These are exported to sysctl. */
  303. extern int dirty_background_ratio;
  304. extern unsigned long dirty_background_bytes;
  305. extern int vm_dirty_ratio;
  306. extern unsigned long vm_dirty_bytes;
  307. extern unsigned int dirty_writeback_interval;
  308. extern unsigned int dirty_expire_interval;
  309. extern unsigned int dirtytime_expire_interval;
  310. extern int vm_highmem_is_dirtyable;
  311. extern int block_dump;
  312. extern int laptop_mode;
  313. extern int dirty_background_ratio_handler(struct ctl_table *table, int write,
  314. void __user *buffer, size_t *lenp,
  315. loff_t *ppos);
  316. extern int dirty_background_bytes_handler(struct ctl_table *table, int write,
  317. void __user *buffer, size_t *lenp,
  318. loff_t *ppos);
  319. extern int dirty_ratio_handler(struct ctl_table *table, int write,
  320. void __user *buffer, size_t *lenp,
  321. loff_t *ppos);
  322. extern int dirty_bytes_handler(struct ctl_table *table, int write,
  323. void __user *buffer, size_t *lenp,
  324. loff_t *ppos);
  325. int dirtytime_interval_handler(struct ctl_table *table, int write,
  326. void __user *buffer, size_t *lenp, loff_t *ppos);
  327. struct ctl_table;
  328. int dirty_writeback_centisecs_handler(struct ctl_table *, int,
  329. void __user *, size_t *, loff_t *);
  330. void global_dirty_limits(unsigned long *pbackground, unsigned long *pdirty);
  331. unsigned long wb_calc_thresh(struct bdi_writeback *wb, unsigned long thresh);
  332. void wb_update_bandwidth(struct bdi_writeback *wb, unsigned long start_time);
  333. void balance_dirty_pages_ratelimited(struct address_space *mapping);
  334. bool wb_over_bg_thresh(struct bdi_writeback *wb);
  335. typedef int (*writepage_t)(struct page *page, struct writeback_control *wbc,
  336. void *data);
  337. int generic_writepages(struct address_space *mapping,
  338. struct writeback_control *wbc);
  339. void tag_pages_for_writeback(struct address_space *mapping,
  340. pgoff_t start, pgoff_t end);
  341. int write_cache_pages(struct address_space *mapping,
  342. struct writeback_control *wbc, writepage_t writepage,
  343. void *data);
  344. int do_writepages(struct address_space *mapping, struct writeback_control *wbc);
  345. void writeback_set_ratelimit(void);
  346. void tag_pages_for_writeback(struct address_space *mapping,
  347. pgoff_t start, pgoff_t end);
  348. void account_page_redirty(struct page *page);
  349. void sb_mark_inode_writeback(struct inode *inode);
  350. void sb_clear_inode_writeback(struct inode *inode);
  351. #endif /* WRITEBACK_H */