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