Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Interface for controlling IO bandwidth on a request queue |
| 3 | * |
| 4 | * Copyright (C) 2010 Vivek Goyal <vgoyal@redhat.com> |
| 5 | */ |
| 6 | |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/blkdev.h> |
| 10 | #include <linux/bio.h> |
| 11 | #include <linux/blktrace_api.h> |
Tejun Heo | eea8f41 | 2015-05-22 17:13:17 -0400 | [diff] [blame] | 12 | #include <linux/blk-cgroup.h> |
Tejun Heo | bc9fcbf | 2011-10-19 14:31:18 +0200 | [diff] [blame] | 13 | #include "blk.h" |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 14 | |
| 15 | /* Max dispatch from a group in 1 round */ |
| 16 | static int throtl_grp_quantum = 8; |
| 17 | |
| 18 | /* Total max dispatch from all groups in one round */ |
| 19 | static int throtl_quantum = 32; |
| 20 | |
Shaohua Li | d61fcfa | 2017-03-27 10:51:38 -0700 | [diff] [blame] | 21 | /* Throttling is performed over a slice and after that slice is renewed */ |
| 22 | #define DFL_THROTL_SLICE_HD (HZ / 10) |
| 23 | #define DFL_THROTL_SLICE_SSD (HZ / 50) |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 24 | #define MAX_THROTL_SLICE (HZ) |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 25 | #define DFL_IDLE_THRESHOLD_SSD (1000L) /* 1 ms */ |
| 26 | #define DFL_IDLE_THRESHOLD_HD (100L * 1000) /* 100 ms */ |
| 27 | #define MAX_IDLE_TIME (5L * 1000 * 1000) /* 5 s */ |
Shaohua Li | ec80991 | 2017-03-27 10:51:44 -0700 | [diff] [blame^] | 28 | /* default latency target is 0, eg, guarantee IO latency by default */ |
| 29 | #define DFL_LATENCY_TARGET (0) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 30 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 31 | static struct blkcg_policy blkcg_policy_throtl; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 32 | |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 33 | /* A workqueue to queue throttle related work */ |
| 34 | static struct workqueue_struct *kthrotld_workqueue; |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 35 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 36 | /* |
| 37 | * To implement hierarchical throttling, throtl_grps form a tree and bios |
| 38 | * are dispatched upwards level by level until they reach the top and get |
| 39 | * issued. When dispatching bios from the children and local group at each |
| 40 | * level, if the bios are dispatched into a single bio_list, there's a risk |
| 41 | * of a local or child group which can queue many bios at once filling up |
| 42 | * the list starving others. |
| 43 | * |
| 44 | * To avoid such starvation, dispatched bios are queued separately |
| 45 | * according to where they came from. When they are again dispatched to |
| 46 | * the parent, they're popped in round-robin order so that no single source |
| 47 | * hogs the dispatch window. |
| 48 | * |
| 49 | * throtl_qnode is used to keep the queued bios separated by their sources. |
| 50 | * Bios are queued to throtl_qnode which in turn is queued to |
| 51 | * throtl_service_queue and then dispatched in round-robin order. |
| 52 | * |
| 53 | * It's also used to track the reference counts on blkg's. A qnode always |
| 54 | * belongs to a throtl_grp and gets queued on itself or the parent, so |
| 55 | * incrementing the reference of the associated throtl_grp when a qnode is |
| 56 | * queued and decrementing when dequeued is enough to keep the whole blkg |
| 57 | * tree pinned while bios are in flight. |
| 58 | */ |
| 59 | struct throtl_qnode { |
| 60 | struct list_head node; /* service_queue->queued[] */ |
| 61 | struct bio_list bios; /* queued bios */ |
| 62 | struct throtl_grp *tg; /* tg this qnode belongs to */ |
| 63 | }; |
| 64 | |
Tejun Heo | c9e0332 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 65 | struct throtl_service_queue { |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 66 | struct throtl_service_queue *parent_sq; /* the parent service_queue */ |
| 67 | |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 68 | /* |
| 69 | * Bios queued directly to this service_queue or dispatched from |
| 70 | * children throtl_grp's. |
| 71 | */ |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 72 | struct list_head queued[2]; /* throtl_qnode [READ/WRITE] */ |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 73 | unsigned int nr_queued[2]; /* number of queued bios */ |
| 74 | |
| 75 | /* |
| 76 | * RB tree of active children throtl_grp's, which are sorted by |
| 77 | * their ->disptime. |
| 78 | */ |
Tejun Heo | c9e0332 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 79 | struct rb_root pending_tree; /* RB tree of active tgs */ |
| 80 | struct rb_node *first_pending; /* first node in the tree */ |
| 81 | unsigned int nr_pending; /* # queued in the tree */ |
| 82 | unsigned long first_pending_disptime; /* disptime of the first tg */ |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 83 | struct timer_list pending_timer; /* fires on first_pending_disptime */ |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 84 | }; |
| 85 | |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 86 | enum tg_state_flags { |
| 87 | THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */ |
Tejun Heo | 0e9f416 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 88 | THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */ |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 91 | #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node) |
| 92 | |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 93 | enum { |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 94 | LIMIT_LOW, |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 95 | LIMIT_MAX, |
| 96 | LIMIT_CNT, |
| 97 | }; |
| 98 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 99 | struct throtl_grp { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 100 | /* must be the first member */ |
| 101 | struct blkg_policy_data pd; |
| 102 | |
Tejun Heo | c9e0332 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 103 | /* active throtl group service_queue member */ |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 104 | struct rb_node rb_node; |
| 105 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 106 | /* throtl_data this group belongs to */ |
| 107 | struct throtl_data *td; |
| 108 | |
Tejun Heo | 49a2f1e | 2013-05-14 13:52:34 -0700 | [diff] [blame] | 109 | /* this group's service queue */ |
| 110 | struct throtl_service_queue service_queue; |
| 111 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 112 | /* |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 113 | * qnode_on_self is used when bios are directly queued to this |
| 114 | * throtl_grp so that local bios compete fairly with bios |
| 115 | * dispatched from children. qnode_on_parent is used when bios are |
| 116 | * dispatched from this throtl_grp into its parent and will compete |
| 117 | * with the sibling qnode_on_parents and the parent's |
| 118 | * qnode_on_self. |
| 119 | */ |
| 120 | struct throtl_qnode qnode_on_self[2]; |
| 121 | struct throtl_qnode qnode_on_parent[2]; |
| 122 | |
| 123 | /* |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 124 | * Dispatch time in jiffies. This is the estimated time when group |
| 125 | * will unthrottle and is ready to dispatch more bio. It is used as |
| 126 | * key to sort active groups in service tree. |
| 127 | */ |
| 128 | unsigned long disptime; |
| 129 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 130 | unsigned int flags; |
| 131 | |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 132 | /* are there any throtl rules between this group and td? */ |
| 133 | bool has_rules[2]; |
| 134 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 135 | /* internally used bytes per second rate limits */ |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 136 | uint64_t bps[2][LIMIT_CNT]; |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 137 | /* user configured bps limits */ |
| 138 | uint64_t bps_conf[2][LIMIT_CNT]; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 139 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 140 | /* internally used IOPS limits */ |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 141 | unsigned int iops[2][LIMIT_CNT]; |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 142 | /* user configured IOPS limits */ |
| 143 | unsigned int iops_conf[2][LIMIT_CNT]; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 144 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 145 | /* Number of bytes disptached in current slice */ |
| 146 | uint64_t bytes_disp[2]; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 147 | /* Number of bio's dispatched in current slice */ |
| 148 | unsigned int io_disp[2]; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 149 | |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 150 | unsigned long last_low_overflow_time[2]; |
| 151 | |
| 152 | uint64_t last_bytes_disp[2]; |
| 153 | unsigned int last_io_disp[2]; |
| 154 | |
| 155 | unsigned long last_check_time; |
| 156 | |
Shaohua Li | ec80991 | 2017-03-27 10:51:44 -0700 | [diff] [blame^] | 157 | unsigned long latency_target; /* us */ |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 158 | /* When did we start a new slice */ |
| 159 | unsigned long slice_start[2]; |
| 160 | unsigned long slice_end[2]; |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 161 | |
| 162 | unsigned long last_finish_time; /* ns / 1024 */ |
| 163 | unsigned long checked_last_finish_time; /* ns / 1024 */ |
| 164 | unsigned long avg_idletime; /* ns / 1024 */ |
| 165 | unsigned long idletime_threshold; /* us */ |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | struct throtl_data |
| 169 | { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 170 | /* service tree for active throtl groups */ |
Tejun Heo | c9e0332 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 171 | struct throtl_service_queue service_queue; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 172 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 173 | struct request_queue *queue; |
| 174 | |
| 175 | /* Total Number of queued bios on READ and WRITE lists */ |
| 176 | unsigned int nr_queued[2]; |
| 177 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 178 | unsigned int throtl_slice; |
| 179 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 180 | /* Work for dispatching throttled bios */ |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 181 | struct work_struct dispatch_work; |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 182 | unsigned int limit_index; |
| 183 | bool limit_valid[LIMIT_CNT]; |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 184 | |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame] | 185 | unsigned long dft_idletime_threshold; /* us */ |
| 186 | |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 187 | unsigned long low_upgrade_time; |
| 188 | unsigned long low_downgrade_time; |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 189 | |
| 190 | unsigned int scale; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 191 | }; |
| 192 | |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 193 | static void throtl_pending_timer_fn(unsigned long arg); |
| 194 | |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 195 | static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd) |
| 196 | { |
| 197 | return pd ? container_of(pd, struct throtl_grp, pd) : NULL; |
| 198 | } |
| 199 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 200 | static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 201 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 202 | return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl)); |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 205 | static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 206 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 207 | return pd_to_blkg(&tg->pd); |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 210 | /** |
| 211 | * sq_to_tg - return the throl_grp the specified service queue belongs to |
| 212 | * @sq: the throtl_service_queue of interest |
| 213 | * |
| 214 | * Return the throtl_grp @sq belongs to. If @sq is the top-level one |
| 215 | * embedded in throtl_data, %NULL is returned. |
| 216 | */ |
| 217 | static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq) |
| 218 | { |
| 219 | if (sq && sq->parent_sq) |
| 220 | return container_of(sq, struct throtl_grp, service_queue); |
| 221 | else |
| 222 | return NULL; |
| 223 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 224 | |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 225 | /** |
| 226 | * sq_to_td - return throtl_data the specified service queue belongs to |
| 227 | * @sq: the throtl_service_queue of interest |
| 228 | * |
Masahiro Yamada | b43daed | 2017-02-27 14:29:09 -0800 | [diff] [blame] | 229 | * A service_queue can be embedded in either a throtl_grp or throtl_data. |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 230 | * Determine the associated throtl_data accordingly and return it. |
| 231 | */ |
| 232 | static struct throtl_data *sq_to_td(struct throtl_service_queue *sq) |
| 233 | { |
| 234 | struct throtl_grp *tg = sq_to_tg(sq); |
| 235 | |
| 236 | if (tg) |
| 237 | return tg->td; |
| 238 | else |
| 239 | return container_of(sq, struct throtl_data, service_queue); |
| 240 | } |
| 241 | |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 242 | /* |
| 243 | * cgroup's limit in LIMIT_MAX is scaled if low limit is set. This scale is to |
| 244 | * make the IO dispatch more smooth. |
| 245 | * Scale up: linearly scale up according to lapsed time since upgrade. For |
| 246 | * every throtl_slice, the limit scales up 1/2 .low limit till the |
| 247 | * limit hits .max limit |
| 248 | * Scale down: exponentially scale down if a cgroup doesn't hit its .low limit |
| 249 | */ |
| 250 | static uint64_t throtl_adjusted_limit(uint64_t low, struct throtl_data *td) |
| 251 | { |
| 252 | /* arbitrary value to avoid too big scale */ |
| 253 | if (td->scale < 4096 && time_after_eq(jiffies, |
| 254 | td->low_upgrade_time + td->scale * td->throtl_slice)) |
| 255 | td->scale = (jiffies - td->low_upgrade_time) / td->throtl_slice; |
| 256 | |
| 257 | return low + (low >> 1) * td->scale; |
| 258 | } |
| 259 | |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 260 | static uint64_t tg_bps_limit(struct throtl_grp *tg, int rw) |
| 261 | { |
Shaohua Li | b22c417 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 262 | struct blkcg_gq *blkg = tg_to_blkg(tg); |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 263 | struct throtl_data *td; |
Shaohua Li | b22c417 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 264 | uint64_t ret; |
| 265 | |
| 266 | if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent) |
| 267 | return U64_MAX; |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 268 | |
| 269 | td = tg->td; |
| 270 | ret = tg->bps[rw][td->limit_index]; |
| 271 | if (ret == 0 && td->limit_index == LIMIT_LOW) |
Shaohua Li | b22c417 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 272 | return tg->bps[rw][LIMIT_MAX]; |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 273 | |
| 274 | if (td->limit_index == LIMIT_MAX && tg->bps[rw][LIMIT_LOW] && |
| 275 | tg->bps[rw][LIMIT_LOW] != tg->bps[rw][LIMIT_MAX]) { |
| 276 | uint64_t adjusted; |
| 277 | |
| 278 | adjusted = throtl_adjusted_limit(tg->bps[rw][LIMIT_LOW], td); |
| 279 | ret = min(tg->bps[rw][LIMIT_MAX], adjusted); |
| 280 | } |
Shaohua Li | b22c417 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 281 | return ret; |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw) |
| 285 | { |
Shaohua Li | b22c417 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 286 | struct blkcg_gq *blkg = tg_to_blkg(tg); |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 287 | struct throtl_data *td; |
Shaohua Li | b22c417 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 288 | unsigned int ret; |
| 289 | |
| 290 | if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent) |
| 291 | return UINT_MAX; |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 292 | td = tg->td; |
| 293 | ret = tg->iops[rw][td->limit_index]; |
Shaohua Li | b22c417 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 294 | if (ret == 0 && tg->td->limit_index == LIMIT_LOW) |
| 295 | return tg->iops[rw][LIMIT_MAX]; |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 296 | |
| 297 | if (td->limit_index == LIMIT_MAX && tg->iops[rw][LIMIT_LOW] && |
| 298 | tg->iops[rw][LIMIT_LOW] != tg->iops[rw][LIMIT_MAX]) { |
| 299 | uint64_t adjusted; |
| 300 | |
| 301 | adjusted = throtl_adjusted_limit(tg->iops[rw][LIMIT_LOW], td); |
| 302 | if (adjusted > UINT_MAX) |
| 303 | adjusted = UINT_MAX; |
| 304 | ret = min_t(unsigned int, tg->iops[rw][LIMIT_MAX], adjusted); |
| 305 | } |
Shaohua Li | b22c417 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 306 | return ret; |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 309 | /** |
| 310 | * throtl_log - log debug message via blktrace |
| 311 | * @sq: the service_queue being reported |
| 312 | * @fmt: printf format string |
| 313 | * @args: printf args |
| 314 | * |
| 315 | * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a |
| 316 | * throtl_grp; otherwise, just "throtl". |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 317 | */ |
| 318 | #define throtl_log(sq, fmt, args...) do { \ |
| 319 | struct throtl_grp *__tg = sq_to_tg((sq)); \ |
| 320 | struct throtl_data *__td = sq_to_td((sq)); \ |
| 321 | \ |
| 322 | (void)__td; \ |
Shaohua Li | 59fa022 | 2016-05-09 17:22:15 -0700 | [diff] [blame] | 323 | if (likely(!blk_trace_note_message_enabled(__td->queue))) \ |
| 324 | break; \ |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 325 | if ((__tg)) { \ |
| 326 | char __pbuf[128]; \ |
| 327 | \ |
| 328 | blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \ |
| 329 | blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \ |
| 330 | } else { \ |
| 331 | blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \ |
| 332 | } \ |
| 333 | } while (0) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 334 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 335 | static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg) |
| 336 | { |
| 337 | INIT_LIST_HEAD(&qn->node); |
| 338 | bio_list_init(&qn->bios); |
| 339 | qn->tg = tg; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it |
| 344 | * @bio: bio being added |
| 345 | * @qn: qnode to add bio to |
| 346 | * @queued: the service_queue->queued[] list @qn belongs to |
| 347 | * |
| 348 | * Add @bio to @qn and put @qn on @queued if it's not already on. |
| 349 | * @qn->tg's reference count is bumped when @qn is activated. See the |
| 350 | * comment on top of throtl_qnode definition for details. |
| 351 | */ |
| 352 | static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn, |
| 353 | struct list_head *queued) |
| 354 | { |
| 355 | bio_list_add(&qn->bios, bio); |
| 356 | if (list_empty(&qn->node)) { |
| 357 | list_add_tail(&qn->node, queued); |
| 358 | blkg_get(tg_to_blkg(qn->tg)); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * throtl_peek_queued - peek the first bio on a qnode list |
| 364 | * @queued: the qnode list to peek |
| 365 | */ |
| 366 | static struct bio *throtl_peek_queued(struct list_head *queued) |
| 367 | { |
| 368 | struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node); |
| 369 | struct bio *bio; |
| 370 | |
| 371 | if (list_empty(queued)) |
| 372 | return NULL; |
| 373 | |
| 374 | bio = bio_list_peek(&qn->bios); |
| 375 | WARN_ON_ONCE(!bio); |
| 376 | return bio; |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * throtl_pop_queued - pop the first bio form a qnode list |
| 381 | * @queued: the qnode list to pop a bio from |
| 382 | * @tg_to_put: optional out argument for throtl_grp to put |
| 383 | * |
| 384 | * Pop the first bio from the qnode list @queued. After popping, the first |
| 385 | * qnode is removed from @queued if empty or moved to the end of @queued so |
| 386 | * that the popping order is round-robin. |
| 387 | * |
| 388 | * When the first qnode is removed, its associated throtl_grp should be put |
| 389 | * too. If @tg_to_put is NULL, this function automatically puts it; |
| 390 | * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is |
| 391 | * responsible for putting it. |
| 392 | */ |
| 393 | static struct bio *throtl_pop_queued(struct list_head *queued, |
| 394 | struct throtl_grp **tg_to_put) |
| 395 | { |
| 396 | struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node); |
| 397 | struct bio *bio; |
| 398 | |
| 399 | if (list_empty(queued)) |
| 400 | return NULL; |
| 401 | |
| 402 | bio = bio_list_pop(&qn->bios); |
| 403 | WARN_ON_ONCE(!bio); |
| 404 | |
| 405 | if (bio_list_empty(&qn->bios)) { |
| 406 | list_del_init(&qn->node); |
| 407 | if (tg_to_put) |
| 408 | *tg_to_put = qn->tg; |
| 409 | else |
| 410 | blkg_put(tg_to_blkg(qn->tg)); |
| 411 | } else { |
| 412 | list_move_tail(&qn->node, queued); |
| 413 | } |
| 414 | |
| 415 | return bio; |
| 416 | } |
| 417 | |
Tejun Heo | 49a2f1e | 2013-05-14 13:52:34 -0700 | [diff] [blame] | 418 | /* init a service_queue, assumes the caller zeroed it */ |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 419 | static void throtl_service_queue_init(struct throtl_service_queue *sq) |
Tejun Heo | 49a2f1e | 2013-05-14 13:52:34 -0700 | [diff] [blame] | 420 | { |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 421 | INIT_LIST_HEAD(&sq->queued[0]); |
| 422 | INIT_LIST_HEAD(&sq->queued[1]); |
Tejun Heo | 49a2f1e | 2013-05-14 13:52:34 -0700 | [diff] [blame] | 423 | sq->pending_tree = RB_ROOT; |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 424 | setup_timer(&sq->pending_timer, throtl_pending_timer_fn, |
| 425 | (unsigned long)sq); |
| 426 | } |
| 427 | |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 428 | static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node) |
| 429 | { |
Tejun Heo | 4fb7203 | 2015-08-18 14:55:12 -0700 | [diff] [blame] | 430 | struct throtl_grp *tg; |
Tejun Heo | 24bdb8e | 2015-08-18 14:55:22 -0700 | [diff] [blame] | 431 | int rw; |
Tejun Heo | 4fb7203 | 2015-08-18 14:55:12 -0700 | [diff] [blame] | 432 | |
| 433 | tg = kzalloc_node(sizeof(*tg), gfp, node); |
| 434 | if (!tg) |
Tejun Heo | 77ea733 | 2015-08-18 14:55:24 -0700 | [diff] [blame] | 435 | return NULL; |
Tejun Heo | 4fb7203 | 2015-08-18 14:55:12 -0700 | [diff] [blame] | 436 | |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 437 | throtl_service_queue_init(&tg->service_queue); |
| 438 | |
| 439 | for (rw = READ; rw <= WRITE; rw++) { |
| 440 | throtl_qnode_init(&tg->qnode_on_self[rw], tg); |
| 441 | throtl_qnode_init(&tg->qnode_on_parent[rw], tg); |
| 442 | } |
| 443 | |
| 444 | RB_CLEAR_NODE(&tg->rb_node); |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 445 | tg->bps[READ][LIMIT_MAX] = U64_MAX; |
| 446 | tg->bps[WRITE][LIMIT_MAX] = U64_MAX; |
| 447 | tg->iops[READ][LIMIT_MAX] = UINT_MAX; |
| 448 | tg->iops[WRITE][LIMIT_MAX] = UINT_MAX; |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 449 | tg->bps_conf[READ][LIMIT_MAX] = U64_MAX; |
| 450 | tg->bps_conf[WRITE][LIMIT_MAX] = U64_MAX; |
| 451 | tg->iops_conf[READ][LIMIT_MAX] = UINT_MAX; |
| 452 | tg->iops_conf[WRITE][LIMIT_MAX] = UINT_MAX; |
| 453 | /* LIMIT_LOW will have default value 0 */ |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 454 | |
Shaohua Li | ec80991 | 2017-03-27 10:51:44 -0700 | [diff] [blame^] | 455 | tg->latency_target = DFL_LATENCY_TARGET; |
| 456 | |
Tejun Heo | 4fb7203 | 2015-08-18 14:55:12 -0700 | [diff] [blame] | 457 | return &tg->pd; |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 460 | static void throtl_pd_init(struct blkg_policy_data *pd) |
Vivek Goyal | a29a171 | 2011-05-19 15:38:19 -0400 | [diff] [blame] | 461 | { |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 462 | struct throtl_grp *tg = pd_to_tg(pd); |
| 463 | struct blkcg_gq *blkg = tg_to_blkg(tg); |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 464 | struct throtl_data *td = blkg->q->td; |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 465 | struct throtl_service_queue *sq = &tg->service_queue; |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 466 | |
Tejun Heo | 9138125 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 467 | /* |
Tejun Heo | aa6ec29 | 2014-07-09 10:08:08 -0400 | [diff] [blame] | 468 | * If on the default hierarchy, we switch to properly hierarchical |
Tejun Heo | 9138125 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 469 | * behavior where limits on a given throtl_grp are applied to the |
| 470 | * whole subtree rather than just the group itself. e.g. If 16M |
| 471 | * read_bps limit is set on the root group, the whole system can't |
| 472 | * exceed 16M for the device. |
| 473 | * |
Tejun Heo | aa6ec29 | 2014-07-09 10:08:08 -0400 | [diff] [blame] | 474 | * If not on the default hierarchy, the broken flat hierarchy |
Tejun Heo | 9138125 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 475 | * behavior is retained where all throtl_grps are treated as if |
| 476 | * they're all separate root groups right below throtl_data. |
| 477 | * Limits of a group don't interact with limits of other groups |
| 478 | * regardless of the position of the group in the hierarchy. |
| 479 | */ |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 480 | sq->parent_sq = &td->service_queue; |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 481 | if (cgroup_subsys_on_dfl(io_cgrp_subsys) && blkg->parent) |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 482 | sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue; |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 483 | tg->td = td; |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 484 | |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame] | 485 | tg->idletime_threshold = td->dft_idletime_threshold; |
Tejun Heo | 8a3d261 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 488 | /* |
| 489 | * Set has_rules[] if @tg or any of its parents have limits configured. |
| 490 | * This doesn't require walking up to the top of the hierarchy as the |
| 491 | * parent's has_rules[] is guaranteed to be correct. |
| 492 | */ |
| 493 | static void tg_update_has_rules(struct throtl_grp *tg) |
| 494 | { |
| 495 | struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq); |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 496 | struct throtl_data *td = tg->td; |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 497 | int rw; |
| 498 | |
| 499 | for (rw = READ; rw <= WRITE; rw++) |
| 500 | tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) || |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 501 | (td->limit_valid[td->limit_index] && |
| 502 | (tg_bps_limit(tg, rw) != U64_MAX || |
| 503 | tg_iops_limit(tg, rw) != UINT_MAX)); |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 506 | static void throtl_pd_online(struct blkg_policy_data *pd) |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 507 | { |
Shaohua Li | aec2424 | 2017-03-27 10:51:39 -0700 | [diff] [blame] | 508 | struct throtl_grp *tg = pd_to_tg(pd); |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 509 | /* |
| 510 | * We don't want new groups to escape the limits of its ancestors. |
| 511 | * Update has_rules[] after a new group is brought online. |
| 512 | */ |
Shaohua Li | aec2424 | 2017-03-27 10:51:39 -0700 | [diff] [blame] | 513 | tg_update_has_rules(tg); |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 516 | static void blk_throtl_update_limit_valid(struct throtl_data *td) |
| 517 | { |
| 518 | struct cgroup_subsys_state *pos_css; |
| 519 | struct blkcg_gq *blkg; |
| 520 | bool low_valid = false; |
| 521 | |
| 522 | rcu_read_lock(); |
| 523 | blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) { |
| 524 | struct throtl_grp *tg = blkg_to_tg(blkg); |
| 525 | |
| 526 | if (tg->bps[READ][LIMIT_LOW] || tg->bps[WRITE][LIMIT_LOW] || |
| 527 | tg->iops[READ][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW]) |
| 528 | low_valid = true; |
| 529 | } |
| 530 | rcu_read_unlock(); |
| 531 | |
| 532 | td->limit_valid[LIMIT_LOW] = low_valid; |
| 533 | } |
| 534 | |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 535 | static void throtl_upgrade_state(struct throtl_data *td); |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 536 | static void throtl_pd_offline(struct blkg_policy_data *pd) |
| 537 | { |
| 538 | struct throtl_grp *tg = pd_to_tg(pd); |
| 539 | |
| 540 | tg->bps[READ][LIMIT_LOW] = 0; |
| 541 | tg->bps[WRITE][LIMIT_LOW] = 0; |
| 542 | tg->iops[READ][LIMIT_LOW] = 0; |
| 543 | tg->iops[WRITE][LIMIT_LOW] = 0; |
| 544 | |
| 545 | blk_throtl_update_limit_valid(tg->td); |
| 546 | |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 547 | if (!tg->td->limit_valid[tg->td->limit_index]) |
| 548 | throtl_upgrade_state(tg->td); |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 551 | static void throtl_pd_free(struct blkg_policy_data *pd) |
| 552 | { |
Tejun Heo | 4fb7203 | 2015-08-18 14:55:12 -0700 | [diff] [blame] | 553 | struct throtl_grp *tg = pd_to_tg(pd); |
| 554 | |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 555 | del_timer_sync(&tg->service_queue.pending_timer); |
Tejun Heo | 4fb7203 | 2015-08-18 14:55:12 -0700 | [diff] [blame] | 556 | kfree(tg); |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 559 | static struct throtl_grp * |
| 560 | throtl_rb_first(struct throtl_service_queue *parent_sq) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 561 | { |
| 562 | /* Service tree is empty */ |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 563 | if (!parent_sq->nr_pending) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 564 | return NULL; |
| 565 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 566 | if (!parent_sq->first_pending) |
| 567 | parent_sq->first_pending = rb_first(&parent_sq->pending_tree); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 568 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 569 | if (parent_sq->first_pending) |
| 570 | return rb_entry_tg(parent_sq->first_pending); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 571 | |
| 572 | return NULL; |
| 573 | } |
| 574 | |
| 575 | static void rb_erase_init(struct rb_node *n, struct rb_root *root) |
| 576 | { |
| 577 | rb_erase(n, root); |
| 578 | RB_CLEAR_NODE(n); |
| 579 | } |
| 580 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 581 | static void throtl_rb_erase(struct rb_node *n, |
| 582 | struct throtl_service_queue *parent_sq) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 583 | { |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 584 | if (parent_sq->first_pending == n) |
| 585 | parent_sq->first_pending = NULL; |
| 586 | rb_erase_init(n, &parent_sq->pending_tree); |
| 587 | --parent_sq->nr_pending; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 588 | } |
| 589 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 590 | static void update_min_dispatch_time(struct throtl_service_queue *parent_sq) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 591 | { |
| 592 | struct throtl_grp *tg; |
| 593 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 594 | tg = throtl_rb_first(parent_sq); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 595 | if (!tg) |
| 596 | return; |
| 597 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 598 | parent_sq->first_pending_disptime = tg->disptime; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 599 | } |
| 600 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 601 | static void tg_service_queue_add(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 602 | { |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 603 | struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq; |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 604 | struct rb_node **node = &parent_sq->pending_tree.rb_node; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 605 | struct rb_node *parent = NULL; |
| 606 | struct throtl_grp *__tg; |
| 607 | unsigned long key = tg->disptime; |
| 608 | int left = 1; |
| 609 | |
| 610 | while (*node != NULL) { |
| 611 | parent = *node; |
| 612 | __tg = rb_entry_tg(parent); |
| 613 | |
| 614 | if (time_before(key, __tg->disptime)) |
| 615 | node = &parent->rb_left; |
| 616 | else { |
| 617 | node = &parent->rb_right; |
| 618 | left = 0; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | if (left) |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 623 | parent_sq->first_pending = &tg->rb_node; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 624 | |
| 625 | rb_link_node(&tg->rb_node, parent, node); |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 626 | rb_insert_color(&tg->rb_node, &parent_sq->pending_tree); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 627 | } |
| 628 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 629 | static void __throtl_enqueue_tg(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 630 | { |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 631 | tg_service_queue_add(tg); |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 632 | tg->flags |= THROTL_TG_PENDING; |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 633 | tg->service_queue.parent_sq->nr_pending++; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 634 | } |
| 635 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 636 | static void throtl_enqueue_tg(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 637 | { |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 638 | if (!(tg->flags & THROTL_TG_PENDING)) |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 639 | __throtl_enqueue_tg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 640 | } |
| 641 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 642 | static void __throtl_dequeue_tg(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 643 | { |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 644 | throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq); |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 645 | tg->flags &= ~THROTL_TG_PENDING; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 646 | } |
| 647 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 648 | static void throtl_dequeue_tg(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 649 | { |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 650 | if (tg->flags & THROTL_TG_PENDING) |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 651 | __throtl_dequeue_tg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 652 | } |
| 653 | |
Tejun Heo | a9131a2 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 654 | /* Call with queue lock held */ |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 655 | static void throtl_schedule_pending_timer(struct throtl_service_queue *sq, |
| 656 | unsigned long expires) |
Tejun Heo | a9131a2 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 657 | { |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 658 | unsigned long max_expire = jiffies + 8 * sq_to_tg(sq)->td->throtl_slice; |
Shaohua Li | 06cceed | 2017-03-27 10:51:36 -0700 | [diff] [blame] | 659 | |
| 660 | /* |
| 661 | * Since we are adjusting the throttle limit dynamically, the sleep |
| 662 | * time calculated according to previous limit might be invalid. It's |
| 663 | * possible the cgroup sleep time is very long and no other cgroups |
| 664 | * have IO running so notify the limit changes. Make sure the cgroup |
| 665 | * doesn't sleep too long to avoid the missed notification. |
| 666 | */ |
| 667 | if (time_after(expires, max_expire)) |
| 668 | expires = max_expire; |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 669 | mod_timer(&sq->pending_timer, expires); |
| 670 | throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu", |
| 671 | expires - jiffies, jiffies); |
Tejun Heo | a9131a2 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 674 | /** |
| 675 | * throtl_schedule_next_dispatch - schedule the next dispatch cycle |
| 676 | * @sq: the service_queue to schedule dispatch for |
| 677 | * @force: force scheduling |
| 678 | * |
| 679 | * Arm @sq->pending_timer so that the next dispatch cycle starts on the |
| 680 | * dispatch time of the first pending child. Returns %true if either timer |
| 681 | * is armed or there's no pending child left. %false if the current |
| 682 | * dispatch window is still open and the caller should continue |
| 683 | * dispatching. |
| 684 | * |
| 685 | * If @force is %true, the dispatch timer is always scheduled and this |
| 686 | * function is guaranteed to return %true. This is to be used when the |
| 687 | * caller can't dispatch itself and needs to invoke pending_timer |
| 688 | * unconditionally. Note that forced scheduling is likely to induce short |
| 689 | * delay before dispatch starts even if @sq->first_pending_disptime is not |
| 690 | * in the future and thus shouldn't be used in hot paths. |
| 691 | */ |
| 692 | static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq, |
| 693 | bool force) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 694 | { |
Tejun Heo | 6a52560 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 695 | /* any pending children left? */ |
Tejun Heo | c9e0332 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 696 | if (!sq->nr_pending) |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 697 | return true; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 698 | |
Tejun Heo | c9e0332 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 699 | update_min_dispatch_time(sq); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 700 | |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 701 | /* is the next dispatch time in the future? */ |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 702 | if (force || time_after(sq->first_pending_disptime, jiffies)) { |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 703 | throtl_schedule_pending_timer(sq, sq->first_pending_disptime); |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 704 | return true; |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 707 | /* tell the caller to continue dispatching */ |
| 708 | return false; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 709 | } |
| 710 | |
Vivek Goyal | 32ee5bc | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 711 | static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg, |
| 712 | bool rw, unsigned long start) |
| 713 | { |
| 714 | tg->bytes_disp[rw] = 0; |
| 715 | tg->io_disp[rw] = 0; |
| 716 | |
| 717 | /* |
| 718 | * Previous slice has expired. We must have trimmed it after last |
| 719 | * bio dispatch. That means since start of last slice, we never used |
| 720 | * that bandwidth. Do try to make use of that bandwidth while giving |
| 721 | * credit. |
| 722 | */ |
| 723 | if (time_after_eq(start, tg->slice_start[rw])) |
| 724 | tg->slice_start[rw] = start; |
| 725 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 726 | tg->slice_end[rw] = jiffies + tg->td->throtl_slice; |
Vivek Goyal | 32ee5bc | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 727 | throtl_log(&tg->service_queue, |
| 728 | "[%c] new slice with credit start=%lu end=%lu jiffies=%lu", |
| 729 | rw == READ ? 'R' : 'W', tg->slice_start[rw], |
| 730 | tg->slice_end[rw], jiffies); |
| 731 | } |
| 732 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 733 | static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 734 | { |
| 735 | tg->bytes_disp[rw] = 0; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 736 | tg->io_disp[rw] = 0; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 737 | tg->slice_start[rw] = jiffies; |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 738 | tg->slice_end[rw] = jiffies + tg->td->throtl_slice; |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 739 | throtl_log(&tg->service_queue, |
| 740 | "[%c] new slice start=%lu end=%lu jiffies=%lu", |
| 741 | rw == READ ? 'R' : 'W', tg->slice_start[rw], |
| 742 | tg->slice_end[rw], jiffies); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 743 | } |
| 744 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 745 | static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw, |
| 746 | unsigned long jiffy_end) |
Vivek Goyal | d1ae8ff | 2010-12-01 19:34:46 +0100 | [diff] [blame] | 747 | { |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 748 | tg->slice_end[rw] = roundup(jiffy_end, tg->td->throtl_slice); |
Vivek Goyal | d1ae8ff | 2010-12-01 19:34:46 +0100 | [diff] [blame] | 749 | } |
| 750 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 751 | static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw, |
| 752 | unsigned long jiffy_end) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 753 | { |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 754 | tg->slice_end[rw] = roundup(jiffy_end, tg->td->throtl_slice); |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 755 | throtl_log(&tg->service_queue, |
| 756 | "[%c] extend slice start=%lu end=%lu jiffies=%lu", |
| 757 | rw == READ ? 'R' : 'W', tg->slice_start[rw], |
| 758 | tg->slice_end[rw], jiffies); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | /* Determine if previously allocated or extended slice is complete or not */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 762 | static bool throtl_slice_used(struct throtl_grp *tg, bool rw) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 763 | { |
| 764 | if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw])) |
Fabian Frederick | 5cf8c22 | 2014-05-02 18:28:17 +0200 | [diff] [blame] | 765 | return false; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 766 | |
| 767 | return 1; |
| 768 | } |
| 769 | |
| 770 | /* Trim the used slices and adjust slice start accordingly */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 771 | static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 772 | { |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 773 | unsigned long nr_slices, time_elapsed, io_trim; |
| 774 | u64 bytes_trim, tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 775 | |
| 776 | BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw])); |
| 777 | |
| 778 | /* |
| 779 | * If bps are unlimited (-1), then time slice don't get |
| 780 | * renewed. Don't try to trim the slice if slice is used. A new |
| 781 | * slice will start when appropriate. |
| 782 | */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 783 | if (throtl_slice_used(tg, rw)) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 784 | return; |
| 785 | |
Vivek Goyal | d1ae8ff | 2010-12-01 19:34:46 +0100 | [diff] [blame] | 786 | /* |
| 787 | * A bio has been dispatched. Also adjust slice_end. It might happen |
| 788 | * that initially cgroup limit was very low resulting in high |
| 789 | * slice_end, but later limit was bumped up and bio was dispached |
| 790 | * sooner, then we need to reduce slice_end. A high bogus slice_end |
| 791 | * is bad because it does not allow new slice to start. |
| 792 | */ |
| 793 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 794 | throtl_set_slice_end(tg, rw, jiffies + tg->td->throtl_slice); |
Vivek Goyal | d1ae8ff | 2010-12-01 19:34:46 +0100 | [diff] [blame] | 795 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 796 | time_elapsed = jiffies - tg->slice_start[rw]; |
| 797 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 798 | nr_slices = time_elapsed / tg->td->throtl_slice; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 799 | |
| 800 | if (!nr_slices) |
| 801 | return; |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 802 | tmp = tg_bps_limit(tg, rw) * tg->td->throtl_slice * nr_slices; |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 803 | do_div(tmp, HZ); |
| 804 | bytes_trim = tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 805 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 806 | io_trim = (tg_iops_limit(tg, rw) * tg->td->throtl_slice * nr_slices) / |
| 807 | HZ; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 808 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 809 | if (!bytes_trim && !io_trim) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 810 | return; |
| 811 | |
| 812 | if (tg->bytes_disp[rw] >= bytes_trim) |
| 813 | tg->bytes_disp[rw] -= bytes_trim; |
| 814 | else |
| 815 | tg->bytes_disp[rw] = 0; |
| 816 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 817 | if (tg->io_disp[rw] >= io_trim) |
| 818 | tg->io_disp[rw] -= io_trim; |
| 819 | else |
| 820 | tg->io_disp[rw] = 0; |
| 821 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 822 | tg->slice_start[rw] += nr_slices * tg->td->throtl_slice; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 823 | |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 824 | throtl_log(&tg->service_queue, |
| 825 | "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu", |
| 826 | rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim, |
| 827 | tg->slice_start[rw], tg->slice_end[rw], jiffies); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 828 | } |
| 829 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 830 | static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio, |
| 831 | unsigned long *wait) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 832 | { |
| 833 | bool rw = bio_data_dir(bio); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 834 | unsigned int io_allowed; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 835 | unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd; |
Vivek Goyal | c49c06e | 2010-10-01 21:16:42 +0200 | [diff] [blame] | 836 | u64 tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 837 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 838 | jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw]; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 839 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 840 | /* Slice has just started. Consider one slice interval */ |
| 841 | if (!jiffy_elapsed) |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 842 | jiffy_elapsed_rnd = tg->td->throtl_slice; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 843 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 844 | jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 845 | |
Vivek Goyal | c49c06e | 2010-10-01 21:16:42 +0200 | [diff] [blame] | 846 | /* |
| 847 | * jiffy_elapsed_rnd should not be a big value as minimum iops can be |
| 848 | * 1 then at max jiffy elapsed should be equivalent of 1 second as we |
| 849 | * will allow dispatch after 1 second and after that slice should |
| 850 | * have been trimmed. |
| 851 | */ |
| 852 | |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 853 | tmp = (u64)tg_iops_limit(tg, rw) * jiffy_elapsed_rnd; |
Vivek Goyal | c49c06e | 2010-10-01 21:16:42 +0200 | [diff] [blame] | 854 | do_div(tmp, HZ); |
| 855 | |
| 856 | if (tmp > UINT_MAX) |
| 857 | io_allowed = UINT_MAX; |
| 858 | else |
| 859 | io_allowed = tmp; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 860 | |
| 861 | if (tg->io_disp[rw] + 1 <= io_allowed) { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 862 | if (wait) |
| 863 | *wait = 0; |
Fabian Frederick | 5cf8c22 | 2014-05-02 18:28:17 +0200 | [diff] [blame] | 864 | return true; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 865 | } |
| 866 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 867 | /* Calc approx time to dispatch */ |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 868 | jiffy_wait = ((tg->io_disp[rw] + 1) * HZ) / tg_iops_limit(tg, rw) + 1; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 869 | |
| 870 | if (jiffy_wait > jiffy_elapsed) |
| 871 | jiffy_wait = jiffy_wait - jiffy_elapsed; |
| 872 | else |
| 873 | jiffy_wait = 1; |
| 874 | |
| 875 | if (wait) |
| 876 | *wait = jiffy_wait; |
| 877 | return 0; |
| 878 | } |
| 879 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 880 | static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio, |
| 881 | unsigned long *wait) |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 882 | { |
| 883 | bool rw = bio_data_dir(bio); |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 884 | u64 bytes_allowed, extra_bytes, tmp; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 885 | unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 886 | |
| 887 | jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw]; |
| 888 | |
| 889 | /* Slice has just started. Consider one slice interval */ |
| 890 | if (!jiffy_elapsed) |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 891 | jiffy_elapsed_rnd = tg->td->throtl_slice; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 892 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 893 | jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 894 | |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 895 | tmp = tg_bps_limit(tg, rw) * jiffy_elapsed_rnd; |
Vivek Goyal | 5e901a2 | 2010-10-01 21:16:38 +0200 | [diff] [blame] | 896 | do_div(tmp, HZ); |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 897 | bytes_allowed = tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 898 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 899 | if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 900 | if (wait) |
| 901 | *wait = 0; |
Fabian Frederick | 5cf8c22 | 2014-05-02 18:28:17 +0200 | [diff] [blame] | 902 | return true; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | /* Calc approx time to dispatch */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 906 | extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed; |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 907 | jiffy_wait = div64_u64(extra_bytes * HZ, tg_bps_limit(tg, rw)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 908 | |
| 909 | if (!jiffy_wait) |
| 910 | jiffy_wait = 1; |
| 911 | |
| 912 | /* |
| 913 | * This wait time is without taking into consideration the rounding |
| 914 | * up we did. Add that time also. |
| 915 | */ |
| 916 | jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 917 | if (wait) |
| 918 | *wait = jiffy_wait; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 919 | return 0; |
| 920 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 921 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 922 | /* |
| 923 | * Returns whether one can dispatch a bio or not. Also returns approx number |
| 924 | * of jiffies to wait before this bio is with-in IO rate and can be dispatched |
| 925 | */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 926 | static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio, |
| 927 | unsigned long *wait) |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 928 | { |
| 929 | bool rw = bio_data_dir(bio); |
| 930 | unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0; |
| 931 | |
| 932 | /* |
| 933 | * Currently whole state machine of group depends on first bio |
| 934 | * queued in the group bio list. So one should not be calling |
| 935 | * this function with a different bio if there are other bios |
| 936 | * queued. |
| 937 | */ |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 938 | BUG_ON(tg->service_queue.nr_queued[rw] && |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 939 | bio != throtl_peek_queued(&tg->service_queue.queued[rw])); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 940 | |
| 941 | /* If tg->bps = -1, then BW is unlimited */ |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 942 | if (tg_bps_limit(tg, rw) == U64_MAX && |
| 943 | tg_iops_limit(tg, rw) == UINT_MAX) { |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 944 | if (wait) |
| 945 | *wait = 0; |
Fabian Frederick | 5cf8c22 | 2014-05-02 18:28:17 +0200 | [diff] [blame] | 946 | return true; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | /* |
| 950 | * If previous slice expired, start a new one otherwise renew/extend |
| 951 | * existing slice to make sure it is at least throtl_slice interval |
Vivek Goyal | 164c80e | 2016-09-19 15:12:41 -0600 | [diff] [blame] | 952 | * long since now. New slice is started only for empty throttle group. |
| 953 | * If there is queued bio, that means there should be an active |
| 954 | * slice and it should be extended instead. |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 955 | */ |
Vivek Goyal | 164c80e | 2016-09-19 15:12:41 -0600 | [diff] [blame] | 956 | if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw])) |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 957 | throtl_start_new_slice(tg, rw); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 958 | else { |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 959 | if (time_before(tg->slice_end[rw], |
| 960 | jiffies + tg->td->throtl_slice)) |
| 961 | throtl_extend_slice(tg, rw, |
| 962 | jiffies + tg->td->throtl_slice); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 963 | } |
| 964 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 965 | if (tg_with_in_bps_limit(tg, bio, &bps_wait) && |
| 966 | tg_with_in_iops_limit(tg, bio, &iops_wait)) { |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 967 | if (wait) |
| 968 | *wait = 0; |
| 969 | return 1; |
| 970 | } |
| 971 | |
| 972 | max_wait = max(bps_wait, iops_wait); |
| 973 | |
| 974 | if (wait) |
| 975 | *wait = max_wait; |
| 976 | |
| 977 | if (time_before(tg->slice_end[rw], jiffies + max_wait)) |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 978 | throtl_extend_slice(tg, rw, jiffies + max_wait); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 979 | |
| 980 | return 0; |
| 981 | } |
| 982 | |
| 983 | static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio) |
| 984 | { |
| 985 | bool rw = bio_data_dir(bio); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 986 | |
| 987 | /* Charge the bio to the group */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 988 | tg->bytes_disp[rw] += bio->bi_iter.bi_size; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 989 | tg->io_disp[rw]++; |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 990 | tg->last_bytes_disp[rw] += bio->bi_iter.bi_size; |
| 991 | tg->last_io_disp[rw]++; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 992 | |
Tejun Heo | 2a0f61e | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 993 | /* |
Christoph Hellwig | 8d2bbd4 | 2016-10-20 15:12:12 +0200 | [diff] [blame] | 994 | * BIO_THROTTLED is used to prevent the same bio to be throttled |
Tejun Heo | 2a0f61e | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 995 | * more than once as a throttled bio will go through blk-throtl the |
| 996 | * second time when it eventually gets issued. Set it when a bio |
| 997 | * is being charged to a tg. |
Tejun Heo | 2a0f61e | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 998 | */ |
Christoph Hellwig | 8d2bbd4 | 2016-10-20 15:12:12 +0200 | [diff] [blame] | 999 | if (!bio_flagged(bio, BIO_THROTTLED)) |
| 1000 | bio_set_flag(bio, BIO_THROTTLED); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1001 | } |
| 1002 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1003 | /** |
| 1004 | * throtl_add_bio_tg - add a bio to the specified throtl_grp |
| 1005 | * @bio: bio to add |
| 1006 | * @qn: qnode to use |
| 1007 | * @tg: the target throtl_grp |
| 1008 | * |
| 1009 | * Add @bio to @tg's service_queue using @qn. If @qn is not specified, |
| 1010 | * tg->qnode_on_self[] is used. |
| 1011 | */ |
| 1012 | static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn, |
| 1013 | struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1014 | { |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1015 | struct throtl_service_queue *sq = &tg->service_queue; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1016 | bool rw = bio_data_dir(bio); |
| 1017 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1018 | if (!qn) |
| 1019 | qn = &tg->qnode_on_self[rw]; |
| 1020 | |
Tejun Heo | 0e9f416 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1021 | /* |
| 1022 | * If @tg doesn't currently have any bios queued in the same |
| 1023 | * direction, queueing @bio can change when @tg should be |
| 1024 | * dispatched. Mark that @tg was empty. This is automatically |
| 1025 | * cleaered on the next tg_update_disptime(). |
| 1026 | */ |
| 1027 | if (!sq->nr_queued[rw]) |
| 1028 | tg->flags |= THROTL_TG_WAS_EMPTY; |
| 1029 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1030 | throtl_qnode_add_bio(bio, qn, &sq->queued[rw]); |
| 1031 | |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1032 | sq->nr_queued[rw]++; |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1033 | throtl_enqueue_tg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1034 | } |
| 1035 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1036 | static void tg_update_disptime(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1037 | { |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1038 | struct throtl_service_queue *sq = &tg->service_queue; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1039 | unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime; |
| 1040 | struct bio *bio; |
| 1041 | |
Markus Elfring | d609af3 | 2017-01-21 22:15:33 +0100 | [diff] [blame] | 1042 | bio = throtl_peek_queued(&sq->queued[READ]); |
| 1043 | if (bio) |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1044 | tg_may_dispatch(tg, bio, &read_wait); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1045 | |
Markus Elfring | d609af3 | 2017-01-21 22:15:33 +0100 | [diff] [blame] | 1046 | bio = throtl_peek_queued(&sq->queued[WRITE]); |
| 1047 | if (bio) |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1048 | tg_may_dispatch(tg, bio, &write_wait); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1049 | |
| 1050 | min_wait = min(read_wait, write_wait); |
| 1051 | disptime = jiffies + min_wait; |
| 1052 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1053 | /* Update dispatch time */ |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1054 | throtl_dequeue_tg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1055 | tg->disptime = disptime; |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1056 | throtl_enqueue_tg(tg); |
Tejun Heo | 0e9f416 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1057 | |
| 1058 | /* see throtl_add_bio_tg() */ |
| 1059 | tg->flags &= ~THROTL_TG_WAS_EMPTY; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1060 | } |
| 1061 | |
Vivek Goyal | 32ee5bc | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1062 | static void start_parent_slice_with_credit(struct throtl_grp *child_tg, |
| 1063 | struct throtl_grp *parent_tg, bool rw) |
| 1064 | { |
| 1065 | if (throtl_slice_used(parent_tg, rw)) { |
| 1066 | throtl_start_new_slice_with_credit(parent_tg, rw, |
| 1067 | child_tg->slice_start[rw]); |
| 1068 | } |
| 1069 | |
| 1070 | } |
| 1071 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1072 | static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1073 | { |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1074 | struct throtl_service_queue *sq = &tg->service_queue; |
Tejun Heo | 6bc9c2b | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1075 | struct throtl_service_queue *parent_sq = sq->parent_sq; |
| 1076 | struct throtl_grp *parent_tg = sq_to_tg(parent_sq); |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1077 | struct throtl_grp *tg_to_put = NULL; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1078 | struct bio *bio; |
| 1079 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1080 | /* |
| 1081 | * @bio is being transferred from @tg to @parent_sq. Popping a bio |
| 1082 | * from @tg may put its reference and @parent_sq might end up |
| 1083 | * getting released prematurely. Remember the tg to put and put it |
| 1084 | * after @bio is transferred to @parent_sq. |
| 1085 | */ |
| 1086 | bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put); |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1087 | sq->nr_queued[rw]--; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1088 | |
| 1089 | throtl_charge_bio(tg, bio); |
Tejun Heo | 6bc9c2b | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1090 | |
| 1091 | /* |
| 1092 | * If our parent is another tg, we just need to transfer @bio to |
| 1093 | * the parent using throtl_add_bio_tg(). If our parent is |
| 1094 | * @td->service_queue, @bio is ready to be issued. Put it on its |
| 1095 | * bio_lists[] and decrease total number queued. The caller is |
| 1096 | * responsible for issuing these bios. |
| 1097 | */ |
| 1098 | if (parent_tg) { |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1099 | throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg); |
Vivek Goyal | 32ee5bc | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1100 | start_parent_slice_with_credit(tg, parent_tg, rw); |
Tejun Heo | 6bc9c2b | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1101 | } else { |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1102 | throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw], |
| 1103 | &parent_sq->queued[rw]); |
Tejun Heo | 6bc9c2b | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1104 | BUG_ON(tg->td->nr_queued[rw] <= 0); |
| 1105 | tg->td->nr_queued[rw]--; |
| 1106 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1107 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1108 | throtl_trim_slice(tg, rw); |
Tejun Heo | 6bc9c2b | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1109 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1110 | if (tg_to_put) |
| 1111 | blkg_put(tg_to_blkg(tg_to_put)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1112 | } |
| 1113 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1114 | static int throtl_dispatch_tg(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1115 | { |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1116 | struct throtl_service_queue *sq = &tg->service_queue; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1117 | unsigned int nr_reads = 0, nr_writes = 0; |
| 1118 | unsigned int max_nr_reads = throtl_grp_quantum*3/4; |
Vivek Goyal | c2f6805 | 2010-11-15 19:32:42 +0100 | [diff] [blame] | 1119 | unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1120 | struct bio *bio; |
| 1121 | |
| 1122 | /* Try to dispatch 75% READS and 25% WRITES */ |
| 1123 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1124 | while ((bio = throtl_peek_queued(&sq->queued[READ])) && |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1125 | tg_may_dispatch(tg, bio, NULL)) { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1126 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1127 | tg_dispatch_one_bio(tg, bio_data_dir(bio)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1128 | nr_reads++; |
| 1129 | |
| 1130 | if (nr_reads >= max_nr_reads) |
| 1131 | break; |
| 1132 | } |
| 1133 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1134 | while ((bio = throtl_peek_queued(&sq->queued[WRITE])) && |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1135 | tg_may_dispatch(tg, bio, NULL)) { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1136 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1137 | tg_dispatch_one_bio(tg, bio_data_dir(bio)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1138 | nr_writes++; |
| 1139 | |
| 1140 | if (nr_writes >= max_nr_writes) |
| 1141 | break; |
| 1142 | } |
| 1143 | |
| 1144 | return nr_reads + nr_writes; |
| 1145 | } |
| 1146 | |
Tejun Heo | 651930b | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1147 | static int throtl_select_dispatch(struct throtl_service_queue *parent_sq) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1148 | { |
| 1149 | unsigned int nr_disp = 0; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1150 | |
| 1151 | while (1) { |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1152 | struct throtl_grp *tg = throtl_rb_first(parent_sq); |
| 1153 | struct throtl_service_queue *sq = &tg->service_queue; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1154 | |
| 1155 | if (!tg) |
| 1156 | break; |
| 1157 | |
| 1158 | if (time_before(jiffies, tg->disptime)) |
| 1159 | break; |
| 1160 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1161 | throtl_dequeue_tg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1162 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1163 | nr_disp += throtl_dispatch_tg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1164 | |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1165 | if (sq->nr_queued[0] || sq->nr_queued[1]) |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1166 | tg_update_disptime(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1167 | |
| 1168 | if (nr_disp >= throtl_quantum) |
| 1169 | break; |
| 1170 | } |
| 1171 | |
| 1172 | return nr_disp; |
| 1173 | } |
| 1174 | |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1175 | static bool throtl_can_upgrade(struct throtl_data *td, |
| 1176 | struct throtl_grp *this_tg); |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1177 | /** |
| 1178 | * throtl_pending_timer_fn - timer function for service_queue->pending_timer |
| 1179 | * @arg: the throtl_service_queue being serviced |
| 1180 | * |
| 1181 | * This timer is armed when a child throtl_grp with active bio's become |
| 1182 | * pending and queued on the service_queue's pending_tree and expires when |
| 1183 | * the first child throtl_grp should be dispatched. This function |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1184 | * dispatches bio's from the children throtl_grps to the parent |
| 1185 | * service_queue. |
| 1186 | * |
| 1187 | * If the parent's parent is another throtl_grp, dispatching is propagated |
| 1188 | * by either arming its pending_timer or repeating dispatch directly. If |
| 1189 | * the top-level service_tree is reached, throtl_data->dispatch_work is |
| 1190 | * kicked so that the ready bio's are issued. |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1191 | */ |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1192 | static void throtl_pending_timer_fn(unsigned long arg) |
| 1193 | { |
| 1194 | struct throtl_service_queue *sq = (void *)arg; |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1195 | struct throtl_grp *tg = sq_to_tg(sq); |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1196 | struct throtl_data *td = sq_to_td(sq); |
Tejun Heo | cb76199 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 1197 | struct request_queue *q = td->queue; |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1198 | struct throtl_service_queue *parent_sq; |
| 1199 | bool dispatched; |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1200 | int ret; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1201 | |
| 1202 | spin_lock_irq(q->queue_lock); |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1203 | if (throtl_can_upgrade(td, NULL)) |
| 1204 | throtl_upgrade_state(td); |
| 1205 | |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1206 | again: |
| 1207 | parent_sq = sq->parent_sq; |
| 1208 | dispatched = false; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1209 | |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1210 | while (true) { |
| 1211 | throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u", |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1212 | sq->nr_queued[READ] + sq->nr_queued[WRITE], |
| 1213 | sq->nr_queued[READ], sq->nr_queued[WRITE]); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1214 | |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1215 | ret = throtl_select_dispatch(sq); |
| 1216 | if (ret) { |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1217 | throtl_log(sq, "bios disp=%u", ret); |
| 1218 | dispatched = true; |
Tejun Heo | 651930b | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1219 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1220 | |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1221 | if (throtl_schedule_next_dispatch(sq, false)) |
| 1222 | break; |
| 1223 | |
| 1224 | /* this dispatch windows is still open, relax and repeat */ |
| 1225 | spin_unlock_irq(q->queue_lock); |
| 1226 | cpu_relax(); |
| 1227 | spin_lock_irq(q->queue_lock); |
| 1228 | } |
Tejun Heo | 6a52560 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1229 | |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1230 | if (!dispatched) |
| 1231 | goto out_unlock; |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1232 | |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1233 | if (parent_sq) { |
| 1234 | /* @parent_sq is another throl_grp, propagate dispatch */ |
| 1235 | if (tg->flags & THROTL_TG_WAS_EMPTY) { |
| 1236 | tg_update_disptime(tg); |
| 1237 | if (!throtl_schedule_next_dispatch(parent_sq, false)) { |
| 1238 | /* window is already open, repeat dispatching */ |
| 1239 | sq = parent_sq; |
| 1240 | tg = sq_to_tg(sq); |
| 1241 | goto again; |
| 1242 | } |
| 1243 | } |
| 1244 | } else { |
| 1245 | /* reached the top-level, queue issueing */ |
| 1246 | queue_work(kthrotld_workqueue, &td->dispatch_work); |
| 1247 | } |
| 1248 | out_unlock: |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1249 | spin_unlock_irq(q->queue_lock); |
| 1250 | } |
| 1251 | |
| 1252 | /** |
| 1253 | * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work |
| 1254 | * @work: work item being executed |
| 1255 | * |
| 1256 | * This function is queued for execution when bio's reach the bio_lists[] |
| 1257 | * of throtl_data->service_queue. Those bio's are ready and issued by this |
| 1258 | * function. |
| 1259 | */ |
Fabian Frederick | 8876e14 | 2014-04-17 21:41:16 +0200 | [diff] [blame] | 1260 | static void blk_throtl_dispatch_work_fn(struct work_struct *work) |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1261 | { |
| 1262 | struct throtl_data *td = container_of(work, struct throtl_data, |
| 1263 | dispatch_work); |
| 1264 | struct throtl_service_queue *td_sq = &td->service_queue; |
| 1265 | struct request_queue *q = td->queue; |
| 1266 | struct bio_list bio_list_on_stack; |
| 1267 | struct bio *bio; |
| 1268 | struct blk_plug plug; |
| 1269 | int rw; |
| 1270 | |
| 1271 | bio_list_init(&bio_list_on_stack); |
| 1272 | |
| 1273 | spin_lock_irq(q->queue_lock); |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1274 | for (rw = READ; rw <= WRITE; rw++) |
| 1275 | while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL))) |
| 1276 | bio_list_add(&bio_list_on_stack, bio); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1277 | spin_unlock_irq(q->queue_lock); |
| 1278 | |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1279 | if (!bio_list_empty(&bio_list_on_stack)) { |
Vivek Goyal | 69d60eb | 2011-03-09 08:27:37 +0100 | [diff] [blame] | 1280 | blk_start_plug(&plug); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1281 | while((bio = bio_list_pop(&bio_list_on_stack))) |
| 1282 | generic_make_request(bio); |
Vivek Goyal | 69d60eb | 2011-03-09 08:27:37 +0100 | [diff] [blame] | 1283 | blk_finish_plug(&plug); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1284 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1285 | } |
| 1286 | |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1287 | static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd, |
| 1288 | int off) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1289 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1290 | struct throtl_grp *tg = pd_to_tg(pd); |
| 1291 | u64 v = *(u64 *)((void *)tg + off); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1292 | |
Shaohua Li | 2ab5492 | 2017-03-27 10:51:29 -0700 | [diff] [blame] | 1293 | if (v == U64_MAX) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1294 | return 0; |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1295 | return __blkg_prfill_u64(sf, pd, v); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1298 | static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd, |
| 1299 | int off) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1300 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1301 | struct throtl_grp *tg = pd_to_tg(pd); |
| 1302 | unsigned int v = *(unsigned int *)((void *)tg + off); |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1303 | |
Shaohua Li | 2ab5492 | 2017-03-27 10:51:29 -0700 | [diff] [blame] | 1304 | if (v == UINT_MAX) |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1305 | return 0; |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1306 | return __blkg_prfill_u64(sf, pd, v); |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1309 | static int tg_print_conf_u64(struct seq_file *sf, void *v) |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1310 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1311 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64, |
| 1312 | &blkcg_policy_throtl, seq_cft(sf)->private, false); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1313 | return 0; |
| 1314 | } |
| 1315 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1316 | static int tg_print_conf_uint(struct seq_file *sf, void *v) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1317 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1318 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint, |
| 1319 | &blkcg_policy_throtl, seq_cft(sf)->private, false); |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1320 | return 0; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1321 | } |
| 1322 | |
Tejun Heo | 69948b0 | 2015-08-18 14:55:32 -0700 | [diff] [blame] | 1323 | static void tg_conf_updated(struct throtl_grp *tg) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1324 | { |
Tejun Heo | 69948b0 | 2015-08-18 14:55:32 -0700 | [diff] [blame] | 1325 | struct throtl_service_queue *sq = &tg->service_queue; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 1326 | struct cgroup_subsys_state *pos_css; |
Tejun Heo | 69948b0 | 2015-08-18 14:55:32 -0700 | [diff] [blame] | 1327 | struct blkcg_gq *blkg; |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1328 | |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1329 | throtl_log(&tg->service_queue, |
| 1330 | "limit change rbps=%llu wbps=%llu riops=%u wiops=%u", |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1331 | tg_bps_limit(tg, READ), tg_bps_limit(tg, WRITE), |
| 1332 | tg_iops_limit(tg, READ), tg_iops_limit(tg, WRITE)); |
Tejun Heo | 632b449 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 1333 | |
| 1334 | /* |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1335 | * Update has_rules[] flags for the updated tg's subtree. A tg is |
| 1336 | * considered to have rules if either the tg itself or any of its |
| 1337 | * ancestors has rules. This identifies groups without any |
| 1338 | * restrictions in the whole hierarchy and allows them to bypass |
| 1339 | * blk-throttle. |
| 1340 | */ |
Tejun Heo | 69948b0 | 2015-08-18 14:55:32 -0700 | [diff] [blame] | 1341 | blkg_for_each_descendant_pre(blkg, pos_css, tg_to_blkg(tg)) |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1342 | tg_update_has_rules(blkg_to_tg(blkg)); |
| 1343 | |
| 1344 | /* |
Tejun Heo | 632b449 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 1345 | * We're already holding queue_lock and know @tg is valid. Let's |
| 1346 | * apply the new config directly. |
| 1347 | * |
| 1348 | * Restart the slices for both READ and WRITES. It might happen |
| 1349 | * that a group's limit are dropped suddenly and we don't want to |
| 1350 | * account recently dispatched IO with new low rate. |
| 1351 | */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1352 | throtl_start_new_slice(tg, 0); |
| 1353 | throtl_start_new_slice(tg, 1); |
Tejun Heo | 632b449 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 1354 | |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1355 | if (tg->flags & THROTL_TG_PENDING) { |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1356 | tg_update_disptime(tg); |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1357 | throtl_schedule_next_dispatch(sq->parent_sq, true); |
Tejun Heo | 632b449 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 1358 | } |
Tejun Heo | 69948b0 | 2015-08-18 14:55:32 -0700 | [diff] [blame] | 1359 | } |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1360 | |
Tejun Heo | 69948b0 | 2015-08-18 14:55:32 -0700 | [diff] [blame] | 1361 | static ssize_t tg_set_conf(struct kernfs_open_file *of, |
| 1362 | char *buf, size_t nbytes, loff_t off, bool is_u64) |
| 1363 | { |
| 1364 | struct blkcg *blkcg = css_to_blkcg(of_css(of)); |
| 1365 | struct blkg_conf_ctx ctx; |
| 1366 | struct throtl_grp *tg; |
| 1367 | int ret; |
| 1368 | u64 v; |
| 1369 | |
| 1370 | ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx); |
| 1371 | if (ret) |
| 1372 | return ret; |
| 1373 | |
| 1374 | ret = -EINVAL; |
| 1375 | if (sscanf(ctx.body, "%llu", &v) != 1) |
| 1376 | goto out_finish; |
| 1377 | if (!v) |
Shaohua Li | 2ab5492 | 2017-03-27 10:51:29 -0700 | [diff] [blame] | 1378 | v = U64_MAX; |
Tejun Heo | 69948b0 | 2015-08-18 14:55:32 -0700 | [diff] [blame] | 1379 | |
| 1380 | tg = blkg_to_tg(ctx.blkg); |
| 1381 | |
| 1382 | if (is_u64) |
| 1383 | *(u64 *)((void *)tg + of_cft(of)->private) = v; |
| 1384 | else |
| 1385 | *(unsigned int *)((void *)tg + of_cft(of)->private) = v; |
| 1386 | |
| 1387 | tg_conf_updated(tg); |
Tejun Heo | 36aa9e5 | 2015-08-18 14:55:31 -0700 | [diff] [blame] | 1388 | ret = 0; |
| 1389 | out_finish: |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1390 | blkg_conf_finish(&ctx); |
Tejun Heo | 36aa9e5 | 2015-08-18 14:55:31 -0700 | [diff] [blame] | 1391 | return ret ?: nbytes; |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1394 | static ssize_t tg_set_conf_u64(struct kernfs_open_file *of, |
| 1395 | char *buf, size_t nbytes, loff_t off) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1396 | { |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1397 | return tg_set_conf(of, buf, nbytes, off, true); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1400 | static ssize_t tg_set_conf_uint(struct kernfs_open_file *of, |
| 1401 | char *buf, size_t nbytes, loff_t off) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1402 | { |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1403 | return tg_set_conf(of, buf, nbytes, off, false); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
Tejun Heo | 880f50e | 2015-08-18 14:55:30 -0700 | [diff] [blame] | 1406 | static struct cftype throtl_legacy_files[] = { |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1407 | { |
| 1408 | .name = "throttle.read_bps_device", |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1409 | .private = offsetof(struct throtl_grp, bps[READ][LIMIT_MAX]), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1410 | .seq_show = tg_print_conf_u64, |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1411 | .write = tg_set_conf_u64, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1412 | }, |
| 1413 | { |
| 1414 | .name = "throttle.write_bps_device", |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1415 | .private = offsetof(struct throtl_grp, bps[WRITE][LIMIT_MAX]), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1416 | .seq_show = tg_print_conf_u64, |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1417 | .write = tg_set_conf_u64, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1418 | }, |
| 1419 | { |
| 1420 | .name = "throttle.read_iops_device", |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1421 | .private = offsetof(struct throtl_grp, iops[READ][LIMIT_MAX]), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1422 | .seq_show = tg_print_conf_uint, |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1423 | .write = tg_set_conf_uint, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1424 | }, |
| 1425 | { |
| 1426 | .name = "throttle.write_iops_device", |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1427 | .private = offsetof(struct throtl_grp, iops[WRITE][LIMIT_MAX]), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1428 | .seq_show = tg_print_conf_uint, |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1429 | .write = tg_set_conf_uint, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1430 | }, |
| 1431 | { |
| 1432 | .name = "throttle.io_service_bytes", |
Tejun Heo | 77ea733 | 2015-08-18 14:55:24 -0700 | [diff] [blame] | 1433 | .private = (unsigned long)&blkcg_policy_throtl, |
| 1434 | .seq_show = blkg_print_stat_bytes, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1435 | }, |
| 1436 | { |
| 1437 | .name = "throttle.io_serviced", |
Tejun Heo | 77ea733 | 2015-08-18 14:55:24 -0700 | [diff] [blame] | 1438 | .private = (unsigned long)&blkcg_policy_throtl, |
| 1439 | .seq_show = blkg_print_stat_ios, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1440 | }, |
| 1441 | { } /* terminate */ |
| 1442 | }; |
| 1443 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1444 | static u64 tg_prfill_limit(struct seq_file *sf, struct blkg_policy_data *pd, |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1445 | int off) |
| 1446 | { |
| 1447 | struct throtl_grp *tg = pd_to_tg(pd); |
| 1448 | const char *dname = blkg_dev_name(pd->blkg); |
| 1449 | char bufs[4][21] = { "max", "max", "max", "max" }; |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1450 | u64 bps_dft; |
| 1451 | unsigned int iops_dft; |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame] | 1452 | char idle_time[26] = ""; |
Shaohua Li | ec80991 | 2017-03-27 10:51:44 -0700 | [diff] [blame^] | 1453 | char latency_time[26] = ""; |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1454 | |
| 1455 | if (!dname) |
| 1456 | return 0; |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1457 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1458 | if (off == LIMIT_LOW) { |
| 1459 | bps_dft = 0; |
| 1460 | iops_dft = 0; |
| 1461 | } else { |
| 1462 | bps_dft = U64_MAX; |
| 1463 | iops_dft = UINT_MAX; |
| 1464 | } |
| 1465 | |
| 1466 | if (tg->bps_conf[READ][off] == bps_dft && |
| 1467 | tg->bps_conf[WRITE][off] == bps_dft && |
| 1468 | tg->iops_conf[READ][off] == iops_dft && |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame] | 1469 | tg->iops_conf[WRITE][off] == iops_dft && |
Shaohua Li | ec80991 | 2017-03-27 10:51:44 -0700 | [diff] [blame^] | 1470 | (off != LIMIT_LOW || |
| 1471 | (tg->idletime_threshold == tg->td->dft_idletime_threshold && |
| 1472 | tg->latency_target == DFL_LATENCY_TARGET))) |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1473 | return 0; |
| 1474 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1475 | if (tg->bps_conf[READ][off] != bps_dft) |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1476 | snprintf(bufs[0], sizeof(bufs[0]), "%llu", |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1477 | tg->bps_conf[READ][off]); |
| 1478 | if (tg->bps_conf[WRITE][off] != bps_dft) |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1479 | snprintf(bufs[1], sizeof(bufs[1]), "%llu", |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1480 | tg->bps_conf[WRITE][off]); |
| 1481 | if (tg->iops_conf[READ][off] != iops_dft) |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1482 | snprintf(bufs[2], sizeof(bufs[2]), "%u", |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1483 | tg->iops_conf[READ][off]); |
| 1484 | if (tg->iops_conf[WRITE][off] != iops_dft) |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1485 | snprintf(bufs[3], sizeof(bufs[3]), "%u", |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1486 | tg->iops_conf[WRITE][off]); |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame] | 1487 | if (off == LIMIT_LOW) { |
| 1488 | if (tg->idletime_threshold == ULONG_MAX) |
| 1489 | strcpy(idle_time, " idle=max"); |
| 1490 | else |
| 1491 | snprintf(idle_time, sizeof(idle_time), " idle=%lu", |
| 1492 | tg->idletime_threshold); |
Shaohua Li | ec80991 | 2017-03-27 10:51:44 -0700 | [diff] [blame^] | 1493 | |
| 1494 | if (tg->latency_target == ULONG_MAX) |
| 1495 | strcpy(latency_time, " latency=max"); |
| 1496 | else |
| 1497 | snprintf(latency_time, sizeof(latency_time), |
| 1498 | " latency=%lu", tg->latency_target); |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame] | 1499 | } |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1500 | |
Shaohua Li | ec80991 | 2017-03-27 10:51:44 -0700 | [diff] [blame^] | 1501 | seq_printf(sf, "%s rbps=%s wbps=%s riops=%s wiops=%s%s%s\n", |
| 1502 | dname, bufs[0], bufs[1], bufs[2], bufs[3], idle_time, |
| 1503 | latency_time); |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1504 | return 0; |
| 1505 | } |
| 1506 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1507 | static int tg_print_limit(struct seq_file *sf, void *v) |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1508 | { |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1509 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_limit, |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1510 | &blkcg_policy_throtl, seq_cft(sf)->private, false); |
| 1511 | return 0; |
| 1512 | } |
| 1513 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1514 | static ssize_t tg_set_limit(struct kernfs_open_file *of, |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1515 | char *buf, size_t nbytes, loff_t off) |
| 1516 | { |
| 1517 | struct blkcg *blkcg = css_to_blkcg(of_css(of)); |
| 1518 | struct blkg_conf_ctx ctx; |
| 1519 | struct throtl_grp *tg; |
| 1520 | u64 v[4]; |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame] | 1521 | unsigned long idle_time; |
Shaohua Li | ec80991 | 2017-03-27 10:51:44 -0700 | [diff] [blame^] | 1522 | unsigned long latency_time; |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1523 | int ret; |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1524 | int index = of_cft(of)->private; |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1525 | |
| 1526 | ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx); |
| 1527 | if (ret) |
| 1528 | return ret; |
| 1529 | |
| 1530 | tg = blkg_to_tg(ctx.blkg); |
| 1531 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1532 | v[0] = tg->bps_conf[READ][index]; |
| 1533 | v[1] = tg->bps_conf[WRITE][index]; |
| 1534 | v[2] = tg->iops_conf[READ][index]; |
| 1535 | v[3] = tg->iops_conf[WRITE][index]; |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1536 | |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame] | 1537 | idle_time = tg->idletime_threshold; |
Shaohua Li | ec80991 | 2017-03-27 10:51:44 -0700 | [diff] [blame^] | 1538 | latency_time = tg->latency_target; |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1539 | while (true) { |
| 1540 | char tok[27]; /* wiops=18446744073709551616 */ |
| 1541 | char *p; |
Shaohua Li | 2ab5492 | 2017-03-27 10:51:29 -0700 | [diff] [blame] | 1542 | u64 val = U64_MAX; |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1543 | int len; |
| 1544 | |
| 1545 | if (sscanf(ctx.body, "%26s%n", tok, &len) != 1) |
| 1546 | break; |
| 1547 | if (tok[0] == '\0') |
| 1548 | break; |
| 1549 | ctx.body += len; |
| 1550 | |
| 1551 | ret = -EINVAL; |
| 1552 | p = tok; |
| 1553 | strsep(&p, "="); |
| 1554 | if (!p || (sscanf(p, "%llu", &val) != 1 && strcmp(p, "max"))) |
| 1555 | goto out_finish; |
| 1556 | |
| 1557 | ret = -ERANGE; |
| 1558 | if (!val) |
| 1559 | goto out_finish; |
| 1560 | |
| 1561 | ret = -EINVAL; |
| 1562 | if (!strcmp(tok, "rbps")) |
| 1563 | v[0] = val; |
| 1564 | else if (!strcmp(tok, "wbps")) |
| 1565 | v[1] = val; |
| 1566 | else if (!strcmp(tok, "riops")) |
| 1567 | v[2] = min_t(u64, val, UINT_MAX); |
| 1568 | else if (!strcmp(tok, "wiops")) |
| 1569 | v[3] = min_t(u64, val, UINT_MAX); |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame] | 1570 | else if (off == LIMIT_LOW && !strcmp(tok, "idle")) |
| 1571 | idle_time = val; |
Shaohua Li | ec80991 | 2017-03-27 10:51:44 -0700 | [diff] [blame^] | 1572 | else if (off == LIMIT_LOW && !strcmp(tok, "latency")) |
| 1573 | latency_time = val; |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1574 | else |
| 1575 | goto out_finish; |
| 1576 | } |
| 1577 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1578 | tg->bps_conf[READ][index] = v[0]; |
| 1579 | tg->bps_conf[WRITE][index] = v[1]; |
| 1580 | tg->iops_conf[READ][index] = v[2]; |
| 1581 | tg->iops_conf[WRITE][index] = v[3]; |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1582 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1583 | if (index == LIMIT_MAX) { |
| 1584 | tg->bps[READ][index] = v[0]; |
| 1585 | tg->bps[WRITE][index] = v[1]; |
| 1586 | tg->iops[READ][index] = v[2]; |
| 1587 | tg->iops[WRITE][index] = v[3]; |
| 1588 | } |
| 1589 | tg->bps[READ][LIMIT_LOW] = min(tg->bps_conf[READ][LIMIT_LOW], |
| 1590 | tg->bps_conf[READ][LIMIT_MAX]); |
| 1591 | tg->bps[WRITE][LIMIT_LOW] = min(tg->bps_conf[WRITE][LIMIT_LOW], |
| 1592 | tg->bps_conf[WRITE][LIMIT_MAX]); |
| 1593 | tg->iops[READ][LIMIT_LOW] = min(tg->iops_conf[READ][LIMIT_LOW], |
| 1594 | tg->iops_conf[READ][LIMIT_MAX]); |
| 1595 | tg->iops[WRITE][LIMIT_LOW] = min(tg->iops_conf[WRITE][LIMIT_LOW], |
| 1596 | tg->iops_conf[WRITE][LIMIT_MAX]); |
| 1597 | |
| 1598 | if (index == LIMIT_LOW) { |
| 1599 | blk_throtl_update_limit_valid(tg->td); |
| 1600 | if (tg->td->limit_valid[LIMIT_LOW]) |
| 1601 | tg->td->limit_index = LIMIT_LOW; |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame] | 1602 | tg->idletime_threshold = (idle_time == ULONG_MAX) ? |
| 1603 | ULONG_MAX : idle_time; |
Shaohua Li | ec80991 | 2017-03-27 10:51:44 -0700 | [diff] [blame^] | 1604 | tg->latency_target = (latency_time == ULONG_MAX) ? |
| 1605 | ULONG_MAX : latency_time; |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1606 | } |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1607 | tg_conf_updated(tg); |
| 1608 | ret = 0; |
| 1609 | out_finish: |
| 1610 | blkg_conf_finish(&ctx); |
| 1611 | return ret ?: nbytes; |
| 1612 | } |
| 1613 | |
| 1614 | static struct cftype throtl_files[] = { |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1615 | #ifdef CONFIG_BLK_DEV_THROTTLING_LOW |
| 1616 | { |
| 1617 | .name = "low", |
| 1618 | .flags = CFTYPE_NOT_ON_ROOT, |
| 1619 | .seq_show = tg_print_limit, |
| 1620 | .write = tg_set_limit, |
| 1621 | .private = LIMIT_LOW, |
| 1622 | }, |
| 1623 | #endif |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1624 | { |
| 1625 | .name = "max", |
| 1626 | .flags = CFTYPE_NOT_ON_ROOT, |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1627 | .seq_show = tg_print_limit, |
| 1628 | .write = tg_set_limit, |
| 1629 | .private = LIMIT_MAX, |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1630 | }, |
| 1631 | { } /* terminate */ |
| 1632 | }; |
| 1633 | |
Vivek Goyal | da52777 | 2011-03-02 19:05:33 -0500 | [diff] [blame] | 1634 | static void throtl_shutdown_wq(struct request_queue *q) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1635 | { |
| 1636 | struct throtl_data *td = q->td; |
| 1637 | |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1638 | cancel_work_sync(&td->dispatch_work); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1639 | } |
| 1640 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1641 | static struct blkcg_policy blkcg_policy_throtl = { |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1642 | .dfl_cftypes = throtl_files, |
Tejun Heo | 880f50e | 2015-08-18 14:55:30 -0700 | [diff] [blame] | 1643 | .legacy_cftypes = throtl_legacy_files, |
Tejun Heo | f9fcc2d | 2012-04-16 13:57:27 -0700 | [diff] [blame] | 1644 | |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 1645 | .pd_alloc_fn = throtl_pd_alloc, |
Tejun Heo | f9fcc2d | 2012-04-16 13:57:27 -0700 | [diff] [blame] | 1646 | .pd_init_fn = throtl_pd_init, |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1647 | .pd_online_fn = throtl_pd_online, |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1648 | .pd_offline_fn = throtl_pd_offline, |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 1649 | .pd_free_fn = throtl_pd_free, |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1650 | }; |
| 1651 | |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1652 | static unsigned long __tg_last_low_overflow_time(struct throtl_grp *tg) |
| 1653 | { |
| 1654 | unsigned long rtime = jiffies, wtime = jiffies; |
| 1655 | |
| 1656 | if (tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW]) |
| 1657 | rtime = tg->last_low_overflow_time[READ]; |
| 1658 | if (tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW]) |
| 1659 | wtime = tg->last_low_overflow_time[WRITE]; |
| 1660 | return min(rtime, wtime); |
| 1661 | } |
| 1662 | |
| 1663 | /* tg should not be an intermediate node */ |
| 1664 | static unsigned long tg_last_low_overflow_time(struct throtl_grp *tg) |
| 1665 | { |
| 1666 | struct throtl_service_queue *parent_sq; |
| 1667 | struct throtl_grp *parent = tg; |
| 1668 | unsigned long ret = __tg_last_low_overflow_time(tg); |
| 1669 | |
| 1670 | while (true) { |
| 1671 | parent_sq = parent->service_queue.parent_sq; |
| 1672 | parent = sq_to_tg(parent_sq); |
| 1673 | if (!parent) |
| 1674 | break; |
| 1675 | |
| 1676 | /* |
| 1677 | * The parent doesn't have low limit, it always reaches low |
| 1678 | * limit. Its overflow time is useless for children |
| 1679 | */ |
| 1680 | if (!parent->bps[READ][LIMIT_LOW] && |
| 1681 | !parent->iops[READ][LIMIT_LOW] && |
| 1682 | !parent->bps[WRITE][LIMIT_LOW] && |
| 1683 | !parent->iops[WRITE][LIMIT_LOW]) |
| 1684 | continue; |
| 1685 | if (time_after(__tg_last_low_overflow_time(parent), ret)) |
| 1686 | ret = __tg_last_low_overflow_time(parent); |
| 1687 | } |
| 1688 | return ret; |
| 1689 | } |
| 1690 | |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 1691 | static bool throtl_tg_is_idle(struct throtl_grp *tg) |
| 1692 | { |
| 1693 | /* |
| 1694 | * cgroup is idle if: |
| 1695 | * - single idle is too long, longer than a fixed value (in case user |
| 1696 | * configure a too big threshold) or 4 times of slice |
| 1697 | * - average think time is more than threshold |
| 1698 | */ |
| 1699 | unsigned long time = jiffies_to_usecs(4 * tg->td->throtl_slice); |
| 1700 | |
| 1701 | time = min_t(unsigned long, MAX_IDLE_TIME, time); |
| 1702 | return (ktime_get_ns() >> 10) - tg->last_finish_time > time || |
| 1703 | tg->avg_idletime > tg->idletime_threshold; |
| 1704 | } |
| 1705 | |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1706 | static bool throtl_tg_can_upgrade(struct throtl_grp *tg) |
| 1707 | { |
| 1708 | struct throtl_service_queue *sq = &tg->service_queue; |
| 1709 | bool read_limit, write_limit; |
| 1710 | |
| 1711 | /* |
| 1712 | * if cgroup reaches low limit (if low limit is 0, the cgroup always |
| 1713 | * reaches), it's ok to upgrade to next limit |
| 1714 | */ |
| 1715 | read_limit = tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW]; |
| 1716 | write_limit = tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW]; |
| 1717 | if (!read_limit && !write_limit) |
| 1718 | return true; |
| 1719 | if (read_limit && sq->nr_queued[READ] && |
| 1720 | (!write_limit || sq->nr_queued[WRITE])) |
| 1721 | return true; |
| 1722 | if (write_limit && sq->nr_queued[WRITE] && |
| 1723 | (!read_limit || sq->nr_queued[READ])) |
| 1724 | return true; |
Shaohua Li | aec2424 | 2017-03-27 10:51:39 -0700 | [diff] [blame] | 1725 | |
| 1726 | if (time_after_eq(jiffies, |
Shaohua Li | fa6fb5a | 2017-03-27 10:51:43 -0700 | [diff] [blame] | 1727 | tg_last_low_overflow_time(tg) + tg->td->throtl_slice) && |
| 1728 | throtl_tg_is_idle(tg)) |
Shaohua Li | aec2424 | 2017-03-27 10:51:39 -0700 | [diff] [blame] | 1729 | return true; |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1730 | return false; |
| 1731 | } |
| 1732 | |
| 1733 | static bool throtl_hierarchy_can_upgrade(struct throtl_grp *tg) |
| 1734 | { |
| 1735 | while (true) { |
| 1736 | if (throtl_tg_can_upgrade(tg)) |
| 1737 | return true; |
| 1738 | tg = sq_to_tg(tg->service_queue.parent_sq); |
| 1739 | if (!tg || !tg_to_blkg(tg)->parent) |
| 1740 | return false; |
| 1741 | } |
| 1742 | return false; |
| 1743 | } |
| 1744 | |
| 1745 | static bool throtl_can_upgrade(struct throtl_data *td, |
| 1746 | struct throtl_grp *this_tg) |
| 1747 | { |
| 1748 | struct cgroup_subsys_state *pos_css; |
| 1749 | struct blkcg_gq *blkg; |
| 1750 | |
| 1751 | if (td->limit_index != LIMIT_LOW) |
| 1752 | return false; |
| 1753 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 1754 | if (time_before(jiffies, td->low_downgrade_time + td->throtl_slice)) |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1755 | return false; |
| 1756 | |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1757 | rcu_read_lock(); |
| 1758 | blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) { |
| 1759 | struct throtl_grp *tg = blkg_to_tg(blkg); |
| 1760 | |
| 1761 | if (tg == this_tg) |
| 1762 | continue; |
| 1763 | if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children)) |
| 1764 | continue; |
| 1765 | if (!throtl_hierarchy_can_upgrade(tg)) { |
| 1766 | rcu_read_unlock(); |
| 1767 | return false; |
| 1768 | } |
| 1769 | } |
| 1770 | rcu_read_unlock(); |
| 1771 | return true; |
| 1772 | } |
| 1773 | |
Shaohua Li | fa6fb5a | 2017-03-27 10:51:43 -0700 | [diff] [blame] | 1774 | static void throtl_upgrade_check(struct throtl_grp *tg) |
| 1775 | { |
| 1776 | unsigned long now = jiffies; |
| 1777 | |
| 1778 | if (tg->td->limit_index != LIMIT_LOW) |
| 1779 | return; |
| 1780 | |
| 1781 | if (time_after(tg->last_check_time + tg->td->throtl_slice, now)) |
| 1782 | return; |
| 1783 | |
| 1784 | tg->last_check_time = now; |
| 1785 | |
| 1786 | if (!time_after_eq(now, |
| 1787 | __tg_last_low_overflow_time(tg) + tg->td->throtl_slice)) |
| 1788 | return; |
| 1789 | |
| 1790 | if (throtl_can_upgrade(tg->td, NULL)) |
| 1791 | throtl_upgrade_state(tg->td); |
| 1792 | } |
| 1793 | |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1794 | static void throtl_upgrade_state(struct throtl_data *td) |
| 1795 | { |
| 1796 | struct cgroup_subsys_state *pos_css; |
| 1797 | struct blkcg_gq *blkg; |
| 1798 | |
| 1799 | td->limit_index = LIMIT_MAX; |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1800 | td->low_upgrade_time = jiffies; |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 1801 | td->scale = 0; |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1802 | rcu_read_lock(); |
| 1803 | blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) { |
| 1804 | struct throtl_grp *tg = blkg_to_tg(blkg); |
| 1805 | struct throtl_service_queue *sq = &tg->service_queue; |
| 1806 | |
| 1807 | tg->disptime = jiffies - 1; |
| 1808 | throtl_select_dispatch(sq); |
| 1809 | throtl_schedule_next_dispatch(sq, false); |
| 1810 | } |
| 1811 | rcu_read_unlock(); |
| 1812 | throtl_select_dispatch(&td->service_queue); |
| 1813 | throtl_schedule_next_dispatch(&td->service_queue, false); |
| 1814 | queue_work(kthrotld_workqueue, &td->dispatch_work); |
| 1815 | } |
| 1816 | |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1817 | static void throtl_downgrade_state(struct throtl_data *td, int new) |
| 1818 | { |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 1819 | td->scale /= 2; |
| 1820 | |
| 1821 | if (td->scale) { |
| 1822 | td->low_upgrade_time = jiffies - td->scale * td->throtl_slice; |
| 1823 | return; |
| 1824 | } |
| 1825 | |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1826 | td->limit_index = new; |
| 1827 | td->low_downgrade_time = jiffies; |
| 1828 | } |
| 1829 | |
| 1830 | static bool throtl_tg_can_downgrade(struct throtl_grp *tg) |
| 1831 | { |
| 1832 | struct throtl_data *td = tg->td; |
| 1833 | unsigned long now = jiffies; |
| 1834 | |
| 1835 | /* |
| 1836 | * If cgroup is below low limit, consider downgrade and throttle other |
| 1837 | * cgroups |
| 1838 | */ |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 1839 | if (time_after_eq(now, td->low_upgrade_time + td->throtl_slice) && |
| 1840 | time_after_eq(now, tg_last_low_overflow_time(tg) + |
Shaohua Li | fa6fb5a | 2017-03-27 10:51:43 -0700 | [diff] [blame] | 1841 | td->throtl_slice) && |
| 1842 | (!throtl_tg_is_idle(tg) || |
| 1843 | !list_empty(&tg_to_blkg(tg)->blkcg->css.children))) |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1844 | return true; |
| 1845 | return false; |
| 1846 | } |
| 1847 | |
| 1848 | static bool throtl_hierarchy_can_downgrade(struct throtl_grp *tg) |
| 1849 | { |
| 1850 | while (true) { |
| 1851 | if (!throtl_tg_can_downgrade(tg)) |
| 1852 | return false; |
| 1853 | tg = sq_to_tg(tg->service_queue.parent_sq); |
| 1854 | if (!tg || !tg_to_blkg(tg)->parent) |
| 1855 | break; |
| 1856 | } |
| 1857 | return true; |
| 1858 | } |
| 1859 | |
| 1860 | static void throtl_downgrade_check(struct throtl_grp *tg) |
| 1861 | { |
| 1862 | uint64_t bps; |
| 1863 | unsigned int iops; |
| 1864 | unsigned long elapsed_time; |
| 1865 | unsigned long now = jiffies; |
| 1866 | |
| 1867 | if (tg->td->limit_index != LIMIT_MAX || |
| 1868 | !tg->td->limit_valid[LIMIT_LOW]) |
| 1869 | return; |
| 1870 | if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children)) |
| 1871 | return; |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 1872 | if (time_after(tg->last_check_time + tg->td->throtl_slice, now)) |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1873 | return; |
| 1874 | |
| 1875 | elapsed_time = now - tg->last_check_time; |
| 1876 | tg->last_check_time = now; |
| 1877 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 1878 | if (time_before(now, tg_last_low_overflow_time(tg) + |
| 1879 | tg->td->throtl_slice)) |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1880 | return; |
| 1881 | |
| 1882 | if (tg->bps[READ][LIMIT_LOW]) { |
| 1883 | bps = tg->last_bytes_disp[READ] * HZ; |
| 1884 | do_div(bps, elapsed_time); |
| 1885 | if (bps >= tg->bps[READ][LIMIT_LOW]) |
| 1886 | tg->last_low_overflow_time[READ] = now; |
| 1887 | } |
| 1888 | |
| 1889 | if (tg->bps[WRITE][LIMIT_LOW]) { |
| 1890 | bps = tg->last_bytes_disp[WRITE] * HZ; |
| 1891 | do_div(bps, elapsed_time); |
| 1892 | if (bps >= tg->bps[WRITE][LIMIT_LOW]) |
| 1893 | tg->last_low_overflow_time[WRITE] = now; |
| 1894 | } |
| 1895 | |
| 1896 | if (tg->iops[READ][LIMIT_LOW]) { |
| 1897 | iops = tg->last_io_disp[READ] * HZ / elapsed_time; |
| 1898 | if (iops >= tg->iops[READ][LIMIT_LOW]) |
| 1899 | tg->last_low_overflow_time[READ] = now; |
| 1900 | } |
| 1901 | |
| 1902 | if (tg->iops[WRITE][LIMIT_LOW]) { |
| 1903 | iops = tg->last_io_disp[WRITE] * HZ / elapsed_time; |
| 1904 | if (iops >= tg->iops[WRITE][LIMIT_LOW]) |
| 1905 | tg->last_low_overflow_time[WRITE] = now; |
| 1906 | } |
| 1907 | |
| 1908 | /* |
| 1909 | * If cgroup is below low limit, consider downgrade and throttle other |
| 1910 | * cgroups |
| 1911 | */ |
| 1912 | if (throtl_hierarchy_can_downgrade(tg)) |
| 1913 | throtl_downgrade_state(tg->td, LIMIT_LOW); |
| 1914 | |
| 1915 | tg->last_bytes_disp[READ] = 0; |
| 1916 | tg->last_bytes_disp[WRITE] = 0; |
| 1917 | tg->last_io_disp[READ] = 0; |
| 1918 | tg->last_io_disp[WRITE] = 0; |
| 1919 | } |
| 1920 | |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 1921 | static void blk_throtl_update_idletime(struct throtl_grp *tg) |
| 1922 | { |
| 1923 | unsigned long now = ktime_get_ns() >> 10; |
| 1924 | unsigned long last_finish_time = tg->last_finish_time; |
| 1925 | |
| 1926 | if (now <= last_finish_time || last_finish_time == 0 || |
| 1927 | last_finish_time == tg->checked_last_finish_time) |
| 1928 | return; |
| 1929 | |
| 1930 | tg->avg_idletime = (tg->avg_idletime * 7 + now - last_finish_time) >> 3; |
| 1931 | tg->checked_last_finish_time = last_finish_time; |
| 1932 | } |
| 1933 | |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 1934 | bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg, |
| 1935 | struct bio *bio) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1936 | { |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1937 | struct throtl_qnode *qn = NULL; |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 1938 | struct throtl_grp *tg = blkg_to_tg(blkg ?: q->root_blkg); |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1939 | struct throtl_service_queue *sq; |
Tejun Heo | 0e9f416 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1940 | bool rw = bio_data_dir(bio); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1941 | bool throttled = false; |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 1942 | int ret; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1943 | |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 1944 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 1945 | |
Tejun Heo | 2a0f61e | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1946 | /* see throtl_charge_bio() */ |
Christoph Hellwig | 8d2bbd4 | 2016-10-20 15:12:12 +0200 | [diff] [blame] | 1947 | if (bio_flagged(bio, BIO_THROTTLED) || !tg->has_rules[rw]) |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1948 | goto out; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1949 | |
| 1950 | spin_lock_irq(q->queue_lock); |
Tejun Heo | c9589f0 | 2015-08-18 14:55:19 -0700 | [diff] [blame] | 1951 | |
| 1952 | if (unlikely(blk_queue_bypass(q))) |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1953 | goto out_unlock; |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 1954 | |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 1955 | ret = bio_associate_current(bio); |
| 1956 | #ifdef CONFIG_BLK_DEV_THROTTLING_LOW |
| 1957 | if (ret == 0 || ret == -EBUSY) |
| 1958 | bio->bi_cg_private = tg; |
| 1959 | #endif |
| 1960 | blk_throtl_update_idletime(tg); |
| 1961 | |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1962 | sq = &tg->service_queue; |
| 1963 | |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1964 | again: |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1965 | while (true) { |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1966 | if (tg->last_low_overflow_time[rw] == 0) |
| 1967 | tg->last_low_overflow_time[rw] = jiffies; |
| 1968 | throtl_downgrade_check(tg); |
Shaohua Li | fa6fb5a | 2017-03-27 10:51:43 -0700 | [diff] [blame] | 1969 | throtl_upgrade_check(tg); |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1970 | /* throtl is FIFO - if bios are already queued, should queue */ |
| 1971 | if (sq->nr_queued[rw]) |
| 1972 | break; |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 1973 | |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1974 | /* if above limits, break to queue */ |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1975 | if (!tg_may_dispatch(tg, bio, NULL)) { |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1976 | tg->last_low_overflow_time[rw] = jiffies; |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1977 | if (throtl_can_upgrade(tg->td, tg)) { |
| 1978 | throtl_upgrade_state(tg->td); |
| 1979 | goto again; |
| 1980 | } |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1981 | break; |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1982 | } |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1983 | |
| 1984 | /* within limits, let's charge and dispatch directly */ |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1985 | throtl_charge_bio(tg, bio); |
Vivek Goyal | 04521db | 2011-03-22 21:54:29 +0100 | [diff] [blame] | 1986 | |
| 1987 | /* |
| 1988 | * We need to trim slice even when bios are not being queued |
| 1989 | * otherwise it might happen that a bio is not queued for |
| 1990 | * a long time and slice keeps on extending and trim is not |
| 1991 | * called for a long time. Now if limits are reduced suddenly |
| 1992 | * we take into account all the IO dispatched so far at new |
| 1993 | * low rate and * newly queued IO gets a really long dispatch |
| 1994 | * time. |
| 1995 | * |
| 1996 | * So keep on trimming slice even if bio is not queued. |
| 1997 | */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1998 | throtl_trim_slice(tg, rw); |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1999 | |
| 2000 | /* |
| 2001 | * @bio passed through this layer without being throttled. |
| 2002 | * Climb up the ladder. If we''re already at the top, it |
| 2003 | * can be executed directly. |
| 2004 | */ |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 2005 | qn = &tg->qnode_on_parent[rw]; |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 2006 | sq = sq->parent_sq; |
| 2007 | tg = sq_to_tg(sq); |
| 2008 | if (!tg) |
| 2009 | goto out_unlock; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2010 | } |
| 2011 | |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 2012 | /* out-of-limit, queue to @tg */ |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 2013 | throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d", |
| 2014 | rw == READ ? 'R' : 'W', |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 2015 | tg->bytes_disp[rw], bio->bi_iter.bi_size, |
| 2016 | tg_bps_limit(tg, rw), |
| 2017 | tg->io_disp[rw], tg_iops_limit(tg, rw), |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 2018 | sq->nr_queued[READ], sq->nr_queued[WRITE]); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2019 | |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 2020 | tg->last_low_overflow_time[rw] = jiffies; |
| 2021 | |
Tejun Heo | 6bc9c2b | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 2022 | tg->td->nr_queued[rw]++; |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 2023 | throtl_add_bio_tg(bio, qn, tg); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 2024 | throttled = true; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2025 | |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2026 | /* |
| 2027 | * Update @tg's dispatch time and force schedule dispatch if @tg |
| 2028 | * was empty before @bio. The forced scheduling isn't likely to |
| 2029 | * cause undue delay as @bio is likely to be dispatched directly if |
| 2030 | * its @tg's disptime is not in the future. |
| 2031 | */ |
Tejun Heo | 0e9f416 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 2032 | if (tg->flags & THROTL_TG_WAS_EMPTY) { |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 2033 | tg_update_disptime(tg); |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2034 | throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2035 | } |
| 2036 | |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 2037 | out_unlock: |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2038 | spin_unlock_irq(q->queue_lock); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 2039 | out: |
Tejun Heo | 2a0f61e | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 2040 | /* |
| 2041 | * As multiple blk-throtls may stack in the same issue path, we |
| 2042 | * don't want bios to leave with the flag set. Clear the flag if |
| 2043 | * being issued. |
| 2044 | */ |
| 2045 | if (!throttled) |
Christoph Hellwig | 8d2bbd4 | 2016-10-20 15:12:12 +0200 | [diff] [blame] | 2046 | bio_clear_flag(bio, BIO_THROTTLED); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 2047 | return throttled; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2048 | } |
| 2049 | |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 2050 | #ifdef CONFIG_BLK_DEV_THROTTLING_LOW |
| 2051 | void blk_throtl_bio_endio(struct bio *bio) |
| 2052 | { |
| 2053 | struct throtl_grp *tg; |
| 2054 | |
| 2055 | tg = bio->bi_cg_private; |
| 2056 | if (!tg) |
| 2057 | return; |
| 2058 | bio->bi_cg_private = NULL; |
| 2059 | |
| 2060 | tg->last_finish_time = ktime_get_ns() >> 10; |
| 2061 | } |
| 2062 | #endif |
| 2063 | |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2064 | /* |
| 2065 | * Dispatch all bios from all children tg's queued on @parent_sq. On |
| 2066 | * return, @parent_sq is guaranteed to not have any active children tg's |
| 2067 | * and all bios from previously active tg's are on @parent_sq->bio_lists[]. |
| 2068 | */ |
| 2069 | static void tg_drain_bios(struct throtl_service_queue *parent_sq) |
| 2070 | { |
| 2071 | struct throtl_grp *tg; |
| 2072 | |
| 2073 | while ((tg = throtl_rb_first(parent_sq))) { |
| 2074 | struct throtl_service_queue *sq = &tg->service_queue; |
| 2075 | struct bio *bio; |
| 2076 | |
| 2077 | throtl_dequeue_tg(tg); |
| 2078 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 2079 | while ((bio = throtl_peek_queued(&sq->queued[READ]))) |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2080 | tg_dispatch_one_bio(tg, bio_data_dir(bio)); |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 2081 | while ((bio = throtl_peek_queued(&sq->queued[WRITE]))) |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2082 | tg_dispatch_one_bio(tg, bio_data_dir(bio)); |
| 2083 | } |
| 2084 | } |
| 2085 | |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 2086 | /** |
| 2087 | * blk_throtl_drain - drain throttled bios |
| 2088 | * @q: request_queue to drain throttled bios for |
| 2089 | * |
| 2090 | * Dispatch all currently throttled bios on @q through ->make_request_fn(). |
| 2091 | */ |
| 2092 | void blk_throtl_drain(struct request_queue *q) |
| 2093 | __releases(q->queue_lock) __acquires(q->queue_lock) |
| 2094 | { |
| 2095 | struct throtl_data *td = q->td; |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2096 | struct blkcg_gq *blkg; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2097 | struct cgroup_subsys_state *pos_css; |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 2098 | struct bio *bio; |
Tejun Heo | 651930b | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 2099 | int rw; |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 2100 | |
Andi Kleen | 8bcb6c7 | 2012-03-30 12:33:28 +0200 | [diff] [blame] | 2101 | queue_lockdep_assert_held(q); |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2102 | rcu_read_lock(); |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 2103 | |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2104 | /* |
| 2105 | * Drain each tg while doing post-order walk on the blkg tree, so |
| 2106 | * that all bios are propagated to td->service_queue. It'd be |
| 2107 | * better to walk service_queue tree directly but blkg walk is |
| 2108 | * easier. |
| 2109 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2110 | blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2111 | tg_drain_bios(&blkg_to_tg(blkg)->service_queue); |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 2112 | |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2113 | /* finally, transfer bios from top-level tg's into the td */ |
| 2114 | tg_drain_bios(&td->service_queue); |
| 2115 | |
| 2116 | rcu_read_unlock(); |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 2117 | spin_unlock_irq(q->queue_lock); |
| 2118 | |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2119 | /* all bios now should be in td->service_queue, issue them */ |
Tejun Heo | 651930b | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 2120 | for (rw = READ; rw <= WRITE; rw++) |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 2121 | while ((bio = throtl_pop_queued(&td->service_queue.queued[rw], |
| 2122 | NULL))) |
Tejun Heo | 651930b | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 2123 | generic_make_request(bio); |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 2124 | |
| 2125 | spin_lock_irq(q->queue_lock); |
| 2126 | } |
| 2127 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2128 | int blk_throtl_init(struct request_queue *q) |
| 2129 | { |
| 2130 | struct throtl_data *td; |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 2131 | int ret; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2132 | |
| 2133 | td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node); |
| 2134 | if (!td) |
| 2135 | return -ENOMEM; |
| 2136 | |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 2137 | INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn); |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 2138 | throtl_service_queue_init(&td->service_queue); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2139 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 2140 | q->td = td; |
Vivek Goyal | 29b1258 | 2011-05-19 15:38:24 -0400 | [diff] [blame] | 2141 | td->queue = q; |
Vivek Goyal | 02977e4 | 2010-10-01 14:49:48 +0200 | [diff] [blame] | 2142 | |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 2143 | td->limit_valid[LIMIT_MAX] = true; |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 2144 | td->limit_index = LIMIT_MAX; |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 2145 | td->low_upgrade_time = jiffies; |
| 2146 | td->low_downgrade_time = jiffies; |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 2147 | |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 2148 | /* activate policy */ |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 2149 | ret = blkcg_activate_policy(q, &blkcg_policy_throtl); |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 2150 | if (ret) |
Vivek Goyal | 29b1258 | 2011-05-19 15:38:24 -0400 | [diff] [blame] | 2151 | kfree(td); |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 2152 | return ret; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2153 | } |
| 2154 | |
| 2155 | void blk_throtl_exit(struct request_queue *q) |
| 2156 | { |
Tejun Heo | c875f4d | 2012-03-05 13:15:22 -0800 | [diff] [blame] | 2157 | BUG_ON(!q->td); |
Vivek Goyal | da52777 | 2011-03-02 19:05:33 -0500 | [diff] [blame] | 2158 | throtl_shutdown_wq(q); |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 2159 | blkcg_deactivate_policy(q, &blkcg_policy_throtl); |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 2160 | kfree(q->td); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2161 | } |
| 2162 | |
Shaohua Li | d61fcfa | 2017-03-27 10:51:38 -0700 | [diff] [blame] | 2163 | void blk_throtl_register_queue(struct request_queue *q) |
| 2164 | { |
| 2165 | struct throtl_data *td; |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 2166 | struct cgroup_subsys_state *pos_css; |
| 2167 | struct blkcg_gq *blkg; |
Shaohua Li | d61fcfa | 2017-03-27 10:51:38 -0700 | [diff] [blame] | 2168 | |
| 2169 | td = q->td; |
| 2170 | BUG_ON(!td); |
| 2171 | |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame] | 2172 | if (blk_queue_nonrot(q)) { |
Shaohua Li | d61fcfa | 2017-03-27 10:51:38 -0700 | [diff] [blame] | 2173 | td->throtl_slice = DFL_THROTL_SLICE_SSD; |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame] | 2174 | td->dft_idletime_threshold = DFL_IDLE_THRESHOLD_SSD; |
| 2175 | } else { |
Shaohua Li | d61fcfa | 2017-03-27 10:51:38 -0700 | [diff] [blame] | 2176 | td->throtl_slice = DFL_THROTL_SLICE_HD; |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame] | 2177 | td->dft_idletime_threshold = DFL_IDLE_THRESHOLD_HD; |
| 2178 | } |
Shaohua Li | d61fcfa | 2017-03-27 10:51:38 -0700 | [diff] [blame] | 2179 | #ifndef CONFIG_BLK_DEV_THROTTLING_LOW |
| 2180 | /* if no low limit, use previous default */ |
| 2181 | td->throtl_slice = DFL_THROTL_SLICE_HD; |
| 2182 | #endif |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 2183 | |
| 2184 | /* |
| 2185 | * some tg are created before queue is fully initialized, eg, nonrot |
| 2186 | * isn't initialized yet |
| 2187 | */ |
| 2188 | rcu_read_lock(); |
| 2189 | blkg_for_each_descendant_post(blkg, pos_css, q->root_blkg) { |
| 2190 | struct throtl_grp *tg = blkg_to_tg(blkg); |
| 2191 | |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame] | 2192 | tg->idletime_threshold = td->dft_idletime_threshold; |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 2193 | } |
| 2194 | rcu_read_unlock(); |
Shaohua Li | d61fcfa | 2017-03-27 10:51:38 -0700 | [diff] [blame] | 2195 | } |
| 2196 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 2197 | #ifdef CONFIG_BLK_DEV_THROTTLING_LOW |
| 2198 | ssize_t blk_throtl_sample_time_show(struct request_queue *q, char *page) |
| 2199 | { |
| 2200 | if (!q->td) |
| 2201 | return -EINVAL; |
| 2202 | return sprintf(page, "%u\n", jiffies_to_msecs(q->td->throtl_slice)); |
| 2203 | } |
| 2204 | |
| 2205 | ssize_t blk_throtl_sample_time_store(struct request_queue *q, |
| 2206 | const char *page, size_t count) |
| 2207 | { |
| 2208 | unsigned long v; |
| 2209 | unsigned long t; |
| 2210 | |
| 2211 | if (!q->td) |
| 2212 | return -EINVAL; |
| 2213 | if (kstrtoul(page, 10, &v)) |
| 2214 | return -EINVAL; |
| 2215 | t = msecs_to_jiffies(v); |
| 2216 | if (t == 0 || t > MAX_THROTL_SLICE) |
| 2217 | return -EINVAL; |
| 2218 | q->td->throtl_slice = t; |
| 2219 | return count; |
| 2220 | } |
| 2221 | #endif |
| 2222 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2223 | static int __init throtl_init(void) |
| 2224 | { |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 2225 | kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0); |
| 2226 | if (!kthrotld_workqueue) |
| 2227 | panic("Failed to create kthrotld\n"); |
| 2228 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 2229 | return blkcg_policy_register(&blkcg_policy_throtl); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | module_init(throtl_init); |