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