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