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