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 | |
| 21 | /* Throttling is performed over 100ms slice and after that slice is renewed */ |
| 22 | static unsigned long throtl_slice = HZ/10; /* 100 ms */ |
| 23 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 24 | static struct blkcg_policy blkcg_policy_throtl; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 25 | |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 26 | /* A workqueue to queue throttle related work */ |
| 27 | static struct workqueue_struct *kthrotld_workqueue; |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 28 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 29 | /* |
| 30 | * To implement hierarchical throttling, throtl_grps form a tree and bios |
| 31 | * are dispatched upwards level by level until they reach the top and get |
| 32 | * issued. When dispatching bios from the children and local group at each |
| 33 | * level, if the bios are dispatched into a single bio_list, there's a risk |
| 34 | * of a local or child group which can queue many bios at once filling up |
| 35 | * the list starving others. |
| 36 | * |
| 37 | * To avoid such starvation, dispatched bios are queued separately |
| 38 | * according to where they came from. When they are again dispatched to |
| 39 | * the parent, they're popped in round-robin order so that no single source |
| 40 | * hogs the dispatch window. |
| 41 | * |
| 42 | * throtl_qnode is used to keep the queued bios separated by their sources. |
| 43 | * Bios are queued to throtl_qnode which in turn is queued to |
| 44 | * throtl_service_queue and then dispatched in round-robin order. |
| 45 | * |
| 46 | * It's also used to track the reference counts on blkg's. A qnode always |
| 47 | * belongs to a throtl_grp and gets queued on itself or the parent, so |
| 48 | * incrementing the reference of the associated throtl_grp when a qnode is |
| 49 | * queued and decrementing when dequeued is enough to keep the whole blkg |
| 50 | * tree pinned while bios are in flight. |
| 51 | */ |
| 52 | struct throtl_qnode { |
| 53 | struct list_head node; /* service_queue->queued[] */ |
| 54 | struct bio_list bios; /* queued bios */ |
| 55 | struct throtl_grp *tg; /* tg this qnode belongs to */ |
| 56 | }; |
| 57 | |
Tejun Heo | c9e0332 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 58 | struct throtl_service_queue { |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 59 | struct throtl_service_queue *parent_sq; /* the parent service_queue */ |
| 60 | |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 61 | /* |
| 62 | * Bios queued directly to this service_queue or dispatched from |
| 63 | * children throtl_grp's. |
| 64 | */ |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 65 | struct list_head queued[2]; /* throtl_qnode [READ/WRITE] */ |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 66 | unsigned int nr_queued[2]; /* number of queued bios */ |
| 67 | |
| 68 | /* |
| 69 | * RB tree of active children throtl_grp's, which are sorted by |
| 70 | * their ->disptime. |
| 71 | */ |
Tejun Heo | c9e0332 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 72 | struct rb_root pending_tree; /* RB tree of active tgs */ |
| 73 | struct rb_node *first_pending; /* first node in the tree */ |
| 74 | unsigned int nr_pending; /* # queued in the tree */ |
| 75 | unsigned long first_pending_disptime; /* disptime of the first tg */ |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 76 | struct timer_list pending_timer; /* fires on first_pending_disptime */ |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 77 | }; |
| 78 | |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 79 | enum tg_state_flags { |
| 80 | THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */ |
Tejun Heo | 0e9f416 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 81 | THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */ |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 84 | #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node) |
| 85 | |
Tejun Heo | 8a3d261 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 86 | /* Per-cpu group stats */ |
| 87 | struct tg_stats_cpu { |
| 88 | /* total bytes transferred */ |
| 89 | struct blkg_rwstat service_bytes; |
| 90 | /* total IOs serviced, post merge */ |
| 91 | struct blkg_rwstat serviced; |
| 92 | }; |
| 93 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 94 | struct throtl_grp { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 95 | /* must be the first member */ |
| 96 | struct blkg_policy_data pd; |
| 97 | |
Tejun Heo | c9e0332 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 98 | /* active throtl group service_queue member */ |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 99 | struct rb_node rb_node; |
| 100 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 101 | /* throtl_data this group belongs to */ |
| 102 | struct throtl_data *td; |
| 103 | |
Tejun Heo | 49a2f1e | 2013-05-14 13:52:34 -0700 | [diff] [blame] | 104 | /* this group's service queue */ |
| 105 | struct throtl_service_queue service_queue; |
| 106 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 107 | /* |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 108 | * qnode_on_self is used when bios are directly queued to this |
| 109 | * throtl_grp so that local bios compete fairly with bios |
| 110 | * dispatched from children. qnode_on_parent is used when bios are |
| 111 | * dispatched from this throtl_grp into its parent and will compete |
| 112 | * with the sibling qnode_on_parents and the parent's |
| 113 | * qnode_on_self. |
| 114 | */ |
| 115 | struct throtl_qnode qnode_on_self[2]; |
| 116 | struct throtl_qnode qnode_on_parent[2]; |
| 117 | |
| 118 | /* |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 119 | * Dispatch time in jiffies. This is the estimated time when group |
| 120 | * will unthrottle and is ready to dispatch more bio. It is used as |
| 121 | * key to sort active groups in service tree. |
| 122 | */ |
| 123 | unsigned long disptime; |
| 124 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 125 | unsigned int flags; |
| 126 | |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 127 | /* are there any throtl rules between this group and td? */ |
| 128 | bool has_rules[2]; |
| 129 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 130 | /* bytes per second rate limits */ |
| 131 | uint64_t bps[2]; |
| 132 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 133 | /* IOPS limits */ |
| 134 | unsigned int iops[2]; |
| 135 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 136 | /* Number of bytes disptached in current slice */ |
| 137 | uint64_t bytes_disp[2]; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 138 | /* Number of bio's dispatched in current slice */ |
| 139 | unsigned int io_disp[2]; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 140 | |
| 141 | /* When did we start a new slice */ |
| 142 | unsigned long slice_start[2]; |
| 143 | unsigned long slice_end[2]; |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 144 | |
Tejun Heo | 8a3d261 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 145 | /* Per cpu stats pointer */ |
| 146 | struct tg_stats_cpu __percpu *stats_cpu; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | struct throtl_data |
| 150 | { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 151 | /* service tree for active throtl groups */ |
Tejun Heo | c9e0332 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 152 | struct throtl_service_queue service_queue; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 153 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 154 | struct request_queue *queue; |
| 155 | |
| 156 | /* Total Number of queued bios on READ and WRITE lists */ |
| 157 | unsigned int nr_queued[2]; |
| 158 | |
| 159 | /* |
Vivek Goyal | 02977e4 | 2010-10-01 14:49:48 +0200 | [diff] [blame] | 160 | * number of total undestroyed groups |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 161 | */ |
| 162 | unsigned int nr_undestroyed_grps; |
| 163 | |
| 164 | /* Work for dispatching throttled bios */ |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 165 | struct work_struct dispatch_work; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 166 | }; |
| 167 | |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 168 | static void throtl_pending_timer_fn(unsigned long arg); |
| 169 | |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 170 | static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd) |
| 171 | { |
| 172 | return pd ? container_of(pd, struct throtl_grp, pd) : NULL; |
| 173 | } |
| 174 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 175 | static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 176 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 177 | return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl)); |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 180 | static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 181 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 182 | return pd_to_blkg(&tg->pd); |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Tejun Heo | 03d8e11 | 2012-04-13 13:11:32 -0700 | [diff] [blame] | 185 | static inline struct throtl_grp *td_root_tg(struct throtl_data *td) |
| 186 | { |
| 187 | return blkg_to_tg(td->queue->root_blkg); |
| 188 | } |
| 189 | |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 190 | /** |
| 191 | * sq_to_tg - return the throl_grp the specified service queue belongs to |
| 192 | * @sq: the throtl_service_queue of interest |
| 193 | * |
| 194 | * Return the throtl_grp @sq belongs to. If @sq is the top-level one |
| 195 | * embedded in throtl_data, %NULL is returned. |
| 196 | */ |
| 197 | static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq) |
| 198 | { |
| 199 | if (sq && sq->parent_sq) |
| 200 | return container_of(sq, struct throtl_grp, service_queue); |
| 201 | else |
| 202 | return NULL; |
| 203 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 204 | |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 205 | /** |
| 206 | * sq_to_td - return throtl_data the specified service queue belongs to |
| 207 | * @sq: the throtl_service_queue of interest |
| 208 | * |
| 209 | * A service_queue can be embeded in either a throtl_grp or throtl_data. |
| 210 | * Determine the associated throtl_data accordingly and return it. |
| 211 | */ |
| 212 | static struct throtl_data *sq_to_td(struct throtl_service_queue *sq) |
| 213 | { |
| 214 | struct throtl_grp *tg = sq_to_tg(sq); |
| 215 | |
| 216 | if (tg) |
| 217 | return tg->td; |
| 218 | else |
| 219 | return container_of(sq, struct throtl_data, service_queue); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * throtl_log - log debug message via blktrace |
| 224 | * @sq: the service_queue being reported |
| 225 | * @fmt: printf format string |
| 226 | * @args: printf args |
| 227 | * |
| 228 | * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a |
| 229 | * throtl_grp; otherwise, just "throtl". |
| 230 | * |
| 231 | * TODO: this should be made a function and name formatting should happen |
| 232 | * after testing whether blktrace is enabled. |
| 233 | */ |
| 234 | #define throtl_log(sq, fmt, args...) do { \ |
| 235 | struct throtl_grp *__tg = sq_to_tg((sq)); \ |
| 236 | struct throtl_data *__td = sq_to_td((sq)); \ |
| 237 | \ |
| 238 | (void)__td; \ |
| 239 | if ((__tg)) { \ |
| 240 | char __pbuf[128]; \ |
| 241 | \ |
| 242 | blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \ |
| 243 | blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \ |
| 244 | } else { \ |
| 245 | blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \ |
| 246 | } \ |
| 247 | } while (0) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 248 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 249 | static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg) |
| 250 | { |
| 251 | INIT_LIST_HEAD(&qn->node); |
| 252 | bio_list_init(&qn->bios); |
| 253 | qn->tg = tg; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it |
| 258 | * @bio: bio being added |
| 259 | * @qn: qnode to add bio to |
| 260 | * @queued: the service_queue->queued[] list @qn belongs to |
| 261 | * |
| 262 | * Add @bio to @qn and put @qn on @queued if it's not already on. |
| 263 | * @qn->tg's reference count is bumped when @qn is activated. See the |
| 264 | * comment on top of throtl_qnode definition for details. |
| 265 | */ |
| 266 | static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn, |
| 267 | struct list_head *queued) |
| 268 | { |
| 269 | bio_list_add(&qn->bios, bio); |
| 270 | if (list_empty(&qn->node)) { |
| 271 | list_add_tail(&qn->node, queued); |
| 272 | blkg_get(tg_to_blkg(qn->tg)); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * throtl_peek_queued - peek the first bio on a qnode list |
| 278 | * @queued: the qnode list to peek |
| 279 | */ |
| 280 | static struct bio *throtl_peek_queued(struct list_head *queued) |
| 281 | { |
| 282 | struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node); |
| 283 | struct bio *bio; |
| 284 | |
| 285 | if (list_empty(queued)) |
| 286 | return NULL; |
| 287 | |
| 288 | bio = bio_list_peek(&qn->bios); |
| 289 | WARN_ON_ONCE(!bio); |
| 290 | return bio; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * throtl_pop_queued - pop the first bio form a qnode list |
| 295 | * @queued: the qnode list to pop a bio from |
| 296 | * @tg_to_put: optional out argument for throtl_grp to put |
| 297 | * |
| 298 | * Pop the first bio from the qnode list @queued. After popping, the first |
| 299 | * qnode is removed from @queued if empty or moved to the end of @queued so |
| 300 | * that the popping order is round-robin. |
| 301 | * |
| 302 | * When the first qnode is removed, its associated throtl_grp should be put |
| 303 | * too. If @tg_to_put is NULL, this function automatically puts it; |
| 304 | * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is |
| 305 | * responsible for putting it. |
| 306 | */ |
| 307 | static struct bio *throtl_pop_queued(struct list_head *queued, |
| 308 | struct throtl_grp **tg_to_put) |
| 309 | { |
| 310 | struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node); |
| 311 | struct bio *bio; |
| 312 | |
| 313 | if (list_empty(queued)) |
| 314 | return NULL; |
| 315 | |
| 316 | bio = bio_list_pop(&qn->bios); |
| 317 | WARN_ON_ONCE(!bio); |
| 318 | |
| 319 | if (bio_list_empty(&qn->bios)) { |
| 320 | list_del_init(&qn->node); |
| 321 | if (tg_to_put) |
| 322 | *tg_to_put = qn->tg; |
| 323 | else |
| 324 | blkg_put(tg_to_blkg(qn->tg)); |
| 325 | } else { |
| 326 | list_move_tail(&qn->node, queued); |
| 327 | } |
| 328 | |
| 329 | return bio; |
| 330 | } |
| 331 | |
Tejun Heo | 49a2f1e | 2013-05-14 13:52:34 -0700 | [diff] [blame] | 332 | /* init a service_queue, assumes the caller zeroed it */ |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 333 | static void throtl_service_queue_init(struct throtl_service_queue *sq) |
Tejun Heo | 49a2f1e | 2013-05-14 13:52:34 -0700 | [diff] [blame] | 334 | { |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 335 | INIT_LIST_HEAD(&sq->queued[0]); |
| 336 | INIT_LIST_HEAD(&sq->queued[1]); |
Tejun Heo | 49a2f1e | 2013-05-14 13:52:34 -0700 | [diff] [blame] | 337 | sq->pending_tree = RB_ROOT; |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 338 | setup_timer(&sq->pending_timer, throtl_pending_timer_fn, |
| 339 | (unsigned long)sq); |
| 340 | } |
| 341 | |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 342 | static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node) |
| 343 | { |
Tejun Heo | 4fb7203 | 2015-08-18 14:55:12 -0700 | [diff] [blame] | 344 | struct throtl_grp *tg; |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 345 | int rw, cpu; |
Tejun Heo | 4fb7203 | 2015-08-18 14:55:12 -0700 | [diff] [blame] | 346 | |
| 347 | tg = kzalloc_node(sizeof(*tg), gfp, node); |
| 348 | if (!tg) |
| 349 | return NULL; |
| 350 | |
| 351 | tg->stats_cpu = alloc_percpu_gfp(struct tg_stats_cpu, gfp); |
| 352 | if (!tg->stats_cpu) { |
| 353 | kfree(tg); |
| 354 | return NULL; |
| 355 | } |
| 356 | |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 357 | throtl_service_queue_init(&tg->service_queue); |
| 358 | |
| 359 | for (rw = READ; rw <= WRITE; rw++) { |
| 360 | throtl_qnode_init(&tg->qnode_on_self[rw], tg); |
| 361 | throtl_qnode_init(&tg->qnode_on_parent[rw], tg); |
| 362 | } |
| 363 | |
| 364 | RB_CLEAR_NODE(&tg->rb_node); |
| 365 | tg->bps[READ] = -1; |
| 366 | tg->bps[WRITE] = -1; |
| 367 | tg->iops[READ] = -1; |
| 368 | tg->iops[WRITE] = -1; |
| 369 | |
Tejun Heo | 4fb7203 | 2015-08-18 14:55:12 -0700 | [diff] [blame] | 370 | for_each_possible_cpu(cpu) { |
| 371 | struct tg_stats_cpu *stats_cpu = per_cpu_ptr(tg->stats_cpu, cpu); |
| 372 | |
| 373 | blkg_rwstat_init(&stats_cpu->service_bytes); |
| 374 | blkg_rwstat_init(&stats_cpu->serviced); |
| 375 | } |
| 376 | |
| 377 | return &tg->pd; |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame^] | 380 | static void throtl_pd_init(struct blkg_policy_data *pd) |
Vivek Goyal | a29a171 | 2011-05-19 15:38:19 -0400 | [diff] [blame] | 381 | { |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame^] | 382 | struct throtl_grp *tg = pd_to_tg(pd); |
| 383 | struct blkcg_gq *blkg = tg_to_blkg(tg); |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 384 | struct throtl_data *td = blkg->q->td; |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 385 | struct throtl_service_queue *sq = &tg->service_queue; |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 386 | |
Tejun Heo | 9138125 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 387 | /* |
Tejun Heo | aa6ec29 | 2014-07-09 10:08:08 -0400 | [diff] [blame] | 388 | * If on the default hierarchy, we switch to properly hierarchical |
Tejun Heo | 9138125 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 389 | * behavior where limits on a given throtl_grp are applied to the |
| 390 | * whole subtree rather than just the group itself. e.g. If 16M |
| 391 | * read_bps limit is set on the root group, the whole system can't |
| 392 | * exceed 16M for the device. |
| 393 | * |
Tejun Heo | aa6ec29 | 2014-07-09 10:08:08 -0400 | [diff] [blame] | 394 | * If not on the default hierarchy, the broken flat hierarchy |
Tejun Heo | 9138125 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 395 | * behavior is retained where all throtl_grps are treated as if |
| 396 | * they're all separate root groups right below throtl_data. |
| 397 | * Limits of a group don't interact with limits of other groups |
| 398 | * regardless of the position of the group in the hierarchy. |
| 399 | */ |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 400 | sq->parent_sq = &td->service_queue; |
Tejun Heo | aa6ec29 | 2014-07-09 10:08:08 -0400 | [diff] [blame] | 401 | if (cgroup_on_dfl(blkg->blkcg->css.cgroup) && blkg->parent) |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 402 | sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue; |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 403 | tg->td = td; |
Tejun Heo | 8a3d261 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 406 | /* |
| 407 | * Set has_rules[] if @tg or any of its parents have limits configured. |
| 408 | * This doesn't require walking up to the top of the hierarchy as the |
| 409 | * parent's has_rules[] is guaranteed to be correct. |
| 410 | */ |
| 411 | static void tg_update_has_rules(struct throtl_grp *tg) |
| 412 | { |
| 413 | struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq); |
| 414 | int rw; |
| 415 | |
| 416 | for (rw = READ; rw <= WRITE; rw++) |
| 417 | tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) || |
| 418 | (tg->bps[rw] != -1 || tg->iops[rw] != -1); |
| 419 | } |
| 420 | |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame^] | 421 | static void throtl_pd_online(struct blkg_policy_data *pd) |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 422 | { |
| 423 | /* |
| 424 | * We don't want new groups to escape the limits of its ancestors. |
| 425 | * Update has_rules[] after a new group is brought online. |
| 426 | */ |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame^] | 427 | tg_update_has_rules(pd_to_tg(pd)); |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 430 | static void throtl_pd_free(struct blkg_policy_data *pd) |
| 431 | { |
Tejun Heo | 4fb7203 | 2015-08-18 14:55:12 -0700 | [diff] [blame] | 432 | struct throtl_grp *tg = pd_to_tg(pd); |
| 433 | |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 434 | del_timer_sync(&tg->service_queue.pending_timer); |
Tejun Heo | 4fb7203 | 2015-08-18 14:55:12 -0700 | [diff] [blame] | 435 | free_percpu(tg->stats_cpu); |
| 436 | kfree(tg); |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame^] | 439 | static void throtl_pd_reset_stats(struct blkg_policy_data *pd) |
Tejun Heo | 8a3d261 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 440 | { |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame^] | 441 | struct throtl_grp *tg = pd_to_tg(pd); |
Tejun Heo | 8a3d261 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 442 | int cpu; |
| 443 | |
Tejun Heo | 8a3d261 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 444 | for_each_possible_cpu(cpu) { |
| 445 | struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu); |
| 446 | |
| 447 | blkg_rwstat_reset(&sc->service_bytes); |
| 448 | blkg_rwstat_reset(&sc->serviced); |
| 449 | } |
Vivek Goyal | a29a171 | 2011-05-19 15:38:19 -0400 | [diff] [blame] | 450 | } |
| 451 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 452 | static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td, |
| 453 | struct blkcg *blkcg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 454 | { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 455 | /* |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 456 | * This is the common case when there are no blkcgs. Avoid lookup |
| 457 | * in this case |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 458 | */ |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 459 | if (blkcg == &blkcg_root) |
Tejun Heo | 03d8e11 | 2012-04-13 13:11:32 -0700 | [diff] [blame] | 460 | return td_root_tg(td); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 461 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 462 | return blkg_to_tg(blkg_lookup(blkcg, td->queue)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 463 | } |
| 464 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 465 | static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td, |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 466 | struct blkcg *blkcg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 467 | { |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 468 | struct request_queue *q = td->queue; |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 469 | struct throtl_grp *tg = NULL; |
Tejun Heo | 0a5a7d0 | 2012-03-05 13:15:02 -0800 | [diff] [blame] | 470 | |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 471 | /* |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 472 | * This is the common case when there are no blkcgs. Avoid lookup |
| 473 | * in this case |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 474 | */ |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 475 | if (blkcg == &blkcg_root) { |
Tejun Heo | 03d8e11 | 2012-04-13 13:11:32 -0700 | [diff] [blame] | 476 | tg = td_root_tg(td); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 477 | } else { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 478 | struct blkcg_gq *blkg; |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 479 | |
Tejun Heo | 3c96cb3 | 2012-04-13 13:11:34 -0700 | [diff] [blame] | 480 | blkg = blkg_lookup_create(blkcg, q); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 481 | |
| 482 | /* if %NULL and @q is alive, fall back to root_tg */ |
| 483 | if (!IS_ERR(blkg)) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 484 | tg = blkg_to_tg(blkg); |
Bart Van Assche | 3f3299d | 2012-11-28 13:42:38 +0100 | [diff] [blame] | 485 | else if (!blk_queue_dying(q)) |
Tejun Heo | 03d8e11 | 2012-04-13 13:11:32 -0700 | [diff] [blame] | 486 | tg = td_root_tg(td); |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 487 | } |
| 488 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 489 | return tg; |
| 490 | } |
| 491 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 492 | static struct throtl_grp * |
| 493 | throtl_rb_first(struct throtl_service_queue *parent_sq) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 494 | { |
| 495 | /* Service tree is empty */ |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 496 | if (!parent_sq->nr_pending) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 497 | return NULL; |
| 498 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 499 | if (!parent_sq->first_pending) |
| 500 | parent_sq->first_pending = rb_first(&parent_sq->pending_tree); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 501 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 502 | if (parent_sq->first_pending) |
| 503 | return rb_entry_tg(parent_sq->first_pending); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 504 | |
| 505 | return NULL; |
| 506 | } |
| 507 | |
| 508 | static void rb_erase_init(struct rb_node *n, struct rb_root *root) |
| 509 | { |
| 510 | rb_erase(n, root); |
| 511 | RB_CLEAR_NODE(n); |
| 512 | } |
| 513 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 514 | static void throtl_rb_erase(struct rb_node *n, |
| 515 | struct throtl_service_queue *parent_sq) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 516 | { |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 517 | if (parent_sq->first_pending == n) |
| 518 | parent_sq->first_pending = NULL; |
| 519 | rb_erase_init(n, &parent_sq->pending_tree); |
| 520 | --parent_sq->nr_pending; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 521 | } |
| 522 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 523 | static void update_min_dispatch_time(struct throtl_service_queue *parent_sq) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 524 | { |
| 525 | struct throtl_grp *tg; |
| 526 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 527 | tg = throtl_rb_first(parent_sq); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 528 | if (!tg) |
| 529 | return; |
| 530 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 531 | parent_sq->first_pending_disptime = tg->disptime; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 532 | } |
| 533 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 534 | static void tg_service_queue_add(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 535 | { |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 536 | struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq; |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 537 | struct rb_node **node = &parent_sq->pending_tree.rb_node; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 538 | struct rb_node *parent = NULL; |
| 539 | struct throtl_grp *__tg; |
| 540 | unsigned long key = tg->disptime; |
| 541 | int left = 1; |
| 542 | |
| 543 | while (*node != NULL) { |
| 544 | parent = *node; |
| 545 | __tg = rb_entry_tg(parent); |
| 546 | |
| 547 | if (time_before(key, __tg->disptime)) |
| 548 | node = &parent->rb_left; |
| 549 | else { |
| 550 | node = &parent->rb_right; |
| 551 | left = 0; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | if (left) |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 556 | parent_sq->first_pending = &tg->rb_node; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 557 | |
| 558 | rb_link_node(&tg->rb_node, parent, node); |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 559 | rb_insert_color(&tg->rb_node, &parent_sq->pending_tree); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 560 | } |
| 561 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 562 | static void __throtl_enqueue_tg(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 563 | { |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 564 | tg_service_queue_add(tg); |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 565 | tg->flags |= THROTL_TG_PENDING; |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 566 | tg->service_queue.parent_sq->nr_pending++; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 567 | } |
| 568 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 569 | static void throtl_enqueue_tg(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 570 | { |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 571 | if (!(tg->flags & THROTL_TG_PENDING)) |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 572 | __throtl_enqueue_tg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 573 | } |
| 574 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 575 | static void __throtl_dequeue_tg(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 576 | { |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 577 | throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq); |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 578 | tg->flags &= ~THROTL_TG_PENDING; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 579 | } |
| 580 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 581 | static void throtl_dequeue_tg(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 582 | { |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 583 | if (tg->flags & THROTL_TG_PENDING) |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 584 | __throtl_dequeue_tg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 585 | } |
| 586 | |
Tejun Heo | a9131a2 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 587 | /* Call with queue lock held */ |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 588 | static void throtl_schedule_pending_timer(struct throtl_service_queue *sq, |
| 589 | unsigned long expires) |
Tejun Heo | a9131a2 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 590 | { |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 591 | mod_timer(&sq->pending_timer, expires); |
| 592 | throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu", |
| 593 | expires - jiffies, jiffies); |
Tejun Heo | a9131a2 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 596 | /** |
| 597 | * throtl_schedule_next_dispatch - schedule the next dispatch cycle |
| 598 | * @sq: the service_queue to schedule dispatch for |
| 599 | * @force: force scheduling |
| 600 | * |
| 601 | * Arm @sq->pending_timer so that the next dispatch cycle starts on the |
| 602 | * dispatch time of the first pending child. Returns %true if either timer |
| 603 | * is armed or there's no pending child left. %false if the current |
| 604 | * dispatch window is still open and the caller should continue |
| 605 | * dispatching. |
| 606 | * |
| 607 | * If @force is %true, the dispatch timer is always scheduled and this |
| 608 | * function is guaranteed to return %true. This is to be used when the |
| 609 | * caller can't dispatch itself and needs to invoke pending_timer |
| 610 | * unconditionally. Note that forced scheduling is likely to induce short |
| 611 | * delay before dispatch starts even if @sq->first_pending_disptime is not |
| 612 | * in the future and thus shouldn't be used in hot paths. |
| 613 | */ |
| 614 | static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq, |
| 615 | bool force) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 616 | { |
Tejun Heo | 6a52560 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 617 | /* any pending children left? */ |
Tejun Heo | c9e0332 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 618 | if (!sq->nr_pending) |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 619 | return true; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 620 | |
Tejun Heo | c9e0332 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 621 | update_min_dispatch_time(sq); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 622 | |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 623 | /* is the next dispatch time in the future? */ |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 624 | if (force || time_after(sq->first_pending_disptime, jiffies)) { |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 625 | throtl_schedule_pending_timer(sq, sq->first_pending_disptime); |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 626 | return true; |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 629 | /* tell the caller to continue dispatching */ |
| 630 | return false; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 631 | } |
| 632 | |
Vivek Goyal | 32ee5bc | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 633 | static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg, |
| 634 | bool rw, unsigned long start) |
| 635 | { |
| 636 | tg->bytes_disp[rw] = 0; |
| 637 | tg->io_disp[rw] = 0; |
| 638 | |
| 639 | /* |
| 640 | * Previous slice has expired. We must have trimmed it after last |
| 641 | * bio dispatch. That means since start of last slice, we never used |
| 642 | * that bandwidth. Do try to make use of that bandwidth while giving |
| 643 | * credit. |
| 644 | */ |
| 645 | if (time_after_eq(start, tg->slice_start[rw])) |
| 646 | tg->slice_start[rw] = start; |
| 647 | |
| 648 | tg->slice_end[rw] = jiffies + throtl_slice; |
| 649 | throtl_log(&tg->service_queue, |
| 650 | "[%c] new slice with credit start=%lu end=%lu jiffies=%lu", |
| 651 | rw == READ ? 'R' : 'W', tg->slice_start[rw], |
| 652 | tg->slice_end[rw], jiffies); |
| 653 | } |
| 654 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 655 | 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] | 656 | { |
| 657 | tg->bytes_disp[rw] = 0; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 658 | tg->io_disp[rw] = 0; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 659 | tg->slice_start[rw] = jiffies; |
| 660 | tg->slice_end[rw] = jiffies + throtl_slice; |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 661 | throtl_log(&tg->service_queue, |
| 662 | "[%c] new slice start=%lu end=%lu jiffies=%lu", |
| 663 | rw == READ ? 'R' : 'W', tg->slice_start[rw], |
| 664 | tg->slice_end[rw], jiffies); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 665 | } |
| 666 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 667 | static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw, |
| 668 | unsigned long jiffy_end) |
Vivek Goyal | d1ae8ff | 2010-12-01 19:34:46 +0100 | [diff] [blame] | 669 | { |
| 670 | tg->slice_end[rw] = roundup(jiffy_end, throtl_slice); |
| 671 | } |
| 672 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 673 | static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw, |
| 674 | unsigned long jiffy_end) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 675 | { |
| 676 | tg->slice_end[rw] = roundup(jiffy_end, throtl_slice); |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 677 | throtl_log(&tg->service_queue, |
| 678 | "[%c] extend slice start=%lu end=%lu jiffies=%lu", |
| 679 | rw == READ ? 'R' : 'W', tg->slice_start[rw], |
| 680 | tg->slice_end[rw], jiffies); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* Determine if previously allocated or extended slice is complete or not */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 684 | static bool throtl_slice_used(struct throtl_grp *tg, bool rw) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 685 | { |
| 686 | 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] | 687 | return false; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 688 | |
| 689 | return 1; |
| 690 | } |
| 691 | |
| 692 | /* Trim the used slices and adjust slice start accordingly */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 693 | static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 694 | { |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 695 | unsigned long nr_slices, time_elapsed, io_trim; |
| 696 | u64 bytes_trim, tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 697 | |
| 698 | BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw])); |
| 699 | |
| 700 | /* |
| 701 | * If bps are unlimited (-1), then time slice don't get |
| 702 | * renewed. Don't try to trim the slice if slice is used. A new |
| 703 | * slice will start when appropriate. |
| 704 | */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 705 | if (throtl_slice_used(tg, rw)) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 706 | return; |
| 707 | |
Vivek Goyal | d1ae8ff | 2010-12-01 19:34:46 +0100 | [diff] [blame] | 708 | /* |
| 709 | * A bio has been dispatched. Also adjust slice_end. It might happen |
| 710 | * that initially cgroup limit was very low resulting in high |
| 711 | * slice_end, but later limit was bumped up and bio was dispached |
| 712 | * sooner, then we need to reduce slice_end. A high bogus slice_end |
| 713 | * is bad because it does not allow new slice to start. |
| 714 | */ |
| 715 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 716 | throtl_set_slice_end(tg, rw, jiffies + throtl_slice); |
Vivek Goyal | d1ae8ff | 2010-12-01 19:34:46 +0100 | [diff] [blame] | 717 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 718 | time_elapsed = jiffies - tg->slice_start[rw]; |
| 719 | |
| 720 | nr_slices = time_elapsed / throtl_slice; |
| 721 | |
| 722 | if (!nr_slices) |
| 723 | return; |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 724 | tmp = tg->bps[rw] * throtl_slice * nr_slices; |
| 725 | do_div(tmp, HZ); |
| 726 | bytes_trim = tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 727 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 728 | io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 729 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 730 | if (!bytes_trim && !io_trim) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 731 | return; |
| 732 | |
| 733 | if (tg->bytes_disp[rw] >= bytes_trim) |
| 734 | tg->bytes_disp[rw] -= bytes_trim; |
| 735 | else |
| 736 | tg->bytes_disp[rw] = 0; |
| 737 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 738 | if (tg->io_disp[rw] >= io_trim) |
| 739 | tg->io_disp[rw] -= io_trim; |
| 740 | else |
| 741 | tg->io_disp[rw] = 0; |
| 742 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 743 | tg->slice_start[rw] += nr_slices * throtl_slice; |
| 744 | |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 745 | throtl_log(&tg->service_queue, |
| 746 | "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu", |
| 747 | rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim, |
| 748 | tg->slice_start[rw], tg->slice_end[rw], jiffies); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 749 | } |
| 750 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 751 | static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio, |
| 752 | unsigned long *wait) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 753 | { |
| 754 | bool rw = bio_data_dir(bio); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 755 | unsigned int io_allowed; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 756 | unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd; |
Vivek Goyal | c49c06e | 2010-10-01 21:16:42 +0200 | [diff] [blame] | 757 | u64 tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 758 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 759 | jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw]; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 760 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 761 | /* Slice has just started. Consider one slice interval */ |
| 762 | if (!jiffy_elapsed) |
| 763 | jiffy_elapsed_rnd = throtl_slice; |
| 764 | |
| 765 | jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice); |
| 766 | |
Vivek Goyal | c49c06e | 2010-10-01 21:16:42 +0200 | [diff] [blame] | 767 | /* |
| 768 | * jiffy_elapsed_rnd should not be a big value as minimum iops can be |
| 769 | * 1 then at max jiffy elapsed should be equivalent of 1 second as we |
| 770 | * will allow dispatch after 1 second and after that slice should |
| 771 | * have been trimmed. |
| 772 | */ |
| 773 | |
| 774 | tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd; |
| 775 | do_div(tmp, HZ); |
| 776 | |
| 777 | if (tmp > UINT_MAX) |
| 778 | io_allowed = UINT_MAX; |
| 779 | else |
| 780 | io_allowed = tmp; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 781 | |
| 782 | if (tg->io_disp[rw] + 1 <= io_allowed) { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 783 | if (wait) |
| 784 | *wait = 0; |
Fabian Frederick | 5cf8c22 | 2014-05-02 18:28:17 +0200 | [diff] [blame] | 785 | return true; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 786 | } |
| 787 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 788 | /* Calc approx time to dispatch */ |
| 789 | jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1; |
| 790 | |
| 791 | if (jiffy_wait > jiffy_elapsed) |
| 792 | jiffy_wait = jiffy_wait - jiffy_elapsed; |
| 793 | else |
| 794 | jiffy_wait = 1; |
| 795 | |
| 796 | if (wait) |
| 797 | *wait = jiffy_wait; |
| 798 | return 0; |
| 799 | } |
| 800 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 801 | static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio, |
| 802 | unsigned long *wait) |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 803 | { |
| 804 | bool rw = bio_data_dir(bio); |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 805 | u64 bytes_allowed, extra_bytes, tmp; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 806 | unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 807 | |
| 808 | jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw]; |
| 809 | |
| 810 | /* Slice has just started. Consider one slice interval */ |
| 811 | if (!jiffy_elapsed) |
| 812 | jiffy_elapsed_rnd = throtl_slice; |
| 813 | |
| 814 | jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice); |
| 815 | |
Vivek Goyal | 5e901a2 | 2010-10-01 21:16:38 +0200 | [diff] [blame] | 816 | tmp = tg->bps[rw] * jiffy_elapsed_rnd; |
| 817 | do_div(tmp, HZ); |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 818 | bytes_allowed = tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 819 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 820 | if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 821 | if (wait) |
| 822 | *wait = 0; |
Fabian Frederick | 5cf8c22 | 2014-05-02 18:28:17 +0200 | [diff] [blame] | 823 | return true; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | /* Calc approx time to dispatch */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 827 | extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 828 | jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]); |
| 829 | |
| 830 | if (!jiffy_wait) |
| 831 | jiffy_wait = 1; |
| 832 | |
| 833 | /* |
| 834 | * This wait time is without taking into consideration the rounding |
| 835 | * up we did. Add that time also. |
| 836 | */ |
| 837 | jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 838 | if (wait) |
| 839 | *wait = jiffy_wait; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 840 | return 0; |
| 841 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 842 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 843 | /* |
| 844 | * Returns whether one can dispatch a bio or not. Also returns approx number |
| 845 | * of jiffies to wait before this bio is with-in IO rate and can be dispatched |
| 846 | */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 847 | static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio, |
| 848 | unsigned long *wait) |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 849 | { |
| 850 | bool rw = bio_data_dir(bio); |
| 851 | unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0; |
| 852 | |
| 853 | /* |
| 854 | * Currently whole state machine of group depends on first bio |
| 855 | * queued in the group bio list. So one should not be calling |
| 856 | * this function with a different bio if there are other bios |
| 857 | * queued. |
| 858 | */ |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 859 | BUG_ON(tg->service_queue.nr_queued[rw] && |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 860 | bio != throtl_peek_queued(&tg->service_queue.queued[rw])); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 861 | |
| 862 | /* If tg->bps = -1, then BW is unlimited */ |
| 863 | if (tg->bps[rw] == -1 && tg->iops[rw] == -1) { |
| 864 | if (wait) |
| 865 | *wait = 0; |
Fabian Frederick | 5cf8c22 | 2014-05-02 18:28:17 +0200 | [diff] [blame] | 866 | return true; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | /* |
| 870 | * If previous slice expired, start a new one otherwise renew/extend |
| 871 | * existing slice to make sure it is at least throtl_slice interval |
| 872 | * long since now. |
| 873 | */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 874 | if (throtl_slice_used(tg, rw)) |
| 875 | throtl_start_new_slice(tg, rw); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 876 | else { |
| 877 | if (time_before(tg->slice_end[rw], jiffies + throtl_slice)) |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 878 | throtl_extend_slice(tg, rw, jiffies + throtl_slice); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 879 | } |
| 880 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 881 | if (tg_with_in_bps_limit(tg, bio, &bps_wait) && |
| 882 | tg_with_in_iops_limit(tg, bio, &iops_wait)) { |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 883 | if (wait) |
| 884 | *wait = 0; |
| 885 | return 1; |
| 886 | } |
| 887 | |
| 888 | max_wait = max(bps_wait, iops_wait); |
| 889 | |
| 890 | if (wait) |
| 891 | *wait = max_wait; |
| 892 | |
| 893 | if (time_before(tg->slice_end[rw], jiffies + max_wait)) |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 894 | throtl_extend_slice(tg, rw, jiffies + max_wait); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 895 | |
| 896 | return 0; |
| 897 | } |
| 898 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 899 | static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes, |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 900 | int rw) |
| 901 | { |
Tejun Heo | 8a3d261 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 902 | struct throtl_grp *tg = blkg_to_tg(blkg); |
| 903 | struct tg_stats_cpu *stats_cpu; |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 904 | unsigned long flags; |
| 905 | |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 906 | /* |
| 907 | * Disabling interrupts to provide mutual exclusion between two |
| 908 | * writes on same cpu. It probably is not needed for 64bit. Not |
| 909 | * optimizing that case yet. |
| 910 | */ |
| 911 | local_irq_save(flags); |
| 912 | |
Tejun Heo | 8a3d261 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 913 | stats_cpu = this_cpu_ptr(tg->stats_cpu); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 914 | |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 915 | blkg_rwstat_add(&stats_cpu->serviced, rw, 1); |
| 916 | blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes); |
| 917 | |
| 918 | local_irq_restore(flags); |
| 919 | } |
| 920 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 921 | static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio) |
| 922 | { |
| 923 | bool rw = bio_data_dir(bio); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 924 | |
| 925 | /* Charge the bio to the group */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 926 | tg->bytes_disp[rw] += bio->bi_iter.bi_size; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 927 | tg->io_disp[rw]++; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 928 | |
Tejun Heo | 2a0f61e | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 929 | /* |
| 930 | * REQ_THROTTLED is used to prevent the same bio to be throttled |
| 931 | * more than once as a throttled bio will go through blk-throtl the |
| 932 | * second time when it eventually gets issued. Set it when a bio |
| 933 | * is being charged to a tg. |
| 934 | * |
| 935 | * Dispatch stats aren't recursive and each @bio should only be |
| 936 | * accounted by the @tg it was originally associated with. Let's |
| 937 | * update the stats when setting REQ_THROTTLED for the first time |
| 938 | * which is guaranteed to be for the @bio's original tg. |
| 939 | */ |
| 940 | if (!(bio->bi_rw & REQ_THROTTLED)) { |
| 941 | bio->bi_rw |= REQ_THROTTLED; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 942 | throtl_update_dispatch_stats(tg_to_blkg(tg), |
| 943 | bio->bi_iter.bi_size, bio->bi_rw); |
Tejun Heo | 2a0f61e | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 944 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 945 | } |
| 946 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 947 | /** |
| 948 | * throtl_add_bio_tg - add a bio to the specified throtl_grp |
| 949 | * @bio: bio to add |
| 950 | * @qn: qnode to use |
| 951 | * @tg: the target throtl_grp |
| 952 | * |
| 953 | * Add @bio to @tg's service_queue using @qn. If @qn is not specified, |
| 954 | * tg->qnode_on_self[] is used. |
| 955 | */ |
| 956 | static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn, |
| 957 | struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 958 | { |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 959 | struct throtl_service_queue *sq = &tg->service_queue; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 960 | bool rw = bio_data_dir(bio); |
| 961 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 962 | if (!qn) |
| 963 | qn = &tg->qnode_on_self[rw]; |
| 964 | |
Tejun Heo | 0e9f416 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 965 | /* |
| 966 | * If @tg doesn't currently have any bios queued in the same |
| 967 | * direction, queueing @bio can change when @tg should be |
| 968 | * dispatched. Mark that @tg was empty. This is automatically |
| 969 | * cleaered on the next tg_update_disptime(). |
| 970 | */ |
| 971 | if (!sq->nr_queued[rw]) |
| 972 | tg->flags |= THROTL_TG_WAS_EMPTY; |
| 973 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 974 | throtl_qnode_add_bio(bio, qn, &sq->queued[rw]); |
| 975 | |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 976 | sq->nr_queued[rw]++; |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 977 | throtl_enqueue_tg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 978 | } |
| 979 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 980 | static void tg_update_disptime(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 981 | { |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 982 | struct throtl_service_queue *sq = &tg->service_queue; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 983 | unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime; |
| 984 | struct bio *bio; |
| 985 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 986 | if ((bio = throtl_peek_queued(&sq->queued[READ]))) |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 987 | tg_may_dispatch(tg, bio, &read_wait); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 988 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 989 | if ((bio = throtl_peek_queued(&sq->queued[WRITE]))) |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 990 | tg_may_dispatch(tg, bio, &write_wait); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 991 | |
| 992 | min_wait = min(read_wait, write_wait); |
| 993 | disptime = jiffies + min_wait; |
| 994 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 995 | /* Update dispatch time */ |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 996 | throtl_dequeue_tg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 997 | tg->disptime = disptime; |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 998 | throtl_enqueue_tg(tg); |
Tejun Heo | 0e9f416 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 999 | |
| 1000 | /* see throtl_add_bio_tg() */ |
| 1001 | tg->flags &= ~THROTL_TG_WAS_EMPTY; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1002 | } |
| 1003 | |
Vivek Goyal | 32ee5bc | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1004 | static void start_parent_slice_with_credit(struct throtl_grp *child_tg, |
| 1005 | struct throtl_grp *parent_tg, bool rw) |
| 1006 | { |
| 1007 | if (throtl_slice_used(parent_tg, rw)) { |
| 1008 | throtl_start_new_slice_with_credit(parent_tg, rw, |
| 1009 | child_tg->slice_start[rw]); |
| 1010 | } |
| 1011 | |
| 1012 | } |
| 1013 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1014 | static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1015 | { |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1016 | struct throtl_service_queue *sq = &tg->service_queue; |
Tejun Heo | 6bc9c2b | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1017 | struct throtl_service_queue *parent_sq = sq->parent_sq; |
| 1018 | struct throtl_grp *parent_tg = sq_to_tg(parent_sq); |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1019 | struct throtl_grp *tg_to_put = NULL; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1020 | struct bio *bio; |
| 1021 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1022 | /* |
| 1023 | * @bio is being transferred from @tg to @parent_sq. Popping a bio |
| 1024 | * from @tg may put its reference and @parent_sq might end up |
| 1025 | * getting released prematurely. Remember the tg to put and put it |
| 1026 | * after @bio is transferred to @parent_sq. |
| 1027 | */ |
| 1028 | bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put); |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1029 | sq->nr_queued[rw]--; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1030 | |
| 1031 | throtl_charge_bio(tg, bio); |
Tejun Heo | 6bc9c2b | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1032 | |
| 1033 | /* |
| 1034 | * If our parent is another tg, we just need to transfer @bio to |
| 1035 | * the parent using throtl_add_bio_tg(). If our parent is |
| 1036 | * @td->service_queue, @bio is ready to be issued. Put it on its |
| 1037 | * bio_lists[] and decrease total number queued. The caller is |
| 1038 | * responsible for issuing these bios. |
| 1039 | */ |
| 1040 | if (parent_tg) { |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1041 | throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg); |
Vivek Goyal | 32ee5bc | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1042 | start_parent_slice_with_credit(tg, parent_tg, rw); |
Tejun Heo | 6bc9c2b | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1043 | } else { |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1044 | throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw], |
| 1045 | &parent_sq->queued[rw]); |
Tejun Heo | 6bc9c2b | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1046 | BUG_ON(tg->td->nr_queued[rw] <= 0); |
| 1047 | tg->td->nr_queued[rw]--; |
| 1048 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1049 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1050 | throtl_trim_slice(tg, rw); |
Tejun Heo | 6bc9c2b | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1051 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1052 | if (tg_to_put) |
| 1053 | blkg_put(tg_to_blkg(tg_to_put)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1054 | } |
| 1055 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1056 | static int throtl_dispatch_tg(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1057 | { |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1058 | struct throtl_service_queue *sq = &tg->service_queue; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1059 | unsigned int nr_reads = 0, nr_writes = 0; |
| 1060 | unsigned int max_nr_reads = throtl_grp_quantum*3/4; |
Vivek Goyal | c2f6805 | 2010-11-15 19:32:42 +0100 | [diff] [blame] | 1061 | unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1062 | struct bio *bio; |
| 1063 | |
| 1064 | /* Try to dispatch 75% READS and 25% WRITES */ |
| 1065 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1066 | while ((bio = throtl_peek_queued(&sq->queued[READ])) && |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1067 | tg_may_dispatch(tg, bio, NULL)) { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1068 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1069 | tg_dispatch_one_bio(tg, bio_data_dir(bio)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1070 | nr_reads++; |
| 1071 | |
| 1072 | if (nr_reads >= max_nr_reads) |
| 1073 | break; |
| 1074 | } |
| 1075 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1076 | while ((bio = throtl_peek_queued(&sq->queued[WRITE])) && |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1077 | tg_may_dispatch(tg, bio, NULL)) { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1078 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1079 | tg_dispatch_one_bio(tg, bio_data_dir(bio)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1080 | nr_writes++; |
| 1081 | |
| 1082 | if (nr_writes >= max_nr_writes) |
| 1083 | break; |
| 1084 | } |
| 1085 | |
| 1086 | return nr_reads + nr_writes; |
| 1087 | } |
| 1088 | |
Tejun Heo | 651930b | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1089 | static int throtl_select_dispatch(struct throtl_service_queue *parent_sq) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1090 | { |
| 1091 | unsigned int nr_disp = 0; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1092 | |
| 1093 | while (1) { |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1094 | struct throtl_grp *tg = throtl_rb_first(parent_sq); |
| 1095 | struct throtl_service_queue *sq = &tg->service_queue; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1096 | |
| 1097 | if (!tg) |
| 1098 | break; |
| 1099 | |
| 1100 | if (time_before(jiffies, tg->disptime)) |
| 1101 | break; |
| 1102 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1103 | throtl_dequeue_tg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1104 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1105 | nr_disp += throtl_dispatch_tg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1106 | |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1107 | if (sq->nr_queued[0] || sq->nr_queued[1]) |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1108 | tg_update_disptime(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1109 | |
| 1110 | if (nr_disp >= throtl_quantum) |
| 1111 | break; |
| 1112 | } |
| 1113 | |
| 1114 | return nr_disp; |
| 1115 | } |
| 1116 | |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1117 | /** |
| 1118 | * throtl_pending_timer_fn - timer function for service_queue->pending_timer |
| 1119 | * @arg: the throtl_service_queue being serviced |
| 1120 | * |
| 1121 | * This timer is armed when a child throtl_grp with active bio's become |
| 1122 | * pending and queued on the service_queue's pending_tree and expires when |
| 1123 | * the first child throtl_grp should be dispatched. This function |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1124 | * dispatches bio's from the children throtl_grps to the parent |
| 1125 | * service_queue. |
| 1126 | * |
| 1127 | * If the parent's parent is another throtl_grp, dispatching is propagated |
| 1128 | * by either arming its pending_timer or repeating dispatch directly. If |
| 1129 | * the top-level service_tree is reached, throtl_data->dispatch_work is |
| 1130 | * kicked so that the ready bio's are issued. |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1131 | */ |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1132 | static void throtl_pending_timer_fn(unsigned long arg) |
| 1133 | { |
| 1134 | struct throtl_service_queue *sq = (void *)arg; |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1135 | struct throtl_grp *tg = sq_to_tg(sq); |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1136 | struct throtl_data *td = sq_to_td(sq); |
Tejun Heo | cb76199 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 1137 | struct request_queue *q = td->queue; |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1138 | struct throtl_service_queue *parent_sq; |
| 1139 | bool dispatched; |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1140 | int ret; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1141 | |
| 1142 | spin_lock_irq(q->queue_lock); |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1143 | again: |
| 1144 | parent_sq = sq->parent_sq; |
| 1145 | dispatched = false; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1146 | |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1147 | while (true) { |
| 1148 | throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u", |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1149 | sq->nr_queued[READ] + sq->nr_queued[WRITE], |
| 1150 | sq->nr_queued[READ], sq->nr_queued[WRITE]); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1151 | |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1152 | ret = throtl_select_dispatch(sq); |
| 1153 | if (ret) { |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1154 | throtl_log(sq, "bios disp=%u", ret); |
| 1155 | dispatched = true; |
Tejun Heo | 651930b | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1156 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1157 | |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1158 | if (throtl_schedule_next_dispatch(sq, false)) |
| 1159 | break; |
| 1160 | |
| 1161 | /* this dispatch windows is still open, relax and repeat */ |
| 1162 | spin_unlock_irq(q->queue_lock); |
| 1163 | cpu_relax(); |
| 1164 | spin_lock_irq(q->queue_lock); |
| 1165 | } |
Tejun Heo | 6a52560 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1166 | |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1167 | if (!dispatched) |
| 1168 | goto out_unlock; |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1169 | |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1170 | if (parent_sq) { |
| 1171 | /* @parent_sq is another throl_grp, propagate dispatch */ |
| 1172 | if (tg->flags & THROTL_TG_WAS_EMPTY) { |
| 1173 | tg_update_disptime(tg); |
| 1174 | if (!throtl_schedule_next_dispatch(parent_sq, false)) { |
| 1175 | /* window is already open, repeat dispatching */ |
| 1176 | sq = parent_sq; |
| 1177 | tg = sq_to_tg(sq); |
| 1178 | goto again; |
| 1179 | } |
| 1180 | } |
| 1181 | } else { |
| 1182 | /* reached the top-level, queue issueing */ |
| 1183 | queue_work(kthrotld_workqueue, &td->dispatch_work); |
| 1184 | } |
| 1185 | out_unlock: |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1186 | spin_unlock_irq(q->queue_lock); |
| 1187 | } |
| 1188 | |
| 1189 | /** |
| 1190 | * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work |
| 1191 | * @work: work item being executed |
| 1192 | * |
| 1193 | * This function is queued for execution when bio's reach the bio_lists[] |
| 1194 | * of throtl_data->service_queue. Those bio's are ready and issued by this |
| 1195 | * function. |
| 1196 | */ |
Fabian Frederick | 8876e140 | 2014-04-17 21:41:16 +0200 | [diff] [blame] | 1197 | static void blk_throtl_dispatch_work_fn(struct work_struct *work) |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1198 | { |
| 1199 | struct throtl_data *td = container_of(work, struct throtl_data, |
| 1200 | dispatch_work); |
| 1201 | struct throtl_service_queue *td_sq = &td->service_queue; |
| 1202 | struct request_queue *q = td->queue; |
| 1203 | struct bio_list bio_list_on_stack; |
| 1204 | struct bio *bio; |
| 1205 | struct blk_plug plug; |
| 1206 | int rw; |
| 1207 | |
| 1208 | bio_list_init(&bio_list_on_stack); |
| 1209 | |
| 1210 | spin_lock_irq(q->queue_lock); |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1211 | for (rw = READ; rw <= WRITE; rw++) |
| 1212 | while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL))) |
| 1213 | bio_list_add(&bio_list_on_stack, bio); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1214 | spin_unlock_irq(q->queue_lock); |
| 1215 | |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1216 | if (!bio_list_empty(&bio_list_on_stack)) { |
Vivek Goyal | 69d60eb | 2011-03-09 08:27:37 +0100 | [diff] [blame] | 1217 | blk_start_plug(&plug); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1218 | while((bio = bio_list_pop(&bio_list_on_stack))) |
| 1219 | generic_make_request(bio); |
Vivek Goyal | 69d60eb | 2011-03-09 08:27:37 +0100 | [diff] [blame] | 1220 | blk_finish_plug(&plug); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1221 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1222 | } |
| 1223 | |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1224 | static u64 tg_prfill_cpu_rwstat(struct seq_file *sf, |
| 1225 | struct blkg_policy_data *pd, int off) |
Tejun Heo | 41b38b6 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1226 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1227 | struct throtl_grp *tg = pd_to_tg(pd); |
Tejun Heo | 41b38b6 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1228 | struct blkg_rwstat rwstat = { }, tmp; |
| 1229 | int i, cpu; |
| 1230 | |
| 1231 | for_each_possible_cpu(cpu) { |
Tejun Heo | 8a3d261 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1232 | struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu); |
Tejun Heo | 41b38b6 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1233 | |
| 1234 | tmp = blkg_rwstat_read((void *)sc + off); |
| 1235 | for (i = 0; i < BLKG_RWSTAT_NR; i++) |
| 1236 | rwstat.cnt[i] += tmp.cnt[i]; |
| 1237 | } |
| 1238 | |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1239 | return __blkg_prfill_rwstat(sf, pd, &rwstat); |
Tejun Heo | 41b38b6 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1242 | static int tg_print_cpu_rwstat(struct seq_file *sf, void *v) |
Tejun Heo | 41b38b6 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1243 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1244 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_cpu_rwstat, |
| 1245 | &blkcg_policy_throtl, seq_cft(sf)->private, true); |
Tejun Heo | 41b38b6 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1246 | return 0; |
| 1247 | } |
| 1248 | |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1249 | static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd, |
| 1250 | int off) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1251 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1252 | struct throtl_grp *tg = pd_to_tg(pd); |
| 1253 | u64 v = *(u64 *)((void *)tg + off); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1254 | |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1255 | if (v == -1) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1256 | return 0; |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1257 | return __blkg_prfill_u64(sf, pd, v); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1260 | static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd, |
| 1261 | int off) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1262 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1263 | struct throtl_grp *tg = pd_to_tg(pd); |
| 1264 | unsigned int v = *(unsigned int *)((void *)tg + off); |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1265 | |
| 1266 | if (v == -1) |
| 1267 | return 0; |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1268 | return __blkg_prfill_u64(sf, pd, v); |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1271 | static int tg_print_conf_u64(struct seq_file *sf, void *v) |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1272 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1273 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64, |
| 1274 | &blkcg_policy_throtl, seq_cft(sf)->private, false); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1275 | return 0; |
| 1276 | } |
| 1277 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1278 | static int tg_print_conf_uint(struct seq_file *sf, void *v) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1279 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1280 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint, |
| 1281 | &blkcg_policy_throtl, seq_cft(sf)->private, false); |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1282 | return 0; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1283 | } |
| 1284 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1285 | static ssize_t tg_set_conf(struct kernfs_open_file *of, |
| 1286 | char *buf, size_t nbytes, loff_t off, bool is_u64) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1287 | { |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1288 | struct blkcg *blkcg = css_to_blkcg(of_css(of)); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1289 | struct blkg_conf_ctx ctx; |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1290 | struct throtl_grp *tg; |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1291 | struct throtl_service_queue *sq; |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1292 | struct blkcg_gq *blkg; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 1293 | struct cgroup_subsys_state *pos_css; |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1294 | int ret; |
| 1295 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1296 | ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1297 | if (ret) |
| 1298 | return ret; |
| 1299 | |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1300 | tg = blkg_to_tg(ctx.blkg); |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1301 | sq = &tg->service_queue; |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1302 | |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1303 | if (!ctx.v) |
| 1304 | ctx.v = -1; |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1305 | |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1306 | if (is_u64) |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1307 | *(u64 *)((void *)tg + of_cft(of)->private) = ctx.v; |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1308 | else |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1309 | *(unsigned int *)((void *)tg + of_cft(of)->private) = ctx.v; |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1310 | |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1311 | throtl_log(&tg->service_queue, |
| 1312 | "limit change rbps=%llu wbps=%llu riops=%u wiops=%u", |
| 1313 | tg->bps[READ], tg->bps[WRITE], |
| 1314 | tg->iops[READ], tg->iops[WRITE]); |
Tejun Heo | 632b449 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 1315 | |
| 1316 | /* |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1317 | * Update has_rules[] flags for the updated tg's subtree. A tg is |
| 1318 | * considered to have rules if either the tg itself or any of its |
| 1319 | * ancestors has rules. This identifies groups without any |
| 1320 | * restrictions in the whole hierarchy and allows them to bypass |
| 1321 | * blk-throttle. |
| 1322 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 1323 | blkg_for_each_descendant_pre(blkg, pos_css, ctx.blkg) |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1324 | tg_update_has_rules(blkg_to_tg(blkg)); |
| 1325 | |
| 1326 | /* |
Tejun Heo | 632b449 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 1327 | * We're already holding queue_lock and know @tg is valid. Let's |
| 1328 | * apply the new config directly. |
| 1329 | * |
| 1330 | * Restart the slices for both READ and WRITES. It might happen |
| 1331 | * that a group's limit are dropped suddenly and we don't want to |
| 1332 | * account recently dispatched IO with new low rate. |
| 1333 | */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1334 | throtl_start_new_slice(tg, 0); |
| 1335 | throtl_start_new_slice(tg, 1); |
Tejun Heo | 632b449 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 1336 | |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1337 | if (tg->flags & THROTL_TG_PENDING) { |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1338 | tg_update_disptime(tg); |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1339 | throtl_schedule_next_dispatch(sq->parent_sq, true); |
Tejun Heo | 632b449 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 1340 | } |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1341 | |
| 1342 | blkg_conf_finish(&ctx); |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1343 | return nbytes; |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1346 | static ssize_t tg_set_conf_u64(struct kernfs_open_file *of, |
| 1347 | char *buf, size_t nbytes, loff_t off) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1348 | { |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1349 | return tg_set_conf(of, buf, nbytes, off, true); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1352 | static ssize_t tg_set_conf_uint(struct kernfs_open_file *of, |
| 1353 | char *buf, size_t nbytes, loff_t off) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1354 | { |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1355 | return tg_set_conf(of, buf, nbytes, off, false); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | static struct cftype throtl_files[] = { |
| 1359 | { |
| 1360 | .name = "throttle.read_bps_device", |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1361 | .private = offsetof(struct throtl_grp, bps[READ]), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1362 | .seq_show = tg_print_conf_u64, |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1363 | .write = tg_set_conf_u64, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1364 | }, |
| 1365 | { |
| 1366 | .name = "throttle.write_bps_device", |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1367 | .private = offsetof(struct throtl_grp, bps[WRITE]), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1368 | .seq_show = tg_print_conf_u64, |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1369 | .write = tg_set_conf_u64, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1370 | }, |
| 1371 | { |
| 1372 | .name = "throttle.read_iops_device", |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1373 | .private = offsetof(struct throtl_grp, iops[READ]), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1374 | .seq_show = tg_print_conf_uint, |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1375 | .write = tg_set_conf_uint, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1376 | }, |
| 1377 | { |
| 1378 | .name = "throttle.write_iops_device", |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1379 | .private = offsetof(struct throtl_grp, iops[WRITE]), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1380 | .seq_show = tg_print_conf_uint, |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1381 | .write = tg_set_conf_uint, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1382 | }, |
| 1383 | { |
| 1384 | .name = "throttle.io_service_bytes", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1385 | .private = offsetof(struct tg_stats_cpu, service_bytes), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1386 | .seq_show = tg_print_cpu_rwstat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1387 | }, |
| 1388 | { |
| 1389 | .name = "throttle.io_serviced", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1390 | .private = offsetof(struct tg_stats_cpu, serviced), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1391 | .seq_show = tg_print_cpu_rwstat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1392 | }, |
| 1393 | { } /* terminate */ |
| 1394 | }; |
| 1395 | |
Vivek Goyal | da52777 | 2011-03-02 19:05:33 -0500 | [diff] [blame] | 1396 | static void throtl_shutdown_wq(struct request_queue *q) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1397 | { |
| 1398 | struct throtl_data *td = q->td; |
| 1399 | |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1400 | cancel_work_sync(&td->dispatch_work); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1401 | } |
| 1402 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1403 | static struct blkcg_policy blkcg_policy_throtl = { |
Tejun Heo | f9fcc2d | 2012-04-16 13:57:27 -0700 | [diff] [blame] | 1404 | .cftypes = throtl_files, |
| 1405 | |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 1406 | .pd_alloc_fn = throtl_pd_alloc, |
Tejun Heo | f9fcc2d | 2012-04-16 13:57:27 -0700 | [diff] [blame] | 1407 | .pd_init_fn = throtl_pd_init, |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1408 | .pd_online_fn = throtl_pd_online, |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 1409 | .pd_free_fn = throtl_pd_free, |
Tejun Heo | f9fcc2d | 2012-04-16 13:57:27 -0700 | [diff] [blame] | 1410 | .pd_reset_stats_fn = throtl_pd_reset_stats, |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1411 | }; |
| 1412 | |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1413 | bool blk_throtl_bio(struct request_queue *q, struct bio *bio) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1414 | { |
| 1415 | struct throtl_data *td = q->td; |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1416 | struct throtl_qnode *qn = NULL; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1417 | struct throtl_grp *tg; |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1418 | struct throtl_service_queue *sq; |
Tejun Heo | 0e9f416 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1419 | bool rw = bio_data_dir(bio); |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1420 | struct blkcg *blkcg; |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1421 | bool throttled = false; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1422 | |
Tejun Heo | 2a0f61e | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1423 | /* see throtl_charge_bio() */ |
| 1424 | if (bio->bi_rw & REQ_THROTTLED) |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1425 | goto out; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1426 | |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 1427 | /* |
| 1428 | * A throtl_grp pointer retrieved under rcu can be used to access |
| 1429 | * basic fields like stats and io rates. If a group has no rules, |
| 1430 | * just update the dispatch stats in lockless manner and return. |
| 1431 | */ |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 1432 | rcu_read_lock(); |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1433 | blkcg = bio_blkcg(bio); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1434 | tg = throtl_lookup_tg(td, blkcg); |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 1435 | if (tg) { |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1436 | if (!tg->has_rules[rw]) { |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1437 | throtl_update_dispatch_stats(tg_to_blkg(tg), |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1438 | bio->bi_iter.bi_size, bio->bi_rw); |
Tejun Heo | 2a7f124 | 2012-03-05 13:15:01 -0800 | [diff] [blame] | 1439 | goto out_unlock_rcu; |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 1440 | } |
| 1441 | } |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 1442 | |
| 1443 | /* |
| 1444 | * Either group has not been allocated yet or it is not an unlimited |
| 1445 | * IO group |
| 1446 | */ |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1447 | spin_lock_irq(q->queue_lock); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1448 | tg = throtl_lookup_create_tg(td, blkcg); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1449 | if (unlikely(!tg)) |
| 1450 | goto out_unlock; |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 1451 | |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1452 | sq = &tg->service_queue; |
| 1453 | |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1454 | while (true) { |
| 1455 | /* throtl is FIFO - if bios are already queued, should queue */ |
| 1456 | if (sq->nr_queued[rw]) |
| 1457 | break; |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 1458 | |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1459 | /* if above limits, break to queue */ |
| 1460 | if (!tg_may_dispatch(tg, bio, NULL)) |
| 1461 | break; |
| 1462 | |
| 1463 | /* within limits, let's charge and dispatch directly */ |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1464 | throtl_charge_bio(tg, bio); |
Vivek Goyal | 04521db | 2011-03-22 21:54:29 +0100 | [diff] [blame] | 1465 | |
| 1466 | /* |
| 1467 | * We need to trim slice even when bios are not being queued |
| 1468 | * otherwise it might happen that a bio is not queued for |
| 1469 | * a long time and slice keeps on extending and trim is not |
| 1470 | * called for a long time. Now if limits are reduced suddenly |
| 1471 | * we take into account all the IO dispatched so far at new |
| 1472 | * low rate and * newly queued IO gets a really long dispatch |
| 1473 | * time. |
| 1474 | * |
| 1475 | * So keep on trimming slice even if bio is not queued. |
| 1476 | */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1477 | throtl_trim_slice(tg, rw); |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1478 | |
| 1479 | /* |
| 1480 | * @bio passed through this layer without being throttled. |
| 1481 | * Climb up the ladder. If we''re already at the top, it |
| 1482 | * can be executed directly. |
| 1483 | */ |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1484 | qn = &tg->qnode_on_parent[rw]; |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1485 | sq = sq->parent_sq; |
| 1486 | tg = sq_to_tg(sq); |
| 1487 | if (!tg) |
| 1488 | goto out_unlock; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1489 | } |
| 1490 | |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1491 | /* out-of-limit, queue to @tg */ |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1492 | throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d", |
| 1493 | rw == READ ? 'R' : 'W', |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1494 | tg->bytes_disp[rw], bio->bi_iter.bi_size, tg->bps[rw], |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1495 | tg->io_disp[rw], tg->iops[rw], |
| 1496 | sq->nr_queued[READ], sq->nr_queued[WRITE]); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1497 | |
Tejun Heo | 671058f | 2012-03-05 13:15:29 -0800 | [diff] [blame] | 1498 | bio_associate_current(bio); |
Tejun Heo | 6bc9c2b | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1499 | tg->td->nr_queued[rw]++; |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1500 | throtl_add_bio_tg(bio, qn, tg); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1501 | throttled = true; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1502 | |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1503 | /* |
| 1504 | * Update @tg's dispatch time and force schedule dispatch if @tg |
| 1505 | * was empty before @bio. The forced scheduling isn't likely to |
| 1506 | * cause undue delay as @bio is likely to be dispatched directly if |
| 1507 | * its @tg's disptime is not in the future. |
| 1508 | */ |
Tejun Heo | 0e9f416 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1509 | if (tg->flags & THROTL_TG_WAS_EMPTY) { |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1510 | tg_update_disptime(tg); |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1511 | throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1512 | } |
| 1513 | |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1514 | out_unlock: |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1515 | spin_unlock_irq(q->queue_lock); |
Tejun Heo | 2a7f124 | 2012-03-05 13:15:01 -0800 | [diff] [blame] | 1516 | out_unlock_rcu: |
| 1517 | rcu_read_unlock(); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1518 | out: |
Tejun Heo | 2a0f61e | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1519 | /* |
| 1520 | * As multiple blk-throtls may stack in the same issue path, we |
| 1521 | * don't want bios to leave with the flag set. Clear the flag if |
| 1522 | * being issued. |
| 1523 | */ |
| 1524 | if (!throttled) |
| 1525 | bio->bi_rw &= ~REQ_THROTTLED; |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1526 | return throttled; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1527 | } |
| 1528 | |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1529 | /* |
| 1530 | * Dispatch all bios from all children tg's queued on @parent_sq. On |
| 1531 | * return, @parent_sq is guaranteed to not have any active children tg's |
| 1532 | * and all bios from previously active tg's are on @parent_sq->bio_lists[]. |
| 1533 | */ |
| 1534 | static void tg_drain_bios(struct throtl_service_queue *parent_sq) |
| 1535 | { |
| 1536 | struct throtl_grp *tg; |
| 1537 | |
| 1538 | while ((tg = throtl_rb_first(parent_sq))) { |
| 1539 | struct throtl_service_queue *sq = &tg->service_queue; |
| 1540 | struct bio *bio; |
| 1541 | |
| 1542 | throtl_dequeue_tg(tg); |
| 1543 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1544 | while ((bio = throtl_peek_queued(&sq->queued[READ]))) |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1545 | tg_dispatch_one_bio(tg, bio_data_dir(bio)); |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1546 | while ((bio = throtl_peek_queued(&sq->queued[WRITE]))) |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1547 | tg_dispatch_one_bio(tg, bio_data_dir(bio)); |
| 1548 | } |
| 1549 | } |
| 1550 | |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 1551 | /** |
| 1552 | * blk_throtl_drain - drain throttled bios |
| 1553 | * @q: request_queue to drain throttled bios for |
| 1554 | * |
| 1555 | * Dispatch all currently throttled bios on @q through ->make_request_fn(). |
| 1556 | */ |
| 1557 | void blk_throtl_drain(struct request_queue *q) |
| 1558 | __releases(q->queue_lock) __acquires(q->queue_lock) |
| 1559 | { |
| 1560 | struct throtl_data *td = q->td; |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1561 | struct blkcg_gq *blkg; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 1562 | struct cgroup_subsys_state *pos_css; |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 1563 | struct bio *bio; |
Tejun Heo | 651930b | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1564 | int rw; |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 1565 | |
Andi Kleen | 8bcb6c7 | 2012-03-30 12:33:28 +0200 | [diff] [blame] | 1566 | queue_lockdep_assert_held(q); |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1567 | rcu_read_lock(); |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 1568 | |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1569 | /* |
| 1570 | * Drain each tg while doing post-order walk on the blkg tree, so |
| 1571 | * that all bios are propagated to td->service_queue. It'd be |
| 1572 | * better to walk service_queue tree directly but blkg walk is |
| 1573 | * easier. |
| 1574 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 1575 | blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1576 | tg_drain_bios(&blkg_to_tg(blkg)->service_queue); |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1577 | |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1578 | /* finally, transfer bios from top-level tg's into the td */ |
| 1579 | tg_drain_bios(&td->service_queue); |
| 1580 | |
| 1581 | rcu_read_unlock(); |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 1582 | spin_unlock_irq(q->queue_lock); |
| 1583 | |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1584 | /* all bios now should be in td->service_queue, issue them */ |
Tejun Heo | 651930b | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1585 | for (rw = READ; rw <= WRITE; rw++) |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1586 | while ((bio = throtl_pop_queued(&td->service_queue.queued[rw], |
| 1587 | NULL))) |
Tejun Heo | 651930b | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1588 | generic_make_request(bio); |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 1589 | |
| 1590 | spin_lock_irq(q->queue_lock); |
| 1591 | } |
| 1592 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1593 | int blk_throtl_init(struct request_queue *q) |
| 1594 | { |
| 1595 | struct throtl_data *td; |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1596 | int ret; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1597 | |
| 1598 | td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node); |
| 1599 | if (!td) |
| 1600 | return -ENOMEM; |
| 1601 | |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1602 | INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn); |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 1603 | throtl_service_queue_init(&td->service_queue); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1604 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1605 | q->td = td; |
Vivek Goyal | 29b1258 | 2011-05-19 15:38:24 -0400 | [diff] [blame] | 1606 | td->queue = q; |
Vivek Goyal | 02977e4 | 2010-10-01 14:49:48 +0200 | [diff] [blame] | 1607 | |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1608 | /* activate policy */ |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1609 | ret = blkcg_activate_policy(q, &blkcg_policy_throtl); |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1610 | if (ret) |
Vivek Goyal | 29b1258 | 2011-05-19 15:38:24 -0400 | [diff] [blame] | 1611 | kfree(td); |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1612 | return ret; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | void blk_throtl_exit(struct request_queue *q) |
| 1616 | { |
Tejun Heo | c875f4d | 2012-03-05 13:15:22 -0800 | [diff] [blame] | 1617 | BUG_ON(!q->td); |
Vivek Goyal | da52777 | 2011-03-02 19:05:33 -0500 | [diff] [blame] | 1618 | throtl_shutdown_wq(q); |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1619 | blkcg_deactivate_policy(q, &blkcg_policy_throtl); |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 1620 | kfree(q->td); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | static int __init throtl_init(void) |
| 1624 | { |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 1625 | kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0); |
| 1626 | if (!kthrotld_workqueue) |
| 1627 | panic("Failed to create kthrotld\n"); |
| 1628 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1629 | return blkcg_policy_register(&blkcg_policy_throtl); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | module_init(throtl_init); |