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