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