Commit | Line | Data |
---|---|---|
320ae51f JA |
1 | #include <linux/kernel.h> |
2 | #include <linux/module.h> | |
3 | #include <linux/backing-dev.h> | |
4 | #include <linux/bio.h> | |
5 | #include <linux/blkdev.h> | |
6 | #include <linux/mm.h> | |
7 | #include <linux/init.h> | |
8 | #include <linux/slab.h> | |
9 | #include <linux/workqueue.h> | |
10 | #include <linux/smp.h> | |
11 | #include <linux/llist.h> | |
12 | #include <linux/list_sort.h> | |
13 | #include <linux/cpu.h> | |
14 | #include <linux/cache.h> | |
15 | #include <linux/sched/sysctl.h> | |
16 | #include <linux/delay.h> | |
17 | ||
18 | #include <trace/events/block.h> | |
19 | ||
20 | #include <linux/blk-mq.h> | |
21 | #include "blk.h" | |
22 | #include "blk-mq.h" | |
23 | #include "blk-mq-tag.h" | |
24 | ||
25 | static DEFINE_MUTEX(all_q_mutex); | |
26 | static LIST_HEAD(all_q_list); | |
27 | ||
28 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx); | |
29 | ||
320ae51f JA |
30 | static struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q, |
31 | unsigned int cpu) | |
32 | { | |
33 | return per_cpu_ptr(q->queue_ctx, cpu); | |
34 | } | |
35 | ||
36 | /* | |
37 | * This assumes per-cpu software queueing queues. They could be per-node | |
38 | * as well, for instance. For now this is hardcoded as-is. Note that we don't | |
39 | * care about preemption, since we know the ctx's are persistent. This does | |
40 | * mean that we can't rely on ctx always matching the currently running CPU. | |
41 | */ | |
42 | static struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q) | |
43 | { | |
44 | return __blk_mq_get_ctx(q, get_cpu()); | |
45 | } | |
46 | ||
47 | static void blk_mq_put_ctx(struct blk_mq_ctx *ctx) | |
48 | { | |
49 | put_cpu(); | |
50 | } | |
51 | ||
52 | /* | |
53 | * Check if any of the ctx's have pending work in this hardware queue | |
54 | */ | |
55 | static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx) | |
56 | { | |
57 | unsigned int i; | |
58 | ||
59 | for (i = 0; i < hctx->nr_ctx_map; i++) | |
60 | if (hctx->ctx_map[i]) | |
61 | return true; | |
62 | ||
63 | return false; | |
64 | } | |
65 | ||
66 | /* | |
67 | * Mark this ctx as having pending work in this hardware queue | |
68 | */ | |
69 | static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx, | |
70 | struct blk_mq_ctx *ctx) | |
71 | { | |
72 | if (!test_bit(ctx->index_hw, hctx->ctx_map)) | |
73 | set_bit(ctx->index_hw, hctx->ctx_map); | |
74 | } | |
75 | ||
081241e5 CH |
76 | static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx, |
77 | gfp_t gfp, bool reserved) | |
320ae51f JA |
78 | { |
79 | struct request *rq; | |
80 | unsigned int tag; | |
81 | ||
82 | tag = blk_mq_get_tag(hctx->tags, gfp, reserved); | |
83 | if (tag != BLK_MQ_TAG_FAIL) { | |
24d2f903 | 84 | rq = hctx->tags->rqs[tag]; |
70ab0b2d | 85 | blk_rq_init(hctx->queue, rq); |
320ae51f JA |
86 | rq->tag = tag; |
87 | ||
88 | return rq; | |
89 | } | |
90 | ||
91 | return NULL; | |
92 | } | |
93 | ||
94 | static int blk_mq_queue_enter(struct request_queue *q) | |
95 | { | |
96 | int ret; | |
97 | ||
98 | __percpu_counter_add(&q->mq_usage_counter, 1, 1000000); | |
99 | smp_wmb(); | |
100 | /* we have problems to freeze the queue if it's initializing */ | |
101 | if (!blk_queue_bypass(q) || !blk_queue_init_done(q)) | |
102 | return 0; | |
103 | ||
104 | __percpu_counter_add(&q->mq_usage_counter, -1, 1000000); | |
105 | ||
106 | spin_lock_irq(q->queue_lock); | |
107 | ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq, | |
43a5e4e2 ML |
108 | !blk_queue_bypass(q) || blk_queue_dying(q), |
109 | *q->queue_lock); | |
320ae51f | 110 | /* inc usage with lock hold to avoid freeze_queue runs here */ |
43a5e4e2 | 111 | if (!ret && !blk_queue_dying(q)) |
320ae51f | 112 | __percpu_counter_add(&q->mq_usage_counter, 1, 1000000); |
43a5e4e2 ML |
113 | else if (blk_queue_dying(q)) |
114 | ret = -ENODEV; | |
320ae51f JA |
115 | spin_unlock_irq(q->queue_lock); |
116 | ||
117 | return ret; | |
118 | } | |
119 | ||
120 | static void blk_mq_queue_exit(struct request_queue *q) | |
121 | { | |
122 | __percpu_counter_add(&q->mq_usage_counter, -1, 1000000); | |
123 | } | |
124 | ||
43a5e4e2 ML |
125 | static void __blk_mq_drain_queue(struct request_queue *q) |
126 | { | |
127 | while (true) { | |
128 | s64 count; | |
129 | ||
130 | spin_lock_irq(q->queue_lock); | |
131 | count = percpu_counter_sum(&q->mq_usage_counter); | |
132 | spin_unlock_irq(q->queue_lock); | |
133 | ||
134 | if (count == 0) | |
135 | break; | |
136 | blk_mq_run_queues(q, false); | |
137 | msleep(10); | |
138 | } | |
139 | } | |
140 | ||
320ae51f JA |
141 | /* |
142 | * Guarantee no request is in use, so we can change any data structure of | |
143 | * the queue afterward. | |
144 | */ | |
145 | static void blk_mq_freeze_queue(struct request_queue *q) | |
146 | { | |
147 | bool drain; | |
148 | ||
149 | spin_lock_irq(q->queue_lock); | |
150 | drain = !q->bypass_depth++; | |
151 | queue_flag_set(QUEUE_FLAG_BYPASS, q); | |
152 | spin_unlock_irq(q->queue_lock); | |
153 | ||
43a5e4e2 ML |
154 | if (drain) |
155 | __blk_mq_drain_queue(q); | |
156 | } | |
320ae51f | 157 | |
43a5e4e2 ML |
158 | void blk_mq_drain_queue(struct request_queue *q) |
159 | { | |
160 | __blk_mq_drain_queue(q); | |
320ae51f JA |
161 | } |
162 | ||
163 | static void blk_mq_unfreeze_queue(struct request_queue *q) | |
164 | { | |
165 | bool wake = false; | |
166 | ||
167 | spin_lock_irq(q->queue_lock); | |
168 | if (!--q->bypass_depth) { | |
169 | queue_flag_clear(QUEUE_FLAG_BYPASS, q); | |
170 | wake = true; | |
171 | } | |
172 | WARN_ON_ONCE(q->bypass_depth < 0); | |
173 | spin_unlock_irq(q->queue_lock); | |
174 | if (wake) | |
175 | wake_up_all(&q->mq_freeze_wq); | |
176 | } | |
177 | ||
178 | bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx) | |
179 | { | |
180 | return blk_mq_has_free_tags(hctx->tags); | |
181 | } | |
182 | EXPORT_SYMBOL(blk_mq_can_queue); | |
183 | ||
94eddfbe JA |
184 | static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx, |
185 | struct request *rq, unsigned int rw_flags) | |
320ae51f | 186 | { |
94eddfbe JA |
187 | if (blk_queue_io_stat(q)) |
188 | rw_flags |= REQ_IO_STAT; | |
189 | ||
320ae51f JA |
190 | rq->mq_ctx = ctx; |
191 | rq->cmd_flags = rw_flags; | |
0fec08b4 ML |
192 | rq->start_time = jiffies; |
193 | set_start_time_ns(rq); | |
320ae51f JA |
194 | ctx->rq_dispatched[rw_is_sync(rw_flags)]++; |
195 | } | |
196 | ||
320ae51f JA |
197 | static struct request *blk_mq_alloc_request_pinned(struct request_queue *q, |
198 | int rw, gfp_t gfp, | |
199 | bool reserved) | |
200 | { | |
201 | struct request *rq; | |
202 | ||
203 | do { | |
204 | struct blk_mq_ctx *ctx = blk_mq_get_ctx(q); | |
205 | struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
206 | ||
18741986 | 207 | rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved); |
320ae51f | 208 | if (rq) { |
94eddfbe | 209 | blk_mq_rq_ctx_init(q, ctx, rq, rw); |
320ae51f | 210 | break; |
959a35f1 | 211 | } |
320ae51f | 212 | |
e4043dcf JA |
213 | if (gfp & __GFP_WAIT) { |
214 | __blk_mq_run_hw_queue(hctx); | |
215 | blk_mq_put_ctx(ctx); | |
216 | } else { | |
217 | blk_mq_put_ctx(ctx); | |
959a35f1 | 218 | break; |
e4043dcf | 219 | } |
959a35f1 | 220 | |
5810d903 | 221 | blk_mq_wait_for_tags(hctx->tags, reserved); |
320ae51f JA |
222 | } while (1); |
223 | ||
224 | return rq; | |
225 | } | |
226 | ||
18741986 | 227 | struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp) |
320ae51f JA |
228 | { |
229 | struct request *rq; | |
230 | ||
231 | if (blk_mq_queue_enter(q)) | |
232 | return NULL; | |
233 | ||
18741986 | 234 | rq = blk_mq_alloc_request_pinned(q, rw, gfp, false); |
959a35f1 JM |
235 | if (rq) |
236 | blk_mq_put_ctx(rq->mq_ctx); | |
320ae51f JA |
237 | return rq; |
238 | } | |
239 | ||
240 | struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw, | |
241 | gfp_t gfp) | |
242 | { | |
243 | struct request *rq; | |
244 | ||
245 | if (blk_mq_queue_enter(q)) | |
246 | return NULL; | |
247 | ||
248 | rq = blk_mq_alloc_request_pinned(q, rw, gfp, true); | |
959a35f1 JM |
249 | if (rq) |
250 | blk_mq_put_ctx(rq->mq_ctx); | |
320ae51f JA |
251 | return rq; |
252 | } | |
253 | EXPORT_SYMBOL(blk_mq_alloc_reserved_request); | |
254 | ||
320ae51f JA |
255 | static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx, |
256 | struct blk_mq_ctx *ctx, struct request *rq) | |
257 | { | |
258 | const int tag = rq->tag; | |
259 | struct request_queue *q = rq->q; | |
260 | ||
320ae51f | 261 | blk_mq_put_tag(hctx->tags, tag); |
320ae51f JA |
262 | blk_mq_queue_exit(q); |
263 | } | |
264 | ||
265 | void blk_mq_free_request(struct request *rq) | |
266 | { | |
267 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
268 | struct blk_mq_hw_ctx *hctx; | |
269 | struct request_queue *q = rq->q; | |
270 | ||
271 | ctx->rq_completed[rq_is_sync(rq)]++; | |
272 | ||
273 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
274 | __blk_mq_free_request(hctx, ctx, rq); | |
275 | } | |
276 | ||
8727af4b CH |
277 | /* |
278 | * Clone all relevant state from a request that has been put on hold in | |
279 | * the flush state machine into the preallocated flush request that hangs | |
280 | * off the request queue. | |
281 | * | |
282 | * For a driver the flush request should be invisible, that's why we are | |
283 | * impersonating the original request here. | |
284 | */ | |
285 | void blk_mq_clone_flush_request(struct request *flush_rq, | |
286 | struct request *orig_rq) | |
287 | { | |
288 | struct blk_mq_hw_ctx *hctx = | |
289 | orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu); | |
290 | ||
291 | flush_rq->mq_ctx = orig_rq->mq_ctx; | |
292 | flush_rq->tag = orig_rq->tag; | |
293 | memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq), | |
294 | hctx->cmd_size); | |
295 | } | |
296 | ||
63151a44 | 297 | inline void __blk_mq_end_io(struct request *rq, int error) |
320ae51f | 298 | { |
0d11e6ac ML |
299 | blk_account_io_done(rq); |
300 | ||
91b63639 | 301 | if (rq->end_io) { |
320ae51f | 302 | rq->end_io(rq, error); |
91b63639 CH |
303 | } else { |
304 | if (unlikely(blk_bidi_rq(rq))) | |
305 | blk_mq_free_request(rq->next_rq); | |
320ae51f | 306 | blk_mq_free_request(rq); |
91b63639 | 307 | } |
320ae51f | 308 | } |
63151a44 CH |
309 | EXPORT_SYMBOL(__blk_mq_end_io); |
310 | ||
311 | void blk_mq_end_io(struct request *rq, int error) | |
312 | { | |
313 | if (blk_update_request(rq, error, blk_rq_bytes(rq))) | |
314 | BUG(); | |
315 | __blk_mq_end_io(rq, error); | |
316 | } | |
317 | EXPORT_SYMBOL(blk_mq_end_io); | |
320ae51f | 318 | |
30a91cb4 | 319 | static void __blk_mq_complete_request_remote(void *data) |
320ae51f | 320 | { |
3d6efbf6 | 321 | struct request *rq = data; |
320ae51f | 322 | |
30a91cb4 | 323 | rq->q->softirq_done_fn(rq); |
320ae51f | 324 | } |
320ae51f | 325 | |
30a91cb4 | 326 | void __blk_mq_complete_request(struct request *rq) |
320ae51f JA |
327 | { |
328 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
38535201 | 329 | bool shared = false; |
320ae51f JA |
330 | int cpu; |
331 | ||
38535201 | 332 | if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) { |
30a91cb4 CH |
333 | rq->q->softirq_done_fn(rq); |
334 | return; | |
335 | } | |
320ae51f JA |
336 | |
337 | cpu = get_cpu(); | |
38535201 CH |
338 | if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags)) |
339 | shared = cpus_share_cache(cpu, ctx->cpu); | |
340 | ||
341 | if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) { | |
30a91cb4 | 342 | rq->csd.func = __blk_mq_complete_request_remote; |
3d6efbf6 CH |
343 | rq->csd.info = rq; |
344 | rq->csd.flags = 0; | |
c46fff2a | 345 | smp_call_function_single_async(ctx->cpu, &rq->csd); |
3d6efbf6 | 346 | } else { |
30a91cb4 | 347 | rq->q->softirq_done_fn(rq); |
3d6efbf6 | 348 | } |
320ae51f JA |
349 | put_cpu(); |
350 | } | |
30a91cb4 CH |
351 | |
352 | /** | |
353 | * blk_mq_complete_request - end I/O on a request | |
354 | * @rq: the request being processed | |
355 | * | |
356 | * Description: | |
357 | * Ends all I/O on a request. It does not handle partial completions. | |
358 | * The actual completion happens out-of-order, through a IPI handler. | |
359 | **/ | |
360 | void blk_mq_complete_request(struct request *rq) | |
361 | { | |
362 | if (unlikely(blk_should_fake_timeout(rq->q))) | |
363 | return; | |
364 | if (!blk_mark_rq_complete(rq)) | |
365 | __blk_mq_complete_request(rq); | |
366 | } | |
367 | EXPORT_SYMBOL(blk_mq_complete_request); | |
320ae51f | 368 | |
49f5baa5 | 369 | static void blk_mq_start_request(struct request *rq, bool last) |
320ae51f JA |
370 | { |
371 | struct request_queue *q = rq->q; | |
372 | ||
373 | trace_block_rq_issue(q, rq); | |
374 | ||
742ee69b | 375 | rq->resid_len = blk_rq_bytes(rq); |
91b63639 CH |
376 | if (unlikely(blk_bidi_rq(rq))) |
377 | rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq); | |
742ee69b | 378 | |
320ae51f JA |
379 | /* |
380 | * Just mark start time and set the started bit. Due to memory | |
381 | * ordering, we know we'll see the correct deadline as long as | |
382 | * REQ_ATOMIC_STARTED is seen. | |
383 | */ | |
384 | rq->deadline = jiffies + q->rq_timeout; | |
87ee7b11 JA |
385 | |
386 | /* | |
387 | * Mark us as started and clear complete. Complete might have been | |
388 | * set if requeue raced with timeout, which then marked it as | |
389 | * complete. So be sure to clear complete again when we start | |
390 | * the request, otherwise we'll ignore the completion event. | |
391 | */ | |
320ae51f | 392 | set_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
87ee7b11 | 393 | clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags); |
49f5baa5 CH |
394 | |
395 | if (q->dma_drain_size && blk_rq_bytes(rq)) { | |
396 | /* | |
397 | * Make sure space for the drain appears. We know we can do | |
398 | * this because max_hw_segments has been adjusted to be one | |
399 | * fewer than the device can handle. | |
400 | */ | |
401 | rq->nr_phys_segments++; | |
402 | } | |
403 | ||
404 | /* | |
405 | * Flag the last request in the series so that drivers know when IO | |
406 | * should be kicked off, if they don't do it on a per-request basis. | |
407 | * | |
408 | * Note: the flag isn't the only condition drivers should do kick off. | |
409 | * If drive is busy, the last request might not have the bit set. | |
410 | */ | |
411 | if (last) | |
412 | rq->cmd_flags |= REQ_END; | |
320ae51f JA |
413 | } |
414 | ||
ed0791b2 | 415 | static void __blk_mq_requeue_request(struct request *rq) |
320ae51f JA |
416 | { |
417 | struct request_queue *q = rq->q; | |
418 | ||
419 | trace_block_rq_requeue(q, rq); | |
420 | clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags); | |
49f5baa5 CH |
421 | |
422 | rq->cmd_flags &= ~REQ_END; | |
423 | ||
424 | if (q->dma_drain_size && blk_rq_bytes(rq)) | |
425 | rq->nr_phys_segments--; | |
320ae51f JA |
426 | } |
427 | ||
ed0791b2 CH |
428 | void blk_mq_requeue_request(struct request *rq) |
429 | { | |
ed0791b2 CH |
430 | __blk_mq_requeue_request(rq); |
431 | blk_clear_rq_complete(rq); | |
432 | ||
ed0791b2 CH |
433 | BUG_ON(blk_queued_rq(rq)); |
434 | blk_mq_insert_request(rq, true, true, false); | |
435 | } | |
436 | EXPORT_SYMBOL(blk_mq_requeue_request); | |
437 | ||
24d2f903 CH |
438 | struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag) |
439 | { | |
440 | return tags->rqs[tag]; | |
441 | } | |
442 | EXPORT_SYMBOL(blk_mq_tag_to_rq); | |
443 | ||
320ae51f JA |
444 | struct blk_mq_timeout_data { |
445 | struct blk_mq_hw_ctx *hctx; | |
446 | unsigned long *next; | |
447 | unsigned int *next_set; | |
448 | }; | |
449 | ||
450 | static void blk_mq_timeout_check(void *__data, unsigned long *free_tags) | |
451 | { | |
452 | struct blk_mq_timeout_data *data = __data; | |
453 | struct blk_mq_hw_ctx *hctx = data->hctx; | |
454 | unsigned int tag; | |
455 | ||
456 | /* It may not be in flight yet (this is where | |
457 | * the REQ_ATOMIC_STARTED flag comes in). The requests are | |
458 | * statically allocated, so we know it's always safe to access the | |
459 | * memory associated with a bit offset into ->rqs[]. | |
460 | */ | |
461 | tag = 0; | |
462 | do { | |
463 | struct request *rq; | |
464 | ||
24d2f903 CH |
465 | tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag); |
466 | if (tag >= hctx->tags->nr_tags) | |
320ae51f JA |
467 | break; |
468 | ||
24d2f903 CH |
469 | rq = blk_mq_tag_to_rq(hctx->tags, tag++); |
470 | if (rq->q != hctx->queue) | |
471 | continue; | |
320ae51f JA |
472 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
473 | continue; | |
474 | ||
475 | blk_rq_check_expired(rq, data->next, data->next_set); | |
476 | } while (1); | |
477 | } | |
478 | ||
479 | static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx, | |
480 | unsigned long *next, | |
481 | unsigned int *next_set) | |
482 | { | |
483 | struct blk_mq_timeout_data data = { | |
484 | .hctx = hctx, | |
485 | .next = next, | |
486 | .next_set = next_set, | |
487 | }; | |
488 | ||
489 | /* | |
490 | * Ask the tagging code to iterate busy requests, so we can | |
491 | * check them for timeout. | |
492 | */ | |
493 | blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data); | |
494 | } | |
495 | ||
87ee7b11 JA |
496 | static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq) |
497 | { | |
498 | struct request_queue *q = rq->q; | |
499 | ||
500 | /* | |
501 | * We know that complete is set at this point. If STARTED isn't set | |
502 | * anymore, then the request isn't active and the "timeout" should | |
503 | * just be ignored. This can happen due to the bitflag ordering. | |
504 | * Timeout first checks if STARTED is set, and if it is, assumes | |
505 | * the request is active. But if we race with completion, then | |
506 | * we both flags will get cleared. So check here again, and ignore | |
507 | * a timeout event with a request that isn't active. | |
508 | */ | |
509 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) | |
510 | return BLK_EH_NOT_HANDLED; | |
511 | ||
512 | if (!q->mq_ops->timeout) | |
513 | return BLK_EH_RESET_TIMER; | |
514 | ||
515 | return q->mq_ops->timeout(rq); | |
516 | } | |
517 | ||
320ae51f JA |
518 | static void blk_mq_rq_timer(unsigned long data) |
519 | { | |
520 | struct request_queue *q = (struct request_queue *) data; | |
521 | struct blk_mq_hw_ctx *hctx; | |
522 | unsigned long next = 0; | |
523 | int i, next_set = 0; | |
524 | ||
525 | queue_for_each_hw_ctx(q, hctx, i) | |
526 | blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set); | |
527 | ||
528 | if (next_set) | |
529 | mod_timer(&q->timeout, round_jiffies_up(next)); | |
530 | } | |
531 | ||
532 | /* | |
533 | * Reverse check our software queue for entries that we could potentially | |
534 | * merge with. Currently includes a hand-wavy stop count of 8, to not spend | |
535 | * too much time checking for merges. | |
536 | */ | |
537 | static bool blk_mq_attempt_merge(struct request_queue *q, | |
538 | struct blk_mq_ctx *ctx, struct bio *bio) | |
539 | { | |
540 | struct request *rq; | |
541 | int checked = 8; | |
542 | ||
543 | list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) { | |
544 | int el_ret; | |
545 | ||
546 | if (!checked--) | |
547 | break; | |
548 | ||
549 | if (!blk_rq_merge_ok(rq, bio)) | |
550 | continue; | |
551 | ||
552 | el_ret = blk_try_merge(rq, bio); | |
553 | if (el_ret == ELEVATOR_BACK_MERGE) { | |
554 | if (bio_attempt_back_merge(q, rq, bio)) { | |
555 | ctx->rq_merged++; | |
556 | return true; | |
557 | } | |
558 | break; | |
559 | } else if (el_ret == ELEVATOR_FRONT_MERGE) { | |
560 | if (bio_attempt_front_merge(q, rq, bio)) { | |
561 | ctx->rq_merged++; | |
562 | return true; | |
563 | } | |
564 | break; | |
565 | } | |
566 | } | |
567 | ||
568 | return false; | |
569 | } | |
570 | ||
320ae51f JA |
571 | /* |
572 | * Run this hardware queue, pulling any software queues mapped to it in. | |
573 | * Note that this function currently has various problems around ordering | |
574 | * of IO. In particular, we'd like FIFO behaviour on handling existing | |
575 | * items on the hctx->dispatch list. Ignore that for now. | |
576 | */ | |
577 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) | |
578 | { | |
579 | struct request_queue *q = hctx->queue; | |
580 | struct blk_mq_ctx *ctx; | |
581 | struct request *rq; | |
582 | LIST_HEAD(rq_list); | |
583 | int bit, queued; | |
584 | ||
fd1270d5 | 585 | WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)); |
e4043dcf | 586 | |
5d12f905 | 587 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
320ae51f JA |
588 | return; |
589 | ||
590 | hctx->run++; | |
591 | ||
592 | /* | |
593 | * Touch any software queue that has pending entries. | |
594 | */ | |
595 | for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) { | |
596 | clear_bit(bit, hctx->ctx_map); | |
597 | ctx = hctx->ctxs[bit]; | |
320ae51f JA |
598 | |
599 | spin_lock(&ctx->lock); | |
600 | list_splice_tail_init(&ctx->rq_list, &rq_list); | |
601 | spin_unlock(&ctx->lock); | |
602 | } | |
603 | ||
604 | /* | |
605 | * If we have previous entries on our dispatch list, grab them | |
606 | * and stuff them at the front for more fair dispatch. | |
607 | */ | |
608 | if (!list_empty_careful(&hctx->dispatch)) { | |
609 | spin_lock(&hctx->lock); | |
610 | if (!list_empty(&hctx->dispatch)) | |
611 | list_splice_init(&hctx->dispatch, &rq_list); | |
612 | spin_unlock(&hctx->lock); | |
613 | } | |
614 | ||
615 | /* | |
616 | * Delete and return all entries from our dispatch list | |
617 | */ | |
618 | queued = 0; | |
619 | ||
620 | /* | |
621 | * Now process all the entries, sending them to the driver. | |
622 | */ | |
623 | while (!list_empty(&rq_list)) { | |
624 | int ret; | |
625 | ||
626 | rq = list_first_entry(&rq_list, struct request, queuelist); | |
627 | list_del_init(&rq->queuelist); | |
320ae51f | 628 | |
49f5baa5 | 629 | blk_mq_start_request(rq, list_empty(&rq_list)); |
320ae51f JA |
630 | |
631 | ret = q->mq_ops->queue_rq(hctx, rq); | |
632 | switch (ret) { | |
633 | case BLK_MQ_RQ_QUEUE_OK: | |
634 | queued++; | |
635 | continue; | |
636 | case BLK_MQ_RQ_QUEUE_BUSY: | |
637 | /* | |
638 | * FIXME: we should have a mechanism to stop the queue | |
639 | * like blk_stop_queue, otherwise we will waste cpu | |
640 | * time | |
641 | */ | |
642 | list_add(&rq->queuelist, &rq_list); | |
ed0791b2 | 643 | __blk_mq_requeue_request(rq); |
320ae51f JA |
644 | break; |
645 | default: | |
646 | pr_err("blk-mq: bad return on queue: %d\n", ret); | |
320ae51f | 647 | case BLK_MQ_RQ_QUEUE_ERROR: |
1e93b8c2 | 648 | rq->errors = -EIO; |
320ae51f JA |
649 | blk_mq_end_io(rq, rq->errors); |
650 | break; | |
651 | } | |
652 | ||
653 | if (ret == BLK_MQ_RQ_QUEUE_BUSY) | |
654 | break; | |
655 | } | |
656 | ||
657 | if (!queued) | |
658 | hctx->dispatched[0]++; | |
659 | else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1))) | |
660 | hctx->dispatched[ilog2(queued) + 1]++; | |
661 | ||
662 | /* | |
663 | * Any items that need requeuing? Stuff them into hctx->dispatch, | |
664 | * that is where we will continue on next queue run. | |
665 | */ | |
666 | if (!list_empty(&rq_list)) { | |
667 | spin_lock(&hctx->lock); | |
668 | list_splice(&rq_list, &hctx->dispatch); | |
669 | spin_unlock(&hctx->lock); | |
670 | } | |
671 | } | |
672 | ||
673 | void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) | |
674 | { | |
5d12f905 | 675 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
320ae51f JA |
676 | return; |
677 | ||
e4043dcf | 678 | if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask)) |
320ae51f | 679 | __blk_mq_run_hw_queue(hctx); |
e4043dcf | 680 | else if (hctx->queue->nr_hw_queues == 1) |
70f4db63 | 681 | kblockd_schedule_delayed_work(&hctx->run_work, 0); |
e4043dcf JA |
682 | else { |
683 | unsigned int cpu; | |
684 | ||
685 | /* | |
686 | * It'd be great if the workqueue API had a way to pass | |
687 | * in a mask and had some smarts for more clever placement | |
688 | * than the first CPU. Or we could round-robin here. For now, | |
689 | * just queue on the first CPU. | |
690 | */ | |
691 | cpu = cpumask_first(hctx->cpumask); | |
70f4db63 | 692 | kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0); |
e4043dcf | 693 | } |
320ae51f JA |
694 | } |
695 | ||
696 | void blk_mq_run_queues(struct request_queue *q, bool async) | |
697 | { | |
698 | struct blk_mq_hw_ctx *hctx; | |
699 | int i; | |
700 | ||
701 | queue_for_each_hw_ctx(q, hctx, i) { | |
702 | if ((!blk_mq_hctx_has_pending(hctx) && | |
703 | list_empty_careful(&hctx->dispatch)) || | |
5d12f905 | 704 | test_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
320ae51f JA |
705 | continue; |
706 | ||
e4043dcf | 707 | preempt_disable(); |
320ae51f | 708 | blk_mq_run_hw_queue(hctx, async); |
e4043dcf | 709 | preempt_enable(); |
320ae51f JA |
710 | } |
711 | } | |
712 | EXPORT_SYMBOL(blk_mq_run_queues); | |
713 | ||
714 | void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx) | |
715 | { | |
70f4db63 CH |
716 | cancel_delayed_work(&hctx->run_work); |
717 | cancel_delayed_work(&hctx->delay_work); | |
320ae51f JA |
718 | set_bit(BLK_MQ_S_STOPPED, &hctx->state); |
719 | } | |
720 | EXPORT_SYMBOL(blk_mq_stop_hw_queue); | |
721 | ||
280d45f6 CH |
722 | void blk_mq_stop_hw_queues(struct request_queue *q) |
723 | { | |
724 | struct blk_mq_hw_ctx *hctx; | |
725 | int i; | |
726 | ||
727 | queue_for_each_hw_ctx(q, hctx, i) | |
728 | blk_mq_stop_hw_queue(hctx); | |
729 | } | |
730 | EXPORT_SYMBOL(blk_mq_stop_hw_queues); | |
731 | ||
320ae51f JA |
732 | void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx) |
733 | { | |
734 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
e4043dcf JA |
735 | |
736 | preempt_disable(); | |
320ae51f | 737 | __blk_mq_run_hw_queue(hctx); |
e4043dcf | 738 | preempt_enable(); |
320ae51f JA |
739 | } |
740 | EXPORT_SYMBOL(blk_mq_start_hw_queue); | |
741 | ||
2f268556 CH |
742 | void blk_mq_start_hw_queues(struct request_queue *q) |
743 | { | |
744 | struct blk_mq_hw_ctx *hctx; | |
745 | int i; | |
746 | ||
747 | queue_for_each_hw_ctx(q, hctx, i) | |
748 | blk_mq_start_hw_queue(hctx); | |
749 | } | |
750 | EXPORT_SYMBOL(blk_mq_start_hw_queues); | |
751 | ||
752 | ||
1b4a3258 | 753 | void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async) |
320ae51f JA |
754 | { |
755 | struct blk_mq_hw_ctx *hctx; | |
756 | int i; | |
757 | ||
758 | queue_for_each_hw_ctx(q, hctx, i) { | |
759 | if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state)) | |
760 | continue; | |
761 | ||
762 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
e4043dcf | 763 | preempt_disable(); |
1b4a3258 | 764 | blk_mq_run_hw_queue(hctx, async); |
e4043dcf | 765 | preempt_enable(); |
320ae51f JA |
766 | } |
767 | } | |
768 | EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues); | |
769 | ||
70f4db63 | 770 | static void blk_mq_run_work_fn(struct work_struct *work) |
320ae51f JA |
771 | { |
772 | struct blk_mq_hw_ctx *hctx; | |
773 | ||
70f4db63 | 774 | hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work); |
e4043dcf | 775 | |
320ae51f JA |
776 | __blk_mq_run_hw_queue(hctx); |
777 | } | |
778 | ||
70f4db63 CH |
779 | static void blk_mq_delay_work_fn(struct work_struct *work) |
780 | { | |
781 | struct blk_mq_hw_ctx *hctx; | |
782 | ||
783 | hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work); | |
784 | ||
785 | if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state)) | |
786 | __blk_mq_run_hw_queue(hctx); | |
787 | } | |
788 | ||
789 | void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs) | |
790 | { | |
791 | unsigned long tmo = msecs_to_jiffies(msecs); | |
792 | ||
793 | if (hctx->queue->nr_hw_queues == 1) | |
794 | kblockd_schedule_delayed_work(&hctx->delay_work, tmo); | |
795 | else { | |
796 | unsigned int cpu; | |
797 | ||
798 | /* | |
799 | * It'd be great if the workqueue API had a way to pass | |
800 | * in a mask and had some smarts for more clever placement | |
801 | * than the first CPU. Or we could round-robin here. For now, | |
802 | * just queue on the first CPU. | |
803 | */ | |
804 | cpu = cpumask_first(hctx->cpumask); | |
805 | kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo); | |
806 | } | |
807 | } | |
808 | EXPORT_SYMBOL(blk_mq_delay_queue); | |
809 | ||
320ae51f | 810 | static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, |
72a0a36e | 811 | struct request *rq, bool at_head) |
320ae51f JA |
812 | { |
813 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
814 | ||
01b983c9 JA |
815 | trace_block_rq_insert(hctx->queue, rq); |
816 | ||
72a0a36e CH |
817 | if (at_head) |
818 | list_add(&rq->queuelist, &ctx->rq_list); | |
819 | else | |
820 | list_add_tail(&rq->queuelist, &ctx->rq_list); | |
320ae51f JA |
821 | blk_mq_hctx_mark_pending(hctx, ctx); |
822 | ||
823 | /* | |
824 | * We do this early, to ensure we are on the right CPU. | |
825 | */ | |
87ee7b11 | 826 | blk_add_timer(rq); |
320ae51f JA |
827 | } |
828 | ||
eeabc850 CH |
829 | void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue, |
830 | bool async) | |
320ae51f | 831 | { |
eeabc850 | 832 | struct request_queue *q = rq->q; |
320ae51f | 833 | struct blk_mq_hw_ctx *hctx; |
eeabc850 CH |
834 | struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx; |
835 | ||
836 | current_ctx = blk_mq_get_ctx(q); | |
837 | if (!cpu_online(ctx->cpu)) | |
838 | rq->mq_ctx = ctx = current_ctx; | |
320ae51f | 839 | |
320ae51f JA |
840 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
841 | ||
eeabc850 CH |
842 | if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) && |
843 | !(rq->cmd_flags & (REQ_FLUSH_SEQ))) { | |
320ae51f JA |
844 | blk_insert_flush(rq); |
845 | } else { | |
320ae51f | 846 | spin_lock(&ctx->lock); |
72a0a36e | 847 | __blk_mq_insert_request(hctx, rq, at_head); |
320ae51f | 848 | spin_unlock(&ctx->lock); |
320ae51f JA |
849 | } |
850 | ||
320ae51f JA |
851 | if (run_queue) |
852 | blk_mq_run_hw_queue(hctx, async); | |
e4043dcf JA |
853 | |
854 | blk_mq_put_ctx(current_ctx); | |
320ae51f JA |
855 | } |
856 | ||
857 | static void blk_mq_insert_requests(struct request_queue *q, | |
858 | struct blk_mq_ctx *ctx, | |
859 | struct list_head *list, | |
860 | int depth, | |
861 | bool from_schedule) | |
862 | ||
863 | { | |
864 | struct blk_mq_hw_ctx *hctx; | |
865 | struct blk_mq_ctx *current_ctx; | |
866 | ||
867 | trace_block_unplug(q, depth, !from_schedule); | |
868 | ||
869 | current_ctx = blk_mq_get_ctx(q); | |
870 | ||
871 | if (!cpu_online(ctx->cpu)) | |
872 | ctx = current_ctx; | |
873 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
874 | ||
875 | /* | |
876 | * preemption doesn't flush plug list, so it's possible ctx->cpu is | |
877 | * offline now | |
878 | */ | |
879 | spin_lock(&ctx->lock); | |
880 | while (!list_empty(list)) { | |
881 | struct request *rq; | |
882 | ||
883 | rq = list_first_entry(list, struct request, queuelist); | |
884 | list_del_init(&rq->queuelist); | |
885 | rq->mq_ctx = ctx; | |
72a0a36e | 886 | __blk_mq_insert_request(hctx, rq, false); |
320ae51f JA |
887 | } |
888 | spin_unlock(&ctx->lock); | |
889 | ||
320ae51f | 890 | blk_mq_run_hw_queue(hctx, from_schedule); |
e4043dcf | 891 | blk_mq_put_ctx(current_ctx); |
320ae51f JA |
892 | } |
893 | ||
894 | static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b) | |
895 | { | |
896 | struct request *rqa = container_of(a, struct request, queuelist); | |
897 | struct request *rqb = container_of(b, struct request, queuelist); | |
898 | ||
899 | return !(rqa->mq_ctx < rqb->mq_ctx || | |
900 | (rqa->mq_ctx == rqb->mq_ctx && | |
901 | blk_rq_pos(rqa) < blk_rq_pos(rqb))); | |
902 | } | |
903 | ||
904 | void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) | |
905 | { | |
906 | struct blk_mq_ctx *this_ctx; | |
907 | struct request_queue *this_q; | |
908 | struct request *rq; | |
909 | LIST_HEAD(list); | |
910 | LIST_HEAD(ctx_list); | |
911 | unsigned int depth; | |
912 | ||
913 | list_splice_init(&plug->mq_list, &list); | |
914 | ||
915 | list_sort(NULL, &list, plug_ctx_cmp); | |
916 | ||
917 | this_q = NULL; | |
918 | this_ctx = NULL; | |
919 | depth = 0; | |
920 | ||
921 | while (!list_empty(&list)) { | |
922 | rq = list_entry_rq(list.next); | |
923 | list_del_init(&rq->queuelist); | |
924 | BUG_ON(!rq->q); | |
925 | if (rq->mq_ctx != this_ctx) { | |
926 | if (this_ctx) { | |
927 | blk_mq_insert_requests(this_q, this_ctx, | |
928 | &ctx_list, depth, | |
929 | from_schedule); | |
930 | } | |
931 | ||
932 | this_ctx = rq->mq_ctx; | |
933 | this_q = rq->q; | |
934 | depth = 0; | |
935 | } | |
936 | ||
937 | depth++; | |
938 | list_add_tail(&rq->queuelist, &ctx_list); | |
939 | } | |
940 | ||
941 | /* | |
942 | * If 'this_ctx' is set, we know we have entries to complete | |
943 | * on 'ctx_list'. Do those. | |
944 | */ | |
945 | if (this_ctx) { | |
946 | blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth, | |
947 | from_schedule); | |
948 | } | |
949 | } | |
950 | ||
951 | static void blk_mq_bio_to_request(struct request *rq, struct bio *bio) | |
952 | { | |
953 | init_request_from_bio(rq, bio); | |
954 | blk_account_io_start(rq, 1); | |
955 | } | |
956 | ||
957 | static void blk_mq_make_request(struct request_queue *q, struct bio *bio) | |
958 | { | |
959 | struct blk_mq_hw_ctx *hctx; | |
960 | struct blk_mq_ctx *ctx; | |
961 | const int is_sync = rw_is_sync(bio->bi_rw); | |
962 | const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA); | |
963 | int rw = bio_data_dir(bio); | |
964 | struct request *rq; | |
965 | unsigned int use_plug, request_count = 0; | |
966 | ||
967 | /* | |
968 | * If we have multiple hardware queues, just go directly to | |
969 | * one of those for sync IO. | |
970 | */ | |
971 | use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync); | |
972 | ||
973 | blk_queue_bounce(q, &bio); | |
974 | ||
14ec77f3 NB |
975 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { |
976 | bio_endio(bio, -EIO); | |
977 | return; | |
978 | } | |
979 | ||
320ae51f JA |
980 | if (use_plug && blk_attempt_plug_merge(q, bio, &request_count)) |
981 | return; | |
982 | ||
983 | if (blk_mq_queue_enter(q)) { | |
984 | bio_endio(bio, -EIO); | |
985 | return; | |
986 | } | |
987 | ||
988 | ctx = blk_mq_get_ctx(q); | |
989 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
990 | ||
27fbf4e8 SL |
991 | if (is_sync) |
992 | rw |= REQ_SYNC; | |
320ae51f | 993 | trace_block_getrq(q, bio, rw); |
18741986 | 994 | rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false); |
320ae51f | 995 | if (likely(rq)) |
18741986 | 996 | blk_mq_rq_ctx_init(q, ctx, rq, rw); |
320ae51f JA |
997 | else { |
998 | blk_mq_put_ctx(ctx); | |
999 | trace_block_sleeprq(q, bio, rw); | |
18741986 CH |
1000 | rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC, |
1001 | false); | |
320ae51f JA |
1002 | ctx = rq->mq_ctx; |
1003 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
1004 | } | |
1005 | ||
1006 | hctx->queued++; | |
1007 | ||
1008 | if (unlikely(is_flush_fua)) { | |
1009 | blk_mq_bio_to_request(rq, bio); | |
320ae51f JA |
1010 | blk_insert_flush(rq); |
1011 | goto run_queue; | |
1012 | } | |
1013 | ||
1014 | /* | |
1015 | * A task plug currently exists. Since this is completely lockless, | |
1016 | * utilize that to temporarily store requests until the task is | |
1017 | * either done or scheduled away. | |
1018 | */ | |
1019 | if (use_plug) { | |
1020 | struct blk_plug *plug = current->plug; | |
1021 | ||
1022 | if (plug) { | |
1023 | blk_mq_bio_to_request(rq, bio); | |
92f399c7 | 1024 | if (list_empty(&plug->mq_list)) |
320ae51f JA |
1025 | trace_block_plug(q); |
1026 | else if (request_count >= BLK_MAX_REQUEST_COUNT) { | |
1027 | blk_flush_plug_list(plug, false); | |
1028 | trace_block_plug(q); | |
1029 | } | |
1030 | list_add_tail(&rq->queuelist, &plug->mq_list); | |
1031 | blk_mq_put_ctx(ctx); | |
1032 | return; | |
1033 | } | |
1034 | } | |
1035 | ||
c6d600c6 JA |
1036 | if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) { |
1037 | init_request_from_bio(rq, bio); | |
320ae51f | 1038 | |
c6d600c6 JA |
1039 | spin_lock(&ctx->lock); |
1040 | insert_rq: | |
72a0a36e | 1041 | __blk_mq_insert_request(hctx, rq, false); |
c6d600c6 JA |
1042 | spin_unlock(&ctx->lock); |
1043 | blk_account_io_start(rq, 1); | |
1044 | } else { | |
1045 | spin_lock(&ctx->lock); | |
1046 | if (!blk_mq_attempt_merge(q, ctx, bio)) { | |
1047 | init_request_from_bio(rq, bio); | |
1048 | goto insert_rq; | |
1049 | } | |
1050 | ||
1051 | spin_unlock(&ctx->lock); | |
1052 | __blk_mq_free_request(hctx, ctx, rq); | |
320ae51f JA |
1053 | } |
1054 | ||
320ae51f JA |
1055 | |
1056 | /* | |
1057 | * For a SYNC request, send it to the hardware immediately. For an | |
1058 | * ASYNC request, just ensure that we run it later on. The latter | |
1059 | * allows for merging opportunities and more efficient dispatching. | |
1060 | */ | |
1061 | run_queue: | |
1062 | blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua); | |
e4043dcf | 1063 | blk_mq_put_ctx(ctx); |
320ae51f JA |
1064 | } |
1065 | ||
1066 | /* | |
1067 | * Default mapping to a software queue, since we use one per CPU. | |
1068 | */ | |
1069 | struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu) | |
1070 | { | |
1071 | return q->queue_hw_ctx[q->mq_map[cpu]]; | |
1072 | } | |
1073 | EXPORT_SYMBOL(blk_mq_map_queue); | |
1074 | ||
24d2f903 | 1075 | struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *set, |
320ae51f JA |
1076 | unsigned int hctx_index) |
1077 | { | |
1078 | return kmalloc_node(sizeof(struct blk_mq_hw_ctx), | |
24d2f903 | 1079 | GFP_KERNEL | __GFP_ZERO, set->numa_node); |
320ae51f JA |
1080 | } |
1081 | EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue); | |
1082 | ||
1083 | void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx, | |
1084 | unsigned int hctx_index) | |
1085 | { | |
1086 | kfree(hctx); | |
1087 | } | |
1088 | EXPORT_SYMBOL(blk_mq_free_single_hw_queue); | |
1089 | ||
1090 | static void blk_mq_hctx_notify(void *data, unsigned long action, | |
1091 | unsigned int cpu) | |
1092 | { | |
1093 | struct blk_mq_hw_ctx *hctx = data; | |
bccb5f7c | 1094 | struct request_queue *q = hctx->queue; |
320ae51f JA |
1095 | struct blk_mq_ctx *ctx; |
1096 | LIST_HEAD(tmp); | |
1097 | ||
1098 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) | |
1099 | return; | |
1100 | ||
1101 | /* | |
1102 | * Move ctx entries to new CPU, if this one is going away. | |
1103 | */ | |
bccb5f7c | 1104 | ctx = __blk_mq_get_ctx(q, cpu); |
320ae51f JA |
1105 | |
1106 | spin_lock(&ctx->lock); | |
1107 | if (!list_empty(&ctx->rq_list)) { | |
1108 | list_splice_init(&ctx->rq_list, &tmp); | |
1109 | clear_bit(ctx->index_hw, hctx->ctx_map); | |
1110 | } | |
1111 | spin_unlock(&ctx->lock); | |
1112 | ||
1113 | if (list_empty(&tmp)) | |
1114 | return; | |
1115 | ||
bccb5f7c | 1116 | ctx = blk_mq_get_ctx(q); |
320ae51f JA |
1117 | spin_lock(&ctx->lock); |
1118 | ||
1119 | while (!list_empty(&tmp)) { | |
1120 | struct request *rq; | |
1121 | ||
1122 | rq = list_first_entry(&tmp, struct request, queuelist); | |
1123 | rq->mq_ctx = ctx; | |
1124 | list_move_tail(&rq->queuelist, &ctx->rq_list); | |
1125 | } | |
1126 | ||
bccb5f7c | 1127 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
320ae51f JA |
1128 | blk_mq_hctx_mark_pending(hctx, ctx); |
1129 | ||
1130 | spin_unlock(&ctx->lock); | |
bccb5f7c JA |
1131 | |
1132 | blk_mq_run_hw_queue(hctx, true); | |
e4043dcf | 1133 | blk_mq_put_ctx(ctx); |
320ae51f JA |
1134 | } |
1135 | ||
24d2f903 CH |
1136 | static void blk_mq_free_rq_map(struct blk_mq_tag_set *set, |
1137 | struct blk_mq_tags *tags, unsigned int hctx_idx) | |
95363efd | 1138 | { |
e9b267d9 | 1139 | struct page *page; |
320ae51f | 1140 | |
24d2f903 | 1141 | if (tags->rqs && set->ops->exit_request) { |
e9b267d9 | 1142 | int i; |
320ae51f | 1143 | |
24d2f903 CH |
1144 | for (i = 0; i < tags->nr_tags; i++) { |
1145 | if (!tags->rqs[i]) | |
e9b267d9 | 1146 | continue; |
24d2f903 CH |
1147 | set->ops->exit_request(set->driver_data, tags->rqs[i], |
1148 | hctx_idx, i); | |
e9b267d9 | 1149 | } |
320ae51f | 1150 | } |
320ae51f | 1151 | |
24d2f903 CH |
1152 | while (!list_empty(&tags->page_list)) { |
1153 | page = list_first_entry(&tags->page_list, struct page, lru); | |
6753471c | 1154 | list_del_init(&page->lru); |
320ae51f JA |
1155 | __free_pages(page, page->private); |
1156 | } | |
1157 | ||
24d2f903 | 1158 | kfree(tags->rqs); |
320ae51f | 1159 | |
24d2f903 | 1160 | blk_mq_free_tags(tags); |
320ae51f JA |
1161 | } |
1162 | ||
1163 | static size_t order_to_size(unsigned int order) | |
1164 | { | |
4ca08500 | 1165 | return (size_t)PAGE_SIZE << order; |
320ae51f JA |
1166 | } |
1167 | ||
24d2f903 CH |
1168 | static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set, |
1169 | unsigned int hctx_idx) | |
320ae51f | 1170 | { |
24d2f903 | 1171 | struct blk_mq_tags *tags; |
320ae51f JA |
1172 | unsigned int i, j, entries_per_page, max_order = 4; |
1173 | size_t rq_size, left; | |
1174 | ||
24d2f903 CH |
1175 | tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags, |
1176 | set->numa_node); | |
1177 | if (!tags) | |
1178 | return NULL; | |
320ae51f | 1179 | |
24d2f903 CH |
1180 | INIT_LIST_HEAD(&tags->page_list); |
1181 | ||
1182 | tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *), | |
1183 | GFP_KERNEL, set->numa_node); | |
1184 | if (!tags->rqs) { | |
1185 | blk_mq_free_tags(tags); | |
1186 | return NULL; | |
1187 | } | |
320ae51f JA |
1188 | |
1189 | /* | |
1190 | * rq_size is the size of the request plus driver payload, rounded | |
1191 | * to the cacheline size | |
1192 | */ | |
24d2f903 | 1193 | rq_size = round_up(sizeof(struct request) + set->cmd_size, |
320ae51f | 1194 | cache_line_size()); |
24d2f903 | 1195 | left = rq_size * set->queue_depth; |
320ae51f | 1196 | |
24d2f903 | 1197 | for (i = 0; i < set->queue_depth; ) { |
320ae51f JA |
1198 | int this_order = max_order; |
1199 | struct page *page; | |
1200 | int to_do; | |
1201 | void *p; | |
1202 | ||
1203 | while (left < order_to_size(this_order - 1) && this_order) | |
1204 | this_order--; | |
1205 | ||
1206 | do { | |
24d2f903 CH |
1207 | page = alloc_pages_node(set->numa_node, GFP_KERNEL, |
1208 | this_order); | |
320ae51f JA |
1209 | if (page) |
1210 | break; | |
1211 | if (!this_order--) | |
1212 | break; | |
1213 | if (order_to_size(this_order) < rq_size) | |
1214 | break; | |
1215 | } while (1); | |
1216 | ||
1217 | if (!page) | |
24d2f903 | 1218 | goto fail; |
320ae51f JA |
1219 | |
1220 | page->private = this_order; | |
24d2f903 | 1221 | list_add_tail(&page->lru, &tags->page_list); |
320ae51f JA |
1222 | |
1223 | p = page_address(page); | |
1224 | entries_per_page = order_to_size(this_order) / rq_size; | |
24d2f903 | 1225 | to_do = min(entries_per_page, set->queue_depth - i); |
320ae51f JA |
1226 | left -= to_do * rq_size; |
1227 | for (j = 0; j < to_do; j++) { | |
24d2f903 CH |
1228 | tags->rqs[i] = p; |
1229 | if (set->ops->init_request) { | |
1230 | if (set->ops->init_request(set->driver_data, | |
1231 | tags->rqs[i], hctx_idx, i, | |
1232 | set->numa_node)) | |
1233 | goto fail; | |
e9b267d9 CH |
1234 | } |
1235 | ||
320ae51f JA |
1236 | p += rq_size; |
1237 | i++; | |
1238 | } | |
1239 | } | |
1240 | ||
24d2f903 | 1241 | return tags; |
320ae51f | 1242 | |
24d2f903 CH |
1243 | fail: |
1244 | pr_warn("%s: failed to allocate requests\n", __func__); | |
1245 | blk_mq_free_rq_map(set, tags, hctx_idx); | |
1246 | return NULL; | |
320ae51f JA |
1247 | } |
1248 | ||
1249 | static int blk_mq_init_hw_queues(struct request_queue *q, | |
24d2f903 | 1250 | struct blk_mq_tag_set *set) |
320ae51f JA |
1251 | { |
1252 | struct blk_mq_hw_ctx *hctx; | |
1253 | unsigned int i, j; | |
1254 | ||
1255 | /* | |
1256 | * Initialize hardware queues | |
1257 | */ | |
1258 | queue_for_each_hw_ctx(q, hctx, i) { | |
1259 | unsigned int num_maps; | |
1260 | int node; | |
1261 | ||
1262 | node = hctx->numa_node; | |
1263 | if (node == NUMA_NO_NODE) | |
24d2f903 | 1264 | node = hctx->numa_node = set->numa_node; |
320ae51f | 1265 | |
70f4db63 CH |
1266 | INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn); |
1267 | INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn); | |
320ae51f JA |
1268 | spin_lock_init(&hctx->lock); |
1269 | INIT_LIST_HEAD(&hctx->dispatch); | |
1270 | hctx->queue = q; | |
1271 | hctx->queue_num = i; | |
24d2f903 CH |
1272 | hctx->flags = set->flags; |
1273 | hctx->cmd_size = set->cmd_size; | |
320ae51f JA |
1274 | |
1275 | blk_mq_init_cpu_notifier(&hctx->cpu_notifier, | |
1276 | blk_mq_hctx_notify, hctx); | |
1277 | blk_mq_register_cpu_notifier(&hctx->cpu_notifier); | |
1278 | ||
24d2f903 | 1279 | hctx->tags = set->tags[i]; |
320ae51f JA |
1280 | |
1281 | /* | |
1282 | * Allocate space for all possible cpus to avoid allocation in | |
1283 | * runtime | |
1284 | */ | |
1285 | hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *), | |
1286 | GFP_KERNEL, node); | |
1287 | if (!hctx->ctxs) | |
1288 | break; | |
1289 | ||
1290 | num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG; | |
1291 | hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long), | |
1292 | GFP_KERNEL, node); | |
1293 | if (!hctx->ctx_map) | |
1294 | break; | |
1295 | ||
1296 | hctx->nr_ctx_map = num_maps; | |
1297 | hctx->nr_ctx = 0; | |
1298 | ||
24d2f903 CH |
1299 | if (set->ops->init_hctx && |
1300 | set->ops->init_hctx(hctx, set->driver_data, i)) | |
320ae51f JA |
1301 | break; |
1302 | } | |
1303 | ||
1304 | if (i == q->nr_hw_queues) | |
1305 | return 0; | |
1306 | ||
1307 | /* | |
1308 | * Init failed | |
1309 | */ | |
1310 | queue_for_each_hw_ctx(q, hctx, j) { | |
1311 | if (i == j) | |
1312 | break; | |
1313 | ||
24d2f903 CH |
1314 | if (set->ops->exit_hctx) |
1315 | set->ops->exit_hctx(hctx, j); | |
320ae51f JA |
1316 | |
1317 | blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier); | |
320ae51f | 1318 | kfree(hctx->ctxs); |
11471e0d | 1319 | kfree(hctx->ctx_map); |
320ae51f JA |
1320 | } |
1321 | ||
1322 | return 1; | |
1323 | } | |
1324 | ||
1325 | static void blk_mq_init_cpu_queues(struct request_queue *q, | |
1326 | unsigned int nr_hw_queues) | |
1327 | { | |
1328 | unsigned int i; | |
1329 | ||
1330 | for_each_possible_cpu(i) { | |
1331 | struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i); | |
1332 | struct blk_mq_hw_ctx *hctx; | |
1333 | ||
1334 | memset(__ctx, 0, sizeof(*__ctx)); | |
1335 | __ctx->cpu = i; | |
1336 | spin_lock_init(&__ctx->lock); | |
1337 | INIT_LIST_HEAD(&__ctx->rq_list); | |
1338 | __ctx->queue = q; | |
1339 | ||
1340 | /* If the cpu isn't online, the cpu is mapped to first hctx */ | |
320ae51f JA |
1341 | if (!cpu_online(i)) |
1342 | continue; | |
1343 | ||
e4043dcf JA |
1344 | hctx = q->mq_ops->map_queue(q, i); |
1345 | cpumask_set_cpu(i, hctx->cpumask); | |
1346 | hctx->nr_ctx++; | |
1347 | ||
320ae51f JA |
1348 | /* |
1349 | * Set local node, IFF we have more than one hw queue. If | |
1350 | * not, we remain on the home node of the device | |
1351 | */ | |
1352 | if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE) | |
1353 | hctx->numa_node = cpu_to_node(i); | |
1354 | } | |
1355 | } | |
1356 | ||
1357 | static void blk_mq_map_swqueue(struct request_queue *q) | |
1358 | { | |
1359 | unsigned int i; | |
1360 | struct blk_mq_hw_ctx *hctx; | |
1361 | struct blk_mq_ctx *ctx; | |
1362 | ||
1363 | queue_for_each_hw_ctx(q, hctx, i) { | |
e4043dcf | 1364 | cpumask_clear(hctx->cpumask); |
320ae51f JA |
1365 | hctx->nr_ctx = 0; |
1366 | } | |
1367 | ||
1368 | /* | |
1369 | * Map software to hardware queues | |
1370 | */ | |
1371 | queue_for_each_ctx(q, ctx, i) { | |
1372 | /* If the cpu isn't online, the cpu is mapped to first hctx */ | |
e4043dcf JA |
1373 | if (!cpu_online(i)) |
1374 | continue; | |
1375 | ||
320ae51f | 1376 | hctx = q->mq_ops->map_queue(q, i); |
e4043dcf | 1377 | cpumask_set_cpu(i, hctx->cpumask); |
320ae51f JA |
1378 | ctx->index_hw = hctx->nr_ctx; |
1379 | hctx->ctxs[hctx->nr_ctx++] = ctx; | |
1380 | } | |
1381 | } | |
1382 | ||
24d2f903 | 1383 | struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set) |
320ae51f JA |
1384 | { |
1385 | struct blk_mq_hw_ctx **hctxs; | |
1386 | struct blk_mq_ctx *ctx; | |
1387 | struct request_queue *q; | |
1388 | int i; | |
1389 | ||
320ae51f JA |
1390 | ctx = alloc_percpu(struct blk_mq_ctx); |
1391 | if (!ctx) | |
1392 | return ERR_PTR(-ENOMEM); | |
1393 | ||
24d2f903 CH |
1394 | hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL, |
1395 | set->numa_node); | |
320ae51f JA |
1396 | |
1397 | if (!hctxs) | |
1398 | goto err_percpu; | |
1399 | ||
24d2f903 CH |
1400 | for (i = 0; i < set->nr_hw_queues; i++) { |
1401 | hctxs[i] = set->ops->alloc_hctx(set, i); | |
320ae51f JA |
1402 | if (!hctxs[i]) |
1403 | goto err_hctxs; | |
1404 | ||
e4043dcf JA |
1405 | if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL)) |
1406 | goto err_hctxs; | |
1407 | ||
320ae51f JA |
1408 | hctxs[i]->numa_node = NUMA_NO_NODE; |
1409 | hctxs[i]->queue_num = i; | |
1410 | } | |
1411 | ||
24d2f903 | 1412 | q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node); |
320ae51f JA |
1413 | if (!q) |
1414 | goto err_hctxs; | |
1415 | ||
24d2f903 | 1416 | q->mq_map = blk_mq_make_queue_map(set); |
320ae51f JA |
1417 | if (!q->mq_map) |
1418 | goto err_map; | |
1419 | ||
1420 | setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q); | |
1421 | blk_queue_rq_timeout(q, 30000); | |
1422 | ||
1423 | q->nr_queues = nr_cpu_ids; | |
24d2f903 | 1424 | q->nr_hw_queues = set->nr_hw_queues; |
320ae51f JA |
1425 | |
1426 | q->queue_ctx = ctx; | |
1427 | q->queue_hw_ctx = hctxs; | |
1428 | ||
24d2f903 | 1429 | q->mq_ops = set->ops; |
94eddfbe | 1430 | q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT; |
320ae51f | 1431 | |
1be036e9 CH |
1432 | q->sg_reserved_size = INT_MAX; |
1433 | ||
320ae51f | 1434 | blk_queue_make_request(q, blk_mq_make_request); |
87ee7b11 | 1435 | blk_queue_rq_timed_out(q, blk_mq_rq_timed_out); |
24d2f903 CH |
1436 | if (set->timeout) |
1437 | blk_queue_rq_timeout(q, set->timeout); | |
320ae51f | 1438 | |
24d2f903 CH |
1439 | if (set->ops->complete) |
1440 | blk_queue_softirq_done(q, set->ops->complete); | |
30a91cb4 | 1441 | |
320ae51f | 1442 | blk_mq_init_flush(q); |
24d2f903 | 1443 | blk_mq_init_cpu_queues(q, set->nr_hw_queues); |
320ae51f | 1444 | |
24d2f903 CH |
1445 | q->flush_rq = kzalloc(round_up(sizeof(struct request) + |
1446 | set->cmd_size, cache_line_size()), | |
1447 | GFP_KERNEL); | |
18741986 | 1448 | if (!q->flush_rq) |
320ae51f JA |
1449 | goto err_hw; |
1450 | ||
24d2f903 | 1451 | if (blk_mq_init_hw_queues(q, set)) |
18741986 CH |
1452 | goto err_flush_rq; |
1453 | ||
320ae51f JA |
1454 | blk_mq_map_swqueue(q); |
1455 | ||
1456 | mutex_lock(&all_q_mutex); | |
1457 | list_add_tail(&q->all_q_node, &all_q_list); | |
1458 | mutex_unlock(&all_q_mutex); | |
1459 | ||
1460 | return q; | |
18741986 CH |
1461 | |
1462 | err_flush_rq: | |
1463 | kfree(q->flush_rq); | |
320ae51f JA |
1464 | err_hw: |
1465 | kfree(q->mq_map); | |
1466 | err_map: | |
1467 | blk_cleanup_queue(q); | |
1468 | err_hctxs: | |
24d2f903 | 1469 | for (i = 0; i < set->nr_hw_queues; i++) { |
320ae51f JA |
1470 | if (!hctxs[i]) |
1471 | break; | |
e4043dcf | 1472 | free_cpumask_var(hctxs[i]->cpumask); |
24d2f903 | 1473 | set->ops->free_hctx(hctxs[i], i); |
320ae51f JA |
1474 | } |
1475 | kfree(hctxs); | |
1476 | err_percpu: | |
1477 | free_percpu(ctx); | |
1478 | return ERR_PTR(-ENOMEM); | |
1479 | } | |
1480 | EXPORT_SYMBOL(blk_mq_init_queue); | |
1481 | ||
1482 | void blk_mq_free_queue(struct request_queue *q) | |
1483 | { | |
1484 | struct blk_mq_hw_ctx *hctx; | |
1485 | int i; | |
1486 | ||
1487 | queue_for_each_hw_ctx(q, hctx, i) { | |
320ae51f JA |
1488 | kfree(hctx->ctx_map); |
1489 | kfree(hctx->ctxs); | |
320ae51f JA |
1490 | blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier); |
1491 | if (q->mq_ops->exit_hctx) | |
1492 | q->mq_ops->exit_hctx(hctx, i); | |
e4043dcf | 1493 | free_cpumask_var(hctx->cpumask); |
320ae51f JA |
1494 | q->mq_ops->free_hctx(hctx, i); |
1495 | } | |
1496 | ||
1497 | free_percpu(q->queue_ctx); | |
1498 | kfree(q->queue_hw_ctx); | |
1499 | kfree(q->mq_map); | |
1500 | ||
1501 | q->queue_ctx = NULL; | |
1502 | q->queue_hw_ctx = NULL; | |
1503 | q->mq_map = NULL; | |
1504 | ||
1505 | mutex_lock(&all_q_mutex); | |
1506 | list_del_init(&q->all_q_node); | |
1507 | mutex_unlock(&all_q_mutex); | |
1508 | } | |
320ae51f JA |
1509 | |
1510 | /* Basically redo blk_mq_init_queue with queue frozen */ | |
f618ef7c | 1511 | static void blk_mq_queue_reinit(struct request_queue *q) |
320ae51f JA |
1512 | { |
1513 | blk_mq_freeze_queue(q); | |
1514 | ||
1515 | blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues); | |
1516 | ||
1517 | /* | |
1518 | * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe | |
1519 | * we should change hctx numa_node according to new topology (this | |
1520 | * involves free and re-allocate memory, worthy doing?) | |
1521 | */ | |
1522 | ||
1523 | blk_mq_map_swqueue(q); | |
1524 | ||
1525 | blk_mq_unfreeze_queue(q); | |
1526 | } | |
1527 | ||
f618ef7c PG |
1528 | static int blk_mq_queue_reinit_notify(struct notifier_block *nb, |
1529 | unsigned long action, void *hcpu) | |
320ae51f JA |
1530 | { |
1531 | struct request_queue *q; | |
1532 | ||
1533 | /* | |
1534 | * Before new mapping is established, hotadded cpu might already start | |
1535 | * handling requests. This doesn't break anything as we map offline | |
1536 | * CPUs to first hardware queue. We will re-init queue below to get | |
1537 | * optimal settings. | |
1538 | */ | |
1539 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN && | |
1540 | action != CPU_ONLINE && action != CPU_ONLINE_FROZEN) | |
1541 | return NOTIFY_OK; | |
1542 | ||
1543 | mutex_lock(&all_q_mutex); | |
1544 | list_for_each_entry(q, &all_q_list, all_q_node) | |
1545 | blk_mq_queue_reinit(q); | |
1546 | mutex_unlock(&all_q_mutex); | |
1547 | return NOTIFY_OK; | |
1548 | } | |
1549 | ||
24d2f903 CH |
1550 | int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) |
1551 | { | |
1552 | int i; | |
1553 | ||
1554 | if (!set->nr_hw_queues) | |
1555 | return -EINVAL; | |
1556 | if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH) | |
1557 | return -EINVAL; | |
1558 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) | |
1559 | return -EINVAL; | |
1560 | ||
1561 | if (!set->nr_hw_queues || | |
1562 | !set->ops->queue_rq || !set->ops->map_queue || | |
1563 | !set->ops->alloc_hctx || !set->ops->free_hctx) | |
1564 | return -EINVAL; | |
1565 | ||
1566 | ||
48479005 ML |
1567 | set->tags = kmalloc_node(set->nr_hw_queues * |
1568 | sizeof(struct blk_mq_tags *), | |
24d2f903 CH |
1569 | GFP_KERNEL, set->numa_node); |
1570 | if (!set->tags) | |
1571 | goto out; | |
1572 | ||
1573 | for (i = 0; i < set->nr_hw_queues; i++) { | |
1574 | set->tags[i] = blk_mq_init_rq_map(set, i); | |
1575 | if (!set->tags[i]) | |
1576 | goto out_unwind; | |
1577 | } | |
1578 | ||
1579 | return 0; | |
1580 | ||
1581 | out_unwind: | |
1582 | while (--i >= 0) | |
1583 | blk_mq_free_rq_map(set, set->tags[i], i); | |
1584 | out: | |
1585 | return -ENOMEM; | |
1586 | } | |
1587 | EXPORT_SYMBOL(blk_mq_alloc_tag_set); | |
1588 | ||
1589 | void blk_mq_free_tag_set(struct blk_mq_tag_set *set) | |
1590 | { | |
1591 | int i; | |
1592 | ||
1593 | for (i = 0; i < set->nr_hw_queues; i++) | |
1594 | blk_mq_free_rq_map(set, set->tags[i], i); | |
981bd189 | 1595 | kfree(set->tags); |
24d2f903 CH |
1596 | } |
1597 | EXPORT_SYMBOL(blk_mq_free_tag_set); | |
1598 | ||
676141e4 JA |
1599 | void blk_mq_disable_hotplug(void) |
1600 | { | |
1601 | mutex_lock(&all_q_mutex); | |
1602 | } | |
1603 | ||
1604 | void blk_mq_enable_hotplug(void) | |
1605 | { | |
1606 | mutex_unlock(&all_q_mutex); | |
1607 | } | |
1608 | ||
320ae51f JA |
1609 | static int __init blk_mq_init(void) |
1610 | { | |
320ae51f JA |
1611 | blk_mq_cpu_init(); |
1612 | ||
1613 | /* Must be called after percpu_counter_hotcpu_callback() */ | |
1614 | hotcpu_notifier(blk_mq_queue_reinit_notify, -10); | |
1615 | ||
1616 | return 0; | |
1617 | } | |
1618 | subsys_initcall(blk_mq_init); |