backing-dev.h 13 KB

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