backing-dev.h 9.9 KB

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