1 // SPDX-License-Identifier: GPL-2.0
3 #include "blk-rq-qos.h"
6 * Increment 'v', if 'v' is below 'below'. Returns true if we succeeded,
7 * false if 'v' + 1 would be bigger than 'below'.
9 static bool atomic_inc_below(atomic_t
*v
, unsigned int below
)
11 unsigned int cur
= atomic_read(v
);
18 old
= atomic_cmpxchg(v
, cur
, cur
+ 1);
27 bool rq_wait_inc_below(struct rq_wait
*rq_wait
, unsigned int limit
)
29 return atomic_inc_below(&rq_wait
->inflight
, limit
);
32 void __rq_qos_cleanup(struct rq_qos
*rqos
, struct bio
*bio
)
35 if (rqos
->ops
->cleanup
)
36 rqos
->ops
->cleanup(rqos
, bio
);
41 void __rq_qos_done(struct rq_qos
*rqos
, struct request
*rq
)
45 rqos
->ops
->done(rqos
, rq
);
50 void __rq_qos_issue(struct rq_qos
*rqos
, struct request
*rq
)
54 rqos
->ops
->issue(rqos
, rq
);
59 void __rq_qos_requeue(struct rq_qos
*rqos
, struct request
*rq
)
62 if (rqos
->ops
->requeue
)
63 rqos
->ops
->requeue(rqos
, rq
);
68 void __rq_qos_throttle(struct rq_qos
*rqos
, struct bio
*bio
)
71 if (rqos
->ops
->throttle
)
72 rqos
->ops
->throttle(rqos
, bio
);
77 void __rq_qos_track(struct rq_qos
*rqos
, struct request
*rq
, struct bio
*bio
)
81 rqos
->ops
->track(rqos
, rq
, bio
);
86 void __rq_qos_merge(struct rq_qos
*rqos
, struct request
*rq
, struct bio
*bio
)
90 rqos
->ops
->merge(rqos
, rq
, bio
);
95 void __rq_qos_done_bio(struct rq_qos
*rqos
, struct bio
*bio
)
98 if (rqos
->ops
->done_bio
)
99 rqos
->ops
->done_bio(rqos
, bio
);
104 void __rq_qos_queue_depth_changed(struct rq_qos
*rqos
)
107 if (rqos
->ops
->queue_depth_changed
)
108 rqos
->ops
->queue_depth_changed(rqos
);
114 * Return true, if we can't increase the depth further by scaling
116 bool rq_depth_calc_max_depth(struct rq_depth
*rqd
)
122 * For QD=1 devices, this is a special case. It's important for those
123 * to have one request ready when one completes, so force a depth of
124 * 2 for those devices. On the backend, it'll be a depth of 1 anyway,
125 * since the device can't have more than that in flight. If we're
126 * scaling down, then keep a setting of 1/1/1.
128 if (rqd
->queue_depth
== 1) {
129 if (rqd
->scale_step
> 0)
137 * scale_step == 0 is our default state. If we have suffered
138 * latency spikes, step will be > 0, and we shrink the
139 * allowed write depths. If step is < 0, we're only doing
140 * writes, and we allow a temporarily higher depth to
141 * increase performance.
143 depth
= min_t(unsigned int, rqd
->default_depth
,
145 if (rqd
->scale_step
> 0)
146 depth
= 1 + ((depth
- 1) >> min(31, rqd
->scale_step
));
147 else if (rqd
->scale_step
< 0) {
148 unsigned int maxd
= 3 * rqd
->queue_depth
/ 4;
150 depth
= 1 + ((depth
- 1) << -rqd
->scale_step
);
157 rqd
->max_depth
= depth
;
163 /* Returns true on success and false if scaling up wasn't possible */
164 bool rq_depth_scale_up(struct rq_depth
*rqd
)
167 * Hit max in previous round, stop here
174 rqd
->scaled_max
= rq_depth_calc_max_depth(rqd
);
179 * Scale rwb down. If 'hard_throttle' is set, do it quicker, since we
180 * had a latency violation. Returns true on success and returns false if
181 * scaling down wasn't possible.
183 bool rq_depth_scale_down(struct rq_depth
*rqd
, bool hard_throttle
)
186 * Stop scaling down when we've hit the limit. This also prevents
187 * ->scale_step from going to crazy values, if the device can't
190 if (rqd
->max_depth
== 1)
193 if (rqd
->scale_step
< 0 && hard_throttle
)
198 rqd
->scaled_max
= false;
199 rq_depth_calc_max_depth(rqd
);
203 struct rq_qos_wait_data
{
204 struct wait_queue_entry wq
;
205 struct task_struct
*task
;
207 acquire_inflight_cb_t
*cb
;
212 static int rq_qos_wake_function(struct wait_queue_entry
*curr
,
213 unsigned int mode
, int wake_flags
, void *key
)
215 struct rq_qos_wait_data
*data
= container_of(curr
,
216 struct rq_qos_wait_data
,
220 * If we fail to get a budget, return -1 to interrupt the wake up loop
221 * in __wake_up_common.
223 if (!data
->cb(data
->rqw
, data
->private_data
))
226 data
->got_token
= true;
228 list_del_init(&curr
->entry
);
229 wake_up_process(data
->task
);
234 * rq_qos_wait - throttle on a rqw if we need to
235 * @rqw: rqw to throttle on
236 * @private_data: caller provided specific data
237 * @acquire_inflight_cb: inc the rqw->inflight counter if we can
238 * @cleanup_cb: the callback to cleanup in case we race with a waker
240 * This provides a uniform place for the rq_qos users to do their throttling.
241 * Since you can end up with a lot of things sleeping at once, this manages the
242 * waking up based on the resources available. The acquire_inflight_cb should
243 * inc the rqw->inflight if we have the ability to do so, or return false if not
244 * and then we will sleep until the room becomes available.
246 * cleanup_cb is in case that we race with a waker and need to cleanup the
247 * inflight count accordingly.
249 void rq_qos_wait(struct rq_wait
*rqw
, void *private_data
,
250 acquire_inflight_cb_t
*acquire_inflight_cb
,
251 cleanup_cb_t
*cleanup_cb
)
253 struct rq_qos_wait_data data
= {
255 .func
= rq_qos_wake_function
,
256 .entry
= LIST_HEAD_INIT(data
.wq
.entry
),
260 .cb
= acquire_inflight_cb
,
261 .private_data
= private_data
,
265 has_sleeper
= wq_has_sleeper(&rqw
->wait
);
266 if (!has_sleeper
&& acquire_inflight_cb(rqw
, private_data
))
269 prepare_to_wait_exclusive(&rqw
->wait
, &data
.wq
, TASK_UNINTERRUPTIBLE
);
270 has_sleeper
= !wq_has_single_sleeper(&rqw
->wait
);
272 /* The memory barrier in set_task_state saves us here. */
275 if (!has_sleeper
&& acquire_inflight_cb(rqw
, private_data
)) {
276 finish_wait(&rqw
->wait
, &data
.wq
);
279 * We raced with wbt_wake_function() getting a token,
280 * which means we now have two. Put our local token
281 * and wake anyone else potentially waiting for one.
285 cleanup_cb(rqw
, private_data
);
290 set_current_state(TASK_UNINTERRUPTIBLE
);
292 finish_wait(&rqw
->wait
, &data
.wq
);
295 void rq_qos_exit(struct request_queue
*q
)
297 blk_mq_debugfs_unregister_queue_rqos(q
);
300 struct rq_qos
*rqos
= q
->rq_qos
;
301 q
->rq_qos
= rqos
->next
;
302 rqos
->ops
->exit(rqos
);