backing-dev.h 14 KB

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