]>
Commit | Line | Data |
---|---|---|
3dcf60bc CH |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | ||
a7905043 JB |
3 | #include "blk-rq-qos.h" |
4 | ||
a7905043 JB |
5 | /* |
6 | * Increment 'v', if 'v' is below 'below'. Returns true if we succeeded, | |
7 | * false if 'v' + 1 would be bigger than 'below'. | |
8 | */ | |
22f17952 | 9 | static bool atomic_inc_below(atomic_t *v, unsigned int below) |
a7905043 | 10 | { |
22f17952 | 11 | unsigned int cur = atomic_read(v); |
a7905043 JB |
12 | |
13 | for (;;) { | |
22f17952 | 14 | unsigned int old; |
a7905043 JB |
15 | |
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 | ||
22f17952 | 27 | bool rq_wait_inc_below(struct rq_wait *rq_wait, unsigned int limit) |
a7905043 JB |
28 | { |
29 | return atomic_inc_below(&rq_wait->inflight, limit); | |
30 | } | |
31 | ||
e5045454 | 32 | void __rq_qos_cleanup(struct rq_qos *rqos, struct bio *bio) |
a7905043 | 33 | { |
e5045454 | 34 | do { |
a7905043 | 35 | if (rqos->ops->cleanup) |
c1c80384 | 36 | rqos->ops->cleanup(rqos, bio); |
e5045454 JA |
37 | rqos = rqos->next; |
38 | } while (rqos); | |
a7905043 JB |
39 | } |
40 | ||
e5045454 | 41 | void __rq_qos_done(struct rq_qos *rqos, struct request *rq) |
a7905043 | 42 | { |
e5045454 | 43 | do { |
a7905043 JB |
44 | if (rqos->ops->done) |
45 | rqos->ops->done(rqos, rq); | |
e5045454 JA |
46 | rqos = rqos->next; |
47 | } while (rqos); | |
a7905043 JB |
48 | } |
49 | ||
e5045454 | 50 | void __rq_qos_issue(struct rq_qos *rqos, struct request *rq) |
a7905043 | 51 | { |
e5045454 | 52 | do { |
a7905043 JB |
53 | if (rqos->ops->issue) |
54 | rqos->ops->issue(rqos, rq); | |
e5045454 JA |
55 | rqos = rqos->next; |
56 | } while (rqos); | |
a7905043 JB |
57 | } |
58 | ||
e5045454 | 59 | void __rq_qos_requeue(struct rq_qos *rqos, struct request *rq) |
a7905043 | 60 | { |
e5045454 | 61 | do { |
a7905043 JB |
62 | if (rqos->ops->requeue) |
63 | rqos->ops->requeue(rqos, rq); | |
e5045454 JA |
64 | rqos = rqos->next; |
65 | } while (rqos); | |
a7905043 JB |
66 | } |
67 | ||
e5045454 | 68 | void __rq_qos_throttle(struct rq_qos *rqos, struct bio *bio) |
a7905043 | 69 | { |
e5045454 | 70 | do { |
a7905043 | 71 | if (rqos->ops->throttle) |
d5337560 | 72 | rqos->ops->throttle(rqos, bio); |
e5045454 JA |
73 | rqos = rqos->next; |
74 | } while (rqos); | |
c1c80384 JB |
75 | } |
76 | ||
e5045454 | 77 | void __rq_qos_track(struct rq_qos *rqos, struct request *rq, struct bio *bio) |
c1c80384 | 78 | { |
e5045454 | 79 | do { |
c1c80384 JB |
80 | if (rqos->ops->track) |
81 | rqos->ops->track(rqos, rq, bio); | |
d3e65fff TH |
82 | rqos = rqos->next; |
83 | } while (rqos); | |
84 | } | |
85 | ||
86 | void __rq_qos_merge(struct rq_qos *rqos, struct request *rq, struct bio *bio) | |
87 | { | |
88 | do { | |
89 | if (rqos->ops->merge) | |
90 | rqos->ops->merge(rqos, rq, bio); | |
e5045454 JA |
91 | rqos = rqos->next; |
92 | } while (rqos); | |
a7905043 JB |
93 | } |
94 | ||
e5045454 | 95 | void __rq_qos_done_bio(struct rq_qos *rqos, struct bio *bio) |
67b42d0b | 96 | { |
e5045454 | 97 | do { |
67b42d0b JB |
98 | if (rqos->ops->done_bio) |
99 | rqos->ops->done_bio(rqos, bio); | |
e5045454 JA |
100 | rqos = rqos->next; |
101 | } while (rqos); | |
67b42d0b JB |
102 | } |
103 | ||
a7905043 JB |
104 | /* |
105 | * Return true, if we can't increase the depth further by scaling | |
106 | */ | |
107 | bool rq_depth_calc_max_depth(struct rq_depth *rqd) | |
108 | { | |
109 | unsigned int depth; | |
110 | bool ret = false; | |
111 | ||
112 | /* | |
113 | * For QD=1 devices, this is a special case. It's important for those | |
114 | * to have one request ready when one completes, so force a depth of | |
115 | * 2 for those devices. On the backend, it'll be a depth of 1 anyway, | |
116 | * since the device can't have more than that in flight. If we're | |
117 | * scaling down, then keep a setting of 1/1/1. | |
118 | */ | |
119 | if (rqd->queue_depth == 1) { | |
120 | if (rqd->scale_step > 0) | |
121 | rqd->max_depth = 1; | |
122 | else { | |
123 | rqd->max_depth = 2; | |
124 | ret = true; | |
125 | } | |
126 | } else { | |
127 | /* | |
128 | * scale_step == 0 is our default state. If we have suffered | |
129 | * latency spikes, step will be > 0, and we shrink the | |
130 | * allowed write depths. If step is < 0, we're only doing | |
131 | * writes, and we allow a temporarily higher depth to | |
132 | * increase performance. | |
133 | */ | |
134 | depth = min_t(unsigned int, rqd->default_depth, | |
135 | rqd->queue_depth); | |
136 | if (rqd->scale_step > 0) | |
137 | depth = 1 + ((depth - 1) >> min(31, rqd->scale_step)); | |
138 | else if (rqd->scale_step < 0) { | |
139 | unsigned int maxd = 3 * rqd->queue_depth / 4; | |
140 | ||
141 | depth = 1 + ((depth - 1) << -rqd->scale_step); | |
142 | if (depth > maxd) { | |
143 | depth = maxd; | |
144 | ret = true; | |
145 | } | |
146 | } | |
147 | ||
148 | rqd->max_depth = depth; | |
149 | } | |
150 | ||
151 | return ret; | |
152 | } | |
153 | ||
154 | void rq_depth_scale_up(struct rq_depth *rqd) | |
155 | { | |
156 | /* | |
157 | * Hit max in previous round, stop here | |
158 | */ | |
159 | if (rqd->scaled_max) | |
160 | return; | |
161 | ||
162 | rqd->scale_step--; | |
163 | ||
164 | rqd->scaled_max = rq_depth_calc_max_depth(rqd); | |
165 | } | |
166 | ||
167 | /* | |
168 | * Scale rwb down. If 'hard_throttle' is set, do it quicker, since we | |
169 | * had a latency violation. | |
170 | */ | |
171 | void rq_depth_scale_down(struct rq_depth *rqd, bool hard_throttle) | |
172 | { | |
173 | /* | |
174 | * Stop scaling down when we've hit the limit. This also prevents | |
175 | * ->scale_step from going to crazy values, if the device can't | |
176 | * keep up. | |
177 | */ | |
178 | if (rqd->max_depth == 1) | |
179 | return; | |
180 | ||
181 | if (rqd->scale_step < 0 && hard_throttle) | |
182 | rqd->scale_step = 0; | |
183 | else | |
184 | rqd->scale_step++; | |
185 | ||
186 | rqd->scaled_max = false; | |
187 | rq_depth_calc_max_depth(rqd); | |
188 | } | |
189 | ||
84f60324 JB |
190 | struct rq_qos_wait_data { |
191 | struct wait_queue_entry wq; | |
192 | struct task_struct *task; | |
193 | struct rq_wait *rqw; | |
194 | acquire_inflight_cb_t *cb; | |
195 | void *private_data; | |
196 | bool got_token; | |
197 | }; | |
198 | ||
199 | static int rq_qos_wake_function(struct wait_queue_entry *curr, | |
200 | unsigned int mode, int wake_flags, void *key) | |
201 | { | |
202 | struct rq_qos_wait_data *data = container_of(curr, | |
203 | struct rq_qos_wait_data, | |
204 | wq); | |
205 | ||
206 | /* | |
207 | * If we fail to get a budget, return -1 to interrupt the wake up loop | |
208 | * in __wake_up_common. | |
209 | */ | |
210 | if (!data->cb(data->rqw, data->private_data)) | |
211 | return -1; | |
212 | ||
213 | data->got_token = true; | |
ac38297f | 214 | smp_wmb(); |
84f60324 JB |
215 | list_del_init(&curr->entry); |
216 | wake_up_process(data->task); | |
217 | return 1; | |
218 | } | |
219 | ||
220 | /** | |
221 | * rq_qos_wait - throttle on a rqw if we need to | |
83826a50 BVA |
222 | * @rqw: rqw to throttle on |
223 | * @private_data: caller provided specific data | |
224 | * @acquire_inflight_cb: inc the rqw->inflight counter if we can | |
225 | * @cleanup_cb: the callback to cleanup in case we race with a waker | |
84f60324 JB |
226 | * |
227 | * This provides a uniform place for the rq_qos users to do their throttling. | |
228 | * Since you can end up with a lot of things sleeping at once, this manages the | |
229 | * waking up based on the resources available. The acquire_inflight_cb should | |
230 | * inc the rqw->inflight if we have the ability to do so, or return false if not | |
231 | * and then we will sleep until the room becomes available. | |
232 | * | |
233 | * cleanup_cb is in case that we race with a waker and need to cleanup the | |
234 | * inflight count accordingly. | |
235 | */ | |
236 | void rq_qos_wait(struct rq_wait *rqw, void *private_data, | |
237 | acquire_inflight_cb_t *acquire_inflight_cb, | |
238 | cleanup_cb_t *cleanup_cb) | |
239 | { | |
240 | struct rq_qos_wait_data data = { | |
241 | .wq = { | |
242 | .func = rq_qos_wake_function, | |
243 | .entry = LIST_HEAD_INIT(data.wq.entry), | |
244 | }, | |
245 | .task = current, | |
246 | .rqw = rqw, | |
247 | .cb = acquire_inflight_cb, | |
248 | .private_data = private_data, | |
249 | }; | |
250 | bool has_sleeper; | |
251 | ||
252 | has_sleeper = wq_has_sleeper(&rqw->wait); | |
253 | if (!has_sleeper && acquire_inflight_cb(rqw, private_data)) | |
254 | return; | |
255 | ||
256 | prepare_to_wait_exclusive(&rqw->wait, &data.wq, TASK_UNINTERRUPTIBLE); | |
545fbd07 | 257 | has_sleeper = !wq_has_single_sleeper(&rqw->wait); |
84f60324 | 258 | do { |
ac38297f | 259 | /* The memory barrier in set_task_state saves us here. */ |
84f60324 JB |
260 | if (data.got_token) |
261 | break; | |
262 | if (!has_sleeper && acquire_inflight_cb(rqw, private_data)) { | |
263 | finish_wait(&rqw->wait, &data.wq); | |
264 | ||
265 | /* | |
266 | * We raced with wbt_wake_function() getting a token, | |
267 | * which means we now have two. Put our local token | |
268 | * and wake anyone else potentially waiting for one. | |
269 | */ | |
ac38297f | 270 | smp_rmb(); |
84f60324 JB |
271 | if (data.got_token) |
272 | cleanup_cb(rqw, private_data); | |
273 | break; | |
274 | } | |
275 | io_schedule(); | |
64e7ea87 | 276 | has_sleeper = true; |
d14a9b38 | 277 | set_current_state(TASK_UNINTERRUPTIBLE); |
84f60324 JB |
278 | } while (1); |
279 | finish_wait(&rqw->wait, &data.wq); | |
280 | } | |
281 | ||
a7905043 JB |
282 | void rq_qos_exit(struct request_queue *q) |
283 | { | |
cc56694f ML |
284 | blk_mq_debugfs_unregister_queue_rqos(q); |
285 | ||
a7905043 JB |
286 | while (q->rq_qos) { |
287 | struct rq_qos *rqos = q->rq_qos; | |
288 | q->rq_qos = rqos->next; | |
289 | rqos->ops->exit(rqos); | |
290 | } | |
291 | } |