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