]>
Commit | Line | Data |
---|---|---|
75bb4625 JA |
1 | /* |
2 | * Block multiqueue core code | |
3 | * | |
4 | * Copyright (C) 2013-2014 Jens Axboe | |
5 | * Copyright (C) 2013-2014 Christoph Hellwig | |
6 | */ | |
320ae51f JA |
7 | #include <linux/kernel.h> |
8 | #include <linux/module.h> | |
9 | #include <linux/backing-dev.h> | |
10 | #include <linux/bio.h> | |
11 | #include <linux/blkdev.h> | |
f75782e4 | 12 | #include <linux/kmemleak.h> |
320ae51f JA |
13 | #include <linux/mm.h> |
14 | #include <linux/init.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/workqueue.h> | |
17 | #include <linux/smp.h> | |
18 | #include <linux/llist.h> | |
19 | #include <linux/list_sort.h> | |
20 | #include <linux/cpu.h> | |
21 | #include <linux/cache.h> | |
22 | #include <linux/sched/sysctl.h> | |
105ab3d8 | 23 | #include <linux/sched/topology.h> |
174cd4b1 | 24 | #include <linux/sched/signal.h> |
320ae51f | 25 | #include <linux/delay.h> |
aedcd72f | 26 | #include <linux/crash_dump.h> |
88c7b2b7 | 27 | #include <linux/prefetch.h> |
320ae51f JA |
28 | |
29 | #include <trace/events/block.h> | |
30 | ||
31 | #include <linux/blk-mq.h> | |
32 | #include "blk.h" | |
33 | #include "blk-mq.h" | |
9c1051aa | 34 | #include "blk-mq-debugfs.h" |
320ae51f | 35 | #include "blk-mq-tag.h" |
cf43e6be | 36 | #include "blk-stat.h" |
87760e5e | 37 | #include "blk-wbt.h" |
bd166ef1 | 38 | #include "blk-mq-sched.h" |
320ae51f | 39 | |
34dbad5d OS |
40 | static void blk_mq_poll_stats_start(struct request_queue *q); |
41 | static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb); | |
42 | ||
720b8ccc SB |
43 | static int blk_mq_poll_stats_bkt(const struct request *rq) |
44 | { | |
45 | int ddir, bytes, bucket; | |
46 | ||
99c749a4 | 47 | ddir = rq_data_dir(rq); |
720b8ccc SB |
48 | bytes = blk_rq_bytes(rq); |
49 | ||
50 | bucket = ddir + 2*(ilog2(bytes) - 9); | |
51 | ||
52 | if (bucket < 0) | |
53 | return -1; | |
54 | else if (bucket >= BLK_MQ_POLL_STATS_BKTS) | |
55 | return ddir + BLK_MQ_POLL_STATS_BKTS - 2; | |
56 | ||
57 | return bucket; | |
58 | } | |
59 | ||
320ae51f JA |
60 | /* |
61 | * Check if any of the ctx's have pending work in this hardware queue | |
62 | */ | |
50e1dab8 | 63 | bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx) |
320ae51f | 64 | { |
bd166ef1 JA |
65 | return sbitmap_any_bit_set(&hctx->ctx_map) || |
66 | !list_empty_careful(&hctx->dispatch) || | |
67 | blk_mq_sched_has_work(hctx); | |
1429d7c9 JA |
68 | } |
69 | ||
320ae51f JA |
70 | /* |
71 | * Mark this ctx as having pending work in this hardware queue | |
72 | */ | |
73 | static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx, | |
74 | struct blk_mq_ctx *ctx) | |
75 | { | |
88459642 OS |
76 | if (!sbitmap_test_bit(&hctx->ctx_map, ctx->index_hw)) |
77 | sbitmap_set_bit(&hctx->ctx_map, ctx->index_hw); | |
1429d7c9 JA |
78 | } |
79 | ||
80 | static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx, | |
81 | struct blk_mq_ctx *ctx) | |
82 | { | |
88459642 | 83 | sbitmap_clear_bit(&hctx->ctx_map, ctx->index_hw); |
320ae51f JA |
84 | } |
85 | ||
1671d522 | 86 | void blk_freeze_queue_start(struct request_queue *q) |
43a5e4e2 | 87 | { |
4ecd4fef | 88 | int freeze_depth; |
cddd5d17 | 89 | |
4ecd4fef CH |
90 | freeze_depth = atomic_inc_return(&q->mq_freeze_depth); |
91 | if (freeze_depth == 1) { | |
3ef28e83 | 92 | percpu_ref_kill(&q->q_usage_counter); |
b94ec296 | 93 | blk_mq_run_hw_queues(q, false); |
cddd5d17 | 94 | } |
f3af020b | 95 | } |
1671d522 | 96 | EXPORT_SYMBOL_GPL(blk_freeze_queue_start); |
f3af020b | 97 | |
6bae363e | 98 | void blk_mq_freeze_queue_wait(struct request_queue *q) |
f3af020b | 99 | { |
3ef28e83 | 100 | wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter)); |
43a5e4e2 | 101 | } |
6bae363e | 102 | EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait); |
43a5e4e2 | 103 | |
f91328c4 KB |
104 | int blk_mq_freeze_queue_wait_timeout(struct request_queue *q, |
105 | unsigned long timeout) | |
106 | { | |
107 | return wait_event_timeout(q->mq_freeze_wq, | |
108 | percpu_ref_is_zero(&q->q_usage_counter), | |
109 | timeout); | |
110 | } | |
111 | EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout); | |
43a5e4e2 | 112 | |
f3af020b TH |
113 | /* |
114 | * Guarantee no request is in use, so we can change any data structure of | |
115 | * the queue afterward. | |
116 | */ | |
3ef28e83 | 117 | void blk_freeze_queue(struct request_queue *q) |
f3af020b | 118 | { |
3ef28e83 DW |
119 | /* |
120 | * In the !blk_mq case we are only calling this to kill the | |
121 | * q_usage_counter, otherwise this increases the freeze depth | |
122 | * and waits for it to return to zero. For this reason there is | |
123 | * no blk_unfreeze_queue(), and blk_freeze_queue() is not | |
124 | * exported to drivers as the only user for unfreeze is blk_mq. | |
125 | */ | |
1671d522 | 126 | blk_freeze_queue_start(q); |
f3af020b TH |
127 | blk_mq_freeze_queue_wait(q); |
128 | } | |
3ef28e83 DW |
129 | |
130 | void blk_mq_freeze_queue(struct request_queue *q) | |
131 | { | |
132 | /* | |
133 | * ...just an alias to keep freeze and unfreeze actions balanced | |
134 | * in the blk_mq_* namespace | |
135 | */ | |
136 | blk_freeze_queue(q); | |
137 | } | |
c761d96b | 138 | EXPORT_SYMBOL_GPL(blk_mq_freeze_queue); |
f3af020b | 139 | |
b4c6a028 | 140 | void blk_mq_unfreeze_queue(struct request_queue *q) |
320ae51f | 141 | { |
4ecd4fef | 142 | int freeze_depth; |
320ae51f | 143 | |
4ecd4fef CH |
144 | freeze_depth = atomic_dec_return(&q->mq_freeze_depth); |
145 | WARN_ON_ONCE(freeze_depth < 0); | |
146 | if (!freeze_depth) { | |
3ef28e83 | 147 | percpu_ref_reinit(&q->q_usage_counter); |
320ae51f | 148 | wake_up_all(&q->mq_freeze_wq); |
add703fd | 149 | } |
320ae51f | 150 | } |
b4c6a028 | 151 | EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue); |
320ae51f | 152 | |
852ec809 BVA |
153 | /* |
154 | * FIXME: replace the scsi_internal_device_*block_nowait() calls in the | |
155 | * mpt3sas driver such that this function can be removed. | |
156 | */ | |
157 | void blk_mq_quiesce_queue_nowait(struct request_queue *q) | |
158 | { | |
159 | unsigned long flags; | |
160 | ||
161 | spin_lock_irqsave(q->queue_lock, flags); | |
162 | queue_flag_set(QUEUE_FLAG_QUIESCED, q); | |
163 | spin_unlock_irqrestore(q->queue_lock, flags); | |
164 | } | |
165 | EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait); | |
166 | ||
6a83e74d | 167 | /** |
69e07c4a | 168 | * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished |
6a83e74d BVA |
169 | * @q: request queue. |
170 | * | |
171 | * Note: this function does not prevent that the struct request end_io() | |
69e07c4a ML |
172 | * callback function is invoked. Once this function is returned, we make |
173 | * sure no dispatch can happen until the queue is unquiesced via | |
174 | * blk_mq_unquiesce_queue(). | |
6a83e74d BVA |
175 | */ |
176 | void blk_mq_quiesce_queue(struct request_queue *q) | |
177 | { | |
178 | struct blk_mq_hw_ctx *hctx; | |
179 | unsigned int i; | |
180 | bool rcu = false; | |
181 | ||
1d9e9bc6 | 182 | blk_mq_quiesce_queue_nowait(q); |
f4560ffe | 183 | |
6a83e74d BVA |
184 | queue_for_each_hw_ctx(q, hctx, i) { |
185 | if (hctx->flags & BLK_MQ_F_BLOCKING) | |
07319678 | 186 | synchronize_srcu(hctx->queue_rq_srcu); |
6a83e74d BVA |
187 | else |
188 | rcu = true; | |
189 | } | |
190 | if (rcu) | |
191 | synchronize_rcu(); | |
192 | } | |
193 | EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue); | |
194 | ||
e4e73913 ML |
195 | /* |
196 | * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue() | |
197 | * @q: request queue. | |
198 | * | |
199 | * This function recovers queue into the state before quiescing | |
200 | * which is done by blk_mq_quiesce_queue. | |
201 | */ | |
202 | void blk_mq_unquiesce_queue(struct request_queue *q) | |
203 | { | |
852ec809 BVA |
204 | unsigned long flags; |
205 | ||
206 | spin_lock_irqsave(q->queue_lock, flags); | |
f4560ffe | 207 | queue_flag_clear(QUEUE_FLAG_QUIESCED, q); |
852ec809 | 208 | spin_unlock_irqrestore(q->queue_lock, flags); |
f4560ffe | 209 | |
1d9e9bc6 ML |
210 | /* dispatch requests which are inserted during quiescing */ |
211 | blk_mq_run_hw_queues(q, true); | |
e4e73913 ML |
212 | } |
213 | EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue); | |
214 | ||
aed3ea94 JA |
215 | void blk_mq_wake_waiters(struct request_queue *q) |
216 | { | |
217 | struct blk_mq_hw_ctx *hctx; | |
218 | unsigned int i; | |
219 | ||
220 | queue_for_each_hw_ctx(q, hctx, i) | |
221 | if (blk_mq_hw_queue_mapped(hctx)) | |
222 | blk_mq_tag_wakeup_all(hctx->tags, true); | |
3fd5940c KB |
223 | |
224 | /* | |
225 | * If we are called because the queue has now been marked as | |
226 | * dying, we need to ensure that processes currently waiting on | |
227 | * the queue are notified as well. | |
228 | */ | |
229 | wake_up_all(&q->mq_freeze_wq); | |
aed3ea94 JA |
230 | } |
231 | ||
320ae51f JA |
232 | bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx) |
233 | { | |
234 | return blk_mq_has_free_tags(hctx->tags); | |
235 | } | |
236 | EXPORT_SYMBOL(blk_mq_can_queue); | |
237 | ||
e4cdf1a1 CH |
238 | static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data, |
239 | unsigned int tag, unsigned int op) | |
320ae51f | 240 | { |
e4cdf1a1 CH |
241 | struct blk_mq_tags *tags = blk_mq_tags_from_data(data); |
242 | struct request *rq = tags->static_rqs[tag]; | |
243 | ||
c3a148d2 BVA |
244 | rq->rq_flags = 0; |
245 | ||
e4cdf1a1 CH |
246 | if (data->flags & BLK_MQ_REQ_INTERNAL) { |
247 | rq->tag = -1; | |
248 | rq->internal_tag = tag; | |
249 | } else { | |
250 | if (blk_mq_tag_busy(data->hctx)) { | |
251 | rq->rq_flags = RQF_MQ_INFLIGHT; | |
252 | atomic_inc(&data->hctx->nr_active); | |
253 | } | |
254 | rq->tag = tag; | |
255 | rq->internal_tag = -1; | |
256 | data->hctx->tags->rqs[rq->tag] = rq; | |
257 | } | |
258 | ||
af76e555 CH |
259 | INIT_LIST_HEAD(&rq->queuelist); |
260 | /* csd/requeue_work/fifo_time is initialized before use */ | |
e4cdf1a1 CH |
261 | rq->q = data->q; |
262 | rq->mq_ctx = data->ctx; | |
ef295ecf | 263 | rq->cmd_flags = op; |
e4cdf1a1 | 264 | if (blk_queue_io_stat(data->q)) |
e8064021 | 265 | rq->rq_flags |= RQF_IO_STAT; |
af76e555 CH |
266 | /* do not touch atomic flags, it needs atomic ops against the timer */ |
267 | rq->cpu = -1; | |
af76e555 CH |
268 | INIT_HLIST_NODE(&rq->hash); |
269 | RB_CLEAR_NODE(&rq->rb_node); | |
af76e555 CH |
270 | rq->rq_disk = NULL; |
271 | rq->part = NULL; | |
3ee32372 | 272 | rq->start_time = jiffies; |
af76e555 CH |
273 | #ifdef CONFIG_BLK_CGROUP |
274 | rq->rl = NULL; | |
0fec08b4 | 275 | set_start_time_ns(rq); |
af76e555 CH |
276 | rq->io_start_time_ns = 0; |
277 | #endif | |
278 | rq->nr_phys_segments = 0; | |
279 | #if defined(CONFIG_BLK_DEV_INTEGRITY) | |
280 | rq->nr_integrity_segments = 0; | |
281 | #endif | |
af76e555 CH |
282 | rq->special = NULL; |
283 | /* tag was already set */ | |
af76e555 | 284 | rq->extra_len = 0; |
af76e555 | 285 | |
af76e555 | 286 | INIT_LIST_HEAD(&rq->timeout_list); |
f6be4fb4 JA |
287 | rq->timeout = 0; |
288 | ||
af76e555 CH |
289 | rq->end_io = NULL; |
290 | rq->end_io_data = NULL; | |
291 | rq->next_rq = NULL; | |
292 | ||
e4cdf1a1 CH |
293 | data->ctx->rq_dispatched[op_is_sync(op)]++; |
294 | return rq; | |
5dee8577 CH |
295 | } |
296 | ||
d2c0d383 CH |
297 | static struct request *blk_mq_get_request(struct request_queue *q, |
298 | struct bio *bio, unsigned int op, | |
299 | struct blk_mq_alloc_data *data) | |
300 | { | |
301 | struct elevator_queue *e = q->elevator; | |
302 | struct request *rq; | |
e4cdf1a1 | 303 | unsigned int tag; |
1ad43c00 | 304 | struct blk_mq_ctx *local_ctx = NULL; |
d2c0d383 CH |
305 | |
306 | blk_queue_enter_live(q); | |
307 | data->q = q; | |
308 | if (likely(!data->ctx)) | |
1ad43c00 | 309 | data->ctx = local_ctx = blk_mq_get_ctx(q); |
d2c0d383 CH |
310 | if (likely(!data->hctx)) |
311 | data->hctx = blk_mq_map_queue(q, data->ctx->cpu); | |
03a07c92 GR |
312 | if (op & REQ_NOWAIT) |
313 | data->flags |= BLK_MQ_REQ_NOWAIT; | |
d2c0d383 CH |
314 | |
315 | if (e) { | |
316 | data->flags |= BLK_MQ_REQ_INTERNAL; | |
317 | ||
318 | /* | |
319 | * Flush requests are special and go directly to the | |
320 | * dispatch list. | |
321 | */ | |
5bbf4e5a CH |
322 | if (!op_is_flush(op) && e->type->ops.mq.limit_depth) |
323 | e->type->ops.mq.limit_depth(op, data); | |
d2c0d383 CH |
324 | } |
325 | ||
e4cdf1a1 CH |
326 | tag = blk_mq_get_tag(data); |
327 | if (tag == BLK_MQ_TAG_FAIL) { | |
1ad43c00 ML |
328 | if (local_ctx) { |
329 | blk_mq_put_ctx(local_ctx); | |
330 | data->ctx = NULL; | |
331 | } | |
037cebb8 CH |
332 | blk_queue_exit(q); |
333 | return NULL; | |
d2c0d383 CH |
334 | } |
335 | ||
e4cdf1a1 | 336 | rq = blk_mq_rq_ctx_init(data, tag, op); |
037cebb8 CH |
337 | if (!op_is_flush(op)) { |
338 | rq->elv.icq = NULL; | |
5bbf4e5a | 339 | if (e && e->type->ops.mq.prepare_request) { |
44e8c2bf CH |
340 | if (e->type->icq_cache && rq_ioc(bio)) |
341 | blk_mq_sched_assign_ioc(rq, bio); | |
342 | ||
5bbf4e5a CH |
343 | e->type->ops.mq.prepare_request(rq, bio); |
344 | rq->rq_flags |= RQF_ELVPRIV; | |
44e8c2bf | 345 | } |
037cebb8 CH |
346 | } |
347 | data->hctx->queued++; | |
348 | return rq; | |
d2c0d383 CH |
349 | } |
350 | ||
cd6ce148 | 351 | struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op, |
6f3b0e8b | 352 | unsigned int flags) |
320ae51f | 353 | { |
5a797e00 | 354 | struct blk_mq_alloc_data alloc_data = { .flags = flags }; |
bd166ef1 | 355 | struct request *rq; |
a492f075 | 356 | int ret; |
320ae51f | 357 | |
6f3b0e8b | 358 | ret = blk_queue_enter(q, flags & BLK_MQ_REQ_NOWAIT); |
a492f075 JL |
359 | if (ret) |
360 | return ERR_PTR(ret); | |
320ae51f | 361 | |
cd6ce148 | 362 | rq = blk_mq_get_request(q, NULL, op, &alloc_data); |
3280d66a | 363 | blk_queue_exit(q); |
841bac2c | 364 | |
bd166ef1 | 365 | if (!rq) |
a492f075 | 366 | return ERR_PTR(-EWOULDBLOCK); |
0c4de0f3 | 367 | |
1ad43c00 | 368 | blk_mq_put_ctx(alloc_data.ctx); |
1ad43c00 | 369 | |
0c4de0f3 CH |
370 | rq->__data_len = 0; |
371 | rq->__sector = (sector_t) -1; | |
372 | rq->bio = rq->biotail = NULL; | |
320ae51f JA |
373 | return rq; |
374 | } | |
4bb659b1 | 375 | EXPORT_SYMBOL(blk_mq_alloc_request); |
320ae51f | 376 | |
cd6ce148 BVA |
377 | struct request *blk_mq_alloc_request_hctx(struct request_queue *q, |
378 | unsigned int op, unsigned int flags, unsigned int hctx_idx) | |
1f5bd336 | 379 | { |
6d2809d5 | 380 | struct blk_mq_alloc_data alloc_data = { .flags = flags }; |
1f5bd336 | 381 | struct request *rq; |
6d2809d5 | 382 | unsigned int cpu; |
1f5bd336 ML |
383 | int ret; |
384 | ||
385 | /* | |
386 | * If the tag allocator sleeps we could get an allocation for a | |
387 | * different hardware context. No need to complicate the low level | |
388 | * allocator for this for the rare use case of a command tied to | |
389 | * a specific queue. | |
390 | */ | |
391 | if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT))) | |
392 | return ERR_PTR(-EINVAL); | |
393 | ||
394 | if (hctx_idx >= q->nr_hw_queues) | |
395 | return ERR_PTR(-EIO); | |
396 | ||
397 | ret = blk_queue_enter(q, true); | |
398 | if (ret) | |
399 | return ERR_PTR(ret); | |
400 | ||
c8712c6a CH |
401 | /* |
402 | * Check if the hardware context is actually mapped to anything. | |
403 | * If not tell the caller that it should skip this queue. | |
404 | */ | |
6d2809d5 OS |
405 | alloc_data.hctx = q->queue_hw_ctx[hctx_idx]; |
406 | if (!blk_mq_hw_queue_mapped(alloc_data.hctx)) { | |
407 | blk_queue_exit(q); | |
408 | return ERR_PTR(-EXDEV); | |
c8712c6a | 409 | } |
6d2809d5 OS |
410 | cpu = cpumask_first(alloc_data.hctx->cpumask); |
411 | alloc_data.ctx = __blk_mq_get_ctx(q, cpu); | |
1f5bd336 | 412 | |
cd6ce148 | 413 | rq = blk_mq_get_request(q, NULL, op, &alloc_data); |
3280d66a | 414 | blk_queue_exit(q); |
c8712c6a | 415 | |
6d2809d5 OS |
416 | if (!rq) |
417 | return ERR_PTR(-EWOULDBLOCK); | |
418 | ||
419 | return rq; | |
1f5bd336 ML |
420 | } |
421 | EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx); | |
422 | ||
6af54051 | 423 | void blk_mq_free_request(struct request *rq) |
320ae51f | 424 | { |
320ae51f | 425 | struct request_queue *q = rq->q; |
6af54051 CH |
426 | struct elevator_queue *e = q->elevator; |
427 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
428 | struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu); | |
429 | const int sched_tag = rq->internal_tag; | |
430 | ||
5bbf4e5a | 431 | if (rq->rq_flags & RQF_ELVPRIV) { |
6af54051 CH |
432 | if (e && e->type->ops.mq.finish_request) |
433 | e->type->ops.mq.finish_request(rq); | |
434 | if (rq->elv.icq) { | |
435 | put_io_context(rq->elv.icq->ioc); | |
436 | rq->elv.icq = NULL; | |
437 | } | |
438 | } | |
320ae51f | 439 | |
6af54051 | 440 | ctx->rq_completed[rq_is_sync(rq)]++; |
e8064021 | 441 | if (rq->rq_flags & RQF_MQ_INFLIGHT) |
0d2602ca | 442 | atomic_dec(&hctx->nr_active); |
87760e5e JA |
443 | |
444 | wbt_done(q->rq_wb, &rq->issue_stat); | |
0d2602ca | 445 | |
af76e555 | 446 | clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
06426adf | 447 | clear_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags); |
bd166ef1 JA |
448 | if (rq->tag != -1) |
449 | blk_mq_put_tag(hctx, hctx->tags, ctx, rq->tag); | |
450 | if (sched_tag != -1) | |
c05f8525 | 451 | blk_mq_put_tag(hctx, hctx->sched_tags, ctx, sched_tag); |
6d8c6c0f | 452 | blk_mq_sched_restart(hctx); |
3ef28e83 | 453 | blk_queue_exit(q); |
320ae51f | 454 | } |
1a3b595a | 455 | EXPORT_SYMBOL_GPL(blk_mq_free_request); |
320ae51f | 456 | |
2a842aca | 457 | inline void __blk_mq_end_request(struct request *rq, blk_status_t error) |
320ae51f | 458 | { |
0d11e6ac ML |
459 | blk_account_io_done(rq); |
460 | ||
91b63639 | 461 | if (rq->end_io) { |
87760e5e | 462 | wbt_done(rq->q->rq_wb, &rq->issue_stat); |
320ae51f | 463 | rq->end_io(rq, error); |
91b63639 CH |
464 | } else { |
465 | if (unlikely(blk_bidi_rq(rq))) | |
466 | blk_mq_free_request(rq->next_rq); | |
320ae51f | 467 | blk_mq_free_request(rq); |
91b63639 | 468 | } |
320ae51f | 469 | } |
c8a446ad | 470 | EXPORT_SYMBOL(__blk_mq_end_request); |
63151a44 | 471 | |
2a842aca | 472 | void blk_mq_end_request(struct request *rq, blk_status_t error) |
63151a44 CH |
473 | { |
474 | if (blk_update_request(rq, error, blk_rq_bytes(rq))) | |
475 | BUG(); | |
c8a446ad | 476 | __blk_mq_end_request(rq, error); |
63151a44 | 477 | } |
c8a446ad | 478 | EXPORT_SYMBOL(blk_mq_end_request); |
320ae51f | 479 | |
30a91cb4 | 480 | static void __blk_mq_complete_request_remote(void *data) |
320ae51f | 481 | { |
3d6efbf6 | 482 | struct request *rq = data; |
320ae51f | 483 | |
30a91cb4 | 484 | rq->q->softirq_done_fn(rq); |
320ae51f | 485 | } |
320ae51f | 486 | |
453f8341 | 487 | static void __blk_mq_complete_request(struct request *rq) |
320ae51f JA |
488 | { |
489 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
38535201 | 490 | bool shared = false; |
320ae51f JA |
491 | int cpu; |
492 | ||
453f8341 CH |
493 | if (rq->internal_tag != -1) |
494 | blk_mq_sched_completed_request(rq); | |
495 | if (rq->rq_flags & RQF_STATS) { | |
496 | blk_mq_poll_stats_start(rq->q); | |
497 | blk_stat_add(rq); | |
498 | } | |
499 | ||
38535201 | 500 | if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) { |
30a91cb4 CH |
501 | rq->q->softirq_done_fn(rq); |
502 | return; | |
503 | } | |
320ae51f JA |
504 | |
505 | cpu = get_cpu(); | |
38535201 CH |
506 | if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags)) |
507 | shared = cpus_share_cache(cpu, ctx->cpu); | |
508 | ||
509 | if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) { | |
30a91cb4 | 510 | rq->csd.func = __blk_mq_complete_request_remote; |
3d6efbf6 CH |
511 | rq->csd.info = rq; |
512 | rq->csd.flags = 0; | |
c46fff2a | 513 | smp_call_function_single_async(ctx->cpu, &rq->csd); |
3d6efbf6 | 514 | } else { |
30a91cb4 | 515 | rq->q->softirq_done_fn(rq); |
3d6efbf6 | 516 | } |
320ae51f JA |
517 | put_cpu(); |
518 | } | |
30a91cb4 CH |
519 | |
520 | /** | |
521 | * blk_mq_complete_request - end I/O on a request | |
522 | * @rq: the request being processed | |
523 | * | |
524 | * Description: | |
525 | * Ends all I/O on a request. It does not handle partial completions. | |
526 | * The actual completion happens out-of-order, through a IPI handler. | |
527 | **/ | |
08e0029a | 528 | void blk_mq_complete_request(struct request *rq) |
30a91cb4 | 529 | { |
95f09684 JA |
530 | struct request_queue *q = rq->q; |
531 | ||
532 | if (unlikely(blk_should_fake_timeout(q))) | |
30a91cb4 | 533 | return; |
08e0029a | 534 | if (!blk_mark_rq_complete(rq)) |
ed851860 | 535 | __blk_mq_complete_request(rq); |
30a91cb4 CH |
536 | } |
537 | EXPORT_SYMBOL(blk_mq_complete_request); | |
320ae51f | 538 | |
973c0191 KB |
539 | int blk_mq_request_started(struct request *rq) |
540 | { | |
541 | return test_bit(REQ_ATOM_STARTED, &rq->atomic_flags); | |
542 | } | |
543 | EXPORT_SYMBOL_GPL(blk_mq_request_started); | |
544 | ||
e2490073 | 545 | void blk_mq_start_request(struct request *rq) |
320ae51f JA |
546 | { |
547 | struct request_queue *q = rq->q; | |
548 | ||
bd166ef1 JA |
549 | blk_mq_sched_started_request(rq); |
550 | ||
320ae51f JA |
551 | trace_block_rq_issue(q, rq); |
552 | ||
cf43e6be | 553 | if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) { |
88eeca49 | 554 | blk_stat_set_issue(&rq->issue_stat, blk_rq_sectors(rq)); |
cf43e6be | 555 | rq->rq_flags |= RQF_STATS; |
87760e5e | 556 | wbt_issue(q->rq_wb, &rq->issue_stat); |
cf43e6be JA |
557 | } |
558 | ||
2b8393b4 | 559 | blk_add_timer(rq); |
87ee7b11 | 560 | |
538b7534 JA |
561 | /* |
562 | * Ensure that ->deadline is visible before set the started | |
563 | * flag and clear the completed flag. | |
564 | */ | |
565 | smp_mb__before_atomic(); | |
566 | ||
87ee7b11 JA |
567 | /* |
568 | * Mark us as started and clear complete. Complete might have been | |
569 | * set if requeue raced with timeout, which then marked it as | |
570 | * complete. So be sure to clear complete again when we start | |
571 | * the request, otherwise we'll ignore the completion event. | |
572 | */ | |
4b570521 JA |
573 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
574 | set_bit(REQ_ATOM_STARTED, &rq->atomic_flags); | |
575 | if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags)) | |
576 | clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags); | |
49f5baa5 CH |
577 | |
578 | if (q->dma_drain_size && blk_rq_bytes(rq)) { | |
579 | /* | |
580 | * Make sure space for the drain appears. We know we can do | |
581 | * this because max_hw_segments has been adjusted to be one | |
582 | * fewer than the device can handle. | |
583 | */ | |
584 | rq->nr_phys_segments++; | |
585 | } | |
320ae51f | 586 | } |
e2490073 | 587 | EXPORT_SYMBOL(blk_mq_start_request); |
320ae51f | 588 | |
d9d149a3 ML |
589 | /* |
590 | * When we reach here because queue is busy, REQ_ATOM_COMPLETE | |
48b99c9d | 591 | * flag isn't set yet, so there may be race with timeout handler, |
d9d149a3 ML |
592 | * but given rq->deadline is just set in .queue_rq() under |
593 | * this situation, the race won't be possible in reality because | |
594 | * rq->timeout should be set as big enough to cover the window | |
595 | * between blk_mq_start_request() called from .queue_rq() and | |
596 | * clearing REQ_ATOM_STARTED here. | |
597 | */ | |
ed0791b2 | 598 | static void __blk_mq_requeue_request(struct request *rq) |
320ae51f JA |
599 | { |
600 | struct request_queue *q = rq->q; | |
601 | ||
602 | trace_block_rq_requeue(q, rq); | |
87760e5e | 603 | wbt_requeue(q->rq_wb, &rq->issue_stat); |
bd166ef1 | 604 | blk_mq_sched_requeue_request(rq); |
49f5baa5 | 605 | |
e2490073 CH |
606 | if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) { |
607 | if (q->dma_drain_size && blk_rq_bytes(rq)) | |
608 | rq->nr_phys_segments--; | |
609 | } | |
320ae51f JA |
610 | } |
611 | ||
2b053aca | 612 | void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list) |
ed0791b2 | 613 | { |
ed0791b2 | 614 | __blk_mq_requeue_request(rq); |
ed0791b2 | 615 | |
ed0791b2 | 616 | BUG_ON(blk_queued_rq(rq)); |
2b053aca | 617 | blk_mq_add_to_requeue_list(rq, true, kick_requeue_list); |
ed0791b2 CH |
618 | } |
619 | EXPORT_SYMBOL(blk_mq_requeue_request); | |
620 | ||
6fca6a61 CH |
621 | static void blk_mq_requeue_work(struct work_struct *work) |
622 | { | |
623 | struct request_queue *q = | |
2849450a | 624 | container_of(work, struct request_queue, requeue_work.work); |
6fca6a61 CH |
625 | LIST_HEAD(rq_list); |
626 | struct request *rq, *next; | |
627 | unsigned long flags; | |
628 | ||
629 | spin_lock_irqsave(&q->requeue_lock, flags); | |
630 | list_splice_init(&q->requeue_list, &rq_list); | |
631 | spin_unlock_irqrestore(&q->requeue_lock, flags); | |
632 | ||
633 | list_for_each_entry_safe(rq, next, &rq_list, queuelist) { | |
e8064021 | 634 | if (!(rq->rq_flags & RQF_SOFTBARRIER)) |
6fca6a61 CH |
635 | continue; |
636 | ||
e8064021 | 637 | rq->rq_flags &= ~RQF_SOFTBARRIER; |
6fca6a61 | 638 | list_del_init(&rq->queuelist); |
bd6737f1 | 639 | blk_mq_sched_insert_request(rq, true, false, false, true); |
6fca6a61 CH |
640 | } |
641 | ||
642 | while (!list_empty(&rq_list)) { | |
643 | rq = list_entry(rq_list.next, struct request, queuelist); | |
644 | list_del_init(&rq->queuelist); | |
bd6737f1 | 645 | blk_mq_sched_insert_request(rq, false, false, false, true); |
6fca6a61 CH |
646 | } |
647 | ||
52d7f1b5 | 648 | blk_mq_run_hw_queues(q, false); |
6fca6a61 CH |
649 | } |
650 | ||
2b053aca BVA |
651 | void blk_mq_add_to_requeue_list(struct request *rq, bool at_head, |
652 | bool kick_requeue_list) | |
6fca6a61 CH |
653 | { |
654 | struct request_queue *q = rq->q; | |
655 | unsigned long flags; | |
656 | ||
657 | /* | |
658 | * We abuse this flag that is otherwise used by the I/O scheduler to | |
659 | * request head insertation from the workqueue. | |
660 | */ | |
e8064021 | 661 | BUG_ON(rq->rq_flags & RQF_SOFTBARRIER); |
6fca6a61 CH |
662 | |
663 | spin_lock_irqsave(&q->requeue_lock, flags); | |
664 | if (at_head) { | |
e8064021 | 665 | rq->rq_flags |= RQF_SOFTBARRIER; |
6fca6a61 CH |
666 | list_add(&rq->queuelist, &q->requeue_list); |
667 | } else { | |
668 | list_add_tail(&rq->queuelist, &q->requeue_list); | |
669 | } | |
670 | spin_unlock_irqrestore(&q->requeue_lock, flags); | |
2b053aca BVA |
671 | |
672 | if (kick_requeue_list) | |
673 | blk_mq_kick_requeue_list(q); | |
6fca6a61 CH |
674 | } |
675 | EXPORT_SYMBOL(blk_mq_add_to_requeue_list); | |
676 | ||
677 | void blk_mq_kick_requeue_list(struct request_queue *q) | |
678 | { | |
2849450a | 679 | kblockd_schedule_delayed_work(&q->requeue_work, 0); |
6fca6a61 CH |
680 | } |
681 | EXPORT_SYMBOL(blk_mq_kick_requeue_list); | |
682 | ||
2849450a MS |
683 | void blk_mq_delay_kick_requeue_list(struct request_queue *q, |
684 | unsigned long msecs) | |
685 | { | |
d4acf365 BVA |
686 | kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, |
687 | msecs_to_jiffies(msecs)); | |
2849450a MS |
688 | } |
689 | EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list); | |
690 | ||
0e62f51f JA |
691 | struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag) |
692 | { | |
88c7b2b7 JA |
693 | if (tag < tags->nr_tags) { |
694 | prefetch(tags->rqs[tag]); | |
4ee86bab | 695 | return tags->rqs[tag]; |
88c7b2b7 | 696 | } |
4ee86bab HR |
697 | |
698 | return NULL; | |
24d2f903 CH |
699 | } |
700 | EXPORT_SYMBOL(blk_mq_tag_to_rq); | |
701 | ||
320ae51f | 702 | struct blk_mq_timeout_data { |
46f92d42 CH |
703 | unsigned long next; |
704 | unsigned int next_set; | |
320ae51f JA |
705 | }; |
706 | ||
90415837 | 707 | void blk_mq_rq_timed_out(struct request *req, bool reserved) |
320ae51f | 708 | { |
f8a5b122 | 709 | const struct blk_mq_ops *ops = req->q->mq_ops; |
46f92d42 | 710 | enum blk_eh_timer_return ret = BLK_EH_RESET_TIMER; |
87ee7b11 JA |
711 | |
712 | /* | |
713 | * We know that complete is set at this point. If STARTED isn't set | |
714 | * anymore, then the request isn't active and the "timeout" should | |
715 | * just be ignored. This can happen due to the bitflag ordering. | |
716 | * Timeout first checks if STARTED is set, and if it is, assumes | |
717 | * the request is active. But if we race with completion, then | |
48b99c9d | 718 | * both flags will get cleared. So check here again, and ignore |
87ee7b11 JA |
719 | * a timeout event with a request that isn't active. |
720 | */ | |
46f92d42 CH |
721 | if (!test_bit(REQ_ATOM_STARTED, &req->atomic_flags)) |
722 | return; | |
87ee7b11 | 723 | |
46f92d42 | 724 | if (ops->timeout) |
0152fb6b | 725 | ret = ops->timeout(req, reserved); |
46f92d42 CH |
726 | |
727 | switch (ret) { | |
728 | case BLK_EH_HANDLED: | |
729 | __blk_mq_complete_request(req); | |
730 | break; | |
731 | case BLK_EH_RESET_TIMER: | |
732 | blk_add_timer(req); | |
733 | blk_clear_rq_complete(req); | |
734 | break; | |
735 | case BLK_EH_NOT_HANDLED: | |
736 | break; | |
737 | default: | |
738 | printk(KERN_ERR "block: bad eh return: %d\n", ret); | |
739 | break; | |
740 | } | |
87ee7b11 | 741 | } |
5b3f25fc | 742 | |
81481eb4 CH |
743 | static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx, |
744 | struct request *rq, void *priv, bool reserved) | |
745 | { | |
746 | struct blk_mq_timeout_data *data = priv; | |
87ee7b11 | 747 | |
95a49603 | 748 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
46f92d42 | 749 | return; |
87ee7b11 | 750 | |
d9d149a3 ML |
751 | /* |
752 | * The rq being checked may have been freed and reallocated | |
753 | * out already here, we avoid this race by checking rq->deadline | |
754 | * and REQ_ATOM_COMPLETE flag together: | |
755 | * | |
756 | * - if rq->deadline is observed as new value because of | |
757 | * reusing, the rq won't be timed out because of timing. | |
758 | * - if rq->deadline is observed as previous value, | |
759 | * REQ_ATOM_COMPLETE flag won't be cleared in reuse path | |
760 | * because we put a barrier between setting rq->deadline | |
761 | * and clearing the flag in blk_mq_start_request(), so | |
762 | * this rq won't be timed out too. | |
763 | */ | |
46f92d42 CH |
764 | if (time_after_eq(jiffies, rq->deadline)) { |
765 | if (!blk_mark_rq_complete(rq)) | |
0152fb6b | 766 | blk_mq_rq_timed_out(rq, reserved); |
46f92d42 CH |
767 | } else if (!data->next_set || time_after(data->next, rq->deadline)) { |
768 | data->next = rq->deadline; | |
769 | data->next_set = 1; | |
770 | } | |
87ee7b11 JA |
771 | } |
772 | ||
287922eb | 773 | static void blk_mq_timeout_work(struct work_struct *work) |
320ae51f | 774 | { |
287922eb CH |
775 | struct request_queue *q = |
776 | container_of(work, struct request_queue, timeout_work); | |
81481eb4 CH |
777 | struct blk_mq_timeout_data data = { |
778 | .next = 0, | |
779 | .next_set = 0, | |
780 | }; | |
81481eb4 | 781 | int i; |
320ae51f | 782 | |
71f79fb3 GKB |
783 | /* A deadlock might occur if a request is stuck requiring a |
784 | * timeout at the same time a queue freeze is waiting | |
785 | * completion, since the timeout code would not be able to | |
786 | * acquire the queue reference here. | |
787 | * | |
788 | * That's why we don't use blk_queue_enter here; instead, we use | |
789 | * percpu_ref_tryget directly, because we need to be able to | |
790 | * obtain a reference even in the short window between the queue | |
791 | * starting to freeze, by dropping the first reference in | |
1671d522 | 792 | * blk_freeze_queue_start, and the moment the last request is |
71f79fb3 GKB |
793 | * consumed, marked by the instant q_usage_counter reaches |
794 | * zero. | |
795 | */ | |
796 | if (!percpu_ref_tryget(&q->q_usage_counter)) | |
287922eb CH |
797 | return; |
798 | ||
0bf6cd5b | 799 | blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &data); |
320ae51f | 800 | |
81481eb4 CH |
801 | if (data.next_set) { |
802 | data.next = blk_rq_timeout(round_jiffies_up(data.next)); | |
803 | mod_timer(&q->timeout, data.next); | |
0d2602ca | 804 | } else { |
0bf6cd5b CH |
805 | struct blk_mq_hw_ctx *hctx; |
806 | ||
f054b56c ML |
807 | queue_for_each_hw_ctx(q, hctx, i) { |
808 | /* the hctx may be unmapped, so check it here */ | |
809 | if (blk_mq_hw_queue_mapped(hctx)) | |
810 | blk_mq_tag_idle(hctx); | |
811 | } | |
0d2602ca | 812 | } |
287922eb | 813 | blk_queue_exit(q); |
320ae51f JA |
814 | } |
815 | ||
88459642 OS |
816 | struct flush_busy_ctx_data { |
817 | struct blk_mq_hw_ctx *hctx; | |
818 | struct list_head *list; | |
819 | }; | |
820 | ||
821 | static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data) | |
822 | { | |
823 | struct flush_busy_ctx_data *flush_data = data; | |
824 | struct blk_mq_hw_ctx *hctx = flush_data->hctx; | |
825 | struct blk_mq_ctx *ctx = hctx->ctxs[bitnr]; | |
826 | ||
827 | sbitmap_clear_bit(sb, bitnr); | |
828 | spin_lock(&ctx->lock); | |
829 | list_splice_tail_init(&ctx->rq_list, flush_data->list); | |
830 | spin_unlock(&ctx->lock); | |
831 | return true; | |
832 | } | |
833 | ||
1429d7c9 JA |
834 | /* |
835 | * Process software queues that have been marked busy, splicing them | |
836 | * to the for-dispatch | |
837 | */ | |
2c3ad667 | 838 | void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list) |
1429d7c9 | 839 | { |
88459642 OS |
840 | struct flush_busy_ctx_data data = { |
841 | .hctx = hctx, | |
842 | .list = list, | |
843 | }; | |
1429d7c9 | 844 | |
88459642 | 845 | sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data); |
1429d7c9 | 846 | } |
2c3ad667 | 847 | EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs); |
1429d7c9 | 848 | |
703fd1c0 JA |
849 | static inline unsigned int queued_to_index(unsigned int queued) |
850 | { | |
851 | if (!queued) | |
852 | return 0; | |
1429d7c9 | 853 | |
703fd1c0 | 854 | return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1); |
1429d7c9 JA |
855 | } |
856 | ||
bd6737f1 JA |
857 | bool blk_mq_get_driver_tag(struct request *rq, struct blk_mq_hw_ctx **hctx, |
858 | bool wait) | |
bd166ef1 JA |
859 | { |
860 | struct blk_mq_alloc_data data = { | |
861 | .q = rq->q, | |
bd166ef1 JA |
862 | .hctx = blk_mq_map_queue(rq->q, rq->mq_ctx->cpu), |
863 | .flags = wait ? 0 : BLK_MQ_REQ_NOWAIT, | |
864 | }; | |
865 | ||
5feeacdd JA |
866 | might_sleep_if(wait); |
867 | ||
81380ca1 OS |
868 | if (rq->tag != -1) |
869 | goto done; | |
bd166ef1 | 870 | |
415b806d SG |
871 | if (blk_mq_tag_is_reserved(data.hctx->sched_tags, rq->internal_tag)) |
872 | data.flags |= BLK_MQ_REQ_RESERVED; | |
873 | ||
bd166ef1 JA |
874 | rq->tag = blk_mq_get_tag(&data); |
875 | if (rq->tag >= 0) { | |
200e86b3 JA |
876 | if (blk_mq_tag_busy(data.hctx)) { |
877 | rq->rq_flags |= RQF_MQ_INFLIGHT; | |
878 | atomic_inc(&data.hctx->nr_active); | |
879 | } | |
bd166ef1 | 880 | data.hctx->tags->rqs[rq->tag] = rq; |
bd166ef1 JA |
881 | } |
882 | ||
81380ca1 OS |
883 | done: |
884 | if (hctx) | |
885 | *hctx = data.hctx; | |
886 | return rq->tag != -1; | |
bd166ef1 JA |
887 | } |
888 | ||
113285b4 JA |
889 | static void __blk_mq_put_driver_tag(struct blk_mq_hw_ctx *hctx, |
890 | struct request *rq) | |
99cf1dc5 | 891 | { |
99cf1dc5 JA |
892 | blk_mq_put_tag(hctx, hctx->tags, rq->mq_ctx, rq->tag); |
893 | rq->tag = -1; | |
894 | ||
895 | if (rq->rq_flags & RQF_MQ_INFLIGHT) { | |
896 | rq->rq_flags &= ~RQF_MQ_INFLIGHT; | |
897 | atomic_dec(&hctx->nr_active); | |
898 | } | |
899 | } | |
900 | ||
113285b4 JA |
901 | static void blk_mq_put_driver_tag_hctx(struct blk_mq_hw_ctx *hctx, |
902 | struct request *rq) | |
903 | { | |
904 | if (rq->tag == -1 || rq->internal_tag == -1) | |
905 | return; | |
906 | ||
907 | __blk_mq_put_driver_tag(hctx, rq); | |
908 | } | |
909 | ||
910 | static void blk_mq_put_driver_tag(struct request *rq) | |
911 | { | |
912 | struct blk_mq_hw_ctx *hctx; | |
913 | ||
914 | if (rq->tag == -1 || rq->internal_tag == -1) | |
915 | return; | |
916 | ||
917 | hctx = blk_mq_map_queue(rq->q, rq->mq_ctx->cpu); | |
918 | __blk_mq_put_driver_tag(hctx, rq); | |
919 | } | |
920 | ||
bd166ef1 JA |
921 | /* |
922 | * If we fail getting a driver tag because all the driver tags are already | |
923 | * assigned and on the dispatch list, BUT the first entry does not have a | |
924 | * tag, then we could deadlock. For that case, move entries with assigned | |
925 | * driver tags to the front, leaving the set of tagged requests in the | |
926 | * same order, and the untagged set in the same order. | |
927 | */ | |
928 | static bool reorder_tags_to_front(struct list_head *list) | |
929 | { | |
930 | struct request *rq, *tmp, *first = NULL; | |
931 | ||
932 | list_for_each_entry_safe_reverse(rq, tmp, list, queuelist) { | |
933 | if (rq == first) | |
934 | break; | |
935 | if (rq->tag != -1) { | |
936 | list_move(&rq->queuelist, list); | |
937 | if (!first) | |
938 | first = rq; | |
939 | } | |
940 | } | |
941 | ||
942 | return first != NULL; | |
943 | } | |
944 | ||
ac6424b9 | 945 | static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode, int flags, |
da55f2cc OS |
946 | void *key) |
947 | { | |
948 | struct blk_mq_hw_ctx *hctx; | |
949 | ||
950 | hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait); | |
951 | ||
2055da97 | 952 | list_del(&wait->entry); |
da55f2cc OS |
953 | clear_bit_unlock(BLK_MQ_S_TAG_WAITING, &hctx->state); |
954 | blk_mq_run_hw_queue(hctx, true); | |
955 | return 1; | |
956 | } | |
957 | ||
958 | static bool blk_mq_dispatch_wait_add(struct blk_mq_hw_ctx *hctx) | |
959 | { | |
960 | struct sbq_wait_state *ws; | |
961 | ||
962 | /* | |
963 | * The TAG_WAITING bit serves as a lock protecting hctx->dispatch_wait. | |
964 | * The thread which wins the race to grab this bit adds the hardware | |
965 | * queue to the wait queue. | |
966 | */ | |
967 | if (test_bit(BLK_MQ_S_TAG_WAITING, &hctx->state) || | |
968 | test_and_set_bit_lock(BLK_MQ_S_TAG_WAITING, &hctx->state)) | |
969 | return false; | |
970 | ||
971 | init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake); | |
972 | ws = bt_wait_ptr(&hctx->tags->bitmap_tags, hctx); | |
973 | ||
974 | /* | |
975 | * As soon as this returns, it's no longer safe to fiddle with | |
976 | * hctx->dispatch_wait, since a completion can wake up the wait queue | |
977 | * and unlock the bit. | |
978 | */ | |
979 | add_wait_queue(&ws->wait, &hctx->dispatch_wait); | |
980 | return true; | |
981 | } | |
982 | ||
81380ca1 | 983 | bool blk_mq_dispatch_rq_list(struct request_queue *q, struct list_head *list) |
320ae51f | 984 | { |
81380ca1 | 985 | struct blk_mq_hw_ctx *hctx; |
320ae51f | 986 | struct request *rq; |
fc17b653 | 987 | int errors, queued; |
320ae51f | 988 | |
81380ca1 OS |
989 | if (list_empty(list)) |
990 | return false; | |
991 | ||
320ae51f JA |
992 | /* |
993 | * Now process all the entries, sending them to the driver. | |
994 | */ | |
93efe981 | 995 | errors = queued = 0; |
81380ca1 | 996 | do { |
74c45052 | 997 | struct blk_mq_queue_data bd; |
fc17b653 | 998 | blk_status_t ret; |
320ae51f | 999 | |
f04c3df3 | 1000 | rq = list_first_entry(list, struct request, queuelist); |
bd166ef1 JA |
1001 | if (!blk_mq_get_driver_tag(rq, &hctx, false)) { |
1002 | if (!queued && reorder_tags_to_front(list)) | |
1003 | continue; | |
3c782d67 JA |
1004 | |
1005 | /* | |
da55f2cc OS |
1006 | * The initial allocation attempt failed, so we need to |
1007 | * rerun the hardware queue when a tag is freed. | |
3c782d67 | 1008 | */ |
807b1041 OS |
1009 | if (!blk_mq_dispatch_wait_add(hctx)) |
1010 | break; | |
1011 | ||
1012 | /* | |
1013 | * It's possible that a tag was freed in the window | |
1014 | * between the allocation failure and adding the | |
1015 | * hardware queue to the wait queue. | |
1016 | */ | |
1017 | if (!blk_mq_get_driver_tag(rq, &hctx, false)) | |
3c782d67 | 1018 | break; |
bd166ef1 | 1019 | } |
da55f2cc | 1020 | |
320ae51f | 1021 | list_del_init(&rq->queuelist); |
320ae51f | 1022 | |
74c45052 | 1023 | bd.rq = rq; |
113285b4 JA |
1024 | |
1025 | /* | |
1026 | * Flag last if we have no more requests, or if we have more | |
1027 | * but can't assign a driver tag to it. | |
1028 | */ | |
1029 | if (list_empty(list)) | |
1030 | bd.last = true; | |
1031 | else { | |
1032 | struct request *nxt; | |
1033 | ||
1034 | nxt = list_first_entry(list, struct request, queuelist); | |
1035 | bd.last = !blk_mq_get_driver_tag(nxt, NULL, false); | |
1036 | } | |
74c45052 JA |
1037 | |
1038 | ret = q->mq_ops->queue_rq(hctx, &bd); | |
fc17b653 | 1039 | if (ret == BLK_STS_RESOURCE) { |
113285b4 | 1040 | blk_mq_put_driver_tag_hctx(hctx, rq); |
f04c3df3 | 1041 | list_add(&rq->queuelist, list); |
ed0791b2 | 1042 | __blk_mq_requeue_request(rq); |
320ae51f | 1043 | break; |
fc17b653 CH |
1044 | } |
1045 | ||
1046 | if (unlikely(ret != BLK_STS_OK)) { | |
93efe981 | 1047 | errors++; |
2a842aca | 1048 | blk_mq_end_request(rq, BLK_STS_IOERR); |
fc17b653 | 1049 | continue; |
320ae51f JA |
1050 | } |
1051 | ||
fc17b653 | 1052 | queued++; |
81380ca1 | 1053 | } while (!list_empty(list)); |
320ae51f | 1054 | |
703fd1c0 | 1055 | hctx->dispatched[queued_to_index(queued)]++; |
320ae51f JA |
1056 | |
1057 | /* | |
1058 | * Any items that need requeuing? Stuff them into hctx->dispatch, | |
1059 | * that is where we will continue on next queue run. | |
1060 | */ | |
f04c3df3 | 1061 | if (!list_empty(list)) { |
113285b4 | 1062 | /* |
710c785f BVA |
1063 | * If an I/O scheduler has been configured and we got a driver |
1064 | * tag for the next request already, free it again. | |
113285b4 JA |
1065 | */ |
1066 | rq = list_first_entry(list, struct request, queuelist); | |
1067 | blk_mq_put_driver_tag(rq); | |
1068 | ||
320ae51f | 1069 | spin_lock(&hctx->lock); |
c13660a0 | 1070 | list_splice_init(list, &hctx->dispatch); |
320ae51f | 1071 | spin_unlock(&hctx->lock); |
f04c3df3 | 1072 | |
9ba52e58 | 1073 | /* |
710c785f BVA |
1074 | * If SCHED_RESTART was set by the caller of this function and |
1075 | * it is no longer set that means that it was cleared by another | |
1076 | * thread and hence that a queue rerun is needed. | |
9ba52e58 | 1077 | * |
710c785f BVA |
1078 | * If TAG_WAITING is set that means that an I/O scheduler has |
1079 | * been configured and another thread is waiting for a driver | |
1080 | * tag. To guarantee fairness, do not rerun this hardware queue | |
1081 | * but let the other thread grab the driver tag. | |
bd166ef1 | 1082 | * |
710c785f BVA |
1083 | * If no I/O scheduler has been configured it is possible that |
1084 | * the hardware queue got stopped and restarted before requests | |
1085 | * were pushed back onto the dispatch list. Rerun the queue to | |
1086 | * avoid starvation. Notes: | |
1087 | * - blk_mq_run_hw_queue() checks whether or not a queue has | |
1088 | * been stopped before rerunning a queue. | |
1089 | * - Some but not all block drivers stop a queue before | |
fc17b653 | 1090 | * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq |
710c785f | 1091 | * and dm-rq. |
bd166ef1 | 1092 | */ |
da55f2cc OS |
1093 | if (!blk_mq_sched_needs_restart(hctx) && |
1094 | !test_bit(BLK_MQ_S_TAG_WAITING, &hctx->state)) | |
bd166ef1 | 1095 | blk_mq_run_hw_queue(hctx, true); |
320ae51f | 1096 | } |
f04c3df3 | 1097 | |
93efe981 | 1098 | return (queued + errors) != 0; |
f04c3df3 JA |
1099 | } |
1100 | ||
6a83e74d BVA |
1101 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) |
1102 | { | |
1103 | int srcu_idx; | |
1104 | ||
1105 | WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) && | |
1106 | cpu_online(hctx->next_cpu)); | |
1107 | ||
1108 | if (!(hctx->flags & BLK_MQ_F_BLOCKING)) { | |
1109 | rcu_read_lock(); | |
bd166ef1 | 1110 | blk_mq_sched_dispatch_requests(hctx); |
6a83e74d BVA |
1111 | rcu_read_unlock(); |
1112 | } else { | |
bf4907c0 JA |
1113 | might_sleep(); |
1114 | ||
07319678 | 1115 | srcu_idx = srcu_read_lock(hctx->queue_rq_srcu); |
bd166ef1 | 1116 | blk_mq_sched_dispatch_requests(hctx); |
07319678 | 1117 | srcu_read_unlock(hctx->queue_rq_srcu, srcu_idx); |
6a83e74d BVA |
1118 | } |
1119 | } | |
1120 | ||
506e931f JA |
1121 | /* |
1122 | * It'd be great if the workqueue API had a way to pass | |
1123 | * in a mask and had some smarts for more clever placement. | |
1124 | * For now we just round-robin here, switching for every | |
1125 | * BLK_MQ_CPU_WORK_BATCH queued items. | |
1126 | */ | |
1127 | static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) | |
1128 | { | |
b657d7e6 CH |
1129 | if (hctx->queue->nr_hw_queues == 1) |
1130 | return WORK_CPU_UNBOUND; | |
506e931f JA |
1131 | |
1132 | if (--hctx->next_cpu_batch <= 0) { | |
c02ebfdd | 1133 | int next_cpu; |
506e931f JA |
1134 | |
1135 | next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask); | |
1136 | if (next_cpu >= nr_cpu_ids) | |
1137 | next_cpu = cpumask_first(hctx->cpumask); | |
1138 | ||
1139 | hctx->next_cpu = next_cpu; | |
1140 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; | |
1141 | } | |
1142 | ||
b657d7e6 | 1143 | return hctx->next_cpu; |
506e931f JA |
1144 | } |
1145 | ||
7587a5ae BVA |
1146 | static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async, |
1147 | unsigned long msecs) | |
320ae51f | 1148 | { |
5435c023 BVA |
1149 | if (WARN_ON_ONCE(!blk_mq_hw_queue_mapped(hctx))) |
1150 | return; | |
1151 | ||
1152 | if (unlikely(blk_mq_hctx_stopped(hctx))) | |
320ae51f JA |
1153 | return; |
1154 | ||
1b792f2f | 1155 | if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) { |
2a90d4aa PB |
1156 | int cpu = get_cpu(); |
1157 | if (cpumask_test_cpu(cpu, hctx->cpumask)) { | |
398205b8 | 1158 | __blk_mq_run_hw_queue(hctx); |
2a90d4aa | 1159 | put_cpu(); |
398205b8 PB |
1160 | return; |
1161 | } | |
e4043dcf | 1162 | |
2a90d4aa | 1163 | put_cpu(); |
e4043dcf | 1164 | } |
398205b8 | 1165 | |
9f993737 JA |
1166 | kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx), |
1167 | &hctx->run_work, | |
1168 | msecs_to_jiffies(msecs)); | |
7587a5ae BVA |
1169 | } |
1170 | ||
1171 | void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs) | |
1172 | { | |
1173 | __blk_mq_delay_run_hw_queue(hctx, true, msecs); | |
1174 | } | |
1175 | EXPORT_SYMBOL(blk_mq_delay_run_hw_queue); | |
1176 | ||
1177 | void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) | |
1178 | { | |
1179 | __blk_mq_delay_run_hw_queue(hctx, async, 0); | |
320ae51f | 1180 | } |
5b727272 | 1181 | EXPORT_SYMBOL(blk_mq_run_hw_queue); |
320ae51f | 1182 | |
b94ec296 | 1183 | void blk_mq_run_hw_queues(struct request_queue *q, bool async) |
320ae51f JA |
1184 | { |
1185 | struct blk_mq_hw_ctx *hctx; | |
1186 | int i; | |
1187 | ||
1188 | queue_for_each_hw_ctx(q, hctx, i) { | |
bd166ef1 | 1189 | if (!blk_mq_hctx_has_pending(hctx) || |
5d1b25c1 | 1190 | blk_mq_hctx_stopped(hctx)) |
320ae51f JA |
1191 | continue; |
1192 | ||
b94ec296 | 1193 | blk_mq_run_hw_queue(hctx, async); |
320ae51f JA |
1194 | } |
1195 | } | |
b94ec296 | 1196 | EXPORT_SYMBOL(blk_mq_run_hw_queues); |
320ae51f | 1197 | |
fd001443 BVA |
1198 | /** |
1199 | * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped | |
1200 | * @q: request queue. | |
1201 | * | |
1202 | * The caller is responsible for serializing this function against | |
1203 | * blk_mq_{start,stop}_hw_queue(). | |
1204 | */ | |
1205 | bool blk_mq_queue_stopped(struct request_queue *q) | |
1206 | { | |
1207 | struct blk_mq_hw_ctx *hctx; | |
1208 | int i; | |
1209 | ||
1210 | queue_for_each_hw_ctx(q, hctx, i) | |
1211 | if (blk_mq_hctx_stopped(hctx)) | |
1212 | return true; | |
1213 | ||
1214 | return false; | |
1215 | } | |
1216 | EXPORT_SYMBOL(blk_mq_queue_stopped); | |
1217 | ||
39a70c76 ML |
1218 | /* |
1219 | * This function is often used for pausing .queue_rq() by driver when | |
1220 | * there isn't enough resource or some conditions aren't satisfied, and | |
1221 | * BLK_MQ_RQ_QUEUE_BUSY is usually returned. | |
1222 | * | |
1223 | * We do not guarantee that dispatch can be drained or blocked | |
1224 | * after blk_mq_stop_hw_queue() returns. Please use | |
1225 | * blk_mq_quiesce_queue() for that requirement. | |
1226 | */ | |
2719aa21 JA |
1227 | void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx) |
1228 | { | |
641a9ed6 | 1229 | cancel_delayed_work(&hctx->run_work); |
280d45f6 | 1230 | |
641a9ed6 | 1231 | set_bit(BLK_MQ_S_STOPPED, &hctx->state); |
2719aa21 | 1232 | } |
641a9ed6 | 1233 | EXPORT_SYMBOL(blk_mq_stop_hw_queue); |
2719aa21 | 1234 | |
39a70c76 ML |
1235 | /* |
1236 | * This function is often used for pausing .queue_rq() by driver when | |
1237 | * there isn't enough resource or some conditions aren't satisfied, and | |
1238 | * BLK_MQ_RQ_QUEUE_BUSY is usually returned. | |
1239 | * | |
1240 | * We do not guarantee that dispatch can be drained or blocked | |
1241 | * after blk_mq_stop_hw_queues() returns. Please use | |
1242 | * blk_mq_quiesce_queue() for that requirement. | |
1243 | */ | |
2719aa21 JA |
1244 | void blk_mq_stop_hw_queues(struct request_queue *q) |
1245 | { | |
641a9ed6 ML |
1246 | struct blk_mq_hw_ctx *hctx; |
1247 | int i; | |
1248 | ||
1249 | queue_for_each_hw_ctx(q, hctx, i) | |
1250 | blk_mq_stop_hw_queue(hctx); | |
280d45f6 CH |
1251 | } |
1252 | EXPORT_SYMBOL(blk_mq_stop_hw_queues); | |
1253 | ||
320ae51f JA |
1254 | void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx) |
1255 | { | |
1256 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
e4043dcf | 1257 | |
0ffbce80 | 1258 | blk_mq_run_hw_queue(hctx, false); |
320ae51f JA |
1259 | } |
1260 | EXPORT_SYMBOL(blk_mq_start_hw_queue); | |
1261 | ||
2f268556 CH |
1262 | void blk_mq_start_hw_queues(struct request_queue *q) |
1263 | { | |
1264 | struct blk_mq_hw_ctx *hctx; | |
1265 | int i; | |
1266 | ||
1267 | queue_for_each_hw_ctx(q, hctx, i) | |
1268 | blk_mq_start_hw_queue(hctx); | |
1269 | } | |
1270 | EXPORT_SYMBOL(blk_mq_start_hw_queues); | |
1271 | ||
ae911c5e JA |
1272 | void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) |
1273 | { | |
1274 | if (!blk_mq_hctx_stopped(hctx)) | |
1275 | return; | |
1276 | ||
1277 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
1278 | blk_mq_run_hw_queue(hctx, async); | |
1279 | } | |
1280 | EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue); | |
1281 | ||
1b4a3258 | 1282 | void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async) |
320ae51f JA |
1283 | { |
1284 | struct blk_mq_hw_ctx *hctx; | |
1285 | int i; | |
1286 | ||
ae911c5e JA |
1287 | queue_for_each_hw_ctx(q, hctx, i) |
1288 | blk_mq_start_stopped_hw_queue(hctx, async); | |
320ae51f JA |
1289 | } |
1290 | EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues); | |
1291 | ||
70f4db63 | 1292 | static void blk_mq_run_work_fn(struct work_struct *work) |
320ae51f JA |
1293 | { |
1294 | struct blk_mq_hw_ctx *hctx; | |
1295 | ||
9f993737 | 1296 | hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work); |
320ae51f | 1297 | |
21c6e939 JA |
1298 | /* |
1299 | * If we are stopped, don't run the queue. The exception is if | |
1300 | * BLK_MQ_S_START_ON_RUN is set. For that case, we auto-clear | |
1301 | * the STOPPED bit and run it. | |
1302 | */ | |
1303 | if (test_bit(BLK_MQ_S_STOPPED, &hctx->state)) { | |
1304 | if (!test_bit(BLK_MQ_S_START_ON_RUN, &hctx->state)) | |
1305 | return; | |
7587a5ae | 1306 | |
21c6e939 JA |
1307 | clear_bit(BLK_MQ_S_START_ON_RUN, &hctx->state); |
1308 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
1309 | } | |
7587a5ae BVA |
1310 | |
1311 | __blk_mq_run_hw_queue(hctx); | |
1312 | } | |
1313 | ||
70f4db63 CH |
1314 | |
1315 | void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs) | |
1316 | { | |
5435c023 | 1317 | if (WARN_ON_ONCE(!blk_mq_hw_queue_mapped(hctx))) |
19c66e59 | 1318 | return; |
70f4db63 | 1319 | |
21c6e939 JA |
1320 | /* |
1321 | * Stop the hw queue, then modify currently delayed work. | |
1322 | * This should prevent us from running the queue prematurely. | |
1323 | * Mark the queue as auto-clearing STOPPED when it runs. | |
1324 | */ | |
7e79dadc | 1325 | blk_mq_stop_hw_queue(hctx); |
21c6e939 JA |
1326 | set_bit(BLK_MQ_S_START_ON_RUN, &hctx->state); |
1327 | kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), | |
1328 | &hctx->run_work, | |
1329 | msecs_to_jiffies(msecs)); | |
70f4db63 CH |
1330 | } |
1331 | EXPORT_SYMBOL(blk_mq_delay_queue); | |
1332 | ||
cfd0c552 | 1333 | static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx, |
cfd0c552 ML |
1334 | struct request *rq, |
1335 | bool at_head) | |
320ae51f | 1336 | { |
e57690fe JA |
1337 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
1338 | ||
7b607814 BVA |
1339 | lockdep_assert_held(&ctx->lock); |
1340 | ||
01b983c9 JA |
1341 | trace_block_rq_insert(hctx->queue, rq); |
1342 | ||
72a0a36e CH |
1343 | if (at_head) |
1344 | list_add(&rq->queuelist, &ctx->rq_list); | |
1345 | else | |
1346 | list_add_tail(&rq->queuelist, &ctx->rq_list); | |
cfd0c552 | 1347 | } |
4bb659b1 | 1348 | |
2c3ad667 JA |
1349 | void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq, |
1350 | bool at_head) | |
cfd0c552 ML |
1351 | { |
1352 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
1353 | ||
7b607814 BVA |
1354 | lockdep_assert_held(&ctx->lock); |
1355 | ||
e57690fe | 1356 | __blk_mq_insert_req_list(hctx, rq, at_head); |
320ae51f | 1357 | blk_mq_hctx_mark_pending(hctx, ctx); |
320ae51f JA |
1358 | } |
1359 | ||
bd166ef1 JA |
1360 | void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx, |
1361 | struct list_head *list) | |
320ae51f JA |
1362 | |
1363 | { | |
320ae51f JA |
1364 | /* |
1365 | * preemption doesn't flush plug list, so it's possible ctx->cpu is | |
1366 | * offline now | |
1367 | */ | |
1368 | spin_lock(&ctx->lock); | |
1369 | while (!list_empty(list)) { | |
1370 | struct request *rq; | |
1371 | ||
1372 | rq = list_first_entry(list, struct request, queuelist); | |
e57690fe | 1373 | BUG_ON(rq->mq_ctx != ctx); |
320ae51f | 1374 | list_del_init(&rq->queuelist); |
e57690fe | 1375 | __blk_mq_insert_req_list(hctx, rq, false); |
320ae51f | 1376 | } |
cfd0c552 | 1377 | blk_mq_hctx_mark_pending(hctx, ctx); |
320ae51f | 1378 | spin_unlock(&ctx->lock); |
320ae51f JA |
1379 | } |
1380 | ||
1381 | static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b) | |
1382 | { | |
1383 | struct request *rqa = container_of(a, struct request, queuelist); | |
1384 | struct request *rqb = container_of(b, struct request, queuelist); | |
1385 | ||
1386 | return !(rqa->mq_ctx < rqb->mq_ctx || | |
1387 | (rqa->mq_ctx == rqb->mq_ctx && | |
1388 | blk_rq_pos(rqa) < blk_rq_pos(rqb))); | |
1389 | } | |
1390 | ||
1391 | void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) | |
1392 | { | |
1393 | struct blk_mq_ctx *this_ctx; | |
1394 | struct request_queue *this_q; | |
1395 | struct request *rq; | |
1396 | LIST_HEAD(list); | |
1397 | LIST_HEAD(ctx_list); | |
1398 | unsigned int depth; | |
1399 | ||
1400 | list_splice_init(&plug->mq_list, &list); | |
1401 | ||
1402 | list_sort(NULL, &list, plug_ctx_cmp); | |
1403 | ||
1404 | this_q = NULL; | |
1405 | this_ctx = NULL; | |
1406 | depth = 0; | |
1407 | ||
1408 | while (!list_empty(&list)) { | |
1409 | rq = list_entry_rq(list.next); | |
1410 | list_del_init(&rq->queuelist); | |
1411 | BUG_ON(!rq->q); | |
1412 | if (rq->mq_ctx != this_ctx) { | |
1413 | if (this_ctx) { | |
bd166ef1 JA |
1414 | trace_block_unplug(this_q, depth, from_schedule); |
1415 | blk_mq_sched_insert_requests(this_q, this_ctx, | |
1416 | &ctx_list, | |
1417 | from_schedule); | |
320ae51f JA |
1418 | } |
1419 | ||
1420 | this_ctx = rq->mq_ctx; | |
1421 | this_q = rq->q; | |
1422 | depth = 0; | |
1423 | } | |
1424 | ||
1425 | depth++; | |
1426 | list_add_tail(&rq->queuelist, &ctx_list); | |
1427 | } | |
1428 | ||
1429 | /* | |
1430 | * If 'this_ctx' is set, we know we have entries to complete | |
1431 | * on 'ctx_list'. Do those. | |
1432 | */ | |
1433 | if (this_ctx) { | |
bd166ef1 JA |
1434 | trace_block_unplug(this_q, depth, from_schedule); |
1435 | blk_mq_sched_insert_requests(this_q, this_ctx, &ctx_list, | |
1436 | from_schedule); | |
320ae51f JA |
1437 | } |
1438 | } | |
1439 | ||
1440 | static void blk_mq_bio_to_request(struct request *rq, struct bio *bio) | |
1441 | { | |
da8d7f07 | 1442 | blk_init_request_from_bio(rq, bio); |
4b570521 | 1443 | |
6e85eaf3 | 1444 | blk_account_io_start(rq, true); |
320ae51f JA |
1445 | } |
1446 | ||
274a5843 JA |
1447 | static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx) |
1448 | { | |
1449 | return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) && | |
1450 | !blk_queue_nomerges(hctx->queue); | |
1451 | } | |
1452 | ||
ab42f35d ML |
1453 | static inline void blk_mq_queue_io(struct blk_mq_hw_ctx *hctx, |
1454 | struct blk_mq_ctx *ctx, | |
1455 | struct request *rq) | |
1456 | { | |
1457 | spin_lock(&ctx->lock); | |
1458 | __blk_mq_insert_request(hctx, rq, false); | |
1459 | spin_unlock(&ctx->lock); | |
07068d5b | 1460 | } |
14ec77f3 | 1461 | |
fd2d3326 JA |
1462 | static blk_qc_t request_to_qc_t(struct blk_mq_hw_ctx *hctx, struct request *rq) |
1463 | { | |
bd166ef1 JA |
1464 | if (rq->tag != -1) |
1465 | return blk_tag_to_qc_t(rq->tag, hctx->queue_num, false); | |
1466 | ||
1467 | return blk_tag_to_qc_t(rq->internal_tag, hctx->queue_num, true); | |
fd2d3326 JA |
1468 | } |
1469 | ||
d964f04a ML |
1470 | static void __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, |
1471 | struct request *rq, | |
1472 | blk_qc_t *cookie, bool may_sleep) | |
f984df1f | 1473 | { |
f984df1f | 1474 | struct request_queue *q = rq->q; |
f984df1f SL |
1475 | struct blk_mq_queue_data bd = { |
1476 | .rq = rq, | |
d945a365 | 1477 | .last = true, |
f984df1f | 1478 | }; |
bd166ef1 | 1479 | blk_qc_t new_cookie; |
f06345ad | 1480 | blk_status_t ret; |
d964f04a ML |
1481 | bool run_queue = true; |
1482 | ||
f4560ffe ML |
1483 | /* RCU or SRCU read lock is needed before checking quiesced flag */ |
1484 | if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) { | |
d964f04a ML |
1485 | run_queue = false; |
1486 | goto insert; | |
1487 | } | |
f984df1f | 1488 | |
bd166ef1 | 1489 | if (q->elevator) |
2253efc8 BVA |
1490 | goto insert; |
1491 | ||
d964f04a | 1492 | if (!blk_mq_get_driver_tag(rq, NULL, false)) |
bd166ef1 JA |
1493 | goto insert; |
1494 | ||
1495 | new_cookie = request_to_qc_t(hctx, rq); | |
1496 | ||
f984df1f SL |
1497 | /* |
1498 | * For OK queue, we are done. For error, kill it. Any other | |
1499 | * error (busy), just add it to our list as we previously | |
1500 | * would have done | |
1501 | */ | |
1502 | ret = q->mq_ops->queue_rq(hctx, &bd); | |
fc17b653 CH |
1503 | switch (ret) { |
1504 | case BLK_STS_OK: | |
7b371636 | 1505 | *cookie = new_cookie; |
2253efc8 | 1506 | return; |
fc17b653 CH |
1507 | case BLK_STS_RESOURCE: |
1508 | __blk_mq_requeue_request(rq); | |
1509 | goto insert; | |
1510 | default: | |
7b371636 | 1511 | *cookie = BLK_QC_T_NONE; |
fc17b653 | 1512 | blk_mq_end_request(rq, ret); |
2253efc8 | 1513 | return; |
f984df1f | 1514 | } |
7b371636 | 1515 | |
2253efc8 | 1516 | insert: |
d964f04a | 1517 | blk_mq_sched_insert_request(rq, false, run_queue, false, may_sleep); |
f984df1f SL |
1518 | } |
1519 | ||
5eb6126e CH |
1520 | static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, |
1521 | struct request *rq, blk_qc_t *cookie) | |
1522 | { | |
1523 | if (!(hctx->flags & BLK_MQ_F_BLOCKING)) { | |
1524 | rcu_read_lock(); | |
d964f04a | 1525 | __blk_mq_try_issue_directly(hctx, rq, cookie, false); |
5eb6126e CH |
1526 | rcu_read_unlock(); |
1527 | } else { | |
bf4907c0 JA |
1528 | unsigned int srcu_idx; |
1529 | ||
1530 | might_sleep(); | |
1531 | ||
07319678 | 1532 | srcu_idx = srcu_read_lock(hctx->queue_rq_srcu); |
d964f04a | 1533 | __blk_mq_try_issue_directly(hctx, rq, cookie, true); |
07319678 | 1534 | srcu_read_unlock(hctx->queue_rq_srcu, srcu_idx); |
5eb6126e CH |
1535 | } |
1536 | } | |
1537 | ||
dece1635 | 1538 | static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio) |
07068d5b | 1539 | { |
ef295ecf | 1540 | const int is_sync = op_is_sync(bio->bi_opf); |
f73f44eb | 1541 | const int is_flush_fua = op_is_flush(bio->bi_opf); |
5a797e00 | 1542 | struct blk_mq_alloc_data data = { .flags = 0 }; |
07068d5b | 1543 | struct request *rq; |
5eb6126e | 1544 | unsigned int request_count = 0; |
f984df1f | 1545 | struct blk_plug *plug; |
5b3f341f | 1546 | struct request *same_queue_rq = NULL; |
7b371636 | 1547 | blk_qc_t cookie; |
87760e5e | 1548 | unsigned int wb_acct; |
07068d5b JA |
1549 | |
1550 | blk_queue_bounce(q, &bio); | |
1551 | ||
af67c31f | 1552 | blk_queue_split(q, &bio); |
f36ea50c | 1553 | |
e23947bd | 1554 | if (!bio_integrity_prep(bio)) |
dece1635 | 1555 | return BLK_QC_T_NONE; |
07068d5b | 1556 | |
87c279e6 OS |
1557 | if (!is_flush_fua && !blk_queue_nomerges(q) && |
1558 | blk_attempt_plug_merge(q, bio, &request_count, &same_queue_rq)) | |
1559 | return BLK_QC_T_NONE; | |
f984df1f | 1560 | |
bd166ef1 JA |
1561 | if (blk_mq_sched_bio_merge(q, bio)) |
1562 | return BLK_QC_T_NONE; | |
1563 | ||
87760e5e JA |
1564 | wb_acct = wbt_wait(q->rq_wb, bio, NULL); |
1565 | ||
bd166ef1 JA |
1566 | trace_block_getrq(q, bio, bio->bi_opf); |
1567 | ||
d2c0d383 | 1568 | rq = blk_mq_get_request(q, bio, bio->bi_opf, &data); |
87760e5e JA |
1569 | if (unlikely(!rq)) { |
1570 | __wbt_done(q->rq_wb, wb_acct); | |
03a07c92 GR |
1571 | if (bio->bi_opf & REQ_NOWAIT) |
1572 | bio_wouldblock_error(bio); | |
dece1635 | 1573 | return BLK_QC_T_NONE; |
87760e5e JA |
1574 | } |
1575 | ||
1576 | wbt_track(&rq->issue_stat, wb_acct); | |
07068d5b | 1577 | |
fd2d3326 | 1578 | cookie = request_to_qc_t(data.hctx, rq); |
07068d5b | 1579 | |
f984df1f | 1580 | plug = current->plug; |
07068d5b | 1581 | if (unlikely(is_flush_fua)) { |
f984df1f | 1582 | blk_mq_put_ctx(data.ctx); |
07068d5b | 1583 | blk_mq_bio_to_request(rq, bio); |
a4d907b6 CH |
1584 | if (q->elevator) { |
1585 | blk_mq_sched_insert_request(rq, false, true, true, | |
1586 | true); | |
6a83e74d | 1587 | } else { |
a4d907b6 CH |
1588 | blk_insert_flush(rq); |
1589 | blk_mq_run_hw_queue(data.hctx, true); | |
6a83e74d | 1590 | } |
a4d907b6 | 1591 | } else if (plug && q->nr_hw_queues == 1) { |
600271d9 SL |
1592 | struct request *last = NULL; |
1593 | ||
b00c53e8 | 1594 | blk_mq_put_ctx(data.ctx); |
e6c4438b | 1595 | blk_mq_bio_to_request(rq, bio); |
0a6219a9 ML |
1596 | |
1597 | /* | |
1598 | * @request_count may become stale because of schedule | |
1599 | * out, so check the list again. | |
1600 | */ | |
1601 | if (list_empty(&plug->mq_list)) | |
1602 | request_count = 0; | |
254d259d CH |
1603 | else if (blk_queue_nomerges(q)) |
1604 | request_count = blk_plug_queued_count(q); | |
1605 | ||
676d0607 | 1606 | if (!request_count) |
e6c4438b | 1607 | trace_block_plug(q); |
600271d9 SL |
1608 | else |
1609 | last = list_entry_rq(plug->mq_list.prev); | |
b094f89c | 1610 | |
600271d9 SL |
1611 | if (request_count >= BLK_MAX_REQUEST_COUNT || (last && |
1612 | blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) { | |
e6c4438b JM |
1613 | blk_flush_plug_list(plug, false); |
1614 | trace_block_plug(q); | |
320ae51f | 1615 | } |
b094f89c | 1616 | |
e6c4438b | 1617 | list_add_tail(&rq->queuelist, &plug->mq_list); |
2299722c | 1618 | } else if (plug && !blk_queue_nomerges(q)) { |
bd166ef1 | 1619 | blk_mq_bio_to_request(rq, bio); |
07068d5b | 1620 | |
07068d5b | 1621 | /* |
6a83e74d | 1622 | * We do limited plugging. If the bio can be merged, do that. |
f984df1f SL |
1623 | * Otherwise the existing request in the plug list will be |
1624 | * issued. So the plug list will have one request at most | |
2299722c CH |
1625 | * The plug list might get flushed before this. If that happens, |
1626 | * the plug list is empty, and same_queue_rq is invalid. | |
07068d5b | 1627 | */ |
2299722c CH |
1628 | if (list_empty(&plug->mq_list)) |
1629 | same_queue_rq = NULL; | |
1630 | if (same_queue_rq) | |
1631 | list_del_init(&same_queue_rq->queuelist); | |
1632 | list_add_tail(&rq->queuelist, &plug->mq_list); | |
1633 | ||
bf4907c0 JA |
1634 | blk_mq_put_ctx(data.ctx); |
1635 | ||
dad7a3be ML |
1636 | if (same_queue_rq) { |
1637 | data.hctx = blk_mq_map_queue(q, | |
1638 | same_queue_rq->mq_ctx->cpu); | |
2299722c CH |
1639 | blk_mq_try_issue_directly(data.hctx, same_queue_rq, |
1640 | &cookie); | |
dad7a3be | 1641 | } |
a4d907b6 | 1642 | } else if (q->nr_hw_queues > 1 && is_sync) { |
bf4907c0 | 1643 | blk_mq_put_ctx(data.ctx); |
2299722c | 1644 | blk_mq_bio_to_request(rq, bio); |
2299722c | 1645 | blk_mq_try_issue_directly(data.hctx, rq, &cookie); |
a4d907b6 | 1646 | } else if (q->elevator) { |
b00c53e8 | 1647 | blk_mq_put_ctx(data.ctx); |
bd166ef1 | 1648 | blk_mq_bio_to_request(rq, bio); |
a4d907b6 | 1649 | blk_mq_sched_insert_request(rq, false, true, true, true); |
ab42f35d | 1650 | } else { |
b00c53e8 | 1651 | blk_mq_put_ctx(data.ctx); |
ab42f35d ML |
1652 | blk_mq_bio_to_request(rq, bio); |
1653 | blk_mq_queue_io(data.hctx, data.ctx, rq); | |
a4d907b6 | 1654 | blk_mq_run_hw_queue(data.hctx, true); |
ab42f35d | 1655 | } |
320ae51f | 1656 | |
7b371636 | 1657 | return cookie; |
320ae51f JA |
1658 | } |
1659 | ||
cc71a6f4 JA |
1660 | void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags, |
1661 | unsigned int hctx_idx) | |
95363efd | 1662 | { |
e9b267d9 | 1663 | struct page *page; |
320ae51f | 1664 | |
24d2f903 | 1665 | if (tags->rqs && set->ops->exit_request) { |
e9b267d9 | 1666 | int i; |
320ae51f | 1667 | |
24d2f903 | 1668 | for (i = 0; i < tags->nr_tags; i++) { |
2af8cbe3 JA |
1669 | struct request *rq = tags->static_rqs[i]; |
1670 | ||
1671 | if (!rq) | |
e9b267d9 | 1672 | continue; |
d6296d39 | 1673 | set->ops->exit_request(set, rq, hctx_idx); |
2af8cbe3 | 1674 | tags->static_rqs[i] = NULL; |
e9b267d9 | 1675 | } |
320ae51f | 1676 | } |
320ae51f | 1677 | |
24d2f903 CH |
1678 | while (!list_empty(&tags->page_list)) { |
1679 | page = list_first_entry(&tags->page_list, struct page, lru); | |
6753471c | 1680 | list_del_init(&page->lru); |
f75782e4 CM |
1681 | /* |
1682 | * Remove kmemleak object previously allocated in | |
1683 | * blk_mq_init_rq_map(). | |
1684 | */ | |
1685 | kmemleak_free(page_address(page)); | |
320ae51f JA |
1686 | __free_pages(page, page->private); |
1687 | } | |
cc71a6f4 | 1688 | } |
320ae51f | 1689 | |
cc71a6f4 JA |
1690 | void blk_mq_free_rq_map(struct blk_mq_tags *tags) |
1691 | { | |
24d2f903 | 1692 | kfree(tags->rqs); |
cc71a6f4 | 1693 | tags->rqs = NULL; |
2af8cbe3 JA |
1694 | kfree(tags->static_rqs); |
1695 | tags->static_rqs = NULL; | |
320ae51f | 1696 | |
24d2f903 | 1697 | blk_mq_free_tags(tags); |
320ae51f JA |
1698 | } |
1699 | ||
cc71a6f4 JA |
1700 | struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, |
1701 | unsigned int hctx_idx, | |
1702 | unsigned int nr_tags, | |
1703 | unsigned int reserved_tags) | |
320ae51f | 1704 | { |
24d2f903 | 1705 | struct blk_mq_tags *tags; |
59f082e4 | 1706 | int node; |
320ae51f | 1707 | |
59f082e4 SL |
1708 | node = blk_mq_hw_queue_to_node(set->mq_map, hctx_idx); |
1709 | if (node == NUMA_NO_NODE) | |
1710 | node = set->numa_node; | |
1711 | ||
1712 | tags = blk_mq_init_tags(nr_tags, reserved_tags, node, | |
24391c0d | 1713 | BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags)); |
24d2f903 CH |
1714 | if (!tags) |
1715 | return NULL; | |
320ae51f | 1716 | |
cc71a6f4 | 1717 | tags->rqs = kzalloc_node(nr_tags * sizeof(struct request *), |
36e1f3d1 | 1718 | GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, |
59f082e4 | 1719 | node); |
24d2f903 CH |
1720 | if (!tags->rqs) { |
1721 | blk_mq_free_tags(tags); | |
1722 | return NULL; | |
1723 | } | |
320ae51f | 1724 | |
2af8cbe3 JA |
1725 | tags->static_rqs = kzalloc_node(nr_tags * sizeof(struct request *), |
1726 | GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, | |
59f082e4 | 1727 | node); |
2af8cbe3 JA |
1728 | if (!tags->static_rqs) { |
1729 | kfree(tags->rqs); | |
1730 | blk_mq_free_tags(tags); | |
1731 | return NULL; | |
1732 | } | |
1733 | ||
cc71a6f4 JA |
1734 | return tags; |
1735 | } | |
1736 | ||
1737 | static size_t order_to_size(unsigned int order) | |
1738 | { | |
1739 | return (size_t)PAGE_SIZE << order; | |
1740 | } | |
1741 | ||
1742 | int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags, | |
1743 | unsigned int hctx_idx, unsigned int depth) | |
1744 | { | |
1745 | unsigned int i, j, entries_per_page, max_order = 4; | |
1746 | size_t rq_size, left; | |
59f082e4 SL |
1747 | int node; |
1748 | ||
1749 | node = blk_mq_hw_queue_to_node(set->mq_map, hctx_idx); | |
1750 | if (node == NUMA_NO_NODE) | |
1751 | node = set->numa_node; | |
cc71a6f4 JA |
1752 | |
1753 | INIT_LIST_HEAD(&tags->page_list); | |
1754 | ||
320ae51f JA |
1755 | /* |
1756 | * rq_size is the size of the request plus driver payload, rounded | |
1757 | * to the cacheline size | |
1758 | */ | |
24d2f903 | 1759 | rq_size = round_up(sizeof(struct request) + set->cmd_size, |
320ae51f | 1760 | cache_line_size()); |
cc71a6f4 | 1761 | left = rq_size * depth; |
320ae51f | 1762 | |
cc71a6f4 | 1763 | for (i = 0; i < depth; ) { |
320ae51f JA |
1764 | int this_order = max_order; |
1765 | struct page *page; | |
1766 | int to_do; | |
1767 | void *p; | |
1768 | ||
b3a834b1 | 1769 | while (this_order && left < order_to_size(this_order - 1)) |
320ae51f JA |
1770 | this_order--; |
1771 | ||
1772 | do { | |
59f082e4 | 1773 | page = alloc_pages_node(node, |
36e1f3d1 | 1774 | GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO, |
a5164405 | 1775 | this_order); |
320ae51f JA |
1776 | if (page) |
1777 | break; | |
1778 | if (!this_order--) | |
1779 | break; | |
1780 | if (order_to_size(this_order) < rq_size) | |
1781 | break; | |
1782 | } while (1); | |
1783 | ||
1784 | if (!page) | |
24d2f903 | 1785 | goto fail; |
320ae51f JA |
1786 | |
1787 | page->private = this_order; | |
24d2f903 | 1788 | list_add_tail(&page->lru, &tags->page_list); |
320ae51f JA |
1789 | |
1790 | p = page_address(page); | |
f75782e4 CM |
1791 | /* |
1792 | * Allow kmemleak to scan these pages as they contain pointers | |
1793 | * to additional allocations like via ops->init_request(). | |
1794 | */ | |
36e1f3d1 | 1795 | kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO); |
320ae51f | 1796 | entries_per_page = order_to_size(this_order) / rq_size; |
cc71a6f4 | 1797 | to_do = min(entries_per_page, depth - i); |
320ae51f JA |
1798 | left -= to_do * rq_size; |
1799 | for (j = 0; j < to_do; j++) { | |
2af8cbe3 JA |
1800 | struct request *rq = p; |
1801 | ||
1802 | tags->static_rqs[i] = rq; | |
24d2f903 | 1803 | if (set->ops->init_request) { |
d6296d39 | 1804 | if (set->ops->init_request(set, rq, hctx_idx, |
59f082e4 | 1805 | node)) { |
2af8cbe3 | 1806 | tags->static_rqs[i] = NULL; |
24d2f903 | 1807 | goto fail; |
a5164405 | 1808 | } |
e9b267d9 CH |
1809 | } |
1810 | ||
320ae51f JA |
1811 | p += rq_size; |
1812 | i++; | |
1813 | } | |
1814 | } | |
cc71a6f4 | 1815 | return 0; |
320ae51f | 1816 | |
24d2f903 | 1817 | fail: |
cc71a6f4 JA |
1818 | blk_mq_free_rqs(set, tags, hctx_idx); |
1819 | return -ENOMEM; | |
320ae51f JA |
1820 | } |
1821 | ||
e57690fe JA |
1822 | /* |
1823 | * 'cpu' is going away. splice any existing rq_list entries from this | |
1824 | * software queue to the hw queue dispatch list, and ensure that it | |
1825 | * gets run. | |
1826 | */ | |
9467f859 | 1827 | static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node) |
484b4061 | 1828 | { |
9467f859 | 1829 | struct blk_mq_hw_ctx *hctx; |
484b4061 JA |
1830 | struct blk_mq_ctx *ctx; |
1831 | LIST_HEAD(tmp); | |
1832 | ||
9467f859 | 1833 | hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead); |
e57690fe | 1834 | ctx = __blk_mq_get_ctx(hctx->queue, cpu); |
484b4061 JA |
1835 | |
1836 | spin_lock(&ctx->lock); | |
1837 | if (!list_empty(&ctx->rq_list)) { | |
1838 | list_splice_init(&ctx->rq_list, &tmp); | |
1839 | blk_mq_hctx_clear_pending(hctx, ctx); | |
1840 | } | |
1841 | spin_unlock(&ctx->lock); | |
1842 | ||
1843 | if (list_empty(&tmp)) | |
9467f859 | 1844 | return 0; |
484b4061 | 1845 | |
e57690fe JA |
1846 | spin_lock(&hctx->lock); |
1847 | list_splice_tail_init(&tmp, &hctx->dispatch); | |
1848 | spin_unlock(&hctx->lock); | |
484b4061 JA |
1849 | |
1850 | blk_mq_run_hw_queue(hctx, true); | |
9467f859 | 1851 | return 0; |
484b4061 JA |
1852 | } |
1853 | ||
9467f859 | 1854 | static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx) |
484b4061 | 1855 | { |
9467f859 TG |
1856 | cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD, |
1857 | &hctx->cpuhp_dead); | |
484b4061 JA |
1858 | } |
1859 | ||
c3b4afca | 1860 | /* hctx->ctxs will be freed in queue's release handler */ |
08e98fc6 ML |
1861 | static void blk_mq_exit_hctx(struct request_queue *q, |
1862 | struct blk_mq_tag_set *set, | |
1863 | struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx) | |
1864 | { | |
9c1051aa OS |
1865 | blk_mq_debugfs_unregister_hctx(hctx); |
1866 | ||
08e98fc6 ML |
1867 | blk_mq_tag_idle(hctx); |
1868 | ||
f70ced09 | 1869 | if (set->ops->exit_request) |
d6296d39 | 1870 | set->ops->exit_request(set, hctx->fq->flush_rq, hctx_idx); |
f70ced09 | 1871 | |
93252632 OS |
1872 | blk_mq_sched_exit_hctx(q, hctx, hctx_idx); |
1873 | ||
08e98fc6 ML |
1874 | if (set->ops->exit_hctx) |
1875 | set->ops->exit_hctx(hctx, hctx_idx); | |
1876 | ||
6a83e74d | 1877 | if (hctx->flags & BLK_MQ_F_BLOCKING) |
07319678 | 1878 | cleanup_srcu_struct(hctx->queue_rq_srcu); |
6a83e74d | 1879 | |
9467f859 | 1880 | blk_mq_remove_cpuhp(hctx); |
f70ced09 | 1881 | blk_free_flush_queue(hctx->fq); |
88459642 | 1882 | sbitmap_free(&hctx->ctx_map); |
08e98fc6 ML |
1883 | } |
1884 | ||
624dbe47 ML |
1885 | static void blk_mq_exit_hw_queues(struct request_queue *q, |
1886 | struct blk_mq_tag_set *set, int nr_queue) | |
1887 | { | |
1888 | struct blk_mq_hw_ctx *hctx; | |
1889 | unsigned int i; | |
1890 | ||
1891 | queue_for_each_hw_ctx(q, hctx, i) { | |
1892 | if (i == nr_queue) | |
1893 | break; | |
08e98fc6 | 1894 | blk_mq_exit_hctx(q, set, hctx, i); |
624dbe47 | 1895 | } |
624dbe47 ML |
1896 | } |
1897 | ||
08e98fc6 ML |
1898 | static int blk_mq_init_hctx(struct request_queue *q, |
1899 | struct blk_mq_tag_set *set, | |
1900 | struct blk_mq_hw_ctx *hctx, unsigned hctx_idx) | |
320ae51f | 1901 | { |
08e98fc6 ML |
1902 | int node; |
1903 | ||
1904 | node = hctx->numa_node; | |
1905 | if (node == NUMA_NO_NODE) | |
1906 | node = hctx->numa_node = set->numa_node; | |
1907 | ||
9f993737 | 1908 | INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn); |
08e98fc6 ML |
1909 | spin_lock_init(&hctx->lock); |
1910 | INIT_LIST_HEAD(&hctx->dispatch); | |
1911 | hctx->queue = q; | |
2404e607 | 1912 | hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED; |
08e98fc6 | 1913 | |
9467f859 | 1914 | cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead); |
08e98fc6 ML |
1915 | |
1916 | hctx->tags = set->tags[hctx_idx]; | |
320ae51f JA |
1917 | |
1918 | /* | |
08e98fc6 ML |
1919 | * Allocate space for all possible cpus to avoid allocation at |
1920 | * runtime | |
320ae51f | 1921 | */ |
08e98fc6 ML |
1922 | hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *), |
1923 | GFP_KERNEL, node); | |
1924 | if (!hctx->ctxs) | |
1925 | goto unregister_cpu_notifier; | |
320ae51f | 1926 | |
88459642 OS |
1927 | if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8), GFP_KERNEL, |
1928 | node)) | |
08e98fc6 | 1929 | goto free_ctxs; |
320ae51f | 1930 | |
08e98fc6 | 1931 | hctx->nr_ctx = 0; |
320ae51f | 1932 | |
08e98fc6 ML |
1933 | if (set->ops->init_hctx && |
1934 | set->ops->init_hctx(hctx, set->driver_data, hctx_idx)) | |
1935 | goto free_bitmap; | |
320ae51f | 1936 | |
93252632 OS |
1937 | if (blk_mq_sched_init_hctx(q, hctx, hctx_idx)) |
1938 | goto exit_hctx; | |
1939 | ||
f70ced09 ML |
1940 | hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size); |
1941 | if (!hctx->fq) | |
93252632 | 1942 | goto sched_exit_hctx; |
320ae51f | 1943 | |
f70ced09 | 1944 | if (set->ops->init_request && |
d6296d39 CH |
1945 | set->ops->init_request(set, hctx->fq->flush_rq, hctx_idx, |
1946 | node)) | |
f70ced09 | 1947 | goto free_fq; |
320ae51f | 1948 | |
6a83e74d | 1949 | if (hctx->flags & BLK_MQ_F_BLOCKING) |
07319678 | 1950 | init_srcu_struct(hctx->queue_rq_srcu); |
6a83e74d | 1951 | |
9c1051aa OS |
1952 | blk_mq_debugfs_register_hctx(q, hctx); |
1953 | ||
08e98fc6 | 1954 | return 0; |
320ae51f | 1955 | |
f70ced09 ML |
1956 | free_fq: |
1957 | kfree(hctx->fq); | |
93252632 OS |
1958 | sched_exit_hctx: |
1959 | blk_mq_sched_exit_hctx(q, hctx, hctx_idx); | |
f70ced09 ML |
1960 | exit_hctx: |
1961 | if (set->ops->exit_hctx) | |
1962 | set->ops->exit_hctx(hctx, hctx_idx); | |
08e98fc6 | 1963 | free_bitmap: |
88459642 | 1964 | sbitmap_free(&hctx->ctx_map); |
08e98fc6 ML |
1965 | free_ctxs: |
1966 | kfree(hctx->ctxs); | |
1967 | unregister_cpu_notifier: | |
9467f859 | 1968 | blk_mq_remove_cpuhp(hctx); |
08e98fc6 ML |
1969 | return -1; |
1970 | } | |
320ae51f | 1971 | |
320ae51f JA |
1972 | static void blk_mq_init_cpu_queues(struct request_queue *q, |
1973 | unsigned int nr_hw_queues) | |
1974 | { | |
1975 | unsigned int i; | |
1976 | ||
1977 | for_each_possible_cpu(i) { | |
1978 | struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i); | |
1979 | struct blk_mq_hw_ctx *hctx; | |
1980 | ||
320ae51f JA |
1981 | __ctx->cpu = i; |
1982 | spin_lock_init(&__ctx->lock); | |
1983 | INIT_LIST_HEAD(&__ctx->rq_list); | |
1984 | __ctx->queue = q; | |
1985 | ||
4b855ad3 CH |
1986 | /* If the cpu isn't present, the cpu is mapped to first hctx */ |
1987 | if (!cpu_present(i)) | |
320ae51f JA |
1988 | continue; |
1989 | ||
7d7e0f90 | 1990 | hctx = blk_mq_map_queue(q, i); |
e4043dcf | 1991 | |
320ae51f JA |
1992 | /* |
1993 | * Set local node, IFF we have more than one hw queue. If | |
1994 | * not, we remain on the home node of the device | |
1995 | */ | |
1996 | if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE) | |
bffed457 | 1997 | hctx->numa_node = local_memory_node(cpu_to_node(i)); |
320ae51f JA |
1998 | } |
1999 | } | |
2000 | ||
cc71a6f4 JA |
2001 | static bool __blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, int hctx_idx) |
2002 | { | |
2003 | int ret = 0; | |
2004 | ||
2005 | set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx, | |
2006 | set->queue_depth, set->reserved_tags); | |
2007 | if (!set->tags[hctx_idx]) | |
2008 | return false; | |
2009 | ||
2010 | ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx, | |
2011 | set->queue_depth); | |
2012 | if (!ret) | |
2013 | return true; | |
2014 | ||
2015 | blk_mq_free_rq_map(set->tags[hctx_idx]); | |
2016 | set->tags[hctx_idx] = NULL; | |
2017 | return false; | |
2018 | } | |
2019 | ||
2020 | static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set, | |
2021 | unsigned int hctx_idx) | |
2022 | { | |
bd166ef1 JA |
2023 | if (set->tags[hctx_idx]) { |
2024 | blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx); | |
2025 | blk_mq_free_rq_map(set->tags[hctx_idx]); | |
2026 | set->tags[hctx_idx] = NULL; | |
2027 | } | |
cc71a6f4 JA |
2028 | } |
2029 | ||
4b855ad3 | 2030 | static void blk_mq_map_swqueue(struct request_queue *q) |
320ae51f | 2031 | { |
d1b1cea1 | 2032 | unsigned int i, hctx_idx; |
320ae51f JA |
2033 | struct blk_mq_hw_ctx *hctx; |
2034 | struct blk_mq_ctx *ctx; | |
2a34c087 | 2035 | struct blk_mq_tag_set *set = q->tag_set; |
320ae51f | 2036 | |
60de074b AM |
2037 | /* |
2038 | * Avoid others reading imcomplete hctx->cpumask through sysfs | |
2039 | */ | |
2040 | mutex_lock(&q->sysfs_lock); | |
2041 | ||
320ae51f | 2042 | queue_for_each_hw_ctx(q, hctx, i) { |
e4043dcf | 2043 | cpumask_clear(hctx->cpumask); |
320ae51f JA |
2044 | hctx->nr_ctx = 0; |
2045 | } | |
2046 | ||
2047 | /* | |
4b855ad3 CH |
2048 | * Map software to hardware queues. |
2049 | * | |
2050 | * If the cpu isn't present, the cpu is mapped to first hctx. | |
320ae51f | 2051 | */ |
4b855ad3 | 2052 | for_each_present_cpu(i) { |
d1b1cea1 GKB |
2053 | hctx_idx = q->mq_map[i]; |
2054 | /* unmapped hw queue can be remapped after CPU topo changed */ | |
cc71a6f4 JA |
2055 | if (!set->tags[hctx_idx] && |
2056 | !__blk_mq_alloc_rq_map(set, hctx_idx)) { | |
d1b1cea1 GKB |
2057 | /* |
2058 | * If tags initialization fail for some hctx, | |
2059 | * that hctx won't be brought online. In this | |
2060 | * case, remap the current ctx to hctx[0] which | |
2061 | * is guaranteed to always have tags allocated | |
2062 | */ | |
cc71a6f4 | 2063 | q->mq_map[i] = 0; |
d1b1cea1 GKB |
2064 | } |
2065 | ||
897bb0c7 | 2066 | ctx = per_cpu_ptr(q->queue_ctx, i); |
7d7e0f90 | 2067 | hctx = blk_mq_map_queue(q, i); |
868f2f0b | 2068 | |
e4043dcf | 2069 | cpumask_set_cpu(i, hctx->cpumask); |
320ae51f JA |
2070 | ctx->index_hw = hctx->nr_ctx; |
2071 | hctx->ctxs[hctx->nr_ctx++] = ctx; | |
2072 | } | |
506e931f | 2073 | |
60de074b AM |
2074 | mutex_unlock(&q->sysfs_lock); |
2075 | ||
506e931f | 2076 | queue_for_each_hw_ctx(q, hctx, i) { |
484b4061 | 2077 | /* |
a68aafa5 JA |
2078 | * If no software queues are mapped to this hardware queue, |
2079 | * disable it and free the request entries. | |
484b4061 JA |
2080 | */ |
2081 | if (!hctx->nr_ctx) { | |
d1b1cea1 GKB |
2082 | /* Never unmap queue 0. We need it as a |
2083 | * fallback in case of a new remap fails | |
2084 | * allocation | |
2085 | */ | |
cc71a6f4 JA |
2086 | if (i && set->tags[i]) |
2087 | blk_mq_free_map_and_requests(set, i); | |
2088 | ||
2a34c087 | 2089 | hctx->tags = NULL; |
484b4061 JA |
2090 | continue; |
2091 | } | |
2092 | ||
2a34c087 ML |
2093 | hctx->tags = set->tags[i]; |
2094 | WARN_ON(!hctx->tags); | |
2095 | ||
889fa31f CY |
2096 | /* |
2097 | * Set the map size to the number of mapped software queues. | |
2098 | * This is more accurate and more efficient than looping | |
2099 | * over all possibly mapped software queues. | |
2100 | */ | |
88459642 | 2101 | sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx); |
889fa31f | 2102 | |
484b4061 JA |
2103 | /* |
2104 | * Initialize batch roundrobin counts | |
2105 | */ | |
506e931f JA |
2106 | hctx->next_cpu = cpumask_first(hctx->cpumask); |
2107 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; | |
2108 | } | |
320ae51f JA |
2109 | } |
2110 | ||
8e8320c9 JA |
2111 | /* |
2112 | * Caller needs to ensure that we're either frozen/quiesced, or that | |
2113 | * the queue isn't live yet. | |
2114 | */ | |
2404e607 | 2115 | static void queue_set_hctx_shared(struct request_queue *q, bool shared) |
0d2602ca JA |
2116 | { |
2117 | struct blk_mq_hw_ctx *hctx; | |
0d2602ca JA |
2118 | int i; |
2119 | ||
2404e607 | 2120 | queue_for_each_hw_ctx(q, hctx, i) { |
8e8320c9 JA |
2121 | if (shared) { |
2122 | if (test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state)) | |
2123 | atomic_inc(&q->shared_hctx_restart); | |
2404e607 | 2124 | hctx->flags |= BLK_MQ_F_TAG_SHARED; |
8e8320c9 JA |
2125 | } else { |
2126 | if (test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state)) | |
2127 | atomic_dec(&q->shared_hctx_restart); | |
2404e607 | 2128 | hctx->flags &= ~BLK_MQ_F_TAG_SHARED; |
8e8320c9 | 2129 | } |
2404e607 JM |
2130 | } |
2131 | } | |
2132 | ||
8e8320c9 JA |
2133 | static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set, |
2134 | bool shared) | |
2404e607 JM |
2135 | { |
2136 | struct request_queue *q; | |
0d2602ca | 2137 | |
705cda97 BVA |
2138 | lockdep_assert_held(&set->tag_list_lock); |
2139 | ||
0d2602ca JA |
2140 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
2141 | blk_mq_freeze_queue(q); | |
2404e607 | 2142 | queue_set_hctx_shared(q, shared); |
0d2602ca JA |
2143 | blk_mq_unfreeze_queue(q); |
2144 | } | |
2145 | } | |
2146 | ||
2147 | static void blk_mq_del_queue_tag_set(struct request_queue *q) | |
2148 | { | |
2149 | struct blk_mq_tag_set *set = q->tag_set; | |
2150 | ||
0d2602ca | 2151 | mutex_lock(&set->tag_list_lock); |
705cda97 BVA |
2152 | list_del_rcu(&q->tag_set_list); |
2153 | INIT_LIST_HEAD(&q->tag_set_list); | |
2404e607 JM |
2154 | if (list_is_singular(&set->tag_list)) { |
2155 | /* just transitioned to unshared */ | |
2156 | set->flags &= ~BLK_MQ_F_TAG_SHARED; | |
2157 | /* update existing queue */ | |
2158 | blk_mq_update_tag_set_depth(set, false); | |
2159 | } | |
0d2602ca | 2160 | mutex_unlock(&set->tag_list_lock); |
705cda97 BVA |
2161 | |
2162 | synchronize_rcu(); | |
0d2602ca JA |
2163 | } |
2164 | ||
2165 | static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set, | |
2166 | struct request_queue *q) | |
2167 | { | |
2168 | q->tag_set = set; | |
2169 | ||
2170 | mutex_lock(&set->tag_list_lock); | |
2404e607 JM |
2171 | |
2172 | /* Check to see if we're transitioning to shared (from 1 to 2 queues). */ | |
2173 | if (!list_empty(&set->tag_list) && !(set->flags & BLK_MQ_F_TAG_SHARED)) { | |
2174 | set->flags |= BLK_MQ_F_TAG_SHARED; | |
2175 | /* update existing queue */ | |
2176 | blk_mq_update_tag_set_depth(set, true); | |
2177 | } | |
2178 | if (set->flags & BLK_MQ_F_TAG_SHARED) | |
2179 | queue_set_hctx_shared(q, true); | |
705cda97 | 2180 | list_add_tail_rcu(&q->tag_set_list, &set->tag_list); |
2404e607 | 2181 | |
0d2602ca JA |
2182 | mutex_unlock(&set->tag_list_lock); |
2183 | } | |
2184 | ||
e09aae7e ML |
2185 | /* |
2186 | * It is the actual release handler for mq, but we do it from | |
2187 | * request queue's release handler for avoiding use-after-free | |
2188 | * and headache because q->mq_kobj shouldn't have been introduced, | |
2189 | * but we can't group ctx/kctx kobj without it. | |
2190 | */ | |
2191 | void blk_mq_release(struct request_queue *q) | |
2192 | { | |
2193 | struct blk_mq_hw_ctx *hctx; | |
2194 | unsigned int i; | |
2195 | ||
2196 | /* hctx kobj stays in hctx */ | |
c3b4afca ML |
2197 | queue_for_each_hw_ctx(q, hctx, i) { |
2198 | if (!hctx) | |
2199 | continue; | |
6c8b232e | 2200 | kobject_put(&hctx->kobj); |
c3b4afca | 2201 | } |
e09aae7e | 2202 | |
a723bab3 AM |
2203 | q->mq_map = NULL; |
2204 | ||
e09aae7e ML |
2205 | kfree(q->queue_hw_ctx); |
2206 | ||
7ea5fe31 ML |
2207 | /* |
2208 | * release .mq_kobj and sw queue's kobject now because | |
2209 | * both share lifetime with request queue. | |
2210 | */ | |
2211 | blk_mq_sysfs_deinit(q); | |
2212 | ||
e09aae7e ML |
2213 | free_percpu(q->queue_ctx); |
2214 | } | |
2215 | ||
24d2f903 | 2216 | struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set) |
b62c21b7 MS |
2217 | { |
2218 | struct request_queue *uninit_q, *q; | |
2219 | ||
2220 | uninit_q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node); | |
2221 | if (!uninit_q) | |
2222 | return ERR_PTR(-ENOMEM); | |
2223 | ||
2224 | q = blk_mq_init_allocated_queue(set, uninit_q); | |
2225 | if (IS_ERR(q)) | |
2226 | blk_cleanup_queue(uninit_q); | |
2227 | ||
2228 | return q; | |
2229 | } | |
2230 | EXPORT_SYMBOL(blk_mq_init_queue); | |
2231 | ||
07319678 BVA |
2232 | static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set) |
2233 | { | |
2234 | int hw_ctx_size = sizeof(struct blk_mq_hw_ctx); | |
2235 | ||
2236 | BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, queue_rq_srcu), | |
2237 | __alignof__(struct blk_mq_hw_ctx)) != | |
2238 | sizeof(struct blk_mq_hw_ctx)); | |
2239 | ||
2240 | if (tag_set->flags & BLK_MQ_F_BLOCKING) | |
2241 | hw_ctx_size += sizeof(struct srcu_struct); | |
2242 | ||
2243 | return hw_ctx_size; | |
2244 | } | |
2245 | ||
868f2f0b KB |
2246 | static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set, |
2247 | struct request_queue *q) | |
320ae51f | 2248 | { |
868f2f0b KB |
2249 | int i, j; |
2250 | struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx; | |
f14bbe77 | 2251 | |
868f2f0b | 2252 | blk_mq_sysfs_unregister(q); |
24d2f903 | 2253 | for (i = 0; i < set->nr_hw_queues; i++) { |
868f2f0b | 2254 | int node; |
f14bbe77 | 2255 | |
868f2f0b KB |
2256 | if (hctxs[i]) |
2257 | continue; | |
2258 | ||
2259 | node = blk_mq_hw_queue_to_node(q->mq_map, i); | |
07319678 | 2260 | hctxs[i] = kzalloc_node(blk_mq_hw_ctx_size(set), |
cdef54dd | 2261 | GFP_KERNEL, node); |
320ae51f | 2262 | if (!hctxs[i]) |
868f2f0b | 2263 | break; |
320ae51f | 2264 | |
a86073e4 | 2265 | if (!zalloc_cpumask_var_node(&hctxs[i]->cpumask, GFP_KERNEL, |
868f2f0b KB |
2266 | node)) { |
2267 | kfree(hctxs[i]); | |
2268 | hctxs[i] = NULL; | |
2269 | break; | |
2270 | } | |
e4043dcf | 2271 | |
0d2602ca | 2272 | atomic_set(&hctxs[i]->nr_active, 0); |
f14bbe77 | 2273 | hctxs[i]->numa_node = node; |
320ae51f | 2274 | hctxs[i]->queue_num = i; |
868f2f0b KB |
2275 | |
2276 | if (blk_mq_init_hctx(q, set, hctxs[i], i)) { | |
2277 | free_cpumask_var(hctxs[i]->cpumask); | |
2278 | kfree(hctxs[i]); | |
2279 | hctxs[i] = NULL; | |
2280 | break; | |
2281 | } | |
2282 | blk_mq_hctx_kobj_init(hctxs[i]); | |
320ae51f | 2283 | } |
868f2f0b KB |
2284 | for (j = i; j < q->nr_hw_queues; j++) { |
2285 | struct blk_mq_hw_ctx *hctx = hctxs[j]; | |
2286 | ||
2287 | if (hctx) { | |
cc71a6f4 JA |
2288 | if (hctx->tags) |
2289 | blk_mq_free_map_and_requests(set, j); | |
868f2f0b | 2290 | blk_mq_exit_hctx(q, set, hctx, j); |
868f2f0b | 2291 | kobject_put(&hctx->kobj); |
868f2f0b KB |
2292 | hctxs[j] = NULL; |
2293 | ||
2294 | } | |
2295 | } | |
2296 | q->nr_hw_queues = i; | |
2297 | blk_mq_sysfs_register(q); | |
2298 | } | |
2299 | ||
2300 | struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set, | |
2301 | struct request_queue *q) | |
2302 | { | |
66841672 ML |
2303 | /* mark the queue as mq asap */ |
2304 | q->mq_ops = set->ops; | |
2305 | ||
34dbad5d | 2306 | q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn, |
720b8ccc SB |
2307 | blk_mq_poll_stats_bkt, |
2308 | BLK_MQ_POLL_STATS_BKTS, q); | |
34dbad5d OS |
2309 | if (!q->poll_cb) |
2310 | goto err_exit; | |
2311 | ||
868f2f0b KB |
2312 | q->queue_ctx = alloc_percpu(struct blk_mq_ctx); |
2313 | if (!q->queue_ctx) | |
c7de5726 | 2314 | goto err_exit; |
868f2f0b | 2315 | |
737f98cf ML |
2316 | /* init q->mq_kobj and sw queues' kobjects */ |
2317 | blk_mq_sysfs_init(q); | |
2318 | ||
868f2f0b KB |
2319 | q->queue_hw_ctx = kzalloc_node(nr_cpu_ids * sizeof(*(q->queue_hw_ctx)), |
2320 | GFP_KERNEL, set->numa_node); | |
2321 | if (!q->queue_hw_ctx) | |
2322 | goto err_percpu; | |
2323 | ||
bdd17e75 | 2324 | q->mq_map = set->mq_map; |
868f2f0b KB |
2325 | |
2326 | blk_mq_realloc_hw_ctxs(set, q); | |
2327 | if (!q->nr_hw_queues) | |
2328 | goto err_hctxs; | |
320ae51f | 2329 | |
287922eb | 2330 | INIT_WORK(&q->timeout_work, blk_mq_timeout_work); |
e56f698b | 2331 | blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ); |
320ae51f JA |
2332 | |
2333 | q->nr_queues = nr_cpu_ids; | |
320ae51f | 2334 | |
94eddfbe | 2335 | q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT; |
320ae51f | 2336 | |
05f1dd53 JA |
2337 | if (!(set->flags & BLK_MQ_F_SG_MERGE)) |
2338 | q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE; | |
2339 | ||
1be036e9 CH |
2340 | q->sg_reserved_size = INT_MAX; |
2341 | ||
2849450a | 2342 | INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work); |
6fca6a61 CH |
2343 | INIT_LIST_HEAD(&q->requeue_list); |
2344 | spin_lock_init(&q->requeue_lock); | |
2345 | ||
254d259d | 2346 | blk_queue_make_request(q, blk_mq_make_request); |
07068d5b | 2347 | |
eba71768 JA |
2348 | /* |
2349 | * Do this after blk_queue_make_request() overrides it... | |
2350 | */ | |
2351 | q->nr_requests = set->queue_depth; | |
2352 | ||
64f1c21e JA |
2353 | /* |
2354 | * Default to classic polling | |
2355 | */ | |
2356 | q->poll_nsec = -1; | |
2357 | ||
24d2f903 CH |
2358 | if (set->ops->complete) |
2359 | blk_queue_softirq_done(q, set->ops->complete); | |
30a91cb4 | 2360 | |
24d2f903 | 2361 | blk_mq_init_cpu_queues(q, set->nr_hw_queues); |
0d2602ca | 2362 | blk_mq_add_queue_tag_set(set, q); |
4b855ad3 | 2363 | blk_mq_map_swqueue(q); |
4593fdbe | 2364 | |
d3484991 JA |
2365 | if (!(set->flags & BLK_MQ_F_NO_SCHED)) { |
2366 | int ret; | |
2367 | ||
2368 | ret = blk_mq_sched_init(q); | |
2369 | if (ret) | |
2370 | return ERR_PTR(ret); | |
2371 | } | |
2372 | ||
320ae51f | 2373 | return q; |
18741986 | 2374 | |
320ae51f | 2375 | err_hctxs: |
868f2f0b | 2376 | kfree(q->queue_hw_ctx); |
320ae51f | 2377 | err_percpu: |
868f2f0b | 2378 | free_percpu(q->queue_ctx); |
c7de5726 ML |
2379 | err_exit: |
2380 | q->mq_ops = NULL; | |
320ae51f JA |
2381 | return ERR_PTR(-ENOMEM); |
2382 | } | |
b62c21b7 | 2383 | EXPORT_SYMBOL(blk_mq_init_allocated_queue); |
320ae51f JA |
2384 | |
2385 | void blk_mq_free_queue(struct request_queue *q) | |
2386 | { | |
624dbe47 | 2387 | struct blk_mq_tag_set *set = q->tag_set; |
320ae51f | 2388 | |
0d2602ca | 2389 | blk_mq_del_queue_tag_set(q); |
624dbe47 | 2390 | blk_mq_exit_hw_queues(q, set, set->nr_hw_queues); |
320ae51f | 2391 | } |
320ae51f JA |
2392 | |
2393 | /* Basically redo blk_mq_init_queue with queue frozen */ | |
4b855ad3 | 2394 | static void blk_mq_queue_reinit(struct request_queue *q) |
320ae51f | 2395 | { |
4ecd4fef | 2396 | WARN_ON_ONCE(!atomic_read(&q->mq_freeze_depth)); |
320ae51f | 2397 | |
9c1051aa | 2398 | blk_mq_debugfs_unregister_hctxs(q); |
67aec14c JA |
2399 | blk_mq_sysfs_unregister(q); |
2400 | ||
320ae51f JA |
2401 | /* |
2402 | * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe | |
2403 | * we should change hctx numa_node according to new topology (this | |
2404 | * involves free and re-allocate memory, worthy doing?) | |
2405 | */ | |
2406 | ||
4b855ad3 | 2407 | blk_mq_map_swqueue(q); |
320ae51f | 2408 | |
67aec14c | 2409 | blk_mq_sysfs_register(q); |
9c1051aa | 2410 | blk_mq_debugfs_register_hctxs(q); |
320ae51f JA |
2411 | } |
2412 | ||
a5164405 JA |
2413 | static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set) |
2414 | { | |
2415 | int i; | |
2416 | ||
cc71a6f4 JA |
2417 | for (i = 0; i < set->nr_hw_queues; i++) |
2418 | if (!__blk_mq_alloc_rq_map(set, i)) | |
a5164405 | 2419 | goto out_unwind; |
a5164405 JA |
2420 | |
2421 | return 0; | |
2422 | ||
2423 | out_unwind: | |
2424 | while (--i >= 0) | |
cc71a6f4 | 2425 | blk_mq_free_rq_map(set->tags[i]); |
a5164405 | 2426 | |
a5164405 JA |
2427 | return -ENOMEM; |
2428 | } | |
2429 | ||
2430 | /* | |
2431 | * Allocate the request maps associated with this tag_set. Note that this | |
2432 | * may reduce the depth asked for, if memory is tight. set->queue_depth | |
2433 | * will be updated to reflect the allocated depth. | |
2434 | */ | |
2435 | static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set) | |
2436 | { | |
2437 | unsigned int depth; | |
2438 | int err; | |
2439 | ||
2440 | depth = set->queue_depth; | |
2441 | do { | |
2442 | err = __blk_mq_alloc_rq_maps(set); | |
2443 | if (!err) | |
2444 | break; | |
2445 | ||
2446 | set->queue_depth >>= 1; | |
2447 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) { | |
2448 | err = -ENOMEM; | |
2449 | break; | |
2450 | } | |
2451 | } while (set->queue_depth); | |
2452 | ||
2453 | if (!set->queue_depth || err) { | |
2454 | pr_err("blk-mq: failed to allocate request map\n"); | |
2455 | return -ENOMEM; | |
2456 | } | |
2457 | ||
2458 | if (depth != set->queue_depth) | |
2459 | pr_info("blk-mq: reduced tag depth (%u -> %u)\n", | |
2460 | depth, set->queue_depth); | |
2461 | ||
2462 | return 0; | |
2463 | } | |
2464 | ||
ebe8bddb OS |
2465 | static int blk_mq_update_queue_map(struct blk_mq_tag_set *set) |
2466 | { | |
2467 | if (set->ops->map_queues) | |
2468 | return set->ops->map_queues(set); | |
2469 | else | |
2470 | return blk_mq_map_queues(set); | |
2471 | } | |
2472 | ||
a4391c64 JA |
2473 | /* |
2474 | * Alloc a tag set to be associated with one or more request queues. | |
2475 | * May fail with EINVAL for various error conditions. May adjust the | |
2476 | * requested depth down, if if it too large. In that case, the set | |
2477 | * value will be stored in set->queue_depth. | |
2478 | */ | |
24d2f903 CH |
2479 | int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) |
2480 | { | |
da695ba2 CH |
2481 | int ret; |
2482 | ||
205fb5f5 BVA |
2483 | BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS); |
2484 | ||
24d2f903 CH |
2485 | if (!set->nr_hw_queues) |
2486 | return -EINVAL; | |
a4391c64 | 2487 | if (!set->queue_depth) |
24d2f903 CH |
2488 | return -EINVAL; |
2489 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) | |
2490 | return -EINVAL; | |
2491 | ||
7d7e0f90 | 2492 | if (!set->ops->queue_rq) |
24d2f903 CH |
2493 | return -EINVAL; |
2494 | ||
a4391c64 JA |
2495 | if (set->queue_depth > BLK_MQ_MAX_DEPTH) { |
2496 | pr_info("blk-mq: reduced tag depth to %u\n", | |
2497 | BLK_MQ_MAX_DEPTH); | |
2498 | set->queue_depth = BLK_MQ_MAX_DEPTH; | |
2499 | } | |
24d2f903 | 2500 | |
6637fadf SL |
2501 | /* |
2502 | * If a crashdump is active, then we are potentially in a very | |
2503 | * memory constrained environment. Limit us to 1 queue and | |
2504 | * 64 tags to prevent using too much memory. | |
2505 | */ | |
2506 | if (is_kdump_kernel()) { | |
2507 | set->nr_hw_queues = 1; | |
2508 | set->queue_depth = min(64U, set->queue_depth); | |
2509 | } | |
868f2f0b KB |
2510 | /* |
2511 | * There is no use for more h/w queues than cpus. | |
2512 | */ | |
2513 | if (set->nr_hw_queues > nr_cpu_ids) | |
2514 | set->nr_hw_queues = nr_cpu_ids; | |
6637fadf | 2515 | |
868f2f0b | 2516 | set->tags = kzalloc_node(nr_cpu_ids * sizeof(struct blk_mq_tags *), |
24d2f903 CH |
2517 | GFP_KERNEL, set->numa_node); |
2518 | if (!set->tags) | |
a5164405 | 2519 | return -ENOMEM; |
24d2f903 | 2520 | |
da695ba2 CH |
2521 | ret = -ENOMEM; |
2522 | set->mq_map = kzalloc_node(sizeof(*set->mq_map) * nr_cpu_ids, | |
2523 | GFP_KERNEL, set->numa_node); | |
bdd17e75 CH |
2524 | if (!set->mq_map) |
2525 | goto out_free_tags; | |
2526 | ||
ebe8bddb | 2527 | ret = blk_mq_update_queue_map(set); |
da695ba2 CH |
2528 | if (ret) |
2529 | goto out_free_mq_map; | |
2530 | ||
2531 | ret = blk_mq_alloc_rq_maps(set); | |
2532 | if (ret) | |
bdd17e75 | 2533 | goto out_free_mq_map; |
24d2f903 | 2534 | |
0d2602ca JA |
2535 | mutex_init(&set->tag_list_lock); |
2536 | INIT_LIST_HEAD(&set->tag_list); | |
2537 | ||
24d2f903 | 2538 | return 0; |
bdd17e75 CH |
2539 | |
2540 | out_free_mq_map: | |
2541 | kfree(set->mq_map); | |
2542 | set->mq_map = NULL; | |
2543 | out_free_tags: | |
5676e7b6 RE |
2544 | kfree(set->tags); |
2545 | set->tags = NULL; | |
da695ba2 | 2546 | return ret; |
24d2f903 CH |
2547 | } |
2548 | EXPORT_SYMBOL(blk_mq_alloc_tag_set); | |
2549 | ||
2550 | void blk_mq_free_tag_set(struct blk_mq_tag_set *set) | |
2551 | { | |
2552 | int i; | |
2553 | ||
cc71a6f4 JA |
2554 | for (i = 0; i < nr_cpu_ids; i++) |
2555 | blk_mq_free_map_and_requests(set, i); | |
484b4061 | 2556 | |
bdd17e75 CH |
2557 | kfree(set->mq_map); |
2558 | set->mq_map = NULL; | |
2559 | ||
981bd189 | 2560 | kfree(set->tags); |
5676e7b6 | 2561 | set->tags = NULL; |
24d2f903 CH |
2562 | } |
2563 | EXPORT_SYMBOL(blk_mq_free_tag_set); | |
2564 | ||
e3a2b3f9 JA |
2565 | int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) |
2566 | { | |
2567 | struct blk_mq_tag_set *set = q->tag_set; | |
2568 | struct blk_mq_hw_ctx *hctx; | |
2569 | int i, ret; | |
2570 | ||
bd166ef1 | 2571 | if (!set) |
e3a2b3f9 JA |
2572 | return -EINVAL; |
2573 | ||
70f36b60 | 2574 | blk_mq_freeze_queue(q); |
70f36b60 | 2575 | |
e3a2b3f9 JA |
2576 | ret = 0; |
2577 | queue_for_each_hw_ctx(q, hctx, i) { | |
e9137d4b KB |
2578 | if (!hctx->tags) |
2579 | continue; | |
bd166ef1 JA |
2580 | /* |
2581 | * If we're using an MQ scheduler, just update the scheduler | |
2582 | * queue depth. This is similar to what the old code would do. | |
2583 | */ | |
70f36b60 JA |
2584 | if (!hctx->sched_tags) { |
2585 | ret = blk_mq_tag_update_depth(hctx, &hctx->tags, | |
2586 | min(nr, set->queue_depth), | |
2587 | false); | |
2588 | } else { | |
2589 | ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags, | |
2590 | nr, true); | |
2591 | } | |
e3a2b3f9 JA |
2592 | if (ret) |
2593 | break; | |
2594 | } | |
2595 | ||
2596 | if (!ret) | |
2597 | q->nr_requests = nr; | |
2598 | ||
70f36b60 | 2599 | blk_mq_unfreeze_queue(q); |
70f36b60 | 2600 | |
e3a2b3f9 JA |
2601 | return ret; |
2602 | } | |
2603 | ||
e4dc2b32 KB |
2604 | static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, |
2605 | int nr_hw_queues) | |
868f2f0b KB |
2606 | { |
2607 | struct request_queue *q; | |
2608 | ||
705cda97 BVA |
2609 | lockdep_assert_held(&set->tag_list_lock); |
2610 | ||
868f2f0b KB |
2611 | if (nr_hw_queues > nr_cpu_ids) |
2612 | nr_hw_queues = nr_cpu_ids; | |
2613 | if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues) | |
2614 | return; | |
2615 | ||
2616 | list_for_each_entry(q, &set->tag_list, tag_set_list) | |
2617 | blk_mq_freeze_queue(q); | |
2618 | ||
2619 | set->nr_hw_queues = nr_hw_queues; | |
ebe8bddb | 2620 | blk_mq_update_queue_map(set); |
868f2f0b KB |
2621 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
2622 | blk_mq_realloc_hw_ctxs(set, q); | |
4b855ad3 | 2623 | blk_mq_queue_reinit(q); |
868f2f0b KB |
2624 | } |
2625 | ||
2626 | list_for_each_entry(q, &set->tag_list, tag_set_list) | |
2627 | blk_mq_unfreeze_queue(q); | |
2628 | } | |
e4dc2b32 KB |
2629 | |
2630 | void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues) | |
2631 | { | |
2632 | mutex_lock(&set->tag_list_lock); | |
2633 | __blk_mq_update_nr_hw_queues(set, nr_hw_queues); | |
2634 | mutex_unlock(&set->tag_list_lock); | |
2635 | } | |
868f2f0b KB |
2636 | EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues); |
2637 | ||
34dbad5d OS |
2638 | /* Enable polling stats and return whether they were already enabled. */ |
2639 | static bool blk_poll_stats_enable(struct request_queue *q) | |
2640 | { | |
2641 | if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) || | |
2642 | test_and_set_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags)) | |
2643 | return true; | |
2644 | blk_stat_add_callback(q, q->poll_cb); | |
2645 | return false; | |
2646 | } | |
2647 | ||
2648 | static void blk_mq_poll_stats_start(struct request_queue *q) | |
2649 | { | |
2650 | /* | |
2651 | * We don't arm the callback if polling stats are not enabled or the | |
2652 | * callback is already active. | |
2653 | */ | |
2654 | if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) || | |
2655 | blk_stat_is_active(q->poll_cb)) | |
2656 | return; | |
2657 | ||
2658 | blk_stat_activate_msecs(q->poll_cb, 100); | |
2659 | } | |
2660 | ||
2661 | static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb) | |
2662 | { | |
2663 | struct request_queue *q = cb->data; | |
720b8ccc | 2664 | int bucket; |
34dbad5d | 2665 | |
720b8ccc SB |
2666 | for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) { |
2667 | if (cb->stat[bucket].nr_samples) | |
2668 | q->poll_stat[bucket] = cb->stat[bucket]; | |
2669 | } | |
34dbad5d OS |
2670 | } |
2671 | ||
64f1c21e JA |
2672 | static unsigned long blk_mq_poll_nsecs(struct request_queue *q, |
2673 | struct blk_mq_hw_ctx *hctx, | |
2674 | struct request *rq) | |
2675 | { | |
64f1c21e | 2676 | unsigned long ret = 0; |
720b8ccc | 2677 | int bucket; |
64f1c21e JA |
2678 | |
2679 | /* | |
2680 | * If stats collection isn't on, don't sleep but turn it on for | |
2681 | * future users | |
2682 | */ | |
34dbad5d | 2683 | if (!blk_poll_stats_enable(q)) |
64f1c21e JA |
2684 | return 0; |
2685 | ||
64f1c21e JA |
2686 | /* |
2687 | * As an optimistic guess, use half of the mean service time | |
2688 | * for this type of request. We can (and should) make this smarter. | |
2689 | * For instance, if the completion latencies are tight, we can | |
2690 | * get closer than just half the mean. This is especially | |
2691 | * important on devices where the completion latencies are longer | |
720b8ccc SB |
2692 | * than ~10 usec. We do use the stats for the relevant IO size |
2693 | * if available which does lead to better estimates. | |
64f1c21e | 2694 | */ |
720b8ccc SB |
2695 | bucket = blk_mq_poll_stats_bkt(rq); |
2696 | if (bucket < 0) | |
2697 | return ret; | |
2698 | ||
2699 | if (q->poll_stat[bucket].nr_samples) | |
2700 | ret = (q->poll_stat[bucket].mean + 1) / 2; | |
64f1c21e JA |
2701 | |
2702 | return ret; | |
2703 | } | |
2704 | ||
06426adf | 2705 | static bool blk_mq_poll_hybrid_sleep(struct request_queue *q, |
64f1c21e | 2706 | struct blk_mq_hw_ctx *hctx, |
06426adf JA |
2707 | struct request *rq) |
2708 | { | |
2709 | struct hrtimer_sleeper hs; | |
2710 | enum hrtimer_mode mode; | |
64f1c21e | 2711 | unsigned int nsecs; |
06426adf JA |
2712 | ktime_t kt; |
2713 | ||
64f1c21e JA |
2714 | if (test_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags)) |
2715 | return false; | |
2716 | ||
2717 | /* | |
2718 | * poll_nsec can be: | |
2719 | * | |
2720 | * -1: don't ever hybrid sleep | |
2721 | * 0: use half of prev avg | |
2722 | * >0: use this specific value | |
2723 | */ | |
2724 | if (q->poll_nsec == -1) | |
2725 | return false; | |
2726 | else if (q->poll_nsec > 0) | |
2727 | nsecs = q->poll_nsec; | |
2728 | else | |
2729 | nsecs = blk_mq_poll_nsecs(q, hctx, rq); | |
2730 | ||
2731 | if (!nsecs) | |
06426adf JA |
2732 | return false; |
2733 | ||
2734 | set_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags); | |
2735 | ||
2736 | /* | |
2737 | * This will be replaced with the stats tracking code, using | |
2738 | * 'avg_completion_time / 2' as the pre-sleep target. | |
2739 | */ | |
8b0e1953 | 2740 | kt = nsecs; |
06426adf JA |
2741 | |
2742 | mode = HRTIMER_MODE_REL; | |
2743 | hrtimer_init_on_stack(&hs.timer, CLOCK_MONOTONIC, mode); | |
2744 | hrtimer_set_expires(&hs.timer, kt); | |
2745 | ||
2746 | hrtimer_init_sleeper(&hs, current); | |
2747 | do { | |
2748 | if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags)) | |
2749 | break; | |
2750 | set_current_state(TASK_UNINTERRUPTIBLE); | |
2751 | hrtimer_start_expires(&hs.timer, mode); | |
2752 | if (hs.task) | |
2753 | io_schedule(); | |
2754 | hrtimer_cancel(&hs.timer); | |
2755 | mode = HRTIMER_MODE_ABS; | |
2756 | } while (hs.task && !signal_pending(current)); | |
2757 | ||
2758 | __set_current_state(TASK_RUNNING); | |
2759 | destroy_hrtimer_on_stack(&hs.timer); | |
2760 | return true; | |
2761 | } | |
2762 | ||
bbd7bb70 JA |
2763 | static bool __blk_mq_poll(struct blk_mq_hw_ctx *hctx, struct request *rq) |
2764 | { | |
2765 | struct request_queue *q = hctx->queue; | |
2766 | long state; | |
2767 | ||
06426adf JA |
2768 | /* |
2769 | * If we sleep, have the caller restart the poll loop to reset | |
2770 | * the state. Like for the other success return cases, the | |
2771 | * caller is responsible for checking if the IO completed. If | |
2772 | * the IO isn't complete, we'll get called again and will go | |
2773 | * straight to the busy poll loop. | |
2774 | */ | |
64f1c21e | 2775 | if (blk_mq_poll_hybrid_sleep(q, hctx, rq)) |
06426adf JA |
2776 | return true; |
2777 | ||
bbd7bb70 JA |
2778 | hctx->poll_considered++; |
2779 | ||
2780 | state = current->state; | |
2781 | while (!need_resched()) { | |
2782 | int ret; | |
2783 | ||
2784 | hctx->poll_invoked++; | |
2785 | ||
2786 | ret = q->mq_ops->poll(hctx, rq->tag); | |
2787 | if (ret > 0) { | |
2788 | hctx->poll_success++; | |
2789 | set_current_state(TASK_RUNNING); | |
2790 | return true; | |
2791 | } | |
2792 | ||
2793 | if (signal_pending_state(state, current)) | |
2794 | set_current_state(TASK_RUNNING); | |
2795 | ||
2796 | if (current->state == TASK_RUNNING) | |
2797 | return true; | |
2798 | if (ret < 0) | |
2799 | break; | |
2800 | cpu_relax(); | |
2801 | } | |
2802 | ||
2803 | return false; | |
2804 | } | |
2805 | ||
2806 | bool blk_mq_poll(struct request_queue *q, blk_qc_t cookie) | |
2807 | { | |
2808 | struct blk_mq_hw_ctx *hctx; | |
2809 | struct blk_plug *plug; | |
2810 | struct request *rq; | |
2811 | ||
2812 | if (!q->mq_ops || !q->mq_ops->poll || !blk_qc_t_valid(cookie) || | |
2813 | !test_bit(QUEUE_FLAG_POLL, &q->queue_flags)) | |
2814 | return false; | |
2815 | ||
2816 | plug = current->plug; | |
2817 | if (plug) | |
2818 | blk_flush_plug_list(plug, false); | |
2819 | ||
2820 | hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)]; | |
bd166ef1 JA |
2821 | if (!blk_qc_t_is_internal(cookie)) |
2822 | rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie)); | |
3a07bb1d | 2823 | else { |
bd166ef1 | 2824 | rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie)); |
3a07bb1d JA |
2825 | /* |
2826 | * With scheduling, if the request has completed, we'll | |
2827 | * get a NULL return here, as we clear the sched tag when | |
2828 | * that happens. The request still remains valid, like always, | |
2829 | * so we should be safe with just the NULL check. | |
2830 | */ | |
2831 | if (!rq) | |
2832 | return false; | |
2833 | } | |
bbd7bb70 JA |
2834 | |
2835 | return __blk_mq_poll(hctx, rq); | |
2836 | } | |
2837 | EXPORT_SYMBOL_GPL(blk_mq_poll); | |
2838 | ||
320ae51f JA |
2839 | static int __init blk_mq_init(void) |
2840 | { | |
9467f859 TG |
2841 | cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL, |
2842 | blk_mq_hctx_notify_dead); | |
320ae51f JA |
2843 | return 0; |
2844 | } | |
2845 | subsys_initcall(blk_mq_init); |