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