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