]>
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]; |
ed44832d | 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 | |
320ae51f JA |
221 | blk_mq_wait_for_tags(hctx->tags); |
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 | ||
7237c740 | 297 | bool blk_mq_end_io_partial(struct request *rq, int error, unsigned int nr_bytes) |
320ae51f | 298 | { |
7237c740 CH |
299 | if (blk_update_request(rq, error, blk_rq_bytes(rq))) |
300 | return true; | |
320ae51f | 301 | |
0d11e6ac ML |
302 | blk_account_io_done(rq); |
303 | ||
320ae51f JA |
304 | if (rq->end_io) |
305 | rq->end_io(rq, error); | |
306 | else | |
307 | blk_mq_free_request(rq); | |
7237c740 | 308 | return false; |
320ae51f | 309 | } |
7237c740 | 310 | EXPORT_SYMBOL(blk_mq_end_io_partial); |
320ae51f | 311 | |
30a91cb4 | 312 | static void __blk_mq_complete_request_remote(void *data) |
320ae51f | 313 | { |
3d6efbf6 | 314 | struct request *rq = data; |
320ae51f | 315 | |
30a91cb4 | 316 | rq->q->softirq_done_fn(rq); |
320ae51f | 317 | } |
320ae51f | 318 | |
30a91cb4 | 319 | void __blk_mq_complete_request(struct request *rq) |
320ae51f JA |
320 | { |
321 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
322 | int cpu; | |
323 | ||
30a91cb4 CH |
324 | if (!ctx->ipi_redirect) { |
325 | rq->q->softirq_done_fn(rq); | |
326 | return; | |
327 | } | |
320ae51f JA |
328 | |
329 | cpu = get_cpu(); | |
3d6efbf6 | 330 | if (cpu != ctx->cpu && cpu_online(ctx->cpu)) { |
30a91cb4 | 331 | rq->csd.func = __blk_mq_complete_request_remote; |
3d6efbf6 CH |
332 | rq->csd.info = rq; |
333 | rq->csd.flags = 0; | |
c46fff2a | 334 | smp_call_function_single_async(ctx->cpu, &rq->csd); |
3d6efbf6 | 335 | } else { |
30a91cb4 | 336 | rq->q->softirq_done_fn(rq); |
3d6efbf6 | 337 | } |
320ae51f JA |
338 | put_cpu(); |
339 | } | |
30a91cb4 CH |
340 | |
341 | /** | |
342 | * blk_mq_complete_request - end I/O on a request | |
343 | * @rq: the request being processed | |
344 | * | |
345 | * Description: | |
346 | * Ends all I/O on a request. It does not handle partial completions. | |
347 | * The actual completion happens out-of-order, through a IPI handler. | |
348 | **/ | |
349 | void blk_mq_complete_request(struct request *rq) | |
350 | { | |
351 | if (unlikely(blk_should_fake_timeout(rq->q))) | |
352 | return; | |
353 | if (!blk_mark_rq_complete(rq)) | |
354 | __blk_mq_complete_request(rq); | |
355 | } | |
356 | EXPORT_SYMBOL(blk_mq_complete_request); | |
320ae51f | 357 | |
49f5baa5 | 358 | static void blk_mq_start_request(struct request *rq, bool last) |
320ae51f JA |
359 | { |
360 | struct request_queue *q = rq->q; | |
361 | ||
362 | trace_block_rq_issue(q, rq); | |
363 | ||
742ee69b CH |
364 | rq->resid_len = blk_rq_bytes(rq); |
365 | ||
320ae51f JA |
366 | /* |
367 | * Just mark start time and set the started bit. Due to memory | |
368 | * ordering, we know we'll see the correct deadline as long as | |
369 | * REQ_ATOMIC_STARTED is seen. | |
370 | */ | |
371 | rq->deadline = jiffies + q->rq_timeout; | |
372 | set_bit(REQ_ATOM_STARTED, &rq->atomic_flags); | |
49f5baa5 CH |
373 | |
374 | if (q->dma_drain_size && blk_rq_bytes(rq)) { | |
375 | /* | |
376 | * Make sure space for the drain appears. We know we can do | |
377 | * this because max_hw_segments has been adjusted to be one | |
378 | * fewer than the device can handle. | |
379 | */ | |
380 | rq->nr_phys_segments++; | |
381 | } | |
382 | ||
383 | /* | |
384 | * Flag the last request in the series so that drivers know when IO | |
385 | * should be kicked off, if they don't do it on a per-request basis. | |
386 | * | |
387 | * Note: the flag isn't the only condition drivers should do kick off. | |
388 | * If drive is busy, the last request might not have the bit set. | |
389 | */ | |
390 | if (last) | |
391 | rq->cmd_flags |= REQ_END; | |
320ae51f JA |
392 | } |
393 | ||
394 | static void blk_mq_requeue_request(struct request *rq) | |
395 | { | |
396 | struct request_queue *q = rq->q; | |
397 | ||
398 | trace_block_rq_requeue(q, rq); | |
399 | clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags); | |
49f5baa5 CH |
400 | |
401 | rq->cmd_flags &= ~REQ_END; | |
402 | ||
403 | if (q->dma_drain_size && blk_rq_bytes(rq)) | |
404 | rq->nr_phys_segments--; | |
320ae51f JA |
405 | } |
406 | ||
24d2f903 CH |
407 | struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag) |
408 | { | |
409 | return tags->rqs[tag]; | |
410 | } | |
411 | EXPORT_SYMBOL(blk_mq_tag_to_rq); | |
412 | ||
320ae51f JA |
413 | struct blk_mq_timeout_data { |
414 | struct blk_mq_hw_ctx *hctx; | |
415 | unsigned long *next; | |
416 | unsigned int *next_set; | |
417 | }; | |
418 | ||
419 | static void blk_mq_timeout_check(void *__data, unsigned long *free_tags) | |
420 | { | |
421 | struct blk_mq_timeout_data *data = __data; | |
422 | struct blk_mq_hw_ctx *hctx = data->hctx; | |
423 | unsigned int tag; | |
424 | ||
425 | /* It may not be in flight yet (this is where | |
426 | * the REQ_ATOMIC_STARTED flag comes in). The requests are | |
427 | * statically allocated, so we know it's always safe to access the | |
428 | * memory associated with a bit offset into ->rqs[]. | |
429 | */ | |
430 | tag = 0; | |
431 | do { | |
432 | struct request *rq; | |
433 | ||
24d2f903 CH |
434 | tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag); |
435 | if (tag >= hctx->tags->nr_tags) | |
320ae51f JA |
436 | break; |
437 | ||
24d2f903 CH |
438 | rq = blk_mq_tag_to_rq(hctx->tags, tag++); |
439 | if (rq->q != hctx->queue) | |
440 | continue; | |
320ae51f JA |
441 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
442 | continue; | |
443 | ||
444 | blk_rq_check_expired(rq, data->next, data->next_set); | |
445 | } while (1); | |
446 | } | |
447 | ||
448 | static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx, | |
449 | unsigned long *next, | |
450 | unsigned int *next_set) | |
451 | { | |
452 | struct blk_mq_timeout_data data = { | |
453 | .hctx = hctx, | |
454 | .next = next, | |
455 | .next_set = next_set, | |
456 | }; | |
457 | ||
458 | /* | |
459 | * Ask the tagging code to iterate busy requests, so we can | |
460 | * check them for timeout. | |
461 | */ | |
462 | blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data); | |
463 | } | |
464 | ||
465 | static void blk_mq_rq_timer(unsigned long data) | |
466 | { | |
467 | struct request_queue *q = (struct request_queue *) data; | |
468 | struct blk_mq_hw_ctx *hctx; | |
469 | unsigned long next = 0; | |
470 | int i, next_set = 0; | |
471 | ||
472 | queue_for_each_hw_ctx(q, hctx, i) | |
473 | blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set); | |
474 | ||
475 | if (next_set) | |
476 | mod_timer(&q->timeout, round_jiffies_up(next)); | |
477 | } | |
478 | ||
479 | /* | |
480 | * Reverse check our software queue for entries that we could potentially | |
481 | * merge with. Currently includes a hand-wavy stop count of 8, to not spend | |
482 | * too much time checking for merges. | |
483 | */ | |
484 | static bool blk_mq_attempt_merge(struct request_queue *q, | |
485 | struct blk_mq_ctx *ctx, struct bio *bio) | |
486 | { | |
487 | struct request *rq; | |
488 | int checked = 8; | |
489 | ||
490 | list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) { | |
491 | int el_ret; | |
492 | ||
493 | if (!checked--) | |
494 | break; | |
495 | ||
496 | if (!blk_rq_merge_ok(rq, bio)) | |
497 | continue; | |
498 | ||
499 | el_ret = blk_try_merge(rq, bio); | |
500 | if (el_ret == ELEVATOR_BACK_MERGE) { | |
501 | if (bio_attempt_back_merge(q, rq, bio)) { | |
502 | ctx->rq_merged++; | |
503 | return true; | |
504 | } | |
505 | break; | |
506 | } else if (el_ret == ELEVATOR_FRONT_MERGE) { | |
507 | if (bio_attempt_front_merge(q, rq, bio)) { | |
508 | ctx->rq_merged++; | |
509 | return true; | |
510 | } | |
511 | break; | |
512 | } | |
513 | } | |
514 | ||
515 | return false; | |
516 | } | |
517 | ||
518 | void blk_mq_add_timer(struct request *rq) | |
519 | { | |
520 | __blk_add_timer(rq, NULL); | |
521 | } | |
522 | ||
523 | /* | |
524 | * Run this hardware queue, pulling any software queues mapped to it in. | |
525 | * Note that this function currently has various problems around ordering | |
526 | * of IO. In particular, we'd like FIFO behaviour on handling existing | |
527 | * items on the hctx->dispatch list. Ignore that for now. | |
528 | */ | |
529 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) | |
530 | { | |
531 | struct request_queue *q = hctx->queue; | |
532 | struct blk_mq_ctx *ctx; | |
533 | struct request *rq; | |
534 | LIST_HEAD(rq_list); | |
535 | int bit, queued; | |
536 | ||
fd1270d5 | 537 | WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)); |
e4043dcf | 538 | |
5d12f905 | 539 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
320ae51f JA |
540 | return; |
541 | ||
542 | hctx->run++; | |
543 | ||
544 | /* | |
545 | * Touch any software queue that has pending entries. | |
546 | */ | |
547 | for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) { | |
548 | clear_bit(bit, hctx->ctx_map); | |
549 | ctx = hctx->ctxs[bit]; | |
550 | BUG_ON(bit != ctx->index_hw); | |
551 | ||
552 | spin_lock(&ctx->lock); | |
553 | list_splice_tail_init(&ctx->rq_list, &rq_list); | |
554 | spin_unlock(&ctx->lock); | |
555 | } | |
556 | ||
557 | /* | |
558 | * If we have previous entries on our dispatch list, grab them | |
559 | * and stuff them at the front for more fair dispatch. | |
560 | */ | |
561 | if (!list_empty_careful(&hctx->dispatch)) { | |
562 | spin_lock(&hctx->lock); | |
563 | if (!list_empty(&hctx->dispatch)) | |
564 | list_splice_init(&hctx->dispatch, &rq_list); | |
565 | spin_unlock(&hctx->lock); | |
566 | } | |
567 | ||
568 | /* | |
569 | * Delete and return all entries from our dispatch list | |
570 | */ | |
571 | queued = 0; | |
572 | ||
573 | /* | |
574 | * Now process all the entries, sending them to the driver. | |
575 | */ | |
576 | while (!list_empty(&rq_list)) { | |
577 | int ret; | |
578 | ||
579 | rq = list_first_entry(&rq_list, struct request, queuelist); | |
580 | list_del_init(&rq->queuelist); | |
320ae51f | 581 | |
49f5baa5 | 582 | blk_mq_start_request(rq, list_empty(&rq_list)); |
320ae51f JA |
583 | |
584 | ret = q->mq_ops->queue_rq(hctx, rq); | |
585 | switch (ret) { | |
586 | case BLK_MQ_RQ_QUEUE_OK: | |
587 | queued++; | |
588 | continue; | |
589 | case BLK_MQ_RQ_QUEUE_BUSY: | |
590 | /* | |
591 | * FIXME: we should have a mechanism to stop the queue | |
592 | * like blk_stop_queue, otherwise we will waste cpu | |
593 | * time | |
594 | */ | |
595 | list_add(&rq->queuelist, &rq_list); | |
596 | blk_mq_requeue_request(rq); | |
597 | break; | |
598 | default: | |
599 | pr_err("blk-mq: bad return on queue: %d\n", ret); | |
320ae51f | 600 | case BLK_MQ_RQ_QUEUE_ERROR: |
1e93b8c2 | 601 | rq->errors = -EIO; |
320ae51f JA |
602 | blk_mq_end_io(rq, rq->errors); |
603 | break; | |
604 | } | |
605 | ||
606 | if (ret == BLK_MQ_RQ_QUEUE_BUSY) | |
607 | break; | |
608 | } | |
609 | ||
610 | if (!queued) | |
611 | hctx->dispatched[0]++; | |
612 | else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1))) | |
613 | hctx->dispatched[ilog2(queued) + 1]++; | |
614 | ||
615 | /* | |
616 | * Any items that need requeuing? Stuff them into hctx->dispatch, | |
617 | * that is where we will continue on next queue run. | |
618 | */ | |
619 | if (!list_empty(&rq_list)) { | |
620 | spin_lock(&hctx->lock); | |
621 | list_splice(&rq_list, &hctx->dispatch); | |
622 | spin_unlock(&hctx->lock); | |
623 | } | |
624 | } | |
625 | ||
626 | void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) | |
627 | { | |
5d12f905 | 628 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
320ae51f JA |
629 | return; |
630 | ||
e4043dcf | 631 | if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask)) |
320ae51f | 632 | __blk_mq_run_hw_queue(hctx); |
e4043dcf | 633 | else if (hctx->queue->nr_hw_queues == 1) |
59c3d45e | 634 | kblockd_schedule_delayed_work(&hctx->delayed_work, 0); |
e4043dcf JA |
635 | else { |
636 | unsigned int cpu; | |
637 | ||
638 | /* | |
639 | * It'd be great if the workqueue API had a way to pass | |
640 | * in a mask and had some smarts for more clever placement | |
641 | * than the first CPU. Or we could round-robin here. For now, | |
642 | * just queue on the first CPU. | |
643 | */ | |
644 | cpu = cpumask_first(hctx->cpumask); | |
645 | kblockd_schedule_delayed_work_on(cpu, &hctx->delayed_work, 0); | |
646 | } | |
320ae51f JA |
647 | } |
648 | ||
649 | void blk_mq_run_queues(struct request_queue *q, bool async) | |
650 | { | |
651 | struct blk_mq_hw_ctx *hctx; | |
652 | int i; | |
653 | ||
654 | queue_for_each_hw_ctx(q, hctx, i) { | |
655 | if ((!blk_mq_hctx_has_pending(hctx) && | |
656 | list_empty_careful(&hctx->dispatch)) || | |
5d12f905 | 657 | test_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
320ae51f JA |
658 | continue; |
659 | ||
e4043dcf | 660 | preempt_disable(); |
320ae51f | 661 | blk_mq_run_hw_queue(hctx, async); |
e4043dcf | 662 | preempt_enable(); |
320ae51f JA |
663 | } |
664 | } | |
665 | EXPORT_SYMBOL(blk_mq_run_queues); | |
666 | ||
667 | void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx) | |
668 | { | |
669 | cancel_delayed_work(&hctx->delayed_work); | |
670 | set_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
671 | } | |
672 | EXPORT_SYMBOL(blk_mq_stop_hw_queue); | |
673 | ||
280d45f6 CH |
674 | void blk_mq_stop_hw_queues(struct request_queue *q) |
675 | { | |
676 | struct blk_mq_hw_ctx *hctx; | |
677 | int i; | |
678 | ||
679 | queue_for_each_hw_ctx(q, hctx, i) | |
680 | blk_mq_stop_hw_queue(hctx); | |
681 | } | |
682 | EXPORT_SYMBOL(blk_mq_stop_hw_queues); | |
683 | ||
320ae51f JA |
684 | void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx) |
685 | { | |
686 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
e4043dcf JA |
687 | |
688 | preempt_disable(); | |
320ae51f | 689 | __blk_mq_run_hw_queue(hctx); |
e4043dcf | 690 | preempt_enable(); |
320ae51f JA |
691 | } |
692 | EXPORT_SYMBOL(blk_mq_start_hw_queue); | |
693 | ||
694 | void blk_mq_start_stopped_hw_queues(struct request_queue *q) | |
695 | { | |
696 | struct blk_mq_hw_ctx *hctx; | |
697 | int i; | |
698 | ||
699 | queue_for_each_hw_ctx(q, hctx, i) { | |
700 | if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state)) | |
701 | continue; | |
702 | ||
703 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
e4043dcf | 704 | preempt_disable(); |
320ae51f | 705 | blk_mq_run_hw_queue(hctx, true); |
e4043dcf | 706 | preempt_enable(); |
320ae51f JA |
707 | } |
708 | } | |
709 | EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues); | |
710 | ||
711 | static void blk_mq_work_fn(struct work_struct *work) | |
712 | { | |
713 | struct blk_mq_hw_ctx *hctx; | |
714 | ||
715 | hctx = container_of(work, struct blk_mq_hw_ctx, delayed_work.work); | |
e4043dcf | 716 | |
320ae51f JA |
717 | __blk_mq_run_hw_queue(hctx); |
718 | } | |
719 | ||
720 | static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, | |
72a0a36e | 721 | struct request *rq, bool at_head) |
320ae51f JA |
722 | { |
723 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
724 | ||
01b983c9 JA |
725 | trace_block_rq_insert(hctx->queue, rq); |
726 | ||
72a0a36e CH |
727 | if (at_head) |
728 | list_add(&rq->queuelist, &ctx->rq_list); | |
729 | else | |
730 | list_add_tail(&rq->queuelist, &ctx->rq_list); | |
320ae51f JA |
731 | blk_mq_hctx_mark_pending(hctx, ctx); |
732 | ||
733 | /* | |
734 | * We do this early, to ensure we are on the right CPU. | |
735 | */ | |
736 | blk_mq_add_timer(rq); | |
737 | } | |
738 | ||
eeabc850 CH |
739 | void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue, |
740 | bool async) | |
320ae51f | 741 | { |
eeabc850 | 742 | struct request_queue *q = rq->q; |
320ae51f | 743 | struct blk_mq_hw_ctx *hctx; |
eeabc850 CH |
744 | struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx; |
745 | ||
746 | current_ctx = blk_mq_get_ctx(q); | |
747 | if (!cpu_online(ctx->cpu)) | |
748 | rq->mq_ctx = ctx = current_ctx; | |
320ae51f | 749 | |
320ae51f JA |
750 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
751 | ||
eeabc850 CH |
752 | if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) && |
753 | !(rq->cmd_flags & (REQ_FLUSH_SEQ))) { | |
320ae51f JA |
754 | blk_insert_flush(rq); |
755 | } else { | |
320ae51f | 756 | spin_lock(&ctx->lock); |
72a0a36e | 757 | __blk_mq_insert_request(hctx, rq, at_head); |
320ae51f | 758 | spin_unlock(&ctx->lock); |
320ae51f JA |
759 | } |
760 | ||
320ae51f JA |
761 | if (run_queue) |
762 | blk_mq_run_hw_queue(hctx, async); | |
e4043dcf JA |
763 | |
764 | blk_mq_put_ctx(current_ctx); | |
320ae51f JA |
765 | } |
766 | ||
767 | static void blk_mq_insert_requests(struct request_queue *q, | |
768 | struct blk_mq_ctx *ctx, | |
769 | struct list_head *list, | |
770 | int depth, | |
771 | bool from_schedule) | |
772 | ||
773 | { | |
774 | struct blk_mq_hw_ctx *hctx; | |
775 | struct blk_mq_ctx *current_ctx; | |
776 | ||
777 | trace_block_unplug(q, depth, !from_schedule); | |
778 | ||
779 | current_ctx = blk_mq_get_ctx(q); | |
780 | ||
781 | if (!cpu_online(ctx->cpu)) | |
782 | ctx = current_ctx; | |
783 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
784 | ||
785 | /* | |
786 | * preemption doesn't flush plug list, so it's possible ctx->cpu is | |
787 | * offline now | |
788 | */ | |
789 | spin_lock(&ctx->lock); | |
790 | while (!list_empty(list)) { | |
791 | struct request *rq; | |
792 | ||
793 | rq = list_first_entry(list, struct request, queuelist); | |
794 | list_del_init(&rq->queuelist); | |
795 | rq->mq_ctx = ctx; | |
72a0a36e | 796 | __blk_mq_insert_request(hctx, rq, false); |
320ae51f JA |
797 | } |
798 | spin_unlock(&ctx->lock); | |
799 | ||
320ae51f | 800 | blk_mq_run_hw_queue(hctx, from_schedule); |
e4043dcf | 801 | blk_mq_put_ctx(current_ctx); |
320ae51f JA |
802 | } |
803 | ||
804 | static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b) | |
805 | { | |
806 | struct request *rqa = container_of(a, struct request, queuelist); | |
807 | struct request *rqb = container_of(b, struct request, queuelist); | |
808 | ||
809 | return !(rqa->mq_ctx < rqb->mq_ctx || | |
810 | (rqa->mq_ctx == rqb->mq_ctx && | |
811 | blk_rq_pos(rqa) < blk_rq_pos(rqb))); | |
812 | } | |
813 | ||
814 | void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) | |
815 | { | |
816 | struct blk_mq_ctx *this_ctx; | |
817 | struct request_queue *this_q; | |
818 | struct request *rq; | |
819 | LIST_HEAD(list); | |
820 | LIST_HEAD(ctx_list); | |
821 | unsigned int depth; | |
822 | ||
823 | list_splice_init(&plug->mq_list, &list); | |
824 | ||
825 | list_sort(NULL, &list, plug_ctx_cmp); | |
826 | ||
827 | this_q = NULL; | |
828 | this_ctx = NULL; | |
829 | depth = 0; | |
830 | ||
831 | while (!list_empty(&list)) { | |
832 | rq = list_entry_rq(list.next); | |
833 | list_del_init(&rq->queuelist); | |
834 | BUG_ON(!rq->q); | |
835 | if (rq->mq_ctx != this_ctx) { | |
836 | if (this_ctx) { | |
837 | blk_mq_insert_requests(this_q, this_ctx, | |
838 | &ctx_list, depth, | |
839 | from_schedule); | |
840 | } | |
841 | ||
842 | this_ctx = rq->mq_ctx; | |
843 | this_q = rq->q; | |
844 | depth = 0; | |
845 | } | |
846 | ||
847 | depth++; | |
848 | list_add_tail(&rq->queuelist, &ctx_list); | |
849 | } | |
850 | ||
851 | /* | |
852 | * If 'this_ctx' is set, we know we have entries to complete | |
853 | * on 'ctx_list'. Do those. | |
854 | */ | |
855 | if (this_ctx) { | |
856 | blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth, | |
857 | from_schedule); | |
858 | } | |
859 | } | |
860 | ||
861 | static void blk_mq_bio_to_request(struct request *rq, struct bio *bio) | |
862 | { | |
863 | init_request_from_bio(rq, bio); | |
864 | blk_account_io_start(rq, 1); | |
865 | } | |
866 | ||
867 | static void blk_mq_make_request(struct request_queue *q, struct bio *bio) | |
868 | { | |
869 | struct blk_mq_hw_ctx *hctx; | |
870 | struct blk_mq_ctx *ctx; | |
871 | const int is_sync = rw_is_sync(bio->bi_rw); | |
872 | const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA); | |
873 | int rw = bio_data_dir(bio); | |
874 | struct request *rq; | |
875 | unsigned int use_plug, request_count = 0; | |
876 | ||
877 | /* | |
878 | * If we have multiple hardware queues, just go directly to | |
879 | * one of those for sync IO. | |
880 | */ | |
881 | use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync); | |
882 | ||
883 | blk_queue_bounce(q, &bio); | |
884 | ||
14ec77f3 NB |
885 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { |
886 | bio_endio(bio, -EIO); | |
887 | return; | |
888 | } | |
889 | ||
320ae51f JA |
890 | if (use_plug && blk_attempt_plug_merge(q, bio, &request_count)) |
891 | return; | |
892 | ||
893 | if (blk_mq_queue_enter(q)) { | |
894 | bio_endio(bio, -EIO); | |
895 | return; | |
896 | } | |
897 | ||
898 | ctx = blk_mq_get_ctx(q); | |
899 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
900 | ||
27fbf4e8 SL |
901 | if (is_sync) |
902 | rw |= REQ_SYNC; | |
320ae51f | 903 | trace_block_getrq(q, bio, rw); |
18741986 | 904 | rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false); |
320ae51f | 905 | if (likely(rq)) |
18741986 | 906 | blk_mq_rq_ctx_init(q, ctx, rq, rw); |
320ae51f JA |
907 | else { |
908 | blk_mq_put_ctx(ctx); | |
909 | trace_block_sleeprq(q, bio, rw); | |
18741986 CH |
910 | rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC, |
911 | false); | |
320ae51f JA |
912 | ctx = rq->mq_ctx; |
913 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
914 | } | |
915 | ||
916 | hctx->queued++; | |
917 | ||
918 | if (unlikely(is_flush_fua)) { | |
919 | blk_mq_bio_to_request(rq, bio); | |
320ae51f JA |
920 | blk_insert_flush(rq); |
921 | goto run_queue; | |
922 | } | |
923 | ||
924 | /* | |
925 | * A task plug currently exists. Since this is completely lockless, | |
926 | * utilize that to temporarily store requests until the task is | |
927 | * either done or scheduled away. | |
928 | */ | |
929 | if (use_plug) { | |
930 | struct blk_plug *plug = current->plug; | |
931 | ||
932 | if (plug) { | |
933 | blk_mq_bio_to_request(rq, bio); | |
92f399c7 | 934 | if (list_empty(&plug->mq_list)) |
320ae51f JA |
935 | trace_block_plug(q); |
936 | else if (request_count >= BLK_MAX_REQUEST_COUNT) { | |
937 | blk_flush_plug_list(plug, false); | |
938 | trace_block_plug(q); | |
939 | } | |
940 | list_add_tail(&rq->queuelist, &plug->mq_list); | |
941 | blk_mq_put_ctx(ctx); | |
942 | return; | |
943 | } | |
944 | } | |
945 | ||
946 | spin_lock(&ctx->lock); | |
947 | ||
948 | if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) && | |
949 | blk_mq_attempt_merge(q, ctx, bio)) | |
950 | __blk_mq_free_request(hctx, ctx, rq); | |
951 | else { | |
952 | blk_mq_bio_to_request(rq, bio); | |
72a0a36e | 953 | __blk_mq_insert_request(hctx, rq, false); |
320ae51f JA |
954 | } |
955 | ||
956 | spin_unlock(&ctx->lock); | |
320ae51f JA |
957 | |
958 | /* | |
959 | * For a SYNC request, send it to the hardware immediately. For an | |
960 | * ASYNC request, just ensure that we run it later on. The latter | |
961 | * allows for merging opportunities and more efficient dispatching. | |
962 | */ | |
963 | run_queue: | |
964 | blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua); | |
e4043dcf | 965 | blk_mq_put_ctx(ctx); |
320ae51f JA |
966 | } |
967 | ||
968 | /* | |
969 | * Default mapping to a software queue, since we use one per CPU. | |
970 | */ | |
971 | struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu) | |
972 | { | |
973 | return q->queue_hw_ctx[q->mq_map[cpu]]; | |
974 | } | |
975 | EXPORT_SYMBOL(blk_mq_map_queue); | |
976 | ||
24d2f903 | 977 | struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *set, |
320ae51f JA |
978 | unsigned int hctx_index) |
979 | { | |
980 | return kmalloc_node(sizeof(struct blk_mq_hw_ctx), | |
24d2f903 | 981 | GFP_KERNEL | __GFP_ZERO, set->numa_node); |
320ae51f JA |
982 | } |
983 | EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue); | |
984 | ||
985 | void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx, | |
986 | unsigned int hctx_index) | |
987 | { | |
988 | kfree(hctx); | |
989 | } | |
990 | EXPORT_SYMBOL(blk_mq_free_single_hw_queue); | |
991 | ||
992 | static void blk_mq_hctx_notify(void *data, unsigned long action, | |
993 | unsigned int cpu) | |
994 | { | |
995 | struct blk_mq_hw_ctx *hctx = data; | |
bccb5f7c | 996 | struct request_queue *q = hctx->queue; |
320ae51f JA |
997 | struct blk_mq_ctx *ctx; |
998 | LIST_HEAD(tmp); | |
999 | ||
1000 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) | |
1001 | return; | |
1002 | ||
1003 | /* | |
1004 | * Move ctx entries to new CPU, if this one is going away. | |
1005 | */ | |
bccb5f7c | 1006 | ctx = __blk_mq_get_ctx(q, cpu); |
320ae51f JA |
1007 | |
1008 | spin_lock(&ctx->lock); | |
1009 | if (!list_empty(&ctx->rq_list)) { | |
1010 | list_splice_init(&ctx->rq_list, &tmp); | |
1011 | clear_bit(ctx->index_hw, hctx->ctx_map); | |
1012 | } | |
1013 | spin_unlock(&ctx->lock); | |
1014 | ||
1015 | if (list_empty(&tmp)) | |
1016 | return; | |
1017 | ||
bccb5f7c | 1018 | ctx = blk_mq_get_ctx(q); |
320ae51f JA |
1019 | spin_lock(&ctx->lock); |
1020 | ||
1021 | while (!list_empty(&tmp)) { | |
1022 | struct request *rq; | |
1023 | ||
1024 | rq = list_first_entry(&tmp, struct request, queuelist); | |
1025 | rq->mq_ctx = ctx; | |
1026 | list_move_tail(&rq->queuelist, &ctx->rq_list); | |
1027 | } | |
1028 | ||
bccb5f7c | 1029 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
320ae51f JA |
1030 | blk_mq_hctx_mark_pending(hctx, ctx); |
1031 | ||
1032 | spin_unlock(&ctx->lock); | |
bccb5f7c JA |
1033 | |
1034 | blk_mq_run_hw_queue(hctx, true); | |
e4043dcf | 1035 | blk_mq_put_ctx(ctx); |
320ae51f JA |
1036 | } |
1037 | ||
24d2f903 CH |
1038 | static void blk_mq_free_rq_map(struct blk_mq_tag_set *set, |
1039 | struct blk_mq_tags *tags, unsigned int hctx_idx) | |
95363efd | 1040 | { |
e9b267d9 | 1041 | struct page *page; |
320ae51f | 1042 | |
24d2f903 | 1043 | if (tags->rqs && set->ops->exit_request) { |
e9b267d9 | 1044 | int i; |
320ae51f | 1045 | |
24d2f903 CH |
1046 | for (i = 0; i < tags->nr_tags; i++) { |
1047 | if (!tags->rqs[i]) | |
e9b267d9 | 1048 | continue; |
24d2f903 CH |
1049 | set->ops->exit_request(set->driver_data, tags->rqs[i], |
1050 | hctx_idx, i); | |
e9b267d9 | 1051 | } |
320ae51f | 1052 | } |
320ae51f | 1053 | |
24d2f903 CH |
1054 | while (!list_empty(&tags->page_list)) { |
1055 | page = list_first_entry(&tags->page_list, struct page, lru); | |
6753471c | 1056 | list_del_init(&page->lru); |
320ae51f JA |
1057 | __free_pages(page, page->private); |
1058 | } | |
1059 | ||
24d2f903 | 1060 | kfree(tags->rqs); |
320ae51f | 1061 | |
24d2f903 | 1062 | blk_mq_free_tags(tags); |
320ae51f JA |
1063 | } |
1064 | ||
1065 | static size_t order_to_size(unsigned int order) | |
1066 | { | |
1067 | size_t ret = PAGE_SIZE; | |
1068 | ||
1069 | while (order--) | |
1070 | ret *= 2; | |
1071 | ||
1072 | return ret; | |
1073 | } | |
1074 | ||
24d2f903 CH |
1075 | static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set, |
1076 | unsigned int hctx_idx) | |
320ae51f | 1077 | { |
24d2f903 | 1078 | struct blk_mq_tags *tags; |
320ae51f JA |
1079 | unsigned int i, j, entries_per_page, max_order = 4; |
1080 | size_t rq_size, left; | |
1081 | ||
24d2f903 CH |
1082 | tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags, |
1083 | set->numa_node); | |
1084 | if (!tags) | |
1085 | return NULL; | |
320ae51f | 1086 | |
24d2f903 CH |
1087 | INIT_LIST_HEAD(&tags->page_list); |
1088 | ||
1089 | tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *), | |
1090 | GFP_KERNEL, set->numa_node); | |
1091 | if (!tags->rqs) { | |
1092 | blk_mq_free_tags(tags); | |
1093 | return NULL; | |
1094 | } | |
320ae51f JA |
1095 | |
1096 | /* | |
1097 | * rq_size is the size of the request plus driver payload, rounded | |
1098 | * to the cacheline size | |
1099 | */ | |
24d2f903 | 1100 | rq_size = round_up(sizeof(struct request) + set->cmd_size, |
320ae51f | 1101 | cache_line_size()); |
24d2f903 | 1102 | left = rq_size * set->queue_depth; |
320ae51f | 1103 | |
24d2f903 | 1104 | for (i = 0; i < set->queue_depth; ) { |
320ae51f JA |
1105 | int this_order = max_order; |
1106 | struct page *page; | |
1107 | int to_do; | |
1108 | void *p; | |
1109 | ||
1110 | while (left < order_to_size(this_order - 1) && this_order) | |
1111 | this_order--; | |
1112 | ||
1113 | do { | |
24d2f903 CH |
1114 | page = alloc_pages_node(set->numa_node, GFP_KERNEL, |
1115 | this_order); | |
320ae51f JA |
1116 | if (page) |
1117 | break; | |
1118 | if (!this_order--) | |
1119 | break; | |
1120 | if (order_to_size(this_order) < rq_size) | |
1121 | break; | |
1122 | } while (1); | |
1123 | ||
1124 | if (!page) | |
24d2f903 | 1125 | goto fail; |
320ae51f JA |
1126 | |
1127 | page->private = this_order; | |
24d2f903 | 1128 | list_add_tail(&page->lru, &tags->page_list); |
320ae51f JA |
1129 | |
1130 | p = page_address(page); | |
1131 | entries_per_page = order_to_size(this_order) / rq_size; | |
24d2f903 | 1132 | to_do = min(entries_per_page, set->queue_depth - i); |
320ae51f JA |
1133 | left -= to_do * rq_size; |
1134 | for (j = 0; j < to_do; j++) { | |
24d2f903 CH |
1135 | tags->rqs[i] = p; |
1136 | if (set->ops->init_request) { | |
1137 | if (set->ops->init_request(set->driver_data, | |
1138 | tags->rqs[i], hctx_idx, i, | |
1139 | set->numa_node)) | |
1140 | goto fail; | |
e9b267d9 CH |
1141 | } |
1142 | ||
320ae51f JA |
1143 | p += rq_size; |
1144 | i++; | |
1145 | } | |
1146 | } | |
1147 | ||
24d2f903 | 1148 | return tags; |
320ae51f | 1149 | |
24d2f903 CH |
1150 | fail: |
1151 | pr_warn("%s: failed to allocate requests\n", __func__); | |
1152 | blk_mq_free_rq_map(set, tags, hctx_idx); | |
1153 | return NULL; | |
320ae51f JA |
1154 | } |
1155 | ||
1156 | static int blk_mq_init_hw_queues(struct request_queue *q, | |
24d2f903 | 1157 | struct blk_mq_tag_set *set) |
320ae51f JA |
1158 | { |
1159 | struct blk_mq_hw_ctx *hctx; | |
1160 | unsigned int i, j; | |
1161 | ||
1162 | /* | |
1163 | * Initialize hardware queues | |
1164 | */ | |
1165 | queue_for_each_hw_ctx(q, hctx, i) { | |
1166 | unsigned int num_maps; | |
1167 | int node; | |
1168 | ||
1169 | node = hctx->numa_node; | |
1170 | if (node == NUMA_NO_NODE) | |
24d2f903 | 1171 | node = hctx->numa_node = set->numa_node; |
320ae51f JA |
1172 | |
1173 | INIT_DELAYED_WORK(&hctx->delayed_work, blk_mq_work_fn); | |
1174 | spin_lock_init(&hctx->lock); | |
1175 | INIT_LIST_HEAD(&hctx->dispatch); | |
1176 | hctx->queue = q; | |
1177 | hctx->queue_num = i; | |
24d2f903 CH |
1178 | hctx->flags = set->flags; |
1179 | hctx->cmd_size = set->cmd_size; | |
320ae51f JA |
1180 | |
1181 | blk_mq_init_cpu_notifier(&hctx->cpu_notifier, | |
1182 | blk_mq_hctx_notify, hctx); | |
1183 | blk_mq_register_cpu_notifier(&hctx->cpu_notifier); | |
1184 | ||
24d2f903 | 1185 | hctx->tags = set->tags[i]; |
320ae51f JA |
1186 | |
1187 | /* | |
1188 | * Allocate space for all possible cpus to avoid allocation in | |
1189 | * runtime | |
1190 | */ | |
1191 | hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *), | |
1192 | GFP_KERNEL, node); | |
1193 | if (!hctx->ctxs) | |
1194 | break; | |
1195 | ||
1196 | num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG; | |
1197 | hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long), | |
1198 | GFP_KERNEL, node); | |
1199 | if (!hctx->ctx_map) | |
1200 | break; | |
1201 | ||
1202 | hctx->nr_ctx_map = num_maps; | |
1203 | hctx->nr_ctx = 0; | |
1204 | ||
24d2f903 CH |
1205 | if (set->ops->init_hctx && |
1206 | set->ops->init_hctx(hctx, set->driver_data, i)) | |
320ae51f JA |
1207 | break; |
1208 | } | |
1209 | ||
1210 | if (i == q->nr_hw_queues) | |
1211 | return 0; | |
1212 | ||
1213 | /* | |
1214 | * Init failed | |
1215 | */ | |
1216 | queue_for_each_hw_ctx(q, hctx, j) { | |
1217 | if (i == j) | |
1218 | break; | |
1219 | ||
24d2f903 CH |
1220 | if (set->ops->exit_hctx) |
1221 | set->ops->exit_hctx(hctx, j); | |
320ae51f JA |
1222 | |
1223 | blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier); | |
320ae51f JA |
1224 | kfree(hctx->ctxs); |
1225 | } | |
1226 | ||
1227 | return 1; | |
1228 | } | |
1229 | ||
1230 | static void blk_mq_init_cpu_queues(struct request_queue *q, | |
1231 | unsigned int nr_hw_queues) | |
1232 | { | |
1233 | unsigned int i; | |
1234 | ||
1235 | for_each_possible_cpu(i) { | |
1236 | struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i); | |
1237 | struct blk_mq_hw_ctx *hctx; | |
1238 | ||
1239 | memset(__ctx, 0, sizeof(*__ctx)); | |
1240 | __ctx->cpu = i; | |
1241 | spin_lock_init(&__ctx->lock); | |
1242 | INIT_LIST_HEAD(&__ctx->rq_list); | |
1243 | __ctx->queue = q; | |
1244 | ||
1245 | /* If the cpu isn't online, the cpu is mapped to first hctx */ | |
320ae51f JA |
1246 | if (!cpu_online(i)) |
1247 | continue; | |
1248 | ||
e4043dcf JA |
1249 | hctx = q->mq_ops->map_queue(q, i); |
1250 | cpumask_set_cpu(i, hctx->cpumask); | |
1251 | hctx->nr_ctx++; | |
1252 | ||
320ae51f JA |
1253 | /* |
1254 | * Set local node, IFF we have more than one hw queue. If | |
1255 | * not, we remain on the home node of the device | |
1256 | */ | |
1257 | if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE) | |
1258 | hctx->numa_node = cpu_to_node(i); | |
1259 | } | |
1260 | } | |
1261 | ||
1262 | static void blk_mq_map_swqueue(struct request_queue *q) | |
1263 | { | |
1264 | unsigned int i; | |
1265 | struct blk_mq_hw_ctx *hctx; | |
1266 | struct blk_mq_ctx *ctx; | |
1267 | ||
1268 | queue_for_each_hw_ctx(q, hctx, i) { | |
e4043dcf | 1269 | cpumask_clear(hctx->cpumask); |
320ae51f JA |
1270 | hctx->nr_ctx = 0; |
1271 | } | |
1272 | ||
1273 | /* | |
1274 | * Map software to hardware queues | |
1275 | */ | |
1276 | queue_for_each_ctx(q, ctx, i) { | |
1277 | /* If the cpu isn't online, the cpu is mapped to first hctx */ | |
e4043dcf JA |
1278 | if (!cpu_online(i)) |
1279 | continue; | |
1280 | ||
320ae51f | 1281 | hctx = q->mq_ops->map_queue(q, i); |
e4043dcf | 1282 | cpumask_set_cpu(i, hctx->cpumask); |
320ae51f JA |
1283 | ctx->index_hw = hctx->nr_ctx; |
1284 | hctx->ctxs[hctx->nr_ctx++] = ctx; | |
1285 | } | |
1286 | } | |
1287 | ||
24d2f903 | 1288 | struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set) |
320ae51f JA |
1289 | { |
1290 | struct blk_mq_hw_ctx **hctxs; | |
1291 | struct blk_mq_ctx *ctx; | |
1292 | struct request_queue *q; | |
1293 | int i; | |
1294 | ||
320ae51f JA |
1295 | ctx = alloc_percpu(struct blk_mq_ctx); |
1296 | if (!ctx) | |
1297 | return ERR_PTR(-ENOMEM); | |
1298 | ||
24d2f903 CH |
1299 | hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL, |
1300 | set->numa_node); | |
320ae51f JA |
1301 | |
1302 | if (!hctxs) | |
1303 | goto err_percpu; | |
1304 | ||
24d2f903 CH |
1305 | for (i = 0; i < set->nr_hw_queues; i++) { |
1306 | hctxs[i] = set->ops->alloc_hctx(set, i); | |
320ae51f JA |
1307 | if (!hctxs[i]) |
1308 | goto err_hctxs; | |
1309 | ||
e4043dcf JA |
1310 | if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL)) |
1311 | goto err_hctxs; | |
1312 | ||
320ae51f JA |
1313 | hctxs[i]->numa_node = NUMA_NO_NODE; |
1314 | hctxs[i]->queue_num = i; | |
1315 | } | |
1316 | ||
24d2f903 | 1317 | q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node); |
320ae51f JA |
1318 | if (!q) |
1319 | goto err_hctxs; | |
1320 | ||
24d2f903 | 1321 | q->mq_map = blk_mq_make_queue_map(set); |
320ae51f JA |
1322 | if (!q->mq_map) |
1323 | goto err_map; | |
1324 | ||
1325 | setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q); | |
1326 | blk_queue_rq_timeout(q, 30000); | |
1327 | ||
1328 | q->nr_queues = nr_cpu_ids; | |
24d2f903 | 1329 | q->nr_hw_queues = set->nr_hw_queues; |
320ae51f JA |
1330 | |
1331 | q->queue_ctx = ctx; | |
1332 | q->queue_hw_ctx = hctxs; | |
1333 | ||
24d2f903 | 1334 | q->mq_ops = set->ops; |
94eddfbe | 1335 | q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT; |
320ae51f | 1336 | |
1be036e9 CH |
1337 | q->sg_reserved_size = INT_MAX; |
1338 | ||
320ae51f | 1339 | blk_queue_make_request(q, blk_mq_make_request); |
24d2f903 CH |
1340 | blk_queue_rq_timed_out(q, set->ops->timeout); |
1341 | if (set->timeout) | |
1342 | blk_queue_rq_timeout(q, set->timeout); | |
320ae51f | 1343 | |
24d2f903 CH |
1344 | if (set->ops->complete) |
1345 | blk_queue_softirq_done(q, set->ops->complete); | |
30a91cb4 | 1346 | |
320ae51f | 1347 | blk_mq_init_flush(q); |
24d2f903 | 1348 | blk_mq_init_cpu_queues(q, set->nr_hw_queues); |
320ae51f | 1349 | |
24d2f903 CH |
1350 | q->flush_rq = kzalloc(round_up(sizeof(struct request) + |
1351 | set->cmd_size, cache_line_size()), | |
1352 | GFP_KERNEL); | |
18741986 | 1353 | if (!q->flush_rq) |
320ae51f JA |
1354 | goto err_hw; |
1355 | ||
24d2f903 | 1356 | if (blk_mq_init_hw_queues(q, set)) |
18741986 CH |
1357 | goto err_flush_rq; |
1358 | ||
320ae51f JA |
1359 | blk_mq_map_swqueue(q); |
1360 | ||
1361 | mutex_lock(&all_q_mutex); | |
1362 | list_add_tail(&q->all_q_node, &all_q_list); | |
1363 | mutex_unlock(&all_q_mutex); | |
1364 | ||
1365 | return q; | |
18741986 CH |
1366 | |
1367 | err_flush_rq: | |
1368 | kfree(q->flush_rq); | |
320ae51f JA |
1369 | err_hw: |
1370 | kfree(q->mq_map); | |
1371 | err_map: | |
1372 | blk_cleanup_queue(q); | |
1373 | err_hctxs: | |
24d2f903 | 1374 | for (i = 0; i < set->nr_hw_queues; i++) { |
320ae51f JA |
1375 | if (!hctxs[i]) |
1376 | break; | |
e4043dcf | 1377 | free_cpumask_var(hctxs[i]->cpumask); |
24d2f903 | 1378 | set->ops->free_hctx(hctxs[i], i); |
320ae51f JA |
1379 | } |
1380 | kfree(hctxs); | |
1381 | err_percpu: | |
1382 | free_percpu(ctx); | |
1383 | return ERR_PTR(-ENOMEM); | |
1384 | } | |
1385 | EXPORT_SYMBOL(blk_mq_init_queue); | |
1386 | ||
1387 | void blk_mq_free_queue(struct request_queue *q) | |
1388 | { | |
1389 | struct blk_mq_hw_ctx *hctx; | |
1390 | int i; | |
1391 | ||
1392 | queue_for_each_hw_ctx(q, hctx, i) { | |
320ae51f JA |
1393 | kfree(hctx->ctx_map); |
1394 | kfree(hctx->ctxs); | |
320ae51f JA |
1395 | blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier); |
1396 | if (q->mq_ops->exit_hctx) | |
1397 | q->mq_ops->exit_hctx(hctx, i); | |
e4043dcf | 1398 | free_cpumask_var(hctx->cpumask); |
320ae51f JA |
1399 | q->mq_ops->free_hctx(hctx, i); |
1400 | } | |
1401 | ||
1402 | free_percpu(q->queue_ctx); | |
1403 | kfree(q->queue_hw_ctx); | |
1404 | kfree(q->mq_map); | |
1405 | ||
1406 | q->queue_ctx = NULL; | |
1407 | q->queue_hw_ctx = NULL; | |
1408 | q->mq_map = NULL; | |
1409 | ||
1410 | mutex_lock(&all_q_mutex); | |
1411 | list_del_init(&q->all_q_node); | |
1412 | mutex_unlock(&all_q_mutex); | |
1413 | } | |
320ae51f JA |
1414 | |
1415 | /* Basically redo blk_mq_init_queue with queue frozen */ | |
f618ef7c | 1416 | static void blk_mq_queue_reinit(struct request_queue *q) |
320ae51f JA |
1417 | { |
1418 | blk_mq_freeze_queue(q); | |
1419 | ||
1420 | blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues); | |
1421 | ||
1422 | /* | |
1423 | * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe | |
1424 | * we should change hctx numa_node according to new topology (this | |
1425 | * involves free and re-allocate memory, worthy doing?) | |
1426 | */ | |
1427 | ||
1428 | blk_mq_map_swqueue(q); | |
1429 | ||
1430 | blk_mq_unfreeze_queue(q); | |
1431 | } | |
1432 | ||
f618ef7c PG |
1433 | static int blk_mq_queue_reinit_notify(struct notifier_block *nb, |
1434 | unsigned long action, void *hcpu) | |
320ae51f JA |
1435 | { |
1436 | struct request_queue *q; | |
1437 | ||
1438 | /* | |
1439 | * Before new mapping is established, hotadded cpu might already start | |
1440 | * handling requests. This doesn't break anything as we map offline | |
1441 | * CPUs to first hardware queue. We will re-init queue below to get | |
1442 | * optimal settings. | |
1443 | */ | |
1444 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN && | |
1445 | action != CPU_ONLINE && action != CPU_ONLINE_FROZEN) | |
1446 | return NOTIFY_OK; | |
1447 | ||
1448 | mutex_lock(&all_q_mutex); | |
1449 | list_for_each_entry(q, &all_q_list, all_q_node) | |
1450 | blk_mq_queue_reinit(q); | |
1451 | mutex_unlock(&all_q_mutex); | |
1452 | return NOTIFY_OK; | |
1453 | } | |
1454 | ||
24d2f903 CH |
1455 | int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) |
1456 | { | |
1457 | int i; | |
1458 | ||
1459 | if (!set->nr_hw_queues) | |
1460 | return -EINVAL; | |
1461 | if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH) | |
1462 | return -EINVAL; | |
1463 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) | |
1464 | return -EINVAL; | |
1465 | ||
1466 | if (!set->nr_hw_queues || | |
1467 | !set->ops->queue_rq || !set->ops->map_queue || | |
1468 | !set->ops->alloc_hctx || !set->ops->free_hctx) | |
1469 | return -EINVAL; | |
1470 | ||
1471 | ||
1472 | set->tags = kmalloc_node(set->nr_hw_queues * sizeof(struct blk_mq_tags), | |
1473 | GFP_KERNEL, set->numa_node); | |
1474 | if (!set->tags) | |
1475 | goto out; | |
1476 | ||
1477 | for (i = 0; i < set->nr_hw_queues; i++) { | |
1478 | set->tags[i] = blk_mq_init_rq_map(set, i); | |
1479 | if (!set->tags[i]) | |
1480 | goto out_unwind; | |
1481 | } | |
1482 | ||
1483 | return 0; | |
1484 | ||
1485 | out_unwind: | |
1486 | while (--i >= 0) | |
1487 | blk_mq_free_rq_map(set, set->tags[i], i); | |
1488 | out: | |
1489 | return -ENOMEM; | |
1490 | } | |
1491 | EXPORT_SYMBOL(blk_mq_alloc_tag_set); | |
1492 | ||
1493 | void blk_mq_free_tag_set(struct blk_mq_tag_set *set) | |
1494 | { | |
1495 | int i; | |
1496 | ||
1497 | for (i = 0; i < set->nr_hw_queues; i++) | |
1498 | blk_mq_free_rq_map(set, set->tags[i], i); | |
1499 | } | |
1500 | EXPORT_SYMBOL(blk_mq_free_tag_set); | |
1501 | ||
676141e4 JA |
1502 | void blk_mq_disable_hotplug(void) |
1503 | { | |
1504 | mutex_lock(&all_q_mutex); | |
1505 | } | |
1506 | ||
1507 | void blk_mq_enable_hotplug(void) | |
1508 | { | |
1509 | mutex_unlock(&all_q_mutex); | |
1510 | } | |
1511 | ||
320ae51f JA |
1512 | static int __init blk_mq_init(void) |
1513 | { | |
320ae51f JA |
1514 | blk_mq_cpu_init(); |
1515 | ||
1516 | /* Must be called after percpu_counter_hotcpu_callback() */ | |
1517 | hotcpu_notifier(blk_mq_queue_reinit_notify, -10); | |
1518 | ||
1519 | return 0; | |
1520 | } | |
1521 | subsys_initcall(blk_mq_init); |