backing-dev-defs.h 8.1 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. #include <linux/refcount.h>
  15. #include <linux/android_kabi.h>
  16. struct page;
  17. struct device;
  18. struct dentry;
  19. /*
  20. * Bits in bdi_writeback.state
  21. */
  22. enum wb_state {
  23. WB_registered, /* bdi_register() was done */
  24. WB_writeback_running, /* Writeback is in progress */
  25. WB_has_dirty_io, /* Dirty inodes on ->b_{dirty|io|more_io} */
  26. WB_start_all, /* nr_pages == 0 (all) work pending */
  27. };
  28. enum wb_congested_state {
  29. WB_async_congested, /* The async (write) queue is getting full */
  30. WB_sync_congested, /* The sync queue is getting full */
  31. };
  32. enum wb_stat_item {
  33. WB_RECLAIMABLE,
  34. WB_WRITEBACK,
  35. WB_DIRTIED,
  36. WB_WRITTEN,
  37. NR_WB_STAT_ITEMS
  38. };
  39. #define WB_STAT_BATCH (8*(1+ilog2(nr_cpu_ids)))
  40. /*
  41. * why some writeback work was initiated
  42. */
  43. enum wb_reason {
  44. WB_REASON_BACKGROUND,
  45. WB_REASON_VMSCAN,
  46. WB_REASON_SYNC,
  47. WB_REASON_PERIODIC,
  48. WB_REASON_LAPTOP_TIMER,
  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_FOREIGN_FLUSH,
  58. WB_REASON_MAX,
  59. };
  60. struct wb_completion {
  61. atomic_t cnt;
  62. wait_queue_head_t *waitq;
  63. };
  64. #define __WB_COMPLETION_INIT(_waitq) \
  65. (struct wb_completion){ .cnt = ATOMIC_INIT(1), .waitq = (_waitq) }
  66. /*
  67. * If one wants to wait for one or more wb_writeback_works, each work's
  68. * ->done should be set to a wb_completion defined using the following
  69. * macro. Once all work items are issued with wb_queue_work(), the caller
  70. * can wait for the completion of all using wb_wait_for_completion(). Work
  71. * items which are waited upon aren't freed automatically on completion.
  72. */
  73. #define WB_COMPLETION_INIT(bdi) __WB_COMPLETION_INIT(&(bdi)->wb_waitq)
  74. #define DEFINE_WB_COMPLETION(cmpl, bdi) \
  75. struct wb_completion cmpl = WB_COMPLETION_INIT(bdi)
  76. /*
  77. * Each wb (bdi_writeback) can perform writeback operations, is measured
  78. * and throttled, independently. Without cgroup writeback, each bdi
  79. * (bdi_writeback) is served by its embedded bdi->wb.
  80. *
  81. * On the default hierarchy, blkcg implicitly enables memcg. This allows
  82. * using memcg's page ownership for attributing writeback IOs, and every
  83. * memcg - blkcg combination can be served by its own wb by assigning a
  84. * dedicated wb to each memcg, which enables isolation across different
  85. * cgroups and propagation of IO back pressure down from the IO layer upto
  86. * the tasks which are generating the dirty pages to be written back.
  87. *
  88. * A cgroup wb is indexed on its bdi by the ID of the associated memcg,
  89. * refcounted with the number of inodes attached to it, and pins the memcg
  90. * and the corresponding blkcg. As the corresponding blkcg for a memcg may
  91. * change as blkcg is disabled and enabled higher up in the hierarchy, a wb
  92. * is tested for blkcg after lookup and removed from index on mismatch so
  93. * that a new wb for the combination can be created.
  94. */
  95. struct bdi_writeback {
  96. struct backing_dev_info *bdi; /* our parent bdi */
  97. unsigned long state; /* Always use atomic bitops on this */
  98. unsigned long last_old_flush; /* last old data flush */
  99. struct list_head b_dirty; /* dirty inodes */
  100. struct list_head b_io; /* parked for writeback */
  101. struct list_head b_more_io; /* parked for more writeback */
  102. struct list_head b_dirty_time; /* time stamps are dirty */
  103. spinlock_t list_lock; /* protects the b_* lists */
  104. struct percpu_counter stat[NR_WB_STAT_ITEMS];
  105. unsigned long congested; /* WB_[a]sync_congested flags */
  106. unsigned long bw_time_stamp; /* last time write bw is updated */
  107. unsigned long dirtied_stamp;
  108. unsigned long written_stamp; /* pages written at bw_time_stamp */
  109. unsigned long write_bandwidth; /* the estimated write bandwidth */
  110. unsigned long avg_write_bandwidth; /* further smoothed write bw, > 0 */
  111. /*
  112. * The base dirty throttle rate, re-calculated on every 200ms.
  113. * All the bdi tasks' dirty rate will be curbed under it.
  114. * @dirty_ratelimit tracks the estimated @balanced_dirty_ratelimit
  115. * in small steps and is much more smooth/stable than the latter.
  116. */
  117. unsigned long dirty_ratelimit;
  118. unsigned long balanced_dirty_ratelimit;
  119. struct fprop_local_percpu completions;
  120. int dirty_exceeded;
  121. enum wb_reason start_all_reason;
  122. spinlock_t work_lock; /* protects work_list & dwork scheduling */
  123. struct list_head work_list;
  124. struct delayed_work dwork; /* work item used for writeback */
  125. unsigned long dirty_sleep; /* last wait */
  126. struct list_head bdi_node; /* anchored at bdi->wb_list */
  127. #ifdef CONFIG_CGROUP_WRITEBACK
  128. struct percpu_ref refcnt; /* used only for !root wb's */
  129. struct fprop_local_percpu memcg_completions;
  130. struct cgroup_subsys_state *memcg_css; /* the associated memcg */
  131. struct cgroup_subsys_state *blkcg_css; /* and blkcg */
  132. struct list_head memcg_node; /* anchored at memcg->cgwb_list */
  133. struct list_head blkcg_node; /* anchored at blkcg->cgwb_list */
  134. union {
  135. struct work_struct release_work;
  136. struct rcu_head rcu;
  137. };
  138. #endif
  139. ANDROID_KABI_RESERVE(1);
  140. ANDROID_KABI_RESERVE(2);
  141. };
  142. struct backing_dev_info {
  143. u64 id;
  144. struct rb_node rb_node; /* keyed by ->id */
  145. struct list_head bdi_list;
  146. unsigned long ra_pages; /* max readahead in PAGE_SIZE units */
  147. unsigned long io_pages; /* max allowed IO size */
  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 mutex cgwb_release_mutex; /* protect shutdown of wb structs */
  162. struct rw_semaphore wb_switch_rwsem; /* no cgwb switch while syncing */
  163. #endif
  164. wait_queue_head_t wb_waitq;
  165. struct device *dev;
  166. char dev_name[64];
  167. struct device *owner;
  168. struct timer_list laptop_mode_wb_timer;
  169. #ifdef CONFIG_DEBUG_FS
  170. struct dentry *debug_dir;
  171. #endif
  172. ANDROID_KABI_RESERVE(1);
  173. ANDROID_KABI_RESERVE(2);
  174. };
  175. enum {
  176. BLK_RW_ASYNC = 0,
  177. BLK_RW_SYNC = 1,
  178. };
  179. void clear_bdi_congested(struct backing_dev_info *bdi, int sync);
  180. void set_bdi_congested(struct backing_dev_info *bdi, int sync);
  181. struct wb_lock_cookie {
  182. bool locked;
  183. unsigned long flags;
  184. };
  185. #ifdef CONFIG_CGROUP_WRITEBACK
  186. /**
  187. * wb_tryget - try to increment a wb's refcount
  188. * @wb: bdi_writeback to get
  189. */
  190. static inline bool wb_tryget(struct bdi_writeback *wb)
  191. {
  192. if (wb != &wb->bdi->wb)
  193. return percpu_ref_tryget(&wb->refcnt);
  194. return true;
  195. }
  196. /**
  197. * wb_get - increment a wb's refcount
  198. * @wb: bdi_writeback to get
  199. */
  200. static inline void wb_get(struct bdi_writeback *wb)
  201. {
  202. if (wb != &wb->bdi->wb)
  203. percpu_ref_get(&wb->refcnt);
  204. }
  205. /**
  206. * wb_put - decrement a wb's refcount
  207. * @wb: bdi_writeback to put
  208. */
  209. static inline void wb_put(struct bdi_writeback *wb)
  210. {
  211. if (WARN_ON_ONCE(!wb->bdi)) {
  212. /*
  213. * A driver bug might cause a file to be removed before bdi was
  214. * initialized.
  215. */
  216. return;
  217. }
  218. if (wb != &wb->bdi->wb)
  219. percpu_ref_put(&wb->refcnt);
  220. }
  221. /**
  222. * wb_dying - is a wb dying?
  223. * @wb: bdi_writeback of interest
  224. *
  225. * Returns whether @wb is unlinked and being drained.
  226. */
  227. static inline bool wb_dying(struct bdi_writeback *wb)
  228. {
  229. return percpu_ref_is_dying(&wb->refcnt);
  230. }
  231. #else /* CONFIG_CGROUP_WRITEBACK */
  232. static inline bool wb_tryget(struct bdi_writeback *wb)
  233. {
  234. return true;
  235. }
  236. static inline void wb_get(struct bdi_writeback *wb)
  237. {
  238. }
  239. static inline void wb_put(struct bdi_writeback *wb)
  240. {
  241. }
  242. static inline bool wb_dying(struct bdi_writeback *wb)
  243. {
  244. return false;
  245. }
  246. #endif /* CONFIG_CGROUP_WRITEBACK */
  247. #endif /* __LINUX_BACKING_DEV_DEFS_H */