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 | { |
Tejun Heo | aaec55a | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 110 | return pdata_to_blkg(tg); |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 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 | |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 160 | static struct |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 161 | 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] | 162 | { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 163 | /* |
Vivek Goyal | be2c6b1 | 2011-01-19 08:25:02 -0700 | [diff] [blame] | 164 | * This is the common case when there are no blkio cgroups. |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 165 | * Avoid lookup in this case |
| 166 | */ |
Vivek Goyal | be2c6b1 | 2011-01-19 08:25:02 -0700 | [diff] [blame] | 167 | if (blkcg == &blkio_root_cgroup) |
Tejun Heo | 7a4dd28 | 2012-03-05 13:15:09 -0800 | [diff] [blame] | 168 | return td->root_tg; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 169 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 170 | return blkg_to_tg(blkg_lookup(blkcg, td->queue)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 171 | } |
| 172 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 173 | static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td, |
| 174 | struct blkio_cgroup *blkcg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 175 | { |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 176 | struct request_queue *q = td->queue; |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 177 | struct throtl_grp *tg = NULL; |
Tejun Heo | 0a5a7d0 | 2012-03-05 13:15:02 -0800 | [diff] [blame] | 178 | |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 179 | /* |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 180 | * This is the common case when there are no blkio cgroups. |
| 181 | * Avoid lookup in this case |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 182 | */ |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 183 | if (blkcg == &blkio_root_cgroup) { |
Vivek Goyal | 29b1258 | 2011-05-19 15:38:24 -0400 | [diff] [blame] | 184 | tg = td->root_tg; |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 185 | } else { |
| 186 | struct blkio_group *blkg; |
| 187 | |
Tejun Heo | aaec55a | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 188 | blkg = blkg_lookup_create(blkcg, q, false); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 189 | |
| 190 | /* if %NULL and @q is alive, fall back to root_tg */ |
| 191 | if (!IS_ERR(blkg)) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 192 | tg = blkg_to_tg(blkg); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 193 | else if (!blk_queue_dead(q)) |
| 194 | tg = td->root_tg; |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 195 | } |
| 196 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 197 | return tg; |
| 198 | } |
| 199 | |
| 200 | static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root) |
| 201 | { |
| 202 | /* Service tree is empty */ |
| 203 | if (!root->count) |
| 204 | return NULL; |
| 205 | |
| 206 | if (!root->left) |
| 207 | root->left = rb_first(&root->rb); |
| 208 | |
| 209 | if (root->left) |
| 210 | return rb_entry_tg(root->left); |
| 211 | |
| 212 | return NULL; |
| 213 | } |
| 214 | |
| 215 | static void rb_erase_init(struct rb_node *n, struct rb_root *root) |
| 216 | { |
| 217 | rb_erase(n, root); |
| 218 | RB_CLEAR_NODE(n); |
| 219 | } |
| 220 | |
| 221 | static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root) |
| 222 | { |
| 223 | if (root->left == n) |
| 224 | root->left = NULL; |
| 225 | rb_erase_init(n, &root->rb); |
| 226 | --root->count; |
| 227 | } |
| 228 | |
| 229 | static void update_min_dispatch_time(struct throtl_rb_root *st) |
| 230 | { |
| 231 | struct throtl_grp *tg; |
| 232 | |
| 233 | tg = throtl_rb_first(st); |
| 234 | if (!tg) |
| 235 | return; |
| 236 | |
| 237 | st->min_disptime = tg->disptime; |
| 238 | } |
| 239 | |
| 240 | static void |
| 241 | tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg) |
| 242 | { |
| 243 | struct rb_node **node = &st->rb.rb_node; |
| 244 | struct rb_node *parent = NULL; |
| 245 | struct throtl_grp *__tg; |
| 246 | unsigned long key = tg->disptime; |
| 247 | int left = 1; |
| 248 | |
| 249 | while (*node != NULL) { |
| 250 | parent = *node; |
| 251 | __tg = rb_entry_tg(parent); |
| 252 | |
| 253 | if (time_before(key, __tg->disptime)) |
| 254 | node = &parent->rb_left; |
| 255 | else { |
| 256 | node = &parent->rb_right; |
| 257 | left = 0; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if (left) |
| 262 | st->left = &tg->rb_node; |
| 263 | |
| 264 | rb_link_node(&tg->rb_node, parent, node); |
| 265 | rb_insert_color(&tg->rb_node, &st->rb); |
| 266 | } |
| 267 | |
| 268 | static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg) |
| 269 | { |
| 270 | struct throtl_rb_root *st = &td->tg_service_tree; |
| 271 | |
| 272 | tg_service_tree_add(st, tg); |
| 273 | throtl_mark_tg_on_rr(tg); |
| 274 | st->count++; |
| 275 | } |
| 276 | |
| 277 | static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg) |
| 278 | { |
| 279 | if (!throtl_tg_on_rr(tg)) |
| 280 | __throtl_enqueue_tg(td, tg); |
| 281 | } |
| 282 | |
| 283 | static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg) |
| 284 | { |
| 285 | throtl_rb_erase(&tg->rb_node, &td->tg_service_tree); |
| 286 | throtl_clear_tg_on_rr(tg); |
| 287 | } |
| 288 | |
| 289 | static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg) |
| 290 | { |
| 291 | if (throtl_tg_on_rr(tg)) |
| 292 | __throtl_dequeue_tg(td, tg); |
| 293 | } |
| 294 | |
| 295 | static void throtl_schedule_next_dispatch(struct throtl_data *td) |
| 296 | { |
| 297 | struct throtl_rb_root *st = &td->tg_service_tree; |
| 298 | |
| 299 | /* |
| 300 | * If there are more bios pending, schedule more work. |
| 301 | */ |
| 302 | if (!total_nr_queued(td)) |
| 303 | return; |
| 304 | |
| 305 | BUG_ON(!st->count); |
| 306 | |
| 307 | update_min_dispatch_time(st); |
| 308 | |
| 309 | if (time_before_eq(st->min_disptime, jiffies)) |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 310 | throtl_schedule_delayed_work(td, 0); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 311 | else |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 312 | throtl_schedule_delayed_work(td, (st->min_disptime - jiffies)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static inline void |
| 316 | throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw) |
| 317 | { |
| 318 | tg->bytes_disp[rw] = 0; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 319 | tg->io_disp[rw] = 0; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 320 | tg->slice_start[rw] = jiffies; |
| 321 | tg->slice_end[rw] = jiffies + throtl_slice; |
| 322 | throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu", |
| 323 | rw == READ ? 'R' : 'W', tg->slice_start[rw], |
| 324 | tg->slice_end[rw], jiffies); |
| 325 | } |
| 326 | |
Vivek Goyal | d1ae8ff | 2010-12-01 19:34:46 +0100 | [diff] [blame] | 327 | static inline void throtl_set_slice_end(struct throtl_data *td, |
| 328 | struct throtl_grp *tg, bool rw, unsigned long jiffy_end) |
| 329 | { |
| 330 | tg->slice_end[rw] = roundup(jiffy_end, throtl_slice); |
| 331 | } |
| 332 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 333 | static inline void throtl_extend_slice(struct throtl_data *td, |
| 334 | struct throtl_grp *tg, bool rw, unsigned long jiffy_end) |
| 335 | { |
| 336 | tg->slice_end[rw] = roundup(jiffy_end, throtl_slice); |
| 337 | throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu", |
| 338 | rw == READ ? 'R' : 'W', tg->slice_start[rw], |
| 339 | tg->slice_end[rw], jiffies); |
| 340 | } |
| 341 | |
| 342 | /* Determine if previously allocated or extended slice is complete or not */ |
| 343 | static bool |
| 344 | throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw) |
| 345 | { |
| 346 | if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw])) |
| 347 | return 0; |
| 348 | |
| 349 | return 1; |
| 350 | } |
| 351 | |
| 352 | /* Trim the used slices and adjust slice start accordingly */ |
| 353 | static inline void |
| 354 | throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw) |
| 355 | { |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 356 | unsigned long nr_slices, time_elapsed, io_trim; |
| 357 | u64 bytes_trim, tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 358 | |
| 359 | BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw])); |
| 360 | |
| 361 | /* |
| 362 | * If bps are unlimited (-1), then time slice don't get |
| 363 | * renewed. Don't try to trim the slice if slice is used. A new |
| 364 | * slice will start when appropriate. |
| 365 | */ |
| 366 | if (throtl_slice_used(td, tg, rw)) |
| 367 | return; |
| 368 | |
Vivek Goyal | d1ae8ff | 2010-12-01 19:34:46 +0100 | [diff] [blame] | 369 | /* |
| 370 | * A bio has been dispatched. Also adjust slice_end. It might happen |
| 371 | * that initially cgroup limit was very low resulting in high |
| 372 | * slice_end, but later limit was bumped up and bio was dispached |
| 373 | * sooner, then we need to reduce slice_end. A high bogus slice_end |
| 374 | * is bad because it does not allow new slice to start. |
| 375 | */ |
| 376 | |
| 377 | throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice); |
| 378 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 379 | time_elapsed = jiffies - tg->slice_start[rw]; |
| 380 | |
| 381 | nr_slices = time_elapsed / throtl_slice; |
| 382 | |
| 383 | if (!nr_slices) |
| 384 | return; |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 385 | tmp = tg->bps[rw] * throtl_slice * nr_slices; |
| 386 | do_div(tmp, HZ); |
| 387 | bytes_trim = tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 388 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 389 | io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 390 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 391 | if (!bytes_trim && !io_trim) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 392 | return; |
| 393 | |
| 394 | if (tg->bytes_disp[rw] >= bytes_trim) |
| 395 | tg->bytes_disp[rw] -= bytes_trim; |
| 396 | else |
| 397 | tg->bytes_disp[rw] = 0; |
| 398 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 399 | if (tg->io_disp[rw] >= io_trim) |
| 400 | tg->io_disp[rw] -= io_trim; |
| 401 | else |
| 402 | tg->io_disp[rw] = 0; |
| 403 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 404 | tg->slice_start[rw] += nr_slices * throtl_slice; |
| 405 | |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 406 | 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] | 407 | " start=%lu end=%lu jiffies=%lu", |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 408 | rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim, |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 409 | tg->slice_start[rw], tg->slice_end[rw], jiffies); |
| 410 | } |
| 411 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 412 | static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg, |
| 413 | struct bio *bio, unsigned long *wait) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 414 | { |
| 415 | bool rw = bio_data_dir(bio); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 416 | unsigned int io_allowed; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 417 | unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd; |
Vivek Goyal | c49c06e | 2010-10-01 21:16:42 +0200 | [diff] [blame] | 418 | u64 tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 419 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 420 | jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw]; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 421 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 422 | /* Slice has just started. Consider one slice interval */ |
| 423 | if (!jiffy_elapsed) |
| 424 | jiffy_elapsed_rnd = throtl_slice; |
| 425 | |
| 426 | jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice); |
| 427 | |
Vivek Goyal | c49c06e | 2010-10-01 21:16:42 +0200 | [diff] [blame] | 428 | /* |
| 429 | * jiffy_elapsed_rnd should not be a big value as minimum iops can be |
| 430 | * 1 then at max jiffy elapsed should be equivalent of 1 second as we |
| 431 | * will allow dispatch after 1 second and after that slice should |
| 432 | * have been trimmed. |
| 433 | */ |
| 434 | |
| 435 | tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd; |
| 436 | do_div(tmp, HZ); |
| 437 | |
| 438 | if (tmp > UINT_MAX) |
| 439 | io_allowed = UINT_MAX; |
| 440 | else |
| 441 | io_allowed = tmp; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 442 | |
| 443 | if (tg->io_disp[rw] + 1 <= io_allowed) { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 444 | if (wait) |
| 445 | *wait = 0; |
| 446 | return 1; |
| 447 | } |
| 448 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 449 | /* Calc approx time to dispatch */ |
| 450 | jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1; |
| 451 | |
| 452 | if (jiffy_wait > jiffy_elapsed) |
| 453 | jiffy_wait = jiffy_wait - jiffy_elapsed; |
| 454 | else |
| 455 | jiffy_wait = 1; |
| 456 | |
| 457 | if (wait) |
| 458 | *wait = jiffy_wait; |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg, |
| 463 | struct bio *bio, unsigned long *wait) |
| 464 | { |
| 465 | bool rw = bio_data_dir(bio); |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 466 | u64 bytes_allowed, extra_bytes, tmp; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 467 | unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 468 | |
| 469 | jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw]; |
| 470 | |
| 471 | /* Slice has just started. Consider one slice interval */ |
| 472 | if (!jiffy_elapsed) |
| 473 | jiffy_elapsed_rnd = throtl_slice; |
| 474 | |
| 475 | jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice); |
| 476 | |
Vivek Goyal | 5e901a2 | 2010-10-01 21:16:38 +0200 | [diff] [blame] | 477 | tmp = tg->bps[rw] * jiffy_elapsed_rnd; |
| 478 | do_div(tmp, HZ); |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 479 | bytes_allowed = tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 480 | |
| 481 | if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) { |
| 482 | if (wait) |
| 483 | *wait = 0; |
| 484 | return 1; |
| 485 | } |
| 486 | |
| 487 | /* Calc approx time to dispatch */ |
| 488 | extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed; |
| 489 | jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]); |
| 490 | |
| 491 | if (!jiffy_wait) |
| 492 | jiffy_wait = 1; |
| 493 | |
| 494 | /* |
| 495 | * This wait time is without taking into consideration the rounding |
| 496 | * up we did. Add that time also. |
| 497 | */ |
| 498 | jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 499 | if (wait) |
| 500 | *wait = jiffy_wait; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 501 | return 0; |
| 502 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 503 | |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 504 | static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) { |
| 505 | if (tg->bps[rw] == -1 && tg->iops[rw] == -1) |
| 506 | return 1; |
| 507 | return 0; |
| 508 | } |
| 509 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 510 | /* |
| 511 | * Returns whether one can dispatch a bio or not. Also returns approx number |
| 512 | * of jiffies to wait before this bio is with-in IO rate and can be dispatched |
| 513 | */ |
| 514 | static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg, |
| 515 | struct bio *bio, unsigned long *wait) |
| 516 | { |
| 517 | bool rw = bio_data_dir(bio); |
| 518 | unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0; |
| 519 | |
| 520 | /* |
| 521 | * Currently whole state machine of group depends on first bio |
| 522 | * queued in the group bio list. So one should not be calling |
| 523 | * this function with a different bio if there are other bios |
| 524 | * queued. |
| 525 | */ |
| 526 | BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw])); |
| 527 | |
| 528 | /* If tg->bps = -1, then BW is unlimited */ |
| 529 | if (tg->bps[rw] == -1 && tg->iops[rw] == -1) { |
| 530 | if (wait) |
| 531 | *wait = 0; |
| 532 | return 1; |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * If previous slice expired, start a new one otherwise renew/extend |
| 537 | * existing slice to make sure it is at least throtl_slice interval |
| 538 | * long since now. |
| 539 | */ |
| 540 | if (throtl_slice_used(td, tg, rw)) |
| 541 | throtl_start_new_slice(td, tg, rw); |
| 542 | else { |
| 543 | if (time_before(tg->slice_end[rw], jiffies + throtl_slice)) |
| 544 | throtl_extend_slice(td, tg, rw, jiffies + throtl_slice); |
| 545 | } |
| 546 | |
| 547 | if (tg_with_in_bps_limit(td, tg, bio, &bps_wait) |
| 548 | && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) { |
| 549 | if (wait) |
| 550 | *wait = 0; |
| 551 | return 1; |
| 552 | } |
| 553 | |
| 554 | max_wait = max(bps_wait, iops_wait); |
| 555 | |
| 556 | if (wait) |
| 557 | *wait = max_wait; |
| 558 | |
| 559 | if (time_before(tg->slice_end[rw], jiffies + max_wait)) |
| 560 | throtl_extend_slice(td, tg, rw, jiffies + max_wait); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 561 | |
| 562 | return 0; |
| 563 | } |
| 564 | |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 565 | static void throtl_update_dispatch_stats(struct blkio_group *blkg, u64 bytes, |
| 566 | int rw) |
| 567 | { |
| 568 | struct blkg_policy_data *pd = blkg->pd[BLKIO_POLICY_THROTL]; |
| 569 | struct blkio_group_stats_cpu *stats_cpu; |
| 570 | unsigned long flags; |
| 571 | |
| 572 | /* If per cpu stats are not allocated yet, don't do any accounting. */ |
| 573 | if (pd->stats_cpu == NULL) |
| 574 | return; |
| 575 | |
| 576 | /* |
| 577 | * Disabling interrupts to provide mutual exclusion between two |
| 578 | * writes on same cpu. It probably is not needed for 64bit. Not |
| 579 | * optimizing that case yet. |
| 580 | */ |
| 581 | local_irq_save(flags); |
| 582 | |
| 583 | stats_cpu = this_cpu_ptr(pd->stats_cpu); |
| 584 | |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 585 | blkg_rwstat_add(&stats_cpu->serviced, rw, 1); |
| 586 | blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes); |
| 587 | |
| 588 | local_irq_restore(flags); |
| 589 | } |
| 590 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 591 | static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio) |
| 592 | { |
| 593 | bool rw = bio_data_dir(bio); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 594 | |
| 595 | /* Charge the bio to the group */ |
| 596 | tg->bytes_disp[rw] += bio->bi_size; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 597 | tg->io_disp[rw]++; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 598 | |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 599 | throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, bio->bi_rw); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg, |
| 603 | struct bio *bio) |
| 604 | { |
| 605 | bool rw = bio_data_dir(bio); |
| 606 | |
| 607 | bio_list_add(&tg->bio_lists[rw], bio); |
| 608 | /* Take a bio reference on tg */ |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 609 | blkg_get(tg_to_blkg(tg)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 610 | tg->nr_queued[rw]++; |
| 611 | td->nr_queued[rw]++; |
| 612 | throtl_enqueue_tg(td, tg); |
| 613 | } |
| 614 | |
| 615 | static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg) |
| 616 | { |
| 617 | unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime; |
| 618 | struct bio *bio; |
| 619 | |
| 620 | if ((bio = bio_list_peek(&tg->bio_lists[READ]))) |
| 621 | tg_may_dispatch(td, tg, bio, &read_wait); |
| 622 | |
| 623 | if ((bio = bio_list_peek(&tg->bio_lists[WRITE]))) |
| 624 | tg_may_dispatch(td, tg, bio, &write_wait); |
| 625 | |
| 626 | min_wait = min(read_wait, write_wait); |
| 627 | disptime = jiffies + min_wait; |
| 628 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 629 | /* Update dispatch time */ |
| 630 | throtl_dequeue_tg(td, tg); |
| 631 | tg->disptime = disptime; |
| 632 | throtl_enqueue_tg(td, tg); |
| 633 | } |
| 634 | |
| 635 | static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg, |
| 636 | bool rw, struct bio_list *bl) |
| 637 | { |
| 638 | struct bio *bio; |
| 639 | |
| 640 | bio = bio_list_pop(&tg->bio_lists[rw]); |
| 641 | tg->nr_queued[rw]--; |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 642 | /* Drop bio reference on blkg */ |
| 643 | blkg_put(tg_to_blkg(tg)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 644 | |
| 645 | BUG_ON(td->nr_queued[rw] <= 0); |
| 646 | td->nr_queued[rw]--; |
| 647 | |
| 648 | throtl_charge_bio(tg, bio); |
| 649 | bio_list_add(bl, bio); |
| 650 | bio->bi_rw |= REQ_THROTTLED; |
| 651 | |
| 652 | throtl_trim_slice(td, tg, rw); |
| 653 | } |
| 654 | |
| 655 | static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg, |
| 656 | struct bio_list *bl) |
| 657 | { |
| 658 | unsigned int nr_reads = 0, nr_writes = 0; |
| 659 | unsigned int max_nr_reads = throtl_grp_quantum*3/4; |
Vivek Goyal | c2f6805 | 2010-11-15 19:32:42 +0100 | [diff] [blame] | 660 | unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 661 | struct bio *bio; |
| 662 | |
| 663 | /* Try to dispatch 75% READS and 25% WRITES */ |
| 664 | |
| 665 | while ((bio = bio_list_peek(&tg->bio_lists[READ])) |
| 666 | && tg_may_dispatch(td, tg, bio, NULL)) { |
| 667 | |
| 668 | tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl); |
| 669 | nr_reads++; |
| 670 | |
| 671 | if (nr_reads >= max_nr_reads) |
| 672 | break; |
| 673 | } |
| 674 | |
| 675 | while ((bio = bio_list_peek(&tg->bio_lists[WRITE])) |
| 676 | && tg_may_dispatch(td, tg, bio, NULL)) { |
| 677 | |
| 678 | tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl); |
| 679 | nr_writes++; |
| 680 | |
| 681 | if (nr_writes >= max_nr_writes) |
| 682 | break; |
| 683 | } |
| 684 | |
| 685 | return nr_reads + nr_writes; |
| 686 | } |
| 687 | |
| 688 | static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl) |
| 689 | { |
| 690 | unsigned int nr_disp = 0; |
| 691 | struct throtl_grp *tg; |
| 692 | struct throtl_rb_root *st = &td->tg_service_tree; |
| 693 | |
| 694 | while (1) { |
| 695 | tg = throtl_rb_first(st); |
| 696 | |
| 697 | if (!tg) |
| 698 | break; |
| 699 | |
| 700 | if (time_before(jiffies, tg->disptime)) |
| 701 | break; |
| 702 | |
| 703 | throtl_dequeue_tg(td, tg); |
| 704 | |
| 705 | nr_disp += throtl_dispatch_tg(td, tg, bl); |
| 706 | |
| 707 | if (tg->nr_queued[0] || tg->nr_queued[1]) { |
| 708 | tg_update_disptime(td, tg); |
| 709 | throtl_enqueue_tg(td, tg); |
| 710 | } |
| 711 | |
| 712 | if (nr_disp >= throtl_quantum) |
| 713 | break; |
| 714 | } |
| 715 | |
| 716 | return nr_disp; |
| 717 | } |
| 718 | |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 719 | static void throtl_process_limit_change(struct throtl_data *td) |
| 720 | { |
Tejun Heo | 4eef304 | 2012-03-05 13:15:18 -0800 | [diff] [blame] | 721 | struct request_queue *q = td->queue; |
| 722 | struct blkio_group *blkg, *n; |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 723 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 724 | if (!td->limits_changed) |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 725 | return; |
| 726 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 727 | xchg(&td->limits_changed, false); |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 728 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 729 | throtl_log(td, "limits changed"); |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 730 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 731 | list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) { |
Tejun Heo | 4eef304 | 2012-03-05 13:15:18 -0800 | [diff] [blame] | 732 | struct throtl_grp *tg = blkg_to_tg(blkg); |
| 733 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 734 | if (!tg->limits_changed) |
| 735 | continue; |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 736 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 737 | if (!xchg(&tg->limits_changed, false)) |
| 738 | continue; |
| 739 | |
| 740 | throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu" |
| 741 | " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE], |
| 742 | tg->iops[READ], tg->iops[WRITE]); |
| 743 | |
Vivek Goyal | 04521db | 2011-03-22 21:54:29 +0100 | [diff] [blame] | 744 | /* |
| 745 | * Restart the slices for both READ and WRITES. It |
| 746 | * might happen that a group's limit are dropped |
| 747 | * suddenly and we don't want to account recently |
| 748 | * dispatched IO with new low rate |
| 749 | */ |
| 750 | throtl_start_new_slice(td, tg, 0); |
| 751 | throtl_start_new_slice(td, tg, 1); |
| 752 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 753 | if (throtl_tg_on_rr(tg)) |
| 754 | tg_update_disptime(td, tg); |
| 755 | } |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 756 | } |
| 757 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 758 | /* Dispatch throttled bios. Should be called without queue lock held. */ |
| 759 | static int throtl_dispatch(struct request_queue *q) |
| 760 | { |
| 761 | struct throtl_data *td = q->td; |
| 762 | unsigned int nr_disp = 0; |
| 763 | struct bio_list bio_list_on_stack; |
| 764 | struct bio *bio; |
Vivek Goyal | 69d60eb | 2011-03-09 08:27:37 +0100 | [diff] [blame] | 765 | struct blk_plug plug; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 766 | |
| 767 | spin_lock_irq(q->queue_lock); |
| 768 | |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 769 | throtl_process_limit_change(td); |
| 770 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 771 | if (!total_nr_queued(td)) |
| 772 | goto out; |
| 773 | |
| 774 | bio_list_init(&bio_list_on_stack); |
| 775 | |
Joe Perches | d2f31a5 | 2011-06-13 20:19:27 +0200 | [diff] [blame] | 776 | throtl_log(td, "dispatch nr_queued=%u read=%u write=%u", |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 777 | total_nr_queued(td), td->nr_queued[READ], |
| 778 | td->nr_queued[WRITE]); |
| 779 | |
| 780 | nr_disp = throtl_select_dispatch(td, &bio_list_on_stack); |
| 781 | |
| 782 | if (nr_disp) |
| 783 | throtl_log(td, "bios disp=%u", nr_disp); |
| 784 | |
| 785 | throtl_schedule_next_dispatch(td); |
| 786 | out: |
| 787 | spin_unlock_irq(q->queue_lock); |
| 788 | |
| 789 | /* |
| 790 | * If we dispatched some requests, unplug the queue to make sure |
| 791 | * immediate dispatch |
| 792 | */ |
| 793 | if (nr_disp) { |
Vivek Goyal | 69d60eb | 2011-03-09 08:27:37 +0100 | [diff] [blame] | 794 | blk_start_plug(&plug); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 795 | while((bio = bio_list_pop(&bio_list_on_stack))) |
| 796 | generic_make_request(bio); |
Vivek Goyal | 69d60eb | 2011-03-09 08:27:37 +0100 | [diff] [blame] | 797 | blk_finish_plug(&plug); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 798 | } |
| 799 | return nr_disp; |
| 800 | } |
| 801 | |
| 802 | void blk_throtl_work(struct work_struct *work) |
| 803 | { |
| 804 | struct throtl_data *td = container_of(work, struct throtl_data, |
| 805 | throtl_work.work); |
| 806 | struct request_queue *q = td->queue; |
| 807 | |
| 808 | throtl_dispatch(q); |
| 809 | } |
| 810 | |
| 811 | /* Call with queue lock held */ |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 812 | static void |
| 813 | throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 814 | { |
| 815 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 816 | struct delayed_work *dwork = &td->throtl_work; |
| 817 | |
Vivek Goyal | 04521db | 2011-03-22 21:54:29 +0100 | [diff] [blame] | 818 | /* schedule work if limits changed even if no bio is queued */ |
Joe Perches | d2f31a5 | 2011-06-13 20:19:27 +0200 | [diff] [blame] | 819 | if (total_nr_queued(td) || td->limits_changed) { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 820 | /* |
| 821 | * We might have a work scheduled to be executed in future. |
| 822 | * Cancel that and schedule a new one. |
| 823 | */ |
| 824 | __cancel_delayed_work(dwork); |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 825 | queue_delayed_work(kthrotld_workqueue, dwork, delay); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 826 | throtl_log(td, "schedule work. delay=%lu jiffies=%lu", |
| 827 | delay, jiffies); |
| 828 | } |
| 829 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 830 | |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 831 | /* |
| 832 | * Can not take queue lock in update functions as queue lock under |
| 833 | * blkcg_lock is not allowed. Under other paths we take blkcg_lock under |
| 834 | * queue_lock. |
| 835 | */ |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 836 | static void throtl_update_blkio_group_common(struct throtl_data *td, |
| 837 | struct throtl_grp *tg) |
| 838 | { |
| 839 | xchg(&tg->limits_changed, true); |
| 840 | xchg(&td->limits_changed, true); |
| 841 | /* Schedule a work now to process the limit change */ |
| 842 | throtl_schedule_delayed_work(td, 0); |
| 843 | } |
| 844 | |
Tejun Heo | 41b38b6 | 2012-04-01 14:38:44 -0700 | [diff] [blame^] | 845 | static u64 blkg_prfill_cpu_rwstat(struct seq_file *sf, |
| 846 | struct blkg_policy_data *pd, int off) |
| 847 | { |
| 848 | struct blkg_rwstat rwstat = { }, tmp; |
| 849 | int i, cpu; |
| 850 | |
| 851 | for_each_possible_cpu(cpu) { |
| 852 | struct blkio_group_stats_cpu *sc = |
| 853 | per_cpu_ptr(pd->stats_cpu, cpu); |
| 854 | |
| 855 | tmp = blkg_rwstat_read((void *)sc + off); |
| 856 | for (i = 0; i < BLKG_RWSTAT_NR; i++) |
| 857 | rwstat.cnt[i] += tmp.cnt[i]; |
| 858 | } |
| 859 | |
| 860 | return __blkg_prfill_rwstat(sf, pd, &rwstat); |
| 861 | } |
| 862 | |
| 863 | /* print per-cpu blkg_rwstat specified by BLKCG_STAT_PRIV() */ |
| 864 | static int blkcg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft, |
| 865 | struct seq_file *sf) |
| 866 | { |
| 867 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); |
| 868 | |
| 869 | blkcg_print_blkgs(sf, blkcg, blkg_prfill_cpu_rwstat, |
| 870 | BLKCG_STAT_POL(cft->private), |
| 871 | BLKCG_STAT_OFF(cft->private), true); |
| 872 | return 0; |
| 873 | } |
| 874 | |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 875 | static u64 blkg_prfill_conf_u64(struct seq_file *sf, |
| 876 | struct blkg_policy_data *pd, int off) |
| 877 | { |
| 878 | u64 v = *(u64 *)((void *)&pd->conf + off); |
| 879 | |
| 880 | if (!v) |
| 881 | return 0; |
| 882 | return __blkg_prfill_u64(sf, pd, v); |
| 883 | } |
| 884 | |
| 885 | static int blkcg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft, |
| 886 | struct seq_file *sf) |
| 887 | { |
| 888 | blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp), |
| 889 | blkg_prfill_conf_u64, BLKIO_POLICY_THROTL, |
| 890 | cft->private, false); |
| 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | static void throtl_update_blkio_group_read_bps(struct blkio_group *blkg, |
| 895 | u64 read_bps) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 896 | { |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 897 | struct throtl_grp *tg = blkg_to_tg(blkg); |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 898 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 899 | tg->bps[READ] = read_bps; |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 900 | throtl_update_blkio_group_common(blkg->q->td, tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 901 | } |
| 902 | |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 903 | static void throtl_update_blkio_group_write_bps(struct blkio_group *blkg, |
| 904 | u64 write_bps) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 905 | { |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 906 | struct throtl_grp *tg = blkg_to_tg(blkg); |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 907 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 908 | tg->bps[WRITE] = write_bps; |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 909 | throtl_update_blkio_group_common(blkg->q->td, tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 910 | } |
| 911 | |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 912 | static void throtl_update_blkio_group_read_iops(struct blkio_group *blkg, |
| 913 | u64 read_iops) |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 914 | { |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 915 | struct throtl_grp *tg = blkg_to_tg(blkg); |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 916 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 917 | tg->iops[READ] = read_iops; |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 918 | throtl_update_blkio_group_common(blkg->q->td, tg); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 919 | } |
| 920 | |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 921 | static void throtl_update_blkio_group_write_iops(struct blkio_group *blkg, |
| 922 | u64 write_iops) |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 923 | { |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 924 | struct throtl_grp *tg = blkg_to_tg(blkg); |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 925 | |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 926 | tg->iops[WRITE] = write_iops; |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 927 | throtl_update_blkio_group_common(blkg->q->td, tg); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 928 | } |
| 929 | |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 930 | static int blkcg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft, |
| 931 | const char *buf, |
| 932 | void (*update)(struct blkio_group *, u64)) |
| 933 | { |
| 934 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); |
| 935 | struct blkg_policy_data *pd; |
| 936 | struct blkg_conf_ctx ctx; |
| 937 | int ret; |
| 938 | |
| 939 | ret = blkg_conf_prep(blkcg, buf, &ctx); |
| 940 | if (ret) |
| 941 | return ret; |
| 942 | |
| 943 | ret = -EINVAL; |
| 944 | pd = ctx.blkg->pd[BLKIO_POLICY_THROTL]; |
| 945 | if (pd) { |
| 946 | *(u64 *)((void *)&pd->conf + cft->private) = ctx.v; |
| 947 | update(ctx.blkg, ctx.v ?: -1); |
| 948 | ret = 0; |
| 949 | } |
| 950 | |
| 951 | blkg_conf_finish(&ctx); |
| 952 | return ret; |
| 953 | } |
| 954 | |
| 955 | static int blkcg_set_conf_bps_r(struct cgroup *cgrp, struct cftype *cft, |
| 956 | const char *buf) |
| 957 | { |
| 958 | return blkcg_set_conf_u64(cgrp, cft, buf, |
| 959 | throtl_update_blkio_group_read_bps); |
| 960 | } |
| 961 | |
| 962 | static int blkcg_set_conf_bps_w(struct cgroup *cgrp, struct cftype *cft, |
| 963 | const char *buf) |
| 964 | { |
| 965 | return blkcg_set_conf_u64(cgrp, cft, buf, |
| 966 | throtl_update_blkio_group_write_bps); |
| 967 | } |
| 968 | |
| 969 | static int blkcg_set_conf_iops_r(struct cgroup *cgrp, struct cftype *cft, |
| 970 | const char *buf) |
| 971 | { |
| 972 | return blkcg_set_conf_u64(cgrp, cft, buf, |
| 973 | throtl_update_blkio_group_read_iops); |
| 974 | } |
| 975 | |
| 976 | static int blkcg_set_conf_iops_w(struct cgroup *cgrp, struct cftype *cft, |
| 977 | const char *buf) |
| 978 | { |
| 979 | return blkcg_set_conf_u64(cgrp, cft, buf, |
| 980 | throtl_update_blkio_group_write_iops); |
| 981 | } |
| 982 | |
| 983 | static struct cftype throtl_files[] = { |
| 984 | { |
| 985 | .name = "throttle.read_bps_device", |
| 986 | .private = offsetof(struct blkio_group_conf, bps[READ]), |
| 987 | .read_seq_string = blkcg_print_conf_u64, |
| 988 | .write_string = blkcg_set_conf_bps_r, |
| 989 | .max_write_len = 256, |
| 990 | }, |
| 991 | { |
| 992 | .name = "throttle.write_bps_device", |
| 993 | .private = offsetof(struct blkio_group_conf, bps[WRITE]), |
| 994 | .read_seq_string = blkcg_print_conf_u64, |
| 995 | .write_string = blkcg_set_conf_bps_w, |
| 996 | .max_write_len = 256, |
| 997 | }, |
| 998 | { |
| 999 | .name = "throttle.read_iops_device", |
| 1000 | .private = offsetof(struct blkio_group_conf, iops[READ]), |
| 1001 | .read_seq_string = blkcg_print_conf_u64, |
| 1002 | .write_string = blkcg_set_conf_iops_r, |
| 1003 | .max_write_len = 256, |
| 1004 | }, |
| 1005 | { |
| 1006 | .name = "throttle.write_iops_device", |
| 1007 | .private = offsetof(struct blkio_group_conf, iops[WRITE]), |
| 1008 | .read_seq_string = blkcg_print_conf_u64, |
| 1009 | .write_string = blkcg_set_conf_iops_w, |
| 1010 | .max_write_len = 256, |
| 1011 | }, |
| 1012 | { |
| 1013 | .name = "throttle.io_service_bytes", |
| 1014 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL, |
| 1015 | offsetof(struct blkio_group_stats_cpu, service_bytes)), |
| 1016 | .read_seq_string = blkcg_print_cpu_rwstat, |
| 1017 | }, |
| 1018 | { |
| 1019 | .name = "throttle.io_serviced", |
| 1020 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL, |
| 1021 | offsetof(struct blkio_group_stats_cpu, serviced)), |
| 1022 | .read_seq_string = blkcg_print_cpu_rwstat, |
| 1023 | }, |
| 1024 | { } /* terminate */ |
| 1025 | }; |
| 1026 | |
Vivek Goyal | da52777 | 2011-03-02 19:05:33 -0500 | [diff] [blame] | 1027 | static void throtl_shutdown_wq(struct request_queue *q) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1028 | { |
| 1029 | struct throtl_data *td = q->td; |
| 1030 | |
| 1031 | cancel_delayed_work_sync(&td->throtl_work); |
| 1032 | } |
| 1033 | |
| 1034 | static struct blkio_policy_type blkio_policy_throtl = { |
| 1035 | .ops = { |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 1036 | .blkio_init_group_fn = throtl_init_blkio_group, |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1037 | }, |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 1038 | .plid = BLKIO_POLICY_THROTL, |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 1039 | .pdata_size = sizeof(struct throtl_grp), |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1040 | .cftypes = throtl_files, |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1041 | }; |
| 1042 | |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1043 | bool blk_throtl_bio(struct request_queue *q, struct bio *bio) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1044 | { |
| 1045 | struct throtl_data *td = q->td; |
| 1046 | struct throtl_grp *tg; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1047 | bool rw = bio_data_dir(bio), update_disptime = true; |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 1048 | struct blkio_cgroup *blkcg; |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1049 | bool throttled = false; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1050 | |
| 1051 | if (bio->bi_rw & REQ_THROTTLED) { |
| 1052 | bio->bi_rw &= ~REQ_THROTTLED; |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1053 | goto out; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1054 | } |
| 1055 | |
Tejun Heo | 671058f | 2012-03-05 13:15:29 -0800 | [diff] [blame] | 1056 | /* bio_associate_current() needs ioc, try creating */ |
| 1057 | create_io_context(GFP_ATOMIC, q->node); |
| 1058 | |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 1059 | /* |
| 1060 | * A throtl_grp pointer retrieved under rcu can be used to access |
| 1061 | * basic fields like stats and io rates. If a group has no rules, |
| 1062 | * just update the dispatch stats in lockless manner and return. |
| 1063 | */ |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 1064 | rcu_read_lock(); |
Tejun Heo | 4f85cb9 | 2012-03-05 13:15:28 -0800 | [diff] [blame] | 1065 | blkcg = bio_blkio_cgroup(bio); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1066 | tg = throtl_lookup_tg(td, blkcg); |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 1067 | if (tg) { |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 1068 | if (tg_no_rule_group(tg, rw)) { |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1069 | throtl_update_dispatch_stats(tg_to_blkg(tg), |
| 1070 | bio->bi_size, bio->bi_rw); |
Tejun Heo | 2a7f124 | 2012-03-05 13:15:01 -0800 | [diff] [blame] | 1071 | goto out_unlock_rcu; |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 1072 | } |
| 1073 | } |
Vivek Goyal | af75cd3 | 2011-05-19 15:38:31 -0400 | [diff] [blame] | 1074 | |
| 1075 | /* |
| 1076 | * Either group has not been allocated yet or it is not an unlimited |
| 1077 | * IO group |
| 1078 | */ |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1079 | spin_lock_irq(q->queue_lock); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1080 | tg = throtl_lookup_create_tg(td, blkcg); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1081 | if (unlikely(!tg)) |
| 1082 | goto out_unlock; |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 1083 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1084 | if (tg->nr_queued[rw]) { |
| 1085 | /* |
| 1086 | * There is already another bio queued in same dir. No |
| 1087 | * need to update dispatch time. |
| 1088 | */ |
Vivek Goyal | 231d704 | 2011-03-07 21:05:14 +0100 | [diff] [blame] | 1089 | update_disptime = false; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1090 | goto queue_bio; |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 1091 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | /* Bio is with-in rate limit of group */ |
| 1095 | if (tg_may_dispatch(td, tg, bio, NULL)) { |
| 1096 | throtl_charge_bio(tg, bio); |
Vivek Goyal | 04521db | 2011-03-22 21:54:29 +0100 | [diff] [blame] | 1097 | |
| 1098 | /* |
| 1099 | * We need to trim slice even when bios are not being queued |
| 1100 | * otherwise it might happen that a bio is not queued for |
| 1101 | * a long time and slice keeps on extending and trim is not |
| 1102 | * called for a long time. Now if limits are reduced suddenly |
| 1103 | * we take into account all the IO dispatched so far at new |
| 1104 | * low rate and * newly queued IO gets a really long dispatch |
| 1105 | * time. |
| 1106 | * |
| 1107 | * So keep on trimming slice even if bio is not queued. |
| 1108 | */ |
| 1109 | throtl_trim_slice(td, tg, rw); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1110 | goto out_unlock; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | queue_bio: |
Joe Perches | fd16d26 | 2011-06-13 10:42:49 +0200 | [diff] [blame] | 1114 | 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] | 1115 | " iodisp=%u iops=%u queued=%d/%d", |
| 1116 | rw == READ ? 'R' : 'W', |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1117 | tg->bytes_disp[rw], bio->bi_size, tg->bps[rw], |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 1118 | tg->io_disp[rw], tg->iops[rw], |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1119 | tg->nr_queued[READ], tg->nr_queued[WRITE]); |
| 1120 | |
Tejun Heo | 671058f | 2012-03-05 13:15:29 -0800 | [diff] [blame] | 1121 | bio_associate_current(bio); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1122 | throtl_add_bio_tg(q->td, tg, bio); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1123 | throttled = true; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1124 | |
| 1125 | if (update_disptime) { |
| 1126 | tg_update_disptime(td, tg); |
| 1127 | throtl_schedule_next_dispatch(td); |
| 1128 | } |
| 1129 | |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1130 | out_unlock: |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1131 | spin_unlock_irq(q->queue_lock); |
Tejun Heo | 2a7f124 | 2012-03-05 13:15:01 -0800 | [diff] [blame] | 1132 | out_unlock_rcu: |
| 1133 | rcu_read_unlock(); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1134 | out: |
| 1135 | return throttled; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1136 | } |
| 1137 | |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 1138 | /** |
| 1139 | * blk_throtl_drain - drain throttled bios |
| 1140 | * @q: request_queue to drain throttled bios for |
| 1141 | * |
| 1142 | * Dispatch all currently throttled bios on @q through ->make_request_fn(). |
| 1143 | */ |
| 1144 | void blk_throtl_drain(struct request_queue *q) |
| 1145 | __releases(q->queue_lock) __acquires(q->queue_lock) |
| 1146 | { |
| 1147 | struct throtl_data *td = q->td; |
| 1148 | struct throtl_rb_root *st = &td->tg_service_tree; |
| 1149 | struct throtl_grp *tg; |
| 1150 | struct bio_list bl; |
| 1151 | struct bio *bio; |
| 1152 | |
Jens Axboe | 334c2b0 | 2011-10-25 15:51:48 +0200 | [diff] [blame] | 1153 | WARN_ON_ONCE(!queue_is_locked(q)); |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 1154 | |
| 1155 | bio_list_init(&bl); |
| 1156 | |
| 1157 | while ((tg = throtl_rb_first(st))) { |
| 1158 | throtl_dequeue_tg(td, tg); |
| 1159 | |
| 1160 | while ((bio = bio_list_peek(&tg->bio_lists[READ]))) |
| 1161 | tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl); |
| 1162 | while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))) |
| 1163 | tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl); |
| 1164 | } |
| 1165 | spin_unlock_irq(q->queue_lock); |
| 1166 | |
| 1167 | while ((bio = bio_list_pop(&bl))) |
| 1168 | generic_make_request(bio); |
| 1169 | |
| 1170 | spin_lock_irq(q->queue_lock); |
| 1171 | } |
| 1172 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1173 | int blk_throtl_init(struct request_queue *q) |
| 1174 | { |
| 1175 | struct throtl_data *td; |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1176 | struct blkio_group *blkg; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1177 | |
| 1178 | td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node); |
| 1179 | if (!td) |
| 1180 | return -ENOMEM; |
| 1181 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1182 | td->tg_service_tree = THROTL_RB_ROOT; |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 1183 | td->limits_changed = false; |
Vivek Goyal | a29a171 | 2011-05-19 15:38:19 -0400 | [diff] [blame] | 1184 | INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1185 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1186 | q->td = td; |
Vivek Goyal | 29b1258 | 2011-05-19 15:38:24 -0400 | [diff] [blame] | 1187 | td->queue = q; |
Vivek Goyal | 02977e4 | 2010-10-01 14:49:48 +0200 | [diff] [blame] | 1188 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1189 | /* alloc and init root group. */ |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 1190 | rcu_read_lock(); |
| 1191 | spin_lock_irq(q->queue_lock); |
| 1192 | |
Tejun Heo | aaec55a | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 1193 | blkg = blkg_lookup_create(&blkio_root_cgroup, q, true); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1194 | if (!IS_ERR(blkg)) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 1195 | td->root_tg = blkg_to_tg(blkg); |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 1196 | |
| 1197 | spin_unlock_irq(q->queue_lock); |
| 1198 | rcu_read_unlock(); |
| 1199 | |
| 1200 | if (!td->root_tg) { |
Vivek Goyal | 29b1258 | 2011-05-19 15:38:24 -0400 | [diff] [blame] | 1201 | kfree(td); |
| 1202 | return -ENOMEM; |
| 1203 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1204 | return 0; |
| 1205 | } |
| 1206 | |
| 1207 | void blk_throtl_exit(struct request_queue *q) |
| 1208 | { |
Tejun Heo | c875f4d | 2012-03-05 13:15:22 -0800 | [diff] [blame] | 1209 | BUG_ON(!q->td); |
Vivek Goyal | da52777 | 2011-03-02 19:05:33 -0500 | [diff] [blame] | 1210 | throtl_shutdown_wq(q); |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 1211 | kfree(q->td); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | static int __init throtl_init(void) |
| 1215 | { |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 1216 | kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0); |
| 1217 | if (!kthrotld_workqueue) |
| 1218 | panic("Failed to create kthrotld\n"); |
| 1219 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1220 | blkio_policy_register(&blkio_policy_throtl); |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
| 1224 | module_init(throtl_init); |