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