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> |
Tejun Heo | eea8f41 | 2015-05-22 17:13:17 -0400 | [diff] [blame] | 12 | #include <linux/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 | |
Shaohua Li | d61fcfa | 2017-03-27 10:51:38 -0700 | [diff] [blame] | 21 | /* Throttling is performed over a slice and after that slice is renewed */ |
| 22 | #define DFL_THROTL_SLICE_HD (HZ / 10) |
| 23 | #define DFL_THROTL_SLICE_SSD (HZ / 50) |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 24 | #define MAX_THROTL_SLICE (HZ) |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 25 | #define DFL_IDLE_THRESHOLD_SSD (1000L) /* 1 ms */ |
| 26 | #define DFL_IDLE_THRESHOLD_HD (100L * 1000) /* 100 ms */ |
| 27 | #define MAX_IDLE_TIME (5L * 1000 * 1000) /* 5 s */ |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 28 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 29 | static struct blkcg_policy blkcg_policy_throtl; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 30 | |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 31 | /* A workqueue to queue throttle related work */ |
| 32 | static struct workqueue_struct *kthrotld_workqueue; |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 33 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 34 | /* |
| 35 | * To implement hierarchical throttling, throtl_grps form a tree and bios |
| 36 | * are dispatched upwards level by level until they reach the top and get |
| 37 | * issued. When dispatching bios from the children and local group at each |
| 38 | * level, if the bios are dispatched into a single bio_list, there's a risk |
| 39 | * of a local or child group which can queue many bios at once filling up |
| 40 | * the list starving others. |
| 41 | * |
| 42 | * To avoid such starvation, dispatched bios are queued separately |
| 43 | * according to where they came from. When they are again dispatched to |
| 44 | * the parent, they're popped in round-robin order so that no single source |
| 45 | * hogs the dispatch window. |
| 46 | * |
| 47 | * throtl_qnode is used to keep the queued bios separated by their sources. |
| 48 | * Bios are queued to throtl_qnode which in turn is queued to |
| 49 | * throtl_service_queue and then dispatched in round-robin order. |
| 50 | * |
| 51 | * It's also used to track the reference counts on blkg's. A qnode always |
| 52 | * belongs to a throtl_grp and gets queued on itself or the parent, so |
| 53 | * incrementing the reference of the associated throtl_grp when a qnode is |
| 54 | * queued and decrementing when dequeued is enough to keep the whole blkg |
| 55 | * tree pinned while bios are in flight. |
| 56 | */ |
| 57 | struct throtl_qnode { |
| 58 | struct list_head node; /* service_queue->queued[] */ |
| 59 | struct bio_list bios; /* queued bios */ |
| 60 | struct throtl_grp *tg; /* tg this qnode belongs to */ |
| 61 | }; |
| 62 | |
Tejun Heo | c9e0332 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 63 | struct throtl_service_queue { |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 64 | struct throtl_service_queue *parent_sq; /* the parent service_queue */ |
| 65 | |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 66 | /* |
| 67 | * Bios queued directly to this service_queue or dispatched from |
| 68 | * children throtl_grp's. |
| 69 | */ |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 70 | struct list_head queued[2]; /* throtl_qnode [READ/WRITE] */ |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 71 | unsigned int nr_queued[2]; /* number of queued bios */ |
| 72 | |
| 73 | /* |
| 74 | * RB tree of active children throtl_grp's, which are sorted by |
| 75 | * their ->disptime. |
| 76 | */ |
Tejun Heo | c9e0332 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 77 | struct rb_root pending_tree; /* RB tree of active tgs */ |
| 78 | struct rb_node *first_pending; /* first node in the tree */ |
| 79 | unsigned int nr_pending; /* # queued in the tree */ |
| 80 | unsigned long first_pending_disptime; /* disptime of the first tg */ |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 81 | struct timer_list pending_timer; /* fires on first_pending_disptime */ |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 82 | }; |
| 83 | |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 84 | enum tg_state_flags { |
| 85 | THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */ |
Tejun Heo | 0e9f416 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 86 | THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */ |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 89 | #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node) |
| 90 | |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 91 | enum { |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 92 | LIMIT_LOW, |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 93 | LIMIT_MAX, |
| 94 | LIMIT_CNT, |
| 95 | }; |
| 96 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 97 | struct throtl_grp { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 98 | /* must be the first member */ |
| 99 | struct blkg_policy_data pd; |
| 100 | |
Tejun Heo | c9e0332 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 101 | /* active throtl group service_queue member */ |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 102 | struct rb_node rb_node; |
| 103 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 104 | /* throtl_data this group belongs to */ |
| 105 | struct throtl_data *td; |
| 106 | |
Tejun Heo | 49a2f1e | 2013-05-14 13:52:34 -0700 | [diff] [blame] | 107 | /* this group's service queue */ |
| 108 | struct throtl_service_queue service_queue; |
| 109 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 110 | /* |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 111 | * qnode_on_self is used when bios are directly queued to this |
| 112 | * throtl_grp so that local bios compete fairly with bios |
| 113 | * dispatched from children. qnode_on_parent is used when bios are |
| 114 | * dispatched from this throtl_grp into its parent and will compete |
| 115 | * with the sibling qnode_on_parents and the parent's |
| 116 | * qnode_on_self. |
| 117 | */ |
| 118 | struct throtl_qnode qnode_on_self[2]; |
| 119 | struct throtl_qnode qnode_on_parent[2]; |
| 120 | |
| 121 | /* |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 122 | * Dispatch time in jiffies. This is the estimated time when group |
| 123 | * will unthrottle and is ready to dispatch more bio. It is used as |
| 124 | * key to sort active groups in service tree. |
| 125 | */ |
| 126 | unsigned long disptime; |
| 127 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 128 | unsigned int flags; |
| 129 | |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 130 | /* are there any throtl rules between this group and td? */ |
| 131 | bool has_rules[2]; |
| 132 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 133 | /* internally used bytes per second rate limits */ |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 134 | uint64_t bps[2][LIMIT_CNT]; |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 135 | /* user configured bps limits */ |
| 136 | uint64_t bps_conf[2][LIMIT_CNT]; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 137 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 138 | /* internally used IOPS limits */ |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 139 | unsigned int iops[2][LIMIT_CNT]; |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 140 | /* user configured IOPS limits */ |
| 141 | unsigned int iops_conf[2][LIMIT_CNT]; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 142 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 143 | /* Number of bytes disptached in current slice */ |
| 144 | uint64_t bytes_disp[2]; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 145 | /* Number of bio's dispatched in current slice */ |
| 146 | unsigned int io_disp[2]; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 147 | |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 148 | unsigned long last_low_overflow_time[2]; |
| 149 | |
| 150 | uint64_t last_bytes_disp[2]; |
| 151 | unsigned int last_io_disp[2]; |
| 152 | |
| 153 | unsigned long last_check_time; |
| 154 | |
Shaohua Li | aec2424 | 2017-03-27 10:51:39 -0700 | [diff] [blame] | 155 | unsigned long last_dispatch_time[2]; |
| 156 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 157 | /* When did we start a new slice */ |
| 158 | unsigned long slice_start[2]; |
| 159 | unsigned long slice_end[2]; |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 160 | |
| 161 | unsigned long last_finish_time; /* ns / 1024 */ |
| 162 | unsigned long checked_last_finish_time; /* ns / 1024 */ |
| 163 | unsigned long avg_idletime; /* ns / 1024 */ |
| 164 | unsigned long idletime_threshold; /* us */ |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | struct throtl_data |
| 168 | { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 169 | /* service tree for active throtl groups */ |
Tejun Heo | c9e0332 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 170 | struct throtl_service_queue service_queue; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 171 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 172 | struct request_queue *queue; |
| 173 | |
| 174 | /* Total Number of queued bios on READ and WRITE lists */ |
| 175 | unsigned int nr_queued[2]; |
| 176 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 177 | unsigned int throtl_slice; |
| 178 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 179 | /* Work for dispatching throttled bios */ |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 180 | struct work_struct dispatch_work; |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 181 | unsigned int limit_index; |
| 182 | bool limit_valid[LIMIT_CNT]; |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 183 | |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame^] | 184 | unsigned long dft_idletime_threshold; /* us */ |
| 185 | |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 186 | unsigned long low_upgrade_time; |
| 187 | unsigned long low_downgrade_time; |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 188 | |
| 189 | unsigned int scale; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 190 | }; |
| 191 | |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 192 | static void throtl_pending_timer_fn(unsigned long arg); |
| 193 | |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 194 | static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd) |
| 195 | { |
| 196 | return pd ? container_of(pd, struct throtl_grp, pd) : NULL; |
| 197 | } |
| 198 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 199 | static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 200 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 201 | return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl)); |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 204 | static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 205 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 206 | return pd_to_blkg(&tg->pd); |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 209 | /** |
| 210 | * sq_to_tg - return the throl_grp the specified service queue belongs to |
| 211 | * @sq: the throtl_service_queue of interest |
| 212 | * |
| 213 | * Return the throtl_grp @sq belongs to. If @sq is the top-level one |
| 214 | * embedded in throtl_data, %NULL is returned. |
| 215 | */ |
| 216 | static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq) |
| 217 | { |
| 218 | if (sq && sq->parent_sq) |
| 219 | return container_of(sq, struct throtl_grp, service_queue); |
| 220 | else |
| 221 | return NULL; |
| 222 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 223 | |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 224 | /** |
| 225 | * sq_to_td - return throtl_data the specified service queue belongs to |
| 226 | * @sq: the throtl_service_queue of interest |
| 227 | * |
Masahiro Yamada | b43daed | 2017-02-27 14:29:09 -0800 | [diff] [blame] | 228 | * A service_queue can be embedded in either a throtl_grp or throtl_data. |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 229 | * Determine the associated throtl_data accordingly and return it. |
| 230 | */ |
| 231 | static struct throtl_data *sq_to_td(struct throtl_service_queue *sq) |
| 232 | { |
| 233 | struct throtl_grp *tg = sq_to_tg(sq); |
| 234 | |
| 235 | if (tg) |
| 236 | return tg->td; |
| 237 | else |
| 238 | return container_of(sq, struct throtl_data, service_queue); |
| 239 | } |
| 240 | |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 241 | /* |
| 242 | * cgroup's limit in LIMIT_MAX is scaled if low limit is set. This scale is to |
| 243 | * make the IO dispatch more smooth. |
| 244 | * Scale up: linearly scale up according to lapsed time since upgrade. For |
| 245 | * every throtl_slice, the limit scales up 1/2 .low limit till the |
| 246 | * limit hits .max limit |
| 247 | * Scale down: exponentially scale down if a cgroup doesn't hit its .low limit |
| 248 | */ |
| 249 | static uint64_t throtl_adjusted_limit(uint64_t low, struct throtl_data *td) |
| 250 | { |
| 251 | /* arbitrary value to avoid too big scale */ |
| 252 | if (td->scale < 4096 && time_after_eq(jiffies, |
| 253 | td->low_upgrade_time + td->scale * td->throtl_slice)) |
| 254 | td->scale = (jiffies - td->low_upgrade_time) / td->throtl_slice; |
| 255 | |
| 256 | return low + (low >> 1) * td->scale; |
| 257 | } |
| 258 | |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 259 | static uint64_t tg_bps_limit(struct throtl_grp *tg, int rw) |
| 260 | { |
Shaohua Li | b22c417 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 261 | struct blkcg_gq *blkg = tg_to_blkg(tg); |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 262 | struct throtl_data *td; |
Shaohua Li | b22c417 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 263 | uint64_t ret; |
| 264 | |
| 265 | if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent) |
| 266 | return U64_MAX; |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 267 | |
| 268 | td = tg->td; |
| 269 | ret = tg->bps[rw][td->limit_index]; |
| 270 | if (ret == 0 && td->limit_index == LIMIT_LOW) |
Shaohua Li | b22c417 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 271 | return tg->bps[rw][LIMIT_MAX]; |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 272 | |
| 273 | if (td->limit_index == LIMIT_MAX && tg->bps[rw][LIMIT_LOW] && |
| 274 | tg->bps[rw][LIMIT_LOW] != tg->bps[rw][LIMIT_MAX]) { |
| 275 | uint64_t adjusted; |
| 276 | |
| 277 | adjusted = throtl_adjusted_limit(tg->bps[rw][LIMIT_LOW], td); |
| 278 | ret = min(tg->bps[rw][LIMIT_MAX], adjusted); |
| 279 | } |
Shaohua Li | b22c417 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 280 | return ret; |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw) |
| 284 | { |
Shaohua Li | b22c417 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 285 | struct blkcg_gq *blkg = tg_to_blkg(tg); |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 286 | struct throtl_data *td; |
Shaohua Li | b22c417 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 287 | unsigned int ret; |
| 288 | |
| 289 | if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent) |
| 290 | return UINT_MAX; |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 291 | td = tg->td; |
| 292 | ret = tg->iops[rw][td->limit_index]; |
Shaohua Li | b22c417 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 293 | if (ret == 0 && tg->td->limit_index == LIMIT_LOW) |
| 294 | return tg->iops[rw][LIMIT_MAX]; |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 295 | |
| 296 | if (td->limit_index == LIMIT_MAX && tg->iops[rw][LIMIT_LOW] && |
| 297 | tg->iops[rw][LIMIT_LOW] != tg->iops[rw][LIMIT_MAX]) { |
| 298 | uint64_t adjusted; |
| 299 | |
| 300 | adjusted = throtl_adjusted_limit(tg->iops[rw][LIMIT_LOW], td); |
| 301 | if (adjusted > UINT_MAX) |
| 302 | adjusted = UINT_MAX; |
| 303 | ret = min_t(unsigned int, tg->iops[rw][LIMIT_MAX], adjusted); |
| 304 | } |
Shaohua Li | b22c417 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 305 | return ret; |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 308 | /** |
| 309 | * throtl_log - log debug message via blktrace |
| 310 | * @sq: the service_queue being reported |
| 311 | * @fmt: printf format string |
| 312 | * @args: printf args |
| 313 | * |
| 314 | * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a |
| 315 | * throtl_grp; otherwise, just "throtl". |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 316 | */ |
| 317 | #define throtl_log(sq, fmt, args...) do { \ |
| 318 | struct throtl_grp *__tg = sq_to_tg((sq)); \ |
| 319 | struct throtl_data *__td = sq_to_td((sq)); \ |
| 320 | \ |
| 321 | (void)__td; \ |
Shaohua Li | 59fa022 | 2016-05-09 17:22:15 -0700 | [diff] [blame] | 322 | if (likely(!blk_trace_note_message_enabled(__td->queue))) \ |
| 323 | break; \ |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 324 | if ((__tg)) { \ |
| 325 | char __pbuf[128]; \ |
| 326 | \ |
| 327 | blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \ |
| 328 | blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \ |
| 329 | } else { \ |
| 330 | blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \ |
| 331 | } \ |
| 332 | } while (0) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 333 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 334 | static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg) |
| 335 | { |
| 336 | INIT_LIST_HEAD(&qn->node); |
| 337 | bio_list_init(&qn->bios); |
| 338 | qn->tg = tg; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it |
| 343 | * @bio: bio being added |
| 344 | * @qn: qnode to add bio to |
| 345 | * @queued: the service_queue->queued[] list @qn belongs to |
| 346 | * |
| 347 | * Add @bio to @qn and put @qn on @queued if it's not already on. |
| 348 | * @qn->tg's reference count is bumped when @qn is activated. See the |
| 349 | * comment on top of throtl_qnode definition for details. |
| 350 | */ |
| 351 | static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn, |
| 352 | struct list_head *queued) |
| 353 | { |
| 354 | bio_list_add(&qn->bios, bio); |
| 355 | if (list_empty(&qn->node)) { |
| 356 | list_add_tail(&qn->node, queued); |
| 357 | blkg_get(tg_to_blkg(qn->tg)); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * throtl_peek_queued - peek the first bio on a qnode list |
| 363 | * @queued: the qnode list to peek |
| 364 | */ |
| 365 | static struct bio *throtl_peek_queued(struct list_head *queued) |
| 366 | { |
| 367 | struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node); |
| 368 | struct bio *bio; |
| 369 | |
| 370 | if (list_empty(queued)) |
| 371 | return NULL; |
| 372 | |
| 373 | bio = bio_list_peek(&qn->bios); |
| 374 | WARN_ON_ONCE(!bio); |
| 375 | return bio; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * throtl_pop_queued - pop the first bio form a qnode list |
| 380 | * @queued: the qnode list to pop a bio from |
| 381 | * @tg_to_put: optional out argument for throtl_grp to put |
| 382 | * |
| 383 | * Pop the first bio from the qnode list @queued. After popping, the first |
| 384 | * qnode is removed from @queued if empty or moved to the end of @queued so |
| 385 | * that the popping order is round-robin. |
| 386 | * |
| 387 | * When the first qnode is removed, its associated throtl_grp should be put |
| 388 | * too. If @tg_to_put is NULL, this function automatically puts it; |
| 389 | * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is |
| 390 | * responsible for putting it. |
| 391 | */ |
| 392 | static struct bio *throtl_pop_queued(struct list_head *queued, |
| 393 | struct throtl_grp **tg_to_put) |
| 394 | { |
| 395 | struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node); |
| 396 | struct bio *bio; |
| 397 | |
| 398 | if (list_empty(queued)) |
| 399 | return NULL; |
| 400 | |
| 401 | bio = bio_list_pop(&qn->bios); |
| 402 | WARN_ON_ONCE(!bio); |
| 403 | |
| 404 | if (bio_list_empty(&qn->bios)) { |
| 405 | list_del_init(&qn->node); |
| 406 | if (tg_to_put) |
| 407 | *tg_to_put = qn->tg; |
| 408 | else |
| 409 | blkg_put(tg_to_blkg(qn->tg)); |
| 410 | } else { |
| 411 | list_move_tail(&qn->node, queued); |
| 412 | } |
| 413 | |
| 414 | return bio; |
| 415 | } |
| 416 | |
Tejun Heo | 49a2f1e | 2013-05-14 13:52:34 -0700 | [diff] [blame] | 417 | /* init a service_queue, assumes the caller zeroed it */ |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 418 | static void throtl_service_queue_init(struct throtl_service_queue *sq) |
Tejun Heo | 49a2f1e | 2013-05-14 13:52:34 -0700 | [diff] [blame] | 419 | { |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 420 | INIT_LIST_HEAD(&sq->queued[0]); |
| 421 | INIT_LIST_HEAD(&sq->queued[1]); |
Tejun Heo | 49a2f1e | 2013-05-14 13:52:34 -0700 | [diff] [blame] | 422 | sq->pending_tree = RB_ROOT; |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 423 | setup_timer(&sq->pending_timer, throtl_pending_timer_fn, |
| 424 | (unsigned long)sq); |
| 425 | } |
| 426 | |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 427 | static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node) |
| 428 | { |
Tejun Heo | 4fb7203 | 2015-08-18 14:55:12 -0700 | [diff] [blame] | 429 | struct throtl_grp *tg; |
Tejun Heo | 24bdb8e | 2015-08-18 14:55:22 -0700 | [diff] [blame] | 430 | int rw; |
Tejun Heo | 4fb7203 | 2015-08-18 14:55:12 -0700 | [diff] [blame] | 431 | |
| 432 | tg = kzalloc_node(sizeof(*tg), gfp, node); |
| 433 | if (!tg) |
Tejun Heo | 77ea733 | 2015-08-18 14:55:24 -0700 | [diff] [blame] | 434 | return NULL; |
Tejun Heo | 4fb7203 | 2015-08-18 14:55:12 -0700 | [diff] [blame] | 435 | |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 436 | throtl_service_queue_init(&tg->service_queue); |
| 437 | |
| 438 | for (rw = READ; rw <= WRITE; rw++) { |
| 439 | throtl_qnode_init(&tg->qnode_on_self[rw], tg); |
| 440 | throtl_qnode_init(&tg->qnode_on_parent[rw], tg); |
| 441 | } |
| 442 | |
| 443 | RB_CLEAR_NODE(&tg->rb_node); |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 444 | tg->bps[READ][LIMIT_MAX] = U64_MAX; |
| 445 | tg->bps[WRITE][LIMIT_MAX] = U64_MAX; |
| 446 | tg->iops[READ][LIMIT_MAX] = UINT_MAX; |
| 447 | tg->iops[WRITE][LIMIT_MAX] = UINT_MAX; |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 448 | tg->bps_conf[READ][LIMIT_MAX] = U64_MAX; |
| 449 | tg->bps_conf[WRITE][LIMIT_MAX] = U64_MAX; |
| 450 | tg->iops_conf[READ][LIMIT_MAX] = UINT_MAX; |
| 451 | tg->iops_conf[WRITE][LIMIT_MAX] = UINT_MAX; |
| 452 | /* LIMIT_LOW will have default value 0 */ |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 453 | |
Tejun Heo | 4fb7203 | 2015-08-18 14:55:12 -0700 | [diff] [blame] | 454 | return &tg->pd; |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 457 | static void throtl_pd_init(struct blkg_policy_data *pd) |
Vivek Goyal | a29a171 | 2011-05-19 15:38:19 -0400 | [diff] [blame] | 458 | { |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 459 | struct throtl_grp *tg = pd_to_tg(pd); |
| 460 | struct blkcg_gq *blkg = tg_to_blkg(tg); |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 461 | struct throtl_data *td = blkg->q->td; |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 462 | struct throtl_service_queue *sq = &tg->service_queue; |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 463 | |
Tejun Heo | 9138125 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 464 | /* |
Tejun Heo | aa6ec29 | 2014-07-09 10:08:08 -0400 | [diff] [blame] | 465 | * If on the default hierarchy, we switch to properly hierarchical |
Tejun Heo | 9138125 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 466 | * behavior where limits on a given throtl_grp are applied to the |
| 467 | * whole subtree rather than just the group itself. e.g. If 16M |
| 468 | * read_bps limit is set on the root group, the whole system can't |
| 469 | * exceed 16M for the device. |
| 470 | * |
Tejun Heo | aa6ec29 | 2014-07-09 10:08:08 -0400 | [diff] [blame] | 471 | * If not on the default hierarchy, the broken flat hierarchy |
Tejun Heo | 9138125 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 472 | * behavior is retained where all throtl_grps are treated as if |
| 473 | * they're all separate root groups right below throtl_data. |
| 474 | * Limits of a group don't interact with limits of other groups |
| 475 | * regardless of the position of the group in the hierarchy. |
| 476 | */ |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 477 | sq->parent_sq = &td->service_queue; |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 478 | if (cgroup_subsys_on_dfl(io_cgrp_subsys) && blkg->parent) |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 479 | sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue; |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 480 | tg->td = td; |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 481 | |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame^] | 482 | tg->idletime_threshold = td->dft_idletime_threshold; |
Tejun Heo | 8a3d261 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 485 | /* |
| 486 | * Set has_rules[] if @tg or any of its parents have limits configured. |
| 487 | * This doesn't require walking up to the top of the hierarchy as the |
| 488 | * parent's has_rules[] is guaranteed to be correct. |
| 489 | */ |
| 490 | static void tg_update_has_rules(struct throtl_grp *tg) |
| 491 | { |
| 492 | struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq); |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 493 | struct throtl_data *td = tg->td; |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 494 | int rw; |
| 495 | |
| 496 | for (rw = READ; rw <= WRITE; rw++) |
| 497 | tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) || |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 498 | (td->limit_valid[td->limit_index] && |
| 499 | (tg_bps_limit(tg, rw) != U64_MAX || |
| 500 | tg_iops_limit(tg, rw) != UINT_MAX)); |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 503 | static void throtl_pd_online(struct blkg_policy_data *pd) |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 504 | { |
Shaohua Li | aec2424 | 2017-03-27 10:51:39 -0700 | [diff] [blame] | 505 | struct throtl_grp *tg = pd_to_tg(pd); |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 506 | /* |
| 507 | * We don't want new groups to escape the limits of its ancestors. |
| 508 | * Update has_rules[] after a new group is brought online. |
| 509 | */ |
Shaohua Li | aec2424 | 2017-03-27 10:51:39 -0700 | [diff] [blame] | 510 | tg_update_has_rules(tg); |
| 511 | tg->last_dispatch_time[READ] = jiffies; |
| 512 | tg->last_dispatch_time[WRITE] = jiffies; |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 515 | static void blk_throtl_update_limit_valid(struct throtl_data *td) |
| 516 | { |
| 517 | struct cgroup_subsys_state *pos_css; |
| 518 | struct blkcg_gq *blkg; |
| 519 | bool low_valid = false; |
| 520 | |
| 521 | rcu_read_lock(); |
| 522 | blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) { |
| 523 | struct throtl_grp *tg = blkg_to_tg(blkg); |
| 524 | |
| 525 | if (tg->bps[READ][LIMIT_LOW] || tg->bps[WRITE][LIMIT_LOW] || |
| 526 | tg->iops[READ][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW]) |
| 527 | low_valid = true; |
| 528 | } |
| 529 | rcu_read_unlock(); |
| 530 | |
| 531 | td->limit_valid[LIMIT_LOW] = low_valid; |
| 532 | } |
| 533 | |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 534 | static void throtl_upgrade_state(struct throtl_data *td); |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 535 | static void throtl_pd_offline(struct blkg_policy_data *pd) |
| 536 | { |
| 537 | struct throtl_grp *tg = pd_to_tg(pd); |
| 538 | |
| 539 | tg->bps[READ][LIMIT_LOW] = 0; |
| 540 | tg->bps[WRITE][LIMIT_LOW] = 0; |
| 541 | tg->iops[READ][LIMIT_LOW] = 0; |
| 542 | tg->iops[WRITE][LIMIT_LOW] = 0; |
| 543 | |
| 544 | blk_throtl_update_limit_valid(tg->td); |
| 545 | |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 546 | if (!tg->td->limit_valid[tg->td->limit_index]) |
| 547 | throtl_upgrade_state(tg->td); |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 550 | static void throtl_pd_free(struct blkg_policy_data *pd) |
| 551 | { |
Tejun Heo | 4fb7203 | 2015-08-18 14:55:12 -0700 | [diff] [blame] | 552 | struct throtl_grp *tg = pd_to_tg(pd); |
| 553 | |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 554 | del_timer_sync(&tg->service_queue.pending_timer); |
Tejun Heo | 4fb7203 | 2015-08-18 14:55:12 -0700 | [diff] [blame] | 555 | kfree(tg); |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 558 | static struct throtl_grp * |
| 559 | throtl_rb_first(struct throtl_service_queue *parent_sq) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 560 | { |
| 561 | /* Service tree is empty */ |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 562 | if (!parent_sq->nr_pending) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 563 | return NULL; |
| 564 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 565 | if (!parent_sq->first_pending) |
| 566 | parent_sq->first_pending = rb_first(&parent_sq->pending_tree); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 567 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 568 | if (parent_sq->first_pending) |
| 569 | return rb_entry_tg(parent_sq->first_pending); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 570 | |
| 571 | return NULL; |
| 572 | } |
| 573 | |
| 574 | static void rb_erase_init(struct rb_node *n, struct rb_root *root) |
| 575 | { |
| 576 | rb_erase(n, root); |
| 577 | RB_CLEAR_NODE(n); |
| 578 | } |
| 579 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 580 | static void throtl_rb_erase(struct rb_node *n, |
| 581 | struct throtl_service_queue *parent_sq) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 582 | { |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 583 | if (parent_sq->first_pending == n) |
| 584 | parent_sq->first_pending = NULL; |
| 585 | rb_erase_init(n, &parent_sq->pending_tree); |
| 586 | --parent_sq->nr_pending; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 587 | } |
| 588 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 589 | static void update_min_dispatch_time(struct throtl_service_queue *parent_sq) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 590 | { |
| 591 | struct throtl_grp *tg; |
| 592 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 593 | tg = throtl_rb_first(parent_sq); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 594 | if (!tg) |
| 595 | return; |
| 596 | |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 597 | parent_sq->first_pending_disptime = tg->disptime; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 598 | } |
| 599 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 600 | static void tg_service_queue_add(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 601 | { |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 602 | struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq; |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 603 | struct rb_node **node = &parent_sq->pending_tree.rb_node; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 604 | struct rb_node *parent = NULL; |
| 605 | struct throtl_grp *__tg; |
| 606 | unsigned long key = tg->disptime; |
| 607 | int left = 1; |
| 608 | |
| 609 | while (*node != NULL) { |
| 610 | parent = *node; |
| 611 | __tg = rb_entry_tg(parent); |
| 612 | |
| 613 | if (time_before(key, __tg->disptime)) |
| 614 | node = &parent->rb_left; |
| 615 | else { |
| 616 | node = &parent->rb_right; |
| 617 | left = 0; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | if (left) |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 622 | parent_sq->first_pending = &tg->rb_node; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 623 | |
| 624 | rb_link_node(&tg->rb_node, parent, node); |
Tejun Heo | 0049af7 | 2013-05-14 13:52:33 -0700 | [diff] [blame] | 625 | rb_insert_color(&tg->rb_node, &parent_sq->pending_tree); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 626 | } |
| 627 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 628 | static void __throtl_enqueue_tg(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 629 | { |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 630 | tg_service_queue_add(tg); |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 631 | tg->flags |= THROTL_TG_PENDING; |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 632 | tg->service_queue.parent_sq->nr_pending++; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 633 | } |
| 634 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 635 | static void throtl_enqueue_tg(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 636 | { |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 637 | if (!(tg->flags & THROTL_TG_PENDING)) |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 638 | __throtl_enqueue_tg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 639 | } |
| 640 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 641 | static void __throtl_dequeue_tg(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 642 | { |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 643 | throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq); |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 644 | tg->flags &= ~THROTL_TG_PENDING; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 645 | } |
| 646 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 647 | static void throtl_dequeue_tg(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 648 | { |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 649 | if (tg->flags & THROTL_TG_PENDING) |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 650 | __throtl_dequeue_tg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 651 | } |
| 652 | |
Tejun Heo | a9131a2 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 653 | /* Call with queue lock held */ |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 654 | static void throtl_schedule_pending_timer(struct throtl_service_queue *sq, |
| 655 | unsigned long expires) |
Tejun Heo | a9131a2 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 656 | { |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 657 | unsigned long max_expire = jiffies + 8 * sq_to_tg(sq)->td->throtl_slice; |
Shaohua Li | 06cceed | 2017-03-27 10:51:36 -0700 | [diff] [blame] | 658 | |
| 659 | /* |
| 660 | * Since we are adjusting the throttle limit dynamically, the sleep |
| 661 | * time calculated according to previous limit might be invalid. It's |
| 662 | * possible the cgroup sleep time is very long and no other cgroups |
| 663 | * have IO running so notify the limit changes. Make sure the cgroup |
| 664 | * doesn't sleep too long to avoid the missed notification. |
| 665 | */ |
| 666 | if (time_after(expires, max_expire)) |
| 667 | expires = max_expire; |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 668 | mod_timer(&sq->pending_timer, expires); |
| 669 | throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu", |
| 670 | expires - jiffies, jiffies); |
Tejun Heo | a9131a2 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 673 | /** |
| 674 | * throtl_schedule_next_dispatch - schedule the next dispatch cycle |
| 675 | * @sq: the service_queue to schedule dispatch for |
| 676 | * @force: force scheduling |
| 677 | * |
| 678 | * Arm @sq->pending_timer so that the next dispatch cycle starts on the |
| 679 | * dispatch time of the first pending child. Returns %true if either timer |
| 680 | * is armed or there's no pending child left. %false if the current |
| 681 | * dispatch window is still open and the caller should continue |
| 682 | * dispatching. |
| 683 | * |
| 684 | * If @force is %true, the dispatch timer is always scheduled and this |
| 685 | * function is guaranteed to return %true. This is to be used when the |
| 686 | * caller can't dispatch itself and needs to invoke pending_timer |
| 687 | * unconditionally. Note that forced scheduling is likely to induce short |
| 688 | * delay before dispatch starts even if @sq->first_pending_disptime is not |
| 689 | * in the future and thus shouldn't be used in hot paths. |
| 690 | */ |
| 691 | static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq, |
| 692 | bool force) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 693 | { |
Tejun Heo | 6a52560 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 694 | /* any pending children left? */ |
Tejun Heo | c9e0332 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 695 | if (!sq->nr_pending) |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 696 | return true; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 697 | |
Tejun Heo | c9e0332 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 698 | update_min_dispatch_time(sq); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 699 | |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 700 | /* is the next dispatch time in the future? */ |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 701 | if (force || time_after(sq->first_pending_disptime, jiffies)) { |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 702 | throtl_schedule_pending_timer(sq, sq->first_pending_disptime); |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 703 | return true; |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 706 | /* tell the caller to continue dispatching */ |
| 707 | return false; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 708 | } |
| 709 | |
Vivek Goyal | 32ee5bc | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 710 | static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg, |
| 711 | bool rw, unsigned long start) |
| 712 | { |
| 713 | tg->bytes_disp[rw] = 0; |
| 714 | tg->io_disp[rw] = 0; |
| 715 | |
| 716 | /* |
| 717 | * Previous slice has expired. We must have trimmed it after last |
| 718 | * bio dispatch. That means since start of last slice, we never used |
| 719 | * that bandwidth. Do try to make use of that bandwidth while giving |
| 720 | * credit. |
| 721 | */ |
| 722 | if (time_after_eq(start, tg->slice_start[rw])) |
| 723 | tg->slice_start[rw] = start; |
| 724 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 725 | tg->slice_end[rw] = jiffies + tg->td->throtl_slice; |
Vivek Goyal | 32ee5bc | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 726 | throtl_log(&tg->service_queue, |
| 727 | "[%c] new slice with credit start=%lu end=%lu jiffies=%lu", |
| 728 | rw == READ ? 'R' : 'W', tg->slice_start[rw], |
| 729 | tg->slice_end[rw], jiffies); |
| 730 | } |
| 731 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 732 | 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] | 733 | { |
| 734 | tg->bytes_disp[rw] = 0; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 735 | tg->io_disp[rw] = 0; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 736 | tg->slice_start[rw] = jiffies; |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 737 | tg->slice_end[rw] = jiffies + tg->td->throtl_slice; |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 738 | throtl_log(&tg->service_queue, |
| 739 | "[%c] new slice start=%lu end=%lu jiffies=%lu", |
| 740 | rw == READ ? 'R' : 'W', tg->slice_start[rw], |
| 741 | tg->slice_end[rw], jiffies); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 742 | } |
| 743 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 744 | static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw, |
| 745 | unsigned long jiffy_end) |
Vivek Goyal | d1ae8ff | 2010-12-01 19:34:46 +0100 | [diff] [blame] | 746 | { |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 747 | tg->slice_end[rw] = roundup(jiffy_end, tg->td->throtl_slice); |
Vivek Goyal | d1ae8ff | 2010-12-01 19:34:46 +0100 | [diff] [blame] | 748 | } |
| 749 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 750 | static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw, |
| 751 | unsigned long jiffy_end) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 752 | { |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 753 | tg->slice_end[rw] = roundup(jiffy_end, tg->td->throtl_slice); |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 754 | throtl_log(&tg->service_queue, |
| 755 | "[%c] extend slice start=%lu end=%lu jiffies=%lu", |
| 756 | rw == READ ? 'R' : 'W', tg->slice_start[rw], |
| 757 | tg->slice_end[rw], jiffies); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /* Determine if previously allocated or extended slice is complete or not */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 761 | static bool throtl_slice_used(struct throtl_grp *tg, bool rw) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 762 | { |
| 763 | if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw])) |
Fabian Frederick | 5cf8c22 | 2014-05-02 18:28:17 +0200 | [diff] [blame] | 764 | return false; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 765 | |
| 766 | return 1; |
| 767 | } |
| 768 | |
| 769 | /* Trim the used slices and adjust slice start accordingly */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 770 | static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 771 | { |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 772 | unsigned long nr_slices, time_elapsed, io_trim; |
| 773 | u64 bytes_trim, tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 774 | |
| 775 | BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw])); |
| 776 | |
| 777 | /* |
| 778 | * If bps are unlimited (-1), then time slice don't get |
| 779 | * renewed. Don't try to trim the slice if slice is used. A new |
| 780 | * slice will start when appropriate. |
| 781 | */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 782 | if (throtl_slice_used(tg, rw)) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 783 | return; |
| 784 | |
Vivek Goyal | d1ae8ff | 2010-12-01 19:34:46 +0100 | [diff] [blame] | 785 | /* |
| 786 | * A bio has been dispatched. Also adjust slice_end. It might happen |
| 787 | * that initially cgroup limit was very low resulting in high |
| 788 | * slice_end, but later limit was bumped up and bio was dispached |
| 789 | * sooner, then we need to reduce slice_end. A high bogus slice_end |
| 790 | * is bad because it does not allow new slice to start. |
| 791 | */ |
| 792 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 793 | throtl_set_slice_end(tg, rw, jiffies + tg->td->throtl_slice); |
Vivek Goyal | d1ae8ff | 2010-12-01 19:34:46 +0100 | [diff] [blame] | 794 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 795 | time_elapsed = jiffies - tg->slice_start[rw]; |
| 796 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 797 | nr_slices = time_elapsed / tg->td->throtl_slice; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 798 | |
| 799 | if (!nr_slices) |
| 800 | return; |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 801 | tmp = tg_bps_limit(tg, rw) * tg->td->throtl_slice * nr_slices; |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 802 | do_div(tmp, HZ); |
| 803 | bytes_trim = tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 804 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 805 | io_trim = (tg_iops_limit(tg, rw) * tg->td->throtl_slice * nr_slices) / |
| 806 | HZ; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 807 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 808 | if (!bytes_trim && !io_trim) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 809 | return; |
| 810 | |
| 811 | if (tg->bytes_disp[rw] >= bytes_trim) |
| 812 | tg->bytes_disp[rw] -= bytes_trim; |
| 813 | else |
| 814 | tg->bytes_disp[rw] = 0; |
| 815 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 816 | if (tg->io_disp[rw] >= io_trim) |
| 817 | tg->io_disp[rw] -= io_trim; |
| 818 | else |
| 819 | tg->io_disp[rw] = 0; |
| 820 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 821 | tg->slice_start[rw] += nr_slices * tg->td->throtl_slice; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 822 | |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 823 | throtl_log(&tg->service_queue, |
| 824 | "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu", |
| 825 | rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim, |
| 826 | tg->slice_start[rw], tg->slice_end[rw], jiffies); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 827 | } |
| 828 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 829 | static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio, |
| 830 | unsigned long *wait) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 831 | { |
| 832 | bool rw = bio_data_dir(bio); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 833 | unsigned int io_allowed; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 834 | unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd; |
Vivek Goyal | c49c06e | 2010-10-01 21:16:42 +0200 | [diff] [blame] | 835 | u64 tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 836 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 837 | jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw]; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 838 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 839 | /* Slice has just started. Consider one slice interval */ |
| 840 | if (!jiffy_elapsed) |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 841 | jiffy_elapsed_rnd = tg->td->throtl_slice; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 842 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 843 | jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 844 | |
Vivek Goyal | c49c06e | 2010-10-01 21:16:42 +0200 | [diff] [blame] | 845 | /* |
| 846 | * jiffy_elapsed_rnd should not be a big value as minimum iops can be |
| 847 | * 1 then at max jiffy elapsed should be equivalent of 1 second as we |
| 848 | * will allow dispatch after 1 second and after that slice should |
| 849 | * have been trimmed. |
| 850 | */ |
| 851 | |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 852 | tmp = (u64)tg_iops_limit(tg, rw) * jiffy_elapsed_rnd; |
Vivek Goyal | c49c06e | 2010-10-01 21:16:42 +0200 | [diff] [blame] | 853 | do_div(tmp, HZ); |
| 854 | |
| 855 | if (tmp > UINT_MAX) |
| 856 | io_allowed = UINT_MAX; |
| 857 | else |
| 858 | io_allowed = tmp; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 859 | |
| 860 | if (tg->io_disp[rw] + 1 <= io_allowed) { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 861 | if (wait) |
| 862 | *wait = 0; |
Fabian Frederick | 5cf8c22 | 2014-05-02 18:28:17 +0200 | [diff] [blame] | 863 | return true; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 864 | } |
| 865 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 866 | /* Calc approx time to dispatch */ |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 867 | jiffy_wait = ((tg->io_disp[rw] + 1) * HZ) / tg_iops_limit(tg, rw) + 1; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 868 | |
| 869 | if (jiffy_wait > jiffy_elapsed) |
| 870 | jiffy_wait = jiffy_wait - jiffy_elapsed; |
| 871 | else |
| 872 | jiffy_wait = 1; |
| 873 | |
| 874 | if (wait) |
| 875 | *wait = jiffy_wait; |
| 876 | return 0; |
| 877 | } |
| 878 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 879 | static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio, |
| 880 | unsigned long *wait) |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 881 | { |
| 882 | bool rw = bio_data_dir(bio); |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 883 | u64 bytes_allowed, extra_bytes, tmp; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 884 | unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 885 | |
| 886 | jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw]; |
| 887 | |
| 888 | /* Slice has just started. Consider one slice interval */ |
| 889 | if (!jiffy_elapsed) |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 890 | jiffy_elapsed_rnd = tg->td->throtl_slice; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 891 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 892 | jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 893 | |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 894 | tmp = tg_bps_limit(tg, rw) * jiffy_elapsed_rnd; |
Vivek Goyal | 5e901a2 | 2010-10-01 21:16:38 +0200 | [diff] [blame] | 895 | do_div(tmp, HZ); |
Vivek Goyal | 3aad5d3 | 2010-10-01 14:51:14 +0200 | [diff] [blame] | 896 | bytes_allowed = tmp; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 897 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 898 | if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 899 | if (wait) |
| 900 | *wait = 0; |
Fabian Frederick | 5cf8c22 | 2014-05-02 18:28:17 +0200 | [diff] [blame] | 901 | return true; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | /* Calc approx time to dispatch */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 905 | extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed; |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 906 | jiffy_wait = div64_u64(extra_bytes * HZ, tg_bps_limit(tg, rw)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 907 | |
| 908 | if (!jiffy_wait) |
| 909 | jiffy_wait = 1; |
| 910 | |
| 911 | /* |
| 912 | * This wait time is without taking into consideration the rounding |
| 913 | * up we did. Add that time also. |
| 914 | */ |
| 915 | jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 916 | if (wait) |
| 917 | *wait = jiffy_wait; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 918 | return 0; |
| 919 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 920 | |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 921 | /* |
| 922 | * Returns whether one can dispatch a bio or not. Also returns approx number |
| 923 | * of jiffies to wait before this bio is with-in IO rate and can be dispatched |
| 924 | */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 925 | static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio, |
| 926 | unsigned long *wait) |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 927 | { |
| 928 | bool rw = bio_data_dir(bio); |
| 929 | unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0; |
| 930 | |
| 931 | /* |
| 932 | * Currently whole state machine of group depends on first bio |
| 933 | * queued in the group bio list. So one should not be calling |
| 934 | * this function with a different bio if there are other bios |
| 935 | * queued. |
| 936 | */ |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 937 | BUG_ON(tg->service_queue.nr_queued[rw] && |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 938 | bio != throtl_peek_queued(&tg->service_queue.queued[rw])); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 939 | |
| 940 | /* If tg->bps = -1, then BW is unlimited */ |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 941 | if (tg_bps_limit(tg, rw) == U64_MAX && |
| 942 | tg_iops_limit(tg, rw) == UINT_MAX) { |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 943 | if (wait) |
| 944 | *wait = 0; |
Fabian Frederick | 5cf8c22 | 2014-05-02 18:28:17 +0200 | [diff] [blame] | 945 | return true; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | /* |
| 949 | * If previous slice expired, start a new one otherwise renew/extend |
| 950 | * existing slice to make sure it is at least throtl_slice interval |
Vivek Goyal | 164c80e | 2016-09-19 15:12:41 -0600 | [diff] [blame] | 951 | * long since now. New slice is started only for empty throttle group. |
| 952 | * If there is queued bio, that means there should be an active |
| 953 | * slice and it should be extended instead. |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 954 | */ |
Vivek Goyal | 164c80e | 2016-09-19 15:12:41 -0600 | [diff] [blame] | 955 | if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw])) |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 956 | throtl_start_new_slice(tg, rw); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 957 | else { |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 958 | if (time_before(tg->slice_end[rw], |
| 959 | jiffies + tg->td->throtl_slice)) |
| 960 | throtl_extend_slice(tg, rw, |
| 961 | jiffies + tg->td->throtl_slice); |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 962 | } |
| 963 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 964 | if (tg_with_in_bps_limit(tg, bio, &bps_wait) && |
| 965 | tg_with_in_iops_limit(tg, bio, &iops_wait)) { |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 966 | if (wait) |
| 967 | *wait = 0; |
| 968 | return 1; |
| 969 | } |
| 970 | |
| 971 | max_wait = max(bps_wait, iops_wait); |
| 972 | |
| 973 | if (wait) |
| 974 | *wait = max_wait; |
| 975 | |
| 976 | if (time_before(tg->slice_end[rw], jiffies + max_wait)) |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 977 | throtl_extend_slice(tg, rw, jiffies + max_wait); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 978 | |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio) |
| 983 | { |
| 984 | bool rw = bio_data_dir(bio); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 985 | |
| 986 | /* Charge the bio to the group */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 987 | tg->bytes_disp[rw] += bio->bi_iter.bi_size; |
Vivek Goyal | 8e89d13 | 2010-09-15 17:06:37 -0400 | [diff] [blame] | 988 | tg->io_disp[rw]++; |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 989 | tg->last_bytes_disp[rw] += bio->bi_iter.bi_size; |
| 990 | tg->last_io_disp[rw]++; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 991 | |
Tejun Heo | 2a0f61e | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 992 | /* |
Christoph Hellwig | 8d2bbd4 | 2016-10-20 15:12:12 +0200 | [diff] [blame] | 993 | * BIO_THROTTLED is used to prevent the same bio to be throttled |
Tejun Heo | 2a0f61e | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 994 | * more than once as a throttled bio will go through blk-throtl the |
| 995 | * second time when it eventually gets issued. Set it when a bio |
| 996 | * is being charged to a tg. |
Tejun Heo | 2a0f61e | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 997 | */ |
Christoph Hellwig | 8d2bbd4 | 2016-10-20 15:12:12 +0200 | [diff] [blame] | 998 | if (!bio_flagged(bio, BIO_THROTTLED)) |
| 999 | bio_set_flag(bio, BIO_THROTTLED); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1000 | } |
| 1001 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1002 | /** |
| 1003 | * throtl_add_bio_tg - add a bio to the specified throtl_grp |
| 1004 | * @bio: bio to add |
| 1005 | * @qn: qnode to use |
| 1006 | * @tg: the target throtl_grp |
| 1007 | * |
| 1008 | * Add @bio to @tg's service_queue using @qn. If @qn is not specified, |
| 1009 | * tg->qnode_on_self[] is used. |
| 1010 | */ |
| 1011 | static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn, |
| 1012 | struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1013 | { |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1014 | struct throtl_service_queue *sq = &tg->service_queue; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1015 | bool rw = bio_data_dir(bio); |
| 1016 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1017 | if (!qn) |
| 1018 | qn = &tg->qnode_on_self[rw]; |
| 1019 | |
Tejun Heo | 0e9f416 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1020 | /* |
| 1021 | * If @tg doesn't currently have any bios queued in the same |
| 1022 | * direction, queueing @bio can change when @tg should be |
| 1023 | * dispatched. Mark that @tg was empty. This is automatically |
| 1024 | * cleaered on the next tg_update_disptime(). |
| 1025 | */ |
| 1026 | if (!sq->nr_queued[rw]) |
| 1027 | tg->flags |= THROTL_TG_WAS_EMPTY; |
| 1028 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1029 | throtl_qnode_add_bio(bio, qn, &sq->queued[rw]); |
| 1030 | |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1031 | sq->nr_queued[rw]++; |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1032 | throtl_enqueue_tg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1033 | } |
| 1034 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1035 | static void tg_update_disptime(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1036 | { |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1037 | struct throtl_service_queue *sq = &tg->service_queue; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1038 | unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime; |
| 1039 | struct bio *bio; |
| 1040 | |
Markus Elfring | d609af3 | 2017-01-21 22:15:33 +0100 | [diff] [blame] | 1041 | bio = throtl_peek_queued(&sq->queued[READ]); |
| 1042 | if (bio) |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1043 | tg_may_dispatch(tg, bio, &read_wait); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1044 | |
Markus Elfring | d609af3 | 2017-01-21 22:15:33 +0100 | [diff] [blame] | 1045 | bio = throtl_peek_queued(&sq->queued[WRITE]); |
| 1046 | if (bio) |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1047 | tg_may_dispatch(tg, bio, &write_wait); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1048 | |
| 1049 | min_wait = min(read_wait, write_wait); |
| 1050 | disptime = jiffies + min_wait; |
| 1051 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1052 | /* Update dispatch time */ |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1053 | throtl_dequeue_tg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1054 | tg->disptime = disptime; |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1055 | throtl_enqueue_tg(tg); |
Tejun Heo | 0e9f416 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1056 | |
| 1057 | /* see throtl_add_bio_tg() */ |
| 1058 | tg->flags &= ~THROTL_TG_WAS_EMPTY; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1059 | } |
| 1060 | |
Vivek Goyal | 32ee5bc | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1061 | static void start_parent_slice_with_credit(struct throtl_grp *child_tg, |
| 1062 | struct throtl_grp *parent_tg, bool rw) |
| 1063 | { |
| 1064 | if (throtl_slice_used(parent_tg, rw)) { |
| 1065 | throtl_start_new_slice_with_credit(parent_tg, rw, |
| 1066 | child_tg->slice_start[rw]); |
| 1067 | } |
| 1068 | |
| 1069 | } |
| 1070 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1071 | static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1072 | { |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1073 | struct throtl_service_queue *sq = &tg->service_queue; |
Tejun Heo | 6bc9c2b | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1074 | struct throtl_service_queue *parent_sq = sq->parent_sq; |
| 1075 | struct throtl_grp *parent_tg = sq_to_tg(parent_sq); |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1076 | struct throtl_grp *tg_to_put = NULL; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1077 | struct bio *bio; |
| 1078 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1079 | /* |
| 1080 | * @bio is being transferred from @tg to @parent_sq. Popping a bio |
| 1081 | * from @tg may put its reference and @parent_sq might end up |
| 1082 | * getting released prematurely. Remember the tg to put and put it |
| 1083 | * after @bio is transferred to @parent_sq. |
| 1084 | */ |
| 1085 | bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put); |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1086 | sq->nr_queued[rw]--; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1087 | |
| 1088 | throtl_charge_bio(tg, bio); |
Tejun Heo | 6bc9c2b | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1089 | |
| 1090 | /* |
| 1091 | * If our parent is another tg, we just need to transfer @bio to |
| 1092 | * the parent using throtl_add_bio_tg(). If our parent is |
| 1093 | * @td->service_queue, @bio is ready to be issued. Put it on its |
| 1094 | * bio_lists[] and decrease total number queued. The caller is |
| 1095 | * responsible for issuing these bios. |
| 1096 | */ |
| 1097 | if (parent_tg) { |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1098 | throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg); |
Vivek Goyal | 32ee5bc | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1099 | start_parent_slice_with_credit(tg, parent_tg, rw); |
Tejun Heo | 6bc9c2b | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1100 | } else { |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1101 | throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw], |
| 1102 | &parent_sq->queued[rw]); |
Tejun Heo | 6bc9c2b | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1103 | BUG_ON(tg->td->nr_queued[rw] <= 0); |
| 1104 | tg->td->nr_queued[rw]--; |
| 1105 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1106 | |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1107 | throtl_trim_slice(tg, rw); |
Tejun Heo | 6bc9c2b | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1108 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1109 | if (tg_to_put) |
| 1110 | blkg_put(tg_to_blkg(tg_to_put)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1111 | } |
| 1112 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1113 | static int throtl_dispatch_tg(struct throtl_grp *tg) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1114 | { |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1115 | struct throtl_service_queue *sq = &tg->service_queue; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1116 | unsigned int nr_reads = 0, nr_writes = 0; |
| 1117 | unsigned int max_nr_reads = throtl_grp_quantum*3/4; |
Vivek Goyal | c2f6805 | 2010-11-15 19:32:42 +0100 | [diff] [blame] | 1118 | unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1119 | struct bio *bio; |
| 1120 | |
| 1121 | /* Try to dispatch 75% READS and 25% WRITES */ |
| 1122 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1123 | while ((bio = throtl_peek_queued(&sq->queued[READ])) && |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1124 | tg_may_dispatch(tg, bio, NULL)) { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1125 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1126 | tg_dispatch_one_bio(tg, bio_data_dir(bio)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1127 | nr_reads++; |
| 1128 | |
| 1129 | if (nr_reads >= max_nr_reads) |
| 1130 | break; |
| 1131 | } |
| 1132 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1133 | while ((bio = throtl_peek_queued(&sq->queued[WRITE])) && |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1134 | tg_may_dispatch(tg, bio, NULL)) { |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1135 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1136 | tg_dispatch_one_bio(tg, bio_data_dir(bio)); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1137 | nr_writes++; |
| 1138 | |
| 1139 | if (nr_writes >= max_nr_writes) |
| 1140 | break; |
| 1141 | } |
| 1142 | |
| 1143 | return nr_reads + nr_writes; |
| 1144 | } |
| 1145 | |
Tejun Heo | 651930b | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1146 | static int throtl_select_dispatch(struct throtl_service_queue *parent_sq) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1147 | { |
| 1148 | unsigned int nr_disp = 0; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1149 | |
| 1150 | while (1) { |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1151 | struct throtl_grp *tg = throtl_rb_first(parent_sq); |
| 1152 | struct throtl_service_queue *sq = &tg->service_queue; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1153 | |
| 1154 | if (!tg) |
| 1155 | break; |
| 1156 | |
| 1157 | if (time_before(jiffies, tg->disptime)) |
| 1158 | break; |
| 1159 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1160 | throtl_dequeue_tg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1161 | |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1162 | nr_disp += throtl_dispatch_tg(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1163 | |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1164 | if (sq->nr_queued[0] || sq->nr_queued[1]) |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1165 | tg_update_disptime(tg); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1166 | |
| 1167 | if (nr_disp >= throtl_quantum) |
| 1168 | break; |
| 1169 | } |
| 1170 | |
| 1171 | return nr_disp; |
| 1172 | } |
| 1173 | |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1174 | static bool throtl_can_upgrade(struct throtl_data *td, |
| 1175 | struct throtl_grp *this_tg); |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1176 | /** |
| 1177 | * throtl_pending_timer_fn - timer function for service_queue->pending_timer |
| 1178 | * @arg: the throtl_service_queue being serviced |
| 1179 | * |
| 1180 | * This timer is armed when a child throtl_grp with active bio's become |
| 1181 | * pending and queued on the service_queue's pending_tree and expires when |
| 1182 | * the first child throtl_grp should be dispatched. This function |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1183 | * dispatches bio's from the children throtl_grps to the parent |
| 1184 | * service_queue. |
| 1185 | * |
| 1186 | * If the parent's parent is another throtl_grp, dispatching is propagated |
| 1187 | * by either arming its pending_timer or repeating dispatch directly. If |
| 1188 | * the top-level service_tree is reached, throtl_data->dispatch_work is |
| 1189 | * kicked so that the ready bio's are issued. |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1190 | */ |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1191 | static void throtl_pending_timer_fn(unsigned long arg) |
| 1192 | { |
| 1193 | struct throtl_service_queue *sq = (void *)arg; |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1194 | struct throtl_grp *tg = sq_to_tg(sq); |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1195 | struct throtl_data *td = sq_to_td(sq); |
Tejun Heo | cb76199 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 1196 | struct request_queue *q = td->queue; |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1197 | struct throtl_service_queue *parent_sq; |
| 1198 | bool dispatched; |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1199 | int ret; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1200 | |
| 1201 | spin_lock_irq(q->queue_lock); |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1202 | if (throtl_can_upgrade(td, NULL)) |
| 1203 | throtl_upgrade_state(td); |
| 1204 | |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1205 | again: |
| 1206 | parent_sq = sq->parent_sq; |
| 1207 | dispatched = false; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1208 | |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1209 | while (true) { |
| 1210 | throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u", |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1211 | sq->nr_queued[READ] + sq->nr_queued[WRITE], |
| 1212 | sq->nr_queued[READ], sq->nr_queued[WRITE]); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1213 | |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1214 | ret = throtl_select_dispatch(sq); |
| 1215 | if (ret) { |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1216 | throtl_log(sq, "bios disp=%u", ret); |
| 1217 | dispatched = true; |
Tejun Heo | 651930b | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1218 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1219 | |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1220 | if (throtl_schedule_next_dispatch(sq, false)) |
| 1221 | break; |
| 1222 | |
| 1223 | /* this dispatch windows is still open, relax and repeat */ |
| 1224 | spin_unlock_irq(q->queue_lock); |
| 1225 | cpu_relax(); |
| 1226 | spin_lock_irq(q->queue_lock); |
| 1227 | } |
Tejun Heo | 6a52560 | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1228 | |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1229 | if (!dispatched) |
| 1230 | goto out_unlock; |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1231 | |
Tejun Heo | 2e48a53 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1232 | if (parent_sq) { |
| 1233 | /* @parent_sq is another throl_grp, propagate dispatch */ |
| 1234 | if (tg->flags & THROTL_TG_WAS_EMPTY) { |
| 1235 | tg_update_disptime(tg); |
| 1236 | if (!throtl_schedule_next_dispatch(parent_sq, false)) { |
| 1237 | /* window is already open, repeat dispatching */ |
| 1238 | sq = parent_sq; |
| 1239 | tg = sq_to_tg(sq); |
| 1240 | goto again; |
| 1241 | } |
| 1242 | } |
| 1243 | } else { |
| 1244 | /* reached the top-level, queue issueing */ |
| 1245 | queue_work(kthrotld_workqueue, &td->dispatch_work); |
| 1246 | } |
| 1247 | out_unlock: |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1248 | spin_unlock_irq(q->queue_lock); |
| 1249 | } |
| 1250 | |
| 1251 | /** |
| 1252 | * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work |
| 1253 | * @work: work item being executed |
| 1254 | * |
| 1255 | * This function is queued for execution when bio's reach the bio_lists[] |
| 1256 | * of throtl_data->service_queue. Those bio's are ready and issued by this |
| 1257 | * function. |
| 1258 | */ |
Fabian Frederick | 8876e14 | 2014-04-17 21:41:16 +0200 | [diff] [blame] | 1259 | static void blk_throtl_dispatch_work_fn(struct work_struct *work) |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1260 | { |
| 1261 | struct throtl_data *td = container_of(work, struct throtl_data, |
| 1262 | dispatch_work); |
| 1263 | struct throtl_service_queue *td_sq = &td->service_queue; |
| 1264 | struct request_queue *q = td->queue; |
| 1265 | struct bio_list bio_list_on_stack; |
| 1266 | struct bio *bio; |
| 1267 | struct blk_plug plug; |
| 1268 | int rw; |
| 1269 | |
| 1270 | bio_list_init(&bio_list_on_stack); |
| 1271 | |
| 1272 | spin_lock_irq(q->queue_lock); |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1273 | for (rw = READ; rw <= WRITE; rw++) |
| 1274 | while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL))) |
| 1275 | bio_list_add(&bio_list_on_stack, bio); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1276 | spin_unlock_irq(q->queue_lock); |
| 1277 | |
Tejun Heo | 6e1a570 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1278 | if (!bio_list_empty(&bio_list_on_stack)) { |
Vivek Goyal | 69d60eb | 2011-03-09 08:27:37 +0100 | [diff] [blame] | 1279 | blk_start_plug(&plug); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1280 | while((bio = bio_list_pop(&bio_list_on_stack))) |
| 1281 | generic_make_request(bio); |
Vivek Goyal | 69d60eb | 2011-03-09 08:27:37 +0100 | [diff] [blame] | 1282 | blk_finish_plug(&plug); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1283 | } |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1284 | } |
| 1285 | |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1286 | static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd, |
| 1287 | int off) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1288 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1289 | struct throtl_grp *tg = pd_to_tg(pd); |
| 1290 | u64 v = *(u64 *)((void *)tg + off); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1291 | |
Shaohua Li | 2ab5492 | 2017-03-27 10:51:29 -0700 | [diff] [blame] | 1292 | if (v == U64_MAX) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1293 | return 0; |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1294 | return __blkg_prfill_u64(sf, pd, v); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1297 | static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd, |
| 1298 | int off) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1299 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1300 | struct throtl_grp *tg = pd_to_tg(pd); |
| 1301 | unsigned int v = *(unsigned int *)((void *)tg + off); |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1302 | |
Shaohua Li | 2ab5492 | 2017-03-27 10:51:29 -0700 | [diff] [blame] | 1303 | if (v == UINT_MAX) |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1304 | return 0; |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1305 | return __blkg_prfill_u64(sf, pd, v); |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1308 | static int tg_print_conf_u64(struct seq_file *sf, void *v) |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1309 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1310 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64, |
| 1311 | &blkcg_policy_throtl, seq_cft(sf)->private, false); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1312 | return 0; |
| 1313 | } |
| 1314 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1315 | static int tg_print_conf_uint(struct seq_file *sf, void *v) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1316 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1317 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint, |
| 1318 | &blkcg_policy_throtl, seq_cft(sf)->private, false); |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1319 | return 0; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1320 | } |
| 1321 | |
Tejun Heo | 69948b0 | 2015-08-18 14:55:32 -0700 | [diff] [blame] | 1322 | static void tg_conf_updated(struct throtl_grp *tg) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1323 | { |
Tejun Heo | 69948b0 | 2015-08-18 14:55:32 -0700 | [diff] [blame] | 1324 | struct throtl_service_queue *sq = &tg->service_queue; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 1325 | struct cgroup_subsys_state *pos_css; |
Tejun Heo | 69948b0 | 2015-08-18 14:55:32 -0700 | [diff] [blame] | 1326 | struct blkcg_gq *blkg; |
Tejun Heo | af133ce | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1327 | |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1328 | throtl_log(&tg->service_queue, |
| 1329 | "limit change rbps=%llu wbps=%llu riops=%u wiops=%u", |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1330 | tg_bps_limit(tg, READ), tg_bps_limit(tg, WRITE), |
| 1331 | tg_iops_limit(tg, READ), tg_iops_limit(tg, WRITE)); |
Tejun Heo | 632b449 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 1332 | |
| 1333 | /* |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1334 | * Update has_rules[] flags for the updated tg's subtree. A tg is |
| 1335 | * considered to have rules if either the tg itself or any of its |
| 1336 | * ancestors has rules. This identifies groups without any |
| 1337 | * restrictions in the whole hierarchy and allows them to bypass |
| 1338 | * blk-throttle. |
| 1339 | */ |
Tejun Heo | 69948b0 | 2015-08-18 14:55:32 -0700 | [diff] [blame] | 1340 | blkg_for_each_descendant_pre(blkg, pos_css, tg_to_blkg(tg)) |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1341 | tg_update_has_rules(blkg_to_tg(blkg)); |
| 1342 | |
| 1343 | /* |
Tejun Heo | 632b449 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 1344 | * We're already holding queue_lock and know @tg is valid. Let's |
| 1345 | * apply the new config directly. |
| 1346 | * |
| 1347 | * Restart the slices for both READ and WRITES. It might happen |
| 1348 | * that a group's limit are dropped suddenly and we don't want to |
| 1349 | * account recently dispatched IO with new low rate. |
| 1350 | */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1351 | throtl_start_new_slice(tg, 0); |
| 1352 | throtl_start_new_slice(tg, 1); |
Tejun Heo | 632b449 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 1353 | |
Tejun Heo | 5b2c16a | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1354 | if (tg->flags & THROTL_TG_PENDING) { |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1355 | tg_update_disptime(tg); |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1356 | throtl_schedule_next_dispatch(sq->parent_sq, true); |
Tejun Heo | 632b449 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 1357 | } |
Tejun Heo | 69948b0 | 2015-08-18 14:55:32 -0700 | [diff] [blame] | 1358 | } |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1359 | |
Tejun Heo | 69948b0 | 2015-08-18 14:55:32 -0700 | [diff] [blame] | 1360 | static ssize_t tg_set_conf(struct kernfs_open_file *of, |
| 1361 | char *buf, size_t nbytes, loff_t off, bool is_u64) |
| 1362 | { |
| 1363 | struct blkcg *blkcg = css_to_blkcg(of_css(of)); |
| 1364 | struct blkg_conf_ctx ctx; |
| 1365 | struct throtl_grp *tg; |
| 1366 | int ret; |
| 1367 | u64 v; |
| 1368 | |
| 1369 | ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx); |
| 1370 | if (ret) |
| 1371 | return ret; |
| 1372 | |
| 1373 | ret = -EINVAL; |
| 1374 | if (sscanf(ctx.body, "%llu", &v) != 1) |
| 1375 | goto out_finish; |
| 1376 | if (!v) |
Shaohua Li | 2ab5492 | 2017-03-27 10:51:29 -0700 | [diff] [blame] | 1377 | v = U64_MAX; |
Tejun Heo | 69948b0 | 2015-08-18 14:55:32 -0700 | [diff] [blame] | 1378 | |
| 1379 | tg = blkg_to_tg(ctx.blkg); |
| 1380 | |
| 1381 | if (is_u64) |
| 1382 | *(u64 *)((void *)tg + of_cft(of)->private) = v; |
| 1383 | else |
| 1384 | *(unsigned int *)((void *)tg + of_cft(of)->private) = v; |
| 1385 | |
| 1386 | tg_conf_updated(tg); |
Tejun Heo | 36aa9e5 | 2015-08-18 14:55:31 -0700 | [diff] [blame] | 1387 | ret = 0; |
| 1388 | out_finish: |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1389 | blkg_conf_finish(&ctx); |
Tejun Heo | 36aa9e5 | 2015-08-18 14:55:31 -0700 | [diff] [blame] | 1390 | return ret ?: nbytes; |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1393 | static ssize_t tg_set_conf_u64(struct kernfs_open_file *of, |
| 1394 | char *buf, size_t nbytes, loff_t off) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1395 | { |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1396 | return tg_set_conf(of, buf, nbytes, off, true); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1399 | static ssize_t tg_set_conf_uint(struct kernfs_open_file *of, |
| 1400 | char *buf, size_t nbytes, loff_t off) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1401 | { |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1402 | return tg_set_conf(of, buf, nbytes, off, false); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
Tejun Heo | 880f50e | 2015-08-18 14:55:30 -0700 | [diff] [blame] | 1405 | static struct cftype throtl_legacy_files[] = { |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1406 | { |
| 1407 | .name = "throttle.read_bps_device", |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1408 | .private = offsetof(struct throtl_grp, bps[READ][LIMIT_MAX]), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1409 | .seq_show = tg_print_conf_u64, |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1410 | .write = tg_set_conf_u64, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1411 | }, |
| 1412 | { |
| 1413 | .name = "throttle.write_bps_device", |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1414 | .private = offsetof(struct throtl_grp, bps[WRITE][LIMIT_MAX]), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1415 | .seq_show = tg_print_conf_u64, |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1416 | .write = tg_set_conf_u64, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1417 | }, |
| 1418 | { |
| 1419 | .name = "throttle.read_iops_device", |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1420 | .private = offsetof(struct throtl_grp, iops[READ][LIMIT_MAX]), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1421 | .seq_show = tg_print_conf_uint, |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1422 | .write = tg_set_conf_uint, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1423 | }, |
| 1424 | { |
| 1425 | .name = "throttle.write_iops_device", |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1426 | .private = offsetof(struct throtl_grp, iops[WRITE][LIMIT_MAX]), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1427 | .seq_show = tg_print_conf_uint, |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1428 | .write = tg_set_conf_uint, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1429 | }, |
| 1430 | { |
| 1431 | .name = "throttle.io_service_bytes", |
Tejun Heo | 77ea733 | 2015-08-18 14:55:24 -0700 | [diff] [blame] | 1432 | .private = (unsigned long)&blkcg_policy_throtl, |
| 1433 | .seq_show = blkg_print_stat_bytes, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1434 | }, |
| 1435 | { |
| 1436 | .name = "throttle.io_serviced", |
Tejun Heo | 77ea733 | 2015-08-18 14:55:24 -0700 | [diff] [blame] | 1437 | .private = (unsigned long)&blkcg_policy_throtl, |
| 1438 | .seq_show = blkg_print_stat_ios, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1439 | }, |
| 1440 | { } /* terminate */ |
| 1441 | }; |
| 1442 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1443 | static u64 tg_prfill_limit(struct seq_file *sf, struct blkg_policy_data *pd, |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1444 | int off) |
| 1445 | { |
| 1446 | struct throtl_grp *tg = pd_to_tg(pd); |
| 1447 | const char *dname = blkg_dev_name(pd->blkg); |
| 1448 | char bufs[4][21] = { "max", "max", "max", "max" }; |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1449 | u64 bps_dft; |
| 1450 | unsigned int iops_dft; |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame^] | 1451 | char idle_time[26] = ""; |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1452 | |
| 1453 | if (!dname) |
| 1454 | return 0; |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1455 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1456 | if (off == LIMIT_LOW) { |
| 1457 | bps_dft = 0; |
| 1458 | iops_dft = 0; |
| 1459 | } else { |
| 1460 | bps_dft = U64_MAX; |
| 1461 | iops_dft = UINT_MAX; |
| 1462 | } |
| 1463 | |
| 1464 | if (tg->bps_conf[READ][off] == bps_dft && |
| 1465 | tg->bps_conf[WRITE][off] == bps_dft && |
| 1466 | tg->iops_conf[READ][off] == iops_dft && |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame^] | 1467 | tg->iops_conf[WRITE][off] == iops_dft && |
| 1468 | (off != LIMIT_LOW || tg->idletime_threshold == |
| 1469 | tg->td->dft_idletime_threshold)) |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1470 | return 0; |
| 1471 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1472 | if (tg->bps_conf[READ][off] != bps_dft) |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1473 | snprintf(bufs[0], sizeof(bufs[0]), "%llu", |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1474 | tg->bps_conf[READ][off]); |
| 1475 | if (tg->bps_conf[WRITE][off] != bps_dft) |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1476 | snprintf(bufs[1], sizeof(bufs[1]), "%llu", |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1477 | tg->bps_conf[WRITE][off]); |
| 1478 | if (tg->iops_conf[READ][off] != iops_dft) |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1479 | snprintf(bufs[2], sizeof(bufs[2]), "%u", |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1480 | tg->iops_conf[READ][off]); |
| 1481 | if (tg->iops_conf[WRITE][off] != iops_dft) |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1482 | snprintf(bufs[3], sizeof(bufs[3]), "%u", |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1483 | tg->iops_conf[WRITE][off]); |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame^] | 1484 | if (off == LIMIT_LOW) { |
| 1485 | if (tg->idletime_threshold == ULONG_MAX) |
| 1486 | strcpy(idle_time, " idle=max"); |
| 1487 | else |
| 1488 | snprintf(idle_time, sizeof(idle_time), " idle=%lu", |
| 1489 | tg->idletime_threshold); |
| 1490 | } |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1491 | |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame^] | 1492 | seq_printf(sf, "%s rbps=%s wbps=%s riops=%s wiops=%s%s\n", |
| 1493 | dname, bufs[0], bufs[1], bufs[2], bufs[3], idle_time); |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1494 | return 0; |
| 1495 | } |
| 1496 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1497 | static int tg_print_limit(struct seq_file *sf, void *v) |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1498 | { |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1499 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_limit, |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1500 | &blkcg_policy_throtl, seq_cft(sf)->private, false); |
| 1501 | return 0; |
| 1502 | } |
| 1503 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1504 | static ssize_t tg_set_limit(struct kernfs_open_file *of, |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1505 | char *buf, size_t nbytes, loff_t off) |
| 1506 | { |
| 1507 | struct blkcg *blkcg = css_to_blkcg(of_css(of)); |
| 1508 | struct blkg_conf_ctx ctx; |
| 1509 | struct throtl_grp *tg; |
| 1510 | u64 v[4]; |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame^] | 1511 | unsigned long idle_time; |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1512 | int ret; |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1513 | int index = of_cft(of)->private; |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1514 | |
| 1515 | ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx); |
| 1516 | if (ret) |
| 1517 | return ret; |
| 1518 | |
| 1519 | tg = blkg_to_tg(ctx.blkg); |
| 1520 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1521 | v[0] = tg->bps_conf[READ][index]; |
| 1522 | v[1] = tg->bps_conf[WRITE][index]; |
| 1523 | v[2] = tg->iops_conf[READ][index]; |
| 1524 | v[3] = tg->iops_conf[WRITE][index]; |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1525 | |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame^] | 1526 | idle_time = tg->idletime_threshold; |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1527 | while (true) { |
| 1528 | char tok[27]; /* wiops=18446744073709551616 */ |
| 1529 | char *p; |
Shaohua Li | 2ab5492 | 2017-03-27 10:51:29 -0700 | [diff] [blame] | 1530 | u64 val = U64_MAX; |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1531 | int len; |
| 1532 | |
| 1533 | if (sscanf(ctx.body, "%26s%n", tok, &len) != 1) |
| 1534 | break; |
| 1535 | if (tok[0] == '\0') |
| 1536 | break; |
| 1537 | ctx.body += len; |
| 1538 | |
| 1539 | ret = -EINVAL; |
| 1540 | p = tok; |
| 1541 | strsep(&p, "="); |
| 1542 | if (!p || (sscanf(p, "%llu", &val) != 1 && strcmp(p, "max"))) |
| 1543 | goto out_finish; |
| 1544 | |
| 1545 | ret = -ERANGE; |
| 1546 | if (!val) |
| 1547 | goto out_finish; |
| 1548 | |
| 1549 | ret = -EINVAL; |
| 1550 | if (!strcmp(tok, "rbps")) |
| 1551 | v[0] = val; |
| 1552 | else if (!strcmp(tok, "wbps")) |
| 1553 | v[1] = val; |
| 1554 | else if (!strcmp(tok, "riops")) |
| 1555 | v[2] = min_t(u64, val, UINT_MAX); |
| 1556 | else if (!strcmp(tok, "wiops")) |
| 1557 | v[3] = min_t(u64, val, UINT_MAX); |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame^] | 1558 | else if (off == LIMIT_LOW && !strcmp(tok, "idle")) |
| 1559 | idle_time = val; |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1560 | else |
| 1561 | goto out_finish; |
| 1562 | } |
| 1563 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1564 | tg->bps_conf[READ][index] = v[0]; |
| 1565 | tg->bps_conf[WRITE][index] = v[1]; |
| 1566 | tg->iops_conf[READ][index] = v[2]; |
| 1567 | tg->iops_conf[WRITE][index] = v[3]; |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1568 | |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1569 | if (index == LIMIT_MAX) { |
| 1570 | tg->bps[READ][index] = v[0]; |
| 1571 | tg->bps[WRITE][index] = v[1]; |
| 1572 | tg->iops[READ][index] = v[2]; |
| 1573 | tg->iops[WRITE][index] = v[3]; |
| 1574 | } |
| 1575 | tg->bps[READ][LIMIT_LOW] = min(tg->bps_conf[READ][LIMIT_LOW], |
| 1576 | tg->bps_conf[READ][LIMIT_MAX]); |
| 1577 | tg->bps[WRITE][LIMIT_LOW] = min(tg->bps_conf[WRITE][LIMIT_LOW], |
| 1578 | tg->bps_conf[WRITE][LIMIT_MAX]); |
| 1579 | tg->iops[READ][LIMIT_LOW] = min(tg->iops_conf[READ][LIMIT_LOW], |
| 1580 | tg->iops_conf[READ][LIMIT_MAX]); |
| 1581 | tg->iops[WRITE][LIMIT_LOW] = min(tg->iops_conf[WRITE][LIMIT_LOW], |
| 1582 | tg->iops_conf[WRITE][LIMIT_MAX]); |
| 1583 | |
| 1584 | if (index == LIMIT_LOW) { |
| 1585 | blk_throtl_update_limit_valid(tg->td); |
| 1586 | if (tg->td->limit_valid[LIMIT_LOW]) |
| 1587 | tg->td->limit_index = LIMIT_LOW; |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame^] | 1588 | tg->idletime_threshold = (idle_time == ULONG_MAX) ? |
| 1589 | ULONG_MAX : idle_time; |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1590 | } |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1591 | tg_conf_updated(tg); |
| 1592 | ret = 0; |
| 1593 | out_finish: |
| 1594 | blkg_conf_finish(&ctx); |
| 1595 | return ret ?: nbytes; |
| 1596 | } |
| 1597 | |
| 1598 | static struct cftype throtl_files[] = { |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1599 | #ifdef CONFIG_BLK_DEV_THROTTLING_LOW |
| 1600 | { |
| 1601 | .name = "low", |
| 1602 | .flags = CFTYPE_NOT_ON_ROOT, |
| 1603 | .seq_show = tg_print_limit, |
| 1604 | .write = tg_set_limit, |
| 1605 | .private = LIMIT_LOW, |
| 1606 | }, |
| 1607 | #endif |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1608 | { |
| 1609 | .name = "max", |
| 1610 | .flags = CFTYPE_NOT_ON_ROOT, |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1611 | .seq_show = tg_print_limit, |
| 1612 | .write = tg_set_limit, |
| 1613 | .private = LIMIT_MAX, |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1614 | }, |
| 1615 | { } /* terminate */ |
| 1616 | }; |
| 1617 | |
Vivek Goyal | da52777 | 2011-03-02 19:05:33 -0500 | [diff] [blame] | 1618 | static void throtl_shutdown_wq(struct request_queue *q) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1619 | { |
| 1620 | struct throtl_data *td = q->td; |
| 1621 | |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1622 | cancel_work_sync(&td->dispatch_work); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1623 | } |
| 1624 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1625 | static struct blkcg_policy blkcg_policy_throtl = { |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1626 | .dfl_cftypes = throtl_files, |
Tejun Heo | 880f50e | 2015-08-18 14:55:30 -0700 | [diff] [blame] | 1627 | .legacy_cftypes = throtl_legacy_files, |
Tejun Heo | f9fcc2d | 2012-04-16 13:57:27 -0700 | [diff] [blame] | 1628 | |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 1629 | .pd_alloc_fn = throtl_pd_alloc, |
Tejun Heo | f9fcc2d | 2012-04-16 13:57:27 -0700 | [diff] [blame] | 1630 | .pd_init_fn = throtl_pd_init, |
Tejun Heo | 693e751 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1631 | .pd_online_fn = throtl_pd_online, |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 1632 | .pd_offline_fn = throtl_pd_offline, |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 1633 | .pd_free_fn = throtl_pd_free, |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1634 | }; |
| 1635 | |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1636 | static unsigned long __tg_last_low_overflow_time(struct throtl_grp *tg) |
| 1637 | { |
| 1638 | unsigned long rtime = jiffies, wtime = jiffies; |
| 1639 | |
| 1640 | if (tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW]) |
| 1641 | rtime = tg->last_low_overflow_time[READ]; |
| 1642 | if (tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW]) |
| 1643 | wtime = tg->last_low_overflow_time[WRITE]; |
| 1644 | return min(rtime, wtime); |
| 1645 | } |
| 1646 | |
| 1647 | /* tg should not be an intermediate node */ |
| 1648 | static unsigned long tg_last_low_overflow_time(struct throtl_grp *tg) |
| 1649 | { |
| 1650 | struct throtl_service_queue *parent_sq; |
| 1651 | struct throtl_grp *parent = tg; |
| 1652 | unsigned long ret = __tg_last_low_overflow_time(tg); |
| 1653 | |
| 1654 | while (true) { |
| 1655 | parent_sq = parent->service_queue.parent_sq; |
| 1656 | parent = sq_to_tg(parent_sq); |
| 1657 | if (!parent) |
| 1658 | break; |
| 1659 | |
| 1660 | /* |
| 1661 | * The parent doesn't have low limit, it always reaches low |
| 1662 | * limit. Its overflow time is useless for children |
| 1663 | */ |
| 1664 | if (!parent->bps[READ][LIMIT_LOW] && |
| 1665 | !parent->iops[READ][LIMIT_LOW] && |
| 1666 | !parent->bps[WRITE][LIMIT_LOW] && |
| 1667 | !parent->iops[WRITE][LIMIT_LOW]) |
| 1668 | continue; |
| 1669 | if (time_after(__tg_last_low_overflow_time(parent), ret)) |
| 1670 | ret = __tg_last_low_overflow_time(parent); |
| 1671 | } |
| 1672 | return ret; |
| 1673 | } |
| 1674 | |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 1675 | static bool throtl_tg_is_idle(struct throtl_grp *tg) |
| 1676 | { |
| 1677 | /* |
| 1678 | * cgroup is idle if: |
| 1679 | * - single idle is too long, longer than a fixed value (in case user |
| 1680 | * configure a too big threshold) or 4 times of slice |
| 1681 | * - average think time is more than threshold |
| 1682 | */ |
| 1683 | unsigned long time = jiffies_to_usecs(4 * tg->td->throtl_slice); |
| 1684 | |
| 1685 | time = min_t(unsigned long, MAX_IDLE_TIME, time); |
| 1686 | return (ktime_get_ns() >> 10) - tg->last_finish_time > time || |
| 1687 | tg->avg_idletime > tg->idletime_threshold; |
| 1688 | } |
| 1689 | |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1690 | static bool throtl_tg_can_upgrade(struct throtl_grp *tg) |
| 1691 | { |
| 1692 | struct throtl_service_queue *sq = &tg->service_queue; |
| 1693 | bool read_limit, write_limit; |
| 1694 | |
| 1695 | /* |
| 1696 | * if cgroup reaches low limit (if low limit is 0, the cgroup always |
| 1697 | * reaches), it's ok to upgrade to next limit |
| 1698 | */ |
| 1699 | read_limit = tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW]; |
| 1700 | write_limit = tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW]; |
| 1701 | if (!read_limit && !write_limit) |
| 1702 | return true; |
| 1703 | if (read_limit && sq->nr_queued[READ] && |
| 1704 | (!write_limit || sq->nr_queued[WRITE])) |
| 1705 | return true; |
| 1706 | if (write_limit && sq->nr_queued[WRITE] && |
| 1707 | (!read_limit || sq->nr_queued[READ])) |
| 1708 | return true; |
Shaohua Li | aec2424 | 2017-03-27 10:51:39 -0700 | [diff] [blame] | 1709 | |
| 1710 | if (time_after_eq(jiffies, |
| 1711 | tg->last_dispatch_time[READ] + tg->td->throtl_slice) && |
| 1712 | time_after_eq(jiffies, |
| 1713 | tg->last_dispatch_time[WRITE] + tg->td->throtl_slice)) |
| 1714 | return true; |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1715 | return false; |
| 1716 | } |
| 1717 | |
| 1718 | static bool throtl_hierarchy_can_upgrade(struct throtl_grp *tg) |
| 1719 | { |
| 1720 | while (true) { |
| 1721 | if (throtl_tg_can_upgrade(tg)) |
| 1722 | return true; |
| 1723 | tg = sq_to_tg(tg->service_queue.parent_sq); |
| 1724 | if (!tg || !tg_to_blkg(tg)->parent) |
| 1725 | return false; |
| 1726 | } |
| 1727 | return false; |
| 1728 | } |
| 1729 | |
| 1730 | static bool throtl_can_upgrade(struct throtl_data *td, |
| 1731 | struct throtl_grp *this_tg) |
| 1732 | { |
| 1733 | struct cgroup_subsys_state *pos_css; |
| 1734 | struct blkcg_gq *blkg; |
| 1735 | |
| 1736 | if (td->limit_index != LIMIT_LOW) |
| 1737 | return false; |
| 1738 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 1739 | if (time_before(jiffies, td->low_downgrade_time + td->throtl_slice)) |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1740 | return false; |
| 1741 | |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1742 | rcu_read_lock(); |
| 1743 | blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) { |
| 1744 | struct throtl_grp *tg = blkg_to_tg(blkg); |
| 1745 | |
| 1746 | if (tg == this_tg) |
| 1747 | continue; |
| 1748 | if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children)) |
| 1749 | continue; |
| 1750 | if (!throtl_hierarchy_can_upgrade(tg)) { |
| 1751 | rcu_read_unlock(); |
| 1752 | return false; |
| 1753 | } |
| 1754 | } |
| 1755 | rcu_read_unlock(); |
| 1756 | return true; |
| 1757 | } |
| 1758 | |
| 1759 | static void throtl_upgrade_state(struct throtl_data *td) |
| 1760 | { |
| 1761 | struct cgroup_subsys_state *pos_css; |
| 1762 | struct blkcg_gq *blkg; |
| 1763 | |
| 1764 | td->limit_index = LIMIT_MAX; |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1765 | td->low_upgrade_time = jiffies; |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 1766 | td->scale = 0; |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1767 | rcu_read_lock(); |
| 1768 | blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) { |
| 1769 | struct throtl_grp *tg = blkg_to_tg(blkg); |
| 1770 | struct throtl_service_queue *sq = &tg->service_queue; |
| 1771 | |
| 1772 | tg->disptime = jiffies - 1; |
| 1773 | throtl_select_dispatch(sq); |
| 1774 | throtl_schedule_next_dispatch(sq, false); |
| 1775 | } |
| 1776 | rcu_read_unlock(); |
| 1777 | throtl_select_dispatch(&td->service_queue); |
| 1778 | throtl_schedule_next_dispatch(&td->service_queue, false); |
| 1779 | queue_work(kthrotld_workqueue, &td->dispatch_work); |
| 1780 | } |
| 1781 | |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1782 | static void throtl_downgrade_state(struct throtl_data *td, int new) |
| 1783 | { |
Shaohua Li | 7394e31 | 2017-03-27 10:51:40 -0700 | [diff] [blame] | 1784 | td->scale /= 2; |
| 1785 | |
| 1786 | if (td->scale) { |
| 1787 | td->low_upgrade_time = jiffies - td->scale * td->throtl_slice; |
| 1788 | return; |
| 1789 | } |
| 1790 | |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1791 | td->limit_index = new; |
| 1792 | td->low_downgrade_time = jiffies; |
| 1793 | } |
| 1794 | |
| 1795 | static bool throtl_tg_can_downgrade(struct throtl_grp *tg) |
| 1796 | { |
| 1797 | struct throtl_data *td = tg->td; |
| 1798 | unsigned long now = jiffies; |
| 1799 | |
Shaohua Li | aec2424 | 2017-03-27 10:51:39 -0700 | [diff] [blame] | 1800 | if (time_after_eq(now, tg->last_dispatch_time[READ] + |
| 1801 | td->throtl_slice) && |
| 1802 | time_after_eq(now, tg->last_dispatch_time[WRITE] + |
| 1803 | td->throtl_slice)) |
| 1804 | return false; |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1805 | /* |
| 1806 | * If cgroup is below low limit, consider downgrade and throttle other |
| 1807 | * cgroups |
| 1808 | */ |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 1809 | if (time_after_eq(now, td->low_upgrade_time + td->throtl_slice) && |
| 1810 | time_after_eq(now, tg_last_low_overflow_time(tg) + |
| 1811 | td->throtl_slice)) |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1812 | return true; |
| 1813 | return false; |
| 1814 | } |
| 1815 | |
| 1816 | static bool throtl_hierarchy_can_downgrade(struct throtl_grp *tg) |
| 1817 | { |
| 1818 | while (true) { |
| 1819 | if (!throtl_tg_can_downgrade(tg)) |
| 1820 | return false; |
| 1821 | tg = sq_to_tg(tg->service_queue.parent_sq); |
| 1822 | if (!tg || !tg_to_blkg(tg)->parent) |
| 1823 | break; |
| 1824 | } |
| 1825 | return true; |
| 1826 | } |
| 1827 | |
| 1828 | static void throtl_downgrade_check(struct throtl_grp *tg) |
| 1829 | { |
| 1830 | uint64_t bps; |
| 1831 | unsigned int iops; |
| 1832 | unsigned long elapsed_time; |
| 1833 | unsigned long now = jiffies; |
| 1834 | |
| 1835 | if (tg->td->limit_index != LIMIT_MAX || |
| 1836 | !tg->td->limit_valid[LIMIT_LOW]) |
| 1837 | return; |
| 1838 | if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children)) |
| 1839 | return; |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 1840 | if (time_after(tg->last_check_time + tg->td->throtl_slice, now)) |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1841 | return; |
| 1842 | |
| 1843 | elapsed_time = now - tg->last_check_time; |
| 1844 | tg->last_check_time = now; |
| 1845 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 1846 | if (time_before(now, tg_last_low_overflow_time(tg) + |
| 1847 | tg->td->throtl_slice)) |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1848 | return; |
| 1849 | |
| 1850 | if (tg->bps[READ][LIMIT_LOW]) { |
| 1851 | bps = tg->last_bytes_disp[READ] * HZ; |
| 1852 | do_div(bps, elapsed_time); |
| 1853 | if (bps >= tg->bps[READ][LIMIT_LOW]) |
| 1854 | tg->last_low_overflow_time[READ] = now; |
| 1855 | } |
| 1856 | |
| 1857 | if (tg->bps[WRITE][LIMIT_LOW]) { |
| 1858 | bps = tg->last_bytes_disp[WRITE] * HZ; |
| 1859 | do_div(bps, elapsed_time); |
| 1860 | if (bps >= tg->bps[WRITE][LIMIT_LOW]) |
| 1861 | tg->last_low_overflow_time[WRITE] = now; |
| 1862 | } |
| 1863 | |
| 1864 | if (tg->iops[READ][LIMIT_LOW]) { |
| 1865 | iops = tg->last_io_disp[READ] * HZ / elapsed_time; |
| 1866 | if (iops >= tg->iops[READ][LIMIT_LOW]) |
| 1867 | tg->last_low_overflow_time[READ] = now; |
| 1868 | } |
| 1869 | |
| 1870 | if (tg->iops[WRITE][LIMIT_LOW]) { |
| 1871 | iops = tg->last_io_disp[WRITE] * HZ / elapsed_time; |
| 1872 | if (iops >= tg->iops[WRITE][LIMIT_LOW]) |
| 1873 | tg->last_low_overflow_time[WRITE] = now; |
| 1874 | } |
| 1875 | |
| 1876 | /* |
| 1877 | * If cgroup is below low limit, consider downgrade and throttle other |
| 1878 | * cgroups |
| 1879 | */ |
| 1880 | if (throtl_hierarchy_can_downgrade(tg)) |
| 1881 | throtl_downgrade_state(tg->td, LIMIT_LOW); |
| 1882 | |
| 1883 | tg->last_bytes_disp[READ] = 0; |
| 1884 | tg->last_bytes_disp[WRITE] = 0; |
| 1885 | tg->last_io_disp[READ] = 0; |
| 1886 | tg->last_io_disp[WRITE] = 0; |
| 1887 | } |
| 1888 | |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 1889 | static void blk_throtl_update_idletime(struct throtl_grp *tg) |
| 1890 | { |
| 1891 | unsigned long now = ktime_get_ns() >> 10; |
| 1892 | unsigned long last_finish_time = tg->last_finish_time; |
| 1893 | |
| 1894 | if (now <= last_finish_time || last_finish_time == 0 || |
| 1895 | last_finish_time == tg->checked_last_finish_time) |
| 1896 | return; |
| 1897 | |
| 1898 | tg->avg_idletime = (tg->avg_idletime * 7 + now - last_finish_time) >> 3; |
| 1899 | tg->checked_last_finish_time = last_finish_time; |
| 1900 | } |
| 1901 | |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 1902 | bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg, |
| 1903 | struct bio *bio) |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1904 | { |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1905 | struct throtl_qnode *qn = NULL; |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 1906 | struct throtl_grp *tg = blkg_to_tg(blkg ?: q->root_blkg); |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1907 | struct throtl_service_queue *sq; |
Tejun Heo | 0e9f416 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1908 | bool rw = bio_data_dir(bio); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1909 | bool throttled = false; |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 1910 | int ret; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1911 | |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 1912 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 1913 | |
Tejun Heo | 2a0f61e | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1914 | /* see throtl_charge_bio() */ |
Christoph Hellwig | 8d2bbd4 | 2016-10-20 15:12:12 +0200 | [diff] [blame] | 1915 | if (bio_flagged(bio, BIO_THROTTLED) || !tg->has_rules[rw]) |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1916 | goto out; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1917 | |
| 1918 | spin_lock_irq(q->queue_lock); |
Tejun Heo | c9589f0 | 2015-08-18 14:55:19 -0700 | [diff] [blame] | 1919 | |
| 1920 | if (unlikely(blk_queue_bypass(q))) |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1921 | goto out_unlock; |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 1922 | |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 1923 | ret = bio_associate_current(bio); |
| 1924 | #ifdef CONFIG_BLK_DEV_THROTTLING_LOW |
| 1925 | if (ret == 0 || ret == -EBUSY) |
| 1926 | bio->bi_cg_private = tg; |
| 1927 | #endif |
| 1928 | blk_throtl_update_idletime(tg); |
| 1929 | |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 1930 | sq = &tg->service_queue; |
| 1931 | |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1932 | again: |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1933 | while (true) { |
Shaohua Li | aec2424 | 2017-03-27 10:51:39 -0700 | [diff] [blame] | 1934 | tg->last_dispatch_time[rw] = jiffies; |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1935 | if (tg->last_low_overflow_time[rw] == 0) |
| 1936 | tg->last_low_overflow_time[rw] = jiffies; |
| 1937 | throtl_downgrade_check(tg); |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1938 | /* throtl is FIFO - if bios are already queued, should queue */ |
| 1939 | if (sq->nr_queued[rw]) |
| 1940 | break; |
Vivek Goyal | de701c7 | 2011-03-07 21:09:32 +0100 | [diff] [blame] | 1941 | |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1942 | /* if above limits, break to queue */ |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1943 | if (!tg_may_dispatch(tg, bio, NULL)) { |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1944 | tg->last_low_overflow_time[rw] = jiffies; |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1945 | if (throtl_can_upgrade(tg->td, tg)) { |
| 1946 | throtl_upgrade_state(tg->td); |
| 1947 | goto again; |
| 1948 | } |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1949 | break; |
Shaohua Li | c79892c | 2017-03-27 10:51:34 -0700 | [diff] [blame] | 1950 | } |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1951 | |
| 1952 | /* within limits, let's charge and dispatch directly */ |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1953 | throtl_charge_bio(tg, bio); |
Vivek Goyal | 04521db | 2011-03-22 21:54:29 +0100 | [diff] [blame] | 1954 | |
| 1955 | /* |
| 1956 | * We need to trim slice even when bios are not being queued |
| 1957 | * otherwise it might happen that a bio is not queued for |
| 1958 | * a long time and slice keeps on extending and trim is not |
| 1959 | * called for a long time. Now if limits are reduced suddenly |
| 1960 | * we take into account all the IO dispatched so far at new |
| 1961 | * low rate and * newly queued IO gets a really long dispatch |
| 1962 | * time. |
| 1963 | * |
| 1964 | * So keep on trimming slice even if bio is not queued. |
| 1965 | */ |
Tejun Heo | 0f3457f | 2013-05-14 13:52:32 -0700 | [diff] [blame] | 1966 | throtl_trim_slice(tg, rw); |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1967 | |
| 1968 | /* |
| 1969 | * @bio passed through this layer without being throttled. |
| 1970 | * Climb up the ladder. If we''re already at the top, it |
| 1971 | * can be executed directly. |
| 1972 | */ |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1973 | qn = &tg->qnode_on_parent[rw]; |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1974 | sq = sq->parent_sq; |
| 1975 | tg = sq_to_tg(sq); |
| 1976 | if (!tg) |
| 1977 | goto out_unlock; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1978 | } |
| 1979 | |
Tejun Heo | 9e660ac | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1980 | /* out-of-limit, queue to @tg */ |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1981 | throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d", |
| 1982 | rw == READ ? 'R' : 'W', |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 1983 | tg->bytes_disp[rw], bio->bi_iter.bi_size, |
| 1984 | tg_bps_limit(tg, rw), |
| 1985 | tg->io_disp[rw], tg_iops_limit(tg, rw), |
Tejun Heo | fda6f27 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 1986 | sq->nr_queued[READ], sq->nr_queued[WRITE]); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1987 | |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 1988 | tg->last_low_overflow_time[rw] = jiffies; |
| 1989 | |
Tejun Heo | 6bc9c2b | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1990 | tg->td->nr_queued[rw]++; |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 1991 | throtl_add_bio_tg(bio, qn, tg); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 1992 | throttled = true; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 1993 | |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 1994 | /* |
| 1995 | * Update @tg's dispatch time and force schedule dispatch if @tg |
| 1996 | * was empty before @bio. The forced scheduling isn't likely to |
| 1997 | * cause undue delay as @bio is likely to be dispatched directly if |
| 1998 | * its @tg's disptime is not in the future. |
| 1999 | */ |
Tejun Heo | 0e9f416 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 2000 | if (tg->flags & THROTL_TG_WAS_EMPTY) { |
Tejun Heo | 77216b0 | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 2001 | tg_update_disptime(tg); |
Tejun Heo | 7f52f98 | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2002 | throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2003 | } |
| 2004 | |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 2005 | out_unlock: |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2006 | spin_unlock_irq(q->queue_lock); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 2007 | out: |
Tejun Heo | 2a0f61e | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 2008 | /* |
| 2009 | * As multiple blk-throtls may stack in the same issue path, we |
| 2010 | * don't want bios to leave with the flag set. Clear the flag if |
| 2011 | * being issued. |
| 2012 | */ |
| 2013 | if (!throttled) |
Christoph Hellwig | 8d2bbd4 | 2016-10-20 15:12:12 +0200 | [diff] [blame] | 2014 | bio_clear_flag(bio, BIO_THROTTLED); |
Tejun Heo | bc16a4f | 2011-10-19 14:33:01 +0200 | [diff] [blame] | 2015 | return throttled; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2016 | } |
| 2017 | |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 2018 | #ifdef CONFIG_BLK_DEV_THROTTLING_LOW |
| 2019 | void blk_throtl_bio_endio(struct bio *bio) |
| 2020 | { |
| 2021 | struct throtl_grp *tg; |
| 2022 | |
| 2023 | tg = bio->bi_cg_private; |
| 2024 | if (!tg) |
| 2025 | return; |
| 2026 | bio->bi_cg_private = NULL; |
| 2027 | |
| 2028 | tg->last_finish_time = ktime_get_ns() >> 10; |
| 2029 | } |
| 2030 | #endif |
| 2031 | |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2032 | /* |
| 2033 | * Dispatch all bios from all children tg's queued on @parent_sq. On |
| 2034 | * return, @parent_sq is guaranteed to not have any active children tg's |
| 2035 | * and all bios from previously active tg's are on @parent_sq->bio_lists[]. |
| 2036 | */ |
| 2037 | static void tg_drain_bios(struct throtl_service_queue *parent_sq) |
| 2038 | { |
| 2039 | struct throtl_grp *tg; |
| 2040 | |
| 2041 | while ((tg = throtl_rb_first(parent_sq))) { |
| 2042 | struct throtl_service_queue *sq = &tg->service_queue; |
| 2043 | struct bio *bio; |
| 2044 | |
| 2045 | throtl_dequeue_tg(tg); |
| 2046 | |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 2047 | while ((bio = throtl_peek_queued(&sq->queued[READ]))) |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2048 | tg_dispatch_one_bio(tg, bio_data_dir(bio)); |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 2049 | while ((bio = throtl_peek_queued(&sq->queued[WRITE]))) |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2050 | tg_dispatch_one_bio(tg, bio_data_dir(bio)); |
| 2051 | } |
| 2052 | } |
| 2053 | |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 2054 | /** |
| 2055 | * blk_throtl_drain - drain throttled bios |
| 2056 | * @q: request_queue to drain throttled bios for |
| 2057 | * |
| 2058 | * Dispatch all currently throttled bios on @q through ->make_request_fn(). |
| 2059 | */ |
| 2060 | void blk_throtl_drain(struct request_queue *q) |
| 2061 | __releases(q->queue_lock) __acquires(q->queue_lock) |
| 2062 | { |
| 2063 | struct throtl_data *td = q->td; |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2064 | struct blkcg_gq *blkg; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2065 | struct cgroup_subsys_state *pos_css; |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 2066 | struct bio *bio; |
Tejun Heo | 651930b | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 2067 | int rw; |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 2068 | |
Andi Kleen | 8bcb6c7 | 2012-03-30 12:33:28 +0200 | [diff] [blame] | 2069 | queue_lockdep_assert_held(q); |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2070 | rcu_read_lock(); |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 2071 | |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2072 | /* |
| 2073 | * Drain each tg while doing post-order walk on the blkg tree, so |
| 2074 | * that all bios are propagated to td->service_queue. It'd be |
| 2075 | * better to walk service_queue tree directly but blkg walk is |
| 2076 | * easier. |
| 2077 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2078 | blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2079 | tg_drain_bios(&blkg_to_tg(blkg)->service_queue); |
Tejun Heo | 73f0d49 | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 2080 | |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2081 | /* finally, transfer bios from top-level tg's into the td */ |
| 2082 | tg_drain_bios(&td->service_queue); |
| 2083 | |
| 2084 | rcu_read_unlock(); |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 2085 | spin_unlock_irq(q->queue_lock); |
| 2086 | |
Tejun Heo | 2a12f0d | 2013-05-14 13:52:37 -0700 | [diff] [blame] | 2087 | /* all bios now should be in td->service_queue, issue them */ |
Tejun Heo | 651930b | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 2088 | for (rw = READ; rw <= WRITE; rw++) |
Tejun Heo | c5cc207 | 2013-05-14 13:52:38 -0700 | [diff] [blame] | 2089 | while ((bio = throtl_pop_queued(&td->service_queue.queued[rw], |
| 2090 | NULL))) |
Tejun Heo | 651930b | 2013-05-14 13:52:35 -0700 | [diff] [blame] | 2091 | generic_make_request(bio); |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 2092 | |
| 2093 | spin_lock_irq(q->queue_lock); |
| 2094 | } |
| 2095 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2096 | int blk_throtl_init(struct request_queue *q) |
| 2097 | { |
| 2098 | struct throtl_data *td; |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 2099 | int ret; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2100 | |
| 2101 | td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node); |
| 2102 | if (!td) |
| 2103 | return -ENOMEM; |
| 2104 | |
Tejun Heo | 69df0ab | 2013-05-14 13:52:36 -0700 | [diff] [blame] | 2105 | INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn); |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 2106 | throtl_service_queue_init(&td->service_queue); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2107 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 2108 | q->td = td; |
Vivek Goyal | 29b1258 | 2011-05-19 15:38:24 -0400 | [diff] [blame] | 2109 | td->queue = q; |
Vivek Goyal | 02977e4 | 2010-10-01 14:49:48 +0200 | [diff] [blame] | 2110 | |
Shaohua Li | 9f626e3 | 2017-03-27 10:51:30 -0700 | [diff] [blame] | 2111 | td->limit_valid[LIMIT_MAX] = true; |
Shaohua Li | cd5ab1b | 2017-03-27 10:51:32 -0700 | [diff] [blame] | 2112 | td->limit_index = LIMIT_MAX; |
Shaohua Li | 3f0abd8 | 2017-03-27 10:51:35 -0700 | [diff] [blame] | 2113 | td->low_upgrade_time = jiffies; |
| 2114 | td->low_downgrade_time = jiffies; |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 2115 | |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 2116 | /* activate policy */ |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 2117 | ret = blkcg_activate_policy(q, &blkcg_policy_throtl); |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 2118 | if (ret) |
Vivek Goyal | 29b1258 | 2011-05-19 15:38:24 -0400 | [diff] [blame] | 2119 | kfree(td); |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 2120 | return ret; |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | void blk_throtl_exit(struct request_queue *q) |
| 2124 | { |
Tejun Heo | c875f4d | 2012-03-05 13:15:22 -0800 | [diff] [blame] | 2125 | BUG_ON(!q->td); |
Vivek Goyal | da52777 | 2011-03-02 19:05:33 -0500 | [diff] [blame] | 2126 | throtl_shutdown_wq(q); |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 2127 | blkcg_deactivate_policy(q, &blkcg_policy_throtl); |
Tejun Heo | c9a929d | 2011-10-19 14:42:16 +0200 | [diff] [blame] | 2128 | kfree(q->td); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2129 | } |
| 2130 | |
Shaohua Li | d61fcfa | 2017-03-27 10:51:38 -0700 | [diff] [blame] | 2131 | void blk_throtl_register_queue(struct request_queue *q) |
| 2132 | { |
| 2133 | struct throtl_data *td; |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 2134 | struct cgroup_subsys_state *pos_css; |
| 2135 | struct blkcg_gq *blkg; |
Shaohua Li | d61fcfa | 2017-03-27 10:51:38 -0700 | [diff] [blame] | 2136 | |
| 2137 | td = q->td; |
| 2138 | BUG_ON(!td); |
| 2139 | |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame^] | 2140 | if (blk_queue_nonrot(q)) { |
Shaohua Li | d61fcfa | 2017-03-27 10:51:38 -0700 | [diff] [blame] | 2141 | td->throtl_slice = DFL_THROTL_SLICE_SSD; |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame^] | 2142 | td->dft_idletime_threshold = DFL_IDLE_THRESHOLD_SSD; |
| 2143 | } else { |
Shaohua Li | d61fcfa | 2017-03-27 10:51:38 -0700 | [diff] [blame] | 2144 | td->throtl_slice = DFL_THROTL_SLICE_HD; |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame^] | 2145 | td->dft_idletime_threshold = DFL_IDLE_THRESHOLD_HD; |
| 2146 | } |
Shaohua Li | d61fcfa | 2017-03-27 10:51:38 -0700 | [diff] [blame] | 2147 | #ifndef CONFIG_BLK_DEV_THROTTLING_LOW |
| 2148 | /* if no low limit, use previous default */ |
| 2149 | td->throtl_slice = DFL_THROTL_SLICE_HD; |
| 2150 | #endif |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 2151 | |
| 2152 | /* |
| 2153 | * some tg are created before queue is fully initialized, eg, nonrot |
| 2154 | * isn't initialized yet |
| 2155 | */ |
| 2156 | rcu_read_lock(); |
| 2157 | blkg_for_each_descendant_post(blkg, pos_css, q->root_blkg) { |
| 2158 | struct throtl_grp *tg = blkg_to_tg(blkg); |
| 2159 | |
Shaohua Li | ada75b6 | 2017-03-27 10:51:42 -0700 | [diff] [blame^] | 2160 | tg->idletime_threshold = td->dft_idletime_threshold; |
Shaohua Li | 9e234ee | 2017-03-27 10:51:41 -0700 | [diff] [blame] | 2161 | } |
| 2162 | rcu_read_unlock(); |
Shaohua Li | d61fcfa | 2017-03-27 10:51:38 -0700 | [diff] [blame] | 2163 | } |
| 2164 | |
Shaohua Li | 297e3d8 | 2017-03-27 10:51:37 -0700 | [diff] [blame] | 2165 | #ifdef CONFIG_BLK_DEV_THROTTLING_LOW |
| 2166 | ssize_t blk_throtl_sample_time_show(struct request_queue *q, char *page) |
| 2167 | { |
| 2168 | if (!q->td) |
| 2169 | return -EINVAL; |
| 2170 | return sprintf(page, "%u\n", jiffies_to_msecs(q->td->throtl_slice)); |
| 2171 | } |
| 2172 | |
| 2173 | ssize_t blk_throtl_sample_time_store(struct request_queue *q, |
| 2174 | const char *page, size_t count) |
| 2175 | { |
| 2176 | unsigned long v; |
| 2177 | unsigned long t; |
| 2178 | |
| 2179 | if (!q->td) |
| 2180 | return -EINVAL; |
| 2181 | if (kstrtoul(page, 10, &v)) |
| 2182 | return -EINVAL; |
| 2183 | t = msecs_to_jiffies(v); |
| 2184 | if (t == 0 || t > MAX_THROTL_SLICE) |
| 2185 | return -EINVAL; |
| 2186 | q->td->throtl_slice = t; |
| 2187 | return count; |
| 2188 | } |
| 2189 | #endif |
| 2190 | |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2191 | static int __init throtl_init(void) |
| 2192 | { |
Vivek Goyal | 450adcb | 2011-03-01 13:40:54 -0500 | [diff] [blame] | 2193 | kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0); |
| 2194 | if (!kthrotld_workqueue) |
| 2195 | panic("Failed to create kthrotld\n"); |
| 2196 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 2197 | return blkcg_policy_register(&blkcg_policy_throtl); |
Vivek Goyal | e43473b | 2010-09-15 17:06:35 -0400 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | module_init(throtl_init); |