blob: 659ccb8b693fa63cedfc53536589f5afcf3b1afc [file] [log] [blame]
Christoph Hellwig3dcf60b2019-04-30 14:42:43 -04001// SPDX-License-Identifier: GPL-2.0
2
Josef Bacika7905042018-07-03 09:32:35 -06003#include "blk-rq-qos.h"
4
Josef Bacika7905042018-07-03 09:32:35 -06005/*
6 * Increment 'v', if 'v' is below 'below'. Returns true if we succeeded,
7 * false if 'v' + 1 would be bigger than 'below'.
8 */
Josef Bacik22f17952018-07-19 21:42:13 -04009static bool atomic_inc_below(atomic_t *v, unsigned int below)
Josef Bacika7905042018-07-03 09:32:35 -060010{
Josef Bacik22f17952018-07-19 21:42:13 -040011 unsigned int cur = atomic_read(v);
Josef Bacika7905042018-07-03 09:32:35 -060012
13 for (;;) {
Josef Bacik22f17952018-07-19 21:42:13 -040014 unsigned int old;
Josef Bacika7905042018-07-03 09:32:35 -060015
16 if (cur >= below)
17 return false;
18 old = atomic_cmpxchg(v, cur, cur + 1);
19 if (old == cur)
20 break;
21 cur = old;
22 }
23
24 return true;
25}
26
Josef Bacik22f17952018-07-19 21:42:13 -040027bool rq_wait_inc_below(struct rq_wait *rq_wait, unsigned int limit)
Josef Bacika7905042018-07-03 09:32:35 -060028{
29 return atomic_inc_below(&rq_wait->inflight, limit);
30}
31
Jens Axboee5045452018-11-15 12:25:10 -070032void __rq_qos_cleanup(struct rq_qos *rqos, struct bio *bio)
Josef Bacika7905042018-07-03 09:32:35 -060033{
Jens Axboee5045452018-11-15 12:25:10 -070034 do {
Josef Bacika7905042018-07-03 09:32:35 -060035 if (rqos->ops->cleanup)
Josef Bacikc1c80382018-07-03 11:14:59 -040036 rqos->ops->cleanup(rqos, bio);
Jens Axboee5045452018-11-15 12:25:10 -070037 rqos = rqos->next;
38 } while (rqos);
Josef Bacika7905042018-07-03 09:32:35 -060039}
40
Jens Axboee5045452018-11-15 12:25:10 -070041void __rq_qos_done(struct rq_qos *rqos, struct request *rq)
Josef Bacika7905042018-07-03 09:32:35 -060042{
Jens Axboee5045452018-11-15 12:25:10 -070043 do {
Josef Bacika7905042018-07-03 09:32:35 -060044 if (rqos->ops->done)
45 rqos->ops->done(rqos, rq);
Jens Axboee5045452018-11-15 12:25:10 -070046 rqos = rqos->next;
47 } while (rqos);
Josef Bacika7905042018-07-03 09:32:35 -060048}
49
Jens Axboee5045452018-11-15 12:25:10 -070050void __rq_qos_issue(struct rq_qos *rqos, struct request *rq)
Josef Bacika7905042018-07-03 09:32:35 -060051{
Jens Axboee5045452018-11-15 12:25:10 -070052 do {
Josef Bacika7905042018-07-03 09:32:35 -060053 if (rqos->ops->issue)
54 rqos->ops->issue(rqos, rq);
Jens Axboee5045452018-11-15 12:25:10 -070055 rqos = rqos->next;
56 } while (rqos);
Josef Bacika7905042018-07-03 09:32:35 -060057}
58
Jens Axboee5045452018-11-15 12:25:10 -070059void __rq_qos_requeue(struct rq_qos *rqos, struct request *rq)
Josef Bacika7905042018-07-03 09:32:35 -060060{
Jens Axboee5045452018-11-15 12:25:10 -070061 do {
Josef Bacika7905042018-07-03 09:32:35 -060062 if (rqos->ops->requeue)
63 rqos->ops->requeue(rqos, rq);
Jens Axboee5045452018-11-15 12:25:10 -070064 rqos = rqos->next;
65 } while (rqos);
Josef Bacika7905042018-07-03 09:32:35 -060066}
67
Jens Axboee5045452018-11-15 12:25:10 -070068void __rq_qos_throttle(struct rq_qos *rqos, struct bio *bio)
Josef Bacika7905042018-07-03 09:32:35 -060069{
Jens Axboee5045452018-11-15 12:25:10 -070070 do {
Josef Bacika7905042018-07-03 09:32:35 -060071 if (rqos->ops->throttle)
Christoph Hellwigd5337562018-11-14 17:02:09 +010072 rqos->ops->throttle(rqos, bio);
Jens Axboee5045452018-11-15 12:25:10 -070073 rqos = rqos->next;
74 } while (rqos);
Josef Bacikc1c80382018-07-03 11:14:59 -040075}
76
Jens Axboee5045452018-11-15 12:25:10 -070077void __rq_qos_track(struct rq_qos *rqos, struct request *rq, struct bio *bio)
Josef Bacikc1c80382018-07-03 11:14:59 -040078{
Jens Axboee5045452018-11-15 12:25:10 -070079 do {
Josef Bacikc1c80382018-07-03 11:14:59 -040080 if (rqos->ops->track)
81 rqos->ops->track(rqos, rq, bio);
Jens Axboee5045452018-11-15 12:25:10 -070082 rqos = rqos->next;
83 } while (rqos);
Josef Bacika7905042018-07-03 09:32:35 -060084}
85
Jens Axboee5045452018-11-15 12:25:10 -070086void __rq_qos_done_bio(struct rq_qos *rqos, struct bio *bio)
Josef Bacik67b42d02018-07-03 11:15:00 -040087{
Jens Axboee5045452018-11-15 12:25:10 -070088 do {
Josef Bacik67b42d02018-07-03 11:15:00 -040089 if (rqos->ops->done_bio)
90 rqos->ops->done_bio(rqos, bio);
Jens Axboee5045452018-11-15 12:25:10 -070091 rqos = rqos->next;
92 } while (rqos);
Josef Bacik67b42d02018-07-03 11:15:00 -040093}
94
Josef Bacika7905042018-07-03 09:32:35 -060095/*
96 * Return true, if we can't increase the depth further by scaling
97 */
98bool rq_depth_calc_max_depth(struct rq_depth *rqd)
99{
100 unsigned int depth;
101 bool ret = false;
102
103 /*
104 * For QD=1 devices, this is a special case. It's important for those
105 * to have one request ready when one completes, so force a depth of
106 * 2 for those devices. On the backend, it'll be a depth of 1 anyway,
107 * since the device can't have more than that in flight. If we're
108 * scaling down, then keep a setting of 1/1/1.
109 */
110 if (rqd->queue_depth == 1) {
111 if (rqd->scale_step > 0)
112 rqd->max_depth = 1;
113 else {
114 rqd->max_depth = 2;
115 ret = true;
116 }
117 } else {
118 /*
119 * scale_step == 0 is our default state. If we have suffered
120 * latency spikes, step will be > 0, and we shrink the
121 * allowed write depths. If step is < 0, we're only doing
122 * writes, and we allow a temporarily higher depth to
123 * increase performance.
124 */
125 depth = min_t(unsigned int, rqd->default_depth,
126 rqd->queue_depth);
127 if (rqd->scale_step > 0)
128 depth = 1 + ((depth - 1) >> min(31, rqd->scale_step));
129 else if (rqd->scale_step < 0) {
130 unsigned int maxd = 3 * rqd->queue_depth / 4;
131
132 depth = 1 + ((depth - 1) << -rqd->scale_step);
133 if (depth > maxd) {
134 depth = maxd;
135 ret = true;
136 }
137 }
138
139 rqd->max_depth = depth;
140 }
141
142 return ret;
143}
144
145void rq_depth_scale_up(struct rq_depth *rqd)
146{
147 /*
148 * Hit max in previous round, stop here
149 */
150 if (rqd->scaled_max)
151 return;
152
153 rqd->scale_step--;
154
155 rqd->scaled_max = rq_depth_calc_max_depth(rqd);
156}
157
158/*
159 * Scale rwb down. If 'hard_throttle' is set, do it quicker, since we
160 * had a latency violation.
161 */
162void rq_depth_scale_down(struct rq_depth *rqd, bool hard_throttle)
163{
164 /*
165 * Stop scaling down when we've hit the limit. This also prevents
166 * ->scale_step from going to crazy values, if the device can't
167 * keep up.
168 */
169 if (rqd->max_depth == 1)
170 return;
171
172 if (rqd->scale_step < 0 && hard_throttle)
173 rqd->scale_step = 0;
174 else
175 rqd->scale_step++;
176
177 rqd->scaled_max = false;
178 rq_depth_calc_max_depth(rqd);
179}
180
Josef Bacik84f60322018-12-04 12:59:02 -0500181struct rq_qos_wait_data {
182 struct wait_queue_entry wq;
183 struct task_struct *task;
184 struct rq_wait *rqw;
185 acquire_inflight_cb_t *cb;
186 void *private_data;
187 bool got_token;
188};
189
190static int rq_qos_wake_function(struct wait_queue_entry *curr,
191 unsigned int mode, int wake_flags, void *key)
192{
193 struct rq_qos_wait_data *data = container_of(curr,
194 struct rq_qos_wait_data,
195 wq);
196
197 /*
198 * If we fail to get a budget, return -1 to interrupt the wake up loop
199 * in __wake_up_common.
200 */
201 if (!data->cb(data->rqw, data->private_data))
202 return -1;
203
204 data->got_token = true;
205 list_del_init(&curr->entry);
206 wake_up_process(data->task);
207 return 1;
208}
209
210/**
211 * rq_qos_wait - throttle on a rqw if we need to
Bart Van Assche83826a52019-05-30 17:00:50 -0700212 * @rqw: rqw to throttle on
213 * @private_data: caller provided specific data
214 * @acquire_inflight_cb: inc the rqw->inflight counter if we can
215 * @cleanup_cb: the callback to cleanup in case we race with a waker
Josef Bacik84f60322018-12-04 12:59:02 -0500216 *
217 * This provides a uniform place for the rq_qos users to do their throttling.
218 * Since you can end up with a lot of things sleeping at once, this manages the
219 * waking up based on the resources available. The acquire_inflight_cb should
220 * inc the rqw->inflight if we have the ability to do so, or return false if not
221 * and then we will sleep until the room becomes available.
222 *
223 * cleanup_cb is in case that we race with a waker and need to cleanup the
224 * inflight count accordingly.
225 */
226void rq_qos_wait(struct rq_wait *rqw, void *private_data,
227 acquire_inflight_cb_t *acquire_inflight_cb,
228 cleanup_cb_t *cleanup_cb)
229{
230 struct rq_qos_wait_data data = {
231 .wq = {
232 .func = rq_qos_wake_function,
233 .entry = LIST_HEAD_INIT(data.wq.entry),
234 },
235 .task = current,
236 .rqw = rqw,
237 .cb = acquire_inflight_cb,
238 .private_data = private_data,
239 };
240 bool has_sleeper;
241
242 has_sleeper = wq_has_sleeper(&rqw->wait);
243 if (!has_sleeper && acquire_inflight_cb(rqw, private_data))
244 return;
245
246 prepare_to_wait_exclusive(&rqw->wait, &data.wq, TASK_UNINTERRUPTIBLE);
247 do {
248 if (data.got_token)
249 break;
250 if (!has_sleeper && acquire_inflight_cb(rqw, private_data)) {
251 finish_wait(&rqw->wait, &data.wq);
252
253 /*
254 * We raced with wbt_wake_function() getting a token,
255 * which means we now have two. Put our local token
256 * and wake anyone else potentially waiting for one.
257 */
258 if (data.got_token)
259 cleanup_cb(rqw, private_data);
260 break;
261 }
262 io_schedule();
263 has_sleeper = false;
264 } while (1);
265 finish_wait(&rqw->wait, &data.wq);
266}
267
Josef Bacika7905042018-07-03 09:32:35 -0600268void rq_qos_exit(struct request_queue *q)
269{
Ming Leicc566942018-12-17 09:46:00 +0800270 blk_mq_debugfs_unregister_queue_rqos(q);
271
Josef Bacika7905042018-07-03 09:32:35 -0600272 while (q->rq_qos) {
273 struct rq_qos *rqos = q->rq_qos;
274 q->rq_qos = rqos->next;
275 rqos->ops->exit(rqos);
276 }
277}