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