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