backing-dev-defs.h 7.7 KB

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