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. };
  25. enum wb_congested_state {
  26. WB_async_congested, /* The async (write) queue is getting full */
  27. WB_sync_congested, /* The sync queue is getting full */
  28. };
  29. typedef int (congested_fn)(void *, int);
  30. enum wb_stat_item {
  31. WB_RECLAIMABLE,
  32. WB_WRITEBACK,
  33. WB_DIRTIED,
  34. WB_WRITTEN,
  35. NR_WB_STAT_ITEMS
  36. };
  37. #define WB_STAT_BATCH (8*(1+ilog2(nr_cpu_ids)))
  38. /*
  39. * For cgroup writeback, multiple wb's may map to the same blkcg. Those
  40. * wb's can operate mostly independently but should share the congested
  41. * state. To facilitate such sharing, the congested state is tracked using
  42. * the following struct which is created on demand, indexed by blkcg ID on
  43. * its bdi, and refcounted.
  44. */
  45. struct bdi_writeback_congested {
  46. unsigned long state; /* WB_[a]sync_congested flags */
  47. atomic_t refcnt; /* nr of attached wb's and blkg */
  48. #ifdef CONFIG_CGROUP_WRITEBACK
  49. struct backing_dev_info *__bdi; /* the associated bdi, set to NULL
  50. * on bdi unregistration. For memcg-wb
  51. * internal use only! */
  52. int blkcg_id; /* ID of the associated blkcg */
  53. struct rb_node rb_node; /* on bdi->cgwb_congestion_tree */
  54. #endif
  55. };
  56. /*
  57. * Each wb (bdi_writeback) can perform writeback operations, is measured
  58. * and throttled, independently. Without cgroup writeback, each bdi
  59. * (bdi_writeback) is served by its embedded bdi->wb.
  60. *
  61. * On the default hierarchy, blkcg implicitly enables memcg. This allows
  62. * using memcg's page ownership for attributing writeback IOs, and every
  63. * memcg - blkcg combination can be served by its own wb by assigning a
  64. * dedicated wb to each memcg, which enables isolation across different
  65. * cgroups and propagation of IO back pressure down from the IO layer upto
  66. * the tasks which are generating the dirty pages to be written back.
  67. *
  68. * A cgroup wb is indexed on its bdi by the ID of the associated memcg,
  69. * refcounted with the number of inodes attached to it, and pins the memcg
  70. * and the corresponding blkcg. As the corresponding blkcg for a memcg may
  71. * change as blkcg is disabled and enabled higher up in the hierarchy, a wb
  72. * is tested for blkcg after lookup and removed from index on mismatch so
  73. * that a new wb for the combination can be created.
  74. */
  75. struct bdi_writeback {
  76. struct backing_dev_info *bdi; /* our parent bdi */
  77. unsigned long state; /* Always use atomic bitops on this */
  78. unsigned long last_old_flush; /* last old data flush */
  79. struct list_head b_dirty; /* dirty inodes */
  80. struct list_head b_io; /* parked for writeback */
  81. struct list_head b_more_io; /* parked for more writeback */
  82. struct list_head b_dirty_time; /* time stamps are dirty */
  83. spinlock_t list_lock; /* protects the b_* lists */
  84. struct percpu_counter stat[NR_WB_STAT_ITEMS];
  85. struct bdi_writeback_congested *congested;
  86. unsigned long bw_time_stamp; /* last time write bw is updated */
  87. unsigned long dirtied_stamp;
  88. unsigned long written_stamp; /* pages written at bw_time_stamp */
  89. unsigned long write_bandwidth; /* the estimated write bandwidth */
  90. unsigned long avg_write_bandwidth; /* further smoothed write bw, > 0 */
  91. /*
  92. * The base dirty throttle rate, re-calculated on every 200ms.
  93. * All the bdi tasks' dirty rate will be curbed under it.
  94. * @dirty_ratelimit tracks the estimated @balanced_dirty_ratelimit
  95. * in small steps and is much more smooth/stable than the latter.
  96. */
  97. unsigned long dirty_ratelimit;
  98. unsigned long balanced_dirty_ratelimit;
  99. struct fprop_local_percpu completions;
  100. int dirty_exceeded;
  101. spinlock_t work_lock; /* protects work_list & dwork scheduling */
  102. struct list_head work_list;
  103. struct delayed_work dwork; /* work item used for writeback */
  104. unsigned long dirty_sleep; /* last wait */
  105. struct list_head bdi_node; /* anchored at bdi->wb_list */
  106. #ifdef CONFIG_CGROUP_WRITEBACK
  107. struct percpu_ref refcnt; /* used only for !root wb's */
  108. struct fprop_local_percpu memcg_completions;
  109. struct cgroup_subsys_state *memcg_css; /* the associated memcg */
  110. struct cgroup_subsys_state *blkcg_css; /* and blkcg */
  111. struct list_head memcg_node; /* anchored at memcg->cgwb_list */
  112. struct list_head blkcg_node; /* anchored at blkcg->cgwb_list */
  113. union {
  114. struct work_struct release_work;
  115. struct rcu_head rcu;
  116. };
  117. #endif
  118. };
  119. struct backing_dev_info {
  120. struct list_head bdi_list;
  121. unsigned long ra_pages; /* max readahead in PAGE_SIZE units */
  122. unsigned long io_pages; /* max allowed IO size */
  123. congested_fn *congested_fn; /* Function pointer if device is md/dm */
  124. void *congested_data; /* Pointer to aux data for congested func */
  125. char *name;
  126. struct kref refcnt; /* Reference counter for the structure */
  127. unsigned int capabilities; /* Device capabilities */
  128. unsigned int min_ratio;
  129. unsigned int max_ratio, max_prop_frac;
  130. /*
  131. * Sum of avg_write_bw of wbs with dirty inodes. > 0 if there are
  132. * any dirty wbs, which is depended upon by bdi_has_dirty().
  133. */
  134. atomic_long_t tot_write_bandwidth;
  135. struct bdi_writeback wb; /* the root writeback info for this bdi */
  136. struct list_head wb_list; /* list of all wbs */
  137. #ifdef CONFIG_CGROUP_WRITEBACK
  138. struct radix_tree_root cgwb_tree; /* radix tree of active cgroup wbs */
  139. struct rb_root cgwb_congested_tree; /* their congested states */
  140. atomic_t usage_cnt; /* counts both cgwbs and cgwb_contested's */
  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 */