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