backing-dev.h 14 KB

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