backing-dev.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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/writeback.h>
  15. #include <linux/blk-cgroup.h>
  16. #include <linux/backing-dev-defs.h>
  17. #include <linux/slab.h>
  18. static inline struct backing_dev_info *bdi_get(struct backing_dev_info *bdi)
  19. {
  20. kref_get(&bdi->refcnt);
  21. return bdi;
  22. }
  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. int bdi_register_va(struct backing_dev_info *bdi, const char *fmt,
  27. va_list args);
  28. int bdi_register_owner(struct backing_dev_info *bdi, struct device *owner);
  29. void bdi_unregister(struct backing_dev_info *bdi);
  30. struct backing_dev_info *bdi_alloc_node(gfp_t gfp_mask, int node_id);
  31. static inline struct backing_dev_info *bdi_alloc(gfp_t gfp_mask)
  32. {
  33. return bdi_alloc_node(gfp_mask, NUMA_NO_NODE);
  34. }
  35. void wb_start_background_writeback(struct bdi_writeback *wb);
  36. void wb_workfn(struct work_struct *work);
  37. void wb_wakeup_delayed(struct bdi_writeback *wb);
  38. extern spinlock_t bdi_lock;
  39. extern struct list_head bdi_list;
  40. extern struct workqueue_struct *bdi_wq;
  41. static inline bool wb_has_dirty_io(struct bdi_writeback *wb)
  42. {
  43. return test_bit(WB_has_dirty_io, &wb->state);
  44. }
  45. static inline bool bdi_has_dirty_io(struct backing_dev_info *bdi)
  46. {
  47. /*
  48. * @bdi->tot_write_bandwidth is guaranteed to be > 0 if there are
  49. * any dirty wbs. See wb_update_write_bandwidth().
  50. */
  51. return atomic_long_read(&bdi->tot_write_bandwidth);
  52. }
  53. static inline void __add_wb_stat(struct bdi_writeback *wb,
  54. enum wb_stat_item item, s64 amount)
  55. {
  56. percpu_counter_add_batch(&wb->stat[item], amount, WB_STAT_BATCH);
  57. }
  58. static inline void inc_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
  59. {
  60. __add_wb_stat(wb, item, 1);
  61. }
  62. static inline void dec_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
  63. {
  64. __add_wb_stat(wb, item, -1);
  65. }
  66. static inline s64 wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
  67. {
  68. return percpu_counter_read_positive(&wb->stat[item]);
  69. }
  70. static inline s64 wb_stat_sum(struct bdi_writeback *wb, enum wb_stat_item item)
  71. {
  72. return percpu_counter_sum_positive(&wb->stat[item]);
  73. }
  74. extern void wb_writeout_inc(struct bdi_writeback *wb);
  75. /*
  76. * maximal error of a stat counter.
  77. */
  78. static inline unsigned long wb_stat_error(void)
  79. {
  80. #ifdef CONFIG_SMP
  81. return nr_cpu_ids * WB_STAT_BATCH;
  82. #else
  83. return 1;
  84. #endif
  85. }
  86. int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio);
  87. int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio);
  88. /*
  89. * Flags in backing_dev_info::capability
  90. *
  91. * The first three flags control whether dirty pages will contribute to the
  92. * VM's accounting and whether writepages() should be called for dirty pages
  93. * (something that would not, for example, be appropriate for ramfs)
  94. *
  95. * WARNING: these flags are closely related and should not normally be
  96. * used separately. The BDI_CAP_NO_ACCT_AND_WRITEBACK combines these
  97. * three flags into a single convenience macro.
  98. *
  99. * BDI_CAP_NO_ACCT_DIRTY: Dirty pages shouldn't contribute to accounting
  100. * BDI_CAP_NO_WRITEBACK: Don't write pages back
  101. * BDI_CAP_NO_ACCT_WB: Don't automatically account writeback pages
  102. * BDI_CAP_STRICTLIMIT: Keep number of dirty pages below bdi threshold.
  103. *
  104. * BDI_CAP_CGROUP_WRITEBACK: Supports cgroup-aware writeback.
  105. * BDI_CAP_SYNCHRONOUS_IO: Device is so fast that asynchronous IO would be
  106. * inefficient.
  107. */
  108. #define BDI_CAP_NO_ACCT_DIRTY 0x00000001
  109. #define BDI_CAP_NO_WRITEBACK 0x00000002
  110. #define BDI_CAP_NO_ACCT_WB 0x00000004
  111. #define BDI_CAP_STABLE_WRITES 0x00000008
  112. #define BDI_CAP_STRICTLIMIT 0x00000010
  113. #define BDI_CAP_CGROUP_WRITEBACK 0x00000020
  114. #define BDI_CAP_SYNCHRONOUS_IO 0x00000040
  115. #define BDI_CAP_NO_ACCT_AND_WRITEBACK \
  116. (BDI_CAP_NO_WRITEBACK | BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_ACCT_WB)
  117. extern struct backing_dev_info noop_backing_dev_info;
  118. /**
  119. * writeback_in_progress - determine whether there is writeback in progress
  120. * @wb: bdi_writeback of interest
  121. *
  122. * Determine whether there is writeback waiting to be handled against a
  123. * bdi_writeback.
  124. */
  125. static inline bool writeback_in_progress(struct bdi_writeback *wb)
  126. {
  127. return test_bit(WB_writeback_running, &wb->state);
  128. }
  129. static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
  130. {
  131. struct super_block *sb;
  132. if (!inode)
  133. return &noop_backing_dev_info;
  134. sb = inode->i_sb;
  135. #ifdef CONFIG_BLOCK
  136. if (sb_is_blkdev_sb(sb))
  137. return I_BDEV(inode)->bd_bdi;
  138. #endif
  139. return sb->s_bdi;
  140. }
  141. static inline int wb_congested(struct bdi_writeback *wb, int cong_bits)
  142. {
  143. struct backing_dev_info *bdi = wb->bdi;
  144. if (bdi->congested_fn)
  145. return bdi->congested_fn(bdi->congested_data, cong_bits);
  146. return wb->congested->state & cong_bits;
  147. }
  148. long congestion_wait(int sync, long timeout);
  149. long wait_iff_congested(int sync, long timeout);
  150. static inline bool bdi_cap_synchronous_io(struct backing_dev_info *bdi)
  151. {
  152. return bdi->capabilities & BDI_CAP_SYNCHRONOUS_IO;
  153. }
  154. static inline bool bdi_cap_stable_pages_required(struct backing_dev_info *bdi)
  155. {
  156. return bdi->capabilities & BDI_CAP_STABLE_WRITES;
  157. }
  158. static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi)
  159. {
  160. return !(bdi->capabilities & BDI_CAP_NO_WRITEBACK);
  161. }
  162. static inline bool bdi_cap_account_dirty(struct backing_dev_info *bdi)
  163. {
  164. return !(bdi->capabilities & BDI_CAP_NO_ACCT_DIRTY);
  165. }
  166. static inline bool bdi_cap_account_writeback(struct backing_dev_info *bdi)
  167. {
  168. /* Paranoia: BDI_CAP_NO_WRITEBACK implies BDI_CAP_NO_ACCT_WB */
  169. return !(bdi->capabilities & (BDI_CAP_NO_ACCT_WB |
  170. BDI_CAP_NO_WRITEBACK));
  171. }
  172. static inline bool mapping_cap_writeback_dirty(struct address_space *mapping)
  173. {
  174. return bdi_cap_writeback_dirty(inode_to_bdi(mapping->host));
  175. }
  176. static inline bool mapping_cap_account_dirty(struct address_space *mapping)
  177. {
  178. return bdi_cap_account_dirty(inode_to_bdi(mapping->host));
  179. }
  180. static inline int bdi_sched_wait(void *word)
  181. {
  182. schedule();
  183. return 0;
  184. }
  185. #ifdef CONFIG_CGROUP_WRITEBACK
  186. struct bdi_writeback_congested *
  187. wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp);
  188. void wb_congested_put(struct bdi_writeback_congested *congested);
  189. struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
  190. struct cgroup_subsys_state *memcg_css,
  191. gfp_t gfp);
  192. void wb_memcg_offline(struct mem_cgroup *memcg);
  193. void wb_blkcg_offline(struct blkcg *blkcg);
  194. int inode_congested(struct inode *inode, int cong_bits);
  195. /**
  196. * inode_cgwb_enabled - test whether cgroup writeback is enabled on an inode
  197. * @inode: inode of interest
  198. *
  199. * cgroup writeback requires support from both the bdi and filesystem.
  200. * Also, both memcg and iocg have to be on the default hierarchy. Test
  201. * whether all conditions are met.
  202. *
  203. * Note that the test result may change dynamically on the same inode
  204. * depending on how memcg and iocg are configured.
  205. */
  206. static inline bool inode_cgwb_enabled(struct inode *inode)
  207. {
  208. struct backing_dev_info *bdi = inode_to_bdi(inode);
  209. return cgroup_subsys_on_dfl(memory_cgrp_subsys) &&
  210. cgroup_subsys_on_dfl(io_cgrp_subsys) &&
  211. bdi_cap_account_dirty(bdi) &&
  212. (bdi->capabilities & BDI_CAP_CGROUP_WRITEBACK) &&
  213. (inode->i_sb->s_iflags & SB_I_CGROUPWB);
  214. }
  215. /**
  216. * wb_find_current - find wb for %current on a bdi
  217. * @bdi: bdi of interest
  218. *
  219. * Find the wb of @bdi which matches both the memcg and blkcg of %current.
  220. * Must be called under rcu_read_lock() which protects the returend wb.
  221. * NULL if not found.
  222. */
  223. static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
  224. {
  225. struct cgroup_subsys_state *memcg_css;
  226. struct bdi_writeback *wb;
  227. memcg_css = task_css(current, memory_cgrp_id);
  228. if (!memcg_css->parent)
  229. return &bdi->wb;
  230. wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
  231. /*
  232. * %current's blkcg equals the effective blkcg of its memcg. No
  233. * need to use the relatively expensive cgroup_get_e_css().
  234. */
  235. if (likely(wb && wb->blkcg_css == task_css(current, io_cgrp_id)))
  236. return wb;
  237. return NULL;
  238. }
  239. /**
  240. * wb_get_create_current - get or create wb for %current on a bdi
  241. * @bdi: bdi of interest
  242. * @gfp: allocation mask
  243. *
  244. * Equivalent to wb_get_create() on %current's memcg. This function is
  245. * called from a relatively hot path and optimizes the common cases using
  246. * wb_find_current().
  247. */
  248. static inline struct bdi_writeback *
  249. wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
  250. {
  251. struct bdi_writeback *wb;
  252. rcu_read_lock();
  253. wb = wb_find_current(bdi);
  254. if (wb && unlikely(!wb_tryget(wb)))
  255. wb = NULL;
  256. rcu_read_unlock();
  257. if (unlikely(!wb)) {
  258. struct cgroup_subsys_state *memcg_css;
  259. memcg_css = task_get_css(current, memory_cgrp_id);
  260. wb = wb_get_create(bdi, memcg_css, gfp);
  261. css_put(memcg_css);
  262. }
  263. return wb;
  264. }
  265. /**
  266. * inode_to_wb_is_valid - test whether an inode has a wb associated
  267. * @inode: inode of interest
  268. *
  269. * Returns %true if @inode has a wb associated. May be called without any
  270. * locking.
  271. */
  272. static inline bool inode_to_wb_is_valid(struct inode *inode)
  273. {
  274. return inode->i_wb;
  275. }
  276. /**
  277. * inode_to_wb - determine the wb of an inode
  278. * @inode: inode of interest
  279. *
  280. * Returns the wb @inode is currently associated with. The caller must be
  281. * holding either @inode->i_lock, the i_pages lock, or the
  282. * associated wb's list_lock.
  283. */
  284. static inline struct bdi_writeback *inode_to_wb(const struct inode *inode)
  285. {
  286. #ifdef CONFIG_LOCKDEP
  287. WARN_ON_ONCE(debug_locks &&
  288. (!lockdep_is_held(&inode->i_lock) &&
  289. !lockdep_is_held(&inode->i_mapping->i_pages.xa_lock) &&
  290. !lockdep_is_held(&inode->i_wb->list_lock)));
  291. #endif
  292. return inode->i_wb;
  293. }
  294. /**
  295. * unlocked_inode_to_wb_begin - begin unlocked inode wb access transaction
  296. * @inode: target inode
  297. * @lockedp: temp bool output param, to be passed to the end function
  298. *
  299. * The caller wants to access the wb associated with @inode but isn't
  300. * holding inode->i_lock, the i_pages lock or wb->list_lock. This
  301. * function determines the wb associated with @inode and ensures that the
  302. * association doesn't change until the transaction is finished with
  303. * unlocked_inode_to_wb_end().
  304. *
  305. * The caller must call unlocked_inode_to_wb_end() with *@lockdep
  306. * afterwards and can't sleep during transaction. IRQ may or may not be
  307. * disabled on return.
  308. */
  309. static inline struct bdi_writeback *
  310. unlocked_inode_to_wb_begin(struct inode *inode, bool *lockedp)
  311. {
  312. rcu_read_lock();
  313. /*
  314. * Paired with store_release in inode_switch_wb_work_fn() and
  315. * ensures that we see the new wb if we see cleared I_WB_SWITCH.
  316. */
  317. *lockedp = smp_load_acquire(&inode->i_state) & I_WB_SWITCH;
  318. if (unlikely(*lockedp))
  319. xa_lock_irq(&inode->i_mapping->i_pages);
  320. /*
  321. * Protected by either !I_WB_SWITCH + rcu_read_lock() or the i_pages
  322. * lock. inode_to_wb() will bark. Deref directly.
  323. */
  324. return inode->i_wb;
  325. }
  326. /**
  327. * unlocked_inode_to_wb_end - end inode wb access transaction
  328. * @inode: target inode
  329. * @locked: *@lockedp from unlocked_inode_to_wb_begin()
  330. */
  331. static inline void unlocked_inode_to_wb_end(struct inode *inode, bool locked)
  332. {
  333. if (unlikely(locked))
  334. xa_unlock_irq(&inode->i_mapping->i_pages);
  335. rcu_read_unlock();
  336. }
  337. #else /* CONFIG_CGROUP_WRITEBACK */
  338. static inline bool inode_cgwb_enabled(struct inode *inode)
  339. {
  340. return false;
  341. }
  342. static inline struct bdi_writeback_congested *
  343. wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)
  344. {
  345. atomic_inc(&bdi->wb_congested->refcnt);
  346. return bdi->wb_congested;
  347. }
  348. static inline void wb_congested_put(struct bdi_writeback_congested *congested)
  349. {
  350. if (atomic_dec_and_test(&congested->refcnt))
  351. kfree(congested);
  352. }
  353. static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
  354. {
  355. return &bdi->wb;
  356. }
  357. static inline struct bdi_writeback *
  358. wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
  359. {
  360. return &bdi->wb;
  361. }
  362. static inline bool inode_to_wb_is_valid(struct inode *inode)
  363. {
  364. return true;
  365. }
  366. static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
  367. {
  368. return &inode_to_bdi(inode)->wb;
  369. }
  370. static inline struct bdi_writeback *
  371. unlocked_inode_to_wb_begin(struct inode *inode, bool *lockedp)
  372. {
  373. return inode_to_wb(inode);
  374. }
  375. static inline void unlocked_inode_to_wb_end(struct inode *inode, bool locked)
  376. {
  377. }
  378. static inline void wb_memcg_offline(struct mem_cgroup *memcg)
  379. {
  380. }
  381. static inline void wb_blkcg_offline(struct blkcg *blkcg)
  382. {
  383. }
  384. static inline int inode_congested(struct inode *inode, int cong_bits)
  385. {
  386. return wb_congested(&inode_to_bdi(inode)->wb, cong_bits);
  387. }
  388. #endif /* CONFIG_CGROUP_WRITEBACK */
  389. static inline int inode_read_congested(struct inode *inode)
  390. {
  391. return inode_congested(inode, 1 << WB_sync_congested);
  392. }
  393. static inline int inode_write_congested(struct inode *inode)
  394. {
  395. return inode_congested(inode, 1 << WB_async_congested);
  396. }
  397. static inline int inode_rw_congested(struct inode *inode)
  398. {
  399. return inode_congested(inode, (1 << WB_sync_congested) |
  400. (1 << WB_async_congested));
  401. }
  402. static inline int bdi_congested(struct backing_dev_info *bdi, int cong_bits)
  403. {
  404. return wb_congested(&bdi->wb, cong_bits);
  405. }
  406. static inline int bdi_read_congested(struct backing_dev_info *bdi)
  407. {
  408. return bdi_congested(bdi, 1 << WB_sync_congested);
  409. }
  410. static inline int bdi_write_congested(struct backing_dev_info *bdi)
  411. {
  412. return bdi_congested(bdi, 1 << WB_async_congested);
  413. }
  414. static inline int bdi_rw_congested(struct backing_dev_info *bdi)
  415. {
  416. return bdi_congested(bdi, (1 << WB_sync_congested) |
  417. (1 << WB_async_congested));
  418. }
  419. #endif /* _LINUX_BACKING_DEV_H */