Commit | Line | Data |
---|---|---|
75bb4625 JA |
1 | /* |
2 | * Block multiqueue core code | |
3 | * | |
4 | * Copyright (C) 2013-2014 Jens Axboe | |
5 | * Copyright (C) 2013-2014 Christoph Hellwig | |
6 | */ | |
320ae51f JA |
7 | #include <linux/kernel.h> |
8 | #include <linux/module.h> | |
9 | #include <linux/backing-dev.h> | |
10 | #include <linux/bio.h> | |
11 | #include <linux/blkdev.h> | |
12 | #include <linux/mm.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/slab.h> | |
15 | #include <linux/workqueue.h> | |
16 | #include <linux/smp.h> | |
17 | #include <linux/llist.h> | |
18 | #include <linux/list_sort.h> | |
19 | #include <linux/cpu.h> | |
20 | #include <linux/cache.h> | |
21 | #include <linux/sched/sysctl.h> | |
22 | #include <linux/delay.h> | |
23 | ||
24 | #include <trace/events/block.h> | |
25 | ||
26 | #include <linux/blk-mq.h> | |
27 | #include "blk.h" | |
28 | #include "blk-mq.h" | |
29 | #include "blk-mq-tag.h" | |
30 | ||
31 | static DEFINE_MUTEX(all_q_mutex); | |
32 | static LIST_HEAD(all_q_list); | |
33 | ||
34 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx); | |
35 | ||
320ae51f JA |
36 | /* |
37 | * Check if any of the ctx's have pending work in this hardware queue | |
38 | */ | |
39 | static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx) | |
40 | { | |
41 | unsigned int i; | |
42 | ||
1429d7c9 JA |
43 | for (i = 0; i < hctx->ctx_map.map_size; i++) |
44 | if (hctx->ctx_map.map[i].word) | |
320ae51f JA |
45 | return true; |
46 | ||
47 | return false; | |
48 | } | |
49 | ||
1429d7c9 JA |
50 | static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx, |
51 | struct blk_mq_ctx *ctx) | |
52 | { | |
53 | return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word]; | |
54 | } | |
55 | ||
56 | #define CTX_TO_BIT(hctx, ctx) \ | |
57 | ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1)) | |
58 | ||
320ae51f JA |
59 | /* |
60 | * Mark this ctx as having pending work in this hardware queue | |
61 | */ | |
62 | static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx, | |
63 | struct blk_mq_ctx *ctx) | |
64 | { | |
1429d7c9 JA |
65 | struct blk_align_bitmap *bm = get_bm(hctx, ctx); |
66 | ||
67 | if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word)) | |
68 | set_bit(CTX_TO_BIT(hctx, ctx), &bm->word); | |
69 | } | |
70 | ||
71 | static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx, | |
72 | struct blk_mq_ctx *ctx) | |
73 | { | |
74 | struct blk_align_bitmap *bm = get_bm(hctx, ctx); | |
75 | ||
76 | clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word); | |
320ae51f JA |
77 | } |
78 | ||
320ae51f JA |
79 | static int blk_mq_queue_enter(struct request_queue *q) |
80 | { | |
81 | int ret; | |
82 | ||
83 | __percpu_counter_add(&q->mq_usage_counter, 1, 1000000); | |
531ed626 | 84 | smp_mb(); |
3b632cf0 KB |
85 | |
86 | /* we have problems freezing the queue if it's initializing */ | |
780db207 | 87 | if (!q->mq_freeze_depth) |
320ae51f JA |
88 | return 0; |
89 | ||
90 | __percpu_counter_add(&q->mq_usage_counter, -1, 1000000); | |
91 | ||
92 | spin_lock_irq(q->queue_lock); | |
93 | ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq, | |
780db207 | 94 | !q->mq_freeze_depth || blk_queue_dying(q), |
43a5e4e2 | 95 | *q->queue_lock); |
320ae51f | 96 | /* inc usage with lock hold to avoid freeze_queue runs here */ |
43a5e4e2 | 97 | if (!ret && !blk_queue_dying(q)) |
320ae51f | 98 | __percpu_counter_add(&q->mq_usage_counter, 1, 1000000); |
43a5e4e2 ML |
99 | else if (blk_queue_dying(q)) |
100 | ret = -ENODEV; | |
320ae51f JA |
101 | spin_unlock_irq(q->queue_lock); |
102 | ||
103 | return ret; | |
104 | } | |
105 | ||
106 | static void blk_mq_queue_exit(struct request_queue *q) | |
107 | { | |
108 | __percpu_counter_add(&q->mq_usage_counter, -1, 1000000); | |
109 | } | |
110 | ||
95ed0681 | 111 | void blk_mq_drain_queue(struct request_queue *q) |
43a5e4e2 ML |
112 | { |
113 | while (true) { | |
114 | s64 count; | |
115 | ||
116 | spin_lock_irq(q->queue_lock); | |
117 | count = percpu_counter_sum(&q->mq_usage_counter); | |
118 | spin_unlock_irq(q->queue_lock); | |
119 | ||
120 | if (count == 0) | |
121 | break; | |
8f5280f4 | 122 | blk_mq_start_hw_queues(q); |
43a5e4e2 ML |
123 | msleep(10); |
124 | } | |
125 | } | |
126 | ||
320ae51f JA |
127 | /* |
128 | * Guarantee no request is in use, so we can change any data structure of | |
129 | * the queue afterward. | |
130 | */ | |
780db207 | 131 | void blk_mq_freeze_queue(struct request_queue *q) |
320ae51f | 132 | { |
320ae51f | 133 | spin_lock_irq(q->queue_lock); |
780db207 | 134 | q->mq_freeze_depth++; |
320ae51f JA |
135 | spin_unlock_irq(q->queue_lock); |
136 | ||
776687bc | 137 | blk_mq_drain_queue(q); |
320ae51f JA |
138 | } |
139 | ||
140 | static void blk_mq_unfreeze_queue(struct request_queue *q) | |
141 | { | |
142 | bool wake = false; | |
143 | ||
144 | spin_lock_irq(q->queue_lock); | |
780db207 TH |
145 | wake = !--q->mq_freeze_depth; |
146 | WARN_ON_ONCE(q->mq_freeze_depth < 0); | |
320ae51f JA |
147 | spin_unlock_irq(q->queue_lock); |
148 | if (wake) | |
149 | wake_up_all(&q->mq_freeze_wq); | |
150 | } | |
151 | ||
152 | bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx) | |
153 | { | |
154 | return blk_mq_has_free_tags(hctx->tags); | |
155 | } | |
156 | EXPORT_SYMBOL(blk_mq_can_queue); | |
157 | ||
94eddfbe JA |
158 | static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx, |
159 | struct request *rq, unsigned int rw_flags) | |
320ae51f | 160 | { |
94eddfbe JA |
161 | if (blk_queue_io_stat(q)) |
162 | rw_flags |= REQ_IO_STAT; | |
163 | ||
af76e555 CH |
164 | INIT_LIST_HEAD(&rq->queuelist); |
165 | /* csd/requeue_work/fifo_time is initialized before use */ | |
166 | rq->q = q; | |
320ae51f | 167 | rq->mq_ctx = ctx; |
0d2602ca | 168 | rq->cmd_flags |= rw_flags; |
af76e555 CH |
169 | /* do not touch atomic flags, it needs atomic ops against the timer */ |
170 | rq->cpu = -1; | |
af76e555 CH |
171 | INIT_HLIST_NODE(&rq->hash); |
172 | RB_CLEAR_NODE(&rq->rb_node); | |
af76e555 CH |
173 | rq->rq_disk = NULL; |
174 | rq->part = NULL; | |
3ee32372 | 175 | rq->start_time = jiffies; |
af76e555 CH |
176 | #ifdef CONFIG_BLK_CGROUP |
177 | rq->rl = NULL; | |
0fec08b4 | 178 | set_start_time_ns(rq); |
af76e555 CH |
179 | rq->io_start_time_ns = 0; |
180 | #endif | |
181 | rq->nr_phys_segments = 0; | |
182 | #if defined(CONFIG_BLK_DEV_INTEGRITY) | |
183 | rq->nr_integrity_segments = 0; | |
184 | #endif | |
af76e555 CH |
185 | rq->special = NULL; |
186 | /* tag was already set */ | |
187 | rq->errors = 0; | |
af76e555 CH |
188 | |
189 | rq->extra_len = 0; | |
190 | rq->sense_len = 0; | |
191 | rq->resid_len = 0; | |
192 | rq->sense = NULL; | |
193 | ||
af76e555 | 194 | INIT_LIST_HEAD(&rq->timeout_list); |
f6be4fb4 JA |
195 | rq->timeout = 0; |
196 | ||
af76e555 CH |
197 | rq->end_io = NULL; |
198 | rq->end_io_data = NULL; | |
199 | rq->next_rq = NULL; | |
200 | ||
320ae51f JA |
201 | ctx->rq_dispatched[rw_is_sync(rw_flags)]++; |
202 | } | |
203 | ||
5dee8577 | 204 | static struct request * |
cb96a42c | 205 | __blk_mq_alloc_request(struct blk_mq_alloc_data *data, int rw) |
5dee8577 CH |
206 | { |
207 | struct request *rq; | |
208 | unsigned int tag; | |
209 | ||
cb96a42c | 210 | tag = blk_mq_get_tag(data); |
5dee8577 | 211 | if (tag != BLK_MQ_TAG_FAIL) { |
cb96a42c | 212 | rq = data->hctx->tags->rqs[tag]; |
5dee8577 CH |
213 | |
214 | rq->cmd_flags = 0; | |
cb96a42c | 215 | if (blk_mq_tag_busy(data->hctx)) { |
5dee8577 | 216 | rq->cmd_flags = REQ_MQ_INFLIGHT; |
cb96a42c | 217 | atomic_inc(&data->hctx->nr_active); |
5dee8577 CH |
218 | } |
219 | ||
220 | rq->tag = tag; | |
cb96a42c | 221 | blk_mq_rq_ctx_init(data->q, data->ctx, rq, rw); |
5dee8577 CH |
222 | return rq; |
223 | } | |
224 | ||
225 | return NULL; | |
226 | } | |
227 | ||
4ce01dd1 CH |
228 | struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp, |
229 | bool reserved) | |
320ae51f | 230 | { |
d852564f CH |
231 | struct blk_mq_ctx *ctx; |
232 | struct blk_mq_hw_ctx *hctx; | |
320ae51f | 233 | struct request *rq; |
cb96a42c | 234 | struct blk_mq_alloc_data alloc_data; |
320ae51f JA |
235 | |
236 | if (blk_mq_queue_enter(q)) | |
237 | return NULL; | |
238 | ||
d852564f CH |
239 | ctx = blk_mq_get_ctx(q); |
240 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
cb96a42c ML |
241 | blk_mq_set_alloc_data(&alloc_data, q, gfp & ~__GFP_WAIT, |
242 | reserved, ctx, hctx); | |
d852564f | 243 | |
cb96a42c | 244 | rq = __blk_mq_alloc_request(&alloc_data, rw); |
d852564f CH |
245 | if (!rq && (gfp & __GFP_WAIT)) { |
246 | __blk_mq_run_hw_queue(hctx); | |
247 | blk_mq_put_ctx(ctx); | |
248 | ||
249 | ctx = blk_mq_get_ctx(q); | |
250 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
cb96a42c ML |
251 | blk_mq_set_alloc_data(&alloc_data, q, gfp, reserved, ctx, |
252 | hctx); | |
253 | rq = __blk_mq_alloc_request(&alloc_data, rw); | |
254 | ctx = alloc_data.ctx; | |
d852564f CH |
255 | } |
256 | blk_mq_put_ctx(ctx); | |
320ae51f JA |
257 | return rq; |
258 | } | |
4bb659b1 | 259 | EXPORT_SYMBOL(blk_mq_alloc_request); |
320ae51f | 260 | |
320ae51f JA |
261 | static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx, |
262 | struct blk_mq_ctx *ctx, struct request *rq) | |
263 | { | |
264 | const int tag = rq->tag; | |
265 | struct request_queue *q = rq->q; | |
266 | ||
0d2602ca JA |
267 | if (rq->cmd_flags & REQ_MQ_INFLIGHT) |
268 | atomic_dec(&hctx->nr_active); | |
269 | ||
af76e555 | 270 | clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
0d2602ca | 271 | blk_mq_put_tag(hctx, tag, &ctx->last_tag); |
320ae51f JA |
272 | blk_mq_queue_exit(q); |
273 | } | |
274 | ||
275 | void blk_mq_free_request(struct request *rq) | |
276 | { | |
277 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
278 | struct blk_mq_hw_ctx *hctx; | |
279 | struct request_queue *q = rq->q; | |
280 | ||
281 | ctx->rq_completed[rq_is_sync(rq)]++; | |
282 | ||
283 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
284 | __blk_mq_free_request(hctx, ctx, rq); | |
285 | } | |
286 | ||
8727af4b CH |
287 | /* |
288 | * Clone all relevant state from a request that has been put on hold in | |
289 | * the flush state machine into the preallocated flush request that hangs | |
290 | * off the request queue. | |
291 | * | |
292 | * For a driver the flush request should be invisible, that's why we are | |
293 | * impersonating the original request here. | |
294 | */ | |
295 | void blk_mq_clone_flush_request(struct request *flush_rq, | |
296 | struct request *orig_rq) | |
297 | { | |
298 | struct blk_mq_hw_ctx *hctx = | |
299 | orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu); | |
300 | ||
301 | flush_rq->mq_ctx = orig_rq->mq_ctx; | |
302 | flush_rq->tag = orig_rq->tag; | |
303 | memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq), | |
304 | hctx->cmd_size); | |
305 | } | |
306 | ||
63151a44 | 307 | inline void __blk_mq_end_io(struct request *rq, int error) |
320ae51f | 308 | { |
0d11e6ac ML |
309 | blk_account_io_done(rq); |
310 | ||
91b63639 | 311 | if (rq->end_io) { |
320ae51f | 312 | rq->end_io(rq, error); |
91b63639 CH |
313 | } else { |
314 | if (unlikely(blk_bidi_rq(rq))) | |
315 | blk_mq_free_request(rq->next_rq); | |
320ae51f | 316 | blk_mq_free_request(rq); |
91b63639 | 317 | } |
320ae51f | 318 | } |
63151a44 CH |
319 | EXPORT_SYMBOL(__blk_mq_end_io); |
320 | ||
321 | void blk_mq_end_io(struct request *rq, int error) | |
322 | { | |
323 | if (blk_update_request(rq, error, blk_rq_bytes(rq))) | |
324 | BUG(); | |
325 | __blk_mq_end_io(rq, error); | |
326 | } | |
327 | EXPORT_SYMBOL(blk_mq_end_io); | |
320ae51f | 328 | |
30a91cb4 | 329 | static void __blk_mq_complete_request_remote(void *data) |
320ae51f | 330 | { |
3d6efbf6 | 331 | struct request *rq = data; |
320ae51f | 332 | |
30a91cb4 | 333 | rq->q->softirq_done_fn(rq); |
320ae51f | 334 | } |
320ae51f | 335 | |
ed851860 | 336 | static void blk_mq_ipi_complete_request(struct request *rq) |
320ae51f JA |
337 | { |
338 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
38535201 | 339 | bool shared = false; |
320ae51f JA |
340 | int cpu; |
341 | ||
38535201 | 342 | if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) { |
30a91cb4 CH |
343 | rq->q->softirq_done_fn(rq); |
344 | return; | |
345 | } | |
320ae51f JA |
346 | |
347 | cpu = get_cpu(); | |
38535201 CH |
348 | if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags)) |
349 | shared = cpus_share_cache(cpu, ctx->cpu); | |
350 | ||
351 | if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) { | |
30a91cb4 | 352 | rq->csd.func = __blk_mq_complete_request_remote; |
3d6efbf6 CH |
353 | rq->csd.info = rq; |
354 | rq->csd.flags = 0; | |
c46fff2a | 355 | smp_call_function_single_async(ctx->cpu, &rq->csd); |
3d6efbf6 | 356 | } else { |
30a91cb4 | 357 | rq->q->softirq_done_fn(rq); |
3d6efbf6 | 358 | } |
320ae51f JA |
359 | put_cpu(); |
360 | } | |
30a91cb4 | 361 | |
ed851860 JA |
362 | void __blk_mq_complete_request(struct request *rq) |
363 | { | |
364 | struct request_queue *q = rq->q; | |
365 | ||
366 | if (!q->softirq_done_fn) | |
367 | blk_mq_end_io(rq, rq->errors); | |
368 | else | |
369 | blk_mq_ipi_complete_request(rq); | |
370 | } | |
371 | ||
30a91cb4 CH |
372 | /** |
373 | * blk_mq_complete_request - end I/O on a request | |
374 | * @rq: the request being processed | |
375 | * | |
376 | * Description: | |
377 | * Ends all I/O on a request. It does not handle partial completions. | |
378 | * The actual completion happens out-of-order, through a IPI handler. | |
379 | **/ | |
380 | void blk_mq_complete_request(struct request *rq) | |
381 | { | |
95f09684 JA |
382 | struct request_queue *q = rq->q; |
383 | ||
384 | if (unlikely(blk_should_fake_timeout(q))) | |
30a91cb4 | 385 | return; |
ed851860 JA |
386 | if (!blk_mark_rq_complete(rq)) |
387 | __blk_mq_complete_request(rq); | |
30a91cb4 CH |
388 | } |
389 | EXPORT_SYMBOL(blk_mq_complete_request); | |
320ae51f | 390 | |
49f5baa5 | 391 | static void blk_mq_start_request(struct request *rq, bool last) |
320ae51f JA |
392 | { |
393 | struct request_queue *q = rq->q; | |
394 | ||
395 | trace_block_rq_issue(q, rq); | |
396 | ||
742ee69b | 397 | rq->resid_len = blk_rq_bytes(rq); |
91b63639 CH |
398 | if (unlikely(blk_bidi_rq(rq))) |
399 | rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq); | |
742ee69b | 400 | |
2b8393b4 | 401 | blk_add_timer(rq); |
87ee7b11 JA |
402 | |
403 | /* | |
404 | * Mark us as started and clear complete. Complete might have been | |
405 | * set if requeue raced with timeout, which then marked it as | |
406 | * complete. So be sure to clear complete again when we start | |
407 | * the request, otherwise we'll ignore the completion event. | |
408 | */ | |
4b570521 JA |
409 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
410 | set_bit(REQ_ATOM_STARTED, &rq->atomic_flags); | |
411 | if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags)) | |
412 | clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags); | |
49f5baa5 CH |
413 | |
414 | if (q->dma_drain_size && blk_rq_bytes(rq)) { | |
415 | /* | |
416 | * Make sure space for the drain appears. We know we can do | |
417 | * this because max_hw_segments has been adjusted to be one | |
418 | * fewer than the device can handle. | |
419 | */ | |
420 | rq->nr_phys_segments++; | |
421 | } | |
422 | ||
423 | /* | |
424 | * Flag the last request in the series so that drivers know when IO | |
425 | * should be kicked off, if they don't do it on a per-request basis. | |
426 | * | |
427 | * Note: the flag isn't the only condition drivers should do kick off. | |
428 | * If drive is busy, the last request might not have the bit set. | |
429 | */ | |
430 | if (last) | |
431 | rq->cmd_flags |= REQ_END; | |
320ae51f JA |
432 | } |
433 | ||
ed0791b2 | 434 | static void __blk_mq_requeue_request(struct request *rq) |
320ae51f JA |
435 | { |
436 | struct request_queue *q = rq->q; | |
437 | ||
438 | trace_block_rq_requeue(q, rq); | |
439 | clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags); | |
49f5baa5 CH |
440 | |
441 | rq->cmd_flags &= ~REQ_END; | |
442 | ||
443 | if (q->dma_drain_size && blk_rq_bytes(rq)) | |
444 | rq->nr_phys_segments--; | |
320ae51f JA |
445 | } |
446 | ||
ed0791b2 CH |
447 | void blk_mq_requeue_request(struct request *rq) |
448 | { | |
ed0791b2 CH |
449 | __blk_mq_requeue_request(rq); |
450 | blk_clear_rq_complete(rq); | |
451 | ||
ed0791b2 | 452 | BUG_ON(blk_queued_rq(rq)); |
6fca6a61 | 453 | blk_mq_add_to_requeue_list(rq, true); |
ed0791b2 CH |
454 | } |
455 | EXPORT_SYMBOL(blk_mq_requeue_request); | |
456 | ||
6fca6a61 CH |
457 | static void blk_mq_requeue_work(struct work_struct *work) |
458 | { | |
459 | struct request_queue *q = | |
460 | container_of(work, struct request_queue, requeue_work); | |
461 | LIST_HEAD(rq_list); | |
462 | struct request *rq, *next; | |
463 | unsigned long flags; | |
464 | ||
465 | spin_lock_irqsave(&q->requeue_lock, flags); | |
466 | list_splice_init(&q->requeue_list, &rq_list); | |
467 | spin_unlock_irqrestore(&q->requeue_lock, flags); | |
468 | ||
469 | list_for_each_entry_safe(rq, next, &rq_list, queuelist) { | |
470 | if (!(rq->cmd_flags & REQ_SOFTBARRIER)) | |
471 | continue; | |
472 | ||
473 | rq->cmd_flags &= ~REQ_SOFTBARRIER; | |
474 | list_del_init(&rq->queuelist); | |
475 | blk_mq_insert_request(rq, true, false, false); | |
476 | } | |
477 | ||
478 | while (!list_empty(&rq_list)) { | |
479 | rq = list_entry(rq_list.next, struct request, queuelist); | |
480 | list_del_init(&rq->queuelist); | |
481 | blk_mq_insert_request(rq, false, false, false); | |
482 | } | |
483 | ||
484 | blk_mq_run_queues(q, false); | |
485 | } | |
486 | ||
487 | void blk_mq_add_to_requeue_list(struct request *rq, bool at_head) | |
488 | { | |
489 | struct request_queue *q = rq->q; | |
490 | unsigned long flags; | |
491 | ||
492 | /* | |
493 | * We abuse this flag that is otherwise used by the I/O scheduler to | |
494 | * request head insertation from the workqueue. | |
495 | */ | |
496 | BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER); | |
497 | ||
498 | spin_lock_irqsave(&q->requeue_lock, flags); | |
499 | if (at_head) { | |
500 | rq->cmd_flags |= REQ_SOFTBARRIER; | |
501 | list_add(&rq->queuelist, &q->requeue_list); | |
502 | } else { | |
503 | list_add_tail(&rq->queuelist, &q->requeue_list); | |
504 | } | |
505 | spin_unlock_irqrestore(&q->requeue_lock, flags); | |
506 | } | |
507 | EXPORT_SYMBOL(blk_mq_add_to_requeue_list); | |
508 | ||
509 | void blk_mq_kick_requeue_list(struct request_queue *q) | |
510 | { | |
511 | kblockd_schedule_work(&q->requeue_work); | |
512 | } | |
513 | EXPORT_SYMBOL(blk_mq_kick_requeue_list); | |
514 | ||
0e62f51f | 515 | static inline bool is_flush_request(struct request *rq, unsigned int tag) |
24d2f903 | 516 | { |
0e62f51f JA |
517 | return ((rq->cmd_flags & REQ_FLUSH_SEQ) && |
518 | rq->q->flush_rq->tag == tag); | |
519 | } | |
520 | ||
521 | struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag) | |
522 | { | |
523 | struct request *rq = tags->rqs[tag]; | |
22302375 | 524 | |
0e62f51f JA |
525 | if (!is_flush_request(rq, tag)) |
526 | return rq; | |
22302375 | 527 | |
0e62f51f | 528 | return rq->q->flush_rq; |
24d2f903 CH |
529 | } |
530 | EXPORT_SYMBOL(blk_mq_tag_to_rq); | |
531 | ||
320ae51f JA |
532 | struct blk_mq_timeout_data { |
533 | struct blk_mq_hw_ctx *hctx; | |
534 | unsigned long *next; | |
535 | unsigned int *next_set; | |
536 | }; | |
537 | ||
538 | static void blk_mq_timeout_check(void *__data, unsigned long *free_tags) | |
539 | { | |
540 | struct blk_mq_timeout_data *data = __data; | |
541 | struct blk_mq_hw_ctx *hctx = data->hctx; | |
542 | unsigned int tag; | |
543 | ||
544 | /* It may not be in flight yet (this is where | |
545 | * the REQ_ATOMIC_STARTED flag comes in). The requests are | |
546 | * statically allocated, so we know it's always safe to access the | |
547 | * memory associated with a bit offset into ->rqs[]. | |
548 | */ | |
549 | tag = 0; | |
550 | do { | |
551 | struct request *rq; | |
552 | ||
24d2f903 CH |
553 | tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag); |
554 | if (tag >= hctx->tags->nr_tags) | |
320ae51f JA |
555 | break; |
556 | ||
0e62f51f | 557 | rq = blk_mq_tag_to_rq(hctx->tags, tag++); |
24d2f903 CH |
558 | if (rq->q != hctx->queue) |
559 | continue; | |
320ae51f JA |
560 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
561 | continue; | |
562 | ||
563 | blk_rq_check_expired(rq, data->next, data->next_set); | |
564 | } while (1); | |
565 | } | |
566 | ||
567 | static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx, | |
568 | unsigned long *next, | |
569 | unsigned int *next_set) | |
570 | { | |
571 | struct blk_mq_timeout_data data = { | |
572 | .hctx = hctx, | |
573 | .next = next, | |
574 | .next_set = next_set, | |
575 | }; | |
576 | ||
577 | /* | |
578 | * Ask the tagging code to iterate busy requests, so we can | |
579 | * check them for timeout. | |
580 | */ | |
581 | blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data); | |
582 | } | |
583 | ||
87ee7b11 JA |
584 | static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq) |
585 | { | |
586 | struct request_queue *q = rq->q; | |
587 | ||
588 | /* | |
589 | * We know that complete is set at this point. If STARTED isn't set | |
590 | * anymore, then the request isn't active and the "timeout" should | |
591 | * just be ignored. This can happen due to the bitflag ordering. | |
592 | * Timeout first checks if STARTED is set, and if it is, assumes | |
593 | * the request is active. But if we race with completion, then | |
594 | * we both flags will get cleared. So check here again, and ignore | |
595 | * a timeout event with a request that isn't active. | |
596 | */ | |
597 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) | |
598 | return BLK_EH_NOT_HANDLED; | |
599 | ||
600 | if (!q->mq_ops->timeout) | |
601 | return BLK_EH_RESET_TIMER; | |
602 | ||
603 | return q->mq_ops->timeout(rq); | |
604 | } | |
605 | ||
320ae51f JA |
606 | static void blk_mq_rq_timer(unsigned long data) |
607 | { | |
608 | struct request_queue *q = (struct request_queue *) data; | |
609 | struct blk_mq_hw_ctx *hctx; | |
610 | unsigned long next = 0; | |
611 | int i, next_set = 0; | |
612 | ||
484b4061 JA |
613 | queue_for_each_hw_ctx(q, hctx, i) { |
614 | /* | |
615 | * If not software queues are currently mapped to this | |
616 | * hardware queue, there's nothing to check | |
617 | */ | |
618 | if (!hctx->nr_ctx || !hctx->tags) | |
619 | continue; | |
620 | ||
320ae51f | 621 | blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set); |
484b4061 | 622 | } |
320ae51f | 623 | |
0d2602ca JA |
624 | if (next_set) { |
625 | next = blk_rq_timeout(round_jiffies_up(next)); | |
626 | mod_timer(&q->timeout, next); | |
627 | } else { | |
628 | queue_for_each_hw_ctx(q, hctx, i) | |
629 | blk_mq_tag_idle(hctx); | |
630 | } | |
320ae51f JA |
631 | } |
632 | ||
633 | /* | |
634 | * Reverse check our software queue for entries that we could potentially | |
635 | * merge with. Currently includes a hand-wavy stop count of 8, to not spend | |
636 | * too much time checking for merges. | |
637 | */ | |
638 | static bool blk_mq_attempt_merge(struct request_queue *q, | |
639 | struct blk_mq_ctx *ctx, struct bio *bio) | |
640 | { | |
641 | struct request *rq; | |
642 | int checked = 8; | |
643 | ||
644 | list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) { | |
645 | int el_ret; | |
646 | ||
647 | if (!checked--) | |
648 | break; | |
649 | ||
650 | if (!blk_rq_merge_ok(rq, bio)) | |
651 | continue; | |
652 | ||
653 | el_ret = blk_try_merge(rq, bio); | |
654 | if (el_ret == ELEVATOR_BACK_MERGE) { | |
655 | if (bio_attempt_back_merge(q, rq, bio)) { | |
656 | ctx->rq_merged++; | |
657 | return true; | |
658 | } | |
659 | break; | |
660 | } else if (el_ret == ELEVATOR_FRONT_MERGE) { | |
661 | if (bio_attempt_front_merge(q, rq, bio)) { | |
662 | ctx->rq_merged++; | |
663 | return true; | |
664 | } | |
665 | break; | |
666 | } | |
667 | } | |
668 | ||
669 | return false; | |
670 | } | |
671 | ||
1429d7c9 JA |
672 | /* |
673 | * Process software queues that have been marked busy, splicing them | |
674 | * to the for-dispatch | |
675 | */ | |
676 | static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list) | |
677 | { | |
678 | struct blk_mq_ctx *ctx; | |
679 | int i; | |
680 | ||
681 | for (i = 0; i < hctx->ctx_map.map_size; i++) { | |
682 | struct blk_align_bitmap *bm = &hctx->ctx_map.map[i]; | |
683 | unsigned int off, bit; | |
684 | ||
685 | if (!bm->word) | |
686 | continue; | |
687 | ||
688 | bit = 0; | |
689 | off = i * hctx->ctx_map.bits_per_word; | |
690 | do { | |
691 | bit = find_next_bit(&bm->word, bm->depth, bit); | |
692 | if (bit >= bm->depth) | |
693 | break; | |
694 | ||
695 | ctx = hctx->ctxs[bit + off]; | |
696 | clear_bit(bit, &bm->word); | |
697 | spin_lock(&ctx->lock); | |
698 | list_splice_tail_init(&ctx->rq_list, list); | |
699 | spin_unlock(&ctx->lock); | |
700 | ||
701 | bit++; | |
702 | } while (1); | |
703 | } | |
704 | } | |
705 | ||
320ae51f JA |
706 | /* |
707 | * Run this hardware queue, pulling any software queues mapped to it in. | |
708 | * Note that this function currently has various problems around ordering | |
709 | * of IO. In particular, we'd like FIFO behaviour on handling existing | |
710 | * items on the hctx->dispatch list. Ignore that for now. | |
711 | */ | |
712 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) | |
713 | { | |
714 | struct request_queue *q = hctx->queue; | |
320ae51f JA |
715 | struct request *rq; |
716 | LIST_HEAD(rq_list); | |
1429d7c9 | 717 | int queued; |
320ae51f | 718 | |
fd1270d5 | 719 | WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)); |
e4043dcf | 720 | |
5d12f905 | 721 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
320ae51f JA |
722 | return; |
723 | ||
724 | hctx->run++; | |
725 | ||
726 | /* | |
727 | * Touch any software queue that has pending entries. | |
728 | */ | |
1429d7c9 | 729 | flush_busy_ctxs(hctx, &rq_list); |
320ae51f JA |
730 | |
731 | /* | |
732 | * If we have previous entries on our dispatch list, grab them | |
733 | * and stuff them at the front for more fair dispatch. | |
734 | */ | |
735 | if (!list_empty_careful(&hctx->dispatch)) { | |
736 | spin_lock(&hctx->lock); | |
737 | if (!list_empty(&hctx->dispatch)) | |
738 | list_splice_init(&hctx->dispatch, &rq_list); | |
739 | spin_unlock(&hctx->lock); | |
740 | } | |
741 | ||
320ae51f JA |
742 | /* |
743 | * Now process all the entries, sending them to the driver. | |
744 | */ | |
1429d7c9 | 745 | queued = 0; |
320ae51f JA |
746 | while (!list_empty(&rq_list)) { |
747 | int ret; | |
748 | ||
749 | rq = list_first_entry(&rq_list, struct request, queuelist); | |
750 | list_del_init(&rq->queuelist); | |
320ae51f | 751 | |
49f5baa5 | 752 | blk_mq_start_request(rq, list_empty(&rq_list)); |
320ae51f JA |
753 | |
754 | ret = q->mq_ops->queue_rq(hctx, rq); | |
755 | switch (ret) { | |
756 | case BLK_MQ_RQ_QUEUE_OK: | |
757 | queued++; | |
758 | continue; | |
759 | case BLK_MQ_RQ_QUEUE_BUSY: | |
320ae51f | 760 | list_add(&rq->queuelist, &rq_list); |
ed0791b2 | 761 | __blk_mq_requeue_request(rq); |
320ae51f JA |
762 | break; |
763 | default: | |
764 | pr_err("blk-mq: bad return on queue: %d\n", ret); | |
320ae51f | 765 | case BLK_MQ_RQ_QUEUE_ERROR: |
1e93b8c2 | 766 | rq->errors = -EIO; |
320ae51f JA |
767 | blk_mq_end_io(rq, rq->errors); |
768 | break; | |
769 | } | |
770 | ||
771 | if (ret == BLK_MQ_RQ_QUEUE_BUSY) | |
772 | break; | |
773 | } | |
774 | ||
775 | if (!queued) | |
776 | hctx->dispatched[0]++; | |
777 | else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1))) | |
778 | hctx->dispatched[ilog2(queued) + 1]++; | |
779 | ||
780 | /* | |
781 | * Any items that need requeuing? Stuff them into hctx->dispatch, | |
782 | * that is where we will continue on next queue run. | |
783 | */ | |
784 | if (!list_empty(&rq_list)) { | |
785 | spin_lock(&hctx->lock); | |
786 | list_splice(&rq_list, &hctx->dispatch); | |
787 | spin_unlock(&hctx->lock); | |
788 | } | |
789 | } | |
790 | ||
506e931f JA |
791 | /* |
792 | * It'd be great if the workqueue API had a way to pass | |
793 | * in a mask and had some smarts for more clever placement. | |
794 | * For now we just round-robin here, switching for every | |
795 | * BLK_MQ_CPU_WORK_BATCH queued items. | |
796 | */ | |
797 | static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) | |
798 | { | |
799 | int cpu = hctx->next_cpu; | |
800 | ||
801 | if (--hctx->next_cpu_batch <= 0) { | |
802 | int next_cpu; | |
803 | ||
804 | next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask); | |
805 | if (next_cpu >= nr_cpu_ids) | |
806 | next_cpu = cpumask_first(hctx->cpumask); | |
807 | ||
808 | hctx->next_cpu = next_cpu; | |
809 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; | |
810 | } | |
811 | ||
812 | return cpu; | |
813 | } | |
814 | ||
320ae51f JA |
815 | void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) |
816 | { | |
5d12f905 | 817 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
320ae51f JA |
818 | return; |
819 | ||
e4043dcf | 820 | if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask)) |
320ae51f | 821 | __blk_mq_run_hw_queue(hctx); |
e4043dcf | 822 | else if (hctx->queue->nr_hw_queues == 1) |
70f4db63 | 823 | kblockd_schedule_delayed_work(&hctx->run_work, 0); |
e4043dcf JA |
824 | else { |
825 | unsigned int cpu; | |
826 | ||
506e931f | 827 | cpu = blk_mq_hctx_next_cpu(hctx); |
70f4db63 | 828 | kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0); |
e4043dcf | 829 | } |
320ae51f JA |
830 | } |
831 | ||
832 | void blk_mq_run_queues(struct request_queue *q, bool async) | |
833 | { | |
834 | struct blk_mq_hw_ctx *hctx; | |
835 | int i; | |
836 | ||
837 | queue_for_each_hw_ctx(q, hctx, i) { | |
838 | if ((!blk_mq_hctx_has_pending(hctx) && | |
839 | list_empty_careful(&hctx->dispatch)) || | |
5d12f905 | 840 | test_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
320ae51f JA |
841 | continue; |
842 | ||
e4043dcf | 843 | preempt_disable(); |
320ae51f | 844 | blk_mq_run_hw_queue(hctx, async); |
e4043dcf | 845 | preempt_enable(); |
320ae51f JA |
846 | } |
847 | } | |
848 | EXPORT_SYMBOL(blk_mq_run_queues); | |
849 | ||
850 | void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx) | |
851 | { | |
70f4db63 CH |
852 | cancel_delayed_work(&hctx->run_work); |
853 | cancel_delayed_work(&hctx->delay_work); | |
320ae51f JA |
854 | set_bit(BLK_MQ_S_STOPPED, &hctx->state); |
855 | } | |
856 | EXPORT_SYMBOL(blk_mq_stop_hw_queue); | |
857 | ||
280d45f6 CH |
858 | void blk_mq_stop_hw_queues(struct request_queue *q) |
859 | { | |
860 | struct blk_mq_hw_ctx *hctx; | |
861 | int i; | |
862 | ||
863 | queue_for_each_hw_ctx(q, hctx, i) | |
864 | blk_mq_stop_hw_queue(hctx); | |
865 | } | |
866 | EXPORT_SYMBOL(blk_mq_stop_hw_queues); | |
867 | ||
320ae51f JA |
868 | void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx) |
869 | { | |
870 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
e4043dcf JA |
871 | |
872 | preempt_disable(); | |
0ffbce80 | 873 | blk_mq_run_hw_queue(hctx, false); |
e4043dcf | 874 | preempt_enable(); |
320ae51f JA |
875 | } |
876 | EXPORT_SYMBOL(blk_mq_start_hw_queue); | |
877 | ||
2f268556 CH |
878 | void blk_mq_start_hw_queues(struct request_queue *q) |
879 | { | |
880 | struct blk_mq_hw_ctx *hctx; | |
881 | int i; | |
882 | ||
883 | queue_for_each_hw_ctx(q, hctx, i) | |
884 | blk_mq_start_hw_queue(hctx); | |
885 | } | |
886 | EXPORT_SYMBOL(blk_mq_start_hw_queues); | |
887 | ||
888 | ||
1b4a3258 | 889 | void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async) |
320ae51f JA |
890 | { |
891 | struct blk_mq_hw_ctx *hctx; | |
892 | int i; | |
893 | ||
894 | queue_for_each_hw_ctx(q, hctx, i) { | |
895 | if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state)) | |
896 | continue; | |
897 | ||
898 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
e4043dcf | 899 | preempt_disable(); |
1b4a3258 | 900 | blk_mq_run_hw_queue(hctx, async); |
e4043dcf | 901 | preempt_enable(); |
320ae51f JA |
902 | } |
903 | } | |
904 | EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues); | |
905 | ||
70f4db63 | 906 | static void blk_mq_run_work_fn(struct work_struct *work) |
320ae51f JA |
907 | { |
908 | struct blk_mq_hw_ctx *hctx; | |
909 | ||
70f4db63 | 910 | hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work); |
e4043dcf | 911 | |
320ae51f JA |
912 | __blk_mq_run_hw_queue(hctx); |
913 | } | |
914 | ||
70f4db63 CH |
915 | static void blk_mq_delay_work_fn(struct work_struct *work) |
916 | { | |
917 | struct blk_mq_hw_ctx *hctx; | |
918 | ||
919 | hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work); | |
920 | ||
921 | if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state)) | |
922 | __blk_mq_run_hw_queue(hctx); | |
923 | } | |
924 | ||
925 | void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs) | |
926 | { | |
927 | unsigned long tmo = msecs_to_jiffies(msecs); | |
928 | ||
929 | if (hctx->queue->nr_hw_queues == 1) | |
930 | kblockd_schedule_delayed_work(&hctx->delay_work, tmo); | |
931 | else { | |
932 | unsigned int cpu; | |
933 | ||
506e931f | 934 | cpu = blk_mq_hctx_next_cpu(hctx); |
70f4db63 CH |
935 | kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo); |
936 | } | |
937 | } | |
938 | EXPORT_SYMBOL(blk_mq_delay_queue); | |
939 | ||
320ae51f | 940 | static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, |
72a0a36e | 941 | struct request *rq, bool at_head) |
320ae51f JA |
942 | { |
943 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
944 | ||
01b983c9 JA |
945 | trace_block_rq_insert(hctx->queue, rq); |
946 | ||
72a0a36e CH |
947 | if (at_head) |
948 | list_add(&rq->queuelist, &ctx->rq_list); | |
949 | else | |
950 | list_add_tail(&rq->queuelist, &ctx->rq_list); | |
4bb659b1 | 951 | |
320ae51f | 952 | blk_mq_hctx_mark_pending(hctx, ctx); |
320ae51f JA |
953 | } |
954 | ||
eeabc850 CH |
955 | void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue, |
956 | bool async) | |
320ae51f | 957 | { |
eeabc850 | 958 | struct request_queue *q = rq->q; |
320ae51f | 959 | struct blk_mq_hw_ctx *hctx; |
eeabc850 CH |
960 | struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx; |
961 | ||
962 | current_ctx = blk_mq_get_ctx(q); | |
963 | if (!cpu_online(ctx->cpu)) | |
964 | rq->mq_ctx = ctx = current_ctx; | |
320ae51f | 965 | |
320ae51f JA |
966 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
967 | ||
eeabc850 CH |
968 | if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) && |
969 | !(rq->cmd_flags & (REQ_FLUSH_SEQ))) { | |
320ae51f JA |
970 | blk_insert_flush(rq); |
971 | } else { | |
320ae51f | 972 | spin_lock(&ctx->lock); |
72a0a36e | 973 | __blk_mq_insert_request(hctx, rq, at_head); |
320ae51f | 974 | spin_unlock(&ctx->lock); |
320ae51f JA |
975 | } |
976 | ||
320ae51f JA |
977 | if (run_queue) |
978 | blk_mq_run_hw_queue(hctx, async); | |
e4043dcf JA |
979 | |
980 | blk_mq_put_ctx(current_ctx); | |
320ae51f JA |
981 | } |
982 | ||
983 | static void blk_mq_insert_requests(struct request_queue *q, | |
984 | struct blk_mq_ctx *ctx, | |
985 | struct list_head *list, | |
986 | int depth, | |
987 | bool from_schedule) | |
988 | ||
989 | { | |
990 | struct blk_mq_hw_ctx *hctx; | |
991 | struct blk_mq_ctx *current_ctx; | |
992 | ||
993 | trace_block_unplug(q, depth, !from_schedule); | |
994 | ||
995 | current_ctx = blk_mq_get_ctx(q); | |
996 | ||
997 | if (!cpu_online(ctx->cpu)) | |
998 | ctx = current_ctx; | |
999 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
1000 | ||
1001 | /* | |
1002 | * preemption doesn't flush plug list, so it's possible ctx->cpu is | |
1003 | * offline now | |
1004 | */ | |
1005 | spin_lock(&ctx->lock); | |
1006 | while (!list_empty(list)) { | |
1007 | struct request *rq; | |
1008 | ||
1009 | rq = list_first_entry(list, struct request, queuelist); | |
1010 | list_del_init(&rq->queuelist); | |
1011 | rq->mq_ctx = ctx; | |
72a0a36e | 1012 | __blk_mq_insert_request(hctx, rq, false); |
320ae51f JA |
1013 | } |
1014 | spin_unlock(&ctx->lock); | |
1015 | ||
320ae51f | 1016 | blk_mq_run_hw_queue(hctx, from_schedule); |
e4043dcf | 1017 | blk_mq_put_ctx(current_ctx); |
320ae51f JA |
1018 | } |
1019 | ||
1020 | static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b) | |
1021 | { | |
1022 | struct request *rqa = container_of(a, struct request, queuelist); | |
1023 | struct request *rqb = container_of(b, struct request, queuelist); | |
1024 | ||
1025 | return !(rqa->mq_ctx < rqb->mq_ctx || | |
1026 | (rqa->mq_ctx == rqb->mq_ctx && | |
1027 | blk_rq_pos(rqa) < blk_rq_pos(rqb))); | |
1028 | } | |
1029 | ||
1030 | void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) | |
1031 | { | |
1032 | struct blk_mq_ctx *this_ctx; | |
1033 | struct request_queue *this_q; | |
1034 | struct request *rq; | |
1035 | LIST_HEAD(list); | |
1036 | LIST_HEAD(ctx_list); | |
1037 | unsigned int depth; | |
1038 | ||
1039 | list_splice_init(&plug->mq_list, &list); | |
1040 | ||
1041 | list_sort(NULL, &list, plug_ctx_cmp); | |
1042 | ||
1043 | this_q = NULL; | |
1044 | this_ctx = NULL; | |
1045 | depth = 0; | |
1046 | ||
1047 | while (!list_empty(&list)) { | |
1048 | rq = list_entry_rq(list.next); | |
1049 | list_del_init(&rq->queuelist); | |
1050 | BUG_ON(!rq->q); | |
1051 | if (rq->mq_ctx != this_ctx) { | |
1052 | if (this_ctx) { | |
1053 | blk_mq_insert_requests(this_q, this_ctx, | |
1054 | &ctx_list, depth, | |
1055 | from_schedule); | |
1056 | } | |
1057 | ||
1058 | this_ctx = rq->mq_ctx; | |
1059 | this_q = rq->q; | |
1060 | depth = 0; | |
1061 | } | |
1062 | ||
1063 | depth++; | |
1064 | list_add_tail(&rq->queuelist, &ctx_list); | |
1065 | } | |
1066 | ||
1067 | /* | |
1068 | * If 'this_ctx' is set, we know we have entries to complete | |
1069 | * on 'ctx_list'. Do those. | |
1070 | */ | |
1071 | if (this_ctx) { | |
1072 | blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth, | |
1073 | from_schedule); | |
1074 | } | |
1075 | } | |
1076 | ||
1077 | static void blk_mq_bio_to_request(struct request *rq, struct bio *bio) | |
1078 | { | |
1079 | init_request_from_bio(rq, bio); | |
4b570521 | 1080 | |
3ee32372 | 1081 | if (blk_do_io_stat(rq)) |
4b570521 | 1082 | blk_account_io_start(rq, 1); |
320ae51f JA |
1083 | } |
1084 | ||
07068d5b JA |
1085 | static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx, |
1086 | struct blk_mq_ctx *ctx, | |
1087 | struct request *rq, struct bio *bio) | |
320ae51f | 1088 | { |
07068d5b | 1089 | struct request_queue *q = hctx->queue; |
320ae51f | 1090 | |
07068d5b JA |
1091 | if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) { |
1092 | blk_mq_bio_to_request(rq, bio); | |
1093 | spin_lock(&ctx->lock); | |
1094 | insert_rq: | |
1095 | __blk_mq_insert_request(hctx, rq, false); | |
1096 | spin_unlock(&ctx->lock); | |
1097 | return false; | |
1098 | } else { | |
1099 | spin_lock(&ctx->lock); | |
1100 | if (!blk_mq_attempt_merge(q, ctx, bio)) { | |
1101 | blk_mq_bio_to_request(rq, bio); | |
1102 | goto insert_rq; | |
1103 | } | |
320ae51f | 1104 | |
07068d5b JA |
1105 | spin_unlock(&ctx->lock); |
1106 | __blk_mq_free_request(hctx, ctx, rq); | |
1107 | return true; | |
14ec77f3 | 1108 | } |
07068d5b | 1109 | } |
14ec77f3 | 1110 | |
07068d5b JA |
1111 | struct blk_map_ctx { |
1112 | struct blk_mq_hw_ctx *hctx; | |
1113 | struct blk_mq_ctx *ctx; | |
1114 | }; | |
1115 | ||
1116 | static struct request *blk_mq_map_request(struct request_queue *q, | |
1117 | struct bio *bio, | |
1118 | struct blk_map_ctx *data) | |
1119 | { | |
1120 | struct blk_mq_hw_ctx *hctx; | |
1121 | struct blk_mq_ctx *ctx; | |
1122 | struct request *rq; | |
1123 | int rw = bio_data_dir(bio); | |
cb96a42c | 1124 | struct blk_mq_alloc_data alloc_data; |
320ae51f | 1125 | |
07068d5b | 1126 | if (unlikely(blk_mq_queue_enter(q))) { |
320ae51f | 1127 | bio_endio(bio, -EIO); |
07068d5b | 1128 | return NULL; |
320ae51f JA |
1129 | } |
1130 | ||
1131 | ctx = blk_mq_get_ctx(q); | |
1132 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
1133 | ||
07068d5b | 1134 | if (rw_is_sync(bio->bi_rw)) |
27fbf4e8 | 1135 | rw |= REQ_SYNC; |
07068d5b | 1136 | |
320ae51f | 1137 | trace_block_getrq(q, bio, rw); |
cb96a42c ML |
1138 | blk_mq_set_alloc_data(&alloc_data, q, GFP_ATOMIC, false, ctx, |
1139 | hctx); | |
1140 | rq = __blk_mq_alloc_request(&alloc_data, rw); | |
5dee8577 | 1141 | if (unlikely(!rq)) { |
793597a6 | 1142 | __blk_mq_run_hw_queue(hctx); |
320ae51f JA |
1143 | blk_mq_put_ctx(ctx); |
1144 | trace_block_sleeprq(q, bio, rw); | |
793597a6 CH |
1145 | |
1146 | ctx = blk_mq_get_ctx(q); | |
320ae51f | 1147 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
cb96a42c ML |
1148 | blk_mq_set_alloc_data(&alloc_data, q, |
1149 | __GFP_WAIT|GFP_ATOMIC, false, ctx, hctx); | |
1150 | rq = __blk_mq_alloc_request(&alloc_data, rw); | |
1151 | ctx = alloc_data.ctx; | |
1152 | hctx = alloc_data.hctx; | |
320ae51f JA |
1153 | } |
1154 | ||
1155 | hctx->queued++; | |
07068d5b JA |
1156 | data->hctx = hctx; |
1157 | data->ctx = ctx; | |
1158 | return rq; | |
1159 | } | |
1160 | ||
1161 | /* | |
1162 | * Multiple hardware queue variant. This will not use per-process plugs, | |
1163 | * but will attempt to bypass the hctx queueing if we can go straight to | |
1164 | * hardware for SYNC IO. | |
1165 | */ | |
1166 | static void blk_mq_make_request(struct request_queue *q, struct bio *bio) | |
1167 | { | |
1168 | const int is_sync = rw_is_sync(bio->bi_rw); | |
1169 | const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA); | |
1170 | struct blk_map_ctx data; | |
1171 | struct request *rq; | |
1172 | ||
1173 | blk_queue_bounce(q, &bio); | |
1174 | ||
1175 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { | |
1176 | bio_endio(bio, -EIO); | |
1177 | return; | |
1178 | } | |
1179 | ||
1180 | rq = blk_mq_map_request(q, bio, &data); | |
1181 | if (unlikely(!rq)) | |
1182 | return; | |
1183 | ||
1184 | if (unlikely(is_flush_fua)) { | |
1185 | blk_mq_bio_to_request(rq, bio); | |
1186 | blk_insert_flush(rq); | |
1187 | goto run_queue; | |
1188 | } | |
1189 | ||
1190 | if (is_sync) { | |
1191 | int ret; | |
1192 | ||
1193 | blk_mq_bio_to_request(rq, bio); | |
1194 | blk_mq_start_request(rq, true); | |
1195 | ||
1196 | /* | |
1197 | * For OK queue, we are done. For error, kill it. Any other | |
1198 | * error (busy), just add it to our list as we previously | |
1199 | * would have done | |
1200 | */ | |
1201 | ret = q->mq_ops->queue_rq(data.hctx, rq); | |
1202 | if (ret == BLK_MQ_RQ_QUEUE_OK) | |
1203 | goto done; | |
1204 | else { | |
1205 | __blk_mq_requeue_request(rq); | |
1206 | ||
1207 | if (ret == BLK_MQ_RQ_QUEUE_ERROR) { | |
1208 | rq->errors = -EIO; | |
1209 | blk_mq_end_io(rq, rq->errors); | |
1210 | goto done; | |
1211 | } | |
1212 | } | |
1213 | } | |
1214 | ||
1215 | if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) { | |
1216 | /* | |
1217 | * For a SYNC request, send it to the hardware immediately. For | |
1218 | * an ASYNC request, just ensure that we run it later on. The | |
1219 | * latter allows for merging opportunities and more efficient | |
1220 | * dispatching. | |
1221 | */ | |
1222 | run_queue: | |
1223 | blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua); | |
1224 | } | |
1225 | done: | |
1226 | blk_mq_put_ctx(data.ctx); | |
1227 | } | |
1228 | ||
1229 | /* | |
1230 | * Single hardware queue variant. This will attempt to use any per-process | |
1231 | * plug for merging and IO deferral. | |
1232 | */ | |
1233 | static void blk_sq_make_request(struct request_queue *q, struct bio *bio) | |
1234 | { | |
1235 | const int is_sync = rw_is_sync(bio->bi_rw); | |
1236 | const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA); | |
1237 | unsigned int use_plug, request_count = 0; | |
1238 | struct blk_map_ctx data; | |
1239 | struct request *rq; | |
1240 | ||
1241 | /* | |
1242 | * If we have multiple hardware queues, just go directly to | |
1243 | * one of those for sync IO. | |
1244 | */ | |
1245 | use_plug = !is_flush_fua && !is_sync; | |
1246 | ||
1247 | blk_queue_bounce(q, &bio); | |
1248 | ||
1249 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { | |
1250 | bio_endio(bio, -EIO); | |
1251 | return; | |
1252 | } | |
1253 | ||
1254 | if (use_plug && !blk_queue_nomerges(q) && | |
1255 | blk_attempt_plug_merge(q, bio, &request_count)) | |
1256 | return; | |
1257 | ||
1258 | rq = blk_mq_map_request(q, bio, &data); | |
ff87bcec JA |
1259 | if (unlikely(!rq)) |
1260 | return; | |
320ae51f JA |
1261 | |
1262 | if (unlikely(is_flush_fua)) { | |
1263 | blk_mq_bio_to_request(rq, bio); | |
320ae51f JA |
1264 | blk_insert_flush(rq); |
1265 | goto run_queue; | |
1266 | } | |
1267 | ||
1268 | /* | |
1269 | * A task plug currently exists. Since this is completely lockless, | |
1270 | * utilize that to temporarily store requests until the task is | |
1271 | * either done or scheduled away. | |
1272 | */ | |
1273 | if (use_plug) { | |
1274 | struct blk_plug *plug = current->plug; | |
1275 | ||
1276 | if (plug) { | |
1277 | blk_mq_bio_to_request(rq, bio); | |
92f399c7 | 1278 | if (list_empty(&plug->mq_list)) |
320ae51f JA |
1279 | trace_block_plug(q); |
1280 | else if (request_count >= BLK_MAX_REQUEST_COUNT) { | |
1281 | blk_flush_plug_list(plug, false); | |
1282 | trace_block_plug(q); | |
1283 | } | |
1284 | list_add_tail(&rq->queuelist, &plug->mq_list); | |
07068d5b | 1285 | blk_mq_put_ctx(data.ctx); |
320ae51f JA |
1286 | return; |
1287 | } | |
1288 | } | |
1289 | ||
07068d5b JA |
1290 | if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) { |
1291 | /* | |
1292 | * For a SYNC request, send it to the hardware immediately. For | |
1293 | * an ASYNC request, just ensure that we run it later on. The | |
1294 | * latter allows for merging opportunities and more efficient | |
1295 | * dispatching. | |
1296 | */ | |
1297 | run_queue: | |
1298 | blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua); | |
320ae51f JA |
1299 | } |
1300 | ||
07068d5b | 1301 | blk_mq_put_ctx(data.ctx); |
320ae51f JA |
1302 | } |
1303 | ||
1304 | /* | |
1305 | * Default mapping to a software queue, since we use one per CPU. | |
1306 | */ | |
1307 | struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu) | |
1308 | { | |
1309 | return q->queue_hw_ctx[q->mq_map[cpu]]; | |
1310 | } | |
1311 | EXPORT_SYMBOL(blk_mq_map_queue); | |
1312 | ||
24d2f903 CH |
1313 | static void blk_mq_free_rq_map(struct blk_mq_tag_set *set, |
1314 | struct blk_mq_tags *tags, unsigned int hctx_idx) | |
95363efd | 1315 | { |
e9b267d9 | 1316 | struct page *page; |
320ae51f | 1317 | |
24d2f903 | 1318 | if (tags->rqs && set->ops->exit_request) { |
e9b267d9 | 1319 | int i; |
320ae51f | 1320 | |
24d2f903 CH |
1321 | for (i = 0; i < tags->nr_tags; i++) { |
1322 | if (!tags->rqs[i]) | |
e9b267d9 | 1323 | continue; |
24d2f903 CH |
1324 | set->ops->exit_request(set->driver_data, tags->rqs[i], |
1325 | hctx_idx, i); | |
e9b267d9 | 1326 | } |
320ae51f | 1327 | } |
320ae51f | 1328 | |
24d2f903 CH |
1329 | while (!list_empty(&tags->page_list)) { |
1330 | page = list_first_entry(&tags->page_list, struct page, lru); | |
6753471c | 1331 | list_del_init(&page->lru); |
320ae51f JA |
1332 | __free_pages(page, page->private); |
1333 | } | |
1334 | ||
24d2f903 | 1335 | kfree(tags->rqs); |
320ae51f | 1336 | |
24d2f903 | 1337 | blk_mq_free_tags(tags); |
320ae51f JA |
1338 | } |
1339 | ||
1340 | static size_t order_to_size(unsigned int order) | |
1341 | { | |
4ca08500 | 1342 | return (size_t)PAGE_SIZE << order; |
320ae51f JA |
1343 | } |
1344 | ||
24d2f903 CH |
1345 | static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set, |
1346 | unsigned int hctx_idx) | |
320ae51f | 1347 | { |
24d2f903 | 1348 | struct blk_mq_tags *tags; |
320ae51f JA |
1349 | unsigned int i, j, entries_per_page, max_order = 4; |
1350 | size_t rq_size, left; | |
1351 | ||
24d2f903 CH |
1352 | tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags, |
1353 | set->numa_node); | |
1354 | if (!tags) | |
1355 | return NULL; | |
320ae51f | 1356 | |
24d2f903 CH |
1357 | INIT_LIST_HEAD(&tags->page_list); |
1358 | ||
1359 | tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *), | |
1360 | GFP_KERNEL, set->numa_node); | |
1361 | if (!tags->rqs) { | |
1362 | blk_mq_free_tags(tags); | |
1363 | return NULL; | |
1364 | } | |
320ae51f JA |
1365 | |
1366 | /* | |
1367 | * rq_size is the size of the request plus driver payload, rounded | |
1368 | * to the cacheline size | |
1369 | */ | |
24d2f903 | 1370 | rq_size = round_up(sizeof(struct request) + set->cmd_size, |
320ae51f | 1371 | cache_line_size()); |
24d2f903 | 1372 | left = rq_size * set->queue_depth; |
320ae51f | 1373 | |
24d2f903 | 1374 | for (i = 0; i < set->queue_depth; ) { |
320ae51f JA |
1375 | int this_order = max_order; |
1376 | struct page *page; | |
1377 | int to_do; | |
1378 | void *p; | |
1379 | ||
1380 | while (left < order_to_size(this_order - 1) && this_order) | |
1381 | this_order--; | |
1382 | ||
1383 | do { | |
24d2f903 CH |
1384 | page = alloc_pages_node(set->numa_node, GFP_KERNEL, |
1385 | this_order); | |
320ae51f JA |
1386 | if (page) |
1387 | break; | |
1388 | if (!this_order--) | |
1389 | break; | |
1390 | if (order_to_size(this_order) < rq_size) | |
1391 | break; | |
1392 | } while (1); | |
1393 | ||
1394 | if (!page) | |
24d2f903 | 1395 | goto fail; |
320ae51f JA |
1396 | |
1397 | page->private = this_order; | |
24d2f903 | 1398 | list_add_tail(&page->lru, &tags->page_list); |
320ae51f JA |
1399 | |
1400 | p = page_address(page); | |
1401 | entries_per_page = order_to_size(this_order) / rq_size; | |
24d2f903 | 1402 | to_do = min(entries_per_page, set->queue_depth - i); |
320ae51f JA |
1403 | left -= to_do * rq_size; |
1404 | for (j = 0; j < to_do; j++) { | |
24d2f903 CH |
1405 | tags->rqs[i] = p; |
1406 | if (set->ops->init_request) { | |
1407 | if (set->ops->init_request(set->driver_data, | |
1408 | tags->rqs[i], hctx_idx, i, | |
1409 | set->numa_node)) | |
1410 | goto fail; | |
e9b267d9 CH |
1411 | } |
1412 | ||
320ae51f JA |
1413 | p += rq_size; |
1414 | i++; | |
1415 | } | |
1416 | } | |
1417 | ||
24d2f903 | 1418 | return tags; |
320ae51f | 1419 | |
24d2f903 CH |
1420 | fail: |
1421 | pr_warn("%s: failed to allocate requests\n", __func__); | |
1422 | blk_mq_free_rq_map(set, tags, hctx_idx); | |
1423 | return NULL; | |
320ae51f JA |
1424 | } |
1425 | ||
1429d7c9 JA |
1426 | static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap) |
1427 | { | |
1428 | kfree(bitmap->map); | |
1429 | } | |
1430 | ||
1431 | static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node) | |
1432 | { | |
1433 | unsigned int bpw = 8, total, num_maps, i; | |
1434 | ||
1435 | bitmap->bits_per_word = bpw; | |
1436 | ||
1437 | num_maps = ALIGN(nr_cpu_ids, bpw) / bpw; | |
1438 | bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap), | |
1439 | GFP_KERNEL, node); | |
1440 | if (!bitmap->map) | |
1441 | return -ENOMEM; | |
1442 | ||
1443 | bitmap->map_size = num_maps; | |
1444 | ||
1445 | total = nr_cpu_ids; | |
1446 | for (i = 0; i < num_maps; i++) { | |
1447 | bitmap->map[i].depth = min(total, bitmap->bits_per_word); | |
1448 | total -= bitmap->map[i].depth; | |
1449 | } | |
1450 | ||
1451 | return 0; | |
1452 | } | |
1453 | ||
484b4061 JA |
1454 | static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu) |
1455 | { | |
1456 | struct request_queue *q = hctx->queue; | |
1457 | struct blk_mq_ctx *ctx; | |
1458 | LIST_HEAD(tmp); | |
1459 | ||
1460 | /* | |
1461 | * Move ctx entries to new CPU, if this one is going away. | |
1462 | */ | |
1463 | ctx = __blk_mq_get_ctx(q, cpu); | |
1464 | ||
1465 | spin_lock(&ctx->lock); | |
1466 | if (!list_empty(&ctx->rq_list)) { | |
1467 | list_splice_init(&ctx->rq_list, &tmp); | |
1468 | blk_mq_hctx_clear_pending(hctx, ctx); | |
1469 | } | |
1470 | spin_unlock(&ctx->lock); | |
1471 | ||
1472 | if (list_empty(&tmp)) | |
1473 | return NOTIFY_OK; | |
1474 | ||
1475 | ctx = blk_mq_get_ctx(q); | |
1476 | spin_lock(&ctx->lock); | |
1477 | ||
1478 | while (!list_empty(&tmp)) { | |
1479 | struct request *rq; | |
1480 | ||
1481 | rq = list_first_entry(&tmp, struct request, queuelist); | |
1482 | rq->mq_ctx = ctx; | |
1483 | list_move_tail(&rq->queuelist, &ctx->rq_list); | |
1484 | } | |
1485 | ||
1486 | hctx = q->mq_ops->map_queue(q, ctx->cpu); | |
1487 | blk_mq_hctx_mark_pending(hctx, ctx); | |
1488 | ||
1489 | spin_unlock(&ctx->lock); | |
1490 | ||
1491 | blk_mq_run_hw_queue(hctx, true); | |
1492 | blk_mq_put_ctx(ctx); | |
1493 | return NOTIFY_OK; | |
1494 | } | |
1495 | ||
1496 | static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu) | |
1497 | { | |
1498 | struct request_queue *q = hctx->queue; | |
1499 | struct blk_mq_tag_set *set = q->tag_set; | |
1500 | ||
1501 | if (set->tags[hctx->queue_num]) | |
1502 | return NOTIFY_OK; | |
1503 | ||
1504 | set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num); | |
1505 | if (!set->tags[hctx->queue_num]) | |
1506 | return NOTIFY_STOP; | |
1507 | ||
1508 | hctx->tags = set->tags[hctx->queue_num]; | |
1509 | return NOTIFY_OK; | |
1510 | } | |
1511 | ||
1512 | static int blk_mq_hctx_notify(void *data, unsigned long action, | |
1513 | unsigned int cpu) | |
1514 | { | |
1515 | struct blk_mq_hw_ctx *hctx = data; | |
1516 | ||
1517 | if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) | |
1518 | return blk_mq_hctx_cpu_offline(hctx, cpu); | |
1519 | else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) | |
1520 | return blk_mq_hctx_cpu_online(hctx, cpu); | |
1521 | ||
1522 | return NOTIFY_OK; | |
1523 | } | |
1524 | ||
624dbe47 ML |
1525 | static void blk_mq_exit_hw_queues(struct request_queue *q, |
1526 | struct blk_mq_tag_set *set, int nr_queue) | |
1527 | { | |
1528 | struct blk_mq_hw_ctx *hctx; | |
1529 | unsigned int i; | |
1530 | ||
1531 | queue_for_each_hw_ctx(q, hctx, i) { | |
1532 | if (i == nr_queue) | |
1533 | break; | |
1534 | ||
f899fed4 JA |
1535 | blk_mq_tag_idle(hctx); |
1536 | ||
624dbe47 ML |
1537 | if (set->ops->exit_hctx) |
1538 | set->ops->exit_hctx(hctx, i); | |
1539 | ||
1540 | blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier); | |
1541 | kfree(hctx->ctxs); | |
1542 | blk_mq_free_bitmap(&hctx->ctx_map); | |
1543 | } | |
1544 | ||
1545 | } | |
1546 | ||
1547 | static void blk_mq_free_hw_queues(struct request_queue *q, | |
1548 | struct blk_mq_tag_set *set) | |
1549 | { | |
1550 | struct blk_mq_hw_ctx *hctx; | |
1551 | unsigned int i; | |
1552 | ||
1553 | queue_for_each_hw_ctx(q, hctx, i) { | |
1554 | free_cpumask_var(hctx->cpumask); | |
cdef54dd | 1555 | kfree(hctx); |
624dbe47 ML |
1556 | } |
1557 | } | |
1558 | ||
320ae51f | 1559 | static int blk_mq_init_hw_queues(struct request_queue *q, |
24d2f903 | 1560 | struct blk_mq_tag_set *set) |
320ae51f JA |
1561 | { |
1562 | struct blk_mq_hw_ctx *hctx; | |
624dbe47 | 1563 | unsigned int i; |
320ae51f JA |
1564 | |
1565 | /* | |
1566 | * Initialize hardware queues | |
1567 | */ | |
1568 | queue_for_each_hw_ctx(q, hctx, i) { | |
320ae51f JA |
1569 | int node; |
1570 | ||
1571 | node = hctx->numa_node; | |
1572 | if (node == NUMA_NO_NODE) | |
24d2f903 | 1573 | node = hctx->numa_node = set->numa_node; |
320ae51f | 1574 | |
70f4db63 CH |
1575 | INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn); |
1576 | INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn); | |
320ae51f JA |
1577 | spin_lock_init(&hctx->lock); |
1578 | INIT_LIST_HEAD(&hctx->dispatch); | |
1579 | hctx->queue = q; | |
1580 | hctx->queue_num = i; | |
24d2f903 CH |
1581 | hctx->flags = set->flags; |
1582 | hctx->cmd_size = set->cmd_size; | |
320ae51f JA |
1583 | |
1584 | blk_mq_init_cpu_notifier(&hctx->cpu_notifier, | |
1585 | blk_mq_hctx_notify, hctx); | |
1586 | blk_mq_register_cpu_notifier(&hctx->cpu_notifier); | |
1587 | ||
24d2f903 | 1588 | hctx->tags = set->tags[i]; |
320ae51f JA |
1589 | |
1590 | /* | |
1591 | * Allocate space for all possible cpus to avoid allocation in | |
1592 | * runtime | |
1593 | */ | |
1594 | hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *), | |
1595 | GFP_KERNEL, node); | |
1596 | if (!hctx->ctxs) | |
1597 | break; | |
1598 | ||
1429d7c9 | 1599 | if (blk_mq_alloc_bitmap(&hctx->ctx_map, node)) |
320ae51f JA |
1600 | break; |
1601 | ||
320ae51f JA |
1602 | hctx->nr_ctx = 0; |
1603 | ||
24d2f903 CH |
1604 | if (set->ops->init_hctx && |
1605 | set->ops->init_hctx(hctx, set->driver_data, i)) | |
320ae51f JA |
1606 | break; |
1607 | } | |
1608 | ||
1609 | if (i == q->nr_hw_queues) | |
1610 | return 0; | |
1611 | ||
1612 | /* | |
1613 | * Init failed | |
1614 | */ | |
624dbe47 | 1615 | blk_mq_exit_hw_queues(q, set, i); |
320ae51f JA |
1616 | |
1617 | return 1; | |
1618 | } | |
1619 | ||
1620 | static void blk_mq_init_cpu_queues(struct request_queue *q, | |
1621 | unsigned int nr_hw_queues) | |
1622 | { | |
1623 | unsigned int i; | |
1624 | ||
1625 | for_each_possible_cpu(i) { | |
1626 | struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i); | |
1627 | struct blk_mq_hw_ctx *hctx; | |
1628 | ||
1629 | memset(__ctx, 0, sizeof(*__ctx)); | |
1630 | __ctx->cpu = i; | |
1631 | spin_lock_init(&__ctx->lock); | |
1632 | INIT_LIST_HEAD(&__ctx->rq_list); | |
1633 | __ctx->queue = q; | |
1634 | ||
1635 | /* If the cpu isn't online, the cpu is mapped to first hctx */ | |
320ae51f JA |
1636 | if (!cpu_online(i)) |
1637 | continue; | |
1638 | ||
e4043dcf JA |
1639 | hctx = q->mq_ops->map_queue(q, i); |
1640 | cpumask_set_cpu(i, hctx->cpumask); | |
1641 | hctx->nr_ctx++; | |
1642 | ||
320ae51f JA |
1643 | /* |
1644 | * Set local node, IFF we have more than one hw queue. If | |
1645 | * not, we remain on the home node of the device | |
1646 | */ | |
1647 | if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE) | |
1648 | hctx->numa_node = cpu_to_node(i); | |
1649 | } | |
1650 | } | |
1651 | ||
1652 | static void blk_mq_map_swqueue(struct request_queue *q) | |
1653 | { | |
1654 | unsigned int i; | |
1655 | struct blk_mq_hw_ctx *hctx; | |
1656 | struct blk_mq_ctx *ctx; | |
1657 | ||
1658 | queue_for_each_hw_ctx(q, hctx, i) { | |
e4043dcf | 1659 | cpumask_clear(hctx->cpumask); |
320ae51f JA |
1660 | hctx->nr_ctx = 0; |
1661 | } | |
1662 | ||
1663 | /* | |
1664 | * Map software to hardware queues | |
1665 | */ | |
1666 | queue_for_each_ctx(q, ctx, i) { | |
1667 | /* If the cpu isn't online, the cpu is mapped to first hctx */ | |
e4043dcf JA |
1668 | if (!cpu_online(i)) |
1669 | continue; | |
1670 | ||
320ae51f | 1671 | hctx = q->mq_ops->map_queue(q, i); |
e4043dcf | 1672 | cpumask_set_cpu(i, hctx->cpumask); |
320ae51f JA |
1673 | ctx->index_hw = hctx->nr_ctx; |
1674 | hctx->ctxs[hctx->nr_ctx++] = ctx; | |
1675 | } | |
506e931f JA |
1676 | |
1677 | queue_for_each_hw_ctx(q, hctx, i) { | |
484b4061 JA |
1678 | /* |
1679 | * If not software queues are mapped to this hardware queue, | |
1680 | * disable it and free the request entries | |
1681 | */ | |
1682 | if (!hctx->nr_ctx) { | |
1683 | struct blk_mq_tag_set *set = q->tag_set; | |
1684 | ||
1685 | if (set->tags[i]) { | |
1686 | blk_mq_free_rq_map(set, set->tags[i], i); | |
1687 | set->tags[i] = NULL; | |
1688 | hctx->tags = NULL; | |
1689 | } | |
1690 | continue; | |
1691 | } | |
1692 | ||
1693 | /* | |
1694 | * Initialize batch roundrobin counts | |
1695 | */ | |
506e931f JA |
1696 | hctx->next_cpu = cpumask_first(hctx->cpumask); |
1697 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; | |
1698 | } | |
320ae51f JA |
1699 | } |
1700 | ||
0d2602ca JA |
1701 | static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set) |
1702 | { | |
1703 | struct blk_mq_hw_ctx *hctx; | |
1704 | struct request_queue *q; | |
1705 | bool shared; | |
1706 | int i; | |
1707 | ||
1708 | if (set->tag_list.next == set->tag_list.prev) | |
1709 | shared = false; | |
1710 | else | |
1711 | shared = true; | |
1712 | ||
1713 | list_for_each_entry(q, &set->tag_list, tag_set_list) { | |
1714 | blk_mq_freeze_queue(q); | |
1715 | ||
1716 | queue_for_each_hw_ctx(q, hctx, i) { | |
1717 | if (shared) | |
1718 | hctx->flags |= BLK_MQ_F_TAG_SHARED; | |
1719 | else | |
1720 | hctx->flags &= ~BLK_MQ_F_TAG_SHARED; | |
1721 | } | |
1722 | blk_mq_unfreeze_queue(q); | |
1723 | } | |
1724 | } | |
1725 | ||
1726 | static void blk_mq_del_queue_tag_set(struct request_queue *q) | |
1727 | { | |
1728 | struct blk_mq_tag_set *set = q->tag_set; | |
1729 | ||
1730 | blk_mq_freeze_queue(q); | |
1731 | ||
1732 | mutex_lock(&set->tag_list_lock); | |
1733 | list_del_init(&q->tag_set_list); | |
1734 | blk_mq_update_tag_set_depth(set); | |
1735 | mutex_unlock(&set->tag_list_lock); | |
1736 | ||
1737 | blk_mq_unfreeze_queue(q); | |
1738 | } | |
1739 | ||
1740 | static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set, | |
1741 | struct request_queue *q) | |
1742 | { | |
1743 | q->tag_set = set; | |
1744 | ||
1745 | mutex_lock(&set->tag_list_lock); | |
1746 | list_add_tail(&q->tag_set_list, &set->tag_list); | |
1747 | blk_mq_update_tag_set_depth(set); | |
1748 | mutex_unlock(&set->tag_list_lock); | |
1749 | } | |
1750 | ||
24d2f903 | 1751 | struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set) |
320ae51f JA |
1752 | { |
1753 | struct blk_mq_hw_ctx **hctxs; | |
e6cdb092 | 1754 | struct blk_mq_ctx __percpu *ctx; |
320ae51f | 1755 | struct request_queue *q; |
f14bbe77 | 1756 | unsigned int *map; |
320ae51f JA |
1757 | int i; |
1758 | ||
320ae51f JA |
1759 | ctx = alloc_percpu(struct blk_mq_ctx); |
1760 | if (!ctx) | |
1761 | return ERR_PTR(-ENOMEM); | |
1762 | ||
24d2f903 CH |
1763 | hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL, |
1764 | set->numa_node); | |
320ae51f JA |
1765 | |
1766 | if (!hctxs) | |
1767 | goto err_percpu; | |
1768 | ||
f14bbe77 JA |
1769 | map = blk_mq_make_queue_map(set); |
1770 | if (!map) | |
1771 | goto err_map; | |
1772 | ||
24d2f903 | 1773 | for (i = 0; i < set->nr_hw_queues; i++) { |
f14bbe77 JA |
1774 | int node = blk_mq_hw_queue_to_node(map, i); |
1775 | ||
cdef54dd CH |
1776 | hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx), |
1777 | GFP_KERNEL, node); | |
320ae51f JA |
1778 | if (!hctxs[i]) |
1779 | goto err_hctxs; | |
1780 | ||
e4043dcf JA |
1781 | if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL)) |
1782 | goto err_hctxs; | |
1783 | ||
0d2602ca | 1784 | atomic_set(&hctxs[i]->nr_active, 0); |
f14bbe77 | 1785 | hctxs[i]->numa_node = node; |
320ae51f JA |
1786 | hctxs[i]->queue_num = i; |
1787 | } | |
1788 | ||
24d2f903 | 1789 | q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node); |
320ae51f JA |
1790 | if (!q) |
1791 | goto err_hctxs; | |
1792 | ||
3d2936f4 ML |
1793 | if (percpu_counter_init(&q->mq_usage_counter, 0)) |
1794 | goto err_map; | |
1795 | ||
320ae51f JA |
1796 | setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q); |
1797 | blk_queue_rq_timeout(q, 30000); | |
1798 | ||
1799 | q->nr_queues = nr_cpu_ids; | |
24d2f903 | 1800 | q->nr_hw_queues = set->nr_hw_queues; |
f14bbe77 | 1801 | q->mq_map = map; |
320ae51f JA |
1802 | |
1803 | q->queue_ctx = ctx; | |
1804 | q->queue_hw_ctx = hctxs; | |
1805 | ||
24d2f903 | 1806 | q->mq_ops = set->ops; |
94eddfbe | 1807 | q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT; |
320ae51f | 1808 | |
05f1dd53 JA |
1809 | if (!(set->flags & BLK_MQ_F_SG_MERGE)) |
1810 | q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE; | |
1811 | ||
1be036e9 CH |
1812 | q->sg_reserved_size = INT_MAX; |
1813 | ||
6fca6a61 CH |
1814 | INIT_WORK(&q->requeue_work, blk_mq_requeue_work); |
1815 | INIT_LIST_HEAD(&q->requeue_list); | |
1816 | spin_lock_init(&q->requeue_lock); | |
1817 | ||
07068d5b JA |
1818 | if (q->nr_hw_queues > 1) |
1819 | blk_queue_make_request(q, blk_mq_make_request); | |
1820 | else | |
1821 | blk_queue_make_request(q, blk_sq_make_request); | |
1822 | ||
87ee7b11 | 1823 | blk_queue_rq_timed_out(q, blk_mq_rq_timed_out); |
24d2f903 CH |
1824 | if (set->timeout) |
1825 | blk_queue_rq_timeout(q, set->timeout); | |
320ae51f | 1826 | |
eba71768 JA |
1827 | /* |
1828 | * Do this after blk_queue_make_request() overrides it... | |
1829 | */ | |
1830 | q->nr_requests = set->queue_depth; | |
1831 | ||
24d2f903 CH |
1832 | if (set->ops->complete) |
1833 | blk_queue_softirq_done(q, set->ops->complete); | |
30a91cb4 | 1834 | |
320ae51f | 1835 | blk_mq_init_flush(q); |
24d2f903 | 1836 | blk_mq_init_cpu_queues(q, set->nr_hw_queues); |
320ae51f | 1837 | |
24d2f903 CH |
1838 | q->flush_rq = kzalloc(round_up(sizeof(struct request) + |
1839 | set->cmd_size, cache_line_size()), | |
1840 | GFP_KERNEL); | |
18741986 | 1841 | if (!q->flush_rq) |
320ae51f JA |
1842 | goto err_hw; |
1843 | ||
24d2f903 | 1844 | if (blk_mq_init_hw_queues(q, set)) |
18741986 CH |
1845 | goto err_flush_rq; |
1846 | ||
320ae51f JA |
1847 | mutex_lock(&all_q_mutex); |
1848 | list_add_tail(&q->all_q_node, &all_q_list); | |
1849 | mutex_unlock(&all_q_mutex); | |
1850 | ||
0d2602ca JA |
1851 | blk_mq_add_queue_tag_set(set, q); |
1852 | ||
484b4061 JA |
1853 | blk_mq_map_swqueue(q); |
1854 | ||
320ae51f | 1855 | return q; |
18741986 CH |
1856 | |
1857 | err_flush_rq: | |
1858 | kfree(q->flush_rq); | |
320ae51f | 1859 | err_hw: |
320ae51f JA |
1860 | blk_cleanup_queue(q); |
1861 | err_hctxs: | |
f14bbe77 | 1862 | kfree(map); |
24d2f903 | 1863 | for (i = 0; i < set->nr_hw_queues; i++) { |
320ae51f JA |
1864 | if (!hctxs[i]) |
1865 | break; | |
e4043dcf | 1866 | free_cpumask_var(hctxs[i]->cpumask); |
cdef54dd | 1867 | kfree(hctxs[i]); |
320ae51f | 1868 | } |
f14bbe77 | 1869 | err_map: |
320ae51f JA |
1870 | kfree(hctxs); |
1871 | err_percpu: | |
1872 | free_percpu(ctx); | |
1873 | return ERR_PTR(-ENOMEM); | |
1874 | } | |
1875 | EXPORT_SYMBOL(blk_mq_init_queue); | |
1876 | ||
1877 | void blk_mq_free_queue(struct request_queue *q) | |
1878 | { | |
624dbe47 | 1879 | struct blk_mq_tag_set *set = q->tag_set; |
320ae51f | 1880 | |
0d2602ca JA |
1881 | blk_mq_del_queue_tag_set(q); |
1882 | ||
624dbe47 ML |
1883 | blk_mq_exit_hw_queues(q, set, set->nr_hw_queues); |
1884 | blk_mq_free_hw_queues(q, set); | |
320ae51f | 1885 | |
3d2936f4 ML |
1886 | percpu_counter_destroy(&q->mq_usage_counter); |
1887 | ||
320ae51f JA |
1888 | free_percpu(q->queue_ctx); |
1889 | kfree(q->queue_hw_ctx); | |
1890 | kfree(q->mq_map); | |
1891 | ||
1892 | q->queue_ctx = NULL; | |
1893 | q->queue_hw_ctx = NULL; | |
1894 | q->mq_map = NULL; | |
1895 | ||
1896 | mutex_lock(&all_q_mutex); | |
1897 | list_del_init(&q->all_q_node); | |
1898 | mutex_unlock(&all_q_mutex); | |
1899 | } | |
320ae51f JA |
1900 | |
1901 | /* Basically redo blk_mq_init_queue with queue frozen */ | |
f618ef7c | 1902 | static void blk_mq_queue_reinit(struct request_queue *q) |
320ae51f JA |
1903 | { |
1904 | blk_mq_freeze_queue(q); | |
1905 | ||
67aec14c JA |
1906 | blk_mq_sysfs_unregister(q); |
1907 | ||
320ae51f JA |
1908 | blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues); |
1909 | ||
1910 | /* | |
1911 | * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe | |
1912 | * we should change hctx numa_node according to new topology (this | |
1913 | * involves free and re-allocate memory, worthy doing?) | |
1914 | */ | |
1915 | ||
1916 | blk_mq_map_swqueue(q); | |
1917 | ||
67aec14c JA |
1918 | blk_mq_sysfs_register(q); |
1919 | ||
320ae51f JA |
1920 | blk_mq_unfreeze_queue(q); |
1921 | } | |
1922 | ||
f618ef7c PG |
1923 | static int blk_mq_queue_reinit_notify(struct notifier_block *nb, |
1924 | unsigned long action, void *hcpu) | |
320ae51f JA |
1925 | { |
1926 | struct request_queue *q; | |
1927 | ||
1928 | /* | |
9fccfed8 JA |
1929 | * Before new mappings are established, hotadded cpu might already |
1930 | * start handling requests. This doesn't break anything as we map | |
1931 | * offline CPUs to first hardware queue. We will re-init the queue | |
1932 | * below to get optimal settings. | |
320ae51f JA |
1933 | */ |
1934 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN && | |
1935 | action != CPU_ONLINE && action != CPU_ONLINE_FROZEN) | |
1936 | return NOTIFY_OK; | |
1937 | ||
1938 | mutex_lock(&all_q_mutex); | |
1939 | list_for_each_entry(q, &all_q_list, all_q_node) | |
1940 | blk_mq_queue_reinit(q); | |
1941 | mutex_unlock(&all_q_mutex); | |
1942 | return NOTIFY_OK; | |
1943 | } | |
1944 | ||
a4391c64 JA |
1945 | /* |
1946 | * Alloc a tag set to be associated with one or more request queues. | |
1947 | * May fail with EINVAL for various error conditions. May adjust the | |
1948 | * requested depth down, if if it too large. In that case, the set | |
1949 | * value will be stored in set->queue_depth. | |
1950 | */ | |
24d2f903 CH |
1951 | int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) |
1952 | { | |
1953 | int i; | |
1954 | ||
1955 | if (!set->nr_hw_queues) | |
1956 | return -EINVAL; | |
a4391c64 | 1957 | if (!set->queue_depth) |
24d2f903 CH |
1958 | return -EINVAL; |
1959 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) | |
1960 | return -EINVAL; | |
1961 | ||
cdef54dd | 1962 | if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue) |
24d2f903 CH |
1963 | return -EINVAL; |
1964 | ||
a4391c64 JA |
1965 | if (set->queue_depth > BLK_MQ_MAX_DEPTH) { |
1966 | pr_info("blk-mq: reduced tag depth to %u\n", | |
1967 | BLK_MQ_MAX_DEPTH); | |
1968 | set->queue_depth = BLK_MQ_MAX_DEPTH; | |
1969 | } | |
24d2f903 | 1970 | |
48479005 ML |
1971 | set->tags = kmalloc_node(set->nr_hw_queues * |
1972 | sizeof(struct blk_mq_tags *), | |
24d2f903 CH |
1973 | GFP_KERNEL, set->numa_node); |
1974 | if (!set->tags) | |
1975 | goto out; | |
1976 | ||
1977 | for (i = 0; i < set->nr_hw_queues; i++) { | |
1978 | set->tags[i] = blk_mq_init_rq_map(set, i); | |
1979 | if (!set->tags[i]) | |
1980 | goto out_unwind; | |
1981 | } | |
1982 | ||
0d2602ca JA |
1983 | mutex_init(&set->tag_list_lock); |
1984 | INIT_LIST_HEAD(&set->tag_list); | |
1985 | ||
24d2f903 CH |
1986 | return 0; |
1987 | ||
1988 | out_unwind: | |
1989 | while (--i >= 0) | |
1990 | blk_mq_free_rq_map(set, set->tags[i], i); | |
1991 | out: | |
1992 | return -ENOMEM; | |
1993 | } | |
1994 | EXPORT_SYMBOL(blk_mq_alloc_tag_set); | |
1995 | ||
1996 | void blk_mq_free_tag_set(struct blk_mq_tag_set *set) | |
1997 | { | |
1998 | int i; | |
1999 | ||
484b4061 JA |
2000 | for (i = 0; i < set->nr_hw_queues; i++) { |
2001 | if (set->tags[i]) | |
2002 | blk_mq_free_rq_map(set, set->tags[i], i); | |
2003 | } | |
2004 | ||
981bd189 | 2005 | kfree(set->tags); |
24d2f903 CH |
2006 | } |
2007 | EXPORT_SYMBOL(blk_mq_free_tag_set); | |
2008 | ||
e3a2b3f9 JA |
2009 | int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) |
2010 | { | |
2011 | struct blk_mq_tag_set *set = q->tag_set; | |
2012 | struct blk_mq_hw_ctx *hctx; | |
2013 | int i, ret; | |
2014 | ||
2015 | if (!set || nr > set->queue_depth) | |
2016 | return -EINVAL; | |
2017 | ||
2018 | ret = 0; | |
2019 | queue_for_each_hw_ctx(q, hctx, i) { | |
2020 | ret = blk_mq_tag_update_depth(hctx->tags, nr); | |
2021 | if (ret) | |
2022 | break; | |
2023 | } | |
2024 | ||
2025 | if (!ret) | |
2026 | q->nr_requests = nr; | |
2027 | ||
2028 | return ret; | |
2029 | } | |
2030 | ||
676141e4 JA |
2031 | void blk_mq_disable_hotplug(void) |
2032 | { | |
2033 | mutex_lock(&all_q_mutex); | |
2034 | } | |
2035 | ||
2036 | void blk_mq_enable_hotplug(void) | |
2037 | { | |
2038 | mutex_unlock(&all_q_mutex); | |
2039 | } | |
2040 | ||
320ae51f JA |
2041 | static int __init blk_mq_init(void) |
2042 | { | |
320ae51f JA |
2043 | blk_mq_cpu_init(); |
2044 | ||
2045 | /* Must be called after percpu_counter_hotcpu_callback() */ | |
2046 | hotcpu_notifier(blk_mq_queue_reinit_notify, -10); | |
2047 | ||
2048 | return 0; | |
2049 | } | |
2050 | subsys_initcall(blk_mq_init); |