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