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