Commit | Line | Data |
---|---|---|
3dcf60bc | 1 | // SPDX-License-Identifier: GPL-2.0 |
75bb4625 JA |
2 | /* |
3 | * Block multiqueue core code | |
4 | * | |
5 | * Copyright (C) 2013-2014 Jens Axboe | |
6 | * Copyright (C) 2013-2014 Christoph Hellwig | |
7 | */ | |
320ae51f JA |
8 | #include <linux/kernel.h> |
9 | #include <linux/module.h> | |
10 | #include <linux/backing-dev.h> | |
11 | #include <linux/bio.h> | |
12 | #include <linux/blkdev.h> | |
f75782e4 | 13 | #include <linux/kmemleak.h> |
320ae51f JA |
14 | #include <linux/mm.h> |
15 | #include <linux/init.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/workqueue.h> | |
18 | #include <linux/smp.h> | |
19 | #include <linux/llist.h> | |
20 | #include <linux/list_sort.h> | |
21 | #include <linux/cpu.h> | |
22 | #include <linux/cache.h> | |
23 | #include <linux/sched/sysctl.h> | |
105ab3d8 | 24 | #include <linux/sched/topology.h> |
174cd4b1 | 25 | #include <linux/sched/signal.h> |
320ae51f | 26 | #include <linux/delay.h> |
aedcd72f | 27 | #include <linux/crash_dump.h> |
88c7b2b7 | 28 | #include <linux/prefetch.h> |
a892c8d5 | 29 | #include <linux/blk-crypto.h> |
320ae51f JA |
30 | |
31 | #include <trace/events/block.h> | |
32 | ||
33 | #include <linux/blk-mq.h> | |
54d4e6ab | 34 | #include <linux/t10-pi.h> |
320ae51f JA |
35 | #include "blk.h" |
36 | #include "blk-mq.h" | |
9c1051aa | 37 | #include "blk-mq-debugfs.h" |
320ae51f | 38 | #include "blk-mq-tag.h" |
986d413b | 39 | #include "blk-pm.h" |
cf43e6be | 40 | #include "blk-stat.h" |
bd166ef1 | 41 | #include "blk-mq-sched.h" |
c1c80384 | 42 | #include "blk-rq-qos.h" |
320ae51f | 43 | |
c3077b5d CH |
44 | static DEFINE_PER_CPU(struct list_head, blk_cpu_done); |
45 | ||
34dbad5d OS |
46 | static void blk_mq_poll_stats_start(struct request_queue *q); |
47 | static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb); | |
48 | ||
720b8ccc SB |
49 | static int blk_mq_poll_stats_bkt(const struct request *rq) |
50 | { | |
3d244306 | 51 | int ddir, sectors, bucket; |
720b8ccc | 52 | |
99c749a4 | 53 | ddir = rq_data_dir(rq); |
3d244306 | 54 | sectors = blk_rq_stats_sectors(rq); |
720b8ccc | 55 | |
3d244306 | 56 | bucket = ddir + 2 * ilog2(sectors); |
720b8ccc SB |
57 | |
58 | if (bucket < 0) | |
59 | return -1; | |
60 | else if (bucket >= BLK_MQ_POLL_STATS_BKTS) | |
61 | return ddir + BLK_MQ_POLL_STATS_BKTS - 2; | |
62 | ||
63 | return bucket; | |
64 | } | |
65 | ||
320ae51f | 66 | /* |
85fae294 YY |
67 | * Check if any of the ctx, dispatch list or elevator |
68 | * have pending work in this hardware queue. | |
320ae51f | 69 | */ |
79f720a7 | 70 | static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx) |
320ae51f | 71 | { |
79f720a7 JA |
72 | return !list_empty_careful(&hctx->dispatch) || |
73 | sbitmap_any_bit_set(&hctx->ctx_map) || | |
bd166ef1 | 74 | blk_mq_sched_has_work(hctx); |
1429d7c9 JA |
75 | } |
76 | ||
320ae51f JA |
77 | /* |
78 | * Mark this ctx as having pending work in this hardware queue | |
79 | */ | |
80 | static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx, | |
81 | struct blk_mq_ctx *ctx) | |
82 | { | |
f31967f0 JA |
83 | const int bit = ctx->index_hw[hctx->type]; |
84 | ||
85 | if (!sbitmap_test_bit(&hctx->ctx_map, bit)) | |
86 | sbitmap_set_bit(&hctx->ctx_map, bit); | |
1429d7c9 JA |
87 | } |
88 | ||
89 | static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx, | |
90 | struct blk_mq_ctx *ctx) | |
91 | { | |
f31967f0 JA |
92 | const int bit = ctx->index_hw[hctx->type]; |
93 | ||
94 | sbitmap_clear_bit(&hctx->ctx_map, bit); | |
320ae51f JA |
95 | } |
96 | ||
f299b7c7 JA |
97 | struct mq_inflight { |
98 | struct hd_struct *part; | |
a2e80f6f | 99 | unsigned int inflight[2]; |
f299b7c7 JA |
100 | }; |
101 | ||
7baa8572 | 102 | static bool blk_mq_check_inflight(struct blk_mq_hw_ctx *hctx, |
f299b7c7 JA |
103 | struct request *rq, void *priv, |
104 | bool reserved) | |
105 | { | |
106 | struct mq_inflight *mi = priv; | |
107 | ||
6131837b | 108 | if (rq->part == mi->part) |
bb4e6b14 | 109 | mi->inflight[rq_data_dir(rq)]++; |
7baa8572 JA |
110 | |
111 | return true; | |
f299b7c7 JA |
112 | } |
113 | ||
e016b782 | 114 | unsigned int blk_mq_in_flight(struct request_queue *q, struct hd_struct *part) |
f299b7c7 | 115 | { |
a2e80f6f | 116 | struct mq_inflight mi = { .part = part }; |
f299b7c7 | 117 | |
f299b7c7 | 118 | blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi); |
e016b782 | 119 | |
a2e80f6f | 120 | return mi.inflight[0] + mi.inflight[1]; |
bf0ddaba OS |
121 | } |
122 | ||
123 | void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part, | |
124 | unsigned int inflight[2]) | |
125 | { | |
a2e80f6f | 126 | struct mq_inflight mi = { .part = part }; |
bf0ddaba | 127 | |
bb4e6b14 | 128 | blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi); |
a2e80f6f PB |
129 | inflight[0] = mi.inflight[0]; |
130 | inflight[1] = mi.inflight[1]; | |
bf0ddaba OS |
131 | } |
132 | ||
1671d522 | 133 | void blk_freeze_queue_start(struct request_queue *q) |
43a5e4e2 | 134 | { |
7996a8b5 BL |
135 | mutex_lock(&q->mq_freeze_lock); |
136 | if (++q->mq_freeze_depth == 1) { | |
3ef28e83 | 137 | percpu_ref_kill(&q->q_usage_counter); |
7996a8b5 | 138 | mutex_unlock(&q->mq_freeze_lock); |
344e9ffc | 139 | if (queue_is_mq(q)) |
055f6e18 | 140 | blk_mq_run_hw_queues(q, false); |
7996a8b5 BL |
141 | } else { |
142 | mutex_unlock(&q->mq_freeze_lock); | |
cddd5d17 | 143 | } |
f3af020b | 144 | } |
1671d522 | 145 | EXPORT_SYMBOL_GPL(blk_freeze_queue_start); |
f3af020b | 146 | |
6bae363e | 147 | void blk_mq_freeze_queue_wait(struct request_queue *q) |
f3af020b | 148 | { |
3ef28e83 | 149 | wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter)); |
43a5e4e2 | 150 | } |
6bae363e | 151 | EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait); |
43a5e4e2 | 152 | |
f91328c4 KB |
153 | int blk_mq_freeze_queue_wait_timeout(struct request_queue *q, |
154 | unsigned long timeout) | |
155 | { | |
156 | return wait_event_timeout(q->mq_freeze_wq, | |
157 | percpu_ref_is_zero(&q->q_usage_counter), | |
158 | timeout); | |
159 | } | |
160 | EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout); | |
43a5e4e2 | 161 | |
f3af020b TH |
162 | /* |
163 | * Guarantee no request is in use, so we can change any data structure of | |
164 | * the queue afterward. | |
165 | */ | |
3ef28e83 | 166 | void blk_freeze_queue(struct request_queue *q) |
f3af020b | 167 | { |
3ef28e83 DW |
168 | /* |
169 | * In the !blk_mq case we are only calling this to kill the | |
170 | * q_usage_counter, otherwise this increases the freeze depth | |
171 | * and waits for it to return to zero. For this reason there is | |
172 | * no blk_unfreeze_queue(), and blk_freeze_queue() is not | |
173 | * exported to drivers as the only user for unfreeze is blk_mq. | |
174 | */ | |
1671d522 | 175 | blk_freeze_queue_start(q); |
f3af020b TH |
176 | blk_mq_freeze_queue_wait(q); |
177 | } | |
3ef28e83 DW |
178 | |
179 | void blk_mq_freeze_queue(struct request_queue *q) | |
180 | { | |
181 | /* | |
182 | * ...just an alias to keep freeze and unfreeze actions balanced | |
183 | * in the blk_mq_* namespace | |
184 | */ | |
185 | blk_freeze_queue(q); | |
186 | } | |
c761d96b | 187 | EXPORT_SYMBOL_GPL(blk_mq_freeze_queue); |
f3af020b | 188 | |
b4c6a028 | 189 | void blk_mq_unfreeze_queue(struct request_queue *q) |
320ae51f | 190 | { |
7996a8b5 BL |
191 | mutex_lock(&q->mq_freeze_lock); |
192 | q->mq_freeze_depth--; | |
193 | WARN_ON_ONCE(q->mq_freeze_depth < 0); | |
194 | if (!q->mq_freeze_depth) { | |
bdd63160 | 195 | percpu_ref_resurrect(&q->q_usage_counter); |
320ae51f | 196 | wake_up_all(&q->mq_freeze_wq); |
add703fd | 197 | } |
7996a8b5 | 198 | mutex_unlock(&q->mq_freeze_lock); |
320ae51f | 199 | } |
b4c6a028 | 200 | EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue); |
320ae51f | 201 | |
852ec809 BVA |
202 | /* |
203 | * FIXME: replace the scsi_internal_device_*block_nowait() calls in the | |
204 | * mpt3sas driver such that this function can be removed. | |
205 | */ | |
206 | void blk_mq_quiesce_queue_nowait(struct request_queue *q) | |
207 | { | |
8814ce8a | 208 | blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q); |
852ec809 BVA |
209 | } |
210 | EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait); | |
211 | ||
6a83e74d | 212 | /** |
69e07c4a | 213 | * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished |
6a83e74d BVA |
214 | * @q: request queue. |
215 | * | |
216 | * Note: this function does not prevent that the struct request end_io() | |
69e07c4a ML |
217 | * callback function is invoked. Once this function is returned, we make |
218 | * sure no dispatch can happen until the queue is unquiesced via | |
219 | * blk_mq_unquiesce_queue(). | |
6a83e74d BVA |
220 | */ |
221 | void blk_mq_quiesce_queue(struct request_queue *q) | |
222 | { | |
223 | struct blk_mq_hw_ctx *hctx; | |
224 | unsigned int i; | |
225 | bool rcu = false; | |
226 | ||
1d9e9bc6 | 227 | blk_mq_quiesce_queue_nowait(q); |
f4560ffe | 228 | |
6a83e74d BVA |
229 | queue_for_each_hw_ctx(q, hctx, i) { |
230 | if (hctx->flags & BLK_MQ_F_BLOCKING) | |
05707b64 | 231 | synchronize_srcu(hctx->srcu); |
6a83e74d BVA |
232 | else |
233 | rcu = true; | |
234 | } | |
235 | if (rcu) | |
236 | synchronize_rcu(); | |
237 | } | |
238 | EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue); | |
239 | ||
e4e73913 ML |
240 | /* |
241 | * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue() | |
242 | * @q: request queue. | |
243 | * | |
244 | * This function recovers queue into the state before quiescing | |
245 | * which is done by blk_mq_quiesce_queue. | |
246 | */ | |
247 | void blk_mq_unquiesce_queue(struct request_queue *q) | |
248 | { | |
8814ce8a | 249 | blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q); |
f4560ffe | 250 | |
1d9e9bc6 ML |
251 | /* dispatch requests which are inserted during quiescing */ |
252 | blk_mq_run_hw_queues(q, true); | |
e4e73913 ML |
253 | } |
254 | EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue); | |
255 | ||
aed3ea94 JA |
256 | void blk_mq_wake_waiters(struct request_queue *q) |
257 | { | |
258 | struct blk_mq_hw_ctx *hctx; | |
259 | unsigned int i; | |
260 | ||
261 | queue_for_each_hw_ctx(q, hctx, i) | |
262 | if (blk_mq_hw_queue_mapped(hctx)) | |
263 | blk_mq_tag_wakeup_all(hctx->tags, true); | |
264 | } | |
265 | ||
fe1f4526 | 266 | /* |
9a91b05b HT |
267 | * Only need start/end time stamping if we have iostat or |
268 | * blk stats enabled, or using an IO scheduler. | |
fe1f4526 JA |
269 | */ |
270 | static inline bool blk_mq_need_time_stamp(struct request *rq) | |
271 | { | |
9a91b05b | 272 | return (rq->rq_flags & (RQF_IO_STAT | RQF_STATS)) || rq->q->elevator; |
fe1f4526 JA |
273 | } |
274 | ||
e4cdf1a1 | 275 | static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data, |
7ea4d8a4 | 276 | unsigned int tag, u64 alloc_time_ns) |
320ae51f | 277 | { |
e4cdf1a1 CH |
278 | struct blk_mq_tags *tags = blk_mq_tags_from_data(data); |
279 | struct request *rq = tags->static_rqs[tag]; | |
bf9ae8c5 | 280 | req_flags_t rq_flags = 0; |
c3a148d2 | 281 | |
e4cdf1a1 | 282 | if (data->flags & BLK_MQ_REQ_INTERNAL) { |
76647368 | 283 | rq->tag = BLK_MQ_NO_TAG; |
e4cdf1a1 CH |
284 | rq->internal_tag = tag; |
285 | } else { | |
d263ed99 | 286 | if (data->hctx->flags & BLK_MQ_F_TAG_SHARED) { |
bf9ae8c5 | 287 | rq_flags = RQF_MQ_INFLIGHT; |
e4cdf1a1 CH |
288 | atomic_inc(&data->hctx->nr_active); |
289 | } | |
290 | rq->tag = tag; | |
76647368 | 291 | rq->internal_tag = BLK_MQ_NO_TAG; |
e4cdf1a1 CH |
292 | data->hctx->tags->rqs[rq->tag] = rq; |
293 | } | |
294 | ||
af76e555 | 295 | /* csd/requeue_work/fifo_time is initialized before use */ |
e4cdf1a1 CH |
296 | rq->q = data->q; |
297 | rq->mq_ctx = data->ctx; | |
ea4f995e | 298 | rq->mq_hctx = data->hctx; |
bf9ae8c5 | 299 | rq->rq_flags = rq_flags; |
7ea4d8a4 | 300 | rq->cmd_flags = data->cmd_flags; |
1b6d65a0 BVA |
301 | if (data->flags & BLK_MQ_REQ_PREEMPT) |
302 | rq->rq_flags |= RQF_PREEMPT; | |
e4cdf1a1 | 303 | if (blk_queue_io_stat(data->q)) |
e8064021 | 304 | rq->rq_flags |= RQF_IO_STAT; |
7c3fb70f | 305 | INIT_LIST_HEAD(&rq->queuelist); |
af76e555 CH |
306 | INIT_HLIST_NODE(&rq->hash); |
307 | RB_CLEAR_NODE(&rq->rb_node); | |
af76e555 CH |
308 | rq->rq_disk = NULL; |
309 | rq->part = NULL; | |
6f816b4b TH |
310 | #ifdef CONFIG_BLK_RQ_ALLOC_TIME |
311 | rq->alloc_time_ns = alloc_time_ns; | |
312 | #endif | |
fe1f4526 JA |
313 | if (blk_mq_need_time_stamp(rq)) |
314 | rq->start_time_ns = ktime_get_ns(); | |
315 | else | |
316 | rq->start_time_ns = 0; | |
544ccc8d | 317 | rq->io_start_time_ns = 0; |
3d244306 | 318 | rq->stats_sectors = 0; |
af76e555 CH |
319 | rq->nr_phys_segments = 0; |
320 | #if defined(CONFIG_BLK_DEV_INTEGRITY) | |
321 | rq->nr_integrity_segments = 0; | |
322 | #endif | |
a892c8d5 | 323 | blk_crypto_rq_set_defaults(rq); |
af76e555 | 324 | /* tag was already set */ |
079076b3 | 325 | WRITE_ONCE(rq->deadline, 0); |
af76e555 | 326 | |
f6be4fb4 JA |
327 | rq->timeout = 0; |
328 | ||
af76e555 CH |
329 | rq->end_io = NULL; |
330 | rq->end_io_data = NULL; | |
af76e555 | 331 | |
7ea4d8a4 | 332 | data->ctx->rq_dispatched[op_is_sync(data->cmd_flags)]++; |
12f5b931 | 333 | refcount_set(&rq->ref, 1); |
7ea4d8a4 CH |
334 | |
335 | if (!op_is_flush(data->cmd_flags)) { | |
336 | struct elevator_queue *e = data->q->elevator; | |
337 | ||
338 | rq->elv.icq = NULL; | |
339 | if (e && e->type->ops.prepare_request) { | |
340 | if (e->type->icq_cache) | |
341 | blk_mq_sched_assign_ioc(rq); | |
342 | ||
343 | e->type->ops.prepare_request(rq); | |
344 | rq->rq_flags |= RQF_ELVPRIV; | |
345 | } | |
346 | } | |
347 | ||
348 | data->hctx->queued++; | |
e4cdf1a1 | 349 | return rq; |
5dee8577 CH |
350 | } |
351 | ||
e6e7abff | 352 | static struct request *__blk_mq_alloc_request(struct blk_mq_alloc_data *data) |
d2c0d383 | 353 | { |
e6e7abff | 354 | struct request_queue *q = data->q; |
d2c0d383 | 355 | struct elevator_queue *e = q->elevator; |
6f816b4b | 356 | u64 alloc_time_ns = 0; |
600c3b0c | 357 | unsigned int tag; |
d2c0d383 | 358 | |
6f816b4b TH |
359 | /* alloc_time includes depth and tag waits */ |
360 | if (blk_queue_rq_alloc_time(q)) | |
361 | alloc_time_ns = ktime_get_ns(); | |
362 | ||
f9afca4d | 363 | if (data->cmd_flags & REQ_NOWAIT) |
03a07c92 | 364 | data->flags |= BLK_MQ_REQ_NOWAIT; |
d2c0d383 CH |
365 | |
366 | if (e) { | |
367 | data->flags |= BLK_MQ_REQ_INTERNAL; | |
368 | ||
369 | /* | |
370 | * Flush requests are special and go directly to the | |
17a51199 JA |
371 | * dispatch list. Don't include reserved tags in the |
372 | * limiting, as it isn't useful. | |
d2c0d383 | 373 | */ |
f9afca4d JA |
374 | if (!op_is_flush(data->cmd_flags) && |
375 | e->type->ops.limit_depth && | |
17a51199 | 376 | !(data->flags & BLK_MQ_REQ_RESERVED)) |
f9afca4d | 377 | e->type->ops.limit_depth(data->cmd_flags, data); |
d2c0d383 CH |
378 | } |
379 | ||
bf0beec0 | 380 | retry: |
600c3b0c CH |
381 | data->ctx = blk_mq_get_ctx(q); |
382 | data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx); | |
383 | if (!(data->flags & BLK_MQ_REQ_INTERNAL)) | |
384 | blk_mq_tag_busy(data->hctx); | |
385 | ||
bf0beec0 ML |
386 | /* |
387 | * Waiting allocations only fail because of an inactive hctx. In that | |
388 | * case just retry the hctx assignment and tag allocation as CPU hotplug | |
389 | * should have migrated us to an online CPU by now. | |
390 | */ | |
e4cdf1a1 | 391 | tag = blk_mq_get_tag(data); |
bf0beec0 ML |
392 | if (tag == BLK_MQ_NO_TAG) { |
393 | if (data->flags & BLK_MQ_REQ_NOWAIT) | |
394 | return NULL; | |
395 | ||
396 | /* | |
397 | * Give up the CPU and sleep for a random short time to ensure | |
398 | * that thread using a realtime scheduling class are migrated | |
399 | * off the the CPU, and thus off the hctx that is going away. | |
400 | */ | |
401 | msleep(3); | |
402 | goto retry; | |
403 | } | |
7ea4d8a4 | 404 | return blk_mq_rq_ctx_init(data, tag, alloc_time_ns); |
d2c0d383 CH |
405 | } |
406 | ||
cd6ce148 | 407 | struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op, |
9a95e4ef | 408 | blk_mq_req_flags_t flags) |
320ae51f | 409 | { |
e6e7abff CH |
410 | struct blk_mq_alloc_data data = { |
411 | .q = q, | |
412 | .flags = flags, | |
413 | .cmd_flags = op, | |
414 | }; | |
bd166ef1 | 415 | struct request *rq; |
a492f075 | 416 | int ret; |
320ae51f | 417 | |
3a0a5299 | 418 | ret = blk_queue_enter(q, flags); |
a492f075 JL |
419 | if (ret) |
420 | return ERR_PTR(ret); | |
320ae51f | 421 | |
e6e7abff | 422 | rq = __blk_mq_alloc_request(&data); |
bd166ef1 | 423 | if (!rq) |
a5ea5811 | 424 | goto out_queue_exit; |
0c4de0f3 CH |
425 | rq->__data_len = 0; |
426 | rq->__sector = (sector_t) -1; | |
427 | rq->bio = rq->biotail = NULL; | |
320ae51f | 428 | return rq; |
a5ea5811 CH |
429 | out_queue_exit: |
430 | blk_queue_exit(q); | |
431 | return ERR_PTR(-EWOULDBLOCK); | |
320ae51f | 432 | } |
4bb659b1 | 433 | EXPORT_SYMBOL(blk_mq_alloc_request); |
320ae51f | 434 | |
cd6ce148 | 435 | struct request *blk_mq_alloc_request_hctx(struct request_queue *q, |
9a95e4ef | 436 | unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx) |
1f5bd336 | 437 | { |
e6e7abff CH |
438 | struct blk_mq_alloc_data data = { |
439 | .q = q, | |
440 | .flags = flags, | |
441 | .cmd_flags = op, | |
442 | }; | |
600c3b0c | 443 | u64 alloc_time_ns = 0; |
6d2809d5 | 444 | unsigned int cpu; |
600c3b0c | 445 | unsigned int tag; |
1f5bd336 ML |
446 | int ret; |
447 | ||
600c3b0c CH |
448 | /* alloc_time includes depth and tag waits */ |
449 | if (blk_queue_rq_alloc_time(q)) | |
450 | alloc_time_ns = ktime_get_ns(); | |
451 | ||
1f5bd336 ML |
452 | /* |
453 | * If the tag allocator sleeps we could get an allocation for a | |
454 | * different hardware context. No need to complicate the low level | |
455 | * allocator for this for the rare use case of a command tied to | |
456 | * a specific queue. | |
457 | */ | |
600c3b0c | 458 | if (WARN_ON_ONCE(!(flags & (BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED)))) |
1f5bd336 ML |
459 | return ERR_PTR(-EINVAL); |
460 | ||
461 | if (hctx_idx >= q->nr_hw_queues) | |
462 | return ERR_PTR(-EIO); | |
463 | ||
3a0a5299 | 464 | ret = blk_queue_enter(q, flags); |
1f5bd336 ML |
465 | if (ret) |
466 | return ERR_PTR(ret); | |
467 | ||
c8712c6a CH |
468 | /* |
469 | * Check if the hardware context is actually mapped to anything. | |
470 | * If not tell the caller that it should skip this queue. | |
471 | */ | |
a5ea5811 | 472 | ret = -EXDEV; |
e6e7abff CH |
473 | data.hctx = q->queue_hw_ctx[hctx_idx]; |
474 | if (!blk_mq_hw_queue_mapped(data.hctx)) | |
a5ea5811 | 475 | goto out_queue_exit; |
e6e7abff CH |
476 | cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask); |
477 | data.ctx = __blk_mq_get_ctx(q, cpu); | |
1f5bd336 | 478 | |
600c3b0c CH |
479 | if (q->elevator) |
480 | data.flags |= BLK_MQ_REQ_INTERNAL; | |
481 | else | |
482 | blk_mq_tag_busy(data.hctx); | |
483 | ||
a5ea5811 | 484 | ret = -EWOULDBLOCK; |
600c3b0c CH |
485 | tag = blk_mq_get_tag(&data); |
486 | if (tag == BLK_MQ_NO_TAG) | |
a5ea5811 | 487 | goto out_queue_exit; |
600c3b0c CH |
488 | return blk_mq_rq_ctx_init(&data, tag, alloc_time_ns); |
489 | ||
a5ea5811 CH |
490 | out_queue_exit: |
491 | blk_queue_exit(q); | |
492 | return ERR_PTR(ret); | |
1f5bd336 ML |
493 | } |
494 | EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx); | |
495 | ||
12f5b931 KB |
496 | static void __blk_mq_free_request(struct request *rq) |
497 | { | |
498 | struct request_queue *q = rq->q; | |
499 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
ea4f995e | 500 | struct blk_mq_hw_ctx *hctx = rq->mq_hctx; |
12f5b931 KB |
501 | const int sched_tag = rq->internal_tag; |
502 | ||
a892c8d5 | 503 | blk_crypto_free_request(rq); |
986d413b | 504 | blk_pm_mark_last_busy(rq); |
ea4f995e | 505 | rq->mq_hctx = NULL; |
76647368 | 506 | if (rq->tag != BLK_MQ_NO_TAG) |
cae740a0 | 507 | blk_mq_put_tag(hctx->tags, ctx, rq->tag); |
76647368 | 508 | if (sched_tag != BLK_MQ_NO_TAG) |
cae740a0 | 509 | blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag); |
12f5b931 KB |
510 | blk_mq_sched_restart(hctx); |
511 | blk_queue_exit(q); | |
512 | } | |
513 | ||
6af54051 | 514 | void blk_mq_free_request(struct request *rq) |
320ae51f | 515 | { |
320ae51f | 516 | struct request_queue *q = rq->q; |
6af54051 CH |
517 | struct elevator_queue *e = q->elevator; |
518 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
ea4f995e | 519 | struct blk_mq_hw_ctx *hctx = rq->mq_hctx; |
6af54051 | 520 | |
5bbf4e5a | 521 | if (rq->rq_flags & RQF_ELVPRIV) { |
f9cd4bfe JA |
522 | if (e && e->type->ops.finish_request) |
523 | e->type->ops.finish_request(rq); | |
6af54051 CH |
524 | if (rq->elv.icq) { |
525 | put_io_context(rq->elv.icq->ioc); | |
526 | rq->elv.icq = NULL; | |
527 | } | |
528 | } | |
320ae51f | 529 | |
6af54051 | 530 | ctx->rq_completed[rq_is_sync(rq)]++; |
e8064021 | 531 | if (rq->rq_flags & RQF_MQ_INFLIGHT) |
0d2602ca | 532 | atomic_dec(&hctx->nr_active); |
87760e5e | 533 | |
7beb2f84 JA |
534 | if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq))) |
535 | laptop_io_completion(q->backing_dev_info); | |
536 | ||
a7905043 | 537 | rq_qos_done(q, rq); |
0d2602ca | 538 | |
12f5b931 KB |
539 | WRITE_ONCE(rq->state, MQ_RQ_IDLE); |
540 | if (refcount_dec_and_test(&rq->ref)) | |
541 | __blk_mq_free_request(rq); | |
320ae51f | 542 | } |
1a3b595a | 543 | EXPORT_SYMBOL_GPL(blk_mq_free_request); |
320ae51f | 544 | |
2a842aca | 545 | inline void __blk_mq_end_request(struct request *rq, blk_status_t error) |
320ae51f | 546 | { |
fe1f4526 JA |
547 | u64 now = 0; |
548 | ||
549 | if (blk_mq_need_time_stamp(rq)) | |
550 | now = ktime_get_ns(); | |
522a7775 | 551 | |
4bc6339a OS |
552 | if (rq->rq_flags & RQF_STATS) { |
553 | blk_mq_poll_stats_start(rq->q); | |
522a7775 | 554 | blk_stat_add(rq, now); |
4bc6339a OS |
555 | } |
556 | ||
76647368 | 557 | if (rq->internal_tag != BLK_MQ_NO_TAG) |
ed88660a OS |
558 | blk_mq_sched_completed_request(rq, now); |
559 | ||
522a7775 | 560 | blk_account_io_done(rq, now); |
0d11e6ac | 561 | |
91b63639 | 562 | if (rq->end_io) { |
a7905043 | 563 | rq_qos_done(rq->q, rq); |
320ae51f | 564 | rq->end_io(rq, error); |
91b63639 | 565 | } else { |
320ae51f | 566 | blk_mq_free_request(rq); |
91b63639 | 567 | } |
320ae51f | 568 | } |
c8a446ad | 569 | EXPORT_SYMBOL(__blk_mq_end_request); |
63151a44 | 570 | |
2a842aca | 571 | void blk_mq_end_request(struct request *rq, blk_status_t error) |
63151a44 CH |
572 | { |
573 | if (blk_update_request(rq, error, blk_rq_bytes(rq))) | |
574 | BUG(); | |
c8a446ad | 575 | __blk_mq_end_request(rq, error); |
63151a44 | 576 | } |
c8a446ad | 577 | EXPORT_SYMBOL(blk_mq_end_request); |
320ae51f | 578 | |
c3077b5d CH |
579 | /* |
580 | * Softirq action handler - move entries to local list and loop over them | |
581 | * while passing them to the queue registered handler. | |
582 | */ | |
583 | static __latent_entropy void blk_done_softirq(struct softirq_action *h) | |
584 | { | |
585 | struct list_head *cpu_list, local_list; | |
586 | ||
587 | local_irq_disable(); | |
588 | cpu_list = this_cpu_ptr(&blk_cpu_done); | |
589 | list_replace_init(cpu_list, &local_list); | |
590 | local_irq_enable(); | |
591 | ||
592 | while (!list_empty(&local_list)) { | |
593 | struct request *rq; | |
594 | ||
595 | rq = list_entry(local_list.next, struct request, ipi_list); | |
596 | list_del_init(&rq->ipi_list); | |
597 | rq->q->mq_ops->complete(rq); | |
598 | } | |
599 | } | |
600 | ||
115243f5 | 601 | static void blk_mq_trigger_softirq(struct request *rq) |
c3077b5d | 602 | { |
d391a7a3 CH |
603 | struct list_head *list; |
604 | unsigned long flags; | |
c3077b5d | 605 | |
d391a7a3 CH |
606 | local_irq_save(flags); |
607 | list = this_cpu_ptr(&blk_cpu_done); | |
c3077b5d CH |
608 | list_add_tail(&rq->ipi_list, list); |
609 | ||
115243f5 CH |
610 | /* |
611 | * If the list only contains our just added request, signal a raise of | |
612 | * the softirq. If there are already entries there, someone already | |
613 | * raised the irq but it hasn't run yet. | |
614 | */ | |
c3077b5d CH |
615 | if (list->next == &rq->ipi_list) |
616 | raise_softirq_irqoff(BLOCK_SOFTIRQ); | |
d391a7a3 | 617 | local_irq_restore(flags); |
115243f5 CH |
618 | } |
619 | ||
c3077b5d CH |
620 | static int blk_softirq_cpu_dead(unsigned int cpu) |
621 | { | |
622 | /* | |
623 | * If a CPU goes away, splice its entries to the current CPU | |
624 | * and trigger a run of the softirq | |
625 | */ | |
626 | local_irq_disable(); | |
627 | list_splice_init(&per_cpu(blk_cpu_done, cpu), | |
628 | this_cpu_ptr(&blk_cpu_done)); | |
629 | raise_softirq_irqoff(BLOCK_SOFTIRQ); | |
630 | local_irq_enable(); | |
631 | ||
632 | return 0; | |
633 | } | |
634 | ||
d391a7a3 | 635 | static void __blk_mq_complete_request(struct request *rq) |
c3077b5d | 636 | { |
c3077b5d | 637 | /* |
d391a7a3 CH |
638 | * For most of single queue controllers, there is only one irq vector |
639 | * for handling I/O completion, and the only irq's affinity is set | |
640 | * to all possible CPUs. On most of ARCHs, this affinity means the irq | |
641 | * is handled on one specific CPU. | |
642 | * | |
643 | * So complete I/O requests in softirq context in case of single queue | |
644 | * devices to avoid degrading I/O performance due to irqsoff latency. | |
c3077b5d | 645 | */ |
d391a7a3 CH |
646 | if (rq->q->nr_hw_queues == 1) |
647 | blk_mq_trigger_softirq(rq); | |
648 | else | |
649 | rq->q->mq_ops->complete(rq); | |
c3077b5d CH |
650 | } |
651 | ||
30a91cb4 | 652 | static void __blk_mq_complete_request_remote(void *data) |
320ae51f | 653 | { |
d391a7a3 | 654 | __blk_mq_complete_request(data); |
320ae51f | 655 | } |
320ae51f | 656 | |
7b11eab0 KB |
657 | /** |
658 | * blk_mq_force_complete_rq() - Force complete the request, bypassing any error | |
659 | * injection that could drop the completion. | |
660 | * @rq: Request to be force completed | |
661 | * | |
662 | * Drivers should use blk_mq_complete_request() to complete requests in their | |
663 | * normal IO path. For timeout error recovery, drivers may call this forced | |
664 | * completion routine after they've reclaimed timed out requests to bypass | |
665 | * potentially subsequent fake timeouts. | |
666 | */ | |
667 | void blk_mq_force_complete_rq(struct request *rq) | |
320ae51f JA |
668 | { |
669 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
c7bb9ad1 | 670 | struct request_queue *q = rq->q; |
38535201 | 671 | bool shared = false; |
320ae51f JA |
672 | int cpu; |
673 | ||
af78ff7c | 674 | WRITE_ONCE(rq->state, MQ_RQ_COMPLETE); |
6aab1da6 CH |
675 | |
676 | /* | |
677 | * For a polled request, always complete locallly, it's pointless | |
678 | * to redirect the completion. | |
679 | */ | |
680 | if (rq->cmd_flags & REQ_HIPRI) { | |
681 | q->mq_ops->complete(rq); | |
682 | return; | |
683 | } | |
684 | ||
d6cc464c CH |
685 | if (!IS_ENABLED(CONFIG_SMP) || |
686 | !test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags)) { | |
d391a7a3 | 687 | __blk_mq_complete_request(rq); |
30a91cb4 CH |
688 | return; |
689 | } | |
320ae51f JA |
690 | |
691 | cpu = get_cpu(); | |
c7bb9ad1 | 692 | if (!test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags)) |
38535201 CH |
693 | shared = cpus_share_cache(cpu, ctx->cpu); |
694 | ||
695 | if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) { | |
30a91cb4 | 696 | rq->csd.func = __blk_mq_complete_request_remote; |
3d6efbf6 CH |
697 | rq->csd.info = rq; |
698 | rq->csd.flags = 0; | |
c46fff2a | 699 | smp_call_function_single_async(ctx->cpu, &rq->csd); |
3d6efbf6 | 700 | } else { |
d391a7a3 | 701 | __blk_mq_complete_request(rq); |
3d6efbf6 | 702 | } |
320ae51f JA |
703 | put_cpu(); |
704 | } | |
7b11eab0 | 705 | EXPORT_SYMBOL_GPL(blk_mq_force_complete_rq); |
30a91cb4 | 706 | |
04ced159 | 707 | static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx) |
b7435db8 | 708 | __releases(hctx->srcu) |
04ced159 JA |
709 | { |
710 | if (!(hctx->flags & BLK_MQ_F_BLOCKING)) | |
711 | rcu_read_unlock(); | |
712 | else | |
05707b64 | 713 | srcu_read_unlock(hctx->srcu, srcu_idx); |
04ced159 JA |
714 | } |
715 | ||
716 | static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx) | |
b7435db8 | 717 | __acquires(hctx->srcu) |
04ced159 | 718 | { |
08b5a6e2 JA |
719 | if (!(hctx->flags & BLK_MQ_F_BLOCKING)) { |
720 | /* shut up gcc false positive */ | |
721 | *srcu_idx = 0; | |
04ced159 | 722 | rcu_read_lock(); |
08b5a6e2 | 723 | } else |
05707b64 | 724 | *srcu_idx = srcu_read_lock(hctx->srcu); |
04ced159 JA |
725 | } |
726 | ||
30a91cb4 CH |
727 | /** |
728 | * blk_mq_complete_request - end I/O on a request | |
729 | * @rq: the request being processed | |
730 | * | |
731 | * Description: | |
732 | * Ends all I/O on a request. It does not handle partial completions. | |
733 | * The actual completion happens out-of-order, through a IPI handler. | |
734 | **/ | |
16c15eb1 | 735 | bool blk_mq_complete_request(struct request *rq) |
30a91cb4 | 736 | { |
12f5b931 | 737 | if (unlikely(blk_should_fake_timeout(rq->q))) |
16c15eb1 | 738 | return false; |
7b11eab0 | 739 | blk_mq_force_complete_rq(rq); |
16c15eb1 | 740 | return true; |
30a91cb4 CH |
741 | } |
742 | EXPORT_SYMBOL(blk_mq_complete_request); | |
320ae51f | 743 | |
105663f7 AA |
744 | /** |
745 | * blk_mq_start_request - Start processing a request | |
746 | * @rq: Pointer to request to be started | |
747 | * | |
748 | * Function used by device drivers to notify the block layer that a request | |
749 | * is going to be processed now, so blk layer can do proper initializations | |
750 | * such as starting the timeout timer. | |
751 | */ | |
e2490073 | 752 | void blk_mq_start_request(struct request *rq) |
320ae51f JA |
753 | { |
754 | struct request_queue *q = rq->q; | |
755 | ||
756 | trace_block_rq_issue(q, rq); | |
757 | ||
cf43e6be | 758 | if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) { |
544ccc8d | 759 | rq->io_start_time_ns = ktime_get_ns(); |
3d244306 | 760 | rq->stats_sectors = blk_rq_sectors(rq); |
cf43e6be | 761 | rq->rq_flags |= RQF_STATS; |
a7905043 | 762 | rq_qos_issue(q, rq); |
cf43e6be JA |
763 | } |
764 | ||
1d9bd516 | 765 | WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE); |
538b7534 | 766 | |
1d9bd516 | 767 | blk_add_timer(rq); |
12f5b931 | 768 | WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT); |
49f5baa5 | 769 | |
54d4e6ab MG |
770 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
771 | if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE) | |
772 | q->integrity.profile->prepare_fn(rq); | |
773 | #endif | |
320ae51f | 774 | } |
e2490073 | 775 | EXPORT_SYMBOL(blk_mq_start_request); |
320ae51f | 776 | |
ed0791b2 | 777 | static void __blk_mq_requeue_request(struct request *rq) |
320ae51f JA |
778 | { |
779 | struct request_queue *q = rq->q; | |
780 | ||
923218f6 ML |
781 | blk_mq_put_driver_tag(rq); |
782 | ||
320ae51f | 783 | trace_block_rq_requeue(q, rq); |
a7905043 | 784 | rq_qos_requeue(q, rq); |
49f5baa5 | 785 | |
12f5b931 KB |
786 | if (blk_mq_request_started(rq)) { |
787 | WRITE_ONCE(rq->state, MQ_RQ_IDLE); | |
da661267 | 788 | rq->rq_flags &= ~RQF_TIMED_OUT; |
e2490073 | 789 | } |
320ae51f JA |
790 | } |
791 | ||
2b053aca | 792 | void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list) |
ed0791b2 | 793 | { |
ed0791b2 | 794 | __blk_mq_requeue_request(rq); |
ed0791b2 | 795 | |
105976f5 ML |
796 | /* this request will be re-inserted to io scheduler queue */ |
797 | blk_mq_sched_requeue_request(rq); | |
798 | ||
7d692330 | 799 | BUG_ON(!list_empty(&rq->queuelist)); |
2b053aca | 800 | blk_mq_add_to_requeue_list(rq, true, kick_requeue_list); |
ed0791b2 CH |
801 | } |
802 | EXPORT_SYMBOL(blk_mq_requeue_request); | |
803 | ||
6fca6a61 CH |
804 | static void blk_mq_requeue_work(struct work_struct *work) |
805 | { | |
806 | struct request_queue *q = | |
2849450a | 807 | container_of(work, struct request_queue, requeue_work.work); |
6fca6a61 CH |
808 | LIST_HEAD(rq_list); |
809 | struct request *rq, *next; | |
6fca6a61 | 810 | |
18e9781d | 811 | spin_lock_irq(&q->requeue_lock); |
6fca6a61 | 812 | list_splice_init(&q->requeue_list, &rq_list); |
18e9781d | 813 | spin_unlock_irq(&q->requeue_lock); |
6fca6a61 CH |
814 | |
815 | list_for_each_entry_safe(rq, next, &rq_list, queuelist) { | |
aef1897c | 816 | if (!(rq->rq_flags & (RQF_SOFTBARRIER | RQF_DONTPREP))) |
6fca6a61 CH |
817 | continue; |
818 | ||
e8064021 | 819 | rq->rq_flags &= ~RQF_SOFTBARRIER; |
6fca6a61 | 820 | list_del_init(&rq->queuelist); |
aef1897c JW |
821 | /* |
822 | * If RQF_DONTPREP, rq has contained some driver specific | |
823 | * data, so insert it to hctx dispatch list to avoid any | |
824 | * merge. | |
825 | */ | |
826 | if (rq->rq_flags & RQF_DONTPREP) | |
01e99aec | 827 | blk_mq_request_bypass_insert(rq, false, false); |
aef1897c JW |
828 | else |
829 | blk_mq_sched_insert_request(rq, true, false, false); | |
6fca6a61 CH |
830 | } |
831 | ||
832 | while (!list_empty(&rq_list)) { | |
833 | rq = list_entry(rq_list.next, struct request, queuelist); | |
834 | list_del_init(&rq->queuelist); | |
9e97d295 | 835 | blk_mq_sched_insert_request(rq, false, false, false); |
6fca6a61 CH |
836 | } |
837 | ||
52d7f1b5 | 838 | blk_mq_run_hw_queues(q, false); |
6fca6a61 CH |
839 | } |
840 | ||
2b053aca BVA |
841 | void blk_mq_add_to_requeue_list(struct request *rq, bool at_head, |
842 | bool kick_requeue_list) | |
6fca6a61 CH |
843 | { |
844 | struct request_queue *q = rq->q; | |
845 | unsigned long flags; | |
846 | ||
847 | /* | |
848 | * We abuse this flag that is otherwise used by the I/O scheduler to | |
ff821d27 | 849 | * request head insertion from the workqueue. |
6fca6a61 | 850 | */ |
e8064021 | 851 | BUG_ON(rq->rq_flags & RQF_SOFTBARRIER); |
6fca6a61 CH |
852 | |
853 | spin_lock_irqsave(&q->requeue_lock, flags); | |
854 | if (at_head) { | |
e8064021 | 855 | rq->rq_flags |= RQF_SOFTBARRIER; |
6fca6a61 CH |
856 | list_add(&rq->queuelist, &q->requeue_list); |
857 | } else { | |
858 | list_add_tail(&rq->queuelist, &q->requeue_list); | |
859 | } | |
860 | spin_unlock_irqrestore(&q->requeue_lock, flags); | |
2b053aca BVA |
861 | |
862 | if (kick_requeue_list) | |
863 | blk_mq_kick_requeue_list(q); | |
6fca6a61 | 864 | } |
6fca6a61 CH |
865 | |
866 | void blk_mq_kick_requeue_list(struct request_queue *q) | |
867 | { | |
ae943d20 | 868 | kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0); |
6fca6a61 CH |
869 | } |
870 | EXPORT_SYMBOL(blk_mq_kick_requeue_list); | |
871 | ||
2849450a MS |
872 | void blk_mq_delay_kick_requeue_list(struct request_queue *q, |
873 | unsigned long msecs) | |
874 | { | |
d4acf365 BVA |
875 | kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, |
876 | msecs_to_jiffies(msecs)); | |
2849450a MS |
877 | } |
878 | EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list); | |
879 | ||
0e62f51f JA |
880 | struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag) |
881 | { | |
88c7b2b7 JA |
882 | if (tag < tags->nr_tags) { |
883 | prefetch(tags->rqs[tag]); | |
4ee86bab | 884 | return tags->rqs[tag]; |
88c7b2b7 | 885 | } |
4ee86bab HR |
886 | |
887 | return NULL; | |
24d2f903 CH |
888 | } |
889 | EXPORT_SYMBOL(blk_mq_tag_to_rq); | |
890 | ||
3c94d83c JA |
891 | static bool blk_mq_rq_inflight(struct blk_mq_hw_ctx *hctx, struct request *rq, |
892 | void *priv, bool reserved) | |
ae879912 JA |
893 | { |
894 | /* | |
3c94d83c JA |
895 | * If we find a request that is inflight and the queue matches, |
896 | * we know the queue is busy. Return false to stop the iteration. | |
ae879912 | 897 | */ |
3c94d83c | 898 | if (rq->state == MQ_RQ_IN_FLIGHT && rq->q == hctx->queue) { |
ae879912 JA |
899 | bool *busy = priv; |
900 | ||
901 | *busy = true; | |
902 | return false; | |
903 | } | |
904 | ||
905 | return true; | |
906 | } | |
907 | ||
3c94d83c | 908 | bool blk_mq_queue_inflight(struct request_queue *q) |
ae879912 JA |
909 | { |
910 | bool busy = false; | |
911 | ||
3c94d83c | 912 | blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy); |
ae879912 JA |
913 | return busy; |
914 | } | |
3c94d83c | 915 | EXPORT_SYMBOL_GPL(blk_mq_queue_inflight); |
ae879912 | 916 | |
358f70da | 917 | static void blk_mq_rq_timed_out(struct request *req, bool reserved) |
320ae51f | 918 | { |
da661267 | 919 | req->rq_flags |= RQF_TIMED_OUT; |
d1210d5a CH |
920 | if (req->q->mq_ops->timeout) { |
921 | enum blk_eh_timer_return ret; | |
922 | ||
923 | ret = req->q->mq_ops->timeout(req, reserved); | |
924 | if (ret == BLK_EH_DONE) | |
925 | return; | |
926 | WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER); | |
46f92d42 | 927 | } |
d1210d5a CH |
928 | |
929 | blk_add_timer(req); | |
87ee7b11 | 930 | } |
5b3f25fc | 931 | |
12f5b931 | 932 | static bool blk_mq_req_expired(struct request *rq, unsigned long *next) |
81481eb4 | 933 | { |
12f5b931 | 934 | unsigned long deadline; |
87ee7b11 | 935 | |
12f5b931 KB |
936 | if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT) |
937 | return false; | |
da661267 CH |
938 | if (rq->rq_flags & RQF_TIMED_OUT) |
939 | return false; | |
a7af0af3 | 940 | |
079076b3 | 941 | deadline = READ_ONCE(rq->deadline); |
12f5b931 KB |
942 | if (time_after_eq(jiffies, deadline)) |
943 | return true; | |
a7af0af3 | 944 | |
12f5b931 KB |
945 | if (*next == 0) |
946 | *next = deadline; | |
947 | else if (time_after(*next, deadline)) | |
948 | *next = deadline; | |
949 | return false; | |
87ee7b11 JA |
950 | } |
951 | ||
7baa8572 | 952 | static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx, |
1d9bd516 TH |
953 | struct request *rq, void *priv, bool reserved) |
954 | { | |
12f5b931 KB |
955 | unsigned long *next = priv; |
956 | ||
957 | /* | |
958 | * Just do a quick check if it is expired before locking the request in | |
959 | * so we're not unnecessarilly synchronizing across CPUs. | |
960 | */ | |
961 | if (!blk_mq_req_expired(rq, next)) | |
7baa8572 | 962 | return true; |
12f5b931 KB |
963 | |
964 | /* | |
965 | * We have reason to believe the request may be expired. Take a | |
966 | * reference on the request to lock this request lifetime into its | |
967 | * currently allocated context to prevent it from being reallocated in | |
968 | * the event the completion by-passes this timeout handler. | |
969 | * | |
970 | * If the reference was already released, then the driver beat the | |
971 | * timeout handler to posting a natural completion. | |
972 | */ | |
973 | if (!refcount_inc_not_zero(&rq->ref)) | |
7baa8572 | 974 | return true; |
12f5b931 | 975 | |
1d9bd516 | 976 | /* |
12f5b931 KB |
977 | * The request is now locked and cannot be reallocated underneath the |
978 | * timeout handler's processing. Re-verify this exact request is truly | |
979 | * expired; if it is not expired, then the request was completed and | |
980 | * reallocated as a new request. | |
1d9bd516 | 981 | */ |
12f5b931 | 982 | if (blk_mq_req_expired(rq, next)) |
1d9bd516 | 983 | blk_mq_rq_timed_out(rq, reserved); |
8d699663 YY |
984 | |
985 | if (is_flush_rq(rq, hctx)) | |
986 | rq->end_io(rq, 0); | |
987 | else if (refcount_dec_and_test(&rq->ref)) | |
12f5b931 | 988 | __blk_mq_free_request(rq); |
7baa8572 JA |
989 | |
990 | return true; | |
1d9bd516 TH |
991 | } |
992 | ||
287922eb | 993 | static void blk_mq_timeout_work(struct work_struct *work) |
320ae51f | 994 | { |
287922eb CH |
995 | struct request_queue *q = |
996 | container_of(work, struct request_queue, timeout_work); | |
12f5b931 | 997 | unsigned long next = 0; |
1d9bd516 | 998 | struct blk_mq_hw_ctx *hctx; |
81481eb4 | 999 | int i; |
320ae51f | 1000 | |
71f79fb3 GKB |
1001 | /* A deadlock might occur if a request is stuck requiring a |
1002 | * timeout at the same time a queue freeze is waiting | |
1003 | * completion, since the timeout code would not be able to | |
1004 | * acquire the queue reference here. | |
1005 | * | |
1006 | * That's why we don't use blk_queue_enter here; instead, we use | |
1007 | * percpu_ref_tryget directly, because we need to be able to | |
1008 | * obtain a reference even in the short window between the queue | |
1009 | * starting to freeze, by dropping the first reference in | |
1671d522 | 1010 | * blk_freeze_queue_start, and the moment the last request is |
71f79fb3 GKB |
1011 | * consumed, marked by the instant q_usage_counter reaches |
1012 | * zero. | |
1013 | */ | |
1014 | if (!percpu_ref_tryget(&q->q_usage_counter)) | |
287922eb CH |
1015 | return; |
1016 | ||
12f5b931 | 1017 | blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next); |
320ae51f | 1018 | |
12f5b931 KB |
1019 | if (next != 0) { |
1020 | mod_timer(&q->timeout, next); | |
0d2602ca | 1021 | } else { |
fcd36c36 BVA |
1022 | /* |
1023 | * Request timeouts are handled as a forward rolling timer. If | |
1024 | * we end up here it means that no requests are pending and | |
1025 | * also that no request has been pending for a while. Mark | |
1026 | * each hctx as idle. | |
1027 | */ | |
f054b56c ML |
1028 | queue_for_each_hw_ctx(q, hctx, i) { |
1029 | /* the hctx may be unmapped, so check it here */ | |
1030 | if (blk_mq_hw_queue_mapped(hctx)) | |
1031 | blk_mq_tag_idle(hctx); | |
1032 | } | |
0d2602ca | 1033 | } |
287922eb | 1034 | blk_queue_exit(q); |
320ae51f JA |
1035 | } |
1036 | ||
88459642 OS |
1037 | struct flush_busy_ctx_data { |
1038 | struct blk_mq_hw_ctx *hctx; | |
1039 | struct list_head *list; | |
1040 | }; | |
1041 | ||
1042 | static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data) | |
1043 | { | |
1044 | struct flush_busy_ctx_data *flush_data = data; | |
1045 | struct blk_mq_hw_ctx *hctx = flush_data->hctx; | |
1046 | struct blk_mq_ctx *ctx = hctx->ctxs[bitnr]; | |
c16d6b5a | 1047 | enum hctx_type type = hctx->type; |
88459642 | 1048 | |
88459642 | 1049 | spin_lock(&ctx->lock); |
c16d6b5a | 1050 | list_splice_tail_init(&ctx->rq_lists[type], flush_data->list); |
e9a99a63 | 1051 | sbitmap_clear_bit(sb, bitnr); |
88459642 OS |
1052 | spin_unlock(&ctx->lock); |
1053 | return true; | |
1054 | } | |
1055 | ||
1429d7c9 JA |
1056 | /* |
1057 | * Process software queues that have been marked busy, splicing them | |
1058 | * to the for-dispatch | |
1059 | */ | |
2c3ad667 | 1060 | void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list) |
1429d7c9 | 1061 | { |
88459642 OS |
1062 | struct flush_busy_ctx_data data = { |
1063 | .hctx = hctx, | |
1064 | .list = list, | |
1065 | }; | |
1429d7c9 | 1066 | |
88459642 | 1067 | sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data); |
1429d7c9 | 1068 | } |
2c3ad667 | 1069 | EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs); |
1429d7c9 | 1070 | |
b347689f ML |
1071 | struct dispatch_rq_data { |
1072 | struct blk_mq_hw_ctx *hctx; | |
1073 | struct request *rq; | |
1074 | }; | |
1075 | ||
1076 | static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr, | |
1077 | void *data) | |
1078 | { | |
1079 | struct dispatch_rq_data *dispatch_data = data; | |
1080 | struct blk_mq_hw_ctx *hctx = dispatch_data->hctx; | |
1081 | struct blk_mq_ctx *ctx = hctx->ctxs[bitnr]; | |
c16d6b5a | 1082 | enum hctx_type type = hctx->type; |
b347689f ML |
1083 | |
1084 | spin_lock(&ctx->lock); | |
c16d6b5a ML |
1085 | if (!list_empty(&ctx->rq_lists[type])) { |
1086 | dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next); | |
b347689f | 1087 | list_del_init(&dispatch_data->rq->queuelist); |
c16d6b5a | 1088 | if (list_empty(&ctx->rq_lists[type])) |
b347689f ML |
1089 | sbitmap_clear_bit(sb, bitnr); |
1090 | } | |
1091 | spin_unlock(&ctx->lock); | |
1092 | ||
1093 | return !dispatch_data->rq; | |
1094 | } | |
1095 | ||
1096 | struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx, | |
1097 | struct blk_mq_ctx *start) | |
1098 | { | |
f31967f0 | 1099 | unsigned off = start ? start->index_hw[hctx->type] : 0; |
b347689f ML |
1100 | struct dispatch_rq_data data = { |
1101 | .hctx = hctx, | |
1102 | .rq = NULL, | |
1103 | }; | |
1104 | ||
1105 | __sbitmap_for_each_set(&hctx->ctx_map, off, | |
1106 | dispatch_rq_from_ctx, &data); | |
1107 | ||
1108 | return data.rq; | |
1109 | } | |
1110 | ||
703fd1c0 JA |
1111 | static inline unsigned int queued_to_index(unsigned int queued) |
1112 | { | |
1113 | if (!queued) | |
1114 | return 0; | |
1429d7c9 | 1115 | |
703fd1c0 | 1116 | return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1); |
1429d7c9 JA |
1117 | } |
1118 | ||
eb619fdb JA |
1119 | static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode, |
1120 | int flags, void *key) | |
da55f2cc OS |
1121 | { |
1122 | struct blk_mq_hw_ctx *hctx; | |
1123 | ||
1124 | hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait); | |
1125 | ||
5815839b | 1126 | spin_lock(&hctx->dispatch_wait_lock); |
e8618575 JA |
1127 | if (!list_empty(&wait->entry)) { |
1128 | struct sbitmap_queue *sbq; | |
1129 | ||
1130 | list_del_init(&wait->entry); | |
1131 | sbq = &hctx->tags->bitmap_tags; | |
1132 | atomic_dec(&sbq->ws_active); | |
1133 | } | |
5815839b ML |
1134 | spin_unlock(&hctx->dispatch_wait_lock); |
1135 | ||
da55f2cc OS |
1136 | blk_mq_run_hw_queue(hctx, true); |
1137 | return 1; | |
1138 | } | |
1139 | ||
f906a6a0 JA |
1140 | /* |
1141 | * Mark us waiting for a tag. For shared tags, this involves hooking us into | |
ee3e4de5 BVA |
1142 | * the tag wakeups. For non-shared tags, we can simply mark us needing a |
1143 | * restart. For both cases, take care to check the condition again after | |
f906a6a0 JA |
1144 | * marking us as waiting. |
1145 | */ | |
2278d69f | 1146 | static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx, |
f906a6a0 | 1147 | struct request *rq) |
da55f2cc | 1148 | { |
e8618575 | 1149 | struct sbitmap_queue *sbq = &hctx->tags->bitmap_tags; |
5815839b | 1150 | struct wait_queue_head *wq; |
f906a6a0 JA |
1151 | wait_queue_entry_t *wait; |
1152 | bool ret; | |
da55f2cc | 1153 | |
2278d69f | 1154 | if (!(hctx->flags & BLK_MQ_F_TAG_SHARED)) { |
684b7324 | 1155 | blk_mq_sched_mark_restart_hctx(hctx); |
f906a6a0 | 1156 | |
c27d53fb BVA |
1157 | /* |
1158 | * It's possible that a tag was freed in the window between the | |
1159 | * allocation failure and adding the hardware queue to the wait | |
1160 | * queue. | |
1161 | * | |
1162 | * Don't clear RESTART here, someone else could have set it. | |
1163 | * At most this will cost an extra queue run. | |
1164 | */ | |
8ab6bb9e | 1165 | return blk_mq_get_driver_tag(rq); |
eb619fdb | 1166 | } |
eb619fdb | 1167 | |
2278d69f | 1168 | wait = &hctx->dispatch_wait; |
c27d53fb BVA |
1169 | if (!list_empty_careful(&wait->entry)) |
1170 | return false; | |
1171 | ||
e8618575 | 1172 | wq = &bt_wait_ptr(sbq, hctx)->wait; |
5815839b ML |
1173 | |
1174 | spin_lock_irq(&wq->lock); | |
1175 | spin_lock(&hctx->dispatch_wait_lock); | |
c27d53fb | 1176 | if (!list_empty(&wait->entry)) { |
5815839b ML |
1177 | spin_unlock(&hctx->dispatch_wait_lock); |
1178 | spin_unlock_irq(&wq->lock); | |
c27d53fb | 1179 | return false; |
eb619fdb JA |
1180 | } |
1181 | ||
e8618575 | 1182 | atomic_inc(&sbq->ws_active); |
5815839b ML |
1183 | wait->flags &= ~WQ_FLAG_EXCLUSIVE; |
1184 | __add_wait_queue(wq, wait); | |
c27d53fb | 1185 | |
da55f2cc | 1186 | /* |
eb619fdb JA |
1187 | * It's possible that a tag was freed in the window between the |
1188 | * allocation failure and adding the hardware queue to the wait | |
1189 | * queue. | |
da55f2cc | 1190 | */ |
8ab6bb9e | 1191 | ret = blk_mq_get_driver_tag(rq); |
c27d53fb | 1192 | if (!ret) { |
5815839b ML |
1193 | spin_unlock(&hctx->dispatch_wait_lock); |
1194 | spin_unlock_irq(&wq->lock); | |
c27d53fb | 1195 | return false; |
eb619fdb | 1196 | } |
c27d53fb BVA |
1197 | |
1198 | /* | |
1199 | * We got a tag, remove ourselves from the wait queue to ensure | |
1200 | * someone else gets the wakeup. | |
1201 | */ | |
c27d53fb | 1202 | list_del_init(&wait->entry); |
e8618575 | 1203 | atomic_dec(&sbq->ws_active); |
5815839b ML |
1204 | spin_unlock(&hctx->dispatch_wait_lock); |
1205 | spin_unlock_irq(&wq->lock); | |
c27d53fb BVA |
1206 | |
1207 | return true; | |
da55f2cc OS |
1208 | } |
1209 | ||
6e768717 ML |
1210 | #define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8 |
1211 | #define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4 | |
1212 | /* | |
1213 | * Update dispatch busy with the Exponential Weighted Moving Average(EWMA): | |
1214 | * - EWMA is one simple way to compute running average value | |
1215 | * - weight(7/8 and 1/8) is applied so that it can decrease exponentially | |
1216 | * - take 4 as factor for avoiding to get too small(0) result, and this | |
1217 | * factor doesn't matter because EWMA decreases exponentially | |
1218 | */ | |
1219 | static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy) | |
1220 | { | |
1221 | unsigned int ewma; | |
1222 | ||
1223 | if (hctx->queue->elevator) | |
1224 | return; | |
1225 | ||
1226 | ewma = hctx->dispatch_busy; | |
1227 | ||
1228 | if (!ewma && !busy) | |
1229 | return; | |
1230 | ||
1231 | ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1; | |
1232 | if (busy) | |
1233 | ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR; | |
1234 | ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT; | |
1235 | ||
1236 | hctx->dispatch_busy = ewma; | |
1237 | } | |
1238 | ||
86ff7c2a ML |
1239 | #define BLK_MQ_RESOURCE_DELAY 3 /* ms units */ |
1240 | ||
c92a4103 JT |
1241 | static void blk_mq_handle_dev_resource(struct request *rq, |
1242 | struct list_head *list) | |
1243 | { | |
1244 | struct request *next = | |
1245 | list_first_entry_or_null(list, struct request, queuelist); | |
1246 | ||
1247 | /* | |
1248 | * If an I/O scheduler has been configured and we got a driver tag for | |
1249 | * the next request already, free it. | |
1250 | */ | |
1251 | if (next) | |
1252 | blk_mq_put_driver_tag(next); | |
1253 | ||
1254 | list_add(&rq->queuelist, list); | |
1255 | __blk_mq_requeue_request(rq); | |
1256 | } | |
1257 | ||
0512a75b KB |
1258 | static void blk_mq_handle_zone_resource(struct request *rq, |
1259 | struct list_head *zone_list) | |
1260 | { | |
1261 | /* | |
1262 | * If we end up here it is because we cannot dispatch a request to a | |
1263 | * specific zone due to LLD level zone-write locking or other zone | |
1264 | * related resource not being available. In this case, set the request | |
1265 | * aside in zone_list for retrying it later. | |
1266 | */ | |
1267 | list_add(&rq->queuelist, zone_list); | |
1268 | __blk_mq_requeue_request(rq); | |
1269 | } | |
1270 | ||
1f57f8d4 JA |
1271 | /* |
1272 | * Returns true if we did some work AND can potentially do more. | |
1273 | */ | |
de148297 | 1274 | bool blk_mq_dispatch_rq_list(struct request_queue *q, struct list_head *list, |
eb619fdb | 1275 | bool got_budget) |
320ae51f | 1276 | { |
81380ca1 | 1277 | struct blk_mq_hw_ctx *hctx; |
6d6f167c | 1278 | struct request *rq, *nxt; |
eb619fdb | 1279 | bool no_tag = false; |
fc17b653 | 1280 | int errors, queued; |
86ff7c2a | 1281 | blk_status_t ret = BLK_STS_OK; |
ab3cee37 | 1282 | bool no_budget_avail = false; |
0512a75b | 1283 | LIST_HEAD(zone_list); |
320ae51f | 1284 | |
81380ca1 OS |
1285 | if (list_empty(list)) |
1286 | return false; | |
1287 | ||
de148297 ML |
1288 | WARN_ON(!list_is_singular(list) && got_budget); |
1289 | ||
320ae51f JA |
1290 | /* |
1291 | * Now process all the entries, sending them to the driver. | |
1292 | */ | |
93efe981 | 1293 | errors = queued = 0; |
81380ca1 | 1294 | do { |
74c45052 | 1295 | struct blk_mq_queue_data bd; |
320ae51f | 1296 | |
f04c3df3 | 1297 | rq = list_first_entry(list, struct request, queuelist); |
0bca799b | 1298 | |
ea4f995e | 1299 | hctx = rq->mq_hctx; |
5fe56de7 JG |
1300 | if (!got_budget && !blk_mq_get_dispatch_budget(hctx)) { |
1301 | blk_mq_put_driver_tag(rq); | |
ab3cee37 | 1302 | no_budget_avail = true; |
0bca799b | 1303 | break; |
5fe56de7 | 1304 | } |
0bca799b | 1305 | |
8ab6bb9e | 1306 | if (!blk_mq_get_driver_tag(rq)) { |
3c782d67 | 1307 | /* |
da55f2cc | 1308 | * The initial allocation attempt failed, so we need to |
eb619fdb JA |
1309 | * rerun the hardware queue when a tag is freed. The |
1310 | * waitqueue takes care of that. If the queue is run | |
1311 | * before we add this entry back on the dispatch list, | |
1312 | * we'll re-run it below. | |
3c782d67 | 1313 | */ |
2278d69f | 1314 | if (!blk_mq_mark_tag_wait(hctx, rq)) { |
0bca799b | 1315 | blk_mq_put_dispatch_budget(hctx); |
f906a6a0 JA |
1316 | /* |
1317 | * For non-shared tags, the RESTART check | |
1318 | * will suffice. | |
1319 | */ | |
1320 | if (hctx->flags & BLK_MQ_F_TAG_SHARED) | |
1321 | no_tag = true; | |
de148297 ML |
1322 | break; |
1323 | } | |
1324 | } | |
1325 | ||
320ae51f | 1326 | list_del_init(&rq->queuelist); |
320ae51f | 1327 | |
74c45052 | 1328 | bd.rq = rq; |
113285b4 JA |
1329 | |
1330 | /* | |
1331 | * Flag last if we have no more requests, or if we have more | |
1332 | * but can't assign a driver tag to it. | |
1333 | */ | |
1334 | if (list_empty(list)) | |
1335 | bd.last = true; | |
1336 | else { | |
113285b4 | 1337 | nxt = list_first_entry(list, struct request, queuelist); |
8ab6bb9e | 1338 | bd.last = !blk_mq_get_driver_tag(nxt); |
113285b4 | 1339 | } |
74c45052 JA |
1340 | |
1341 | ret = q->mq_ops->queue_rq(hctx, &bd); | |
86ff7c2a | 1342 | if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE) { |
c92a4103 | 1343 | blk_mq_handle_dev_resource(rq, list); |
320ae51f | 1344 | break; |
0512a75b KB |
1345 | } else if (ret == BLK_STS_ZONE_RESOURCE) { |
1346 | /* | |
1347 | * Move the request to zone_list and keep going through | |
1348 | * the dispatch list to find more requests the drive can | |
1349 | * accept. | |
1350 | */ | |
1351 | blk_mq_handle_zone_resource(rq, &zone_list); | |
1352 | if (list_empty(list)) | |
1353 | break; | |
1354 | continue; | |
fc17b653 CH |
1355 | } |
1356 | ||
1357 | if (unlikely(ret != BLK_STS_OK)) { | |
93efe981 | 1358 | errors++; |
2a842aca | 1359 | blk_mq_end_request(rq, BLK_STS_IOERR); |
fc17b653 | 1360 | continue; |
320ae51f JA |
1361 | } |
1362 | ||
fc17b653 | 1363 | queued++; |
81380ca1 | 1364 | } while (!list_empty(list)); |
320ae51f | 1365 | |
0512a75b KB |
1366 | if (!list_empty(&zone_list)) |
1367 | list_splice_tail_init(&zone_list, list); | |
1368 | ||
703fd1c0 | 1369 | hctx->dispatched[queued_to_index(queued)]++; |
320ae51f JA |
1370 | |
1371 | /* | |
1372 | * Any items that need requeuing? Stuff them into hctx->dispatch, | |
1373 | * that is where we will continue on next queue run. | |
1374 | */ | |
f04c3df3 | 1375 | if (!list_empty(list)) { |
86ff7c2a ML |
1376 | bool needs_restart; |
1377 | ||
d666ba98 JA |
1378 | /* |
1379 | * If we didn't flush the entire list, we could have told | |
1380 | * the driver there was more coming, but that turned out to | |
1381 | * be a lie. | |
1382 | */ | |
536167d4 | 1383 | if (q->mq_ops->commit_rqs && queued) |
d666ba98 JA |
1384 | q->mq_ops->commit_rqs(hctx); |
1385 | ||
320ae51f | 1386 | spin_lock(&hctx->lock); |
01e99aec | 1387 | list_splice_tail_init(list, &hctx->dispatch); |
320ae51f | 1388 | spin_unlock(&hctx->lock); |
f04c3df3 | 1389 | |
9ba52e58 | 1390 | /* |
710c785f BVA |
1391 | * If SCHED_RESTART was set by the caller of this function and |
1392 | * it is no longer set that means that it was cleared by another | |
1393 | * thread and hence that a queue rerun is needed. | |
9ba52e58 | 1394 | * |
eb619fdb JA |
1395 | * If 'no_tag' is set, that means that we failed getting |
1396 | * a driver tag with an I/O scheduler attached. If our dispatch | |
1397 | * waitqueue is no longer active, ensure that we run the queue | |
1398 | * AFTER adding our entries back to the list. | |
bd166ef1 | 1399 | * |
710c785f BVA |
1400 | * If no I/O scheduler has been configured it is possible that |
1401 | * the hardware queue got stopped and restarted before requests | |
1402 | * were pushed back onto the dispatch list. Rerun the queue to | |
1403 | * avoid starvation. Notes: | |
1404 | * - blk_mq_run_hw_queue() checks whether or not a queue has | |
1405 | * been stopped before rerunning a queue. | |
1406 | * - Some but not all block drivers stop a queue before | |
fc17b653 | 1407 | * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq |
710c785f | 1408 | * and dm-rq. |
86ff7c2a ML |
1409 | * |
1410 | * If driver returns BLK_STS_RESOURCE and SCHED_RESTART | |
1411 | * bit is set, run queue after a delay to avoid IO stalls | |
ab3cee37 DA |
1412 | * that could otherwise occur if the queue is idle. We'll do |
1413 | * similar if we couldn't get budget and SCHED_RESTART is set. | |
bd166ef1 | 1414 | */ |
86ff7c2a ML |
1415 | needs_restart = blk_mq_sched_needs_restart(hctx); |
1416 | if (!needs_restart || | |
eb619fdb | 1417 | (no_tag && list_empty_careful(&hctx->dispatch_wait.entry))) |
bd166ef1 | 1418 | blk_mq_run_hw_queue(hctx, true); |
ab3cee37 DA |
1419 | else if (needs_restart && (ret == BLK_STS_RESOURCE || |
1420 | no_budget_avail)) | |
86ff7c2a | 1421 | blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY); |
1f57f8d4 | 1422 | |
6e768717 | 1423 | blk_mq_update_dispatch_busy(hctx, true); |
1f57f8d4 | 1424 | return false; |
6e768717 ML |
1425 | } else |
1426 | blk_mq_update_dispatch_busy(hctx, false); | |
f04c3df3 | 1427 | |
1f57f8d4 JA |
1428 | /* |
1429 | * If the host/device is unable to accept more work, inform the | |
1430 | * caller of that. | |
1431 | */ | |
1432 | if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE) | |
1433 | return false; | |
1434 | ||
93efe981 | 1435 | return (queued + errors) != 0; |
f04c3df3 JA |
1436 | } |
1437 | ||
105663f7 AA |
1438 | /** |
1439 | * __blk_mq_run_hw_queue - Run a hardware queue. | |
1440 | * @hctx: Pointer to the hardware queue to run. | |
1441 | * | |
1442 | * Send pending requests to the hardware. | |
1443 | */ | |
6a83e74d BVA |
1444 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) |
1445 | { | |
1446 | int srcu_idx; | |
1447 | ||
b7a71e66 JA |
1448 | /* |
1449 | * We should be running this queue from one of the CPUs that | |
1450 | * are mapped to it. | |
7df938fb ML |
1451 | * |
1452 | * There are at least two related races now between setting | |
1453 | * hctx->next_cpu from blk_mq_hctx_next_cpu() and running | |
1454 | * __blk_mq_run_hw_queue(): | |
1455 | * | |
1456 | * - hctx->next_cpu is found offline in blk_mq_hctx_next_cpu(), | |
1457 | * but later it becomes online, then this warning is harmless | |
1458 | * at all | |
1459 | * | |
1460 | * - hctx->next_cpu is found online in blk_mq_hctx_next_cpu(), | |
1461 | * but later it becomes offline, then the warning can't be | |
1462 | * triggered, and we depend on blk-mq timeout handler to | |
1463 | * handle dispatched requests to this hctx | |
b7a71e66 | 1464 | */ |
7df938fb ML |
1465 | if (!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) && |
1466 | cpu_online(hctx->next_cpu)) { | |
1467 | printk(KERN_WARNING "run queue from wrong CPU %d, hctx %s\n", | |
1468 | raw_smp_processor_id(), | |
1469 | cpumask_empty(hctx->cpumask) ? "inactive": "active"); | |
1470 | dump_stack(); | |
1471 | } | |
6a83e74d | 1472 | |
b7a71e66 JA |
1473 | /* |
1474 | * We can't run the queue inline with ints disabled. Ensure that | |
1475 | * we catch bad users of this early. | |
1476 | */ | |
1477 | WARN_ON_ONCE(in_interrupt()); | |
1478 | ||
04ced159 | 1479 | might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING); |
bf4907c0 | 1480 | |
04ced159 JA |
1481 | hctx_lock(hctx, &srcu_idx); |
1482 | blk_mq_sched_dispatch_requests(hctx); | |
1483 | hctx_unlock(hctx, srcu_idx); | |
6a83e74d BVA |
1484 | } |
1485 | ||
f82ddf19 ML |
1486 | static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx) |
1487 | { | |
1488 | int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask); | |
1489 | ||
1490 | if (cpu >= nr_cpu_ids) | |
1491 | cpu = cpumask_first(hctx->cpumask); | |
1492 | return cpu; | |
1493 | } | |
1494 | ||
506e931f JA |
1495 | /* |
1496 | * It'd be great if the workqueue API had a way to pass | |
1497 | * in a mask and had some smarts for more clever placement. | |
1498 | * For now we just round-robin here, switching for every | |
1499 | * BLK_MQ_CPU_WORK_BATCH queued items. | |
1500 | */ | |
1501 | static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) | |
1502 | { | |
7bed4595 | 1503 | bool tried = false; |
476f8c98 | 1504 | int next_cpu = hctx->next_cpu; |
7bed4595 | 1505 | |
b657d7e6 CH |
1506 | if (hctx->queue->nr_hw_queues == 1) |
1507 | return WORK_CPU_UNBOUND; | |
506e931f JA |
1508 | |
1509 | if (--hctx->next_cpu_batch <= 0) { | |
7bed4595 | 1510 | select_cpu: |
476f8c98 | 1511 | next_cpu = cpumask_next_and(next_cpu, hctx->cpumask, |
20e4d813 | 1512 | cpu_online_mask); |
506e931f | 1513 | if (next_cpu >= nr_cpu_ids) |
f82ddf19 | 1514 | next_cpu = blk_mq_first_mapped_cpu(hctx); |
506e931f JA |
1515 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; |
1516 | } | |
1517 | ||
7bed4595 ML |
1518 | /* |
1519 | * Do unbound schedule if we can't find a online CPU for this hctx, | |
1520 | * and it should only happen in the path of handling CPU DEAD. | |
1521 | */ | |
476f8c98 | 1522 | if (!cpu_online(next_cpu)) { |
7bed4595 ML |
1523 | if (!tried) { |
1524 | tried = true; | |
1525 | goto select_cpu; | |
1526 | } | |
1527 | ||
1528 | /* | |
1529 | * Make sure to re-select CPU next time once after CPUs | |
1530 | * in hctx->cpumask become online again. | |
1531 | */ | |
476f8c98 | 1532 | hctx->next_cpu = next_cpu; |
7bed4595 ML |
1533 | hctx->next_cpu_batch = 1; |
1534 | return WORK_CPU_UNBOUND; | |
1535 | } | |
476f8c98 ML |
1536 | |
1537 | hctx->next_cpu = next_cpu; | |
1538 | return next_cpu; | |
506e931f JA |
1539 | } |
1540 | ||
105663f7 AA |
1541 | /** |
1542 | * __blk_mq_delay_run_hw_queue - Run (or schedule to run) a hardware queue. | |
1543 | * @hctx: Pointer to the hardware queue to run. | |
1544 | * @async: If we want to run the queue asynchronously. | |
1545 | * @msecs: Microseconds of delay to wait before running the queue. | |
1546 | * | |
1547 | * If !@async, try to run the queue now. Else, run the queue asynchronously and | |
1548 | * with a delay of @msecs. | |
1549 | */ | |
7587a5ae BVA |
1550 | static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async, |
1551 | unsigned long msecs) | |
320ae51f | 1552 | { |
5435c023 | 1553 | if (unlikely(blk_mq_hctx_stopped(hctx))) |
320ae51f JA |
1554 | return; |
1555 | ||
1b792f2f | 1556 | if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) { |
2a90d4aa PB |
1557 | int cpu = get_cpu(); |
1558 | if (cpumask_test_cpu(cpu, hctx->cpumask)) { | |
398205b8 | 1559 | __blk_mq_run_hw_queue(hctx); |
2a90d4aa | 1560 | put_cpu(); |
398205b8 PB |
1561 | return; |
1562 | } | |
e4043dcf | 1563 | |
2a90d4aa | 1564 | put_cpu(); |
e4043dcf | 1565 | } |
398205b8 | 1566 | |
ae943d20 BVA |
1567 | kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work, |
1568 | msecs_to_jiffies(msecs)); | |
7587a5ae BVA |
1569 | } |
1570 | ||
105663f7 AA |
1571 | /** |
1572 | * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously. | |
1573 | * @hctx: Pointer to the hardware queue to run. | |
1574 | * @msecs: Microseconds of delay to wait before running the queue. | |
1575 | * | |
1576 | * Run a hardware queue asynchronously with a delay of @msecs. | |
1577 | */ | |
7587a5ae BVA |
1578 | void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs) |
1579 | { | |
1580 | __blk_mq_delay_run_hw_queue(hctx, true, msecs); | |
1581 | } | |
1582 | EXPORT_SYMBOL(blk_mq_delay_run_hw_queue); | |
1583 | ||
105663f7 AA |
1584 | /** |
1585 | * blk_mq_run_hw_queue - Start to run a hardware queue. | |
1586 | * @hctx: Pointer to the hardware queue to run. | |
1587 | * @async: If we want to run the queue asynchronously. | |
1588 | * | |
1589 | * Check if the request queue is not in a quiesced state and if there are | |
1590 | * pending requests to be sent. If this is true, run the queue to send requests | |
1591 | * to hardware. | |
1592 | */ | |
626fb735 | 1593 | void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) |
7587a5ae | 1594 | { |
24f5a90f ML |
1595 | int srcu_idx; |
1596 | bool need_run; | |
1597 | ||
1598 | /* | |
1599 | * When queue is quiesced, we may be switching io scheduler, or | |
1600 | * updating nr_hw_queues, or other things, and we can't run queue | |
1601 | * any more, even __blk_mq_hctx_has_pending() can't be called safely. | |
1602 | * | |
1603 | * And queue will be rerun in blk_mq_unquiesce_queue() if it is | |
1604 | * quiesced. | |
1605 | */ | |
04ced159 JA |
1606 | hctx_lock(hctx, &srcu_idx); |
1607 | need_run = !blk_queue_quiesced(hctx->queue) && | |
1608 | blk_mq_hctx_has_pending(hctx); | |
1609 | hctx_unlock(hctx, srcu_idx); | |
24f5a90f | 1610 | |
626fb735 | 1611 | if (need_run) |
79f720a7 | 1612 | __blk_mq_delay_run_hw_queue(hctx, async, 0); |
320ae51f | 1613 | } |
5b727272 | 1614 | EXPORT_SYMBOL(blk_mq_run_hw_queue); |
320ae51f | 1615 | |
105663f7 AA |
1616 | /** |
1617 | * blk_mq_run_hw_queue - Run all hardware queues in a request queue. | |
1618 | * @q: Pointer to the request queue to run. | |
1619 | * @async: If we want to run the queue asynchronously. | |
1620 | */ | |
b94ec296 | 1621 | void blk_mq_run_hw_queues(struct request_queue *q, bool async) |
320ae51f JA |
1622 | { |
1623 | struct blk_mq_hw_ctx *hctx; | |
1624 | int i; | |
1625 | ||
1626 | queue_for_each_hw_ctx(q, hctx, i) { | |
79f720a7 | 1627 | if (blk_mq_hctx_stopped(hctx)) |
320ae51f JA |
1628 | continue; |
1629 | ||
b94ec296 | 1630 | blk_mq_run_hw_queue(hctx, async); |
320ae51f JA |
1631 | } |
1632 | } | |
b94ec296 | 1633 | EXPORT_SYMBOL(blk_mq_run_hw_queues); |
320ae51f | 1634 | |
b9151e7b DA |
1635 | /** |
1636 | * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously. | |
1637 | * @q: Pointer to the request queue to run. | |
1638 | * @msecs: Microseconds of delay to wait before running the queues. | |
1639 | */ | |
1640 | void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs) | |
1641 | { | |
1642 | struct blk_mq_hw_ctx *hctx; | |
1643 | int i; | |
1644 | ||
1645 | queue_for_each_hw_ctx(q, hctx, i) { | |
1646 | if (blk_mq_hctx_stopped(hctx)) | |
1647 | continue; | |
1648 | ||
1649 | blk_mq_delay_run_hw_queue(hctx, msecs); | |
1650 | } | |
1651 | } | |
1652 | EXPORT_SYMBOL(blk_mq_delay_run_hw_queues); | |
1653 | ||
fd001443 BVA |
1654 | /** |
1655 | * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped | |
1656 | * @q: request queue. | |
1657 | * | |
1658 | * The caller is responsible for serializing this function against | |
1659 | * blk_mq_{start,stop}_hw_queue(). | |
1660 | */ | |
1661 | bool blk_mq_queue_stopped(struct request_queue *q) | |
1662 | { | |
1663 | struct blk_mq_hw_ctx *hctx; | |
1664 | int i; | |
1665 | ||
1666 | queue_for_each_hw_ctx(q, hctx, i) | |
1667 | if (blk_mq_hctx_stopped(hctx)) | |
1668 | return true; | |
1669 | ||
1670 | return false; | |
1671 | } | |
1672 | EXPORT_SYMBOL(blk_mq_queue_stopped); | |
1673 | ||
39a70c76 ML |
1674 | /* |
1675 | * This function is often used for pausing .queue_rq() by driver when | |
1676 | * there isn't enough resource or some conditions aren't satisfied, and | |
4d606219 | 1677 | * BLK_STS_RESOURCE is usually returned. |
39a70c76 ML |
1678 | * |
1679 | * We do not guarantee that dispatch can be drained or blocked | |
1680 | * after blk_mq_stop_hw_queue() returns. Please use | |
1681 | * blk_mq_quiesce_queue() for that requirement. | |
1682 | */ | |
2719aa21 JA |
1683 | void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx) |
1684 | { | |
641a9ed6 | 1685 | cancel_delayed_work(&hctx->run_work); |
280d45f6 | 1686 | |
641a9ed6 | 1687 | set_bit(BLK_MQ_S_STOPPED, &hctx->state); |
2719aa21 | 1688 | } |
641a9ed6 | 1689 | EXPORT_SYMBOL(blk_mq_stop_hw_queue); |
2719aa21 | 1690 | |
39a70c76 ML |
1691 | /* |
1692 | * This function is often used for pausing .queue_rq() by driver when | |
1693 | * there isn't enough resource or some conditions aren't satisfied, and | |
4d606219 | 1694 | * BLK_STS_RESOURCE is usually returned. |
39a70c76 ML |
1695 | * |
1696 | * We do not guarantee that dispatch can be drained or blocked | |
1697 | * after blk_mq_stop_hw_queues() returns. Please use | |
1698 | * blk_mq_quiesce_queue() for that requirement. | |
1699 | */ | |
2719aa21 JA |
1700 | void blk_mq_stop_hw_queues(struct request_queue *q) |
1701 | { | |
641a9ed6 ML |
1702 | struct blk_mq_hw_ctx *hctx; |
1703 | int i; | |
1704 | ||
1705 | queue_for_each_hw_ctx(q, hctx, i) | |
1706 | blk_mq_stop_hw_queue(hctx); | |
280d45f6 CH |
1707 | } |
1708 | EXPORT_SYMBOL(blk_mq_stop_hw_queues); | |
1709 | ||
320ae51f JA |
1710 | void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx) |
1711 | { | |
1712 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
e4043dcf | 1713 | |
0ffbce80 | 1714 | blk_mq_run_hw_queue(hctx, false); |
320ae51f JA |
1715 | } |
1716 | EXPORT_SYMBOL(blk_mq_start_hw_queue); | |
1717 | ||
2f268556 CH |
1718 | void blk_mq_start_hw_queues(struct request_queue *q) |
1719 | { | |
1720 | struct blk_mq_hw_ctx *hctx; | |
1721 | int i; | |
1722 | ||
1723 | queue_for_each_hw_ctx(q, hctx, i) | |
1724 | blk_mq_start_hw_queue(hctx); | |
1725 | } | |
1726 | EXPORT_SYMBOL(blk_mq_start_hw_queues); | |
1727 | ||
ae911c5e JA |
1728 | void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) |
1729 | { | |
1730 | if (!blk_mq_hctx_stopped(hctx)) | |
1731 | return; | |
1732 | ||
1733 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
1734 | blk_mq_run_hw_queue(hctx, async); | |
1735 | } | |
1736 | EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue); | |
1737 | ||
1b4a3258 | 1738 | void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async) |
320ae51f JA |
1739 | { |
1740 | struct blk_mq_hw_ctx *hctx; | |
1741 | int i; | |
1742 | ||
ae911c5e JA |
1743 | queue_for_each_hw_ctx(q, hctx, i) |
1744 | blk_mq_start_stopped_hw_queue(hctx, async); | |
320ae51f JA |
1745 | } |
1746 | EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues); | |
1747 | ||
70f4db63 | 1748 | static void blk_mq_run_work_fn(struct work_struct *work) |
320ae51f JA |
1749 | { |
1750 | struct blk_mq_hw_ctx *hctx; | |
1751 | ||
9f993737 | 1752 | hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work); |
320ae51f | 1753 | |
21c6e939 | 1754 | /* |
15fe8a90 | 1755 | * If we are stopped, don't run the queue. |
21c6e939 | 1756 | */ |
15fe8a90 | 1757 | if (test_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
0196d6b4 | 1758 | return; |
7587a5ae BVA |
1759 | |
1760 | __blk_mq_run_hw_queue(hctx); | |
1761 | } | |
1762 | ||
cfd0c552 | 1763 | static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx, |
cfd0c552 ML |
1764 | struct request *rq, |
1765 | bool at_head) | |
320ae51f | 1766 | { |
e57690fe | 1767 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
c16d6b5a | 1768 | enum hctx_type type = hctx->type; |
e57690fe | 1769 | |
7b607814 BVA |
1770 | lockdep_assert_held(&ctx->lock); |
1771 | ||
01b983c9 JA |
1772 | trace_block_rq_insert(hctx->queue, rq); |
1773 | ||
72a0a36e | 1774 | if (at_head) |
c16d6b5a | 1775 | list_add(&rq->queuelist, &ctx->rq_lists[type]); |
72a0a36e | 1776 | else |
c16d6b5a | 1777 | list_add_tail(&rq->queuelist, &ctx->rq_lists[type]); |
cfd0c552 | 1778 | } |
4bb659b1 | 1779 | |
2c3ad667 JA |
1780 | void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq, |
1781 | bool at_head) | |
cfd0c552 ML |
1782 | { |
1783 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
1784 | ||
7b607814 BVA |
1785 | lockdep_assert_held(&ctx->lock); |
1786 | ||
e57690fe | 1787 | __blk_mq_insert_req_list(hctx, rq, at_head); |
320ae51f | 1788 | blk_mq_hctx_mark_pending(hctx, ctx); |
320ae51f JA |
1789 | } |
1790 | ||
105663f7 AA |
1791 | /** |
1792 | * blk_mq_request_bypass_insert - Insert a request at dispatch list. | |
1793 | * @rq: Pointer to request to be inserted. | |
1794 | * @run_queue: If we should run the hardware queue after inserting the request. | |
1795 | * | |
157f377b JA |
1796 | * Should only be used carefully, when the caller knows we want to |
1797 | * bypass a potential IO scheduler on the target device. | |
1798 | */ | |
01e99aec ML |
1799 | void blk_mq_request_bypass_insert(struct request *rq, bool at_head, |
1800 | bool run_queue) | |
157f377b | 1801 | { |
ea4f995e | 1802 | struct blk_mq_hw_ctx *hctx = rq->mq_hctx; |
157f377b JA |
1803 | |
1804 | spin_lock(&hctx->lock); | |
01e99aec ML |
1805 | if (at_head) |
1806 | list_add(&rq->queuelist, &hctx->dispatch); | |
1807 | else | |
1808 | list_add_tail(&rq->queuelist, &hctx->dispatch); | |
157f377b JA |
1809 | spin_unlock(&hctx->lock); |
1810 | ||
b0850297 ML |
1811 | if (run_queue) |
1812 | blk_mq_run_hw_queue(hctx, false); | |
157f377b JA |
1813 | } |
1814 | ||
bd166ef1 JA |
1815 | void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx, |
1816 | struct list_head *list) | |
320ae51f JA |
1817 | |
1818 | { | |
3f0cedc7 | 1819 | struct request *rq; |
c16d6b5a | 1820 | enum hctx_type type = hctx->type; |
3f0cedc7 | 1821 | |
320ae51f JA |
1822 | /* |
1823 | * preemption doesn't flush plug list, so it's possible ctx->cpu is | |
1824 | * offline now | |
1825 | */ | |
3f0cedc7 | 1826 | list_for_each_entry(rq, list, queuelist) { |
e57690fe | 1827 | BUG_ON(rq->mq_ctx != ctx); |
3f0cedc7 | 1828 | trace_block_rq_insert(hctx->queue, rq); |
320ae51f | 1829 | } |
3f0cedc7 ML |
1830 | |
1831 | spin_lock(&ctx->lock); | |
c16d6b5a | 1832 | list_splice_tail_init(list, &ctx->rq_lists[type]); |
cfd0c552 | 1833 | blk_mq_hctx_mark_pending(hctx, ctx); |
320ae51f | 1834 | spin_unlock(&ctx->lock); |
320ae51f JA |
1835 | } |
1836 | ||
3110fc79 | 1837 | static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b) |
320ae51f JA |
1838 | { |
1839 | struct request *rqa = container_of(a, struct request, queuelist); | |
1840 | struct request *rqb = container_of(b, struct request, queuelist); | |
1841 | ||
7d30a621 PB |
1842 | if (rqa->mq_ctx != rqb->mq_ctx) |
1843 | return rqa->mq_ctx > rqb->mq_ctx; | |
1844 | if (rqa->mq_hctx != rqb->mq_hctx) | |
1845 | return rqa->mq_hctx > rqb->mq_hctx; | |
3110fc79 JA |
1846 | |
1847 | return blk_rq_pos(rqa) > blk_rq_pos(rqb); | |
320ae51f JA |
1848 | } |
1849 | ||
1850 | void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) | |
1851 | { | |
320ae51f | 1852 | LIST_HEAD(list); |
320ae51f | 1853 | |
95ed0c5b PB |
1854 | if (list_empty(&plug->mq_list)) |
1855 | return; | |
320ae51f JA |
1856 | list_splice_init(&plug->mq_list, &list); |
1857 | ||
ce5b009c JA |
1858 | if (plug->rq_count > 2 && plug->multiple_queues) |
1859 | list_sort(NULL, &list, plug_rq_cmp); | |
320ae51f | 1860 | |
bcc816df DZ |
1861 | plug->rq_count = 0; |
1862 | ||
95ed0c5b PB |
1863 | do { |
1864 | struct list_head rq_list; | |
1865 | struct request *rq, *head_rq = list_entry_rq(list.next); | |
1866 | struct list_head *pos = &head_rq->queuelist; /* skip first */ | |
1867 | struct blk_mq_hw_ctx *this_hctx = head_rq->mq_hctx; | |
1868 | struct blk_mq_ctx *this_ctx = head_rq->mq_ctx; | |
1869 | unsigned int depth = 1; | |
1870 | ||
1871 | list_for_each_continue(pos, &list) { | |
1872 | rq = list_entry_rq(pos); | |
1873 | BUG_ON(!rq->q); | |
1874 | if (rq->mq_hctx != this_hctx || rq->mq_ctx != this_ctx) | |
1875 | break; | |
1876 | depth++; | |
320ae51f JA |
1877 | } |
1878 | ||
95ed0c5b PB |
1879 | list_cut_before(&rq_list, &list, pos); |
1880 | trace_block_unplug(head_rq->q, depth, !from_schedule); | |
67cae4c9 | 1881 | blk_mq_sched_insert_requests(this_hctx, this_ctx, &rq_list, |
bd166ef1 | 1882 | from_schedule); |
95ed0c5b | 1883 | } while(!list_empty(&list)); |
320ae51f JA |
1884 | } |
1885 | ||
14ccb66b CH |
1886 | static void blk_mq_bio_to_request(struct request *rq, struct bio *bio, |
1887 | unsigned int nr_segs) | |
320ae51f | 1888 | { |
f924cdde CH |
1889 | if (bio->bi_opf & REQ_RAHEAD) |
1890 | rq->cmd_flags |= REQ_FAILFAST_MASK; | |
1891 | ||
1892 | rq->__sector = bio->bi_iter.bi_sector; | |
1893 | rq->write_hint = bio->bi_write_hint; | |
14ccb66b | 1894 | blk_rq_bio_prep(rq, bio, nr_segs); |
a892c8d5 | 1895 | blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO); |
4b570521 | 1896 | |
b5af37ab | 1897 | blk_account_io_start(rq); |
320ae51f JA |
1898 | } |
1899 | ||
0f95549c MS |
1900 | static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx, |
1901 | struct request *rq, | |
be94f058 | 1902 | blk_qc_t *cookie, bool last) |
f984df1f | 1903 | { |
f984df1f | 1904 | struct request_queue *q = rq->q; |
f984df1f SL |
1905 | struct blk_mq_queue_data bd = { |
1906 | .rq = rq, | |
be94f058 | 1907 | .last = last, |
f984df1f | 1908 | }; |
bd166ef1 | 1909 | blk_qc_t new_cookie; |
f06345ad | 1910 | blk_status_t ret; |
0f95549c MS |
1911 | |
1912 | new_cookie = request_to_qc_t(hctx, rq); | |
1913 | ||
1914 | /* | |
1915 | * For OK queue, we are done. For error, caller may kill it. | |
1916 | * Any other error (busy), just add it to our list as we | |
1917 | * previously would have done. | |
1918 | */ | |
1919 | ret = q->mq_ops->queue_rq(hctx, &bd); | |
1920 | switch (ret) { | |
1921 | case BLK_STS_OK: | |
6ce3dd6e | 1922 | blk_mq_update_dispatch_busy(hctx, false); |
0f95549c MS |
1923 | *cookie = new_cookie; |
1924 | break; | |
1925 | case BLK_STS_RESOURCE: | |
86ff7c2a | 1926 | case BLK_STS_DEV_RESOURCE: |
6ce3dd6e | 1927 | blk_mq_update_dispatch_busy(hctx, true); |
0f95549c MS |
1928 | __blk_mq_requeue_request(rq); |
1929 | break; | |
1930 | default: | |
6ce3dd6e | 1931 | blk_mq_update_dispatch_busy(hctx, false); |
0f95549c MS |
1932 | *cookie = BLK_QC_T_NONE; |
1933 | break; | |
1934 | } | |
1935 | ||
1936 | return ret; | |
1937 | } | |
1938 | ||
fd9c40f6 | 1939 | static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, |
0f95549c | 1940 | struct request *rq, |
396eaf21 | 1941 | blk_qc_t *cookie, |
fd9c40f6 | 1942 | bool bypass_insert, bool last) |
0f95549c MS |
1943 | { |
1944 | struct request_queue *q = rq->q; | |
d964f04a ML |
1945 | bool run_queue = true; |
1946 | ||
23d4ee19 | 1947 | /* |
fd9c40f6 | 1948 | * RCU or SRCU read lock is needed before checking quiesced flag. |
23d4ee19 | 1949 | * |
fd9c40f6 BVA |
1950 | * When queue is stopped or quiesced, ignore 'bypass_insert' from |
1951 | * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller, | |
1952 | * and avoid driver to try to dispatch again. | |
23d4ee19 | 1953 | */ |
fd9c40f6 | 1954 | if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) { |
d964f04a | 1955 | run_queue = false; |
fd9c40f6 BVA |
1956 | bypass_insert = false; |
1957 | goto insert; | |
d964f04a | 1958 | } |
f984df1f | 1959 | |
fd9c40f6 BVA |
1960 | if (q->elevator && !bypass_insert) |
1961 | goto insert; | |
2253efc8 | 1962 | |
0bca799b | 1963 | if (!blk_mq_get_dispatch_budget(hctx)) |
fd9c40f6 | 1964 | goto insert; |
bd166ef1 | 1965 | |
8ab6bb9e | 1966 | if (!blk_mq_get_driver_tag(rq)) { |
0bca799b | 1967 | blk_mq_put_dispatch_budget(hctx); |
fd9c40f6 | 1968 | goto insert; |
88022d72 | 1969 | } |
de148297 | 1970 | |
fd9c40f6 BVA |
1971 | return __blk_mq_issue_directly(hctx, rq, cookie, last); |
1972 | insert: | |
1973 | if (bypass_insert) | |
1974 | return BLK_STS_RESOURCE; | |
1975 | ||
01e99aec | 1976 | blk_mq_request_bypass_insert(rq, false, run_queue); |
fd9c40f6 BVA |
1977 | return BLK_STS_OK; |
1978 | } | |
1979 | ||
105663f7 AA |
1980 | /** |
1981 | * blk_mq_try_issue_directly - Try to send a request directly to device driver. | |
1982 | * @hctx: Pointer of the associated hardware queue. | |
1983 | * @rq: Pointer to request to be sent. | |
1984 | * @cookie: Request queue cookie. | |
1985 | * | |
1986 | * If the device has enough resources to accept a new request now, send the | |
1987 | * request directly to device driver. Else, insert at hctx->dispatch queue, so | |
1988 | * we can try send it another time in the future. Requests inserted at this | |
1989 | * queue have higher priority. | |
1990 | */ | |
fd9c40f6 BVA |
1991 | static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, |
1992 | struct request *rq, blk_qc_t *cookie) | |
1993 | { | |
1994 | blk_status_t ret; | |
1995 | int srcu_idx; | |
1996 | ||
1997 | might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING); | |
1998 | ||
1999 | hctx_lock(hctx, &srcu_idx); | |
2000 | ||
2001 | ret = __blk_mq_try_issue_directly(hctx, rq, cookie, false, true); | |
2002 | if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE) | |
01e99aec | 2003 | blk_mq_request_bypass_insert(rq, false, true); |
fd9c40f6 BVA |
2004 | else if (ret != BLK_STS_OK) |
2005 | blk_mq_end_request(rq, ret); | |
2006 | ||
2007 | hctx_unlock(hctx, srcu_idx); | |
2008 | } | |
2009 | ||
2010 | blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last) | |
2011 | { | |
2012 | blk_status_t ret; | |
2013 | int srcu_idx; | |
2014 | blk_qc_t unused_cookie; | |
2015 | struct blk_mq_hw_ctx *hctx = rq->mq_hctx; | |
2016 | ||
2017 | hctx_lock(hctx, &srcu_idx); | |
2018 | ret = __blk_mq_try_issue_directly(hctx, rq, &unused_cookie, true, last); | |
04ced159 | 2019 | hctx_unlock(hctx, srcu_idx); |
7f556a44 JW |
2020 | |
2021 | return ret; | |
5eb6126e CH |
2022 | } |
2023 | ||
6ce3dd6e ML |
2024 | void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx, |
2025 | struct list_head *list) | |
2026 | { | |
536167d4 KB |
2027 | int queued = 0; |
2028 | ||
6ce3dd6e | 2029 | while (!list_empty(list)) { |
fd9c40f6 | 2030 | blk_status_t ret; |
6ce3dd6e ML |
2031 | struct request *rq = list_first_entry(list, struct request, |
2032 | queuelist); | |
2033 | ||
2034 | list_del_init(&rq->queuelist); | |
fd9c40f6 BVA |
2035 | ret = blk_mq_request_issue_directly(rq, list_empty(list)); |
2036 | if (ret != BLK_STS_OK) { | |
2037 | if (ret == BLK_STS_RESOURCE || | |
2038 | ret == BLK_STS_DEV_RESOURCE) { | |
01e99aec | 2039 | blk_mq_request_bypass_insert(rq, false, |
c616cbee | 2040 | list_empty(list)); |
fd9c40f6 BVA |
2041 | break; |
2042 | } | |
2043 | blk_mq_end_request(rq, ret); | |
536167d4 KB |
2044 | } else |
2045 | queued++; | |
6ce3dd6e | 2046 | } |
d666ba98 JA |
2047 | |
2048 | /* | |
2049 | * If we didn't flush the entire list, we could have told | |
2050 | * the driver there was more coming, but that turned out to | |
2051 | * be a lie. | |
2052 | */ | |
536167d4 | 2053 | if (!list_empty(list) && hctx->queue->mq_ops->commit_rqs && queued) |
d666ba98 | 2054 | hctx->queue->mq_ops->commit_rqs(hctx); |
6ce3dd6e ML |
2055 | } |
2056 | ||
ce5b009c JA |
2057 | static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq) |
2058 | { | |
2059 | list_add_tail(&rq->queuelist, &plug->mq_list); | |
2060 | plug->rq_count++; | |
2061 | if (!plug->multiple_queues && !list_is_singular(&plug->mq_list)) { | |
2062 | struct request *tmp; | |
2063 | ||
2064 | tmp = list_first_entry(&plug->mq_list, struct request, | |
2065 | queuelist); | |
2066 | if (tmp->q != rq->q) | |
2067 | plug->multiple_queues = true; | |
2068 | } | |
2069 | } | |
2070 | ||
105663f7 AA |
2071 | /** |
2072 | * blk_mq_make_request - Create and send a request to block device. | |
2073 | * @q: Request queue pointer. | |
2074 | * @bio: Bio pointer. | |
2075 | * | |
2076 | * Builds up a request structure from @q and @bio and send to the device. The | |
2077 | * request may not be queued directly to hardware if: | |
2078 | * * This request can be merged with another one | |
2079 | * * We want to place request at plug queue for possible future merging | |
2080 | * * There is an IO scheduler active at this queue | |
2081 | * | |
2082 | * It will not queue the request if there is an error with the bio, or at the | |
2083 | * request creation. | |
2084 | * | |
2085 | * Returns: Request queue cookie. | |
2086 | */ | |
8cf7961d | 2087 | blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio) |
07068d5b | 2088 | { |
ef295ecf | 2089 | const int is_sync = op_is_sync(bio->bi_opf); |
f73f44eb | 2090 | const int is_flush_fua = op_is_flush(bio->bi_opf); |
e6e7abff CH |
2091 | struct blk_mq_alloc_data data = { |
2092 | .q = q, | |
2093 | }; | |
07068d5b | 2094 | struct request *rq; |
f984df1f | 2095 | struct blk_plug *plug; |
5b3f341f | 2096 | struct request *same_queue_rq = NULL; |
14ccb66b | 2097 | unsigned int nr_segs; |
7b371636 | 2098 | blk_qc_t cookie; |
a892c8d5 | 2099 | blk_status_t ret; |
07068d5b JA |
2100 | |
2101 | blk_queue_bounce(q, &bio); | |
14ccb66b | 2102 | __blk_queue_split(q, &bio, &nr_segs); |
f36ea50c | 2103 | |
e23947bd | 2104 | if (!bio_integrity_prep(bio)) |
ac7c5675 | 2105 | goto queue_exit; |
07068d5b | 2106 | |
87c279e6 | 2107 | if (!is_flush_fua && !blk_queue_nomerges(q) && |
14ccb66b | 2108 | blk_attempt_plug_merge(q, bio, nr_segs, &same_queue_rq)) |
ac7c5675 | 2109 | goto queue_exit; |
f984df1f | 2110 | |
14ccb66b | 2111 | if (blk_mq_sched_bio_merge(q, bio, nr_segs)) |
ac7c5675 | 2112 | goto queue_exit; |
bd166ef1 | 2113 | |
d5337560 | 2114 | rq_qos_throttle(q, bio); |
87760e5e | 2115 | |
7809167d | 2116 | data.cmd_flags = bio->bi_opf; |
e6e7abff | 2117 | rq = __blk_mq_alloc_request(&data); |
87760e5e | 2118 | if (unlikely(!rq)) { |
c1c80384 | 2119 | rq_qos_cleanup(q, bio); |
7b6620d7 | 2120 | if (bio->bi_opf & REQ_NOWAIT) |
03a07c92 | 2121 | bio_wouldblock_error(bio); |
ac7c5675 | 2122 | goto queue_exit; |
87760e5e JA |
2123 | } |
2124 | ||
d6f1dda2 XW |
2125 | trace_block_getrq(q, bio, bio->bi_opf); |
2126 | ||
c1c80384 | 2127 | rq_qos_track(q, rq, bio); |
07068d5b | 2128 | |
fd2d3326 | 2129 | cookie = request_to_qc_t(data.hctx, rq); |
07068d5b | 2130 | |
970d168d BVA |
2131 | blk_mq_bio_to_request(rq, bio, nr_segs); |
2132 | ||
a892c8d5 ST |
2133 | ret = blk_crypto_init_request(rq); |
2134 | if (ret != BLK_STS_OK) { | |
2135 | bio->bi_status = ret; | |
2136 | bio_endio(bio); | |
2137 | blk_mq_free_request(rq); | |
2138 | return BLK_QC_T_NONE; | |
2139 | } | |
2140 | ||
b49773e7 | 2141 | plug = blk_mq_plug(q, bio); |
07068d5b | 2142 | if (unlikely(is_flush_fua)) { |
105663f7 | 2143 | /* Bypass scheduler for flush requests */ |
923218f6 ML |
2144 | blk_insert_flush(rq); |
2145 | blk_mq_run_hw_queue(data.hctx, true); | |
3154df26 ML |
2146 | } else if (plug && (q->nr_hw_queues == 1 || q->mq_ops->commit_rqs || |
2147 | !blk_queue_nonrot(q))) { | |
b2c5d16b JA |
2148 | /* |
2149 | * Use plugging if we have a ->commit_rqs() hook as well, as | |
2150 | * we know the driver uses bd->last in a smart fashion. | |
3154df26 ML |
2151 | * |
2152 | * Use normal plugging if this disk is slow HDD, as sequential | |
2153 | * IO may benefit a lot from plug merging. | |
b2c5d16b | 2154 | */ |
5f0ed774 | 2155 | unsigned int request_count = plug->rq_count; |
600271d9 SL |
2156 | struct request *last = NULL; |
2157 | ||
676d0607 | 2158 | if (!request_count) |
e6c4438b | 2159 | trace_block_plug(q); |
600271d9 SL |
2160 | else |
2161 | last = list_entry_rq(plug->mq_list.prev); | |
b094f89c | 2162 | |
600271d9 SL |
2163 | if (request_count >= BLK_MAX_REQUEST_COUNT || (last && |
2164 | blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) { | |
e6c4438b JM |
2165 | blk_flush_plug_list(plug, false); |
2166 | trace_block_plug(q); | |
320ae51f | 2167 | } |
b094f89c | 2168 | |
ce5b009c | 2169 | blk_add_rq_to_plug(plug, rq); |
a12de1d4 | 2170 | } else if (q->elevator) { |
105663f7 | 2171 | /* Insert the request at the IO scheduler queue */ |
a12de1d4 | 2172 | blk_mq_sched_insert_request(rq, false, true, true); |
2299722c | 2173 | } else if (plug && !blk_queue_nomerges(q)) { |
07068d5b | 2174 | /* |
6a83e74d | 2175 | * We do limited plugging. If the bio can be merged, do that. |
f984df1f SL |
2176 | * Otherwise the existing request in the plug list will be |
2177 | * issued. So the plug list will have one request at most | |
2299722c CH |
2178 | * The plug list might get flushed before this. If that happens, |
2179 | * the plug list is empty, and same_queue_rq is invalid. | |
07068d5b | 2180 | */ |
2299722c CH |
2181 | if (list_empty(&plug->mq_list)) |
2182 | same_queue_rq = NULL; | |
4711b573 | 2183 | if (same_queue_rq) { |
2299722c | 2184 | list_del_init(&same_queue_rq->queuelist); |
4711b573 JA |
2185 | plug->rq_count--; |
2186 | } | |
ce5b009c | 2187 | blk_add_rq_to_plug(plug, rq); |
ff3b74b8 | 2188 | trace_block_plug(q); |
2299722c | 2189 | |
dad7a3be | 2190 | if (same_queue_rq) { |
ea4f995e | 2191 | data.hctx = same_queue_rq->mq_hctx; |
ff3b74b8 | 2192 | trace_block_unplug(q, 1, true); |
2299722c | 2193 | blk_mq_try_issue_directly(data.hctx, same_queue_rq, |
fd9c40f6 | 2194 | &cookie); |
dad7a3be | 2195 | } |
a12de1d4 ML |
2196 | } else if ((q->nr_hw_queues > 1 && is_sync) || |
2197 | !data.hctx->dispatch_busy) { | |
105663f7 AA |
2198 | /* |
2199 | * There is no scheduler and we can try to send directly | |
2200 | * to the hardware. | |
2201 | */ | |
fd9c40f6 | 2202 | blk_mq_try_issue_directly(data.hctx, rq, &cookie); |
ab42f35d | 2203 | } else { |
105663f7 | 2204 | /* Default case. */ |
8fa9f556 | 2205 | blk_mq_sched_insert_request(rq, false, true, true); |
ab42f35d | 2206 | } |
320ae51f | 2207 | |
7b371636 | 2208 | return cookie; |
ac7c5675 CH |
2209 | queue_exit: |
2210 | blk_queue_exit(q); | |
2211 | return BLK_QC_T_NONE; | |
320ae51f | 2212 | } |
8cf7961d | 2213 | EXPORT_SYMBOL_GPL(blk_mq_make_request); /* only for request based dm */ |
320ae51f | 2214 | |
cc71a6f4 JA |
2215 | void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags, |
2216 | unsigned int hctx_idx) | |
95363efd | 2217 | { |
e9b267d9 | 2218 | struct page *page; |
320ae51f | 2219 | |
24d2f903 | 2220 | if (tags->rqs && set->ops->exit_request) { |
e9b267d9 | 2221 | int i; |
320ae51f | 2222 | |
24d2f903 | 2223 | for (i = 0; i < tags->nr_tags; i++) { |
2af8cbe3 JA |
2224 | struct request *rq = tags->static_rqs[i]; |
2225 | ||
2226 | if (!rq) | |
e9b267d9 | 2227 | continue; |
d6296d39 | 2228 | set->ops->exit_request(set, rq, hctx_idx); |
2af8cbe3 | 2229 | tags->static_rqs[i] = NULL; |
e9b267d9 | 2230 | } |
320ae51f | 2231 | } |
320ae51f | 2232 | |
24d2f903 CH |
2233 | while (!list_empty(&tags->page_list)) { |
2234 | page = list_first_entry(&tags->page_list, struct page, lru); | |
6753471c | 2235 | list_del_init(&page->lru); |
f75782e4 CM |
2236 | /* |
2237 | * Remove kmemleak object previously allocated in | |
273938bf | 2238 | * blk_mq_alloc_rqs(). |
f75782e4 CM |
2239 | */ |
2240 | kmemleak_free(page_address(page)); | |
320ae51f JA |
2241 | __free_pages(page, page->private); |
2242 | } | |
cc71a6f4 | 2243 | } |
320ae51f | 2244 | |
cc71a6f4 JA |
2245 | void blk_mq_free_rq_map(struct blk_mq_tags *tags) |
2246 | { | |
24d2f903 | 2247 | kfree(tags->rqs); |
cc71a6f4 | 2248 | tags->rqs = NULL; |
2af8cbe3 JA |
2249 | kfree(tags->static_rqs); |
2250 | tags->static_rqs = NULL; | |
320ae51f | 2251 | |
24d2f903 | 2252 | blk_mq_free_tags(tags); |
320ae51f JA |
2253 | } |
2254 | ||
cc71a6f4 JA |
2255 | struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, |
2256 | unsigned int hctx_idx, | |
2257 | unsigned int nr_tags, | |
2258 | unsigned int reserved_tags) | |
320ae51f | 2259 | { |
24d2f903 | 2260 | struct blk_mq_tags *tags; |
59f082e4 | 2261 | int node; |
320ae51f | 2262 | |
7d76f856 | 2263 | node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx); |
59f082e4 SL |
2264 | if (node == NUMA_NO_NODE) |
2265 | node = set->numa_node; | |
2266 | ||
2267 | tags = blk_mq_init_tags(nr_tags, reserved_tags, node, | |
24391c0d | 2268 | BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags)); |
24d2f903 CH |
2269 | if (!tags) |
2270 | return NULL; | |
320ae51f | 2271 | |
590b5b7d | 2272 | tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *), |
36e1f3d1 | 2273 | GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, |
59f082e4 | 2274 | node); |
24d2f903 CH |
2275 | if (!tags->rqs) { |
2276 | blk_mq_free_tags(tags); | |
2277 | return NULL; | |
2278 | } | |
320ae51f | 2279 | |
590b5b7d KC |
2280 | tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *), |
2281 | GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, | |
2282 | node); | |
2af8cbe3 JA |
2283 | if (!tags->static_rqs) { |
2284 | kfree(tags->rqs); | |
2285 | blk_mq_free_tags(tags); | |
2286 | return NULL; | |
2287 | } | |
2288 | ||
cc71a6f4 JA |
2289 | return tags; |
2290 | } | |
2291 | ||
2292 | static size_t order_to_size(unsigned int order) | |
2293 | { | |
2294 | return (size_t)PAGE_SIZE << order; | |
2295 | } | |
2296 | ||
1d9bd516 TH |
2297 | static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq, |
2298 | unsigned int hctx_idx, int node) | |
2299 | { | |
2300 | int ret; | |
2301 | ||
2302 | if (set->ops->init_request) { | |
2303 | ret = set->ops->init_request(set, rq, hctx_idx, node); | |
2304 | if (ret) | |
2305 | return ret; | |
2306 | } | |
2307 | ||
12f5b931 | 2308 | WRITE_ONCE(rq->state, MQ_RQ_IDLE); |
1d9bd516 TH |
2309 | return 0; |
2310 | } | |
2311 | ||
cc71a6f4 JA |
2312 | int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags, |
2313 | unsigned int hctx_idx, unsigned int depth) | |
2314 | { | |
2315 | unsigned int i, j, entries_per_page, max_order = 4; | |
2316 | size_t rq_size, left; | |
59f082e4 SL |
2317 | int node; |
2318 | ||
7d76f856 | 2319 | node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx); |
59f082e4 SL |
2320 | if (node == NUMA_NO_NODE) |
2321 | node = set->numa_node; | |
cc71a6f4 JA |
2322 | |
2323 | INIT_LIST_HEAD(&tags->page_list); | |
2324 | ||
320ae51f JA |
2325 | /* |
2326 | * rq_size is the size of the request plus driver payload, rounded | |
2327 | * to the cacheline size | |
2328 | */ | |
24d2f903 | 2329 | rq_size = round_up(sizeof(struct request) + set->cmd_size, |
320ae51f | 2330 | cache_line_size()); |
cc71a6f4 | 2331 | left = rq_size * depth; |
320ae51f | 2332 | |
cc71a6f4 | 2333 | for (i = 0; i < depth; ) { |
320ae51f JA |
2334 | int this_order = max_order; |
2335 | struct page *page; | |
2336 | int to_do; | |
2337 | void *p; | |
2338 | ||
b3a834b1 | 2339 | while (this_order && left < order_to_size(this_order - 1)) |
320ae51f JA |
2340 | this_order--; |
2341 | ||
2342 | do { | |
59f082e4 | 2343 | page = alloc_pages_node(node, |
36e1f3d1 | 2344 | GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO, |
a5164405 | 2345 | this_order); |
320ae51f JA |
2346 | if (page) |
2347 | break; | |
2348 | if (!this_order--) | |
2349 | break; | |
2350 | if (order_to_size(this_order) < rq_size) | |
2351 | break; | |
2352 | } while (1); | |
2353 | ||
2354 | if (!page) | |
24d2f903 | 2355 | goto fail; |
320ae51f JA |
2356 | |
2357 | page->private = this_order; | |
24d2f903 | 2358 | list_add_tail(&page->lru, &tags->page_list); |
320ae51f JA |
2359 | |
2360 | p = page_address(page); | |
f75782e4 CM |
2361 | /* |
2362 | * Allow kmemleak to scan these pages as they contain pointers | |
2363 | * to additional allocations like via ops->init_request(). | |
2364 | */ | |
36e1f3d1 | 2365 | kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO); |
320ae51f | 2366 | entries_per_page = order_to_size(this_order) / rq_size; |
cc71a6f4 | 2367 | to_do = min(entries_per_page, depth - i); |
320ae51f JA |
2368 | left -= to_do * rq_size; |
2369 | for (j = 0; j < to_do; j++) { | |
2af8cbe3 JA |
2370 | struct request *rq = p; |
2371 | ||
2372 | tags->static_rqs[i] = rq; | |
1d9bd516 TH |
2373 | if (blk_mq_init_request(set, rq, hctx_idx, node)) { |
2374 | tags->static_rqs[i] = NULL; | |
2375 | goto fail; | |
e9b267d9 CH |
2376 | } |
2377 | ||
320ae51f JA |
2378 | p += rq_size; |
2379 | i++; | |
2380 | } | |
2381 | } | |
cc71a6f4 | 2382 | return 0; |
320ae51f | 2383 | |
24d2f903 | 2384 | fail: |
cc71a6f4 JA |
2385 | blk_mq_free_rqs(set, tags, hctx_idx); |
2386 | return -ENOMEM; | |
320ae51f JA |
2387 | } |
2388 | ||
bf0beec0 ML |
2389 | struct rq_iter_data { |
2390 | struct blk_mq_hw_ctx *hctx; | |
2391 | bool has_rq; | |
2392 | }; | |
2393 | ||
2394 | static bool blk_mq_has_request(struct request *rq, void *data, bool reserved) | |
2395 | { | |
2396 | struct rq_iter_data *iter_data = data; | |
2397 | ||
2398 | if (rq->mq_hctx != iter_data->hctx) | |
2399 | return true; | |
2400 | iter_data->has_rq = true; | |
2401 | return false; | |
2402 | } | |
2403 | ||
2404 | static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx) | |
2405 | { | |
2406 | struct blk_mq_tags *tags = hctx->sched_tags ? | |
2407 | hctx->sched_tags : hctx->tags; | |
2408 | struct rq_iter_data data = { | |
2409 | .hctx = hctx, | |
2410 | }; | |
2411 | ||
2412 | blk_mq_all_tag_iter(tags, blk_mq_has_request, &data); | |
2413 | return data.has_rq; | |
2414 | } | |
2415 | ||
2416 | static inline bool blk_mq_last_cpu_in_hctx(unsigned int cpu, | |
2417 | struct blk_mq_hw_ctx *hctx) | |
2418 | { | |
2419 | if (cpumask_next_and(-1, hctx->cpumask, cpu_online_mask) != cpu) | |
2420 | return false; | |
2421 | if (cpumask_next_and(cpu, hctx->cpumask, cpu_online_mask) < nr_cpu_ids) | |
2422 | return false; | |
2423 | return true; | |
2424 | } | |
2425 | ||
2426 | static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node) | |
2427 | { | |
2428 | struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node, | |
2429 | struct blk_mq_hw_ctx, cpuhp_online); | |
2430 | ||
2431 | if (!cpumask_test_cpu(cpu, hctx->cpumask) || | |
2432 | !blk_mq_last_cpu_in_hctx(cpu, hctx)) | |
2433 | return 0; | |
2434 | ||
2435 | /* | |
2436 | * Prevent new request from being allocated on the current hctx. | |
2437 | * | |
2438 | * The smp_mb__after_atomic() Pairs with the implied barrier in | |
2439 | * test_and_set_bit_lock in sbitmap_get(). Ensures the inactive flag is | |
2440 | * seen once we return from the tag allocator. | |
2441 | */ | |
2442 | set_bit(BLK_MQ_S_INACTIVE, &hctx->state); | |
2443 | smp_mb__after_atomic(); | |
2444 | ||
2445 | /* | |
2446 | * Try to grab a reference to the queue and wait for any outstanding | |
2447 | * requests. If we could not grab a reference the queue has been | |
2448 | * frozen and there are no requests. | |
2449 | */ | |
2450 | if (percpu_ref_tryget(&hctx->queue->q_usage_counter)) { | |
2451 | while (blk_mq_hctx_has_requests(hctx)) | |
2452 | msleep(5); | |
2453 | percpu_ref_put(&hctx->queue->q_usage_counter); | |
2454 | } | |
2455 | ||
2456 | return 0; | |
2457 | } | |
2458 | ||
2459 | static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node) | |
2460 | { | |
2461 | struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node, | |
2462 | struct blk_mq_hw_ctx, cpuhp_online); | |
2463 | ||
2464 | if (cpumask_test_cpu(cpu, hctx->cpumask)) | |
2465 | clear_bit(BLK_MQ_S_INACTIVE, &hctx->state); | |
2466 | return 0; | |
2467 | } | |
2468 | ||
e57690fe JA |
2469 | /* |
2470 | * 'cpu' is going away. splice any existing rq_list entries from this | |
2471 | * software queue to the hw queue dispatch list, and ensure that it | |
2472 | * gets run. | |
2473 | */ | |
9467f859 | 2474 | static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node) |
484b4061 | 2475 | { |
9467f859 | 2476 | struct blk_mq_hw_ctx *hctx; |
484b4061 JA |
2477 | struct blk_mq_ctx *ctx; |
2478 | LIST_HEAD(tmp); | |
c16d6b5a | 2479 | enum hctx_type type; |
484b4061 | 2480 | |
9467f859 | 2481 | hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead); |
bf0beec0 ML |
2482 | if (!cpumask_test_cpu(cpu, hctx->cpumask)) |
2483 | return 0; | |
2484 | ||
e57690fe | 2485 | ctx = __blk_mq_get_ctx(hctx->queue, cpu); |
c16d6b5a | 2486 | type = hctx->type; |
484b4061 JA |
2487 | |
2488 | spin_lock(&ctx->lock); | |
c16d6b5a ML |
2489 | if (!list_empty(&ctx->rq_lists[type])) { |
2490 | list_splice_init(&ctx->rq_lists[type], &tmp); | |
484b4061 JA |
2491 | blk_mq_hctx_clear_pending(hctx, ctx); |
2492 | } | |
2493 | spin_unlock(&ctx->lock); | |
2494 | ||
2495 | if (list_empty(&tmp)) | |
9467f859 | 2496 | return 0; |
484b4061 | 2497 | |
e57690fe JA |
2498 | spin_lock(&hctx->lock); |
2499 | list_splice_tail_init(&tmp, &hctx->dispatch); | |
2500 | spin_unlock(&hctx->lock); | |
484b4061 JA |
2501 | |
2502 | blk_mq_run_hw_queue(hctx, true); | |
9467f859 | 2503 | return 0; |
484b4061 JA |
2504 | } |
2505 | ||
9467f859 | 2506 | static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx) |
484b4061 | 2507 | { |
bf0beec0 ML |
2508 | if (!(hctx->flags & BLK_MQ_F_STACKING)) |
2509 | cpuhp_state_remove_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE, | |
2510 | &hctx->cpuhp_online); | |
9467f859 TG |
2511 | cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD, |
2512 | &hctx->cpuhp_dead); | |
484b4061 JA |
2513 | } |
2514 | ||
c3b4afca | 2515 | /* hctx->ctxs will be freed in queue's release handler */ |
08e98fc6 ML |
2516 | static void blk_mq_exit_hctx(struct request_queue *q, |
2517 | struct blk_mq_tag_set *set, | |
2518 | struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx) | |
2519 | { | |
8ab0b7dc ML |
2520 | if (blk_mq_hw_queue_mapped(hctx)) |
2521 | blk_mq_tag_idle(hctx); | |
08e98fc6 | 2522 | |
f70ced09 | 2523 | if (set->ops->exit_request) |
d6296d39 | 2524 | set->ops->exit_request(set, hctx->fq->flush_rq, hctx_idx); |
f70ced09 | 2525 | |
08e98fc6 ML |
2526 | if (set->ops->exit_hctx) |
2527 | set->ops->exit_hctx(hctx, hctx_idx); | |
2528 | ||
9467f859 | 2529 | blk_mq_remove_cpuhp(hctx); |
2f8f1336 ML |
2530 | |
2531 | spin_lock(&q->unused_hctx_lock); | |
2532 | list_add(&hctx->hctx_list, &q->unused_hctx_list); | |
2533 | spin_unlock(&q->unused_hctx_lock); | |
08e98fc6 ML |
2534 | } |
2535 | ||
624dbe47 ML |
2536 | static void blk_mq_exit_hw_queues(struct request_queue *q, |
2537 | struct blk_mq_tag_set *set, int nr_queue) | |
2538 | { | |
2539 | struct blk_mq_hw_ctx *hctx; | |
2540 | unsigned int i; | |
2541 | ||
2542 | queue_for_each_hw_ctx(q, hctx, i) { | |
2543 | if (i == nr_queue) | |
2544 | break; | |
477e19de | 2545 | blk_mq_debugfs_unregister_hctx(hctx); |
08e98fc6 | 2546 | blk_mq_exit_hctx(q, set, hctx, i); |
624dbe47 | 2547 | } |
624dbe47 ML |
2548 | } |
2549 | ||
7c6c5b7c ML |
2550 | static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set) |
2551 | { | |
2552 | int hw_ctx_size = sizeof(struct blk_mq_hw_ctx); | |
2553 | ||
2554 | BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu), | |
2555 | __alignof__(struct blk_mq_hw_ctx)) != | |
2556 | sizeof(struct blk_mq_hw_ctx)); | |
2557 | ||
2558 | if (tag_set->flags & BLK_MQ_F_BLOCKING) | |
2559 | hw_ctx_size += sizeof(struct srcu_struct); | |
2560 | ||
2561 | return hw_ctx_size; | |
2562 | } | |
2563 | ||
08e98fc6 ML |
2564 | static int blk_mq_init_hctx(struct request_queue *q, |
2565 | struct blk_mq_tag_set *set, | |
2566 | struct blk_mq_hw_ctx *hctx, unsigned hctx_idx) | |
320ae51f | 2567 | { |
7c6c5b7c ML |
2568 | hctx->queue_num = hctx_idx; |
2569 | ||
bf0beec0 ML |
2570 | if (!(hctx->flags & BLK_MQ_F_STACKING)) |
2571 | cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE, | |
2572 | &hctx->cpuhp_online); | |
7c6c5b7c ML |
2573 | cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead); |
2574 | ||
2575 | hctx->tags = set->tags[hctx_idx]; | |
2576 | ||
2577 | if (set->ops->init_hctx && | |
2578 | set->ops->init_hctx(hctx, set->driver_data, hctx_idx)) | |
2579 | goto unregister_cpu_notifier; | |
08e98fc6 | 2580 | |
7c6c5b7c ML |
2581 | if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx, |
2582 | hctx->numa_node)) | |
2583 | goto exit_hctx; | |
2584 | return 0; | |
2585 | ||
2586 | exit_hctx: | |
2587 | if (set->ops->exit_hctx) | |
2588 | set->ops->exit_hctx(hctx, hctx_idx); | |
2589 | unregister_cpu_notifier: | |
2590 | blk_mq_remove_cpuhp(hctx); | |
2591 | return -1; | |
2592 | } | |
2593 | ||
2594 | static struct blk_mq_hw_ctx * | |
2595 | blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set, | |
2596 | int node) | |
2597 | { | |
2598 | struct blk_mq_hw_ctx *hctx; | |
2599 | gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY; | |
2600 | ||
2601 | hctx = kzalloc_node(blk_mq_hw_ctx_size(set), gfp, node); | |
2602 | if (!hctx) | |
2603 | goto fail_alloc_hctx; | |
2604 | ||
2605 | if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node)) | |
2606 | goto free_hctx; | |
2607 | ||
2608 | atomic_set(&hctx->nr_active, 0); | |
08e98fc6 | 2609 | if (node == NUMA_NO_NODE) |
7c6c5b7c ML |
2610 | node = set->numa_node; |
2611 | hctx->numa_node = node; | |
08e98fc6 | 2612 | |
9f993737 | 2613 | INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn); |
08e98fc6 ML |
2614 | spin_lock_init(&hctx->lock); |
2615 | INIT_LIST_HEAD(&hctx->dispatch); | |
2616 | hctx->queue = q; | |
2404e607 | 2617 | hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED; |
08e98fc6 | 2618 | |
2f8f1336 ML |
2619 | INIT_LIST_HEAD(&hctx->hctx_list); |
2620 | ||
320ae51f | 2621 | /* |
08e98fc6 ML |
2622 | * Allocate space for all possible cpus to avoid allocation at |
2623 | * runtime | |
320ae51f | 2624 | */ |
d904bfa7 | 2625 | hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *), |
7c6c5b7c | 2626 | gfp, node); |
08e98fc6 | 2627 | if (!hctx->ctxs) |
7c6c5b7c | 2628 | goto free_cpumask; |
320ae51f | 2629 | |
5b202853 | 2630 | if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8), |
7c6c5b7c | 2631 | gfp, node)) |
08e98fc6 | 2632 | goto free_ctxs; |
08e98fc6 | 2633 | hctx->nr_ctx = 0; |
320ae51f | 2634 | |
5815839b | 2635 | spin_lock_init(&hctx->dispatch_wait_lock); |
eb619fdb JA |
2636 | init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake); |
2637 | INIT_LIST_HEAD(&hctx->dispatch_wait.entry); | |
2638 | ||
754a1572 | 2639 | hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp); |
f70ced09 | 2640 | if (!hctx->fq) |
7c6c5b7c | 2641 | goto free_bitmap; |
320ae51f | 2642 | |
6a83e74d | 2643 | if (hctx->flags & BLK_MQ_F_BLOCKING) |
05707b64 | 2644 | init_srcu_struct(hctx->srcu); |
7c6c5b7c | 2645 | blk_mq_hctx_kobj_init(hctx); |
6a83e74d | 2646 | |
7c6c5b7c | 2647 | return hctx; |
320ae51f | 2648 | |
08e98fc6 | 2649 | free_bitmap: |
88459642 | 2650 | sbitmap_free(&hctx->ctx_map); |
08e98fc6 ML |
2651 | free_ctxs: |
2652 | kfree(hctx->ctxs); | |
7c6c5b7c ML |
2653 | free_cpumask: |
2654 | free_cpumask_var(hctx->cpumask); | |
2655 | free_hctx: | |
2656 | kfree(hctx); | |
2657 | fail_alloc_hctx: | |
2658 | return NULL; | |
08e98fc6 | 2659 | } |
320ae51f | 2660 | |
320ae51f JA |
2661 | static void blk_mq_init_cpu_queues(struct request_queue *q, |
2662 | unsigned int nr_hw_queues) | |
2663 | { | |
b3c661b1 JA |
2664 | struct blk_mq_tag_set *set = q->tag_set; |
2665 | unsigned int i, j; | |
320ae51f JA |
2666 | |
2667 | for_each_possible_cpu(i) { | |
2668 | struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i); | |
2669 | struct blk_mq_hw_ctx *hctx; | |
c16d6b5a | 2670 | int k; |
320ae51f | 2671 | |
320ae51f JA |
2672 | __ctx->cpu = i; |
2673 | spin_lock_init(&__ctx->lock); | |
c16d6b5a ML |
2674 | for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++) |
2675 | INIT_LIST_HEAD(&__ctx->rq_lists[k]); | |
2676 | ||
320ae51f JA |
2677 | __ctx->queue = q; |
2678 | ||
320ae51f JA |
2679 | /* |
2680 | * Set local node, IFF we have more than one hw queue. If | |
2681 | * not, we remain on the home node of the device | |
2682 | */ | |
b3c661b1 JA |
2683 | for (j = 0; j < set->nr_maps; j++) { |
2684 | hctx = blk_mq_map_queue_type(q, j, i); | |
2685 | if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE) | |
2686 | hctx->numa_node = local_memory_node(cpu_to_node(i)); | |
2687 | } | |
320ae51f JA |
2688 | } |
2689 | } | |
2690 | ||
03b63b02 WZ |
2691 | static bool __blk_mq_alloc_map_and_request(struct blk_mq_tag_set *set, |
2692 | int hctx_idx) | |
cc71a6f4 JA |
2693 | { |
2694 | int ret = 0; | |
2695 | ||
2696 | set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx, | |
2697 | set->queue_depth, set->reserved_tags); | |
2698 | if (!set->tags[hctx_idx]) | |
2699 | return false; | |
2700 | ||
2701 | ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx, | |
2702 | set->queue_depth); | |
2703 | if (!ret) | |
2704 | return true; | |
2705 | ||
2706 | blk_mq_free_rq_map(set->tags[hctx_idx]); | |
2707 | set->tags[hctx_idx] = NULL; | |
2708 | return false; | |
2709 | } | |
2710 | ||
2711 | static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set, | |
2712 | unsigned int hctx_idx) | |
2713 | { | |
4e6db0f2 | 2714 | if (set->tags && set->tags[hctx_idx]) { |
bd166ef1 JA |
2715 | blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx); |
2716 | blk_mq_free_rq_map(set->tags[hctx_idx]); | |
2717 | set->tags[hctx_idx] = NULL; | |
2718 | } | |
cc71a6f4 JA |
2719 | } |
2720 | ||
4b855ad3 | 2721 | static void blk_mq_map_swqueue(struct request_queue *q) |
320ae51f | 2722 | { |
b3c661b1 | 2723 | unsigned int i, j, hctx_idx; |
320ae51f JA |
2724 | struct blk_mq_hw_ctx *hctx; |
2725 | struct blk_mq_ctx *ctx; | |
2a34c087 | 2726 | struct blk_mq_tag_set *set = q->tag_set; |
320ae51f JA |
2727 | |
2728 | queue_for_each_hw_ctx(q, hctx, i) { | |
e4043dcf | 2729 | cpumask_clear(hctx->cpumask); |
320ae51f | 2730 | hctx->nr_ctx = 0; |
d416c92c | 2731 | hctx->dispatch_from = NULL; |
320ae51f JA |
2732 | } |
2733 | ||
2734 | /* | |
4b855ad3 | 2735 | * Map software to hardware queues. |
4412efec ML |
2736 | * |
2737 | * If the cpu isn't present, the cpu is mapped to first hctx. | |
320ae51f | 2738 | */ |
20e4d813 | 2739 | for_each_possible_cpu(i) { |
4412efec | 2740 | |
897bb0c7 | 2741 | ctx = per_cpu_ptr(q->queue_ctx, i); |
b3c661b1 | 2742 | for (j = 0; j < set->nr_maps; j++) { |
bb94aea1 JW |
2743 | if (!set->map[j].nr_queues) { |
2744 | ctx->hctxs[j] = blk_mq_map_queue_type(q, | |
2745 | HCTX_TYPE_DEFAULT, i); | |
e5edd5f2 | 2746 | continue; |
bb94aea1 | 2747 | } |
fd689871 ML |
2748 | hctx_idx = set->map[j].mq_map[i]; |
2749 | /* unmapped hw queue can be remapped after CPU topo changed */ | |
2750 | if (!set->tags[hctx_idx] && | |
03b63b02 | 2751 | !__blk_mq_alloc_map_and_request(set, hctx_idx)) { |
fd689871 ML |
2752 | /* |
2753 | * If tags initialization fail for some hctx, | |
2754 | * that hctx won't be brought online. In this | |
2755 | * case, remap the current ctx to hctx[0] which | |
2756 | * is guaranteed to always have tags allocated | |
2757 | */ | |
2758 | set->map[j].mq_map[i] = 0; | |
2759 | } | |
e5edd5f2 | 2760 | |
b3c661b1 | 2761 | hctx = blk_mq_map_queue_type(q, j, i); |
8ccdf4a3 | 2762 | ctx->hctxs[j] = hctx; |
b3c661b1 JA |
2763 | /* |
2764 | * If the CPU is already set in the mask, then we've | |
2765 | * mapped this one already. This can happen if | |
2766 | * devices share queues across queue maps. | |
2767 | */ | |
2768 | if (cpumask_test_cpu(i, hctx->cpumask)) | |
2769 | continue; | |
2770 | ||
2771 | cpumask_set_cpu(i, hctx->cpumask); | |
2772 | hctx->type = j; | |
2773 | ctx->index_hw[hctx->type] = hctx->nr_ctx; | |
2774 | hctx->ctxs[hctx->nr_ctx++] = ctx; | |
2775 | ||
2776 | /* | |
2777 | * If the nr_ctx type overflows, we have exceeded the | |
2778 | * amount of sw queues we can support. | |
2779 | */ | |
2780 | BUG_ON(!hctx->nr_ctx); | |
2781 | } | |
bb94aea1 JW |
2782 | |
2783 | for (; j < HCTX_MAX_TYPES; j++) | |
2784 | ctx->hctxs[j] = blk_mq_map_queue_type(q, | |
2785 | HCTX_TYPE_DEFAULT, i); | |
320ae51f | 2786 | } |
506e931f JA |
2787 | |
2788 | queue_for_each_hw_ctx(q, hctx, i) { | |
4412efec ML |
2789 | /* |
2790 | * If no software queues are mapped to this hardware queue, | |
2791 | * disable it and free the request entries. | |
2792 | */ | |
2793 | if (!hctx->nr_ctx) { | |
2794 | /* Never unmap queue 0. We need it as a | |
2795 | * fallback in case of a new remap fails | |
2796 | * allocation | |
2797 | */ | |
2798 | if (i && set->tags[i]) | |
2799 | blk_mq_free_map_and_requests(set, i); | |
2800 | ||
2801 | hctx->tags = NULL; | |
2802 | continue; | |
2803 | } | |
484b4061 | 2804 | |
2a34c087 ML |
2805 | hctx->tags = set->tags[i]; |
2806 | WARN_ON(!hctx->tags); | |
2807 | ||
889fa31f CY |
2808 | /* |
2809 | * Set the map size to the number of mapped software queues. | |
2810 | * This is more accurate and more efficient than looping | |
2811 | * over all possibly mapped software queues. | |
2812 | */ | |
88459642 | 2813 | sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx); |
889fa31f | 2814 | |
484b4061 JA |
2815 | /* |
2816 | * Initialize batch roundrobin counts | |
2817 | */ | |
f82ddf19 | 2818 | hctx->next_cpu = blk_mq_first_mapped_cpu(hctx); |
506e931f JA |
2819 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; |
2820 | } | |
320ae51f JA |
2821 | } |
2822 | ||
8e8320c9 JA |
2823 | /* |
2824 | * Caller needs to ensure that we're either frozen/quiesced, or that | |
2825 | * the queue isn't live yet. | |
2826 | */ | |
2404e607 | 2827 | static void queue_set_hctx_shared(struct request_queue *q, bool shared) |
0d2602ca JA |
2828 | { |
2829 | struct blk_mq_hw_ctx *hctx; | |
0d2602ca JA |
2830 | int i; |
2831 | ||
2404e607 | 2832 | queue_for_each_hw_ctx(q, hctx, i) { |
97889f9a | 2833 | if (shared) |
2404e607 | 2834 | hctx->flags |= BLK_MQ_F_TAG_SHARED; |
97889f9a | 2835 | else |
2404e607 JM |
2836 | hctx->flags &= ~BLK_MQ_F_TAG_SHARED; |
2837 | } | |
2838 | } | |
2839 | ||
8e8320c9 JA |
2840 | static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set, |
2841 | bool shared) | |
2404e607 JM |
2842 | { |
2843 | struct request_queue *q; | |
0d2602ca | 2844 | |
705cda97 BVA |
2845 | lockdep_assert_held(&set->tag_list_lock); |
2846 | ||
0d2602ca JA |
2847 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
2848 | blk_mq_freeze_queue(q); | |
2404e607 | 2849 | queue_set_hctx_shared(q, shared); |
0d2602ca JA |
2850 | blk_mq_unfreeze_queue(q); |
2851 | } | |
2852 | } | |
2853 | ||
2854 | static void blk_mq_del_queue_tag_set(struct request_queue *q) | |
2855 | { | |
2856 | struct blk_mq_tag_set *set = q->tag_set; | |
2857 | ||
0d2602ca | 2858 | mutex_lock(&set->tag_list_lock); |
705cda97 | 2859 | list_del_rcu(&q->tag_set_list); |
2404e607 JM |
2860 | if (list_is_singular(&set->tag_list)) { |
2861 | /* just transitioned to unshared */ | |
2862 | set->flags &= ~BLK_MQ_F_TAG_SHARED; | |
2863 | /* update existing queue */ | |
2864 | blk_mq_update_tag_set_depth(set, false); | |
2865 | } | |
0d2602ca | 2866 | mutex_unlock(&set->tag_list_lock); |
a347c7ad | 2867 | INIT_LIST_HEAD(&q->tag_set_list); |
0d2602ca JA |
2868 | } |
2869 | ||
2870 | static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set, | |
2871 | struct request_queue *q) | |
2872 | { | |
0d2602ca | 2873 | mutex_lock(&set->tag_list_lock); |
2404e607 | 2874 | |
ff821d27 JA |
2875 | /* |
2876 | * Check to see if we're transitioning to shared (from 1 to 2 queues). | |
2877 | */ | |
2878 | if (!list_empty(&set->tag_list) && | |
2879 | !(set->flags & BLK_MQ_F_TAG_SHARED)) { | |
2404e607 JM |
2880 | set->flags |= BLK_MQ_F_TAG_SHARED; |
2881 | /* update existing queue */ | |
2882 | blk_mq_update_tag_set_depth(set, true); | |
2883 | } | |
2884 | if (set->flags & BLK_MQ_F_TAG_SHARED) | |
2885 | queue_set_hctx_shared(q, true); | |
705cda97 | 2886 | list_add_tail_rcu(&q->tag_set_list, &set->tag_list); |
2404e607 | 2887 | |
0d2602ca JA |
2888 | mutex_unlock(&set->tag_list_lock); |
2889 | } | |
2890 | ||
1db4909e ML |
2891 | /* All allocations will be freed in release handler of q->mq_kobj */ |
2892 | static int blk_mq_alloc_ctxs(struct request_queue *q) | |
2893 | { | |
2894 | struct blk_mq_ctxs *ctxs; | |
2895 | int cpu; | |
2896 | ||
2897 | ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL); | |
2898 | if (!ctxs) | |
2899 | return -ENOMEM; | |
2900 | ||
2901 | ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx); | |
2902 | if (!ctxs->queue_ctx) | |
2903 | goto fail; | |
2904 | ||
2905 | for_each_possible_cpu(cpu) { | |
2906 | struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu); | |
2907 | ctx->ctxs = ctxs; | |
2908 | } | |
2909 | ||
2910 | q->mq_kobj = &ctxs->kobj; | |
2911 | q->queue_ctx = ctxs->queue_ctx; | |
2912 | ||
2913 | return 0; | |
2914 | fail: | |
2915 | kfree(ctxs); | |
2916 | return -ENOMEM; | |
2917 | } | |
2918 | ||
e09aae7e ML |
2919 | /* |
2920 | * It is the actual release handler for mq, but we do it from | |
2921 | * request queue's release handler for avoiding use-after-free | |
2922 | * and headache because q->mq_kobj shouldn't have been introduced, | |
2923 | * but we can't group ctx/kctx kobj without it. | |
2924 | */ | |
2925 | void blk_mq_release(struct request_queue *q) | |
2926 | { | |
2f8f1336 ML |
2927 | struct blk_mq_hw_ctx *hctx, *next; |
2928 | int i; | |
e09aae7e | 2929 | |
2f8f1336 ML |
2930 | queue_for_each_hw_ctx(q, hctx, i) |
2931 | WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list)); | |
2932 | ||
2933 | /* all hctx are in .unused_hctx_list now */ | |
2934 | list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) { | |
2935 | list_del_init(&hctx->hctx_list); | |
6c8b232e | 2936 | kobject_put(&hctx->kobj); |
c3b4afca | 2937 | } |
e09aae7e ML |
2938 | |
2939 | kfree(q->queue_hw_ctx); | |
2940 | ||
7ea5fe31 ML |
2941 | /* |
2942 | * release .mq_kobj and sw queue's kobject now because | |
2943 | * both share lifetime with request queue. | |
2944 | */ | |
2945 | blk_mq_sysfs_deinit(q); | |
e09aae7e ML |
2946 | } |
2947 | ||
2f227bb9 CH |
2948 | struct request_queue *blk_mq_init_queue_data(struct blk_mq_tag_set *set, |
2949 | void *queuedata) | |
b62c21b7 MS |
2950 | { |
2951 | struct request_queue *uninit_q, *q; | |
2952 | ||
3d745ea5 | 2953 | uninit_q = __blk_alloc_queue(set->numa_node); |
b62c21b7 MS |
2954 | if (!uninit_q) |
2955 | return ERR_PTR(-ENOMEM); | |
2f227bb9 | 2956 | uninit_q->queuedata = queuedata; |
b62c21b7 | 2957 | |
737eb78e DLM |
2958 | /* |
2959 | * Initialize the queue without an elevator. device_add_disk() will do | |
2960 | * the initialization. | |
2961 | */ | |
2962 | q = blk_mq_init_allocated_queue(set, uninit_q, false); | |
b62c21b7 MS |
2963 | if (IS_ERR(q)) |
2964 | blk_cleanup_queue(uninit_q); | |
2965 | ||
2966 | return q; | |
2967 | } | |
2f227bb9 CH |
2968 | EXPORT_SYMBOL_GPL(blk_mq_init_queue_data); |
2969 | ||
2970 | struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set) | |
2971 | { | |
2972 | return blk_mq_init_queue_data(set, NULL); | |
2973 | } | |
b62c21b7 MS |
2974 | EXPORT_SYMBOL(blk_mq_init_queue); |
2975 | ||
9316a9ed JA |
2976 | /* |
2977 | * Helper for setting up a queue with mq ops, given queue depth, and | |
2978 | * the passed in mq ops flags. | |
2979 | */ | |
2980 | struct request_queue *blk_mq_init_sq_queue(struct blk_mq_tag_set *set, | |
2981 | const struct blk_mq_ops *ops, | |
2982 | unsigned int queue_depth, | |
2983 | unsigned int set_flags) | |
2984 | { | |
2985 | struct request_queue *q; | |
2986 | int ret; | |
2987 | ||
2988 | memset(set, 0, sizeof(*set)); | |
2989 | set->ops = ops; | |
2990 | set->nr_hw_queues = 1; | |
b3c661b1 | 2991 | set->nr_maps = 1; |
9316a9ed JA |
2992 | set->queue_depth = queue_depth; |
2993 | set->numa_node = NUMA_NO_NODE; | |
2994 | set->flags = set_flags; | |
2995 | ||
2996 | ret = blk_mq_alloc_tag_set(set); | |
2997 | if (ret) | |
2998 | return ERR_PTR(ret); | |
2999 | ||
3000 | q = blk_mq_init_queue(set); | |
3001 | if (IS_ERR(q)) { | |
3002 | blk_mq_free_tag_set(set); | |
3003 | return q; | |
3004 | } | |
3005 | ||
3006 | return q; | |
3007 | } | |
3008 | EXPORT_SYMBOL(blk_mq_init_sq_queue); | |
3009 | ||
34d11ffa JW |
3010 | static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx( |
3011 | struct blk_mq_tag_set *set, struct request_queue *q, | |
3012 | int hctx_idx, int node) | |
3013 | { | |
2f8f1336 | 3014 | struct blk_mq_hw_ctx *hctx = NULL, *tmp; |
34d11ffa | 3015 | |
2f8f1336 ML |
3016 | /* reuse dead hctx first */ |
3017 | spin_lock(&q->unused_hctx_lock); | |
3018 | list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) { | |
3019 | if (tmp->numa_node == node) { | |
3020 | hctx = tmp; | |
3021 | break; | |
3022 | } | |
3023 | } | |
3024 | if (hctx) | |
3025 | list_del_init(&hctx->hctx_list); | |
3026 | spin_unlock(&q->unused_hctx_lock); | |
3027 | ||
3028 | if (!hctx) | |
3029 | hctx = blk_mq_alloc_hctx(q, set, node); | |
34d11ffa | 3030 | if (!hctx) |
7c6c5b7c | 3031 | goto fail; |
34d11ffa | 3032 | |
7c6c5b7c ML |
3033 | if (blk_mq_init_hctx(q, set, hctx, hctx_idx)) |
3034 | goto free_hctx; | |
34d11ffa JW |
3035 | |
3036 | return hctx; | |
7c6c5b7c ML |
3037 | |
3038 | free_hctx: | |
3039 | kobject_put(&hctx->kobj); | |
3040 | fail: | |
3041 | return NULL; | |
34d11ffa JW |
3042 | } |
3043 | ||
868f2f0b KB |
3044 | static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set, |
3045 | struct request_queue *q) | |
320ae51f | 3046 | { |
e01ad46d | 3047 | int i, j, end; |
868f2f0b | 3048 | struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx; |
f14bbe77 | 3049 | |
ac0d6b92 BVA |
3050 | if (q->nr_hw_queues < set->nr_hw_queues) { |
3051 | struct blk_mq_hw_ctx **new_hctxs; | |
3052 | ||
3053 | new_hctxs = kcalloc_node(set->nr_hw_queues, | |
3054 | sizeof(*new_hctxs), GFP_KERNEL, | |
3055 | set->numa_node); | |
3056 | if (!new_hctxs) | |
3057 | return; | |
3058 | if (hctxs) | |
3059 | memcpy(new_hctxs, hctxs, q->nr_hw_queues * | |
3060 | sizeof(*hctxs)); | |
3061 | q->queue_hw_ctx = new_hctxs; | |
ac0d6b92 BVA |
3062 | kfree(hctxs); |
3063 | hctxs = new_hctxs; | |
3064 | } | |
3065 | ||
fb350e0a ML |
3066 | /* protect against switching io scheduler */ |
3067 | mutex_lock(&q->sysfs_lock); | |
24d2f903 | 3068 | for (i = 0; i < set->nr_hw_queues; i++) { |
868f2f0b | 3069 | int node; |
34d11ffa | 3070 | struct blk_mq_hw_ctx *hctx; |
868f2f0b | 3071 | |
7d76f856 | 3072 | node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], i); |
34d11ffa JW |
3073 | /* |
3074 | * If the hw queue has been mapped to another numa node, | |
3075 | * we need to realloc the hctx. If allocation fails, fallback | |
3076 | * to use the previous one. | |
3077 | */ | |
3078 | if (hctxs[i] && (hctxs[i]->numa_node == node)) | |
3079 | continue; | |
868f2f0b | 3080 | |
34d11ffa JW |
3081 | hctx = blk_mq_alloc_and_init_hctx(set, q, i, node); |
3082 | if (hctx) { | |
2f8f1336 | 3083 | if (hctxs[i]) |
34d11ffa | 3084 | blk_mq_exit_hctx(q, set, hctxs[i], i); |
34d11ffa JW |
3085 | hctxs[i] = hctx; |
3086 | } else { | |
3087 | if (hctxs[i]) | |
3088 | pr_warn("Allocate new hctx on node %d fails,\ | |
3089 | fallback to previous one on node %d\n", | |
3090 | node, hctxs[i]->numa_node); | |
3091 | else | |
3092 | break; | |
868f2f0b | 3093 | } |
320ae51f | 3094 | } |
e01ad46d JW |
3095 | /* |
3096 | * Increasing nr_hw_queues fails. Free the newly allocated | |
3097 | * hctxs and keep the previous q->nr_hw_queues. | |
3098 | */ | |
3099 | if (i != set->nr_hw_queues) { | |
3100 | j = q->nr_hw_queues; | |
3101 | end = i; | |
3102 | } else { | |
3103 | j = i; | |
3104 | end = q->nr_hw_queues; | |
3105 | q->nr_hw_queues = set->nr_hw_queues; | |
3106 | } | |
34d11ffa | 3107 | |
e01ad46d | 3108 | for (; j < end; j++) { |
868f2f0b KB |
3109 | struct blk_mq_hw_ctx *hctx = hctxs[j]; |
3110 | ||
3111 | if (hctx) { | |
cc71a6f4 JA |
3112 | if (hctx->tags) |
3113 | blk_mq_free_map_and_requests(set, j); | |
868f2f0b | 3114 | blk_mq_exit_hctx(q, set, hctx, j); |
868f2f0b | 3115 | hctxs[j] = NULL; |
868f2f0b KB |
3116 | } |
3117 | } | |
fb350e0a | 3118 | mutex_unlock(&q->sysfs_lock); |
868f2f0b KB |
3119 | } |
3120 | ||
3121 | struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set, | |
737eb78e DLM |
3122 | struct request_queue *q, |
3123 | bool elevator_init) | |
868f2f0b | 3124 | { |
66841672 ML |
3125 | /* mark the queue as mq asap */ |
3126 | q->mq_ops = set->ops; | |
3127 | ||
34dbad5d | 3128 | q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn, |
720b8ccc SB |
3129 | blk_mq_poll_stats_bkt, |
3130 | BLK_MQ_POLL_STATS_BKTS, q); | |
34dbad5d OS |
3131 | if (!q->poll_cb) |
3132 | goto err_exit; | |
3133 | ||
1db4909e | 3134 | if (blk_mq_alloc_ctxs(q)) |
41de54c6 | 3135 | goto err_poll; |
868f2f0b | 3136 | |
737f98cf ML |
3137 | /* init q->mq_kobj and sw queues' kobjects */ |
3138 | blk_mq_sysfs_init(q); | |
3139 | ||
2f8f1336 ML |
3140 | INIT_LIST_HEAD(&q->unused_hctx_list); |
3141 | spin_lock_init(&q->unused_hctx_lock); | |
3142 | ||
868f2f0b KB |
3143 | blk_mq_realloc_hw_ctxs(set, q); |
3144 | if (!q->nr_hw_queues) | |
3145 | goto err_hctxs; | |
320ae51f | 3146 | |
287922eb | 3147 | INIT_WORK(&q->timeout_work, blk_mq_timeout_work); |
e56f698b | 3148 | blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ); |
320ae51f | 3149 | |
a8908939 | 3150 | q->tag_set = set; |
320ae51f | 3151 | |
94eddfbe | 3152 | q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT; |
cd19181b ML |
3153 | if (set->nr_maps > HCTX_TYPE_POLL && |
3154 | set->map[HCTX_TYPE_POLL].nr_queues) | |
6544d229 | 3155 | blk_queue_flag_set(QUEUE_FLAG_POLL, q); |
320ae51f | 3156 | |
1be036e9 CH |
3157 | q->sg_reserved_size = INT_MAX; |
3158 | ||
2849450a | 3159 | INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work); |
6fca6a61 CH |
3160 | INIT_LIST_HEAD(&q->requeue_list); |
3161 | spin_lock_init(&q->requeue_lock); | |
3162 | ||
eba71768 JA |
3163 | q->nr_requests = set->queue_depth; |
3164 | ||
64f1c21e JA |
3165 | /* |
3166 | * Default to classic polling | |
3167 | */ | |
29ece8b4 | 3168 | q->poll_nsec = BLK_MQ_POLL_CLASSIC; |
64f1c21e | 3169 | |
24d2f903 | 3170 | blk_mq_init_cpu_queues(q, set->nr_hw_queues); |
0d2602ca | 3171 | blk_mq_add_queue_tag_set(set, q); |
4b855ad3 | 3172 | blk_mq_map_swqueue(q); |
4593fdbe | 3173 | |
737eb78e DLM |
3174 | if (elevator_init) |
3175 | elevator_init_mq(q); | |
d3484991 | 3176 | |
320ae51f | 3177 | return q; |
18741986 | 3178 | |
320ae51f | 3179 | err_hctxs: |
868f2f0b | 3180 | kfree(q->queue_hw_ctx); |
73d9c8d4 | 3181 | q->nr_hw_queues = 0; |
1db4909e | 3182 | blk_mq_sysfs_deinit(q); |
41de54c6 JS |
3183 | err_poll: |
3184 | blk_stat_free_callback(q->poll_cb); | |
3185 | q->poll_cb = NULL; | |
c7de5726 ML |
3186 | err_exit: |
3187 | q->mq_ops = NULL; | |
320ae51f JA |
3188 | return ERR_PTR(-ENOMEM); |
3189 | } | |
b62c21b7 | 3190 | EXPORT_SYMBOL(blk_mq_init_allocated_queue); |
320ae51f | 3191 | |
c7e2d94b ML |
3192 | /* tags can _not_ be used after returning from blk_mq_exit_queue */ |
3193 | void blk_mq_exit_queue(struct request_queue *q) | |
320ae51f | 3194 | { |
624dbe47 | 3195 | struct blk_mq_tag_set *set = q->tag_set; |
320ae51f | 3196 | |
0d2602ca | 3197 | blk_mq_del_queue_tag_set(q); |
624dbe47 | 3198 | blk_mq_exit_hw_queues(q, set, set->nr_hw_queues); |
320ae51f | 3199 | } |
320ae51f | 3200 | |
a5164405 JA |
3201 | static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set) |
3202 | { | |
3203 | int i; | |
3204 | ||
cc71a6f4 | 3205 | for (i = 0; i < set->nr_hw_queues; i++) |
03b63b02 | 3206 | if (!__blk_mq_alloc_map_and_request(set, i)) |
a5164405 | 3207 | goto out_unwind; |
a5164405 JA |
3208 | |
3209 | return 0; | |
3210 | ||
3211 | out_unwind: | |
3212 | while (--i >= 0) | |
2e194422 | 3213 | blk_mq_free_map_and_requests(set, i); |
a5164405 | 3214 | |
a5164405 JA |
3215 | return -ENOMEM; |
3216 | } | |
3217 | ||
3218 | /* | |
3219 | * Allocate the request maps associated with this tag_set. Note that this | |
3220 | * may reduce the depth asked for, if memory is tight. set->queue_depth | |
3221 | * will be updated to reflect the allocated depth. | |
3222 | */ | |
79fab528 | 3223 | static int blk_mq_alloc_map_and_requests(struct blk_mq_tag_set *set) |
a5164405 JA |
3224 | { |
3225 | unsigned int depth; | |
3226 | int err; | |
3227 | ||
3228 | depth = set->queue_depth; | |
3229 | do { | |
3230 | err = __blk_mq_alloc_rq_maps(set); | |
3231 | if (!err) | |
3232 | break; | |
3233 | ||
3234 | set->queue_depth >>= 1; | |
3235 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) { | |
3236 | err = -ENOMEM; | |
3237 | break; | |
3238 | } | |
3239 | } while (set->queue_depth); | |
3240 | ||
3241 | if (!set->queue_depth || err) { | |
3242 | pr_err("blk-mq: failed to allocate request map\n"); | |
3243 | return -ENOMEM; | |
3244 | } | |
3245 | ||
3246 | if (depth != set->queue_depth) | |
3247 | pr_info("blk-mq: reduced tag depth (%u -> %u)\n", | |
3248 | depth, set->queue_depth); | |
3249 | ||
3250 | return 0; | |
3251 | } | |
3252 | ||
ebe8bddb OS |
3253 | static int blk_mq_update_queue_map(struct blk_mq_tag_set *set) |
3254 | { | |
6e66b493 BVA |
3255 | /* |
3256 | * blk_mq_map_queues() and multiple .map_queues() implementations | |
3257 | * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the | |
3258 | * number of hardware queues. | |
3259 | */ | |
3260 | if (set->nr_maps == 1) | |
3261 | set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues; | |
3262 | ||
59388702 | 3263 | if (set->ops->map_queues && !is_kdump_kernel()) { |
b3c661b1 JA |
3264 | int i; |
3265 | ||
7d4901a9 ML |
3266 | /* |
3267 | * transport .map_queues is usually done in the following | |
3268 | * way: | |
3269 | * | |
3270 | * for (queue = 0; queue < set->nr_hw_queues; queue++) { | |
3271 | * mask = get_cpu_mask(queue) | |
3272 | * for_each_cpu(cpu, mask) | |
b3c661b1 | 3273 | * set->map[x].mq_map[cpu] = queue; |
7d4901a9 ML |
3274 | * } |
3275 | * | |
3276 | * When we need to remap, the table has to be cleared for | |
3277 | * killing stale mapping since one CPU may not be mapped | |
3278 | * to any hw queue. | |
3279 | */ | |
b3c661b1 JA |
3280 | for (i = 0; i < set->nr_maps; i++) |
3281 | blk_mq_clear_mq_map(&set->map[i]); | |
7d4901a9 | 3282 | |
ebe8bddb | 3283 | return set->ops->map_queues(set); |
b3c661b1 JA |
3284 | } else { |
3285 | BUG_ON(set->nr_maps > 1); | |
7d76f856 | 3286 | return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]); |
b3c661b1 | 3287 | } |
ebe8bddb OS |
3288 | } |
3289 | ||
f7e76dbc BVA |
3290 | static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set, |
3291 | int cur_nr_hw_queues, int new_nr_hw_queues) | |
3292 | { | |
3293 | struct blk_mq_tags **new_tags; | |
3294 | ||
3295 | if (cur_nr_hw_queues >= new_nr_hw_queues) | |
3296 | return 0; | |
3297 | ||
3298 | new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *), | |
3299 | GFP_KERNEL, set->numa_node); | |
3300 | if (!new_tags) | |
3301 | return -ENOMEM; | |
3302 | ||
3303 | if (set->tags) | |
3304 | memcpy(new_tags, set->tags, cur_nr_hw_queues * | |
3305 | sizeof(*set->tags)); | |
3306 | kfree(set->tags); | |
3307 | set->tags = new_tags; | |
3308 | set->nr_hw_queues = new_nr_hw_queues; | |
3309 | ||
3310 | return 0; | |
3311 | } | |
3312 | ||
a4391c64 JA |
3313 | /* |
3314 | * Alloc a tag set to be associated with one or more request queues. | |
3315 | * May fail with EINVAL for various error conditions. May adjust the | |
c018c84f | 3316 | * requested depth down, if it's too large. In that case, the set |
a4391c64 JA |
3317 | * value will be stored in set->queue_depth. |
3318 | */ | |
24d2f903 CH |
3319 | int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) |
3320 | { | |
b3c661b1 | 3321 | int i, ret; |
da695ba2 | 3322 | |
205fb5f5 BVA |
3323 | BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS); |
3324 | ||
24d2f903 CH |
3325 | if (!set->nr_hw_queues) |
3326 | return -EINVAL; | |
a4391c64 | 3327 | if (!set->queue_depth) |
24d2f903 CH |
3328 | return -EINVAL; |
3329 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) | |
3330 | return -EINVAL; | |
3331 | ||
7d7e0f90 | 3332 | if (!set->ops->queue_rq) |
24d2f903 CH |
3333 | return -EINVAL; |
3334 | ||
de148297 ML |
3335 | if (!set->ops->get_budget ^ !set->ops->put_budget) |
3336 | return -EINVAL; | |
3337 | ||
a4391c64 JA |
3338 | if (set->queue_depth > BLK_MQ_MAX_DEPTH) { |
3339 | pr_info("blk-mq: reduced tag depth to %u\n", | |
3340 | BLK_MQ_MAX_DEPTH); | |
3341 | set->queue_depth = BLK_MQ_MAX_DEPTH; | |
3342 | } | |
24d2f903 | 3343 | |
b3c661b1 JA |
3344 | if (!set->nr_maps) |
3345 | set->nr_maps = 1; | |
3346 | else if (set->nr_maps > HCTX_MAX_TYPES) | |
3347 | return -EINVAL; | |
3348 | ||
6637fadf SL |
3349 | /* |
3350 | * If a crashdump is active, then we are potentially in a very | |
3351 | * memory constrained environment. Limit us to 1 queue and | |
3352 | * 64 tags to prevent using too much memory. | |
3353 | */ | |
3354 | if (is_kdump_kernel()) { | |
3355 | set->nr_hw_queues = 1; | |
59388702 | 3356 | set->nr_maps = 1; |
6637fadf SL |
3357 | set->queue_depth = min(64U, set->queue_depth); |
3358 | } | |
868f2f0b | 3359 | /* |
392546ae JA |
3360 | * There is no use for more h/w queues than cpus if we just have |
3361 | * a single map | |
868f2f0b | 3362 | */ |
392546ae | 3363 | if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids) |
868f2f0b | 3364 | set->nr_hw_queues = nr_cpu_ids; |
6637fadf | 3365 | |
f7e76dbc | 3366 | if (blk_mq_realloc_tag_set_tags(set, 0, set->nr_hw_queues) < 0) |
a5164405 | 3367 | return -ENOMEM; |
24d2f903 | 3368 | |
da695ba2 | 3369 | ret = -ENOMEM; |
b3c661b1 JA |
3370 | for (i = 0; i < set->nr_maps; i++) { |
3371 | set->map[i].mq_map = kcalloc_node(nr_cpu_ids, | |
07b35eb5 | 3372 | sizeof(set->map[i].mq_map[0]), |
b3c661b1 JA |
3373 | GFP_KERNEL, set->numa_node); |
3374 | if (!set->map[i].mq_map) | |
3375 | goto out_free_mq_map; | |
59388702 | 3376 | set->map[i].nr_queues = is_kdump_kernel() ? 1 : set->nr_hw_queues; |
b3c661b1 | 3377 | } |
bdd17e75 | 3378 | |
ebe8bddb | 3379 | ret = blk_mq_update_queue_map(set); |
da695ba2 CH |
3380 | if (ret) |
3381 | goto out_free_mq_map; | |
3382 | ||
79fab528 | 3383 | ret = blk_mq_alloc_map_and_requests(set); |
da695ba2 | 3384 | if (ret) |
bdd17e75 | 3385 | goto out_free_mq_map; |
24d2f903 | 3386 | |
0d2602ca JA |
3387 | mutex_init(&set->tag_list_lock); |
3388 | INIT_LIST_HEAD(&set->tag_list); | |
3389 | ||
24d2f903 | 3390 | return 0; |
bdd17e75 CH |
3391 | |
3392 | out_free_mq_map: | |
b3c661b1 JA |
3393 | for (i = 0; i < set->nr_maps; i++) { |
3394 | kfree(set->map[i].mq_map); | |
3395 | set->map[i].mq_map = NULL; | |
3396 | } | |
5676e7b6 RE |
3397 | kfree(set->tags); |
3398 | set->tags = NULL; | |
da695ba2 | 3399 | return ret; |
24d2f903 CH |
3400 | } |
3401 | EXPORT_SYMBOL(blk_mq_alloc_tag_set); | |
3402 | ||
3403 | void blk_mq_free_tag_set(struct blk_mq_tag_set *set) | |
3404 | { | |
b3c661b1 | 3405 | int i, j; |
24d2f903 | 3406 | |
f7e76dbc | 3407 | for (i = 0; i < set->nr_hw_queues; i++) |
cc71a6f4 | 3408 | blk_mq_free_map_and_requests(set, i); |
484b4061 | 3409 | |
b3c661b1 JA |
3410 | for (j = 0; j < set->nr_maps; j++) { |
3411 | kfree(set->map[j].mq_map); | |
3412 | set->map[j].mq_map = NULL; | |
3413 | } | |
bdd17e75 | 3414 | |
981bd189 | 3415 | kfree(set->tags); |
5676e7b6 | 3416 | set->tags = NULL; |
24d2f903 CH |
3417 | } |
3418 | EXPORT_SYMBOL(blk_mq_free_tag_set); | |
3419 | ||
e3a2b3f9 JA |
3420 | int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) |
3421 | { | |
3422 | struct blk_mq_tag_set *set = q->tag_set; | |
3423 | struct blk_mq_hw_ctx *hctx; | |
3424 | int i, ret; | |
3425 | ||
bd166ef1 | 3426 | if (!set) |
e3a2b3f9 JA |
3427 | return -EINVAL; |
3428 | ||
e5fa8140 AZ |
3429 | if (q->nr_requests == nr) |
3430 | return 0; | |
3431 | ||
70f36b60 | 3432 | blk_mq_freeze_queue(q); |
24f5a90f | 3433 | blk_mq_quiesce_queue(q); |
70f36b60 | 3434 | |
e3a2b3f9 JA |
3435 | ret = 0; |
3436 | queue_for_each_hw_ctx(q, hctx, i) { | |
e9137d4b KB |
3437 | if (!hctx->tags) |
3438 | continue; | |
bd166ef1 JA |
3439 | /* |
3440 | * If we're using an MQ scheduler, just update the scheduler | |
3441 | * queue depth. This is similar to what the old code would do. | |
3442 | */ | |
70f36b60 | 3443 | if (!hctx->sched_tags) { |
c2e82a23 | 3444 | ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr, |
70f36b60 JA |
3445 | false); |
3446 | } else { | |
3447 | ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags, | |
3448 | nr, true); | |
3449 | } | |
e3a2b3f9 JA |
3450 | if (ret) |
3451 | break; | |
77f1e0a5 JA |
3452 | if (q->elevator && q->elevator->type->ops.depth_updated) |
3453 | q->elevator->type->ops.depth_updated(hctx); | |
e3a2b3f9 JA |
3454 | } |
3455 | ||
3456 | if (!ret) | |
3457 | q->nr_requests = nr; | |
3458 | ||
24f5a90f | 3459 | blk_mq_unquiesce_queue(q); |
70f36b60 | 3460 | blk_mq_unfreeze_queue(q); |
70f36b60 | 3461 | |
e3a2b3f9 JA |
3462 | return ret; |
3463 | } | |
3464 | ||
d48ece20 JW |
3465 | /* |
3466 | * request_queue and elevator_type pair. | |
3467 | * It is just used by __blk_mq_update_nr_hw_queues to cache | |
3468 | * the elevator_type associated with a request_queue. | |
3469 | */ | |
3470 | struct blk_mq_qe_pair { | |
3471 | struct list_head node; | |
3472 | struct request_queue *q; | |
3473 | struct elevator_type *type; | |
3474 | }; | |
3475 | ||
3476 | /* | |
3477 | * Cache the elevator_type in qe pair list and switch the | |
3478 | * io scheduler to 'none' | |
3479 | */ | |
3480 | static bool blk_mq_elv_switch_none(struct list_head *head, | |
3481 | struct request_queue *q) | |
3482 | { | |
3483 | struct blk_mq_qe_pair *qe; | |
3484 | ||
3485 | if (!q->elevator) | |
3486 | return true; | |
3487 | ||
3488 | qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY); | |
3489 | if (!qe) | |
3490 | return false; | |
3491 | ||
3492 | INIT_LIST_HEAD(&qe->node); | |
3493 | qe->q = q; | |
3494 | qe->type = q->elevator->type; | |
3495 | list_add(&qe->node, head); | |
3496 | ||
3497 | mutex_lock(&q->sysfs_lock); | |
3498 | /* | |
3499 | * After elevator_switch_mq, the previous elevator_queue will be | |
3500 | * released by elevator_release. The reference of the io scheduler | |
3501 | * module get by elevator_get will also be put. So we need to get | |
3502 | * a reference of the io scheduler module here to prevent it to be | |
3503 | * removed. | |
3504 | */ | |
3505 | __module_get(qe->type->elevator_owner); | |
3506 | elevator_switch_mq(q, NULL); | |
3507 | mutex_unlock(&q->sysfs_lock); | |
3508 | ||
3509 | return true; | |
3510 | } | |
3511 | ||
3512 | static void blk_mq_elv_switch_back(struct list_head *head, | |
3513 | struct request_queue *q) | |
3514 | { | |
3515 | struct blk_mq_qe_pair *qe; | |
3516 | struct elevator_type *t = NULL; | |
3517 | ||
3518 | list_for_each_entry(qe, head, node) | |
3519 | if (qe->q == q) { | |
3520 | t = qe->type; | |
3521 | break; | |
3522 | } | |
3523 | ||
3524 | if (!t) | |
3525 | return; | |
3526 | ||
3527 | list_del(&qe->node); | |
3528 | kfree(qe); | |
3529 | ||
3530 | mutex_lock(&q->sysfs_lock); | |
3531 | elevator_switch_mq(q, t); | |
3532 | mutex_unlock(&q->sysfs_lock); | |
3533 | } | |
3534 | ||
e4dc2b32 KB |
3535 | static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, |
3536 | int nr_hw_queues) | |
868f2f0b KB |
3537 | { |
3538 | struct request_queue *q; | |
d48ece20 | 3539 | LIST_HEAD(head); |
e01ad46d | 3540 | int prev_nr_hw_queues; |
868f2f0b | 3541 | |
705cda97 BVA |
3542 | lockdep_assert_held(&set->tag_list_lock); |
3543 | ||
392546ae | 3544 | if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids) |
868f2f0b | 3545 | nr_hw_queues = nr_cpu_ids; |
fe35ec58 WZ |
3546 | if (nr_hw_queues < 1) |
3547 | return; | |
3548 | if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues) | |
868f2f0b KB |
3549 | return; |
3550 | ||
3551 | list_for_each_entry(q, &set->tag_list, tag_set_list) | |
3552 | blk_mq_freeze_queue(q); | |
d48ece20 JW |
3553 | /* |
3554 | * Switch IO scheduler to 'none', cleaning up the data associated | |
3555 | * with the previous scheduler. We will switch back once we are done | |
3556 | * updating the new sw to hw queue mappings. | |
3557 | */ | |
3558 | list_for_each_entry(q, &set->tag_list, tag_set_list) | |
3559 | if (!blk_mq_elv_switch_none(&head, q)) | |
3560 | goto switch_back; | |
868f2f0b | 3561 | |
477e19de JW |
3562 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
3563 | blk_mq_debugfs_unregister_hctxs(q); | |
3564 | blk_mq_sysfs_unregister(q); | |
3565 | } | |
3566 | ||
a2584e43 | 3567 | prev_nr_hw_queues = set->nr_hw_queues; |
f7e76dbc BVA |
3568 | if (blk_mq_realloc_tag_set_tags(set, set->nr_hw_queues, nr_hw_queues) < |
3569 | 0) | |
3570 | goto reregister; | |
3571 | ||
868f2f0b | 3572 | set->nr_hw_queues = nr_hw_queues; |
e01ad46d | 3573 | fallback: |
aa880ad6 | 3574 | blk_mq_update_queue_map(set); |
868f2f0b KB |
3575 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
3576 | blk_mq_realloc_hw_ctxs(set, q); | |
e01ad46d JW |
3577 | if (q->nr_hw_queues != set->nr_hw_queues) { |
3578 | pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n", | |
3579 | nr_hw_queues, prev_nr_hw_queues); | |
3580 | set->nr_hw_queues = prev_nr_hw_queues; | |
7d76f856 | 3581 | blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]); |
e01ad46d JW |
3582 | goto fallback; |
3583 | } | |
477e19de JW |
3584 | blk_mq_map_swqueue(q); |
3585 | } | |
3586 | ||
f7e76dbc | 3587 | reregister: |
477e19de JW |
3588 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
3589 | blk_mq_sysfs_register(q); | |
3590 | blk_mq_debugfs_register_hctxs(q); | |
868f2f0b KB |
3591 | } |
3592 | ||
d48ece20 JW |
3593 | switch_back: |
3594 | list_for_each_entry(q, &set->tag_list, tag_set_list) | |
3595 | blk_mq_elv_switch_back(&head, q); | |
3596 | ||
868f2f0b KB |
3597 | list_for_each_entry(q, &set->tag_list, tag_set_list) |
3598 | blk_mq_unfreeze_queue(q); | |
3599 | } | |
e4dc2b32 KB |
3600 | |
3601 | void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues) | |
3602 | { | |
3603 | mutex_lock(&set->tag_list_lock); | |
3604 | __blk_mq_update_nr_hw_queues(set, nr_hw_queues); | |
3605 | mutex_unlock(&set->tag_list_lock); | |
3606 | } | |
868f2f0b KB |
3607 | EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues); |
3608 | ||
34dbad5d OS |
3609 | /* Enable polling stats and return whether they were already enabled. */ |
3610 | static bool blk_poll_stats_enable(struct request_queue *q) | |
3611 | { | |
3612 | if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) || | |
7dfdbc73 | 3613 | blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q)) |
34dbad5d OS |
3614 | return true; |
3615 | blk_stat_add_callback(q, q->poll_cb); | |
3616 | return false; | |
3617 | } | |
3618 | ||
3619 | static void blk_mq_poll_stats_start(struct request_queue *q) | |
3620 | { | |
3621 | /* | |
3622 | * We don't arm the callback if polling stats are not enabled or the | |
3623 | * callback is already active. | |
3624 | */ | |
3625 | if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) || | |
3626 | blk_stat_is_active(q->poll_cb)) | |
3627 | return; | |
3628 | ||
3629 | blk_stat_activate_msecs(q->poll_cb, 100); | |
3630 | } | |
3631 | ||
3632 | static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb) | |
3633 | { | |
3634 | struct request_queue *q = cb->data; | |
720b8ccc | 3635 | int bucket; |
34dbad5d | 3636 | |
720b8ccc SB |
3637 | for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) { |
3638 | if (cb->stat[bucket].nr_samples) | |
3639 | q->poll_stat[bucket] = cb->stat[bucket]; | |
3640 | } | |
34dbad5d OS |
3641 | } |
3642 | ||
64f1c21e | 3643 | static unsigned long blk_mq_poll_nsecs(struct request_queue *q, |
64f1c21e JA |
3644 | struct request *rq) |
3645 | { | |
64f1c21e | 3646 | unsigned long ret = 0; |
720b8ccc | 3647 | int bucket; |
64f1c21e JA |
3648 | |
3649 | /* | |
3650 | * If stats collection isn't on, don't sleep but turn it on for | |
3651 | * future users | |
3652 | */ | |
34dbad5d | 3653 | if (!blk_poll_stats_enable(q)) |
64f1c21e JA |
3654 | return 0; |
3655 | ||
64f1c21e JA |
3656 | /* |
3657 | * As an optimistic guess, use half of the mean service time | |
3658 | * for this type of request. We can (and should) make this smarter. | |
3659 | * For instance, if the completion latencies are tight, we can | |
3660 | * get closer than just half the mean. This is especially | |
3661 | * important on devices where the completion latencies are longer | |
720b8ccc SB |
3662 | * than ~10 usec. We do use the stats for the relevant IO size |
3663 | * if available which does lead to better estimates. | |
64f1c21e | 3664 | */ |
720b8ccc SB |
3665 | bucket = blk_mq_poll_stats_bkt(rq); |
3666 | if (bucket < 0) | |
3667 | return ret; | |
3668 | ||
3669 | if (q->poll_stat[bucket].nr_samples) | |
3670 | ret = (q->poll_stat[bucket].mean + 1) / 2; | |
64f1c21e JA |
3671 | |
3672 | return ret; | |
3673 | } | |
3674 | ||
06426adf JA |
3675 | static bool blk_mq_poll_hybrid_sleep(struct request_queue *q, |
3676 | struct request *rq) | |
3677 | { | |
3678 | struct hrtimer_sleeper hs; | |
3679 | enum hrtimer_mode mode; | |
64f1c21e | 3680 | unsigned int nsecs; |
06426adf JA |
3681 | ktime_t kt; |
3682 | ||
76a86f9d | 3683 | if (rq->rq_flags & RQF_MQ_POLL_SLEPT) |
64f1c21e JA |
3684 | return false; |
3685 | ||
3686 | /* | |
1052b8ac | 3687 | * If we get here, hybrid polling is enabled. Hence poll_nsec can be: |
64f1c21e | 3688 | * |
64f1c21e JA |
3689 | * 0: use half of prev avg |
3690 | * >0: use this specific value | |
3691 | */ | |
1052b8ac | 3692 | if (q->poll_nsec > 0) |
64f1c21e JA |
3693 | nsecs = q->poll_nsec; |
3694 | else | |
cae740a0 | 3695 | nsecs = blk_mq_poll_nsecs(q, rq); |
64f1c21e JA |
3696 | |
3697 | if (!nsecs) | |
06426adf JA |
3698 | return false; |
3699 | ||
76a86f9d | 3700 | rq->rq_flags |= RQF_MQ_POLL_SLEPT; |
06426adf JA |
3701 | |
3702 | /* | |
3703 | * This will be replaced with the stats tracking code, using | |
3704 | * 'avg_completion_time / 2' as the pre-sleep target. | |
3705 | */ | |
8b0e1953 | 3706 | kt = nsecs; |
06426adf JA |
3707 | |
3708 | mode = HRTIMER_MODE_REL; | |
dbc1625f | 3709 | hrtimer_init_sleeper_on_stack(&hs, CLOCK_MONOTONIC, mode); |
06426adf JA |
3710 | hrtimer_set_expires(&hs.timer, kt); |
3711 | ||
06426adf | 3712 | do { |
5a61c363 | 3713 | if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE) |
06426adf JA |
3714 | break; |
3715 | set_current_state(TASK_UNINTERRUPTIBLE); | |
9dd8813e | 3716 | hrtimer_sleeper_start_expires(&hs, mode); |
06426adf JA |
3717 | if (hs.task) |
3718 | io_schedule(); | |
3719 | hrtimer_cancel(&hs.timer); | |
3720 | mode = HRTIMER_MODE_ABS; | |
3721 | } while (hs.task && !signal_pending(current)); | |
3722 | ||
3723 | __set_current_state(TASK_RUNNING); | |
3724 | destroy_hrtimer_on_stack(&hs.timer); | |
3725 | return true; | |
3726 | } | |
3727 | ||
1052b8ac JA |
3728 | static bool blk_mq_poll_hybrid(struct request_queue *q, |
3729 | struct blk_mq_hw_ctx *hctx, blk_qc_t cookie) | |
bbd7bb70 | 3730 | { |
1052b8ac JA |
3731 | struct request *rq; |
3732 | ||
29ece8b4 | 3733 | if (q->poll_nsec == BLK_MQ_POLL_CLASSIC) |
1052b8ac JA |
3734 | return false; |
3735 | ||
3736 | if (!blk_qc_t_is_internal(cookie)) | |
3737 | rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie)); | |
3738 | else { | |
3739 | rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie)); | |
3740 | /* | |
3741 | * With scheduling, if the request has completed, we'll | |
3742 | * get a NULL return here, as we clear the sched tag when | |
3743 | * that happens. The request still remains valid, like always, | |
3744 | * so we should be safe with just the NULL check. | |
3745 | */ | |
3746 | if (!rq) | |
3747 | return false; | |
3748 | } | |
3749 | ||
cae740a0 | 3750 | return blk_mq_poll_hybrid_sleep(q, rq); |
1052b8ac JA |
3751 | } |
3752 | ||
529262d5 CH |
3753 | /** |
3754 | * blk_poll - poll for IO completions | |
3755 | * @q: the queue | |
3756 | * @cookie: cookie passed back at IO submission time | |
3757 | * @spin: whether to spin for completions | |
3758 | * | |
3759 | * Description: | |
3760 | * Poll for completions on the passed in queue. Returns number of | |
3761 | * completed entries found. If @spin is true, then blk_poll will continue | |
3762 | * looping until at least one completion is found, unless the task is | |
3763 | * otherwise marked running (or we need to reschedule). | |
3764 | */ | |
3765 | int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin) | |
1052b8ac JA |
3766 | { |
3767 | struct blk_mq_hw_ctx *hctx; | |
bbd7bb70 JA |
3768 | long state; |
3769 | ||
529262d5 CH |
3770 | if (!blk_qc_t_valid(cookie) || |
3771 | !test_bit(QUEUE_FLAG_POLL, &q->queue_flags)) | |
1052b8ac JA |
3772 | return 0; |
3773 | ||
529262d5 CH |
3774 | if (current->plug) |
3775 | blk_flush_plug_list(current->plug, false); | |
3776 | ||
1052b8ac JA |
3777 | hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)]; |
3778 | ||
06426adf JA |
3779 | /* |
3780 | * If we sleep, have the caller restart the poll loop to reset | |
3781 | * the state. Like for the other success return cases, the | |
3782 | * caller is responsible for checking if the IO completed. If | |
3783 | * the IO isn't complete, we'll get called again and will go | |
3784 | * straight to the busy poll loop. | |
3785 | */ | |
1052b8ac | 3786 | if (blk_mq_poll_hybrid(q, hctx, cookie)) |
85f4d4b6 | 3787 | return 1; |
06426adf | 3788 | |
bbd7bb70 JA |
3789 | hctx->poll_considered++; |
3790 | ||
3791 | state = current->state; | |
aa61bec3 | 3792 | do { |
bbd7bb70 JA |
3793 | int ret; |
3794 | ||
3795 | hctx->poll_invoked++; | |
3796 | ||
9743139c | 3797 | ret = q->mq_ops->poll(hctx); |
bbd7bb70 JA |
3798 | if (ret > 0) { |
3799 | hctx->poll_success++; | |
849a3700 | 3800 | __set_current_state(TASK_RUNNING); |
85f4d4b6 | 3801 | return ret; |
bbd7bb70 JA |
3802 | } |
3803 | ||
3804 | if (signal_pending_state(state, current)) | |
849a3700 | 3805 | __set_current_state(TASK_RUNNING); |
bbd7bb70 JA |
3806 | |
3807 | if (current->state == TASK_RUNNING) | |
85f4d4b6 | 3808 | return 1; |
0a1b8b87 | 3809 | if (ret < 0 || !spin) |
bbd7bb70 JA |
3810 | break; |
3811 | cpu_relax(); | |
aa61bec3 | 3812 | } while (!need_resched()); |
bbd7bb70 | 3813 | |
67b4110f | 3814 | __set_current_state(TASK_RUNNING); |
85f4d4b6 | 3815 | return 0; |
bbd7bb70 | 3816 | } |
529262d5 | 3817 | EXPORT_SYMBOL_GPL(blk_poll); |
bbd7bb70 | 3818 | |
9cf2bab6 JA |
3819 | unsigned int blk_mq_rq_cpu(struct request *rq) |
3820 | { | |
3821 | return rq->mq_ctx->cpu; | |
3822 | } | |
3823 | EXPORT_SYMBOL(blk_mq_rq_cpu); | |
3824 | ||
320ae51f JA |
3825 | static int __init blk_mq_init(void) |
3826 | { | |
c3077b5d CH |
3827 | int i; |
3828 | ||
3829 | for_each_possible_cpu(i) | |
3830 | INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i)); | |
3831 | open_softirq(BLOCK_SOFTIRQ, blk_done_softirq); | |
3832 | ||
3833 | cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD, | |
3834 | "block/softirq:dead", NULL, | |
3835 | blk_softirq_cpu_dead); | |
9467f859 TG |
3836 | cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL, |
3837 | blk_mq_hctx_notify_dead); | |
bf0beec0 ML |
3838 | cpuhp_setup_state_multi(CPUHP_AP_BLK_MQ_ONLINE, "block/mq:online", |
3839 | blk_mq_hctx_notify_online, | |
3840 | blk_mq_hctx_notify_offline); | |
320ae51f JA |
3841 | return 0; |
3842 | } | |
3843 | subsys_initcall(blk_mq_init); |