blk-throttle.h 6.4 KB

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