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