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