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