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> |
| 12 | #include "blk-cgroup.h" |
Tejun Heo | bc9fcbf | 2011-10-19 14:31:18 +0200 | [diff] [blame] | 13 | #include "blk.h" |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 14 | |
| 15 | /* Max dispatch from a group in 1 round */ |
| 16 | static int throtl_grp_quantum = 8; |
| 17 | |
| 18 | /* Total max dispatch from all groups in one round */ |
| 19 | static int throtl_quantum = 32; |
| 20 | |
| 21 | /* Throttling is performed over 100ms slice and after that slice is renewed */ |
| 22 | static unsigned long throtl_slice = HZ/10; /* 100 ms */ |
| 23 | |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 24 | static struct blkio_policy_type blkio_policy_throtl; |
| 25 | |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 26 | /* A workqueue to queue throttle related work */ |
| 27 | static struct workqueue_struct *kthrotld_workqueue; |
| 28 | static void throtl_schedule_delayed_work(struct throtl_data *td, |
| 29 | unsigned long delay); |
| 30 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 31 | struct throtl_rb_root { |
| 32 | struct rb_root rb; |
| 33 | struct rb_node *left; |
| 34 | unsigned int count; |
| 35 | unsigned long min_disptime; |
| 36 | }; |
| 37 | |
| 38 | #define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \ |
| 39 | .count = 0, .min_disptime = 0} |
| 40 | |
| 41 | #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node) |
| 42 | |
| 43 | struct throtl_grp { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 44 | /* active throtl group service_tree member */ |
| 45 | struct rb_node rb_node; |
| 46 | |
| 47 | /* |
| 48 | * Dispatch time in jiffies. This is the estimated time when group |
| 49 | * will unthrottle and is ready to dispatch more bio. It is used as |
| 50 | * key to sort active groups in service tree. |
| 51 | */ |
| 52 | unsigned long disptime; |
| 53 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 54 | unsigned int flags; |
| 55 | |
| 56 | /* Two lists for READ and WRITE */ |
| 57 | struct bio_list bio_lists[2]; |
| 58 | |
| 59 | /* Number of queued bios on READ and WRITE lists */ |
| 60 | unsigned int nr_queued[2]; |
| 61 | |
| 62 | /* bytes per second rate limits */ |
| 63 | uint64_t bps[2]; |
| 64 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 65 | /* IOPS limits */ |
| 66 | unsigned int iops[2]; |
| 67 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 68 | /* Number of bytes disptached in current slice */ |
| 69 | uint64_t bytes_disp[2]; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 70 | /* Number of bio's dispatched in current slice */ |
| 71 | unsigned int io_disp[2]; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 72 | |
| 73 | /* When did we start a new slice */ |
| 74 | unsigned long slice_start[2]; |
| 75 | unsigned long slice_end[2]; |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 76 | |
| 77 | /* Some throttle limits got updated for the group */ |
Andreas Schwab | 6f03793 | 2011-03-30 12:21:56 +0200 | [diff] [blame] | 78 | int limits_changed; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | struct throtl_data |
| 82 | { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 83 | /* service tree for active throtl groups */ |
| 84 | struct throtl_rb_root tg_service_tree; |
| 85 | |
Vivek Goyal | 29b1258 | 2011-05-19 15:38:24 -0400 | [diff] [blame] | 86 | struct throtl_grp *root_tg; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 87 | struct request_queue *queue; |
| 88 | |
| 89 | /* Total Number of queued bios on READ and WRITE lists */ |
| 90 | unsigned int nr_queued[2]; |
| 91 | |
| 92 | /* |
Vivek Goyal | 02977e4 | 2010-10-01 14:49:48 +0200 | [diff] [blame] | 93 | * number of total undestroyed groups |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 94 | */ |
| 95 | unsigned int nr_undestroyed_grps; |
| 96 | |
| 97 | /* Work for dispatching throttled bios */ |
| 98 | struct delayed_work throtl_work; |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 99 | |
Andreas Schwab | 6f03793 | 2011-03-30 12:21:56 +0200 | [diff] [blame] | 100 | int limits_changed; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 101 | }; |
| 102 | |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 103 | static inline struct throtl_grp *blkg_to_tg(struct blkio_group *blkg) |
| 104 | { |
| 105 | return blkg_to_pdata(blkg, &blkio_policy_throtl); |
| 106 | } |
| 107 | |
| 108 | static inline struct blkio_group *tg_to_blkg(struct throtl_grp *tg) |
| 109 | { |
| 110 | return pdata_to_blkg(tg, &blkio_policy_throtl); |
| 111 | } |
| 112 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 113 | enum tg_state_flags { |
| 114 | THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */ |
| 115 | }; |
| 116 | |
| 117 | #define THROTL_TG_FNS(name) \ |
| 118 | static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \ |
| 119 | { \ |
| 120 | (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \ |
| 121 | } \ |
| 122 | static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \ |
| 123 | { \ |
| 124 | (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \ |
| 125 | } \ |
| 126 | static inline int throtl_tg_##name(const struct throtl_grp *tg) \ |
| 127 | { \ |
| 128 | return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \ |
| 129 | } |
| 130 | |
| 131 | THROTL_TG_FNS(on_rr); |
| 132 | |
| 133 | #define throtl_log_tg(td, tg, fmt, args...) \ |
| 134 | blk_add_trace_msg((td)->queue, "throtl %s " fmt, \ |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 135 | blkg_path(tg_to_blkg(tg)), ##args); \ |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 136 | |
| 137 | #define throtl_log(td, fmt, args...) \ |
| 138 | blk_add_trace_msg((td)->queue, "throtl " fmt, ##args) |
| 139 | |
Joe Perches | d2f31a5 | 2011-06-13 20:19:27 +0200 | [diff] [blame] | 140 | static inline unsigned int total_nr_queued(struct throtl_data *td) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 141 | { |
Joe Perches | d2f31a5 | 2011-06-13 20:19:27 +0200 | [diff] [blame] | 142 | return td->nr_queued[0] + td->nr_queued[1]; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 145 | static void throtl_init_blkio_group(struct blkio_group *blkg) |
Vivek Goyal | a29a171 | 2011-05-19 15:38:19 -0400 | [diff] [blame] | 146 | { |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 147 | struct throtl_grp *tg = blkg_to_tg(blkg); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 148 | |
Vivek Goyal | a29a171 | 2011-05-19 15:38:19 -0400 | [diff] [blame] | 149 | RB_CLEAR_NODE(&tg->rb_node); |
| 150 | bio_list_init(&tg->bio_lists[0]); |
| 151 | bio_list_init(&tg->bio_lists[1]); |
| 152 | tg->limits_changed = false; |
| 153 | |
Tejun Heo | e56da7e | 2012-03-05 13:15:07 -0800 | [diff] [blame] | 154 | tg->bps[READ] = -1; |
| 155 | tg->bps[WRITE] = -1; |
| 156 | tg->iops[READ] = -1; |
| 157 | tg->iops[WRITE] = -1; |
Vivek Goyal | a29a171 | 2011-05-19 15:38:19 -0400 | [diff] [blame] | 158 | } |
| 159 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 160 | static void throtl_link_blkio_group(struct request_queue *q, |
| 161 | struct blkio_group *blkg) |
Vivek Goyal | 269f541 | 2011-05-19 15:38:25 -0400 | [diff] [blame] | 162 | { |
Tejun Heo | 4eef304 | 2012-03-05 13:15:18 -0800 | [diff] [blame^] | 163 | list_add(&blkg->q_node[BLKIO_POLICY_THROTL], |
| 164 | &q->blkg_list[BLKIO_POLICY_THROTL]); |
| 165 | q->nr_blkgs[BLKIO_POLICY_THROTL]++; |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static struct |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 169 | throtl_grp *throtl_lookup_tg(struct throtl_data *td, struct blkio_cgroup *blkcg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 170 | { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 171 | /* |
Vivek Goyal | be2c6b1 | 2011-01-19 08:25:02 -0700 | [diff] [blame] | 172 | * This is the common case when there are no blkio cgroups. |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 173 | * Avoid lookup in this case |
| 174 | */ |
Vivek Goyal | be2c6b1 | 2011-01-19 08:25:02 -0700 | [diff] [blame] | 175 | if (blkcg == &blkio_root_cgroup) |
Tejun Heo | 7a4dd28 | 2012-03-05 13:15:09 -0800 | [diff] [blame] | 176 | return td->root_tg; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 177 | |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 178 | return blkg_to_tg(blkg_lookup(blkcg, td->queue, BLKIO_POLICY_THROTL)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 179 | } |
| 180 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 181 | static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td, |
| 182 | struct blkio_cgroup *blkcg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 183 | { |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 184 | struct request_queue *q = td->queue; |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 185 | struct throtl_grp *tg = NULL; |
Tejun Heo | 0a5a7d0 | 2012-03-05 13:15:02 -0800 | [diff] [blame] | 186 | |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 187 | /* |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 188 | * This is the common case when there are no blkio cgroups. |
| 189 | * Avoid lookup in this case |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 190 | */ |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 191 | if (blkcg == &blkio_root_cgroup) { |
Vivek Goyal | 29b1258 | 2011-05-19 15:38:24 -0400 | [diff] [blame] | 192 | tg = td->root_tg; |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 193 | } else { |
| 194 | struct blkio_group *blkg; |
| 195 | |
| 196 | blkg = blkg_lookup_create(blkcg, q, BLKIO_POLICY_THROTL, false); |
| 197 | |
| 198 | /* if %NULL and @q is alive, fall back to root_tg */ |
| 199 | if (!IS_ERR(blkg)) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 200 | tg = blkg_to_tg(blkg); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 201 | else if (!blk_queue_dead(q)) |
| 202 | tg = td->root_tg; |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 203 | } |
| 204 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 205 | return tg; |
| 206 | } |
| 207 | |
| 208 | static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root) |
| 209 | { |
| 210 | /* Service tree is empty */ |
| 211 | if (!root->count) |
| 212 | return NULL; |
| 213 | |
| 214 | if (!root->left) |
| 215 | root->left = rb_first(&root->rb); |
| 216 | |
| 217 | if (root->left) |
| 218 | return rb_entry_tg(root->left); |
| 219 | |
| 220 | return NULL; |
| 221 | } |
| 222 | |
| 223 | static void rb_erase_init(struct rb_node *n, struct rb_root *root) |
| 224 | { |
| 225 | rb_erase(n, root); |
| 226 | RB_CLEAR_NODE(n); |
| 227 | } |
| 228 | |
| 229 | static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root) |
| 230 | { |
| 231 | if (root->left == n) |
| 232 | root->left = NULL; |
| 233 | rb_erase_init(n, &root->rb); |
| 234 | --root->count; |
| 235 | } |
| 236 | |
| 237 | static void update_min_dispatch_time(struct throtl_rb_root *st) |
| 238 | { |
| 239 | struct throtl_grp *tg; |
| 240 | |
| 241 | tg = throtl_rb_first(st); |
| 242 | if (!tg) |
| 243 | return; |
| 244 | |
| 245 | st->min_disptime = tg->disptime; |
| 246 | } |
| 247 | |
| 248 | static void |
| 249 | tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg) |
| 250 | { |
| 251 | struct rb_node **node = &st->rb.rb_node; |
| 252 | struct rb_node *parent = NULL; |
| 253 | struct throtl_grp *__tg; |
| 254 | unsigned long key = tg->disptime; |
| 255 | int left = 1; |
| 256 | |
| 257 | while (*node != NULL) { |
| 258 | parent = *node; |
| 259 | __tg = rb_entry_tg(parent); |
| 260 | |
| 261 | if (time_before(key, __tg->disptime)) |
| 262 | node = &parent->rb_left; |
| 263 | else { |
| 264 | node = &parent->rb_right; |
| 265 | left = 0; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | if (left) |
| 270 | st->left = &tg->rb_node; |
| 271 | |
| 272 | rb_link_node(&tg->rb_node, parent, node); |
| 273 | rb_insert_color(&tg->rb_node, &st->rb); |
| 274 | } |
| 275 | |
| 276 | static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg) |
| 277 | { |
| 278 | struct throtl_rb_root *st = &td->tg_service_tree; |
| 279 | |
| 280 | tg_service_tree_add(st, tg); |
| 281 | throtl_mark_tg_on_rr(tg); |
| 282 | st->count++; |
| 283 | } |
| 284 | |
| 285 | static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg) |
| 286 | { |
| 287 | if (!throtl_tg_on_rr(tg)) |
| 288 | __throtl_enqueue_tg(td, tg); |
| 289 | } |
| 290 | |
| 291 | static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg) |
| 292 | { |
| 293 | throtl_rb_erase(&tg->rb_node, &td->tg_service_tree); |
| 294 | throtl_clear_tg_on_rr(tg); |
| 295 | } |
| 296 | |
| 297 | static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg) |
| 298 | { |
| 299 | if (throtl_tg_on_rr(tg)) |
| 300 | __throtl_dequeue_tg(td, tg); |
| 301 | } |
| 302 | |
| 303 | static void throtl_schedule_next_dispatch(struct throtl_data *td) |
| 304 | { |
| 305 | struct throtl_rb_root *st = &td->tg_service_tree; |
| 306 | |
| 307 | /* |
| 308 | * If there are more bios pending, schedule more work. |
| 309 | */ |
| 310 | if (!total_nr_queued(td)) |
| 311 | return; |
| 312 | |
| 313 | BUG_ON(!st->count); |
| 314 | |
| 315 | update_min_dispatch_time(st); |
| 316 | |
| 317 | if (time_before_eq(st->min_disptime, jiffies)) |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 318 | throtl_schedule_delayed_work(td, 0); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 319 | else |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 320 | throtl_schedule_delayed_work(td, (st->min_disptime - jiffies)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | static inline void |
| 324 | throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw) |
| 325 | { |
| 326 | tg->bytes_disp[rw] = 0; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 327 | tg->io_disp[rw] = 0; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 328 | tg->slice_start[rw] = jiffies; |
| 329 | tg->slice_end[rw] = jiffies + throtl_slice; |
| 330 | throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu", |
| 331 | rw == READ ? 'R' : 'W', tg->slice_start[rw], |
| 332 | tg->slice_end[rw], jiffies); |
| 333 | } |
| 334 | |
Vivek Goyal | d1ae8ff | 2010-12-01 19:34:46 +0100 | [diff] [blame] | 335 | static inline void throtl_set_slice_end(struct throtl_data *td, |
| 336 | struct throtl_grp *tg, bool rw, unsigned long jiffy_end) |
| 337 | { |
| 338 | tg->slice_end[rw] = roundup(jiffy_end, throtl_slice); |
| 339 | } |
| 340 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 341 | static inline void throtl_extend_slice(struct throtl_data *td, |
| 342 | struct throtl_grp *tg, bool rw, unsigned long jiffy_end) |
| 343 | { |
| 344 | tg->slice_end[rw] = roundup(jiffy_end, throtl_slice); |
| 345 | throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu", |
| 346 | rw == READ ? 'R' : 'W', tg->slice_start[rw], |
| 347 | tg->slice_end[rw], jiffies); |
| 348 | } |
| 349 | |
| 350 | /* Determine if previously allocated or extended slice is complete or not */ |
| 351 | static bool |
| 352 | throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw) |
| 353 | { |
| 354 | if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw])) |
| 355 | return 0; |
| 356 | |
| 357 | return 1; |
| 358 | } |
| 359 | |
| 360 | /* Trim the used slices and adjust slice start accordingly */ |
| 361 | static inline void |
| 362 | throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw) |
| 363 | { |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 364 | unsigned long nr_slices, time_elapsed, io_trim; |
| 365 | u64 bytes_trim, tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 366 | |
| 367 | BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw])); |
| 368 | |
| 369 | /* |
| 370 | * If bps are unlimited (-1), then time slice don't get |
| 371 | * renewed. Don't try to trim the slice if slice is used. A new |
| 372 | * slice will start when appropriate. |
| 373 | */ |
| 374 | if (throtl_slice_used(td, tg, rw)) |
| 375 | return; |
| 376 | |
Vivek Goyal | d1ae8ff | 2010-12-01 19:34:46 +0100 | [diff] [blame] | 377 | /* |
| 378 | * A bio has been dispatched. Also adjust slice_end. It might happen |
| 379 | * that initially cgroup limit was very low resulting in high |
| 380 | * slice_end, but later limit was bumped up and bio was dispached |
| 381 | * sooner, then we need to reduce slice_end. A high bogus slice_end |
| 382 | * is bad because it does not allow new slice to start. |
| 383 | */ |
| 384 | |
| 385 | throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice); |
| 386 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 387 | time_elapsed = jiffies - tg->slice_start[rw]; |
| 388 | |
| 389 | nr_slices = time_elapsed / throtl_slice; |
| 390 | |
| 391 | if (!nr_slices) |
| 392 | return; |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 393 | tmp = tg->bps[rw] * throtl_slice * nr_slices; |
| 394 | do_div(tmp, HZ); |
| 395 | bytes_trim = tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 396 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 397 | io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 398 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 399 | if (!bytes_trim && !io_trim) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 400 | return; |
| 401 | |
| 402 | if (tg->bytes_disp[rw] >= bytes_trim) |
| 403 | tg->bytes_disp[rw] -= bytes_trim; |
| 404 | else |
| 405 | tg->bytes_disp[rw] = 0; |
| 406 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 407 | if (tg->io_disp[rw] >= io_trim) |
| 408 | tg->io_disp[rw] -= io_trim; |
| 409 | else |
| 410 | tg->io_disp[rw] = 0; |
| 411 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 412 | tg->slice_start[rw] += nr_slices * throtl_slice; |
| 413 | |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 414 | throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu" |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 415 | " start=%lu end=%lu jiffies=%lu", |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 416 | rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim, |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 417 | tg->slice_start[rw], tg->slice_end[rw], jiffies); |
| 418 | } |
| 419 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 420 | static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg, |
| 421 | struct bio *bio, unsigned long *wait) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 422 | { |
| 423 | bool rw = bio_data_dir(bio); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 424 | unsigned int io_allowed; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 425 | unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd; |
Vivek Goyal | c49c06e | 2010-10-01 21:16:42 +0200 | [diff] [blame] | 426 | u64 tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 427 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 428 | jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw]; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 429 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 430 | /* Slice has just started. Consider one slice interval */ |
| 431 | if (!jiffy_elapsed) |
| 432 | jiffy_elapsed_rnd = throtl_slice; |
| 433 | |
| 434 | jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice); |
| 435 | |
Vivek Goyal | c49c06e | 2010-10-01 21:16:42 +0200 | [diff] [blame] | 436 | /* |
| 437 | * jiffy_elapsed_rnd should not be a big value as minimum iops can be |
| 438 | * 1 then at max jiffy elapsed should be equivalent of 1 second as we |
| 439 | * will allow dispatch after 1 second and after that slice should |
| 440 | * have been trimmed. |
| 441 | */ |
| 442 | |
| 443 | tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd; |
| 444 | do_div(tmp, HZ); |
| 445 | |
| 446 | if (tmp > UINT_MAX) |
| 447 | io_allowed = UINT_MAX; |
| 448 | else |
| 449 | io_allowed = tmp; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 450 | |
| 451 | if (tg->io_disp[rw] + 1 <= io_allowed) { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 452 | if (wait) |
| 453 | *wait = 0; |
| 454 | return 1; |
| 455 | } |
| 456 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 457 | /* Calc approx time to dispatch */ |
| 458 | jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1; |
| 459 | |
| 460 | if (jiffy_wait > jiffy_elapsed) |
| 461 | jiffy_wait = jiffy_wait - jiffy_elapsed; |
| 462 | else |
| 463 | jiffy_wait = 1; |
| 464 | |
| 465 | if (wait) |
| 466 | *wait = jiffy_wait; |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg, |
| 471 | struct bio *bio, unsigned long *wait) |
| 472 | { |
| 473 | bool rw = bio_data_dir(bio); |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 474 | u64 bytes_allowed, extra_bytes, tmp; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 475 | unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 476 | |
| 477 | jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw]; |
| 478 | |
| 479 | /* Slice has just started. Consider one slice interval */ |
| 480 | if (!jiffy_elapsed) |
| 481 | jiffy_elapsed_rnd = throtl_slice; |
| 482 | |
| 483 | jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice); |
| 484 | |
Vivek Goyal | 5e901a2 | 2010-10-01 21:16:38 +0200 | [diff] [blame] | 485 | tmp = tg->bps[rw] * jiffy_elapsed_rnd; |
| 486 | do_div(tmp, HZ); |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 487 | bytes_allowed = tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 488 | |
| 489 | if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) { |
| 490 | if (wait) |
| 491 | *wait = 0; |
| 492 | return 1; |
| 493 | } |
| 494 | |
| 495 | /* Calc approx time to dispatch */ |
| 496 | extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed; |
| 497 | jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]); |
| 498 | |
| 499 | if (!jiffy_wait) |
| 500 | jiffy_wait = 1; |
| 501 | |
| 502 | /* |
| 503 | * This wait time is without taking into consideration the rounding |
| 504 | * up we did. Add that time also. |
| 505 | */ |
| 506 | jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 507 | if (wait) |
| 508 | *wait = jiffy_wait; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 509 | return 0; |
| 510 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 511 | |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 512 | static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) { |
| 513 | if (tg->bps[rw] == -1 && tg->iops[rw] == -1) |
| 514 | return 1; |
| 515 | return 0; |
| 516 | } |
| 517 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 518 | /* |
| 519 | * Returns whether one can dispatch a bio or not. Also returns approx number |
| 520 | * of jiffies to wait before this bio is with-in IO rate and can be dispatched |
| 521 | */ |
| 522 | static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg, |
| 523 | struct bio *bio, unsigned long *wait) |
| 524 | { |
| 525 | bool rw = bio_data_dir(bio); |
| 526 | unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0; |
| 527 | |
| 528 | /* |
| 529 | * Currently whole state machine of group depends on first bio |
| 530 | * queued in the group bio list. So one should not be calling |
| 531 | * this function with a different bio if there are other bios |
| 532 | * queued. |
| 533 | */ |
| 534 | BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw])); |
| 535 | |
| 536 | /* If tg->bps = -1, then BW is unlimited */ |
| 537 | if (tg->bps[rw] == -1 && tg->iops[rw] == -1) { |
| 538 | if (wait) |
| 539 | *wait = 0; |
| 540 | return 1; |
| 541 | } |
| 542 | |
| 543 | /* |
| 544 | * If previous slice expired, start a new one otherwise renew/extend |
| 545 | * existing slice to make sure it is at least throtl_slice interval |
| 546 | * long since now. |
| 547 | */ |
| 548 | if (throtl_slice_used(td, tg, rw)) |
| 549 | throtl_start_new_slice(td, tg, rw); |
| 550 | else { |
| 551 | if (time_before(tg->slice_end[rw], jiffies + throtl_slice)) |
| 552 | throtl_extend_slice(td, tg, rw, jiffies + throtl_slice); |
| 553 | } |
| 554 | |
| 555 | if (tg_with_in_bps_limit(td, tg, bio, &bps_wait) |
| 556 | && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) { |
| 557 | if (wait) |
| 558 | *wait = 0; |
| 559 | return 1; |
| 560 | } |
| 561 | |
| 562 | max_wait = max(bps_wait, iops_wait); |
| 563 | |
| 564 | if (wait) |
| 565 | *wait = max_wait; |
| 566 | |
| 567 | if (time_before(tg->slice_end[rw], jiffies + max_wait)) |
| 568 | throtl_extend_slice(td, tg, rw, jiffies + max_wait); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio) |
| 574 | { |
| 575 | bool rw = bio_data_dir(bio); |
Shaohua Li | e5a94f5 | 2011-08-01 10:31:06 +0200 | [diff] [blame] | 576 | bool sync = rw_is_sync(bio->bi_rw); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 577 | |
| 578 | /* Charge the bio to the group */ |
| 579 | tg->bytes_disp[rw] += bio->bi_size; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 580 | tg->io_disp[rw]++; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 581 | |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 582 | blkiocg_update_dispatch_stats(tg_to_blkg(tg), &blkio_policy_throtl, |
| 583 | bio->bi_size, rw, sync); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg, |
| 587 | struct bio *bio) |
| 588 | { |
| 589 | bool rw = bio_data_dir(bio); |
| 590 | |
| 591 | bio_list_add(&tg->bio_lists[rw], bio); |
| 592 | /* Take a bio reference on tg */ |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 593 | blkg_get(tg_to_blkg(tg)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 594 | tg->nr_queued[rw]++; |
| 595 | td->nr_queued[rw]++; |
| 596 | throtl_enqueue_tg(td, tg); |
| 597 | } |
| 598 | |
| 599 | static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg) |
| 600 | { |
| 601 | unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime; |
| 602 | struct bio *bio; |
| 603 | |
| 604 | if ((bio = bio_list_peek(&tg->bio_lists[READ]))) |
| 605 | tg_may_dispatch(td, tg, bio, &read_wait); |
| 606 | |
| 607 | if ((bio = bio_list_peek(&tg->bio_lists[WRITE]))) |
| 608 | tg_may_dispatch(td, tg, bio, &write_wait); |
| 609 | |
| 610 | min_wait = min(read_wait, write_wait); |
| 611 | disptime = jiffies + min_wait; |
| 612 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 613 | /* Update dispatch time */ |
| 614 | throtl_dequeue_tg(td, tg); |
| 615 | tg->disptime = disptime; |
| 616 | throtl_enqueue_tg(td, tg); |
| 617 | } |
| 618 | |
| 619 | static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg, |
| 620 | bool rw, struct bio_list *bl) |
| 621 | { |
| 622 | struct bio *bio; |
| 623 | |
| 624 | bio = bio_list_pop(&tg->bio_lists[rw]); |
| 625 | tg->nr_queued[rw]--; |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 626 | /* Drop bio reference on blkg */ |
| 627 | blkg_put(tg_to_blkg(tg)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 628 | |
| 629 | BUG_ON(td->nr_queued[rw] <= 0); |
| 630 | td->nr_queued[rw]--; |
| 631 | |
| 632 | throtl_charge_bio(tg, bio); |
| 633 | bio_list_add(bl, bio); |
| 634 | bio->bi_rw |= REQ_THROTTLED; |
| 635 | |
| 636 | throtl_trim_slice(td, tg, rw); |
| 637 | } |
| 638 | |
| 639 | static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg, |
| 640 | struct bio_list *bl) |
| 641 | { |
| 642 | unsigned int nr_reads = 0, nr_writes = 0; |
| 643 | unsigned int max_nr_reads = throtl_grp_quantum*3/4; |
Vivek Goyal | c2f6805 | 2010-11-15 19:32:42 +0100 | [diff] [blame] | 644 | unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 645 | struct bio *bio; |
| 646 | |
| 647 | /* Try to dispatch 75% READS and 25% WRITES */ |
| 648 | |
| 649 | while ((bio = bio_list_peek(&tg->bio_lists[READ])) |
| 650 | && tg_may_dispatch(td, tg, bio, NULL)) { |
| 651 | |
| 652 | tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl); |
| 653 | nr_reads++; |
| 654 | |
| 655 | if (nr_reads >= max_nr_reads) |
| 656 | break; |
| 657 | } |
| 658 | |
| 659 | while ((bio = bio_list_peek(&tg->bio_lists[WRITE])) |
| 660 | && tg_may_dispatch(td, tg, bio, NULL)) { |
| 661 | |
| 662 | tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl); |
| 663 | nr_writes++; |
| 664 | |
| 665 | if (nr_writes >= max_nr_writes) |
| 666 | break; |
| 667 | } |
| 668 | |
| 669 | return nr_reads + nr_writes; |
| 670 | } |
| 671 | |
| 672 | static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl) |
| 673 | { |
| 674 | unsigned int nr_disp = 0; |
| 675 | struct throtl_grp *tg; |
| 676 | struct throtl_rb_root *st = &td->tg_service_tree; |
| 677 | |
| 678 | while (1) { |
| 679 | tg = throtl_rb_first(st); |
| 680 | |
| 681 | if (!tg) |
| 682 | break; |
| 683 | |
| 684 | if (time_before(jiffies, tg->disptime)) |
| 685 | break; |
| 686 | |
| 687 | throtl_dequeue_tg(td, tg); |
| 688 | |
| 689 | nr_disp += throtl_dispatch_tg(td, tg, bl); |
| 690 | |
| 691 | if (tg->nr_queued[0] || tg->nr_queued[1]) { |
| 692 | tg_update_disptime(td, tg); |
| 693 | throtl_enqueue_tg(td, tg); |
| 694 | } |
| 695 | |
| 696 | if (nr_disp >= throtl_quantum) |
| 697 | break; |
| 698 | } |
| 699 | |
| 700 | return nr_disp; |
| 701 | } |
| 702 | |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 703 | static void throtl_process_limit_change(struct throtl_data *td) |
| 704 | { |
Tejun Heo | 4eef304 | 2012-03-05 13:15:18 -0800 | [diff] [blame^] | 705 | struct request_queue *q = td->queue; |
| 706 | struct blkio_group *blkg, *n; |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 707 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 708 | if (!td->limits_changed) |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 709 | return; |
| 710 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 711 | xchg(&td->limits_changed, false); |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 712 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 713 | throtl_log(td, "limits changed"); |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 714 | |
Tejun Heo | 4eef304 | 2012-03-05 13:15:18 -0800 | [diff] [blame^] | 715 | list_for_each_entry_safe(blkg, n, &q->blkg_list[BLKIO_POLICY_THROTL], |
| 716 | q_node[BLKIO_POLICY_THROTL]) { |
| 717 | struct throtl_grp *tg = blkg_to_tg(blkg); |
| 718 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 719 | if (!tg->limits_changed) |
| 720 | continue; |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 721 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 722 | if (!xchg(&tg->limits_changed, false)) |
| 723 | continue; |
| 724 | |
| 725 | throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu" |
| 726 | " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE], |
| 727 | tg->iops[READ], tg->iops[WRITE]); |
| 728 | |
Vivek Goyal | 04521db | 2011-03-22 21:54:29 +0100 | [diff] [blame] | 729 | /* |
| 730 | * Restart the slices for both READ and WRITES. It |
| 731 | * might happen that a group's limit are dropped |
| 732 | * suddenly and we don't want to account recently |
| 733 | * dispatched IO with new low rate |
| 734 | */ |
| 735 | throtl_start_new_slice(td, tg, 0); |
| 736 | throtl_start_new_slice(td, tg, 1); |
| 737 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 738 | if (throtl_tg_on_rr(tg)) |
| 739 | tg_update_disptime(td, tg); |
| 740 | } |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 741 | } |
| 742 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 743 | /* Dispatch throttled bios. Should be called without queue lock held. */ |
| 744 | static int throtl_dispatch(struct request_queue *q) |
| 745 | { |
| 746 | struct throtl_data *td = q->td; |
| 747 | unsigned int nr_disp = 0; |
| 748 | struct bio_list bio_list_on_stack; |
| 749 | struct bio *bio; |
Vivek Goyal | 69d60eb | 2011-03-09 08:27:37 +0100 | [diff] [blame] | 750 | struct blk_plug plug; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 751 | |
| 752 | spin_lock_irq(q->queue_lock); |
| 753 | |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 754 | throtl_process_limit_change(td); |
| 755 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 756 | if (!total_nr_queued(td)) |
| 757 | goto out; |
| 758 | |
| 759 | bio_list_init(&bio_list_on_stack); |
| 760 | |
Joe Perches | d2f31a5 | 2011-06-13 20:19:27 +0200 | [diff] [blame] | 761 | throtl_log(td, "dispatch nr_queued=%u read=%u write=%u", |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 762 | total_nr_queued(td), td->nr_queued[READ], |
| 763 | td->nr_queued[WRITE]); |
| 764 | |
| 765 | nr_disp = throtl_select_dispatch(td, &bio_list_on_stack); |
| 766 | |
| 767 | if (nr_disp) |
| 768 | throtl_log(td, "bios disp=%u", nr_disp); |
| 769 | |
| 770 | throtl_schedule_next_dispatch(td); |
| 771 | out: |
| 772 | spin_unlock_irq(q->queue_lock); |
| 773 | |
| 774 | /* |
| 775 | * If we dispatched some requests, unplug the queue to make sure |
| 776 | * immediate dispatch |
| 777 | */ |
| 778 | if (nr_disp) { |
Vivek Goyal | 69d60eb | 2011-03-09 08:27:37 +0100 | [diff] [blame] | 779 | blk_start_plug(&plug); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 780 | while((bio = bio_list_pop(&bio_list_on_stack))) |
| 781 | generic_make_request(bio); |
Vivek Goyal | 69d60eb | 2011-03-09 08:27:37 +0100 | [diff] [blame] | 782 | blk_finish_plug(&plug); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 783 | } |
| 784 | return nr_disp; |
| 785 | } |
| 786 | |
| 787 | void blk_throtl_work(struct work_struct *work) |
| 788 | { |
| 789 | struct throtl_data *td = container_of(work, struct throtl_data, |
| 790 | throtl_work.work); |
| 791 | struct request_queue *q = td->queue; |
| 792 | |
| 793 | throtl_dispatch(q); |
| 794 | } |
| 795 | |
| 796 | /* Call with queue lock held */ |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 797 | static void |
| 798 | throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 799 | { |
| 800 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 801 | struct delayed_work *dwork = &td->throtl_work; |
| 802 | |
Vivek Goyal | 04521db | 2011-03-22 21:54:29 +0100 | [diff] [blame] | 803 | /* schedule work if limits changed even if no bio is queued */ |
Joe Perches | d2f31a5 | 2011-06-13 20:19:27 +0200 | [diff] [blame] | 804 | if (total_nr_queued(td) || td->limits_changed) { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 805 | /* |
| 806 | * We might have a work scheduled to be executed in future. |
| 807 | * Cancel that and schedule a new one. |
| 808 | */ |
| 809 | __cancel_delayed_work(dwork); |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 810 | queue_delayed_work(kthrotld_workqueue, dwork, delay); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 811 | throtl_log(td, "schedule work. delay=%lu jiffies=%lu", |
| 812 | delay, jiffies); |
| 813 | } |
| 814 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 815 | |
| 816 | static void |
| 817 | throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg) |
| 818 | { |
Tejun Heo | 4eef304 | 2012-03-05 13:15:18 -0800 | [diff] [blame^] | 819 | struct blkio_group *blkg = tg_to_blkg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 820 | |
Tejun Heo | 4eef304 | 2012-03-05 13:15:18 -0800 | [diff] [blame^] | 821 | /* Something wrong if we are trying to remove same group twice */ |
| 822 | WARN_ON_ONCE(list_empty(&blkg->q_node[BLKIO_POLICY_THROTL])); |
| 823 | |
| 824 | list_del_init(&blkg->q_node[BLKIO_POLICY_THROTL]); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 825 | |
| 826 | /* |
| 827 | * Put the reference taken at the time of creation so that when all |
| 828 | * queues are gone, group can be destroyed. |
| 829 | */ |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 830 | blkg_put(tg_to_blkg(tg)); |
Tejun Heo | 4eef304 | 2012-03-05 13:15:18 -0800 | [diff] [blame^] | 831 | td->queue->nr_blkgs[BLKIO_POLICY_THROTL]--; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 832 | } |
| 833 | |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 834 | static bool throtl_release_tgs(struct throtl_data *td, bool release_root) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 835 | { |
Tejun Heo | 4eef304 | 2012-03-05 13:15:18 -0800 | [diff] [blame^] | 836 | struct request_queue *q = td->queue; |
| 837 | struct blkio_group *blkg, *n; |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 838 | bool empty = true; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 839 | |
Tejun Heo | 4eef304 | 2012-03-05 13:15:18 -0800 | [diff] [blame^] | 840 | list_for_each_entry_safe(blkg, n, &q->blkg_list[BLKIO_POLICY_THROTL], |
| 841 | q_node[BLKIO_POLICY_THROTL]) { |
| 842 | struct throtl_grp *tg = blkg_to_tg(blkg); |
| 843 | |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 844 | /* skip root? */ |
| 845 | if (!release_root && tg == td->root_tg) |
| 846 | continue; |
| 847 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 848 | /* |
| 849 | * If cgroup removal path got to blk_group first and removed |
| 850 | * it from cgroup list, then it will take care of destroying |
| 851 | * cfqg also. |
| 852 | */ |
Tejun Heo | 4eef304 | 2012-03-05 13:15:18 -0800 | [diff] [blame^] | 853 | if (!blkiocg_del_blkio_group(blkg)) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 854 | throtl_destroy_tg(td, tg); |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 855 | else |
| 856 | empty = false; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 857 | } |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 858 | return empty; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 859 | } |
| 860 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 861 | /* |
| 862 | * Blk cgroup controller notification saying that blkio_group object is being |
| 863 | * delinked as associated cgroup object is going away. That also means that |
| 864 | * no new IO will come in this group. So get rid of this group as soon as |
| 865 | * any pending IO in the group is finished. |
| 866 | * |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 867 | * This function is called under rcu_read_lock(). @q is the rcu protected |
| 868 | * pointer. That means @q is a valid request_queue pointer as long as we |
| 869 | * are rcu read lock. |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 870 | * |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 871 | * @q was fetched from blkio_group under blkio_cgroup->lock. That means |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 872 | * it should not be NULL as even if queue was going away, cgroup deltion |
| 873 | * path got to it first. |
| 874 | */ |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 875 | void throtl_unlink_blkio_group(struct request_queue *q, |
| 876 | struct blkio_group *blkg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 877 | { |
| 878 | unsigned long flags; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 879 | |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 880 | spin_lock_irqsave(q->queue_lock, flags); |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 881 | throtl_destroy_tg(q->td, blkg_to_tg(blkg)); |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 882 | spin_unlock_irqrestore(q->queue_lock, flags); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 883 | } |
| 884 | |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 885 | static bool throtl_clear_queue(struct request_queue *q) |
| 886 | { |
| 887 | lockdep_assert_held(q->queue_lock); |
| 888 | |
| 889 | /* |
| 890 | * Clear tgs but leave the root one alone. This is necessary |
| 891 | * because root_tg is expected to be persistent and safe because |
| 892 | * blk-throtl can never be disabled while @q is alive. This is a |
| 893 | * kludge to prepare for unified blkg. This whole function will be |
| 894 | * removed soon. |
| 895 | */ |
| 896 | return throtl_release_tgs(q->td, false); |
| 897 | } |
| 898 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 899 | static void throtl_update_blkio_group_common(struct throtl_data *td, |
| 900 | struct throtl_grp *tg) |
| 901 | { |
| 902 | xchg(&tg->limits_changed, true); |
| 903 | xchg(&td->limits_changed, true); |
| 904 | /* Schedule a work now to process the limit change */ |
| 905 | throtl_schedule_delayed_work(td, 0); |
| 906 | } |
| 907 | |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 908 | /* |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 909 | * For all update functions, @q should be a valid pointer because these |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 910 | * update functions are called under blkcg_lock, that means, blkg is |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 911 | * valid and in turn @q is valid. queue exit path can not race because |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 912 | * of blkcg_lock |
| 913 | * |
| 914 | * Can not take queue lock in update functions as queue lock under blkcg_lock |
| 915 | * is not allowed. Under other paths we take blkcg_lock under queue_lock. |
| 916 | */ |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 917 | static void throtl_update_blkio_group_read_bps(struct request_queue *q, |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 918 | struct blkio_group *blkg, u64 read_bps) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 919 | { |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 920 | struct throtl_grp *tg = blkg_to_tg(blkg); |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 921 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 922 | tg->bps[READ] = read_bps; |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 923 | throtl_update_blkio_group_common(q->td, tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 924 | } |
| 925 | |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 926 | static void throtl_update_blkio_group_write_bps(struct request_queue *q, |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 927 | struct blkio_group *blkg, u64 write_bps) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 928 | { |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 929 | struct throtl_grp *tg = blkg_to_tg(blkg); |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 930 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 931 | tg->bps[WRITE] = write_bps; |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 932 | throtl_update_blkio_group_common(q->td, tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 933 | } |
| 934 | |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 935 | static void throtl_update_blkio_group_read_iops(struct request_queue *q, |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 936 | struct blkio_group *blkg, unsigned int read_iops) |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 937 | { |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 938 | struct throtl_grp *tg = blkg_to_tg(blkg); |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 939 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 940 | tg->iops[READ] = read_iops; |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 941 | throtl_update_blkio_group_common(q->td, tg); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 942 | } |
| 943 | |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 944 | static void throtl_update_blkio_group_write_iops(struct request_queue *q, |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 945 | struct blkio_group *blkg, unsigned int write_iops) |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 946 | { |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 947 | struct throtl_grp *tg = blkg_to_tg(blkg); |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 948 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 949 | tg->iops[WRITE] = write_iops; |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 950 | throtl_update_blkio_group_common(q->td, tg); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 951 | } |
| 952 | |
Vivek Goyal | da52777 | 2011-03-02 19:05:33 -0500 | [diff] [blame] | 953 | static void throtl_shutdown_wq(struct request_queue *q) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 954 | { |
| 955 | struct throtl_data *td = q->td; |
| 956 | |
| 957 | cancel_delayed_work_sync(&td->throtl_work); |
| 958 | } |
| 959 | |
| 960 | static struct blkio_policy_type blkio_policy_throtl = { |
| 961 | .ops = { |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 962 | .blkio_init_group_fn = throtl_init_blkio_group, |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 963 | .blkio_link_group_fn = throtl_link_blkio_group, |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 964 | .blkio_unlink_group_fn = throtl_unlink_blkio_group, |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 965 | .blkio_clear_queue_fn = throtl_clear_queue, |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 966 | .blkio_update_group_read_bps_fn = |
| 967 | throtl_update_blkio_group_read_bps, |
| 968 | .blkio_update_group_write_bps_fn = |
| 969 | throtl_update_blkio_group_write_bps, |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 970 | .blkio_update_group_read_iops_fn = |
| 971 | throtl_update_blkio_group_read_iops, |
| 972 | .blkio_update_group_write_iops_fn = |
| 973 | throtl_update_blkio_group_write_iops, |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 974 | }, |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 975 | .plid = BLKIO_POLICY_THROTL, |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 976 | .pdata_size = sizeof(struct throtl_grp), |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 977 | }; |
| 978 | |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 979 | bool blk_throtl_bio(struct request_queue *q, struct bio *bio) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 980 | { |
| 981 | struct throtl_data *td = q->td; |
| 982 | struct throtl_grp *tg; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 983 | bool rw = bio_data_dir(bio), update_disptime = true; |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 984 | struct blkio_cgroup *blkcg; |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 985 | bool throttled = false; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 986 | |
| 987 | if (bio->bi_rw & REQ_THROTTLED) { |
| 988 | bio->bi_rw &= ~REQ_THROTTLED; |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 989 | goto out; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 990 | } |
| 991 | |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 992 | /* |
| 993 | * A throtl_grp pointer retrieved under rcu can be used to access |
| 994 | * basic fields like stats and io rates. If a group has no rules, |
| 995 | * just update the dispatch stats in lockless manner and return. |
| 996 | */ |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 997 | rcu_read_lock(); |
| 998 | blkcg = task_blkio_cgroup(current); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 999 | tg = throtl_lookup_tg(td, blkcg); |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 1000 | if (tg) { |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 1001 | if (tg_no_rule_group(tg, rw)) { |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 1002 | blkiocg_update_dispatch_stats(tg_to_blkg(tg), |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 1003 | &blkio_policy_throtl, |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 1004 | bio->bi_size, rw, |
| 1005 | rw_is_sync(bio->bi_rw)); |
Tejun Heo | 2a7f124 | 2012-03-05 13:15:01 -0800 | [diff] [blame] | 1006 | goto out_unlock_rcu; |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 1007 | } |
| 1008 | } |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 1009 | |
| 1010 | /* |
| 1011 | * Either group has not been allocated yet or it is not an unlimited |
| 1012 | * IO group |
| 1013 | */ |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1014 | spin_lock_irq(q->queue_lock); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1015 | tg = throtl_lookup_create_tg(td, blkcg); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1016 | if (unlikely(!tg)) |
| 1017 | goto out_unlock; |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 1018 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1019 | if (tg->nr_queued[rw]) { |
| 1020 | /* |
| 1021 | * There is already another bio queued in same dir. No |
| 1022 | * need to update dispatch time. |
| 1023 | */ |
Vivek Goyal | 231d704 | 2011-03-07 21:05:14 +0100 | [diff] [blame] | 1024 | update_disptime = false; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1025 | goto queue_bio; |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 1026 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | /* Bio is with-in rate limit of group */ |
| 1030 | if (tg_may_dispatch(td, tg, bio, NULL)) { |
| 1031 | throtl_charge_bio(tg, bio); |
Vivek Goyal | 04521db | 2011-03-22 21:54:29 +0100 | [diff] [blame] | 1032 | |
| 1033 | /* |
| 1034 | * We need to trim slice even when bios are not being queued |
| 1035 | * otherwise it might happen that a bio is not queued for |
| 1036 | * a long time and slice keeps on extending and trim is not |
| 1037 | * called for a long time. Now if limits are reduced suddenly |
| 1038 | * we take into account all the IO dispatched so far at new |
| 1039 | * low rate and * newly queued IO gets a really long dispatch |
| 1040 | * time. |
| 1041 | * |
| 1042 | * So keep on trimming slice even if bio is not queued. |
| 1043 | */ |
| 1044 | throtl_trim_slice(td, tg, rw); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1045 | goto out_unlock; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | queue_bio: |
Joe Perches | fd16d26 | 2011-06-13 10:42:49 +0200 | [diff] [blame] | 1049 | throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu" |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 1050 | " iodisp=%u iops=%u queued=%d/%d", |
| 1051 | rw == READ ? 'R' : 'W', |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1052 | tg->bytes_disp[rw], bio->bi_size, tg->bps[rw], |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 1053 | tg->io_disp[rw], tg->iops[rw], |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1054 | tg->nr_queued[READ], tg->nr_queued[WRITE]); |
| 1055 | |
| 1056 | throtl_add_bio_tg(q->td, tg, bio); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1057 | throttled = true; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1058 | |
| 1059 | if (update_disptime) { |
| 1060 | tg_update_disptime(td, tg); |
| 1061 | throtl_schedule_next_dispatch(td); |
| 1062 | } |
| 1063 | |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1064 | out_unlock: |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1065 | spin_unlock_irq(q->queue_lock); |
Tejun Heo | 2a7f124 | 2012-03-05 13:15:01 -0800 | [diff] [blame] | 1066 | out_unlock_rcu: |
| 1067 | rcu_read_unlock(); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1068 | out: |
| 1069 | return throttled; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1070 | } |
| 1071 | |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 1072 | /** |
| 1073 | * blk_throtl_drain - drain throttled bios |
| 1074 | * @q: request_queue to drain throttled bios for |
| 1075 | * |
| 1076 | * Dispatch all currently throttled bios on @q through ->make_request_fn(). |
| 1077 | */ |
| 1078 | void blk_throtl_drain(struct request_queue *q) |
| 1079 | __releases(q->queue_lock) __acquires(q->queue_lock) |
| 1080 | { |
| 1081 | struct throtl_data *td = q->td; |
| 1082 | struct throtl_rb_root *st = &td->tg_service_tree; |
| 1083 | struct throtl_grp *tg; |
| 1084 | struct bio_list bl; |
| 1085 | struct bio *bio; |
| 1086 | |
Jens Axboe | 334c2b0 | 2011-10-25 15:51:48 +0200 | [diff] [blame] | 1087 | WARN_ON_ONCE(!queue_is_locked(q)); |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 1088 | |
| 1089 | bio_list_init(&bl); |
| 1090 | |
| 1091 | while ((tg = throtl_rb_first(st))) { |
| 1092 | throtl_dequeue_tg(td, tg); |
| 1093 | |
| 1094 | while ((bio = bio_list_peek(&tg->bio_lists[READ]))) |
| 1095 | tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl); |
| 1096 | while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))) |
| 1097 | tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl); |
| 1098 | } |
| 1099 | spin_unlock_irq(q->queue_lock); |
| 1100 | |
| 1101 | while ((bio = bio_list_pop(&bl))) |
| 1102 | generic_make_request(bio); |
| 1103 | |
| 1104 | spin_lock_irq(q->queue_lock); |
| 1105 | } |
| 1106 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1107 | int blk_throtl_init(struct request_queue *q) |
| 1108 | { |
| 1109 | struct throtl_data *td; |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1110 | struct blkio_group *blkg; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1111 | |
| 1112 | td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node); |
| 1113 | if (!td) |
| 1114 | return -ENOMEM; |
| 1115 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1116 | td->tg_service_tree = THROTL_RB_ROOT; |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 1117 | td->limits_changed = false; |
Vivek Goyal | a29a171 | 2011-05-19 15:38:19 -0400 | [diff] [blame] | 1118 | INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1119 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1120 | q->td = td; |
Vivek Goyal | 29b1258 | 2011-05-19 15:38:24 -0400 | [diff] [blame] | 1121 | td->queue = q; |
Vivek Goyal | 02977e4 | 2010-10-01 14:49:48 +0200 | [diff] [blame] | 1122 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1123 | /* alloc and init root group. */ |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 1124 | rcu_read_lock(); |
| 1125 | spin_lock_irq(q->queue_lock); |
| 1126 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1127 | blkg = blkg_lookup_create(&blkio_root_cgroup, q, BLKIO_POLICY_THROTL, |
| 1128 | true); |
| 1129 | if (!IS_ERR(blkg)) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 1130 | td->root_tg = blkg_to_tg(blkg); |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 1131 | |
| 1132 | spin_unlock_irq(q->queue_lock); |
| 1133 | rcu_read_unlock(); |
| 1134 | |
| 1135 | if (!td->root_tg) { |
Vivek Goyal | 29b1258 | 2011-05-19 15:38:24 -0400 | [diff] [blame] | 1136 | kfree(td); |
| 1137 | return -ENOMEM; |
| 1138 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1139 | return 0; |
| 1140 | } |
| 1141 | |
| 1142 | void blk_throtl_exit(struct request_queue *q) |
| 1143 | { |
| 1144 | struct throtl_data *td = q->td; |
Tejun Heo | 4eef304 | 2012-03-05 13:15:18 -0800 | [diff] [blame^] | 1145 | bool wait; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1146 | |
| 1147 | BUG_ON(!td); |
| 1148 | |
Vivek Goyal | da52777 | 2011-03-02 19:05:33 -0500 | [diff] [blame] | 1149 | throtl_shutdown_wq(q); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1150 | |
| 1151 | spin_lock_irq(q->queue_lock); |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 1152 | throtl_release_tgs(td, true); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1153 | |
| 1154 | /* If there are other groups */ |
Tejun Heo | 4eef304 | 2012-03-05 13:15:18 -0800 | [diff] [blame^] | 1155 | wait = q->nr_blkgs[BLKIO_POLICY_THROTL]; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1156 | |
| 1157 | spin_unlock_irq(q->queue_lock); |
| 1158 | |
| 1159 | /* |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 1160 | * Wait for tg_to_blkg(tg)->q accessors to exit their grace periods. |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1161 | * Do this wait only if there are other undestroyed groups out |
| 1162 | * there (other than root group). This can happen if cgroup deletion |
| 1163 | * path claimed the responsibility of cleaning up a group before |
| 1164 | * queue cleanup code get to the group. |
| 1165 | * |
| 1166 | * Do not call synchronize_rcu() unconditionally as there are drivers |
| 1167 | * which create/delete request queue hundreds of times during scan/boot |
| 1168 | * and synchronize_rcu() can take significant time and slow down boot. |
| 1169 | */ |
| 1170 | if (wait) |
| 1171 | synchronize_rcu(); |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 1172 | |
| 1173 | /* |
| 1174 | * Just being safe to make sure after previous flush if some body did |
| 1175 | * update limits through cgroup and another work got queued, cancel |
| 1176 | * it. |
| 1177 | */ |
Vivek Goyal | da52777 | 2011-03-02 19:05:33 -0500 | [diff] [blame] | 1178 | throtl_shutdown_wq(q); |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 1179 | |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 1180 | kfree(q->td); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | static int __init throtl_init(void) |
| 1184 | { |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 1185 | kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0); |
| 1186 | if (!kthrotld_workqueue) |
| 1187 | panic("Failed to create kthrotld\n"); |
| 1188 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1189 | blkio_policy_register(&blkio_policy_throtl); |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | module_init(throtl_init); |