backing-dev.h 14 KB

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