backing-dev.h 14 KB

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