backing-dev.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * include/linux/backing-dev.h
  4. *
  5. * low-level device information and state which is propagated up through
  6. * to high-level code.
  7. */
  8. #ifndef _LINUX_BACKING_DEV_H
  9. #define _LINUX_BACKING_DEV_H
  10. #include <linux/kernel.h>
  11. #include <linux/fs.h>
  12. #include <linux/sched.h>
  13. #include <linux/blkdev.h>
  14. #include <linux/device.h>
  15. #include <linux/writeback.h>
  16. #include <linux/blk-cgroup.h>
  17. #include <linux/backing-dev-defs.h>
  18. #include <linux/slab.h>
  19. static inline struct backing_dev_info *bdi_get(struct backing_dev_info *bdi)
  20. {
  21. kref_get(&bdi->refcnt);
  22. return bdi;
  23. }
  24. struct backing_dev_info *bdi_get_by_id(u64 id);
  25. void bdi_put(struct backing_dev_info *bdi);
  26. __printf(2, 3)
  27. int bdi_register(struct backing_dev_info *bdi, const char *fmt, ...);
  28. __printf(2, 0)
  29. int bdi_register_va(struct backing_dev_info *bdi, const char *fmt,
  30. va_list args);
  31. void bdi_set_owner(struct backing_dev_info *bdi, struct device *owner);
  32. void bdi_unregister(struct backing_dev_info *bdi);
  33. struct backing_dev_info *bdi_alloc(int node_id);
  34. void wb_start_background_writeback(struct bdi_writeback *wb);
  35. void wb_workfn(struct work_struct *work);
  36. void wb_wakeup_delayed(struct bdi_writeback *wb);
  37. void wb_wait_for_completion(struct wb_completion *done);
  38. extern spinlock_t bdi_lock;
  39. extern struct list_head bdi_list;
  40. extern struct workqueue_struct *bdi_wq;
  41. extern struct workqueue_struct *bdi_async_bio_wq;
  42. static inline bool wb_has_dirty_io(struct bdi_writeback *wb)
  43. {
  44. return test_bit(WB_has_dirty_io, &wb->state);
  45. }
  46. static inline bool bdi_has_dirty_io(struct backing_dev_info *bdi)
  47. {
  48. /*
  49. * @bdi->tot_write_bandwidth is guaranteed to be > 0 if there are
  50. * any dirty wbs. See wb_update_write_bandwidth().
  51. */
  52. return atomic_long_read(&bdi->tot_write_bandwidth);
  53. }
  54. static inline void __add_wb_stat(struct bdi_writeback *wb,
  55. enum wb_stat_item item, s64 amount)
  56. {
  57. percpu_counter_add_batch(&wb->stat[item], amount, WB_STAT_BATCH);
  58. }
  59. static inline void inc_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
  60. {
  61. __add_wb_stat(wb, item, 1);
  62. }
  63. static inline void dec_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
  64. {
  65. __add_wb_stat(wb, item, -1);
  66. }
  67. static inline s64 wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
  68. {
  69. return percpu_counter_read_positive(&wb->stat[item]);
  70. }
  71. static inline s64 wb_stat_sum(struct bdi_writeback *wb, enum wb_stat_item item)
  72. {
  73. return percpu_counter_sum_positive(&wb->stat[item]);
  74. }
  75. extern void wb_writeout_inc(struct bdi_writeback *wb);
  76. /*
  77. * maximal error of a stat counter.
  78. */
  79. static inline unsigned long wb_stat_error(void)
  80. {
  81. #ifdef CONFIG_SMP
  82. return nr_cpu_ids * WB_STAT_BATCH;
  83. #else
  84. return 1;
  85. #endif
  86. }
  87. int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio);
  88. int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio);
  89. /*
  90. * Flags in backing_dev_info::capability
  91. *
  92. * BDI_CAP_WRITEBACK: Supports dirty page writeback, and dirty pages
  93. * should contribute to accounting
  94. * BDI_CAP_WRITEBACK_ACCT: Automatically account writeback pages
  95. * BDI_CAP_STRICTLIMIT: Keep number of dirty pages below bdi threshold
  96. */
  97. #define BDI_CAP_WRITEBACK (1 << 0)
  98. #define BDI_CAP_WRITEBACK_ACCT (1 << 1)
  99. #define BDI_CAP_STRICTLIMIT (1 << 2)
  100. extern struct backing_dev_info noop_backing_dev_info;
  101. /**
  102. * writeback_in_progress - determine whether there is writeback in progress
  103. * @wb: bdi_writeback of interest
  104. *
  105. * Determine whether there is writeback waiting to be handled against a
  106. * bdi_writeback.
  107. */
  108. static inline bool writeback_in_progress(struct bdi_writeback *wb)
  109. {
  110. return test_bit(WB_writeback_running, &wb->state);
  111. }
  112. static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
  113. {
  114. struct super_block *sb;
  115. if (!inode)
  116. return &noop_backing_dev_info;
  117. sb = inode->i_sb;
  118. #ifdef CONFIG_BLOCK
  119. if (sb_is_blkdev_sb(sb))
  120. return I_BDEV(inode)->bd_bdi;
  121. #endif
  122. return sb->s_bdi;
  123. }
  124. static inline int wb_congested(struct bdi_writeback *wb, int cong_bits)
  125. {
  126. return wb->congested & cong_bits;
  127. }
  128. long congestion_wait(int sync, long timeout);
  129. long wait_iff_congested(int sync, long timeout);
  130. static inline bool mapping_can_writeback(struct address_space *mapping)
  131. {
  132. return inode_to_bdi(mapping->host)->capabilities & BDI_CAP_WRITEBACK;
  133. }
  134. static inline int bdi_sched_wait(void *word)
  135. {
  136. schedule();
  137. return 0;
  138. }
  139. #ifdef CONFIG_CGROUP_WRITEBACK
  140. struct bdi_writeback *wb_get_lookup(struct backing_dev_info *bdi,
  141. struct cgroup_subsys_state *memcg_css);
  142. struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
  143. struct cgroup_subsys_state *memcg_css,
  144. gfp_t gfp);
  145. void wb_memcg_offline(struct mem_cgroup *memcg);
  146. void wb_blkcg_offline(struct blkcg *blkcg);
  147. int inode_congested(struct inode *inode, int cong_bits);
  148. /**
  149. * inode_cgwb_enabled - test whether cgroup writeback is enabled on an inode
  150. * @inode: inode of interest
  151. *
  152. * Cgroup writeback requires support from the filesystem. Also, both memcg and
  153. * iocg have to be on the default hierarchy. Test whether all conditions are
  154. * met.
  155. *
  156. * Note that the test result may change dynamically on the same inode
  157. * depending on how memcg and iocg are configured.
  158. */
  159. static inline bool inode_cgwb_enabled(struct inode *inode)
  160. {
  161. struct backing_dev_info *bdi = inode_to_bdi(inode);
  162. return cgroup_subsys_on_dfl(memory_cgrp_subsys) &&
  163. cgroup_subsys_on_dfl(io_cgrp_subsys) &&
  164. (bdi->capabilities & BDI_CAP_WRITEBACK) &&
  165. (inode->i_sb->s_iflags & SB_I_CGROUPWB);
  166. }
  167. /**
  168. * wb_find_current - find wb for %current on a bdi
  169. * @bdi: bdi of interest
  170. *
  171. * Find the wb of @bdi which matches both the memcg and blkcg of %current.
  172. * Must be called under rcu_read_lock() which protects the returend wb.
  173. * NULL if not found.
  174. */
  175. static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
  176. {
  177. struct cgroup_subsys_state *memcg_css;
  178. struct bdi_writeback *wb;
  179. memcg_css = task_css(current, memory_cgrp_id);
  180. if (!memcg_css->parent)
  181. return &bdi->wb;
  182. wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
  183. /*
  184. * %current's blkcg equals the effective blkcg of its memcg. No
  185. * need to use the relatively expensive cgroup_get_e_css().
  186. */
  187. if (likely(wb && wb->blkcg_css == task_css(current, io_cgrp_id)))
  188. return wb;
  189. return NULL;
  190. }
  191. /**
  192. * wb_get_create_current - get or create wb for %current on a bdi
  193. * @bdi: bdi of interest
  194. * @gfp: allocation mask
  195. *
  196. * Equivalent to wb_get_create() on %current's memcg. This function is
  197. * called from a relatively hot path and optimizes the common cases using
  198. * wb_find_current().
  199. */
  200. static inline struct bdi_writeback *
  201. wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
  202. {
  203. struct bdi_writeback *wb;
  204. rcu_read_lock();
  205. wb = wb_find_current(bdi);
  206. if (wb && unlikely(!wb_tryget(wb)))
  207. wb = NULL;
  208. rcu_read_unlock();
  209. if (unlikely(!wb)) {
  210. struct cgroup_subsys_state *memcg_css;
  211. memcg_css = task_get_css(current, memory_cgrp_id);
  212. wb = wb_get_create(bdi, memcg_css, gfp);
  213. css_put(memcg_css);
  214. }
  215. return wb;
  216. }
  217. /**
  218. * inode_to_wb_is_valid - test whether an inode has a wb associated
  219. * @inode: inode of interest
  220. *
  221. * Returns %true if @inode has a wb associated. May be called without any
  222. * locking.
  223. */
  224. static inline bool inode_to_wb_is_valid(struct inode *inode)
  225. {
  226. return inode->i_wb;
  227. }
  228. /**
  229. * inode_to_wb - determine the wb of an inode
  230. * @inode: inode of interest
  231. *
  232. * Returns the wb @inode is currently associated with. The caller must be
  233. * holding either @inode->i_lock, the i_pages lock, or the
  234. * associated wb's list_lock.
  235. */
  236. static inline struct bdi_writeback *inode_to_wb(const struct inode *inode)
  237. {
  238. #ifdef CONFIG_LOCKDEP
  239. WARN_ON_ONCE(debug_locks &&
  240. (!lockdep_is_held(&inode->i_lock) &&
  241. !lockdep_is_held(&inode->i_mapping->i_pages.xa_lock) &&
  242. !lockdep_is_held(&inode->i_wb->list_lock)));
  243. #endif
  244. return inode->i_wb;
  245. }
  246. /**
  247. * unlocked_inode_to_wb_begin - begin unlocked inode wb access transaction
  248. * @inode: target inode
  249. * @cookie: output param, to be passed to the end function
  250. *
  251. * The caller wants to access the wb associated with @inode but isn't
  252. * holding inode->i_lock, the i_pages lock or wb->list_lock. This
  253. * function determines the wb associated with @inode and ensures that the
  254. * association doesn't change until the transaction is finished with
  255. * unlocked_inode_to_wb_end().
  256. *
  257. * The caller must call unlocked_inode_to_wb_end() with *@cookie afterwards and
  258. * can't sleep during the transaction. IRQs may or may not be disabled on
  259. * return.
  260. */
  261. static inline struct bdi_writeback *
  262. unlocked_inode_to_wb_begin(struct inode *inode, struct wb_lock_cookie *cookie)
  263. {
  264. rcu_read_lock();
  265. /*
  266. * Paired with store_release in inode_switch_wbs_work_fn() and
  267. * ensures that we see the new wb if we see cleared I_WB_SWITCH.
  268. */
  269. cookie->locked = smp_load_acquire(&inode->i_state) & I_WB_SWITCH;
  270. if (unlikely(cookie->locked))
  271. xa_lock_irqsave(&inode->i_mapping->i_pages, cookie->flags);
  272. /*
  273. * Protected by either !I_WB_SWITCH + rcu_read_lock() or the i_pages
  274. * lock. inode_to_wb() will bark. Deref directly.
  275. */
  276. return inode->i_wb;
  277. }
  278. /**
  279. * unlocked_inode_to_wb_end - end inode wb access transaction
  280. * @inode: target inode
  281. * @cookie: @cookie from unlocked_inode_to_wb_begin()
  282. */
  283. static inline void unlocked_inode_to_wb_end(struct inode *inode,
  284. struct wb_lock_cookie *cookie)
  285. {
  286. if (unlikely(cookie->locked))
  287. xa_unlock_irqrestore(&inode->i_mapping->i_pages, cookie->flags);
  288. rcu_read_unlock();
  289. }
  290. #else /* CONFIG_CGROUP_WRITEBACK */
  291. static inline bool inode_cgwb_enabled(struct inode *inode)
  292. {
  293. return false;
  294. }
  295. static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
  296. {
  297. return &bdi->wb;
  298. }
  299. static inline struct bdi_writeback *
  300. wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
  301. {
  302. return &bdi->wb;
  303. }
  304. static inline bool inode_to_wb_is_valid(struct inode *inode)
  305. {
  306. return true;
  307. }
  308. static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
  309. {
  310. return &inode_to_bdi(inode)->wb;
  311. }
  312. static inline struct bdi_writeback *
  313. unlocked_inode_to_wb_begin(struct inode *inode, struct wb_lock_cookie *cookie)
  314. {
  315. return inode_to_wb(inode);
  316. }
  317. static inline void unlocked_inode_to_wb_end(struct inode *inode,
  318. struct wb_lock_cookie *cookie)
  319. {
  320. }
  321. static inline void wb_memcg_offline(struct mem_cgroup *memcg)
  322. {
  323. }
  324. static inline void wb_blkcg_offline(struct blkcg *blkcg)
  325. {
  326. }
  327. static inline int inode_congested(struct inode *inode, int cong_bits)
  328. {
  329. return wb_congested(&inode_to_bdi(inode)->wb, cong_bits);
  330. }
  331. #endif /* CONFIG_CGROUP_WRITEBACK */
  332. static inline int inode_read_congested(struct inode *inode)
  333. {
  334. return inode_congested(inode, 1 << WB_sync_congested);
  335. }
  336. static inline int inode_write_congested(struct inode *inode)
  337. {
  338. return inode_congested(inode, 1 << WB_async_congested);
  339. }
  340. static inline int inode_rw_congested(struct inode *inode)
  341. {
  342. return inode_congested(inode, (1 << WB_sync_congested) |
  343. (1 << WB_async_congested));
  344. }
  345. static inline int bdi_congested(struct backing_dev_info *bdi, int cong_bits)
  346. {
  347. return wb_congested(&bdi->wb, cong_bits);
  348. }
  349. static inline int bdi_read_congested(struct backing_dev_info *bdi)
  350. {
  351. return bdi_congested(bdi, 1 << WB_sync_congested);
  352. }
  353. static inline int bdi_write_congested(struct backing_dev_info *bdi)
  354. {
  355. return bdi_congested(bdi, 1 << WB_async_congested);
  356. }
  357. static inline int bdi_rw_congested(struct backing_dev_info *bdi)
  358. {
  359. return bdi_congested(bdi, (1 << WB_sync_congested) |
  360. (1 << WB_async_congested));
  361. }
  362. const char *bdi_dev_name(struct backing_dev_info *bdi);
  363. #endif /* _LINUX_BACKING_DEV_H */