blk-throttle.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. enum {
  55. LIMIT_LOW,
  56. LIMIT_MAX,
  57. LIMIT_CNT,
  58. };
  59. struct throtl_grp {
  60. /* must be the first member */
  61. struct blkg_policy_data pd;
  62. /* active throtl group service_queue member */
  63. struct rb_node rb_node;
  64. /* throtl_data this group belongs to */
  65. struct throtl_data *td;
  66. /* this group's service queue */
  67. struct throtl_service_queue service_queue;
  68. /*
  69. * qnode_on_self is used when bios are directly queued to this
  70. * throtl_grp so that local bios compete fairly with bios
  71. * dispatched from children. qnode_on_parent is used when bios are
  72. * dispatched from this throtl_grp into its parent and will compete
  73. * with the sibling qnode_on_parents and the parent's
  74. * qnode_on_self.
  75. */
  76. struct throtl_qnode qnode_on_self[2];
  77. struct throtl_qnode qnode_on_parent[2];
  78. /*
  79. * Dispatch time in jiffies. This is the estimated time when group
  80. * will unthrottle and is ready to dispatch more bio. It is used as
  81. * key to sort active groups in service tree.
  82. */
  83. unsigned long disptime;
  84. unsigned int flags;
  85. /* are there any throtl rules between this group and td? */
  86. bool has_rules_bps[2];
  87. bool has_rules_iops[2];
  88. /* internally used bytes per second rate limits */
  89. uint64_t bps[2][LIMIT_CNT];
  90. /* user configured bps limits */
  91. uint64_t bps_conf[2][LIMIT_CNT];
  92. /* internally used IOPS limits */
  93. unsigned int iops[2][LIMIT_CNT];
  94. /* user configured IOPS limits */
  95. unsigned int iops_conf[2][LIMIT_CNT];
  96. /* Number of bytes dispatched in current slice */
  97. uint64_t bytes_disp[2];
  98. /* Number of bio's dispatched in current slice */
  99. unsigned int io_disp[2];
  100. unsigned long last_low_overflow_time[2];
  101. uint64_t last_bytes_disp[2];
  102. unsigned int last_io_disp[2];
  103. /*
  104. * The following two fields are updated when new configuration is
  105. * submitted while some bios are still throttled, they record how many
  106. * bytes/ios are waited already in previous configuration, and they will
  107. * be used to calculate wait time under new configuration.
  108. */
  109. uint64_t carryover_bytes[2];
  110. unsigned int carryover_ios[2];
  111. unsigned long last_check_time;
  112. unsigned long latency_target; /* us */
  113. unsigned long latency_target_conf; /* us */
  114. /* When did we start a new slice */
  115. unsigned long slice_start[2];
  116. unsigned long slice_end[2];
  117. unsigned long last_finish_time; /* ns / 1024 */
  118. unsigned long checked_last_finish_time; /* ns / 1024 */
  119. unsigned long avg_idletime; /* ns / 1024 */
  120. unsigned long idletime_threshold; /* us */
  121. unsigned long idletime_threshold_conf; /* us */
  122. unsigned int bio_cnt; /* total bios */
  123. unsigned int bad_bio_cnt; /* bios exceeding latency threshold */
  124. unsigned long bio_cnt_reset_time;
  125. struct blkg_rwstat stat_bytes;
  126. struct blkg_rwstat stat_ios;
  127. };
  128. extern struct blkcg_policy blkcg_policy_throtl;
  129. static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
  130. {
  131. return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
  132. }
  133. static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
  134. {
  135. return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
  136. }
  137. /*
  138. * Internal throttling interface
  139. */
  140. #ifndef CONFIG_BLK_DEV_THROTTLING
  141. static inline int blk_throtl_init(struct gendisk *disk) { return 0; }
  142. static inline void blk_throtl_exit(struct gendisk *disk) { }
  143. static inline void blk_throtl_register(struct gendisk *disk) { }
  144. static inline bool blk_throtl_bio(struct bio *bio) { return false; }
  145. static inline void blk_throtl_cancel_bios(struct gendisk *disk) { }
  146. #else /* CONFIG_BLK_DEV_THROTTLING */
  147. int blk_throtl_init(struct gendisk *disk);
  148. void blk_throtl_exit(struct gendisk *disk);
  149. void blk_throtl_register(struct gendisk *disk);
  150. bool __blk_throtl_bio(struct bio *bio);
  151. void blk_throtl_cancel_bios(struct gendisk *disk);
  152. static inline bool blk_should_throtl(struct bio *bio)
  153. {
  154. struct throtl_grp *tg = blkg_to_tg(bio->bi_blkg);
  155. int rw = bio_data_dir(bio);
  156. /* iops limit is always counted */
  157. if (tg->has_rules_iops[rw])
  158. return true;
  159. if (tg->has_rules_bps[rw] && !bio_flagged(bio, BIO_BPS_THROTTLED))
  160. return true;
  161. return false;
  162. }
  163. static inline bool blk_throtl_bio(struct bio *bio)
  164. {
  165. if (!blk_should_throtl(bio))
  166. return false;
  167. return __blk_throtl_bio(bio);
  168. }
  169. #endif /* CONFIG_BLK_DEV_THROTTLING */
  170. #endif