blk-throttle.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #ifndef BLK_THROTTLE_H
  2. #define BLK_THROTTLE_H
  3. #include "blk-cgroup-rwstat.h"
  4. /*
  5. * To implement hierarchical throttling, throtl_grps form a tree and bios
  6. * are dispatched upwards level by level until they reach the top and get
  7. * issued. When dispatching bios from the children and local group at each
  8. * level, if the bios are dispatched into a single bio_list, there's a risk
  9. * of a local or child group which can queue many bios at once filling up
  10. * the list starving others.
  11. *
  12. * To avoid such starvation, dispatched bios are queued separately
  13. * according to where they came from. When they are again dispatched to
  14. * the parent, they're popped in round-robin order so that no single source
  15. * hogs the dispatch window.
  16. *
  17. * throtl_qnode is used to keep the queued bios separated by their sources.
  18. * Bios are queued to throtl_qnode which in turn is queued to
  19. * throtl_service_queue and then dispatched in round-robin order.
  20. *
  21. * It's also used to track the reference counts on blkg's. A qnode always
  22. * belongs to a throtl_grp and gets queued on itself or the parent, so
  23. * incrementing the reference of the associated throtl_grp when a qnode is
  24. * queued and decrementing when dequeued is enough to keep the whole blkg
  25. * tree pinned while bios are in flight.
  26. */
  27. struct throtl_qnode {
  28. struct list_head node; /* service_queue->queued[] */
  29. struct bio_list bios; /* queued bios */
  30. struct throtl_grp *tg; /* tg this qnode belongs to */
  31. };
  32. struct throtl_service_queue {
  33. struct throtl_service_queue *parent_sq; /* the parent service_queue */
  34. /*
  35. * Bios queued directly to this service_queue or dispatched from
  36. * children throtl_grp's.
  37. */
  38. struct list_head queued[2]; /* throtl_qnode [READ/WRITE] */
  39. unsigned int nr_queued[2]; /* number of queued bios */
  40. /*
  41. * RB tree of active children throtl_grp's, which are sorted by
  42. * their ->disptime.
  43. */
  44. struct rb_root_cached pending_tree; /* RB tree of active tgs */
  45. unsigned int nr_pending; /* # queued in the tree */
  46. unsigned long first_pending_disptime; /* disptime of the first tg */
  47. struct timer_list pending_timer; /* fires on first_pending_disptime */
  48. };
  49. enum tg_state_flags {
  50. THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */
  51. THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */
  52. THROTL_TG_CANCELING = 1 << 2, /* starts to cancel bio */
  53. };
  54. struct throtl_grp {
  55. /* must be the first member */
  56. struct blkg_policy_data pd;
  57. /* active throtl group service_queue member */
  58. struct rb_node rb_node;
  59. /* throtl_data this group belongs to */
  60. struct throtl_data *td;
  61. /* this group's service queue */
  62. struct throtl_service_queue service_queue;
  63. /*
  64. * qnode_on_self is used when bios are directly queued to this
  65. * throtl_grp so that local bios compete fairly with bios
  66. * dispatched from children. qnode_on_parent is used when bios are
  67. * dispatched from this throtl_grp into its parent and will compete
  68. * with the sibling qnode_on_parents and the parent's
  69. * qnode_on_self.
  70. */
  71. struct throtl_qnode qnode_on_self[2];
  72. struct throtl_qnode qnode_on_parent[2];
  73. /*
  74. * Dispatch time in jiffies. This is the estimated time when group
  75. * will unthrottle and is ready to dispatch more bio. It is used as
  76. * key to sort active groups in service tree.
  77. */
  78. unsigned long disptime;
  79. unsigned int flags;
  80. /* are there any throtl rules between this group and td? */
  81. bool has_rules_bps[2];
  82. bool has_rules_iops[2];
  83. /* bytes per second rate limits */
  84. uint64_t bps[2];
  85. /* IOPS limits */
  86. unsigned int iops[2];
  87. /* Number of bytes dispatched in current slice */
  88. uint64_t bytes_disp[2];
  89. /* Number of bio's dispatched in current slice */
  90. unsigned int io_disp[2];
  91. uint64_t last_bytes_disp[2];
  92. unsigned int last_io_disp[2];
  93. /*
  94. * The following two fields are updated when new configuration is
  95. * submitted while some bios are still throttled, they record how many
  96. * bytes/ios are waited already in previous configuration, and they will
  97. * be used to calculate wait time under new configuration.
  98. */
  99. long long carryover_bytes[2];
  100. int carryover_ios[2];
  101. unsigned long last_check_time;
  102. /* When did we start a new slice */
  103. unsigned long slice_start[2];
  104. unsigned long slice_end[2];
  105. struct blkg_rwstat stat_bytes;
  106. struct blkg_rwstat stat_ios;
  107. };
  108. extern struct blkcg_policy blkcg_policy_throtl;
  109. static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
  110. {
  111. return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
  112. }
  113. static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
  114. {
  115. return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
  116. }
  117. /*
  118. * Internal throttling interface
  119. */
  120. #ifndef CONFIG_BLK_DEV_THROTTLING
  121. static inline void blk_throtl_exit(struct gendisk *disk) { }
  122. static inline bool blk_throtl_bio(struct bio *bio) { return false; }
  123. static inline void blk_throtl_cancel_bios(struct gendisk *disk) { }
  124. #else /* CONFIG_BLK_DEV_THROTTLING */
  125. void blk_throtl_exit(struct gendisk *disk);
  126. bool __blk_throtl_bio(struct bio *bio);
  127. void blk_throtl_cancel_bios(struct gendisk *disk);
  128. static inline bool blk_throtl_activated(struct request_queue *q)
  129. {
  130. return q->td != NULL;
  131. }
  132. static inline bool blk_should_throtl(struct bio *bio)
  133. {
  134. struct throtl_grp *tg;
  135. int rw = bio_data_dir(bio);
  136. /*
  137. * This is called under bio_queue_enter(), and it's synchronized with
  138. * the activation of blk-throtl, which is protected by
  139. * blk_mq_freeze_queue().
  140. */
  141. if (!blk_throtl_activated(bio->bi_bdev->bd_queue))
  142. return false;
  143. tg = blkg_to_tg(bio->bi_blkg);
  144. if (!cgroup_subsys_on_dfl(io_cgrp_subsys)) {
  145. if (!bio_flagged(bio, BIO_CGROUP_ACCT)) {
  146. bio_set_flag(bio, BIO_CGROUP_ACCT);
  147. blkg_rwstat_add(&tg->stat_bytes, bio->bi_opf,
  148. bio->bi_iter.bi_size);
  149. }
  150. blkg_rwstat_add(&tg->stat_ios, bio->bi_opf, 1);
  151. }
  152. /* iops limit is always counted */
  153. if (tg->has_rules_iops[rw])
  154. return true;
  155. if (tg->has_rules_bps[rw] && !bio_flagged(bio, BIO_BPS_THROTTLED))
  156. return true;
  157. return false;
  158. }
  159. static inline bool blk_throtl_bio(struct bio *bio)
  160. {
  161. if (!blk_should_throtl(bio))
  162. return false;
  163. return __blk_throtl_bio(bio);
  164. }
  165. #endif /* CONFIG_BLK_DEV_THROTTLING */
  166. #endif