backing-dev-defs.h 7.6 KB

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