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