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