backing-dev-defs.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __LINUX_BACKING_DEV_DEFS_H
  3. #define __LINUX_BACKING_DEV_DEFS_H
  4. #include <linux/list.h>
  5. #include <linux/radix-tree.h>
  6. #include <linux/rbtree.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/percpu_counter.h>
  9. #include <linux/percpu-refcount.h>
  10. #include <linux/flex_proportions.h>
  11. #include <linux/timer.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/kref.h>
  14. struct page;
  15. struct device;
  16. struct dentry;
  17. /*
  18. * Bits in bdi_writeback.state
  19. */
  20. enum wb_state {
  21. WB_registered, /* bdi_register() was done */
  22. WB_writeback_running, /* Writeback is in progress */
  23. WB_has_dirty_io, /* Dirty inodes on ->b_{dirty|io|more_io} */
  24. WB_start_all, /* nr_pages == 0 (all) work pending */
  25. };
  26. enum wb_congested_state {
  27. WB_async_congested, /* The async (write) queue is getting full */
  28. WB_sync_congested, /* The sync queue is getting full */
  29. };
  30. typedef int (congested_fn)(void *, int);
  31. enum wb_stat_item {
  32. WB_RECLAIMABLE,
  33. WB_WRITEBACK,
  34. WB_DIRTIED,
  35. WB_WRITTEN,
  36. NR_WB_STAT_ITEMS
  37. };
  38. #define WB_STAT_BATCH (8*(1+ilog2(nr_cpu_ids)))
  39. /*
  40. * why some writeback work was initiated
  41. */
  42. enum wb_reason {
  43. WB_REASON_BACKGROUND,
  44. WB_REASON_VMSCAN,
  45. WB_REASON_SYNC,
  46. WB_REASON_PERIODIC,
  47. WB_REASON_LAPTOP_TIMER,
  48. WB_REASON_FREE_MORE_MEM,
  49. WB_REASON_FS_FREE_SPACE,
  50. /*
  51. * There is no bdi forker thread any more and works are done
  52. * by emergency worker, however, this is TPs userland visible
  53. * and we'll be exposing exactly the same information,
  54. * so it has a mismatch name.
  55. */
  56. WB_REASON_FORKER_THREAD,
  57. WB_REASON_MAX,
  58. };
  59. /*
  60. * For cgroup writeback, multiple wb's may map to the same blkcg. Those
  61. * wb's can operate mostly independently but should share the congested
  62. * state. To facilitate such sharing, the congested state is tracked using
  63. * the following struct which is created on demand, indexed by blkcg ID on
  64. * its bdi, and refcounted.
  65. */
  66. struct bdi_writeback_congested {
  67. unsigned long state; /* WB_[a]sync_congested flags */
  68. atomic_t refcnt; /* nr of attached wb's and blkg */
  69. #ifdef CONFIG_CGROUP_WRITEBACK
  70. struct backing_dev_info *__bdi; /* the associated bdi, set to NULL
  71. * on bdi unregistration. For memcg-wb
  72. * internal use only! */
  73. int blkcg_id; /* ID of the associated blkcg */
  74. struct rb_node rb_node; /* on bdi->cgwb_congestion_tree */
  75. #endif
  76. };
  77. /*
  78. * Each wb (bdi_writeback) can perform writeback operations, is measured
  79. * and throttled, independently. Without cgroup writeback, each bdi
  80. * (bdi_writeback) is served by its embedded bdi->wb.
  81. *
  82. * On the default hierarchy, blkcg implicitly enables memcg. This allows
  83. * using memcg's page ownership for attributing writeback IOs, and every
  84. * memcg - blkcg combination can be served by its own wb by assigning a
  85. * dedicated wb to each memcg, which enables isolation across different
  86. * cgroups and propagation of IO back pressure down from the IO layer upto
  87. * the tasks which are generating the dirty pages to be written back.
  88. *
  89. * A cgroup wb is indexed on its bdi by the ID of the associated memcg,
  90. * refcounted with the number of inodes attached to it, and pins the memcg
  91. * and the corresponding blkcg. As the corresponding blkcg for a memcg may
  92. * change as blkcg is disabled and enabled higher up in the hierarchy, a wb
  93. * is tested for blkcg after lookup and removed from index on mismatch so
  94. * that a new wb for the combination can be created.
  95. */
  96. struct bdi_writeback {
  97. struct backing_dev_info *bdi; /* our parent bdi */
  98. unsigned long state; /* Always use atomic bitops on this */
  99. unsigned long last_old_flush; /* last old data flush */
  100. struct list_head b_dirty; /* dirty inodes */
  101. struct list_head b_io; /* parked for writeback */
  102. struct list_head b_more_io; /* parked for more writeback */
  103. struct list_head b_dirty_time; /* time stamps are dirty */
  104. spinlock_t list_lock; /* protects the b_* lists */
  105. struct percpu_counter stat[NR_WB_STAT_ITEMS];
  106. struct bdi_writeback_congested *congested;
  107. unsigned long bw_time_stamp; /* last time write bw is updated */
  108. unsigned long dirtied_stamp;
  109. unsigned long written_stamp; /* pages written at bw_time_stamp */
  110. unsigned long write_bandwidth; /* the estimated write bandwidth */
  111. unsigned long avg_write_bandwidth; /* further smoothed write bw, > 0 */
  112. /*
  113. * The base dirty throttle rate, re-calculated on every 200ms.
  114. * All the bdi tasks' dirty rate will be curbed under it.
  115. * @dirty_ratelimit tracks the estimated @balanced_dirty_ratelimit
  116. * in small steps and is much more smooth/stable than the latter.
  117. */
  118. unsigned long dirty_ratelimit;
  119. unsigned long balanced_dirty_ratelimit;
  120. struct fprop_local_percpu completions;
  121. int dirty_exceeded;
  122. enum wb_reason start_all_reason;
  123. spinlock_t work_lock; /* protects work_list & dwork scheduling */
  124. struct list_head work_list;
  125. struct delayed_work dwork; /* work item used for writeback */
  126. unsigned long dirty_sleep; /* last wait */
  127. struct list_head bdi_node; /* anchored at bdi->wb_list */
  128. #ifdef CONFIG_CGROUP_WRITEBACK
  129. struct percpu_ref refcnt; /* used only for !root wb's */
  130. struct fprop_local_percpu memcg_completions;
  131. struct cgroup_subsys_state *memcg_css; /* the associated memcg */
  132. struct cgroup_subsys_state *blkcg_css; /* and blkcg */
  133. struct list_head memcg_node; /* anchored at memcg->cgwb_list */
  134. struct list_head blkcg_node; /* anchored at blkcg->cgwb_list */
  135. union {
  136. struct work_struct release_work;
  137. struct rcu_head rcu;
  138. };
  139. #endif
  140. };
  141. struct backing_dev_info {
  142. struct list_head bdi_list;
  143. unsigned long ra_pages; /* max readahead in PAGE_SIZE units */
  144. unsigned long io_pages; /* max allowed IO size */
  145. congested_fn *congested_fn; /* Function pointer if device is md/dm */
  146. void *congested_data; /* Pointer to aux data for congested func */
  147. const char *name;
  148. struct kref refcnt; /* Reference counter for the structure */
  149. unsigned int capabilities; /* Device capabilities */
  150. unsigned int min_ratio;
  151. unsigned int max_ratio, max_prop_frac;
  152. /*
  153. * Sum of avg_write_bw of wbs with dirty inodes. > 0 if there are
  154. * any dirty wbs, which is depended upon by bdi_has_dirty().
  155. */
  156. atomic_long_t tot_write_bandwidth;
  157. struct bdi_writeback wb; /* the root writeback info for this bdi */
  158. struct list_head wb_list; /* list of all wbs */
  159. #ifdef CONFIG_CGROUP_WRITEBACK
  160. struct radix_tree_root cgwb_tree; /* radix tree of active cgroup wbs */
  161. struct rb_root cgwb_congested_tree; /* their congested states */
  162. struct mutex cgwb_release_mutex; /* protect shutdown of wb structs */
  163. #else
  164. struct bdi_writeback_congested *wb_congested;
  165. #endif
  166. wait_queue_head_t wb_waitq;
  167. struct device *dev;
  168. struct device *owner;
  169. struct timer_list laptop_mode_wb_timer;
  170. #ifdef CONFIG_DEBUG_FS
  171. struct dentry *debug_dir;
  172. struct dentry *debug_stats;
  173. #endif
  174. };
  175. enum {
  176. BLK_RW_ASYNC = 0,
  177. BLK_RW_SYNC = 1,
  178. };
  179. void clear_wb_congested(struct bdi_writeback_congested *congested, int sync);
  180. void set_wb_congested(struct bdi_writeback_congested *congested, int sync);
  181. static inline void clear_bdi_congested(struct backing_dev_info *bdi, int sync)
  182. {
  183. clear_wb_congested(bdi->wb.congested, sync);
  184. }
  185. static inline void set_bdi_congested(struct backing_dev_info *bdi, int sync)
  186. {
  187. set_wb_congested(bdi->wb.congested, sync);
  188. }
  189. struct wb_lock_cookie {
  190. bool locked;
  191. unsigned long flags;
  192. };
  193. #ifdef CONFIG_CGROUP_WRITEBACK
  194. /**
  195. * wb_tryget - try to increment a wb's refcount
  196. * @wb: bdi_writeback to get
  197. */
  198. static inline bool wb_tryget(struct bdi_writeback *wb)
  199. {
  200. if (wb != &wb->bdi->wb)
  201. return percpu_ref_tryget(&wb->refcnt);
  202. return true;
  203. }
  204. /**
  205. * wb_get - increment a wb's refcount
  206. * @wb: bdi_writeback to get
  207. */
  208. static inline void wb_get(struct bdi_writeback *wb)
  209. {
  210. if (wb != &wb->bdi->wb)
  211. percpu_ref_get(&wb->refcnt);
  212. }
  213. /**
  214. * wb_put - decrement a wb's refcount
  215. * @wb: bdi_writeback to put
  216. */
  217. static inline void wb_put(struct bdi_writeback *wb)
  218. {
  219. if (wb != &wb->bdi->wb)
  220. percpu_ref_put(&wb->refcnt);
  221. }
  222. /**
  223. * wb_dying - is a wb dying?
  224. * @wb: bdi_writeback of interest
  225. *
  226. * Returns whether @wb is unlinked and being drained.
  227. */
  228. static inline bool wb_dying(struct bdi_writeback *wb)
  229. {
  230. return percpu_ref_is_dying(&wb->refcnt);
  231. }
  232. #else /* CONFIG_CGROUP_WRITEBACK */
  233. static inline bool wb_tryget(struct bdi_writeback *wb)
  234. {
  235. return true;
  236. }
  237. static inline void wb_get(struct bdi_writeback *wb)
  238. {
  239. }
  240. static inline void wb_put(struct bdi_writeback *wb)
  241. {
  242. }
  243. static inline bool wb_dying(struct bdi_writeback *wb)
  244. {
  245. return false;
  246. }
  247. #endif /* CONFIG_CGROUP_WRITEBACK */
  248. #endif /* __LINUX_BACKING_DEV_DEFS_H */