writeback.h 13 KB

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