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> | |
12 | #include <linux/mm.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/slab.h> | |
15 | #include <linux/workqueue.h> | |
16 | #include <linux/smp.h> | |
17 | #include <linux/llist.h> | |
18 | #include <linux/list_sort.h> | |
19 | #include <linux/cpu.h> | |
20 | #include <linux/cache.h> | |
21 | #include <linux/sched/sysctl.h> | |
22 | #include <linux/delay.h> | |
23 | ||
24 | #include <trace/events/block.h> | |
25 | ||
26 | #include <linux/blk-mq.h> | |
27 | #include "blk.h" | |
28 | #include "blk-mq.h" | |
29 | #include "blk-mq-tag.h" | |
30 | ||
31 | static DEFINE_MUTEX(all_q_mutex); | |
32 | static LIST_HEAD(all_q_list); | |
33 | ||
34 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx); | |
35 | ||
320ae51f JA |
36 | /* |
37 | * Check if any of the ctx's have pending work in this hardware queue | |
38 | */ | |
39 | static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx) | |
40 | { | |
41 | unsigned int i; | |
42 | ||
1429d7c9 JA |
43 | for (i = 0; i < hctx->ctx_map.map_size; i++) |
44 | if (hctx->ctx_map.map[i].word) | |
320ae51f JA |
45 | return true; |
46 | ||
47 | return false; | |
48 | } | |
49 | ||
1429d7c9 JA |
50 | static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx, |
51 | struct blk_mq_ctx *ctx) | |
52 | { | |
53 | return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word]; | |
54 | } | |
55 | ||
56 | #define CTX_TO_BIT(hctx, ctx) \ | |
57 | ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1)) | |
58 | ||
320ae51f JA |
59 | /* |
60 | * Mark this ctx as having pending work in this hardware queue | |
61 | */ | |
62 | static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx, | |
63 | struct blk_mq_ctx *ctx) | |
64 | { | |
1429d7c9 JA |
65 | struct blk_align_bitmap *bm = get_bm(hctx, ctx); |
66 | ||
67 | if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word)) | |
68 | set_bit(CTX_TO_BIT(hctx, ctx), &bm->word); | |
69 | } | |
70 | ||
71 | static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx, | |
72 | struct blk_mq_ctx *ctx) | |
73 | { | |
74 | struct blk_align_bitmap *bm = get_bm(hctx, ctx); | |
75 | ||
76 | clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word); | |
320ae51f JA |
77 | } |
78 | ||
320ae51f JA |
79 | static int blk_mq_queue_enter(struct request_queue *q) |
80 | { | |
add703fd TH |
81 | while (true) { |
82 | int ret; | |
320ae51f | 83 | |
add703fd TH |
84 | if (percpu_ref_tryget_live(&q->mq_usage_counter)) |
85 | return 0; | |
320ae51f | 86 | |
add703fd TH |
87 | ret = wait_event_interruptible(q->mq_freeze_wq, |
88 | !q->mq_freeze_depth || blk_queue_dying(q)); | |
89 | if (blk_queue_dying(q)) | |
90 | return -ENODEV; | |
91 | if (ret) | |
92 | return ret; | |
93 | } | |
320ae51f JA |
94 | } |
95 | ||
96 | static void blk_mq_queue_exit(struct request_queue *q) | |
97 | { | |
add703fd TH |
98 | percpu_ref_put(&q->mq_usage_counter); |
99 | } | |
100 | ||
101 | static void blk_mq_usage_counter_release(struct percpu_ref *ref) | |
102 | { | |
103 | struct request_queue *q = | |
104 | container_of(ref, struct request_queue, mq_usage_counter); | |
105 | ||
106 | wake_up_all(&q->mq_freeze_wq); | |
320ae51f JA |
107 | } |
108 | ||
72d6f02a TH |
109 | /* |
110 | * Guarantee no request is in use, so we can change any data structure of | |
111 | * the queue afterward. | |
112 | */ | |
113 | void blk_mq_freeze_queue(struct request_queue *q) | |
43a5e4e2 | 114 | { |
cddd5d17 TH |
115 | bool freeze; |
116 | ||
72d6f02a | 117 | spin_lock_irq(q->queue_lock); |
cddd5d17 | 118 | freeze = !q->mq_freeze_depth++; |
72d6f02a TH |
119 | spin_unlock_irq(q->queue_lock); |
120 | ||
cddd5d17 TH |
121 | if (freeze) { |
122 | percpu_ref_kill(&q->mq_usage_counter); | |
123 | blk_mq_run_queues(q, false); | |
124 | } | |
add703fd | 125 | wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->mq_usage_counter)); |
43a5e4e2 ML |
126 | } |
127 | ||
320ae51f JA |
128 | static void blk_mq_unfreeze_queue(struct request_queue *q) |
129 | { | |
cddd5d17 | 130 | bool wake; |
320ae51f JA |
131 | |
132 | spin_lock_irq(q->queue_lock); | |
780db207 TH |
133 | wake = !--q->mq_freeze_depth; |
134 | WARN_ON_ONCE(q->mq_freeze_depth < 0); | |
320ae51f | 135 | spin_unlock_irq(q->queue_lock); |
add703fd TH |
136 | if (wake) { |
137 | percpu_ref_reinit(&q->mq_usage_counter); | |
320ae51f | 138 | wake_up_all(&q->mq_freeze_wq); |
add703fd | 139 | } |
320ae51f JA |
140 | } |
141 | ||
142 | bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx) | |
143 | { | |
144 | return blk_mq_has_free_tags(hctx->tags); | |
145 | } | |
146 | EXPORT_SYMBOL(blk_mq_can_queue); | |
147 | ||
94eddfbe JA |
148 | static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx, |
149 | struct request *rq, unsigned int rw_flags) | |
320ae51f | 150 | { |
94eddfbe JA |
151 | if (blk_queue_io_stat(q)) |
152 | rw_flags |= REQ_IO_STAT; | |
153 | ||
af76e555 CH |
154 | INIT_LIST_HEAD(&rq->queuelist); |
155 | /* csd/requeue_work/fifo_time is initialized before use */ | |
156 | rq->q = q; | |
320ae51f | 157 | rq->mq_ctx = ctx; |
0d2602ca | 158 | rq->cmd_flags |= rw_flags; |
af76e555 CH |
159 | /* do not touch atomic flags, it needs atomic ops against the timer */ |
160 | rq->cpu = -1; | |
af76e555 CH |
161 | INIT_HLIST_NODE(&rq->hash); |
162 | RB_CLEAR_NODE(&rq->rb_node); | |
af76e555 CH |
163 | rq->rq_disk = NULL; |
164 | rq->part = NULL; | |
3ee32372 | 165 | rq->start_time = jiffies; |
af76e555 CH |
166 | #ifdef CONFIG_BLK_CGROUP |
167 | rq->rl = NULL; | |
0fec08b4 | 168 | set_start_time_ns(rq); |
af76e555 CH |
169 | rq->io_start_time_ns = 0; |
170 | #endif | |
171 | rq->nr_phys_segments = 0; | |
172 | #if defined(CONFIG_BLK_DEV_INTEGRITY) | |
173 | rq->nr_integrity_segments = 0; | |
174 | #endif | |
af76e555 CH |
175 | rq->special = NULL; |
176 | /* tag was already set */ | |
177 | rq->errors = 0; | |
af76e555 | 178 | |
6f4a1626 TB |
179 | rq->cmd = rq->__cmd; |
180 | ||
af76e555 CH |
181 | rq->extra_len = 0; |
182 | rq->sense_len = 0; | |
183 | rq->resid_len = 0; | |
184 | rq->sense = NULL; | |
185 | ||
af76e555 | 186 | INIT_LIST_HEAD(&rq->timeout_list); |
f6be4fb4 JA |
187 | rq->timeout = 0; |
188 | ||
af76e555 CH |
189 | rq->end_io = NULL; |
190 | rq->end_io_data = NULL; | |
191 | rq->next_rq = NULL; | |
192 | ||
320ae51f JA |
193 | ctx->rq_dispatched[rw_is_sync(rw_flags)]++; |
194 | } | |
195 | ||
5dee8577 | 196 | static struct request * |
cb96a42c | 197 | __blk_mq_alloc_request(struct blk_mq_alloc_data *data, int rw) |
5dee8577 CH |
198 | { |
199 | struct request *rq; | |
200 | unsigned int tag; | |
201 | ||
cb96a42c | 202 | tag = blk_mq_get_tag(data); |
5dee8577 | 203 | if (tag != BLK_MQ_TAG_FAIL) { |
cb96a42c | 204 | rq = data->hctx->tags->rqs[tag]; |
5dee8577 | 205 | |
cb96a42c | 206 | if (blk_mq_tag_busy(data->hctx)) { |
5dee8577 | 207 | rq->cmd_flags = REQ_MQ_INFLIGHT; |
cb96a42c | 208 | atomic_inc(&data->hctx->nr_active); |
5dee8577 CH |
209 | } |
210 | ||
211 | rq->tag = tag; | |
cb96a42c | 212 | blk_mq_rq_ctx_init(data->q, data->ctx, rq, rw); |
5dee8577 CH |
213 | return rq; |
214 | } | |
215 | ||
216 | return NULL; | |
217 | } | |
218 | ||
4ce01dd1 CH |
219 | struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp, |
220 | bool reserved) | |
320ae51f | 221 | { |
d852564f CH |
222 | struct blk_mq_ctx *ctx; |
223 | struct blk_mq_hw_ctx *hctx; | |
320ae51f | 224 | struct request *rq; |
cb96a42c | 225 | struct blk_mq_alloc_data alloc_data; |
a492f075 | 226 | int ret; |
320ae51f | 227 | |
a492f075 JL |
228 | ret = blk_mq_queue_enter(q); |
229 | if (ret) | |
230 | return ERR_PTR(ret); | |
320ae51f | 231 | |
d852564f CH |
232 | ctx = blk_mq_get_ctx(q); |
233 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
cb96a42c ML |
234 | blk_mq_set_alloc_data(&alloc_data, q, gfp & ~__GFP_WAIT, |
235 | reserved, ctx, hctx); | |
d852564f | 236 | |
cb96a42c | 237 | rq = __blk_mq_alloc_request(&alloc_data, rw); |
d852564f CH |
238 | if (!rq && (gfp & __GFP_WAIT)) { |
239 | __blk_mq_run_hw_queue(hctx); | |
240 | blk_mq_put_ctx(ctx); | |
241 | ||
242 | ctx = blk_mq_get_ctx(q); | |
243 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
cb96a42c ML |
244 | blk_mq_set_alloc_data(&alloc_data, q, gfp, reserved, ctx, |
245 | hctx); | |
246 | rq = __blk_mq_alloc_request(&alloc_data, rw); | |
247 | ctx = alloc_data.ctx; | |
d852564f CH |
248 | } |
249 | blk_mq_put_ctx(ctx); | |
a492f075 JL |
250 | if (!rq) |
251 | return ERR_PTR(-EWOULDBLOCK); | |
320ae51f JA |
252 | return rq; |
253 | } | |
4bb659b1 | 254 | EXPORT_SYMBOL(blk_mq_alloc_request); |
320ae51f | 255 | |
320ae51f JA |
256 | static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx, |
257 | struct blk_mq_ctx *ctx, struct request *rq) | |
258 | { | |
259 | const int tag = rq->tag; | |
260 | struct request_queue *q = rq->q; | |
261 | ||
0d2602ca JA |
262 | if (rq->cmd_flags & REQ_MQ_INFLIGHT) |
263 | atomic_dec(&hctx->nr_active); | |
683d0e12 | 264 | rq->cmd_flags = 0; |
0d2602ca | 265 | |
af76e555 | 266 | clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
0d2602ca | 267 | blk_mq_put_tag(hctx, tag, &ctx->last_tag); |
320ae51f JA |
268 | blk_mq_queue_exit(q); |
269 | } | |
270 | ||
271 | void blk_mq_free_request(struct request *rq) | |
272 | { | |
273 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
274 | struct blk_mq_hw_ctx *hctx; | |
275 | struct request_queue *q = rq->q; | |
276 | ||
277 | ctx->rq_completed[rq_is_sync(rq)]++; | |
278 | ||
279 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
280 | __blk_mq_free_request(hctx, ctx, rq); | |
281 | } | |
282 | ||
8727af4b CH |
283 | /* |
284 | * Clone all relevant state from a request that has been put on hold in | |
285 | * the flush state machine into the preallocated flush request that hangs | |
286 | * off the request queue. | |
287 | * | |
288 | * For a driver the flush request should be invisible, that's why we are | |
289 | * impersonating the original request here. | |
290 | */ | |
291 | void blk_mq_clone_flush_request(struct request *flush_rq, | |
292 | struct request *orig_rq) | |
293 | { | |
294 | struct blk_mq_hw_ctx *hctx = | |
295 | orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu); | |
296 | ||
297 | flush_rq->mq_ctx = orig_rq->mq_ctx; | |
298 | flush_rq->tag = orig_rq->tag; | |
299 | memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq), | |
300 | hctx->cmd_size); | |
301 | } | |
302 | ||
c8a446ad | 303 | inline void __blk_mq_end_request(struct request *rq, int error) |
320ae51f | 304 | { |
0d11e6ac ML |
305 | blk_account_io_done(rq); |
306 | ||
91b63639 | 307 | if (rq->end_io) { |
320ae51f | 308 | rq->end_io(rq, error); |
91b63639 CH |
309 | } else { |
310 | if (unlikely(blk_bidi_rq(rq))) | |
311 | blk_mq_free_request(rq->next_rq); | |
320ae51f | 312 | blk_mq_free_request(rq); |
91b63639 | 313 | } |
320ae51f | 314 | } |
c8a446ad | 315 | EXPORT_SYMBOL(__blk_mq_end_request); |
63151a44 | 316 | |
c8a446ad | 317 | void blk_mq_end_request(struct request *rq, int error) |
63151a44 CH |
318 | { |
319 | if (blk_update_request(rq, error, blk_rq_bytes(rq))) | |
320 | BUG(); | |
c8a446ad | 321 | __blk_mq_end_request(rq, error); |
63151a44 | 322 | } |
c8a446ad | 323 | EXPORT_SYMBOL(blk_mq_end_request); |
320ae51f | 324 | |
30a91cb4 | 325 | static void __blk_mq_complete_request_remote(void *data) |
320ae51f | 326 | { |
3d6efbf6 | 327 | struct request *rq = data; |
320ae51f | 328 | |
30a91cb4 | 329 | rq->q->softirq_done_fn(rq); |
320ae51f | 330 | } |
320ae51f | 331 | |
ed851860 | 332 | static void blk_mq_ipi_complete_request(struct request *rq) |
320ae51f JA |
333 | { |
334 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
38535201 | 335 | bool shared = false; |
320ae51f JA |
336 | int cpu; |
337 | ||
38535201 | 338 | if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) { |
30a91cb4 CH |
339 | rq->q->softirq_done_fn(rq); |
340 | return; | |
341 | } | |
320ae51f JA |
342 | |
343 | cpu = get_cpu(); | |
38535201 CH |
344 | if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags)) |
345 | shared = cpus_share_cache(cpu, ctx->cpu); | |
346 | ||
347 | if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) { | |
30a91cb4 | 348 | rq->csd.func = __blk_mq_complete_request_remote; |
3d6efbf6 CH |
349 | rq->csd.info = rq; |
350 | rq->csd.flags = 0; | |
c46fff2a | 351 | smp_call_function_single_async(ctx->cpu, &rq->csd); |
3d6efbf6 | 352 | } else { |
30a91cb4 | 353 | rq->q->softirq_done_fn(rq); |
3d6efbf6 | 354 | } |
320ae51f JA |
355 | put_cpu(); |
356 | } | |
30a91cb4 | 357 | |
ed851860 JA |
358 | void __blk_mq_complete_request(struct request *rq) |
359 | { | |
360 | struct request_queue *q = rq->q; | |
361 | ||
362 | if (!q->softirq_done_fn) | |
c8a446ad | 363 | blk_mq_end_request(rq, rq->errors); |
ed851860 JA |
364 | else |
365 | blk_mq_ipi_complete_request(rq); | |
366 | } | |
367 | ||
30a91cb4 CH |
368 | /** |
369 | * blk_mq_complete_request - end I/O on a request | |
370 | * @rq: the request being processed | |
371 | * | |
372 | * Description: | |
373 | * Ends all I/O on a request. It does not handle partial completions. | |
374 | * The actual completion happens out-of-order, through a IPI handler. | |
375 | **/ | |
376 | void blk_mq_complete_request(struct request *rq) | |
377 | { | |
95f09684 JA |
378 | struct request_queue *q = rq->q; |
379 | ||
380 | if (unlikely(blk_should_fake_timeout(q))) | |
30a91cb4 | 381 | return; |
ed851860 JA |
382 | if (!blk_mark_rq_complete(rq)) |
383 | __blk_mq_complete_request(rq); | |
30a91cb4 CH |
384 | } |
385 | EXPORT_SYMBOL(blk_mq_complete_request); | |
320ae51f | 386 | |
e2490073 | 387 | void blk_mq_start_request(struct request *rq) |
320ae51f JA |
388 | { |
389 | struct request_queue *q = rq->q; | |
390 | ||
391 | trace_block_rq_issue(q, rq); | |
392 | ||
742ee69b | 393 | rq->resid_len = blk_rq_bytes(rq); |
91b63639 CH |
394 | if (unlikely(blk_bidi_rq(rq))) |
395 | rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq); | |
742ee69b | 396 | |
2b8393b4 | 397 | blk_add_timer(rq); |
87ee7b11 | 398 | |
538b7534 JA |
399 | /* |
400 | * Ensure that ->deadline is visible before set the started | |
401 | * flag and clear the completed flag. | |
402 | */ | |
403 | smp_mb__before_atomic(); | |
404 | ||
87ee7b11 JA |
405 | /* |
406 | * Mark us as started and clear complete. Complete might have been | |
407 | * set if requeue raced with timeout, which then marked it as | |
408 | * complete. So be sure to clear complete again when we start | |
409 | * the request, otherwise we'll ignore the completion event. | |
410 | */ | |
4b570521 JA |
411 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
412 | set_bit(REQ_ATOM_STARTED, &rq->atomic_flags); | |
413 | if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags)) | |
414 | clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags); | |
49f5baa5 CH |
415 | |
416 | if (q->dma_drain_size && blk_rq_bytes(rq)) { | |
417 | /* | |
418 | * Make sure space for the drain appears. We know we can do | |
419 | * this because max_hw_segments has been adjusted to be one | |
420 | * fewer than the device can handle. | |
421 | */ | |
422 | rq->nr_phys_segments++; | |
423 | } | |
320ae51f | 424 | } |
e2490073 | 425 | EXPORT_SYMBOL(blk_mq_start_request); |
320ae51f | 426 | |
ed0791b2 | 427 | static void __blk_mq_requeue_request(struct request *rq) |
320ae51f JA |
428 | { |
429 | struct request_queue *q = rq->q; | |
430 | ||
431 | trace_block_rq_requeue(q, rq); | |
49f5baa5 | 432 | |
e2490073 CH |
433 | if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) { |
434 | if (q->dma_drain_size && blk_rq_bytes(rq)) | |
435 | rq->nr_phys_segments--; | |
436 | } | |
320ae51f JA |
437 | } |
438 | ||
ed0791b2 CH |
439 | void blk_mq_requeue_request(struct request *rq) |
440 | { | |
ed0791b2 | 441 | __blk_mq_requeue_request(rq); |
ed0791b2 | 442 | |
ed0791b2 | 443 | BUG_ON(blk_queued_rq(rq)); |
6fca6a61 | 444 | blk_mq_add_to_requeue_list(rq, true); |
ed0791b2 CH |
445 | } |
446 | EXPORT_SYMBOL(blk_mq_requeue_request); | |
447 | ||
6fca6a61 CH |
448 | static void blk_mq_requeue_work(struct work_struct *work) |
449 | { | |
450 | struct request_queue *q = | |
451 | container_of(work, struct request_queue, requeue_work); | |
452 | LIST_HEAD(rq_list); | |
453 | struct request *rq, *next; | |
454 | unsigned long flags; | |
455 | ||
456 | spin_lock_irqsave(&q->requeue_lock, flags); | |
457 | list_splice_init(&q->requeue_list, &rq_list); | |
458 | spin_unlock_irqrestore(&q->requeue_lock, flags); | |
459 | ||
460 | list_for_each_entry_safe(rq, next, &rq_list, queuelist) { | |
461 | if (!(rq->cmd_flags & REQ_SOFTBARRIER)) | |
462 | continue; | |
463 | ||
464 | rq->cmd_flags &= ~REQ_SOFTBARRIER; | |
465 | list_del_init(&rq->queuelist); | |
466 | blk_mq_insert_request(rq, true, false, false); | |
467 | } | |
468 | ||
469 | while (!list_empty(&rq_list)) { | |
470 | rq = list_entry(rq_list.next, struct request, queuelist); | |
471 | list_del_init(&rq->queuelist); | |
472 | blk_mq_insert_request(rq, false, false, false); | |
473 | } | |
474 | ||
8b957415 JA |
475 | /* |
476 | * Use the start variant of queue running here, so that running | |
477 | * the requeue work will kick stopped queues. | |
478 | */ | |
479 | blk_mq_start_hw_queues(q); | |
6fca6a61 CH |
480 | } |
481 | ||
482 | void blk_mq_add_to_requeue_list(struct request *rq, bool at_head) | |
483 | { | |
484 | struct request_queue *q = rq->q; | |
485 | unsigned long flags; | |
486 | ||
487 | /* | |
488 | * We abuse this flag that is otherwise used by the I/O scheduler to | |
489 | * request head insertation from the workqueue. | |
490 | */ | |
491 | BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER); | |
492 | ||
493 | spin_lock_irqsave(&q->requeue_lock, flags); | |
494 | if (at_head) { | |
495 | rq->cmd_flags |= REQ_SOFTBARRIER; | |
496 | list_add(&rq->queuelist, &q->requeue_list); | |
497 | } else { | |
498 | list_add_tail(&rq->queuelist, &q->requeue_list); | |
499 | } | |
500 | spin_unlock_irqrestore(&q->requeue_lock, flags); | |
501 | } | |
502 | EXPORT_SYMBOL(blk_mq_add_to_requeue_list); | |
503 | ||
504 | void blk_mq_kick_requeue_list(struct request_queue *q) | |
505 | { | |
506 | kblockd_schedule_work(&q->requeue_work); | |
507 | } | |
508 | EXPORT_SYMBOL(blk_mq_kick_requeue_list); | |
509 | ||
0e62f51f | 510 | static inline bool is_flush_request(struct request *rq, unsigned int tag) |
24d2f903 | 511 | { |
0e62f51f JA |
512 | return ((rq->cmd_flags & REQ_FLUSH_SEQ) && |
513 | rq->q->flush_rq->tag == tag); | |
514 | } | |
515 | ||
516 | struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag) | |
517 | { | |
518 | struct request *rq = tags->rqs[tag]; | |
22302375 | 519 | |
0e62f51f JA |
520 | if (!is_flush_request(rq, tag)) |
521 | return rq; | |
22302375 | 522 | |
0e62f51f | 523 | return rq->q->flush_rq; |
24d2f903 CH |
524 | } |
525 | EXPORT_SYMBOL(blk_mq_tag_to_rq); | |
526 | ||
46f92d42 CH |
527 | struct blk_mq_timeout_data { |
528 | unsigned long next; | |
529 | unsigned int next_set; | |
530 | }; | |
531 | ||
0152fb6b | 532 | static void blk_mq_rq_timed_out(struct request *req, bool reserved) |
87ee7b11 | 533 | { |
46f92d42 CH |
534 | struct blk_mq_ops *ops = req->q->mq_ops; |
535 | enum blk_eh_timer_return ret = BLK_EH_RESET_TIMER; | |
87ee7b11 JA |
536 | |
537 | /* | |
538 | * We know that complete is set at this point. If STARTED isn't set | |
539 | * anymore, then the request isn't active and the "timeout" should | |
540 | * just be ignored. This can happen due to the bitflag ordering. | |
541 | * Timeout first checks if STARTED is set, and if it is, assumes | |
542 | * the request is active. But if we race with completion, then | |
543 | * we both flags will get cleared. So check here again, and ignore | |
544 | * a timeout event with a request that isn't active. | |
545 | */ | |
46f92d42 CH |
546 | if (!test_bit(REQ_ATOM_STARTED, &req->atomic_flags)) |
547 | return; | |
87ee7b11 | 548 | |
46f92d42 | 549 | if (ops->timeout) |
0152fb6b | 550 | ret = ops->timeout(req, reserved); |
46f92d42 CH |
551 | |
552 | switch (ret) { | |
553 | case BLK_EH_HANDLED: | |
554 | __blk_mq_complete_request(req); | |
555 | break; | |
556 | case BLK_EH_RESET_TIMER: | |
557 | blk_add_timer(req); | |
558 | blk_clear_rq_complete(req); | |
559 | break; | |
560 | case BLK_EH_NOT_HANDLED: | |
561 | break; | |
562 | default: | |
563 | printk(KERN_ERR "block: bad eh return: %d\n", ret); | |
564 | break; | |
565 | } | |
87ee7b11 | 566 | } |
81481eb4 | 567 | |
81481eb4 CH |
568 | static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx, |
569 | struct request *rq, void *priv, bool reserved) | |
570 | { | |
571 | struct blk_mq_timeout_data *data = priv; | |
87ee7b11 | 572 | |
46f92d42 CH |
573 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
574 | return; | |
575 | ||
576 | if (time_after_eq(jiffies, rq->deadline)) { | |
577 | if (!blk_mark_rq_complete(rq)) | |
0152fb6b | 578 | blk_mq_rq_timed_out(rq, reserved); |
46f92d42 CH |
579 | } else if (!data->next_set || time_after(data->next, rq->deadline)) { |
580 | data->next = rq->deadline; | |
581 | data->next_set = 1; | |
582 | } | |
81481eb4 CH |
583 | } |
584 | ||
585 | static void blk_mq_rq_timer(unsigned long priv) | |
320ae51f | 586 | { |
81481eb4 CH |
587 | struct request_queue *q = (struct request_queue *)priv; |
588 | struct blk_mq_timeout_data data = { | |
589 | .next = 0, | |
590 | .next_set = 0, | |
591 | }; | |
320ae51f | 592 | struct blk_mq_hw_ctx *hctx; |
81481eb4 | 593 | int i; |
320ae51f | 594 | |
484b4061 JA |
595 | queue_for_each_hw_ctx(q, hctx, i) { |
596 | /* | |
597 | * If not software queues are currently mapped to this | |
598 | * hardware queue, there's nothing to check | |
599 | */ | |
600 | if (!hctx->nr_ctx || !hctx->tags) | |
601 | continue; | |
602 | ||
81481eb4 | 603 | blk_mq_tag_busy_iter(hctx, blk_mq_check_expired, &data); |
484b4061 | 604 | } |
320ae51f | 605 | |
81481eb4 CH |
606 | if (data.next_set) { |
607 | data.next = blk_rq_timeout(round_jiffies_up(data.next)); | |
608 | mod_timer(&q->timeout, data.next); | |
0d2602ca JA |
609 | } else { |
610 | queue_for_each_hw_ctx(q, hctx, i) | |
611 | blk_mq_tag_idle(hctx); | |
612 | } | |
320ae51f JA |
613 | } |
614 | ||
615 | /* | |
616 | * Reverse check our software queue for entries that we could potentially | |
617 | * merge with. Currently includes a hand-wavy stop count of 8, to not spend | |
618 | * too much time checking for merges. | |
619 | */ | |
620 | static bool blk_mq_attempt_merge(struct request_queue *q, | |
621 | struct blk_mq_ctx *ctx, struct bio *bio) | |
622 | { | |
623 | struct request *rq; | |
624 | int checked = 8; | |
625 | ||
626 | list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) { | |
627 | int el_ret; | |
628 | ||
629 | if (!checked--) | |
630 | break; | |
631 | ||
632 | if (!blk_rq_merge_ok(rq, bio)) | |
633 | continue; | |
634 | ||
635 | el_ret = blk_try_merge(rq, bio); | |
636 | if (el_ret == ELEVATOR_BACK_MERGE) { | |
637 | if (bio_attempt_back_merge(q, rq, bio)) { | |
638 | ctx->rq_merged++; | |
639 | return true; | |
640 | } | |
641 | break; | |
642 | } else if (el_ret == ELEVATOR_FRONT_MERGE) { | |
643 | if (bio_attempt_front_merge(q, rq, bio)) { | |
644 | ctx->rq_merged++; | |
645 | return true; | |
646 | } | |
647 | break; | |
648 | } | |
649 | } | |
650 | ||
651 | return false; | |
652 | } | |
653 | ||
1429d7c9 JA |
654 | /* |
655 | * Process software queues that have been marked busy, splicing them | |
656 | * to the for-dispatch | |
657 | */ | |
658 | static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list) | |
659 | { | |
660 | struct blk_mq_ctx *ctx; | |
661 | int i; | |
662 | ||
663 | for (i = 0; i < hctx->ctx_map.map_size; i++) { | |
664 | struct blk_align_bitmap *bm = &hctx->ctx_map.map[i]; | |
665 | unsigned int off, bit; | |
666 | ||
667 | if (!bm->word) | |
668 | continue; | |
669 | ||
670 | bit = 0; | |
671 | off = i * hctx->ctx_map.bits_per_word; | |
672 | do { | |
673 | bit = find_next_bit(&bm->word, bm->depth, bit); | |
674 | if (bit >= bm->depth) | |
675 | break; | |
676 | ||
677 | ctx = hctx->ctxs[bit + off]; | |
678 | clear_bit(bit, &bm->word); | |
679 | spin_lock(&ctx->lock); | |
680 | list_splice_tail_init(&ctx->rq_list, list); | |
681 | spin_unlock(&ctx->lock); | |
682 | ||
683 | bit++; | |
684 | } while (1); | |
685 | } | |
686 | } | |
687 | ||
320ae51f JA |
688 | /* |
689 | * Run this hardware queue, pulling any software queues mapped to it in. | |
690 | * Note that this function currently has various problems around ordering | |
691 | * of IO. In particular, we'd like FIFO behaviour on handling existing | |
692 | * items on the hctx->dispatch list. Ignore that for now. | |
693 | */ | |
694 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) | |
695 | { | |
696 | struct request_queue *q = hctx->queue; | |
320ae51f JA |
697 | struct request *rq; |
698 | LIST_HEAD(rq_list); | |
1429d7c9 | 699 | int queued; |
320ae51f | 700 | |
fd1270d5 | 701 | WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)); |
e4043dcf | 702 | |
5d12f905 | 703 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
320ae51f JA |
704 | return; |
705 | ||
706 | hctx->run++; | |
707 | ||
708 | /* | |
709 | * Touch any software queue that has pending entries. | |
710 | */ | |
1429d7c9 | 711 | flush_busy_ctxs(hctx, &rq_list); |
320ae51f JA |
712 | |
713 | /* | |
714 | * If we have previous entries on our dispatch list, grab them | |
715 | * and stuff them at the front for more fair dispatch. | |
716 | */ | |
717 | if (!list_empty_careful(&hctx->dispatch)) { | |
718 | spin_lock(&hctx->lock); | |
719 | if (!list_empty(&hctx->dispatch)) | |
720 | list_splice_init(&hctx->dispatch, &rq_list); | |
721 | spin_unlock(&hctx->lock); | |
722 | } | |
723 | ||
320ae51f JA |
724 | /* |
725 | * Now process all the entries, sending them to the driver. | |
726 | */ | |
1429d7c9 | 727 | queued = 0; |
320ae51f JA |
728 | while (!list_empty(&rq_list)) { |
729 | int ret; | |
730 | ||
731 | rq = list_first_entry(&rq_list, struct request, queuelist); | |
732 | list_del_init(&rq->queuelist); | |
320ae51f | 733 | |
bf572297 | 734 | ret = q->mq_ops->queue_rq(hctx, rq, list_empty(&rq_list)); |
320ae51f JA |
735 | switch (ret) { |
736 | case BLK_MQ_RQ_QUEUE_OK: | |
737 | queued++; | |
738 | continue; | |
739 | case BLK_MQ_RQ_QUEUE_BUSY: | |
320ae51f | 740 | list_add(&rq->queuelist, &rq_list); |
ed0791b2 | 741 | __blk_mq_requeue_request(rq); |
320ae51f JA |
742 | break; |
743 | default: | |
744 | pr_err("blk-mq: bad return on queue: %d\n", ret); | |
320ae51f | 745 | case BLK_MQ_RQ_QUEUE_ERROR: |
1e93b8c2 | 746 | rq->errors = -EIO; |
c8a446ad | 747 | blk_mq_end_request(rq, rq->errors); |
320ae51f JA |
748 | break; |
749 | } | |
750 | ||
751 | if (ret == BLK_MQ_RQ_QUEUE_BUSY) | |
752 | break; | |
753 | } | |
754 | ||
755 | if (!queued) | |
756 | hctx->dispatched[0]++; | |
757 | else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1))) | |
758 | hctx->dispatched[ilog2(queued) + 1]++; | |
759 | ||
760 | /* | |
761 | * Any items that need requeuing? Stuff them into hctx->dispatch, | |
762 | * that is where we will continue on next queue run. | |
763 | */ | |
764 | if (!list_empty(&rq_list)) { | |
765 | spin_lock(&hctx->lock); | |
766 | list_splice(&rq_list, &hctx->dispatch); | |
767 | spin_unlock(&hctx->lock); | |
768 | } | |
769 | } | |
770 | ||
506e931f JA |
771 | /* |
772 | * It'd be great if the workqueue API had a way to pass | |
773 | * in a mask and had some smarts for more clever placement. | |
774 | * For now we just round-robin here, switching for every | |
775 | * BLK_MQ_CPU_WORK_BATCH queued items. | |
776 | */ | |
777 | static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) | |
778 | { | |
779 | int cpu = hctx->next_cpu; | |
780 | ||
781 | if (--hctx->next_cpu_batch <= 0) { | |
782 | int next_cpu; | |
783 | ||
784 | next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask); | |
785 | if (next_cpu >= nr_cpu_ids) | |
786 | next_cpu = cpumask_first(hctx->cpumask); | |
787 | ||
788 | hctx->next_cpu = next_cpu; | |
789 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; | |
790 | } | |
791 | ||
792 | return cpu; | |
793 | } | |
794 | ||
320ae51f JA |
795 | void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) |
796 | { | |
5d12f905 | 797 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
320ae51f JA |
798 | return; |
799 | ||
e4043dcf | 800 | if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask)) |
320ae51f | 801 | __blk_mq_run_hw_queue(hctx); |
e4043dcf | 802 | else if (hctx->queue->nr_hw_queues == 1) |
70f4db63 | 803 | kblockd_schedule_delayed_work(&hctx->run_work, 0); |
e4043dcf JA |
804 | else { |
805 | unsigned int cpu; | |
806 | ||
506e931f | 807 | cpu = blk_mq_hctx_next_cpu(hctx); |
70f4db63 | 808 | kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0); |
e4043dcf | 809 | } |
320ae51f JA |
810 | } |
811 | ||
812 | void blk_mq_run_queues(struct request_queue *q, bool async) | |
813 | { | |
814 | struct blk_mq_hw_ctx *hctx; | |
815 | int i; | |
816 | ||
817 | queue_for_each_hw_ctx(q, hctx, i) { | |
818 | if ((!blk_mq_hctx_has_pending(hctx) && | |
819 | list_empty_careful(&hctx->dispatch)) || | |
5d12f905 | 820 | test_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
320ae51f JA |
821 | continue; |
822 | ||
e4043dcf | 823 | preempt_disable(); |
320ae51f | 824 | blk_mq_run_hw_queue(hctx, async); |
e4043dcf | 825 | preempt_enable(); |
320ae51f JA |
826 | } |
827 | } | |
828 | EXPORT_SYMBOL(blk_mq_run_queues); | |
829 | ||
830 | void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx) | |
831 | { | |
70f4db63 CH |
832 | cancel_delayed_work(&hctx->run_work); |
833 | cancel_delayed_work(&hctx->delay_work); | |
320ae51f JA |
834 | set_bit(BLK_MQ_S_STOPPED, &hctx->state); |
835 | } | |
836 | EXPORT_SYMBOL(blk_mq_stop_hw_queue); | |
837 | ||
280d45f6 CH |
838 | void blk_mq_stop_hw_queues(struct request_queue *q) |
839 | { | |
840 | struct blk_mq_hw_ctx *hctx; | |
841 | int i; | |
842 | ||
843 | queue_for_each_hw_ctx(q, hctx, i) | |
844 | blk_mq_stop_hw_queue(hctx); | |
845 | } | |
846 | EXPORT_SYMBOL(blk_mq_stop_hw_queues); | |
847 | ||
320ae51f JA |
848 | void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx) |
849 | { | |
850 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
e4043dcf JA |
851 | |
852 | preempt_disable(); | |
0ffbce80 | 853 | blk_mq_run_hw_queue(hctx, false); |
e4043dcf | 854 | preempt_enable(); |
320ae51f JA |
855 | } |
856 | EXPORT_SYMBOL(blk_mq_start_hw_queue); | |
857 | ||
2f268556 CH |
858 | void blk_mq_start_hw_queues(struct request_queue *q) |
859 | { | |
860 | struct blk_mq_hw_ctx *hctx; | |
861 | int i; | |
862 | ||
863 | queue_for_each_hw_ctx(q, hctx, i) | |
864 | blk_mq_start_hw_queue(hctx); | |
865 | } | |
866 | EXPORT_SYMBOL(blk_mq_start_hw_queues); | |
867 | ||
868 | ||
1b4a3258 | 869 | void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async) |
320ae51f JA |
870 | { |
871 | struct blk_mq_hw_ctx *hctx; | |
872 | int i; | |
873 | ||
874 | queue_for_each_hw_ctx(q, hctx, i) { | |
875 | if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state)) | |
876 | continue; | |
877 | ||
878 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
e4043dcf | 879 | preempt_disable(); |
1b4a3258 | 880 | blk_mq_run_hw_queue(hctx, async); |
e4043dcf | 881 | preempt_enable(); |
320ae51f JA |
882 | } |
883 | } | |
884 | EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues); | |
885 | ||
70f4db63 | 886 | static void blk_mq_run_work_fn(struct work_struct *work) |
320ae51f JA |
887 | { |
888 | struct blk_mq_hw_ctx *hctx; | |
889 | ||
70f4db63 | 890 | hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work); |
e4043dcf | 891 | |
320ae51f JA |
892 | __blk_mq_run_hw_queue(hctx); |
893 | } | |
894 | ||
70f4db63 CH |
895 | static void blk_mq_delay_work_fn(struct work_struct *work) |
896 | { | |
897 | struct blk_mq_hw_ctx *hctx; | |
898 | ||
899 | hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work); | |
900 | ||
901 | if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state)) | |
902 | __blk_mq_run_hw_queue(hctx); | |
903 | } | |
904 | ||
905 | void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs) | |
906 | { | |
907 | unsigned long tmo = msecs_to_jiffies(msecs); | |
908 | ||
909 | if (hctx->queue->nr_hw_queues == 1) | |
910 | kblockd_schedule_delayed_work(&hctx->delay_work, tmo); | |
911 | else { | |
912 | unsigned int cpu; | |
913 | ||
506e931f | 914 | cpu = blk_mq_hctx_next_cpu(hctx); |
70f4db63 CH |
915 | kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo); |
916 | } | |
917 | } | |
918 | EXPORT_SYMBOL(blk_mq_delay_queue); | |
919 | ||
320ae51f | 920 | static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, |
72a0a36e | 921 | struct request *rq, bool at_head) |
320ae51f JA |
922 | { |
923 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
924 | ||
01b983c9 JA |
925 | trace_block_rq_insert(hctx->queue, rq); |
926 | ||
72a0a36e CH |
927 | if (at_head) |
928 | list_add(&rq->queuelist, &ctx->rq_list); | |
929 | else | |
930 | list_add_tail(&rq->queuelist, &ctx->rq_list); | |
4bb659b1 | 931 | |
320ae51f | 932 | blk_mq_hctx_mark_pending(hctx, ctx); |
320ae51f JA |
933 | } |
934 | ||
eeabc850 CH |
935 | void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue, |
936 | bool async) | |
320ae51f | 937 | { |
eeabc850 | 938 | struct request_queue *q = rq->q; |
320ae51f | 939 | struct blk_mq_hw_ctx *hctx; |
eeabc850 CH |
940 | struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx; |
941 | ||
942 | current_ctx = blk_mq_get_ctx(q); | |
943 | if (!cpu_online(ctx->cpu)) | |
944 | rq->mq_ctx = ctx = current_ctx; | |
320ae51f | 945 | |
320ae51f JA |
946 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
947 | ||
a57a178a CH |
948 | spin_lock(&ctx->lock); |
949 | __blk_mq_insert_request(hctx, rq, at_head); | |
950 | spin_unlock(&ctx->lock); | |
320ae51f | 951 | |
320ae51f JA |
952 | if (run_queue) |
953 | blk_mq_run_hw_queue(hctx, async); | |
e4043dcf JA |
954 | |
955 | blk_mq_put_ctx(current_ctx); | |
320ae51f JA |
956 | } |
957 | ||
958 | static void blk_mq_insert_requests(struct request_queue *q, | |
959 | struct blk_mq_ctx *ctx, | |
960 | struct list_head *list, | |
961 | int depth, | |
962 | bool from_schedule) | |
963 | ||
964 | { | |
965 | struct blk_mq_hw_ctx *hctx; | |
966 | struct blk_mq_ctx *current_ctx; | |
967 | ||
968 | trace_block_unplug(q, depth, !from_schedule); | |
969 | ||
970 | current_ctx = blk_mq_get_ctx(q); | |
971 | ||
972 | if (!cpu_online(ctx->cpu)) | |
973 | ctx = current_ctx; | |
974 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
975 | ||
976 | /* | |
977 | * preemption doesn't flush plug list, so it's possible ctx->cpu is | |
978 | * offline now | |
979 | */ | |
980 | spin_lock(&ctx->lock); | |
981 | while (!list_empty(list)) { | |
982 | struct request *rq; | |
983 | ||
984 | rq = list_first_entry(list, struct request, queuelist); | |
985 | list_del_init(&rq->queuelist); | |
986 | rq->mq_ctx = ctx; | |
72a0a36e | 987 | __blk_mq_insert_request(hctx, rq, false); |
320ae51f JA |
988 | } |
989 | spin_unlock(&ctx->lock); | |
990 | ||
320ae51f | 991 | blk_mq_run_hw_queue(hctx, from_schedule); |
e4043dcf | 992 | blk_mq_put_ctx(current_ctx); |
320ae51f JA |
993 | } |
994 | ||
995 | static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b) | |
996 | { | |
997 | struct request *rqa = container_of(a, struct request, queuelist); | |
998 | struct request *rqb = container_of(b, struct request, queuelist); | |
999 | ||
1000 | return !(rqa->mq_ctx < rqb->mq_ctx || | |
1001 | (rqa->mq_ctx == rqb->mq_ctx && | |
1002 | blk_rq_pos(rqa) < blk_rq_pos(rqb))); | |
1003 | } | |
1004 | ||
1005 | void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) | |
1006 | { | |
1007 | struct blk_mq_ctx *this_ctx; | |
1008 | struct request_queue *this_q; | |
1009 | struct request *rq; | |
1010 | LIST_HEAD(list); | |
1011 | LIST_HEAD(ctx_list); | |
1012 | unsigned int depth; | |
1013 | ||
1014 | list_splice_init(&plug->mq_list, &list); | |
1015 | ||
1016 | list_sort(NULL, &list, plug_ctx_cmp); | |
1017 | ||
1018 | this_q = NULL; | |
1019 | this_ctx = NULL; | |
1020 | depth = 0; | |
1021 | ||
1022 | while (!list_empty(&list)) { | |
1023 | rq = list_entry_rq(list.next); | |
1024 | list_del_init(&rq->queuelist); | |
1025 | BUG_ON(!rq->q); | |
1026 | if (rq->mq_ctx != this_ctx) { | |
1027 | if (this_ctx) { | |
1028 | blk_mq_insert_requests(this_q, this_ctx, | |
1029 | &ctx_list, depth, | |
1030 | from_schedule); | |
1031 | } | |
1032 | ||
1033 | this_ctx = rq->mq_ctx; | |
1034 | this_q = rq->q; | |
1035 | depth = 0; | |
1036 | } | |
1037 | ||
1038 | depth++; | |
1039 | list_add_tail(&rq->queuelist, &ctx_list); | |
1040 | } | |
1041 | ||
1042 | /* | |
1043 | * If 'this_ctx' is set, we know we have entries to complete | |
1044 | * on 'ctx_list'. Do those. | |
1045 | */ | |
1046 | if (this_ctx) { | |
1047 | blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth, | |
1048 | from_schedule); | |
1049 | } | |
1050 | } | |
1051 | ||
1052 | static void blk_mq_bio_to_request(struct request *rq, struct bio *bio) | |
1053 | { | |
1054 | init_request_from_bio(rq, bio); | |
4b570521 | 1055 | |
3ee32372 | 1056 | if (blk_do_io_stat(rq)) |
4b570521 | 1057 | blk_account_io_start(rq, 1); |
320ae51f JA |
1058 | } |
1059 | ||
274a5843 JA |
1060 | static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx) |
1061 | { | |
1062 | return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) && | |
1063 | !blk_queue_nomerges(hctx->queue); | |
1064 | } | |
1065 | ||
07068d5b JA |
1066 | static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx, |
1067 | struct blk_mq_ctx *ctx, | |
1068 | struct request *rq, struct bio *bio) | |
320ae51f | 1069 | { |
274a5843 | 1070 | if (!hctx_allow_merges(hctx)) { |
07068d5b JA |
1071 | blk_mq_bio_to_request(rq, bio); |
1072 | spin_lock(&ctx->lock); | |
1073 | insert_rq: | |
1074 | __blk_mq_insert_request(hctx, rq, false); | |
1075 | spin_unlock(&ctx->lock); | |
1076 | return false; | |
1077 | } else { | |
274a5843 JA |
1078 | struct request_queue *q = hctx->queue; |
1079 | ||
07068d5b JA |
1080 | spin_lock(&ctx->lock); |
1081 | if (!blk_mq_attempt_merge(q, ctx, bio)) { | |
1082 | blk_mq_bio_to_request(rq, bio); | |
1083 | goto insert_rq; | |
1084 | } | |
320ae51f | 1085 | |
07068d5b JA |
1086 | spin_unlock(&ctx->lock); |
1087 | __blk_mq_free_request(hctx, ctx, rq); | |
1088 | return true; | |
14ec77f3 | 1089 | } |
07068d5b | 1090 | } |
14ec77f3 | 1091 | |
07068d5b JA |
1092 | struct blk_map_ctx { |
1093 | struct blk_mq_hw_ctx *hctx; | |
1094 | struct blk_mq_ctx *ctx; | |
1095 | }; | |
1096 | ||
1097 | static struct request *blk_mq_map_request(struct request_queue *q, | |
1098 | struct bio *bio, | |
1099 | struct blk_map_ctx *data) | |
1100 | { | |
1101 | struct blk_mq_hw_ctx *hctx; | |
1102 | struct blk_mq_ctx *ctx; | |
1103 | struct request *rq; | |
1104 | int rw = bio_data_dir(bio); | |
cb96a42c | 1105 | struct blk_mq_alloc_data alloc_data; |
320ae51f | 1106 | |
07068d5b | 1107 | if (unlikely(blk_mq_queue_enter(q))) { |
320ae51f | 1108 | bio_endio(bio, -EIO); |
07068d5b | 1109 | return NULL; |
320ae51f JA |
1110 | } |
1111 | ||
1112 | ctx = blk_mq_get_ctx(q); | |
1113 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
1114 | ||
07068d5b | 1115 | if (rw_is_sync(bio->bi_rw)) |
27fbf4e8 | 1116 | rw |= REQ_SYNC; |
07068d5b | 1117 | |
320ae51f | 1118 | trace_block_getrq(q, bio, rw); |
cb96a42c ML |
1119 | blk_mq_set_alloc_data(&alloc_data, q, GFP_ATOMIC, false, ctx, |
1120 | hctx); | |
1121 | rq = __blk_mq_alloc_request(&alloc_data, rw); | |
5dee8577 | 1122 | if (unlikely(!rq)) { |
793597a6 | 1123 | __blk_mq_run_hw_queue(hctx); |
320ae51f JA |
1124 | blk_mq_put_ctx(ctx); |
1125 | trace_block_sleeprq(q, bio, rw); | |
793597a6 CH |
1126 | |
1127 | ctx = blk_mq_get_ctx(q); | |
320ae51f | 1128 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
cb96a42c ML |
1129 | blk_mq_set_alloc_data(&alloc_data, q, |
1130 | __GFP_WAIT|GFP_ATOMIC, false, ctx, hctx); | |
1131 | rq = __blk_mq_alloc_request(&alloc_data, rw); | |
1132 | ctx = alloc_data.ctx; | |
1133 | hctx = alloc_data.hctx; | |
320ae51f JA |
1134 | } |
1135 | ||
1136 | hctx->queued++; | |
07068d5b JA |
1137 | data->hctx = hctx; |
1138 | data->ctx = ctx; | |
1139 | return rq; | |
1140 | } | |
1141 | ||
1142 | /* | |
1143 | * Multiple hardware queue variant. This will not use per-process plugs, | |
1144 | * but will attempt to bypass the hctx queueing if we can go straight to | |
1145 | * hardware for SYNC IO. | |
1146 | */ | |
1147 | static void blk_mq_make_request(struct request_queue *q, struct bio *bio) | |
1148 | { | |
1149 | const int is_sync = rw_is_sync(bio->bi_rw); | |
1150 | const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA); | |
1151 | struct blk_map_ctx data; | |
1152 | struct request *rq; | |
1153 | ||
1154 | blk_queue_bounce(q, &bio); | |
1155 | ||
1156 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { | |
1157 | bio_endio(bio, -EIO); | |
1158 | return; | |
1159 | } | |
1160 | ||
1161 | rq = blk_mq_map_request(q, bio, &data); | |
1162 | if (unlikely(!rq)) | |
1163 | return; | |
1164 | ||
1165 | if (unlikely(is_flush_fua)) { | |
1166 | blk_mq_bio_to_request(rq, bio); | |
1167 | blk_insert_flush(rq); | |
1168 | goto run_queue; | |
1169 | } | |
1170 | ||
1171 | if (is_sync) { | |
1172 | int ret; | |
1173 | ||
1174 | blk_mq_bio_to_request(rq, bio); | |
07068d5b JA |
1175 | |
1176 | /* | |
1177 | * For OK queue, we are done. For error, kill it. Any other | |
1178 | * error (busy), just add it to our list as we previously | |
1179 | * would have done | |
1180 | */ | |
bf572297 | 1181 | ret = q->mq_ops->queue_rq(data.hctx, rq, true); |
07068d5b JA |
1182 | if (ret == BLK_MQ_RQ_QUEUE_OK) |
1183 | goto done; | |
1184 | else { | |
1185 | __blk_mq_requeue_request(rq); | |
1186 | ||
1187 | if (ret == BLK_MQ_RQ_QUEUE_ERROR) { | |
1188 | rq->errors = -EIO; | |
c8a446ad | 1189 | blk_mq_end_request(rq, rq->errors); |
07068d5b JA |
1190 | goto done; |
1191 | } | |
1192 | } | |
1193 | } | |
1194 | ||
1195 | if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) { | |
1196 | /* | |
1197 | * For a SYNC request, send it to the hardware immediately. For | |
1198 | * an ASYNC request, just ensure that we run it later on. The | |
1199 | * latter allows for merging opportunities and more efficient | |
1200 | * dispatching. | |
1201 | */ | |
1202 | run_queue: | |
1203 | blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua); | |
1204 | } | |
1205 | done: | |
1206 | blk_mq_put_ctx(data.ctx); | |
1207 | } | |
1208 | ||
1209 | /* | |
1210 | * Single hardware queue variant. This will attempt to use any per-process | |
1211 | * plug for merging and IO deferral. | |
1212 | */ | |
1213 | static void blk_sq_make_request(struct request_queue *q, struct bio *bio) | |
1214 | { | |
1215 | const int is_sync = rw_is_sync(bio->bi_rw); | |
1216 | const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA); | |
1217 | unsigned int use_plug, request_count = 0; | |
1218 | struct blk_map_ctx data; | |
1219 | struct request *rq; | |
1220 | ||
1221 | /* | |
1222 | * If we have multiple hardware queues, just go directly to | |
1223 | * one of those for sync IO. | |
1224 | */ | |
1225 | use_plug = !is_flush_fua && !is_sync; | |
1226 | ||
1227 | blk_queue_bounce(q, &bio); | |
1228 | ||
1229 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { | |
1230 | bio_endio(bio, -EIO); | |
1231 | return; | |
1232 | } | |
1233 | ||
1234 | if (use_plug && !blk_queue_nomerges(q) && | |
1235 | blk_attempt_plug_merge(q, bio, &request_count)) | |
1236 | return; | |
1237 | ||
1238 | rq = blk_mq_map_request(q, bio, &data); | |
ff87bcec JA |
1239 | if (unlikely(!rq)) |
1240 | return; | |
320ae51f JA |
1241 | |
1242 | if (unlikely(is_flush_fua)) { | |
1243 | blk_mq_bio_to_request(rq, bio); | |
320ae51f JA |
1244 | blk_insert_flush(rq); |
1245 | goto run_queue; | |
1246 | } | |
1247 | ||
1248 | /* | |
1249 | * A task plug currently exists. Since this is completely lockless, | |
1250 | * utilize that to temporarily store requests until the task is | |
1251 | * either done or scheduled away. | |
1252 | */ | |
1253 | if (use_plug) { | |
1254 | struct blk_plug *plug = current->plug; | |
1255 | ||
1256 | if (plug) { | |
1257 | blk_mq_bio_to_request(rq, bio); | |
92f399c7 | 1258 | if (list_empty(&plug->mq_list)) |
320ae51f JA |
1259 | trace_block_plug(q); |
1260 | else if (request_count >= BLK_MAX_REQUEST_COUNT) { | |
1261 | blk_flush_plug_list(plug, false); | |
1262 | trace_block_plug(q); | |
1263 | } | |
1264 | list_add_tail(&rq->queuelist, &plug->mq_list); | |
07068d5b | 1265 | blk_mq_put_ctx(data.ctx); |
320ae51f JA |
1266 | return; |
1267 | } | |
1268 | } | |
1269 | ||
07068d5b JA |
1270 | if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) { |
1271 | /* | |
1272 | * For a SYNC request, send it to the hardware immediately. For | |
1273 | * an ASYNC request, just ensure that we run it later on. The | |
1274 | * latter allows for merging opportunities and more efficient | |
1275 | * dispatching. | |
1276 | */ | |
1277 | run_queue: | |
1278 | blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua); | |
320ae51f JA |
1279 | } |
1280 | ||
07068d5b | 1281 | blk_mq_put_ctx(data.ctx); |
320ae51f JA |
1282 | } |
1283 | ||
1284 | /* | |
1285 | * Default mapping to a software queue, since we use one per CPU. | |
1286 | */ | |
1287 | struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu) | |
1288 | { | |
1289 | return q->queue_hw_ctx[q->mq_map[cpu]]; | |
1290 | } | |
1291 | EXPORT_SYMBOL(blk_mq_map_queue); | |
1292 | ||
24d2f903 CH |
1293 | static void blk_mq_free_rq_map(struct blk_mq_tag_set *set, |
1294 | struct blk_mq_tags *tags, unsigned int hctx_idx) | |
95363efd | 1295 | { |
e9b267d9 | 1296 | struct page *page; |
320ae51f | 1297 | |
24d2f903 | 1298 | if (tags->rqs && set->ops->exit_request) { |
e9b267d9 | 1299 | int i; |
320ae51f | 1300 | |
24d2f903 CH |
1301 | for (i = 0; i < tags->nr_tags; i++) { |
1302 | if (!tags->rqs[i]) | |
e9b267d9 | 1303 | continue; |
24d2f903 CH |
1304 | set->ops->exit_request(set->driver_data, tags->rqs[i], |
1305 | hctx_idx, i); | |
a5164405 | 1306 | tags->rqs[i] = NULL; |
e9b267d9 | 1307 | } |
320ae51f | 1308 | } |
320ae51f | 1309 | |
24d2f903 CH |
1310 | while (!list_empty(&tags->page_list)) { |
1311 | page = list_first_entry(&tags->page_list, struct page, lru); | |
6753471c | 1312 | list_del_init(&page->lru); |
320ae51f JA |
1313 | __free_pages(page, page->private); |
1314 | } | |
1315 | ||
24d2f903 | 1316 | kfree(tags->rqs); |
320ae51f | 1317 | |
24d2f903 | 1318 | blk_mq_free_tags(tags); |
320ae51f JA |
1319 | } |
1320 | ||
1321 | static size_t order_to_size(unsigned int order) | |
1322 | { | |
4ca08500 | 1323 | return (size_t)PAGE_SIZE << order; |
320ae51f JA |
1324 | } |
1325 | ||
24d2f903 CH |
1326 | static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set, |
1327 | unsigned int hctx_idx) | |
320ae51f | 1328 | { |
24d2f903 | 1329 | struct blk_mq_tags *tags; |
320ae51f JA |
1330 | unsigned int i, j, entries_per_page, max_order = 4; |
1331 | size_t rq_size, left; | |
1332 | ||
24d2f903 CH |
1333 | tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags, |
1334 | set->numa_node); | |
1335 | if (!tags) | |
1336 | return NULL; | |
320ae51f | 1337 | |
24d2f903 CH |
1338 | INIT_LIST_HEAD(&tags->page_list); |
1339 | ||
a5164405 JA |
1340 | tags->rqs = kzalloc_node(set->queue_depth * sizeof(struct request *), |
1341 | GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY, | |
1342 | set->numa_node); | |
24d2f903 CH |
1343 | if (!tags->rqs) { |
1344 | blk_mq_free_tags(tags); | |
1345 | return NULL; | |
1346 | } | |
320ae51f JA |
1347 | |
1348 | /* | |
1349 | * rq_size is the size of the request plus driver payload, rounded | |
1350 | * to the cacheline size | |
1351 | */ | |
24d2f903 | 1352 | rq_size = round_up(sizeof(struct request) + set->cmd_size, |
320ae51f | 1353 | cache_line_size()); |
24d2f903 | 1354 | left = rq_size * set->queue_depth; |
320ae51f | 1355 | |
24d2f903 | 1356 | for (i = 0; i < set->queue_depth; ) { |
320ae51f JA |
1357 | int this_order = max_order; |
1358 | struct page *page; | |
1359 | int to_do; | |
1360 | void *p; | |
1361 | ||
1362 | while (left < order_to_size(this_order - 1) && this_order) | |
1363 | this_order--; | |
1364 | ||
1365 | do { | |
a5164405 JA |
1366 | page = alloc_pages_node(set->numa_node, |
1367 | GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY, | |
1368 | this_order); | |
320ae51f JA |
1369 | if (page) |
1370 | break; | |
1371 | if (!this_order--) | |
1372 | break; | |
1373 | if (order_to_size(this_order) < rq_size) | |
1374 | break; | |
1375 | } while (1); | |
1376 | ||
1377 | if (!page) | |
24d2f903 | 1378 | goto fail; |
320ae51f JA |
1379 | |
1380 | page->private = this_order; | |
24d2f903 | 1381 | list_add_tail(&page->lru, &tags->page_list); |
320ae51f JA |
1382 | |
1383 | p = page_address(page); | |
1384 | entries_per_page = order_to_size(this_order) / rq_size; | |
24d2f903 | 1385 | to_do = min(entries_per_page, set->queue_depth - i); |
320ae51f JA |
1386 | left -= to_do * rq_size; |
1387 | for (j = 0; j < to_do; j++) { | |
24d2f903 | 1388 | tags->rqs[i] = p; |
683d0e12 DH |
1389 | tags->rqs[i]->atomic_flags = 0; |
1390 | tags->rqs[i]->cmd_flags = 0; | |
24d2f903 CH |
1391 | if (set->ops->init_request) { |
1392 | if (set->ops->init_request(set->driver_data, | |
1393 | tags->rqs[i], hctx_idx, i, | |
a5164405 JA |
1394 | set->numa_node)) { |
1395 | tags->rqs[i] = NULL; | |
24d2f903 | 1396 | goto fail; |
a5164405 | 1397 | } |
e9b267d9 CH |
1398 | } |
1399 | ||
320ae51f JA |
1400 | p += rq_size; |
1401 | i++; | |
1402 | } | |
1403 | } | |
1404 | ||
24d2f903 | 1405 | return tags; |
320ae51f | 1406 | |
24d2f903 | 1407 | fail: |
24d2f903 CH |
1408 | blk_mq_free_rq_map(set, tags, hctx_idx); |
1409 | return NULL; | |
320ae51f JA |
1410 | } |
1411 | ||
1429d7c9 JA |
1412 | static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap) |
1413 | { | |
1414 | kfree(bitmap->map); | |
1415 | } | |
1416 | ||
1417 | static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node) | |
1418 | { | |
1419 | unsigned int bpw = 8, total, num_maps, i; | |
1420 | ||
1421 | bitmap->bits_per_word = bpw; | |
1422 | ||
1423 | num_maps = ALIGN(nr_cpu_ids, bpw) / bpw; | |
1424 | bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap), | |
1425 | GFP_KERNEL, node); | |
1426 | if (!bitmap->map) | |
1427 | return -ENOMEM; | |
1428 | ||
1429 | bitmap->map_size = num_maps; | |
1430 | ||
1431 | total = nr_cpu_ids; | |
1432 | for (i = 0; i < num_maps; i++) { | |
1433 | bitmap->map[i].depth = min(total, bitmap->bits_per_word); | |
1434 | total -= bitmap->map[i].depth; | |
1435 | } | |
1436 | ||
1437 | return 0; | |
1438 | } | |
1439 | ||
484b4061 JA |
1440 | static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu) |
1441 | { | |
1442 | struct request_queue *q = hctx->queue; | |
1443 | struct blk_mq_ctx *ctx; | |
1444 | LIST_HEAD(tmp); | |
1445 | ||
1446 | /* | |
1447 | * Move ctx entries to new CPU, if this one is going away. | |
1448 | */ | |
1449 | ctx = __blk_mq_get_ctx(q, cpu); | |
1450 | ||
1451 | spin_lock(&ctx->lock); | |
1452 | if (!list_empty(&ctx->rq_list)) { | |
1453 | list_splice_init(&ctx->rq_list, &tmp); | |
1454 | blk_mq_hctx_clear_pending(hctx, ctx); | |
1455 | } | |
1456 | spin_unlock(&ctx->lock); | |
1457 | ||
1458 | if (list_empty(&tmp)) | |
1459 | return NOTIFY_OK; | |
1460 | ||
1461 | ctx = blk_mq_get_ctx(q); | |
1462 | spin_lock(&ctx->lock); | |
1463 | ||
1464 | while (!list_empty(&tmp)) { | |
1465 | struct request *rq; | |
1466 | ||
1467 | rq = list_first_entry(&tmp, struct request, queuelist); | |
1468 | rq->mq_ctx = ctx; | |
1469 | list_move_tail(&rq->queuelist, &ctx->rq_list); | |
1470 | } | |
1471 | ||
1472 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
1473 | blk_mq_hctx_mark_pending(hctx, ctx); | |
1474 | ||
1475 | spin_unlock(&ctx->lock); | |
1476 | ||
1477 | blk_mq_run_hw_queue(hctx, true); | |
1478 | blk_mq_put_ctx(ctx); | |
1479 | return NOTIFY_OK; | |
1480 | } | |
1481 | ||
1482 | static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu) | |
1483 | { | |
1484 | struct request_queue *q = hctx->queue; | |
1485 | struct blk_mq_tag_set *set = q->tag_set; | |
1486 | ||
1487 | if (set->tags[hctx->queue_num]) | |
1488 | return NOTIFY_OK; | |
1489 | ||
1490 | set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num); | |
1491 | if (!set->tags[hctx->queue_num]) | |
1492 | return NOTIFY_STOP; | |
1493 | ||
1494 | hctx->tags = set->tags[hctx->queue_num]; | |
1495 | return NOTIFY_OK; | |
1496 | } | |
1497 | ||
1498 | static int blk_mq_hctx_notify(void *data, unsigned long action, | |
1499 | unsigned int cpu) | |
1500 | { | |
1501 | struct blk_mq_hw_ctx *hctx = data; | |
1502 | ||
1503 | if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) | |
1504 | return blk_mq_hctx_cpu_offline(hctx, cpu); | |
1505 | else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) | |
1506 | return blk_mq_hctx_cpu_online(hctx, cpu); | |
1507 | ||
1508 | return NOTIFY_OK; | |
1509 | } | |
1510 | ||
624dbe47 ML |
1511 | static void blk_mq_exit_hw_queues(struct request_queue *q, |
1512 | struct blk_mq_tag_set *set, int nr_queue) | |
1513 | { | |
1514 | struct blk_mq_hw_ctx *hctx; | |
1515 | unsigned int i; | |
1516 | ||
1517 | queue_for_each_hw_ctx(q, hctx, i) { | |
1518 | if (i == nr_queue) | |
1519 | break; | |
1520 | ||
f899fed4 JA |
1521 | blk_mq_tag_idle(hctx); |
1522 | ||
624dbe47 ML |
1523 | if (set->ops->exit_hctx) |
1524 | set->ops->exit_hctx(hctx, i); | |
1525 | ||
1526 | blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier); | |
1527 | kfree(hctx->ctxs); | |
1528 | blk_mq_free_bitmap(&hctx->ctx_map); | |
1529 | } | |
1530 | ||
1531 | } | |
1532 | ||
1533 | static void blk_mq_free_hw_queues(struct request_queue *q, | |
1534 | struct blk_mq_tag_set *set) | |
1535 | { | |
1536 | struct blk_mq_hw_ctx *hctx; | |
1537 | unsigned int i; | |
1538 | ||
1539 | queue_for_each_hw_ctx(q, hctx, i) { | |
1540 | free_cpumask_var(hctx->cpumask); | |
cdef54dd | 1541 | kfree(hctx); |
624dbe47 ML |
1542 | } |
1543 | } | |
1544 | ||
320ae51f | 1545 | static int blk_mq_init_hw_queues(struct request_queue *q, |
24d2f903 | 1546 | struct blk_mq_tag_set *set) |
320ae51f JA |
1547 | { |
1548 | struct blk_mq_hw_ctx *hctx; | |
624dbe47 | 1549 | unsigned int i; |
320ae51f JA |
1550 | |
1551 | /* | |
1552 | * Initialize hardware queues | |
1553 | */ | |
1554 | queue_for_each_hw_ctx(q, hctx, i) { | |
320ae51f JA |
1555 | int node; |
1556 | ||
1557 | node = hctx->numa_node; | |
1558 | if (node == NUMA_NO_NODE) | |
24d2f903 | 1559 | node = hctx->numa_node = set->numa_node; |
320ae51f | 1560 | |
70f4db63 CH |
1561 | INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn); |
1562 | INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn); | |
320ae51f JA |
1563 | spin_lock_init(&hctx->lock); |
1564 | INIT_LIST_HEAD(&hctx->dispatch); | |
1565 | hctx->queue = q; | |
1566 | hctx->queue_num = i; | |
24d2f903 CH |
1567 | hctx->flags = set->flags; |
1568 | hctx->cmd_size = set->cmd_size; | |
320ae51f JA |
1569 | |
1570 | blk_mq_init_cpu_notifier(&hctx->cpu_notifier, | |
1571 | blk_mq_hctx_notify, hctx); | |
1572 | blk_mq_register_cpu_notifier(&hctx->cpu_notifier); | |
1573 | ||
24d2f903 | 1574 | hctx->tags = set->tags[i]; |
320ae51f JA |
1575 | |
1576 | /* | |
a68aafa5 | 1577 | * Allocate space for all possible cpus to avoid allocation at |
320ae51f JA |
1578 | * runtime |
1579 | */ | |
1580 | hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *), | |
1581 | GFP_KERNEL, node); | |
1582 | if (!hctx->ctxs) | |
1583 | break; | |
1584 | ||
1429d7c9 | 1585 | if (blk_mq_alloc_bitmap(&hctx->ctx_map, node)) |
320ae51f JA |
1586 | break; |
1587 | ||
320ae51f JA |
1588 | hctx->nr_ctx = 0; |
1589 | ||
24d2f903 CH |
1590 | if (set->ops->init_hctx && |
1591 | set->ops->init_hctx(hctx, set->driver_data, i)) | |
320ae51f JA |
1592 | break; |
1593 | } | |
1594 | ||
1595 | if (i == q->nr_hw_queues) | |
1596 | return 0; | |
1597 | ||
1598 | /* | |
1599 | * Init failed | |
1600 | */ | |
624dbe47 | 1601 | blk_mq_exit_hw_queues(q, set, i); |
320ae51f JA |
1602 | |
1603 | return 1; | |
1604 | } | |
1605 | ||
1606 | static void blk_mq_init_cpu_queues(struct request_queue *q, | |
1607 | unsigned int nr_hw_queues) | |
1608 | { | |
1609 | unsigned int i; | |
1610 | ||
1611 | for_each_possible_cpu(i) { | |
1612 | struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i); | |
1613 | struct blk_mq_hw_ctx *hctx; | |
1614 | ||
1615 | memset(__ctx, 0, sizeof(*__ctx)); | |
1616 | __ctx->cpu = i; | |
1617 | spin_lock_init(&__ctx->lock); | |
1618 | INIT_LIST_HEAD(&__ctx->rq_list); | |
1619 | __ctx->queue = q; | |
1620 | ||
1621 | /* If the cpu isn't online, the cpu is mapped to first hctx */ | |
320ae51f JA |
1622 | if (!cpu_online(i)) |
1623 | continue; | |
1624 | ||
e4043dcf JA |
1625 | hctx = q->mq_ops->map_queue(q, i); |
1626 | cpumask_set_cpu(i, hctx->cpumask); | |
1627 | hctx->nr_ctx++; | |
1628 | ||
320ae51f JA |
1629 | /* |
1630 | * Set local node, IFF we have more than one hw queue. If | |
1631 | * not, we remain on the home node of the device | |
1632 | */ | |
1633 | if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE) | |
1634 | hctx->numa_node = cpu_to_node(i); | |
1635 | } | |
1636 | } | |
1637 | ||
1638 | static void blk_mq_map_swqueue(struct request_queue *q) | |
1639 | { | |
1640 | unsigned int i; | |
1641 | struct blk_mq_hw_ctx *hctx; | |
1642 | struct blk_mq_ctx *ctx; | |
1643 | ||
1644 | queue_for_each_hw_ctx(q, hctx, i) { | |
e4043dcf | 1645 | cpumask_clear(hctx->cpumask); |
320ae51f JA |
1646 | hctx->nr_ctx = 0; |
1647 | } | |
1648 | ||
1649 | /* | |
1650 | * Map software to hardware queues | |
1651 | */ | |
1652 | queue_for_each_ctx(q, ctx, i) { | |
1653 | /* If the cpu isn't online, the cpu is mapped to first hctx */ | |
e4043dcf JA |
1654 | if (!cpu_online(i)) |
1655 | continue; | |
1656 | ||
320ae51f | 1657 | hctx = q->mq_ops->map_queue(q, i); |
e4043dcf | 1658 | cpumask_set_cpu(i, hctx->cpumask); |
320ae51f JA |
1659 | ctx->index_hw = hctx->nr_ctx; |
1660 | hctx->ctxs[hctx->nr_ctx++] = ctx; | |
1661 | } | |
506e931f JA |
1662 | |
1663 | queue_for_each_hw_ctx(q, hctx, i) { | |
484b4061 | 1664 | /* |
a68aafa5 JA |
1665 | * If no software queues are mapped to this hardware queue, |
1666 | * disable it and free the request entries. | |
484b4061 JA |
1667 | */ |
1668 | if (!hctx->nr_ctx) { | |
1669 | struct blk_mq_tag_set *set = q->tag_set; | |
1670 | ||
1671 | if (set->tags[i]) { | |
1672 | blk_mq_free_rq_map(set, set->tags[i], i); | |
1673 | set->tags[i] = NULL; | |
1674 | hctx->tags = NULL; | |
1675 | } | |
1676 | continue; | |
1677 | } | |
1678 | ||
1679 | /* | |
1680 | * Initialize batch roundrobin counts | |
1681 | */ | |
506e931f JA |
1682 | hctx->next_cpu = cpumask_first(hctx->cpumask); |
1683 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; | |
1684 | } | |
320ae51f JA |
1685 | } |
1686 | ||
0d2602ca JA |
1687 | static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set) |
1688 | { | |
1689 | struct blk_mq_hw_ctx *hctx; | |
1690 | struct request_queue *q; | |
1691 | bool shared; | |
1692 | int i; | |
1693 | ||
1694 | if (set->tag_list.next == set->tag_list.prev) | |
1695 | shared = false; | |
1696 | else | |
1697 | shared = true; | |
1698 | ||
1699 | list_for_each_entry(q, &set->tag_list, tag_set_list) { | |
1700 | blk_mq_freeze_queue(q); | |
1701 | ||
1702 | queue_for_each_hw_ctx(q, hctx, i) { | |
1703 | if (shared) | |
1704 | hctx->flags |= BLK_MQ_F_TAG_SHARED; | |
1705 | else | |
1706 | hctx->flags &= ~BLK_MQ_F_TAG_SHARED; | |
1707 | } | |
1708 | blk_mq_unfreeze_queue(q); | |
1709 | } | |
1710 | } | |
1711 | ||
1712 | static void blk_mq_del_queue_tag_set(struct request_queue *q) | |
1713 | { | |
1714 | struct blk_mq_tag_set *set = q->tag_set; | |
1715 | ||
0d2602ca JA |
1716 | mutex_lock(&set->tag_list_lock); |
1717 | list_del_init(&q->tag_set_list); | |
1718 | blk_mq_update_tag_set_depth(set); | |
1719 | mutex_unlock(&set->tag_list_lock); | |
0d2602ca JA |
1720 | } |
1721 | ||
1722 | static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set, | |
1723 | struct request_queue *q) | |
1724 | { | |
1725 | q->tag_set = set; | |
1726 | ||
1727 | mutex_lock(&set->tag_list_lock); | |
1728 | list_add_tail(&q->tag_set_list, &set->tag_list); | |
1729 | blk_mq_update_tag_set_depth(set); | |
1730 | mutex_unlock(&set->tag_list_lock); | |
1731 | } | |
1732 | ||
24d2f903 | 1733 | struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set) |
320ae51f JA |
1734 | { |
1735 | struct blk_mq_hw_ctx **hctxs; | |
e6cdb092 | 1736 | struct blk_mq_ctx __percpu *ctx; |
320ae51f | 1737 | struct request_queue *q; |
f14bbe77 | 1738 | unsigned int *map; |
320ae51f JA |
1739 | int i; |
1740 | ||
320ae51f JA |
1741 | ctx = alloc_percpu(struct blk_mq_ctx); |
1742 | if (!ctx) | |
1743 | return ERR_PTR(-ENOMEM); | |
1744 | ||
24d2f903 CH |
1745 | hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL, |
1746 | set->numa_node); | |
320ae51f JA |
1747 | |
1748 | if (!hctxs) | |
1749 | goto err_percpu; | |
1750 | ||
f14bbe77 JA |
1751 | map = blk_mq_make_queue_map(set); |
1752 | if (!map) | |
1753 | goto err_map; | |
1754 | ||
24d2f903 | 1755 | for (i = 0; i < set->nr_hw_queues; i++) { |
f14bbe77 JA |
1756 | int node = blk_mq_hw_queue_to_node(map, i); |
1757 | ||
cdef54dd CH |
1758 | hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx), |
1759 | GFP_KERNEL, node); | |
320ae51f JA |
1760 | if (!hctxs[i]) |
1761 | goto err_hctxs; | |
1762 | ||
e4043dcf JA |
1763 | if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL)) |
1764 | goto err_hctxs; | |
1765 | ||
0d2602ca | 1766 | atomic_set(&hctxs[i]->nr_active, 0); |
f14bbe77 | 1767 | hctxs[i]->numa_node = node; |
320ae51f JA |
1768 | hctxs[i]->queue_num = i; |
1769 | } | |
1770 | ||
24d2f903 | 1771 | q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node); |
320ae51f JA |
1772 | if (!q) |
1773 | goto err_hctxs; | |
1774 | ||
add703fd | 1775 | if (percpu_ref_init(&q->mq_usage_counter, blk_mq_usage_counter_release)) |
3d2936f4 ML |
1776 | goto err_map; |
1777 | ||
320ae51f JA |
1778 | setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q); |
1779 | blk_queue_rq_timeout(q, 30000); | |
1780 | ||
1781 | q->nr_queues = nr_cpu_ids; | |
24d2f903 | 1782 | q->nr_hw_queues = set->nr_hw_queues; |
f14bbe77 | 1783 | q->mq_map = map; |
320ae51f JA |
1784 | |
1785 | q->queue_ctx = ctx; | |
1786 | q->queue_hw_ctx = hctxs; | |
1787 | ||
24d2f903 | 1788 | q->mq_ops = set->ops; |
94eddfbe | 1789 | q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT; |
320ae51f | 1790 | |
05f1dd53 JA |
1791 | if (!(set->flags & BLK_MQ_F_SG_MERGE)) |
1792 | q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE; | |
1793 | ||
1be036e9 CH |
1794 | q->sg_reserved_size = INT_MAX; |
1795 | ||
6fca6a61 CH |
1796 | INIT_WORK(&q->requeue_work, blk_mq_requeue_work); |
1797 | INIT_LIST_HEAD(&q->requeue_list); | |
1798 | spin_lock_init(&q->requeue_lock); | |
1799 | ||
07068d5b JA |
1800 | if (q->nr_hw_queues > 1) |
1801 | blk_queue_make_request(q, blk_mq_make_request); | |
1802 | else | |
1803 | blk_queue_make_request(q, blk_sq_make_request); | |
1804 | ||
24d2f903 CH |
1805 | if (set->timeout) |
1806 | blk_queue_rq_timeout(q, set->timeout); | |
320ae51f | 1807 | |
eba71768 JA |
1808 | /* |
1809 | * Do this after blk_queue_make_request() overrides it... | |
1810 | */ | |
1811 | q->nr_requests = set->queue_depth; | |
1812 | ||
24d2f903 CH |
1813 | if (set->ops->complete) |
1814 | blk_queue_softirq_done(q, set->ops->complete); | |
30a91cb4 | 1815 | |
320ae51f | 1816 | blk_mq_init_flush(q); |
24d2f903 | 1817 | blk_mq_init_cpu_queues(q, set->nr_hw_queues); |
320ae51f | 1818 | |
24d2f903 CH |
1819 | q->flush_rq = kzalloc(round_up(sizeof(struct request) + |
1820 | set->cmd_size, cache_line_size()), | |
1821 | GFP_KERNEL); | |
18741986 | 1822 | if (!q->flush_rq) |
320ae51f JA |
1823 | goto err_hw; |
1824 | ||
24d2f903 | 1825 | if (blk_mq_init_hw_queues(q, set)) |
18741986 CH |
1826 | goto err_flush_rq; |
1827 | ||
320ae51f JA |
1828 | mutex_lock(&all_q_mutex); |
1829 | list_add_tail(&q->all_q_node, &all_q_list); | |
1830 | mutex_unlock(&all_q_mutex); | |
1831 | ||
0d2602ca JA |
1832 | blk_mq_add_queue_tag_set(set, q); |
1833 | ||
484b4061 JA |
1834 | blk_mq_map_swqueue(q); |
1835 | ||
320ae51f | 1836 | return q; |
18741986 CH |
1837 | |
1838 | err_flush_rq: | |
1839 | kfree(q->flush_rq); | |
320ae51f | 1840 | err_hw: |
320ae51f JA |
1841 | blk_cleanup_queue(q); |
1842 | err_hctxs: | |
f14bbe77 | 1843 | kfree(map); |
24d2f903 | 1844 | for (i = 0; i < set->nr_hw_queues; i++) { |
320ae51f JA |
1845 | if (!hctxs[i]) |
1846 | break; | |
e4043dcf | 1847 | free_cpumask_var(hctxs[i]->cpumask); |
cdef54dd | 1848 | kfree(hctxs[i]); |
320ae51f | 1849 | } |
f14bbe77 | 1850 | err_map: |
320ae51f JA |
1851 | kfree(hctxs); |
1852 | err_percpu: | |
1853 | free_percpu(ctx); | |
1854 | return ERR_PTR(-ENOMEM); | |
1855 | } | |
1856 | EXPORT_SYMBOL(blk_mq_init_queue); | |
1857 | ||
1858 | void blk_mq_free_queue(struct request_queue *q) | |
1859 | { | |
624dbe47 | 1860 | struct blk_mq_tag_set *set = q->tag_set; |
320ae51f | 1861 | |
0d2602ca JA |
1862 | blk_mq_del_queue_tag_set(q); |
1863 | ||
624dbe47 ML |
1864 | blk_mq_exit_hw_queues(q, set, set->nr_hw_queues); |
1865 | blk_mq_free_hw_queues(q, set); | |
320ae51f | 1866 | |
add703fd | 1867 | percpu_ref_exit(&q->mq_usage_counter); |
3d2936f4 | 1868 | |
320ae51f JA |
1869 | free_percpu(q->queue_ctx); |
1870 | kfree(q->queue_hw_ctx); | |
1871 | kfree(q->mq_map); | |
1872 | ||
1873 | q->queue_ctx = NULL; | |
1874 | q->queue_hw_ctx = NULL; | |
1875 | q->mq_map = NULL; | |
1876 | ||
1877 | mutex_lock(&all_q_mutex); | |
1878 | list_del_init(&q->all_q_node); | |
1879 | mutex_unlock(&all_q_mutex); | |
1880 | } | |
320ae51f JA |
1881 | |
1882 | /* Basically redo blk_mq_init_queue with queue frozen */ | |
f618ef7c | 1883 | static void blk_mq_queue_reinit(struct request_queue *q) |
320ae51f JA |
1884 | { |
1885 | blk_mq_freeze_queue(q); | |
1886 | ||
67aec14c JA |
1887 | blk_mq_sysfs_unregister(q); |
1888 | ||
320ae51f JA |
1889 | blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues); |
1890 | ||
1891 | /* | |
1892 | * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe | |
1893 | * we should change hctx numa_node according to new topology (this | |
1894 | * involves free and re-allocate memory, worthy doing?) | |
1895 | */ | |
1896 | ||
1897 | blk_mq_map_swqueue(q); | |
1898 | ||
67aec14c JA |
1899 | blk_mq_sysfs_register(q); |
1900 | ||
320ae51f JA |
1901 | blk_mq_unfreeze_queue(q); |
1902 | } | |
1903 | ||
f618ef7c PG |
1904 | static int blk_mq_queue_reinit_notify(struct notifier_block *nb, |
1905 | unsigned long action, void *hcpu) | |
320ae51f JA |
1906 | { |
1907 | struct request_queue *q; | |
1908 | ||
1909 | /* | |
9fccfed8 JA |
1910 | * Before new mappings are established, hotadded cpu might already |
1911 | * start handling requests. This doesn't break anything as we map | |
1912 | * offline CPUs to first hardware queue. We will re-init the queue | |
1913 | * below to get optimal settings. | |
320ae51f JA |
1914 | */ |
1915 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN && | |
1916 | action != CPU_ONLINE && action != CPU_ONLINE_FROZEN) | |
1917 | return NOTIFY_OK; | |
1918 | ||
1919 | mutex_lock(&all_q_mutex); | |
1920 | list_for_each_entry(q, &all_q_list, all_q_node) | |
1921 | blk_mq_queue_reinit(q); | |
1922 | mutex_unlock(&all_q_mutex); | |
1923 | return NOTIFY_OK; | |
1924 | } | |
1925 | ||
a5164405 JA |
1926 | static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set) |
1927 | { | |
1928 | int i; | |
1929 | ||
1930 | for (i = 0; i < set->nr_hw_queues; i++) { | |
1931 | set->tags[i] = blk_mq_init_rq_map(set, i); | |
1932 | if (!set->tags[i]) | |
1933 | goto out_unwind; | |
1934 | } | |
1935 | ||
1936 | return 0; | |
1937 | ||
1938 | out_unwind: | |
1939 | while (--i >= 0) | |
1940 | blk_mq_free_rq_map(set, set->tags[i], i); | |
1941 | ||
a5164405 JA |
1942 | return -ENOMEM; |
1943 | } | |
1944 | ||
1945 | /* | |
1946 | * Allocate the request maps associated with this tag_set. Note that this | |
1947 | * may reduce the depth asked for, if memory is tight. set->queue_depth | |
1948 | * will be updated to reflect the allocated depth. | |
1949 | */ | |
1950 | static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set) | |
1951 | { | |
1952 | unsigned int depth; | |
1953 | int err; | |
1954 | ||
1955 | depth = set->queue_depth; | |
1956 | do { | |
1957 | err = __blk_mq_alloc_rq_maps(set); | |
1958 | if (!err) | |
1959 | break; | |
1960 | ||
1961 | set->queue_depth >>= 1; | |
1962 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) { | |
1963 | err = -ENOMEM; | |
1964 | break; | |
1965 | } | |
1966 | } while (set->queue_depth); | |
1967 | ||
1968 | if (!set->queue_depth || err) { | |
1969 | pr_err("blk-mq: failed to allocate request map\n"); | |
1970 | return -ENOMEM; | |
1971 | } | |
1972 | ||
1973 | if (depth != set->queue_depth) | |
1974 | pr_info("blk-mq: reduced tag depth (%u -> %u)\n", | |
1975 | depth, set->queue_depth); | |
1976 | ||
1977 | return 0; | |
1978 | } | |
1979 | ||
a4391c64 JA |
1980 | /* |
1981 | * Alloc a tag set to be associated with one or more request queues. | |
1982 | * May fail with EINVAL for various error conditions. May adjust the | |
1983 | * requested depth down, if if it too large. In that case, the set | |
1984 | * value will be stored in set->queue_depth. | |
1985 | */ | |
24d2f903 CH |
1986 | int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) |
1987 | { | |
24d2f903 CH |
1988 | if (!set->nr_hw_queues) |
1989 | return -EINVAL; | |
a4391c64 | 1990 | if (!set->queue_depth) |
24d2f903 CH |
1991 | return -EINVAL; |
1992 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) | |
1993 | return -EINVAL; | |
1994 | ||
cdef54dd | 1995 | if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue) |
24d2f903 CH |
1996 | return -EINVAL; |
1997 | ||
a4391c64 JA |
1998 | if (set->queue_depth > BLK_MQ_MAX_DEPTH) { |
1999 | pr_info("blk-mq: reduced tag depth to %u\n", | |
2000 | BLK_MQ_MAX_DEPTH); | |
2001 | set->queue_depth = BLK_MQ_MAX_DEPTH; | |
2002 | } | |
24d2f903 | 2003 | |
48479005 ML |
2004 | set->tags = kmalloc_node(set->nr_hw_queues * |
2005 | sizeof(struct blk_mq_tags *), | |
24d2f903 CH |
2006 | GFP_KERNEL, set->numa_node); |
2007 | if (!set->tags) | |
a5164405 | 2008 | return -ENOMEM; |
24d2f903 | 2009 | |
a5164405 JA |
2010 | if (blk_mq_alloc_rq_maps(set)) |
2011 | goto enomem; | |
24d2f903 | 2012 | |
0d2602ca JA |
2013 | mutex_init(&set->tag_list_lock); |
2014 | INIT_LIST_HEAD(&set->tag_list); | |
2015 | ||
24d2f903 | 2016 | return 0; |
a5164405 | 2017 | enomem: |
5676e7b6 RE |
2018 | kfree(set->tags); |
2019 | set->tags = NULL; | |
24d2f903 CH |
2020 | return -ENOMEM; |
2021 | } | |
2022 | EXPORT_SYMBOL(blk_mq_alloc_tag_set); | |
2023 | ||
2024 | void blk_mq_free_tag_set(struct blk_mq_tag_set *set) | |
2025 | { | |
2026 | int i; | |
2027 | ||
484b4061 JA |
2028 | for (i = 0; i < set->nr_hw_queues; i++) { |
2029 | if (set->tags[i]) | |
2030 | blk_mq_free_rq_map(set, set->tags[i], i); | |
2031 | } | |
2032 | ||
981bd189 | 2033 | kfree(set->tags); |
5676e7b6 | 2034 | set->tags = NULL; |
24d2f903 CH |
2035 | } |
2036 | EXPORT_SYMBOL(blk_mq_free_tag_set); | |
2037 | ||
e3a2b3f9 JA |
2038 | int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) |
2039 | { | |
2040 | struct blk_mq_tag_set *set = q->tag_set; | |
2041 | struct blk_mq_hw_ctx *hctx; | |
2042 | int i, ret; | |
2043 | ||
2044 | if (!set || nr > set->queue_depth) | |
2045 | return -EINVAL; | |
2046 | ||
2047 | ret = 0; | |
2048 | queue_for_each_hw_ctx(q, hctx, i) { | |
2049 | ret = blk_mq_tag_update_depth(hctx->tags, nr); | |
2050 | if (ret) | |
2051 | break; | |
2052 | } | |
2053 | ||
2054 | if (!ret) | |
2055 | q->nr_requests = nr; | |
2056 | ||
2057 | return ret; | |
2058 | } | |
2059 | ||
676141e4 JA |
2060 | void blk_mq_disable_hotplug(void) |
2061 | { | |
2062 | mutex_lock(&all_q_mutex); | |
2063 | } | |
2064 | ||
2065 | void blk_mq_enable_hotplug(void) | |
2066 | { | |
2067 | mutex_unlock(&all_q_mutex); | |
2068 | } | |
2069 | ||
320ae51f JA |
2070 | static int __init blk_mq_init(void) |
2071 | { | |
320ae51f JA |
2072 | blk_mq_cpu_init(); |
2073 | ||
add703fd | 2074 | hotcpu_notifier(blk_mq_queue_reinit_notify, 0); |
320ae51f JA |
2075 | |
2076 | return 0; | |
2077 | } | |
2078 | subsys_initcall(blk_mq_init); |