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