backing-dev-defs.h 7.6 KB

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