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