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