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