]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Block multiqueue core code | |
3 | * | |
4 | * Copyright (C) 2013-2014 Jens Axboe | |
5 | * Copyright (C) 2013-2014 Christoph Hellwig | |
6 | */ | |
7 | #include <linux/kernel.h> | |
8 | #include <linux/module.h> | |
9 | #include <linux/backing-dev.h> | |
10 | #include <linux/bio.h> | |
11 | #include <linux/blkdev.h> | |
12 | #include <linux/kmemleak.h> | |
13 | #include <linux/mm.h> | |
14 | #include <linux/init.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/workqueue.h> | |
17 | #include <linux/smp.h> | |
18 | #include <linux/llist.h> | |
19 | #include <linux/list_sort.h> | |
20 | #include <linux/cpu.h> | |
21 | #include <linux/cache.h> | |
22 | #include <linux/sched/sysctl.h> | |
23 | #include <linux/sched/topology.h> | |
24 | #include <linux/sched/signal.h> | |
25 | #include <linux/delay.h> | |
26 | #include <linux/crash_dump.h> | |
27 | #include <linux/prefetch.h> | |
28 | ||
29 | #include <trace/events/block.h> | |
30 | ||
31 | #include <linux/blk-mq.h> | |
32 | #include "blk.h" | |
33 | #include "blk-mq.h" | |
34 | #include "blk-mq-tag.h" | |
35 | #include "blk-stat.h" | |
36 | #include "blk-wbt.h" | |
37 | #include "blk-mq-sched.h" | |
38 | ||
39 | static DEFINE_MUTEX(all_q_mutex); | |
40 | static LIST_HEAD(all_q_list); | |
41 | ||
42 | static void blk_mq_poll_stats_start(struct request_queue *q); | |
43 | static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb); | |
44 | static void __blk_mq_stop_hw_queues(struct request_queue *q, bool sync); | |
45 | ||
46 | static int blk_mq_poll_stats_bkt(const struct request *rq) | |
47 | { | |
48 | int ddir, bytes, bucket; | |
49 | ||
50 | ddir = rq_data_dir(rq); | |
51 | bytes = blk_rq_bytes(rq); | |
52 | ||
53 | bucket = ddir + 2*(ilog2(bytes) - 9); | |
54 | ||
55 | if (bucket < 0) | |
56 | return -1; | |
57 | else if (bucket >= BLK_MQ_POLL_STATS_BKTS) | |
58 | return ddir + BLK_MQ_POLL_STATS_BKTS - 2; | |
59 | ||
60 | return bucket; | |
61 | } | |
62 | ||
63 | /* | |
64 | * Check if any of the ctx's have pending work in this hardware queue | |
65 | */ | |
66 | bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx) | |
67 | { | |
68 | return sbitmap_any_bit_set(&hctx->ctx_map) || | |
69 | !list_empty_careful(&hctx->dispatch) || | |
70 | blk_mq_sched_has_work(hctx); | |
71 | } | |
72 | ||
73 | /* | |
74 | * Mark this ctx as having pending work in this hardware queue | |
75 | */ | |
76 | static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx, | |
77 | struct blk_mq_ctx *ctx) | |
78 | { | |
79 | if (!sbitmap_test_bit(&hctx->ctx_map, ctx->index_hw)) | |
80 | sbitmap_set_bit(&hctx->ctx_map, ctx->index_hw); | |
81 | } | |
82 | ||
83 | static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx, | |
84 | struct blk_mq_ctx *ctx) | |
85 | { | |
86 | sbitmap_clear_bit(&hctx->ctx_map, ctx->index_hw); | |
87 | } | |
88 | ||
89 | void blk_freeze_queue_start(struct request_queue *q) | |
90 | { | |
91 | int freeze_depth; | |
92 | ||
93 | freeze_depth = atomic_inc_return(&q->mq_freeze_depth); | |
94 | if (freeze_depth == 1) { | |
95 | percpu_ref_kill(&q->q_usage_counter); | |
96 | blk_mq_run_hw_queues(q, false); | |
97 | } | |
98 | } | |
99 | EXPORT_SYMBOL_GPL(blk_freeze_queue_start); | |
100 | ||
101 | void blk_mq_freeze_queue_wait(struct request_queue *q) | |
102 | { | |
103 | wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter)); | |
104 | } | |
105 | EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait); | |
106 | ||
107 | int blk_mq_freeze_queue_wait_timeout(struct request_queue *q, | |
108 | unsigned long timeout) | |
109 | { | |
110 | return wait_event_timeout(q->mq_freeze_wq, | |
111 | percpu_ref_is_zero(&q->q_usage_counter), | |
112 | timeout); | |
113 | } | |
114 | EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout); | |
115 | ||
116 | /* | |
117 | * Guarantee no request is in use, so we can change any data structure of | |
118 | * the queue afterward. | |
119 | */ | |
120 | void blk_freeze_queue(struct request_queue *q) | |
121 | { | |
122 | /* | |
123 | * In the !blk_mq case we are only calling this to kill the | |
124 | * q_usage_counter, otherwise this increases the freeze depth | |
125 | * and waits for it to return to zero. For this reason there is | |
126 | * no blk_unfreeze_queue(), and blk_freeze_queue() is not | |
127 | * exported to drivers as the only user for unfreeze is blk_mq. | |
128 | */ | |
129 | blk_freeze_queue_start(q); | |
130 | blk_mq_freeze_queue_wait(q); | |
131 | } | |
132 | ||
133 | void blk_mq_freeze_queue(struct request_queue *q) | |
134 | { | |
135 | /* | |
136 | * ...just an alias to keep freeze and unfreeze actions balanced | |
137 | * in the blk_mq_* namespace | |
138 | */ | |
139 | blk_freeze_queue(q); | |
140 | } | |
141 | EXPORT_SYMBOL_GPL(blk_mq_freeze_queue); | |
142 | ||
143 | void blk_mq_unfreeze_queue(struct request_queue *q) | |
144 | { | |
145 | int freeze_depth; | |
146 | ||
147 | freeze_depth = atomic_dec_return(&q->mq_freeze_depth); | |
148 | WARN_ON_ONCE(freeze_depth < 0); | |
149 | if (!freeze_depth) { | |
150 | percpu_ref_reinit(&q->q_usage_counter); | |
151 | wake_up_all(&q->mq_freeze_wq); | |
152 | } | |
153 | } | |
154 | EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue); | |
155 | ||
156 | /** | |
157 | * blk_mq_quiesce_queue() - wait until all ongoing queue_rq calls have finished | |
158 | * @q: request queue. | |
159 | * | |
160 | * Note: this function does not prevent that the struct request end_io() | |
161 | * callback function is invoked. Additionally, it is not prevented that | |
162 | * new queue_rq() calls occur unless the queue has been stopped first. | |
163 | */ | |
164 | void blk_mq_quiesce_queue(struct request_queue *q) | |
165 | { | |
166 | struct blk_mq_hw_ctx *hctx; | |
167 | unsigned int i; | |
168 | bool rcu = false; | |
169 | ||
170 | __blk_mq_stop_hw_queues(q, true); | |
171 | ||
172 | queue_for_each_hw_ctx(q, hctx, i) { | |
173 | if (hctx->flags & BLK_MQ_F_BLOCKING) | |
174 | synchronize_srcu(&hctx->queue_rq_srcu); | |
175 | else | |
176 | rcu = true; | |
177 | } | |
178 | if (rcu) | |
179 | synchronize_rcu(); | |
180 | } | |
181 | EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue); | |
182 | ||
183 | void blk_mq_wake_waiters(struct request_queue *q) | |
184 | { | |
185 | struct blk_mq_hw_ctx *hctx; | |
186 | unsigned int i; | |
187 | ||
188 | queue_for_each_hw_ctx(q, hctx, i) | |
189 | if (blk_mq_hw_queue_mapped(hctx)) | |
190 | blk_mq_tag_wakeup_all(hctx->tags, true); | |
191 | ||
192 | /* | |
193 | * If we are called because the queue has now been marked as | |
194 | * dying, we need to ensure that processes currently waiting on | |
195 | * the queue are notified as well. | |
196 | */ | |
197 | wake_up_all(&q->mq_freeze_wq); | |
198 | } | |
199 | ||
200 | bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx) | |
201 | { | |
202 | return blk_mq_has_free_tags(hctx->tags); | |
203 | } | |
204 | EXPORT_SYMBOL(blk_mq_can_queue); | |
205 | ||
206 | void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx, | |
207 | struct request *rq, unsigned int op) | |
208 | { | |
209 | INIT_LIST_HEAD(&rq->queuelist); | |
210 | /* csd/requeue_work/fifo_time is initialized before use */ | |
211 | rq->q = q; | |
212 | rq->mq_ctx = ctx; | |
213 | rq->cmd_flags = op; | |
214 | if (blk_queue_io_stat(q)) | |
215 | rq->rq_flags |= RQF_IO_STAT; | |
216 | /* do not touch atomic flags, it needs atomic ops against the timer */ | |
217 | rq->cpu = -1; | |
218 | INIT_HLIST_NODE(&rq->hash); | |
219 | RB_CLEAR_NODE(&rq->rb_node); | |
220 | rq->rq_disk = NULL; | |
221 | rq->part = NULL; | |
222 | rq->start_time = jiffies; | |
223 | #ifdef CONFIG_BLK_CGROUP | |
224 | rq->rl = NULL; | |
225 | set_start_time_ns(rq); | |
226 | rq->io_start_time_ns = 0; | |
227 | #endif | |
228 | rq->nr_phys_segments = 0; | |
229 | #if defined(CONFIG_BLK_DEV_INTEGRITY) | |
230 | rq->nr_integrity_segments = 0; | |
231 | #endif | |
232 | rq->special = NULL; | |
233 | /* tag was already set */ | |
234 | rq->extra_len = 0; | |
235 | ||
236 | INIT_LIST_HEAD(&rq->timeout_list); | |
237 | rq->timeout = 0; | |
238 | ||
239 | rq->end_io = NULL; | |
240 | rq->end_io_data = NULL; | |
241 | rq->next_rq = NULL; | |
242 | ||
243 | ctx->rq_dispatched[op_is_sync(op)]++; | |
244 | } | |
245 | EXPORT_SYMBOL_GPL(blk_mq_rq_ctx_init); | |
246 | ||
247 | struct request *__blk_mq_alloc_request(struct blk_mq_alloc_data *data, | |
248 | unsigned int op) | |
249 | { | |
250 | struct request *rq; | |
251 | unsigned int tag; | |
252 | ||
253 | tag = blk_mq_get_tag(data); | |
254 | if (tag != BLK_MQ_TAG_FAIL) { | |
255 | struct blk_mq_tags *tags = blk_mq_tags_from_data(data); | |
256 | ||
257 | rq = tags->static_rqs[tag]; | |
258 | ||
259 | if (data->flags & BLK_MQ_REQ_INTERNAL) { | |
260 | rq->tag = -1; | |
261 | rq->internal_tag = tag; | |
262 | } else { | |
263 | if (blk_mq_tag_busy(data->hctx)) { | |
264 | rq->rq_flags = RQF_MQ_INFLIGHT; | |
265 | atomic_inc(&data->hctx->nr_active); | |
266 | } | |
267 | rq->tag = tag; | |
268 | rq->internal_tag = -1; | |
269 | data->hctx->tags->rqs[rq->tag] = rq; | |
270 | } | |
271 | ||
272 | blk_mq_rq_ctx_init(data->q, data->ctx, rq, op); | |
273 | return rq; | |
274 | } | |
275 | ||
276 | return NULL; | |
277 | } | |
278 | EXPORT_SYMBOL_GPL(__blk_mq_alloc_request); | |
279 | ||
280 | struct request *blk_mq_alloc_request(struct request_queue *q, int rw, | |
281 | unsigned int flags) | |
282 | { | |
283 | struct blk_mq_alloc_data alloc_data = { .flags = flags }; | |
284 | struct request *rq; | |
285 | int ret; | |
286 | ||
287 | ret = blk_queue_enter(q, flags & BLK_MQ_REQ_NOWAIT); | |
288 | if (ret) | |
289 | return ERR_PTR(ret); | |
290 | ||
291 | rq = blk_mq_sched_get_request(q, NULL, rw, &alloc_data); | |
292 | ||
293 | blk_mq_put_ctx(alloc_data.ctx); | |
294 | blk_queue_exit(q); | |
295 | ||
296 | if (!rq) | |
297 | return ERR_PTR(-EWOULDBLOCK); | |
298 | ||
299 | rq->__data_len = 0; | |
300 | rq->__sector = (sector_t) -1; | |
301 | rq->bio = rq->biotail = NULL; | |
302 | return rq; | |
303 | } | |
304 | EXPORT_SYMBOL(blk_mq_alloc_request); | |
305 | ||
306 | struct request *blk_mq_alloc_request_hctx(struct request_queue *q, int rw, | |
307 | unsigned int flags, unsigned int hctx_idx) | |
308 | { | |
309 | struct blk_mq_alloc_data alloc_data = { .flags = flags }; | |
310 | struct request *rq; | |
311 | unsigned int cpu; | |
312 | int ret; | |
313 | ||
314 | /* | |
315 | * If the tag allocator sleeps we could get an allocation for a | |
316 | * different hardware context. No need to complicate the low level | |
317 | * allocator for this for the rare use case of a command tied to | |
318 | * a specific queue. | |
319 | */ | |
320 | if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT))) | |
321 | return ERR_PTR(-EINVAL); | |
322 | ||
323 | if (hctx_idx >= q->nr_hw_queues) | |
324 | return ERR_PTR(-EIO); | |
325 | ||
326 | ret = blk_queue_enter(q, true); | |
327 | if (ret) | |
328 | return ERR_PTR(ret); | |
329 | ||
330 | /* | |
331 | * Check if the hardware context is actually mapped to anything. | |
332 | * If not tell the caller that it should skip this queue. | |
333 | */ | |
334 | alloc_data.hctx = q->queue_hw_ctx[hctx_idx]; | |
335 | if (!blk_mq_hw_queue_mapped(alloc_data.hctx)) { | |
336 | blk_queue_exit(q); | |
337 | return ERR_PTR(-EXDEV); | |
338 | } | |
339 | cpu = cpumask_first(alloc_data.hctx->cpumask); | |
340 | alloc_data.ctx = __blk_mq_get_ctx(q, cpu); | |
341 | ||
342 | rq = blk_mq_sched_get_request(q, NULL, rw, &alloc_data); | |
343 | ||
344 | blk_queue_exit(q); | |
345 | ||
346 | if (!rq) | |
347 | return ERR_PTR(-EWOULDBLOCK); | |
348 | ||
349 | return rq; | |
350 | } | |
351 | EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx); | |
352 | ||
353 | void __blk_mq_finish_request(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx, | |
354 | struct request *rq) | |
355 | { | |
356 | const int sched_tag = rq->internal_tag; | |
357 | struct request_queue *q = rq->q; | |
358 | ||
359 | if (rq->rq_flags & RQF_MQ_INFLIGHT) | |
360 | atomic_dec(&hctx->nr_active); | |
361 | ||
362 | wbt_done(q->rq_wb, &rq->issue_stat); | |
363 | rq->rq_flags = 0; | |
364 | ||
365 | clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags); | |
366 | clear_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags); | |
367 | if (rq->tag != -1) | |
368 | blk_mq_put_tag(hctx, hctx->tags, ctx, rq->tag); | |
369 | if (sched_tag != -1) | |
370 | blk_mq_put_tag(hctx, hctx->sched_tags, ctx, sched_tag); | |
371 | blk_mq_sched_restart(hctx); | |
372 | blk_queue_exit(q); | |
373 | } | |
374 | ||
375 | static void blk_mq_finish_hctx_request(struct blk_mq_hw_ctx *hctx, | |
376 | struct request *rq) | |
377 | { | |
378 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
379 | ||
380 | ctx->rq_completed[rq_is_sync(rq)]++; | |
381 | __blk_mq_finish_request(hctx, ctx, rq); | |
382 | } | |
383 | ||
384 | void blk_mq_finish_request(struct request *rq) | |
385 | { | |
386 | blk_mq_finish_hctx_request(blk_mq_map_queue(rq->q, rq->mq_ctx->cpu), rq); | |
387 | } | |
388 | EXPORT_SYMBOL_GPL(blk_mq_finish_request); | |
389 | ||
390 | void blk_mq_free_request(struct request *rq) | |
391 | { | |
392 | blk_mq_sched_put_request(rq); | |
393 | } | |
394 | EXPORT_SYMBOL_GPL(blk_mq_free_request); | |
395 | ||
396 | inline void __blk_mq_end_request(struct request *rq, int error) | |
397 | { | |
398 | blk_account_io_done(rq); | |
399 | ||
400 | if (rq->end_io) { | |
401 | wbt_done(rq->q->rq_wb, &rq->issue_stat); | |
402 | rq->end_io(rq, error); | |
403 | } else { | |
404 | if (unlikely(blk_bidi_rq(rq))) | |
405 | blk_mq_free_request(rq->next_rq); | |
406 | blk_mq_free_request(rq); | |
407 | } | |
408 | } | |
409 | EXPORT_SYMBOL(__blk_mq_end_request); | |
410 | ||
411 | void blk_mq_end_request(struct request *rq, int error) | |
412 | { | |
413 | if (blk_update_request(rq, error, blk_rq_bytes(rq))) | |
414 | BUG(); | |
415 | __blk_mq_end_request(rq, error); | |
416 | } | |
417 | EXPORT_SYMBOL(blk_mq_end_request); | |
418 | ||
419 | static void __blk_mq_complete_request_remote(void *data) | |
420 | { | |
421 | struct request *rq = data; | |
422 | ||
423 | rq->q->softirq_done_fn(rq); | |
424 | } | |
425 | ||
426 | static void __blk_mq_complete_request(struct request *rq) | |
427 | { | |
428 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
429 | bool shared = false; | |
430 | int cpu; | |
431 | ||
432 | if (rq->internal_tag != -1) | |
433 | blk_mq_sched_completed_request(rq); | |
434 | if (rq->rq_flags & RQF_STATS) { | |
435 | blk_mq_poll_stats_start(rq->q); | |
436 | blk_stat_add(rq); | |
437 | } | |
438 | ||
439 | if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) { | |
440 | rq->q->softirq_done_fn(rq); | |
441 | return; | |
442 | } | |
443 | ||
444 | cpu = get_cpu(); | |
445 | if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags)) | |
446 | shared = cpus_share_cache(cpu, ctx->cpu); | |
447 | ||
448 | if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) { | |
449 | rq->csd.func = __blk_mq_complete_request_remote; | |
450 | rq->csd.info = rq; | |
451 | rq->csd.flags = 0; | |
452 | smp_call_function_single_async(ctx->cpu, &rq->csd); | |
453 | } else { | |
454 | rq->q->softirq_done_fn(rq); | |
455 | } | |
456 | put_cpu(); | |
457 | } | |
458 | ||
459 | /** | |
460 | * blk_mq_complete_request - end I/O on a request | |
461 | * @rq: the request being processed | |
462 | * | |
463 | * Description: | |
464 | * Ends all I/O on a request. It does not handle partial completions. | |
465 | * The actual completion happens out-of-order, through a IPI handler. | |
466 | **/ | |
467 | void blk_mq_complete_request(struct request *rq) | |
468 | { | |
469 | struct request_queue *q = rq->q; | |
470 | ||
471 | if (unlikely(blk_should_fake_timeout(q))) | |
472 | return; | |
473 | if (!blk_mark_rq_complete(rq)) | |
474 | __blk_mq_complete_request(rq); | |
475 | } | |
476 | EXPORT_SYMBOL(blk_mq_complete_request); | |
477 | ||
478 | int blk_mq_request_started(struct request *rq) | |
479 | { | |
480 | return test_bit(REQ_ATOM_STARTED, &rq->atomic_flags); | |
481 | } | |
482 | EXPORT_SYMBOL_GPL(blk_mq_request_started); | |
483 | ||
484 | void blk_mq_start_request(struct request *rq) | |
485 | { | |
486 | struct request_queue *q = rq->q; | |
487 | ||
488 | blk_mq_sched_started_request(rq); | |
489 | ||
490 | trace_block_rq_issue(q, rq); | |
491 | ||
492 | if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) { | |
493 | blk_stat_set_issue(&rq->issue_stat, blk_rq_sectors(rq)); | |
494 | rq->rq_flags |= RQF_STATS; | |
495 | wbt_issue(q->rq_wb, &rq->issue_stat); | |
496 | } | |
497 | ||
498 | blk_add_timer(rq); | |
499 | ||
500 | /* | |
501 | * Ensure that ->deadline is visible before set the started | |
502 | * flag and clear the completed flag. | |
503 | */ | |
504 | smp_mb__before_atomic(); | |
505 | ||
506 | /* | |
507 | * Mark us as started and clear complete. Complete might have been | |
508 | * set if requeue raced with timeout, which then marked it as | |
509 | * complete. So be sure to clear complete again when we start | |
510 | * the request, otherwise we'll ignore the completion event. | |
511 | */ | |
512 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) | |
513 | set_bit(REQ_ATOM_STARTED, &rq->atomic_flags); | |
514 | if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags)) | |
515 | clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags); | |
516 | ||
517 | if (q->dma_drain_size && blk_rq_bytes(rq)) { | |
518 | /* | |
519 | * Make sure space for the drain appears. We know we can do | |
520 | * this because max_hw_segments has been adjusted to be one | |
521 | * fewer than the device can handle. | |
522 | */ | |
523 | rq->nr_phys_segments++; | |
524 | } | |
525 | } | |
526 | EXPORT_SYMBOL(blk_mq_start_request); | |
527 | ||
528 | /* | |
529 | * When we reach here because queue is busy, REQ_ATOM_COMPLETE | |
530 | * flag isn't set yet, so there may be race with timeout handler, | |
531 | * but given rq->deadline is just set in .queue_rq() under | |
532 | * this situation, the race won't be possible in reality because | |
533 | * rq->timeout should be set as big enough to cover the window | |
534 | * between blk_mq_start_request() called from .queue_rq() and | |
535 | * clearing REQ_ATOM_STARTED here. | |
536 | */ | |
537 | static void __blk_mq_requeue_request(struct request *rq) | |
538 | { | |
539 | struct request_queue *q = rq->q; | |
540 | ||
541 | trace_block_rq_requeue(q, rq); | |
542 | wbt_requeue(q->rq_wb, &rq->issue_stat); | |
543 | blk_mq_sched_requeue_request(rq); | |
544 | ||
545 | if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) { | |
546 | if (q->dma_drain_size && blk_rq_bytes(rq)) | |
547 | rq->nr_phys_segments--; | |
548 | } | |
549 | } | |
550 | ||
551 | void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list) | |
552 | { | |
553 | __blk_mq_requeue_request(rq); | |
554 | ||
555 | BUG_ON(blk_queued_rq(rq)); | |
556 | blk_mq_add_to_requeue_list(rq, true, kick_requeue_list); | |
557 | } | |
558 | EXPORT_SYMBOL(blk_mq_requeue_request); | |
559 | ||
560 | static void blk_mq_requeue_work(struct work_struct *work) | |
561 | { | |
562 | struct request_queue *q = | |
563 | container_of(work, struct request_queue, requeue_work.work); | |
564 | LIST_HEAD(rq_list); | |
565 | struct request *rq, *next; | |
566 | unsigned long flags; | |
567 | ||
568 | spin_lock_irqsave(&q->requeue_lock, flags); | |
569 | list_splice_init(&q->requeue_list, &rq_list); | |
570 | spin_unlock_irqrestore(&q->requeue_lock, flags); | |
571 | ||
572 | list_for_each_entry_safe(rq, next, &rq_list, queuelist) { | |
573 | if (!(rq->rq_flags & RQF_SOFTBARRIER)) | |
574 | continue; | |
575 | ||
576 | rq->rq_flags &= ~RQF_SOFTBARRIER; | |
577 | list_del_init(&rq->queuelist); | |
578 | blk_mq_sched_insert_request(rq, true, false, false, true); | |
579 | } | |
580 | ||
581 | while (!list_empty(&rq_list)) { | |
582 | rq = list_entry(rq_list.next, struct request, queuelist); | |
583 | list_del_init(&rq->queuelist); | |
584 | blk_mq_sched_insert_request(rq, false, false, false, true); | |
585 | } | |
586 | ||
587 | blk_mq_run_hw_queues(q, false); | |
588 | } | |
589 | ||
590 | void blk_mq_add_to_requeue_list(struct request *rq, bool at_head, | |
591 | bool kick_requeue_list) | |
592 | { | |
593 | struct request_queue *q = rq->q; | |
594 | unsigned long flags; | |
595 | ||
596 | /* | |
597 | * We abuse this flag that is otherwise used by the I/O scheduler to | |
598 | * request head insertation from the workqueue. | |
599 | */ | |
600 | BUG_ON(rq->rq_flags & RQF_SOFTBARRIER); | |
601 | ||
602 | spin_lock_irqsave(&q->requeue_lock, flags); | |
603 | if (at_head) { | |
604 | rq->rq_flags |= RQF_SOFTBARRIER; | |
605 | list_add(&rq->queuelist, &q->requeue_list); | |
606 | } else { | |
607 | list_add_tail(&rq->queuelist, &q->requeue_list); | |
608 | } | |
609 | spin_unlock_irqrestore(&q->requeue_lock, flags); | |
610 | ||
611 | if (kick_requeue_list) | |
612 | blk_mq_kick_requeue_list(q); | |
613 | } | |
614 | EXPORT_SYMBOL(blk_mq_add_to_requeue_list); | |
615 | ||
616 | void blk_mq_kick_requeue_list(struct request_queue *q) | |
617 | { | |
618 | kblockd_schedule_delayed_work(&q->requeue_work, 0); | |
619 | } | |
620 | EXPORT_SYMBOL(blk_mq_kick_requeue_list); | |
621 | ||
622 | void blk_mq_delay_kick_requeue_list(struct request_queue *q, | |
623 | unsigned long msecs) | |
624 | { | |
625 | kblockd_schedule_delayed_work(&q->requeue_work, | |
626 | msecs_to_jiffies(msecs)); | |
627 | } | |
628 | EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list); | |
629 | ||
630 | void blk_mq_abort_requeue_list(struct request_queue *q) | |
631 | { | |
632 | unsigned long flags; | |
633 | LIST_HEAD(rq_list); | |
634 | ||
635 | spin_lock_irqsave(&q->requeue_lock, flags); | |
636 | list_splice_init(&q->requeue_list, &rq_list); | |
637 | spin_unlock_irqrestore(&q->requeue_lock, flags); | |
638 | ||
639 | while (!list_empty(&rq_list)) { | |
640 | struct request *rq; | |
641 | ||
642 | rq = list_first_entry(&rq_list, struct request, queuelist); | |
643 | list_del_init(&rq->queuelist); | |
644 | blk_mq_end_request(rq, -EIO); | |
645 | } | |
646 | } | |
647 | EXPORT_SYMBOL(blk_mq_abort_requeue_list); | |
648 | ||
649 | struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag) | |
650 | { | |
651 | if (tag < tags->nr_tags) { | |
652 | prefetch(tags->rqs[tag]); | |
653 | return tags->rqs[tag]; | |
654 | } | |
655 | ||
656 | return NULL; | |
657 | } | |
658 | EXPORT_SYMBOL(blk_mq_tag_to_rq); | |
659 | ||
660 | struct blk_mq_timeout_data { | |
661 | unsigned long next; | |
662 | unsigned int next_set; | |
663 | }; | |
664 | ||
665 | void blk_mq_rq_timed_out(struct request *req, bool reserved) | |
666 | { | |
667 | const struct blk_mq_ops *ops = req->q->mq_ops; | |
668 | enum blk_eh_timer_return ret = BLK_EH_RESET_TIMER; | |
669 | ||
670 | /* | |
671 | * We know that complete is set at this point. If STARTED isn't set | |
672 | * anymore, then the request isn't active and the "timeout" should | |
673 | * just be ignored. This can happen due to the bitflag ordering. | |
674 | * Timeout first checks if STARTED is set, and if it is, assumes | |
675 | * the request is active. But if we race with completion, then | |
676 | * both flags will get cleared. So check here again, and ignore | |
677 | * a timeout event with a request that isn't active. | |
678 | */ | |
679 | if (!test_bit(REQ_ATOM_STARTED, &req->atomic_flags)) | |
680 | return; | |
681 | ||
682 | if (ops->timeout) | |
683 | ret = ops->timeout(req, reserved); | |
684 | ||
685 | switch (ret) { | |
686 | case BLK_EH_HANDLED: | |
687 | __blk_mq_complete_request(req); | |
688 | break; | |
689 | case BLK_EH_RESET_TIMER: | |
690 | blk_add_timer(req); | |
691 | blk_clear_rq_complete(req); | |
692 | break; | |
693 | case BLK_EH_NOT_HANDLED: | |
694 | break; | |
695 | default: | |
696 | printk(KERN_ERR "block: bad eh return: %d\n", ret); | |
697 | break; | |
698 | } | |
699 | } | |
700 | ||
701 | static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx, | |
702 | struct request *rq, void *priv, bool reserved) | |
703 | { | |
704 | struct blk_mq_timeout_data *data = priv; | |
705 | ||
706 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) | |
707 | return; | |
708 | ||
709 | /* | |
710 | * The rq being checked may have been freed and reallocated | |
711 | * out already here, we avoid this race by checking rq->deadline | |
712 | * and REQ_ATOM_COMPLETE flag together: | |
713 | * | |
714 | * - if rq->deadline is observed as new value because of | |
715 | * reusing, the rq won't be timed out because of timing. | |
716 | * - if rq->deadline is observed as previous value, | |
717 | * REQ_ATOM_COMPLETE flag won't be cleared in reuse path | |
718 | * because we put a barrier between setting rq->deadline | |
719 | * and clearing the flag in blk_mq_start_request(), so | |
720 | * this rq won't be timed out too. | |
721 | */ | |
722 | if (time_after_eq(jiffies, rq->deadline)) { | |
723 | if (!blk_mark_rq_complete(rq)) | |
724 | blk_mq_rq_timed_out(rq, reserved); | |
725 | } else if (!data->next_set || time_after(data->next, rq->deadline)) { | |
726 | data->next = rq->deadline; | |
727 | data->next_set = 1; | |
728 | } | |
729 | } | |
730 | ||
731 | static void blk_mq_timeout_work(struct work_struct *work) | |
732 | { | |
733 | struct request_queue *q = | |
734 | container_of(work, struct request_queue, timeout_work); | |
735 | struct blk_mq_timeout_data data = { | |
736 | .next = 0, | |
737 | .next_set = 0, | |
738 | }; | |
739 | int i; | |
740 | ||
741 | /* A deadlock might occur if a request is stuck requiring a | |
742 | * timeout at the same time a queue freeze is waiting | |
743 | * completion, since the timeout code would not be able to | |
744 | * acquire the queue reference here. | |
745 | * | |
746 | * That's why we don't use blk_queue_enter here; instead, we use | |
747 | * percpu_ref_tryget directly, because we need to be able to | |
748 | * obtain a reference even in the short window between the queue | |
749 | * starting to freeze, by dropping the first reference in | |
750 | * blk_freeze_queue_start, and the moment the last request is | |
751 | * consumed, marked by the instant q_usage_counter reaches | |
752 | * zero. | |
753 | */ | |
754 | if (!percpu_ref_tryget(&q->q_usage_counter)) | |
755 | return; | |
756 | ||
757 | blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &data); | |
758 | ||
759 | if (data.next_set) { | |
760 | data.next = blk_rq_timeout(round_jiffies_up(data.next)); | |
761 | mod_timer(&q->timeout, data.next); | |
762 | } else { | |
763 | struct blk_mq_hw_ctx *hctx; | |
764 | ||
765 | queue_for_each_hw_ctx(q, hctx, i) { | |
766 | /* the hctx may be unmapped, so check it here */ | |
767 | if (blk_mq_hw_queue_mapped(hctx)) | |
768 | blk_mq_tag_idle(hctx); | |
769 | } | |
770 | } | |
771 | blk_queue_exit(q); | |
772 | } | |
773 | ||
774 | /* | |
775 | * Reverse check our software queue for entries that we could potentially | |
776 | * merge with. Currently includes a hand-wavy stop count of 8, to not spend | |
777 | * too much time checking for merges. | |
778 | */ | |
779 | static bool blk_mq_attempt_merge(struct request_queue *q, | |
780 | struct blk_mq_ctx *ctx, struct bio *bio) | |
781 | { | |
782 | struct request *rq; | |
783 | int checked = 8; | |
784 | ||
785 | list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) { | |
786 | bool merged = false; | |
787 | ||
788 | if (!checked--) | |
789 | break; | |
790 | ||
791 | if (!blk_rq_merge_ok(rq, bio)) | |
792 | continue; | |
793 | ||
794 | switch (blk_try_merge(rq, bio)) { | |
795 | case ELEVATOR_BACK_MERGE: | |
796 | if (blk_mq_sched_allow_merge(q, rq, bio)) | |
797 | merged = bio_attempt_back_merge(q, rq, bio); | |
798 | break; | |
799 | case ELEVATOR_FRONT_MERGE: | |
800 | if (blk_mq_sched_allow_merge(q, rq, bio)) | |
801 | merged = bio_attempt_front_merge(q, rq, bio); | |
802 | break; | |
803 | case ELEVATOR_DISCARD_MERGE: | |
804 | merged = bio_attempt_discard_merge(q, rq, bio); | |
805 | break; | |
806 | default: | |
807 | continue; | |
808 | } | |
809 | ||
810 | if (merged) | |
811 | ctx->rq_merged++; | |
812 | return merged; | |
813 | } | |
814 | ||
815 | return false; | |
816 | } | |
817 | ||
818 | struct flush_busy_ctx_data { | |
819 | struct blk_mq_hw_ctx *hctx; | |
820 | struct list_head *list; | |
821 | }; | |
822 | ||
823 | static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data) | |
824 | { | |
825 | struct flush_busy_ctx_data *flush_data = data; | |
826 | struct blk_mq_hw_ctx *hctx = flush_data->hctx; | |
827 | struct blk_mq_ctx *ctx = hctx->ctxs[bitnr]; | |
828 | ||
829 | sbitmap_clear_bit(sb, bitnr); | |
830 | spin_lock(&ctx->lock); | |
831 | list_splice_tail_init(&ctx->rq_list, flush_data->list); | |
832 | spin_unlock(&ctx->lock); | |
833 | return true; | |
834 | } | |
835 | ||
836 | /* | |
837 | * Process software queues that have been marked busy, splicing them | |
838 | * to the for-dispatch | |
839 | */ | |
840 | void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list) | |
841 | { | |
842 | struct flush_busy_ctx_data data = { | |
843 | .hctx = hctx, | |
844 | .list = list, | |
845 | }; | |
846 | ||
847 | sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data); | |
848 | } | |
849 | EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs); | |
850 | ||
851 | static inline unsigned int queued_to_index(unsigned int queued) | |
852 | { | |
853 | if (!queued) | |
854 | return 0; | |
855 | ||
856 | return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1); | |
857 | } | |
858 | ||
859 | bool blk_mq_get_driver_tag(struct request *rq, struct blk_mq_hw_ctx **hctx, | |
860 | bool wait) | |
861 | { | |
862 | struct blk_mq_alloc_data data = { | |
863 | .q = rq->q, | |
864 | .hctx = blk_mq_map_queue(rq->q, rq->mq_ctx->cpu), | |
865 | .flags = wait ? 0 : BLK_MQ_REQ_NOWAIT, | |
866 | }; | |
867 | ||
868 | might_sleep_if(wait); | |
869 | ||
870 | if (rq->tag != -1) | |
871 | goto done; | |
872 | ||
873 | if (blk_mq_tag_is_reserved(data.hctx->sched_tags, rq->internal_tag)) | |
874 | data.flags |= BLK_MQ_REQ_RESERVED; | |
875 | ||
876 | rq->tag = blk_mq_get_tag(&data); | |
877 | if (rq->tag >= 0) { | |
878 | if (blk_mq_tag_busy(data.hctx)) { | |
879 | rq->rq_flags |= RQF_MQ_INFLIGHT; | |
880 | atomic_inc(&data.hctx->nr_active); | |
881 | } | |
882 | data.hctx->tags->rqs[rq->tag] = rq; | |
883 | } | |
884 | ||
885 | done: | |
886 | if (hctx) | |
887 | *hctx = data.hctx; | |
888 | return rq->tag != -1; | |
889 | } | |
890 | ||
891 | static void __blk_mq_put_driver_tag(struct blk_mq_hw_ctx *hctx, | |
892 | struct request *rq) | |
893 | { | |
894 | blk_mq_put_tag(hctx, hctx->tags, rq->mq_ctx, rq->tag); | |
895 | rq->tag = -1; | |
896 | ||
897 | if (rq->rq_flags & RQF_MQ_INFLIGHT) { | |
898 | rq->rq_flags &= ~RQF_MQ_INFLIGHT; | |
899 | atomic_dec(&hctx->nr_active); | |
900 | } | |
901 | } | |
902 | ||
903 | static void blk_mq_put_driver_tag_hctx(struct blk_mq_hw_ctx *hctx, | |
904 | struct request *rq) | |
905 | { | |
906 | if (rq->tag == -1 || rq->internal_tag == -1) | |
907 | return; | |
908 | ||
909 | __blk_mq_put_driver_tag(hctx, rq); | |
910 | } | |
911 | ||
912 | static void blk_mq_put_driver_tag(struct request *rq) | |
913 | { | |
914 | struct blk_mq_hw_ctx *hctx; | |
915 | ||
916 | if (rq->tag == -1 || rq->internal_tag == -1) | |
917 | return; | |
918 | ||
919 | hctx = blk_mq_map_queue(rq->q, rq->mq_ctx->cpu); | |
920 | __blk_mq_put_driver_tag(hctx, rq); | |
921 | } | |
922 | ||
923 | /* | |
924 | * If we fail getting a driver tag because all the driver tags are already | |
925 | * assigned and on the dispatch list, BUT the first entry does not have a | |
926 | * tag, then we could deadlock. For that case, move entries with assigned | |
927 | * driver tags to the front, leaving the set of tagged requests in the | |
928 | * same order, and the untagged set in the same order. | |
929 | */ | |
930 | static bool reorder_tags_to_front(struct list_head *list) | |
931 | { | |
932 | struct request *rq, *tmp, *first = NULL; | |
933 | ||
934 | list_for_each_entry_safe_reverse(rq, tmp, list, queuelist) { | |
935 | if (rq == first) | |
936 | break; | |
937 | if (rq->tag != -1) { | |
938 | list_move(&rq->queuelist, list); | |
939 | if (!first) | |
940 | first = rq; | |
941 | } | |
942 | } | |
943 | ||
944 | return first != NULL; | |
945 | } | |
946 | ||
947 | static int blk_mq_dispatch_wake(wait_queue_t *wait, unsigned mode, int flags, | |
948 | void *key) | |
949 | { | |
950 | struct blk_mq_hw_ctx *hctx; | |
951 | ||
952 | hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait); | |
953 | ||
954 | list_del(&wait->task_list); | |
955 | clear_bit_unlock(BLK_MQ_S_TAG_WAITING, &hctx->state); | |
956 | blk_mq_run_hw_queue(hctx, true); | |
957 | return 1; | |
958 | } | |
959 | ||
960 | static bool blk_mq_dispatch_wait_add(struct blk_mq_hw_ctx *hctx) | |
961 | { | |
962 | struct sbq_wait_state *ws; | |
963 | ||
964 | /* | |
965 | * The TAG_WAITING bit serves as a lock protecting hctx->dispatch_wait. | |
966 | * The thread which wins the race to grab this bit adds the hardware | |
967 | * queue to the wait queue. | |
968 | */ | |
969 | if (test_bit(BLK_MQ_S_TAG_WAITING, &hctx->state) || | |
970 | test_and_set_bit_lock(BLK_MQ_S_TAG_WAITING, &hctx->state)) | |
971 | return false; | |
972 | ||
973 | init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake); | |
974 | ws = bt_wait_ptr(&hctx->tags->bitmap_tags, hctx); | |
975 | ||
976 | /* | |
977 | * As soon as this returns, it's no longer safe to fiddle with | |
978 | * hctx->dispatch_wait, since a completion can wake up the wait queue | |
979 | * and unlock the bit. | |
980 | */ | |
981 | add_wait_queue(&ws->wait, &hctx->dispatch_wait); | |
982 | return true; | |
983 | } | |
984 | ||
985 | bool blk_mq_dispatch_rq_list(struct request_queue *q, struct list_head *list) | |
986 | { | |
987 | struct blk_mq_hw_ctx *hctx; | |
988 | struct request *rq; | |
989 | int errors, queued, ret = BLK_MQ_RQ_QUEUE_OK; | |
990 | ||
991 | if (list_empty(list)) | |
992 | return false; | |
993 | ||
994 | /* | |
995 | * Now process all the entries, sending them to the driver. | |
996 | */ | |
997 | errors = queued = 0; | |
998 | do { | |
999 | struct blk_mq_queue_data bd; | |
1000 | ||
1001 | rq = list_first_entry(list, struct request, queuelist); | |
1002 | if (!blk_mq_get_driver_tag(rq, &hctx, false)) { | |
1003 | if (!queued && reorder_tags_to_front(list)) | |
1004 | continue; | |
1005 | ||
1006 | /* | |
1007 | * The initial allocation attempt failed, so we need to | |
1008 | * rerun the hardware queue when a tag is freed. | |
1009 | */ | |
1010 | if (!blk_mq_dispatch_wait_add(hctx)) | |
1011 | break; | |
1012 | ||
1013 | /* | |
1014 | * It's possible that a tag was freed in the window | |
1015 | * between the allocation failure and adding the | |
1016 | * hardware queue to the wait queue. | |
1017 | */ | |
1018 | if (!blk_mq_get_driver_tag(rq, &hctx, false)) | |
1019 | break; | |
1020 | } | |
1021 | ||
1022 | list_del_init(&rq->queuelist); | |
1023 | ||
1024 | bd.rq = rq; | |
1025 | ||
1026 | /* | |
1027 | * Flag last if we have no more requests, or if we have more | |
1028 | * but can't assign a driver tag to it. | |
1029 | */ | |
1030 | if (list_empty(list)) | |
1031 | bd.last = true; | |
1032 | else { | |
1033 | struct request *nxt; | |
1034 | ||
1035 | nxt = list_first_entry(list, struct request, queuelist); | |
1036 | bd.last = !blk_mq_get_driver_tag(nxt, NULL, false); | |
1037 | } | |
1038 | ||
1039 | ret = q->mq_ops->queue_rq(hctx, &bd); | |
1040 | switch (ret) { | |
1041 | case BLK_MQ_RQ_QUEUE_OK: | |
1042 | queued++; | |
1043 | break; | |
1044 | case BLK_MQ_RQ_QUEUE_BUSY: | |
1045 | blk_mq_put_driver_tag_hctx(hctx, rq); | |
1046 | list_add(&rq->queuelist, list); | |
1047 | __blk_mq_requeue_request(rq); | |
1048 | break; | |
1049 | default: | |
1050 | pr_err("blk-mq: bad return on queue: %d\n", ret); | |
1051 | case BLK_MQ_RQ_QUEUE_ERROR: | |
1052 | errors++; | |
1053 | blk_mq_end_request(rq, -EIO); | |
1054 | break; | |
1055 | } | |
1056 | ||
1057 | if (ret == BLK_MQ_RQ_QUEUE_BUSY) | |
1058 | break; | |
1059 | } while (!list_empty(list)); | |
1060 | ||
1061 | hctx->dispatched[queued_to_index(queued)]++; | |
1062 | ||
1063 | /* | |
1064 | * Any items that need requeuing? Stuff them into hctx->dispatch, | |
1065 | * that is where we will continue on next queue run. | |
1066 | */ | |
1067 | if (!list_empty(list)) { | |
1068 | /* | |
1069 | * If an I/O scheduler has been configured and we got a driver | |
1070 | * tag for the next request already, free it again. | |
1071 | */ | |
1072 | rq = list_first_entry(list, struct request, queuelist); | |
1073 | blk_mq_put_driver_tag(rq); | |
1074 | ||
1075 | spin_lock(&hctx->lock); | |
1076 | list_splice_init(list, &hctx->dispatch); | |
1077 | spin_unlock(&hctx->lock); | |
1078 | ||
1079 | /* | |
1080 | * If SCHED_RESTART was set by the caller of this function and | |
1081 | * it is no longer set that means that it was cleared by another | |
1082 | * thread and hence that a queue rerun is needed. | |
1083 | * | |
1084 | * If TAG_WAITING is set that means that an I/O scheduler has | |
1085 | * been configured and another thread is waiting for a driver | |
1086 | * tag. To guarantee fairness, do not rerun this hardware queue | |
1087 | * but let the other thread grab the driver tag. | |
1088 | * | |
1089 | * If no I/O scheduler has been configured it is possible that | |
1090 | * the hardware queue got stopped and restarted before requests | |
1091 | * were pushed back onto the dispatch list. Rerun the queue to | |
1092 | * avoid starvation. Notes: | |
1093 | * - blk_mq_run_hw_queue() checks whether or not a queue has | |
1094 | * been stopped before rerunning a queue. | |
1095 | * - Some but not all block drivers stop a queue before | |
1096 | * returning BLK_MQ_RQ_QUEUE_BUSY. Two exceptions are scsi-mq | |
1097 | * and dm-rq. | |
1098 | */ | |
1099 | if (!blk_mq_sched_needs_restart(hctx) && | |
1100 | !test_bit(BLK_MQ_S_TAG_WAITING, &hctx->state)) | |
1101 | blk_mq_run_hw_queue(hctx, true); | |
1102 | } | |
1103 | ||
1104 | return (queued + errors) != 0; | |
1105 | } | |
1106 | ||
1107 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) | |
1108 | { | |
1109 | int srcu_idx; | |
1110 | ||
1111 | WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) && | |
1112 | cpu_online(hctx->next_cpu)); | |
1113 | ||
1114 | if (!(hctx->flags & BLK_MQ_F_BLOCKING)) { | |
1115 | rcu_read_lock(); | |
1116 | blk_mq_sched_dispatch_requests(hctx); | |
1117 | rcu_read_unlock(); | |
1118 | } else { | |
1119 | might_sleep(); | |
1120 | ||
1121 | srcu_idx = srcu_read_lock(&hctx->queue_rq_srcu); | |
1122 | blk_mq_sched_dispatch_requests(hctx); | |
1123 | srcu_read_unlock(&hctx->queue_rq_srcu, srcu_idx); | |
1124 | } | |
1125 | } | |
1126 | ||
1127 | /* | |
1128 | * It'd be great if the workqueue API had a way to pass | |
1129 | * in a mask and had some smarts for more clever placement. | |
1130 | * For now we just round-robin here, switching for every | |
1131 | * BLK_MQ_CPU_WORK_BATCH queued items. | |
1132 | */ | |
1133 | static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) | |
1134 | { | |
1135 | if (hctx->queue->nr_hw_queues == 1) | |
1136 | return WORK_CPU_UNBOUND; | |
1137 | ||
1138 | if (--hctx->next_cpu_batch <= 0) { | |
1139 | int next_cpu; | |
1140 | ||
1141 | next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask); | |
1142 | if (next_cpu >= nr_cpu_ids) | |
1143 | next_cpu = cpumask_first(hctx->cpumask); | |
1144 | ||
1145 | hctx->next_cpu = next_cpu; | |
1146 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; | |
1147 | } | |
1148 | ||
1149 | return hctx->next_cpu; | |
1150 | } | |
1151 | ||
1152 | static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async, | |
1153 | unsigned long msecs) | |
1154 | { | |
1155 | if (unlikely(blk_mq_hctx_stopped(hctx) || | |
1156 | !blk_mq_hw_queue_mapped(hctx))) | |
1157 | return; | |
1158 | ||
1159 | if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) { | |
1160 | int cpu = get_cpu(); | |
1161 | if (cpumask_test_cpu(cpu, hctx->cpumask)) { | |
1162 | __blk_mq_run_hw_queue(hctx); | |
1163 | put_cpu(); | |
1164 | return; | |
1165 | } | |
1166 | ||
1167 | put_cpu(); | |
1168 | } | |
1169 | ||
1170 | kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx), | |
1171 | &hctx->run_work, | |
1172 | msecs_to_jiffies(msecs)); | |
1173 | } | |
1174 | ||
1175 | void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs) | |
1176 | { | |
1177 | __blk_mq_delay_run_hw_queue(hctx, true, msecs); | |
1178 | } | |
1179 | EXPORT_SYMBOL(blk_mq_delay_run_hw_queue); | |
1180 | ||
1181 | void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) | |
1182 | { | |
1183 | __blk_mq_delay_run_hw_queue(hctx, async, 0); | |
1184 | } | |
1185 | EXPORT_SYMBOL(blk_mq_run_hw_queue); | |
1186 | ||
1187 | void blk_mq_run_hw_queues(struct request_queue *q, bool async) | |
1188 | { | |
1189 | struct blk_mq_hw_ctx *hctx; | |
1190 | int i; | |
1191 | ||
1192 | queue_for_each_hw_ctx(q, hctx, i) { | |
1193 | if (!blk_mq_hctx_has_pending(hctx) || | |
1194 | blk_mq_hctx_stopped(hctx)) | |
1195 | continue; | |
1196 | ||
1197 | blk_mq_run_hw_queue(hctx, async); | |
1198 | } | |
1199 | } | |
1200 | EXPORT_SYMBOL(blk_mq_run_hw_queues); | |
1201 | ||
1202 | /** | |
1203 | * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped | |
1204 | * @q: request queue. | |
1205 | * | |
1206 | * The caller is responsible for serializing this function against | |
1207 | * blk_mq_{start,stop}_hw_queue(). | |
1208 | */ | |
1209 | bool blk_mq_queue_stopped(struct request_queue *q) | |
1210 | { | |
1211 | struct blk_mq_hw_ctx *hctx; | |
1212 | int i; | |
1213 | ||
1214 | queue_for_each_hw_ctx(q, hctx, i) | |
1215 | if (blk_mq_hctx_stopped(hctx)) | |
1216 | return true; | |
1217 | ||
1218 | return false; | |
1219 | } | |
1220 | EXPORT_SYMBOL(blk_mq_queue_stopped); | |
1221 | ||
1222 | static void __blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx, bool sync) | |
1223 | { | |
1224 | if (sync) | |
1225 | cancel_delayed_work_sync(&hctx->run_work); | |
1226 | else | |
1227 | cancel_delayed_work(&hctx->run_work); | |
1228 | ||
1229 | set_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
1230 | } | |
1231 | ||
1232 | void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx) | |
1233 | { | |
1234 | __blk_mq_stop_hw_queue(hctx, false); | |
1235 | } | |
1236 | EXPORT_SYMBOL(blk_mq_stop_hw_queue); | |
1237 | ||
1238 | void __blk_mq_stop_hw_queues(struct request_queue *q, bool sync) | |
1239 | { | |
1240 | struct blk_mq_hw_ctx *hctx; | |
1241 | int i; | |
1242 | ||
1243 | queue_for_each_hw_ctx(q, hctx, i) | |
1244 | __blk_mq_stop_hw_queue(hctx, sync); | |
1245 | } | |
1246 | ||
1247 | void blk_mq_stop_hw_queues(struct request_queue *q) | |
1248 | { | |
1249 | __blk_mq_stop_hw_queues(q, false); | |
1250 | } | |
1251 | EXPORT_SYMBOL(blk_mq_stop_hw_queues); | |
1252 | ||
1253 | void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx) | |
1254 | { | |
1255 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
1256 | ||
1257 | blk_mq_run_hw_queue(hctx, false); | |
1258 | } | |
1259 | EXPORT_SYMBOL(blk_mq_start_hw_queue); | |
1260 | ||
1261 | void blk_mq_start_hw_queues(struct request_queue *q) | |
1262 | { | |
1263 | struct blk_mq_hw_ctx *hctx; | |
1264 | int i; | |
1265 | ||
1266 | queue_for_each_hw_ctx(q, hctx, i) | |
1267 | blk_mq_start_hw_queue(hctx); | |
1268 | } | |
1269 | EXPORT_SYMBOL(blk_mq_start_hw_queues); | |
1270 | ||
1271 | void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) | |
1272 | { | |
1273 | if (!blk_mq_hctx_stopped(hctx)) | |
1274 | return; | |
1275 | ||
1276 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
1277 | blk_mq_run_hw_queue(hctx, async); | |
1278 | } | |
1279 | EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue); | |
1280 | ||
1281 | void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async) | |
1282 | { | |
1283 | struct blk_mq_hw_ctx *hctx; | |
1284 | int i; | |
1285 | ||
1286 | queue_for_each_hw_ctx(q, hctx, i) | |
1287 | blk_mq_start_stopped_hw_queue(hctx, async); | |
1288 | } | |
1289 | EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues); | |
1290 | ||
1291 | static void blk_mq_run_work_fn(struct work_struct *work) | |
1292 | { | |
1293 | struct blk_mq_hw_ctx *hctx; | |
1294 | ||
1295 | hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work); | |
1296 | ||
1297 | /* | |
1298 | * If we are stopped, don't run the queue. The exception is if | |
1299 | * BLK_MQ_S_START_ON_RUN is set. For that case, we auto-clear | |
1300 | * the STOPPED bit and run it. | |
1301 | */ | |
1302 | if (test_bit(BLK_MQ_S_STOPPED, &hctx->state)) { | |
1303 | if (!test_bit(BLK_MQ_S_START_ON_RUN, &hctx->state)) | |
1304 | return; | |
1305 | ||
1306 | clear_bit(BLK_MQ_S_START_ON_RUN, &hctx->state); | |
1307 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
1308 | } | |
1309 | ||
1310 | __blk_mq_run_hw_queue(hctx); | |
1311 | } | |
1312 | ||
1313 | ||
1314 | void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs) | |
1315 | { | |
1316 | if (unlikely(!blk_mq_hw_queue_mapped(hctx))) | |
1317 | return; | |
1318 | ||
1319 | /* | |
1320 | * Stop the hw queue, then modify currently delayed work. | |
1321 | * This should prevent us from running the queue prematurely. | |
1322 | * Mark the queue as auto-clearing STOPPED when it runs. | |
1323 | */ | |
1324 | blk_mq_stop_hw_queue(hctx); | |
1325 | set_bit(BLK_MQ_S_START_ON_RUN, &hctx->state); | |
1326 | kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), | |
1327 | &hctx->run_work, | |
1328 | msecs_to_jiffies(msecs)); | |
1329 | } | |
1330 | EXPORT_SYMBOL(blk_mq_delay_queue); | |
1331 | ||
1332 | static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx, | |
1333 | struct request *rq, | |
1334 | bool at_head) | |
1335 | { | |
1336 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
1337 | ||
1338 | trace_block_rq_insert(hctx->queue, rq); | |
1339 | ||
1340 | if (at_head) | |
1341 | list_add(&rq->queuelist, &ctx->rq_list); | |
1342 | else | |
1343 | list_add_tail(&rq->queuelist, &ctx->rq_list); | |
1344 | } | |
1345 | ||
1346 | void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq, | |
1347 | bool at_head) | |
1348 | { | |
1349 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
1350 | ||
1351 | __blk_mq_insert_req_list(hctx, rq, at_head); | |
1352 | blk_mq_hctx_mark_pending(hctx, ctx); | |
1353 | } | |
1354 | ||
1355 | void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx, | |
1356 | struct list_head *list) | |
1357 | ||
1358 | { | |
1359 | /* | |
1360 | * preemption doesn't flush plug list, so it's possible ctx->cpu is | |
1361 | * offline now | |
1362 | */ | |
1363 | spin_lock(&ctx->lock); | |
1364 | while (!list_empty(list)) { | |
1365 | struct request *rq; | |
1366 | ||
1367 | rq = list_first_entry(list, struct request, queuelist); | |
1368 | BUG_ON(rq->mq_ctx != ctx); | |
1369 | list_del_init(&rq->queuelist); | |
1370 | __blk_mq_insert_req_list(hctx, rq, false); | |
1371 | } | |
1372 | blk_mq_hctx_mark_pending(hctx, ctx); | |
1373 | spin_unlock(&ctx->lock); | |
1374 | } | |
1375 | ||
1376 | static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b) | |
1377 | { | |
1378 | struct request *rqa = container_of(a, struct request, queuelist); | |
1379 | struct request *rqb = container_of(b, struct request, queuelist); | |
1380 | ||
1381 | return !(rqa->mq_ctx < rqb->mq_ctx || | |
1382 | (rqa->mq_ctx == rqb->mq_ctx && | |
1383 | blk_rq_pos(rqa) < blk_rq_pos(rqb))); | |
1384 | } | |
1385 | ||
1386 | void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) | |
1387 | { | |
1388 | struct blk_mq_ctx *this_ctx; | |
1389 | struct request_queue *this_q; | |
1390 | struct request *rq; | |
1391 | LIST_HEAD(list); | |
1392 | LIST_HEAD(ctx_list); | |
1393 | unsigned int depth; | |
1394 | ||
1395 | list_splice_init(&plug->mq_list, &list); | |
1396 | ||
1397 | list_sort(NULL, &list, plug_ctx_cmp); | |
1398 | ||
1399 | this_q = NULL; | |
1400 | this_ctx = NULL; | |
1401 | depth = 0; | |
1402 | ||
1403 | while (!list_empty(&list)) { | |
1404 | rq = list_entry_rq(list.next); | |
1405 | list_del_init(&rq->queuelist); | |
1406 | BUG_ON(!rq->q); | |
1407 | if (rq->mq_ctx != this_ctx) { | |
1408 | if (this_ctx) { | |
1409 | trace_block_unplug(this_q, depth, from_schedule); | |
1410 | blk_mq_sched_insert_requests(this_q, this_ctx, | |
1411 | &ctx_list, | |
1412 | from_schedule); | |
1413 | } | |
1414 | ||
1415 | this_ctx = rq->mq_ctx; | |
1416 | this_q = rq->q; | |
1417 | depth = 0; | |
1418 | } | |
1419 | ||
1420 | depth++; | |
1421 | list_add_tail(&rq->queuelist, &ctx_list); | |
1422 | } | |
1423 | ||
1424 | /* | |
1425 | * If 'this_ctx' is set, we know we have entries to complete | |
1426 | * on 'ctx_list'. Do those. | |
1427 | */ | |
1428 | if (this_ctx) { | |
1429 | trace_block_unplug(this_q, depth, from_schedule); | |
1430 | blk_mq_sched_insert_requests(this_q, this_ctx, &ctx_list, | |
1431 | from_schedule); | |
1432 | } | |
1433 | } | |
1434 | ||
1435 | static void blk_mq_bio_to_request(struct request *rq, struct bio *bio) | |
1436 | { | |
1437 | blk_init_request_from_bio(rq, bio); | |
1438 | ||
1439 | blk_account_io_start(rq, true); | |
1440 | } | |
1441 | ||
1442 | static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx) | |
1443 | { | |
1444 | return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) && | |
1445 | !blk_queue_nomerges(hctx->queue); | |
1446 | } | |
1447 | ||
1448 | static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx, | |
1449 | struct blk_mq_ctx *ctx, | |
1450 | struct request *rq, struct bio *bio) | |
1451 | { | |
1452 | if (!hctx_allow_merges(hctx) || !bio_mergeable(bio)) { | |
1453 | blk_mq_bio_to_request(rq, bio); | |
1454 | spin_lock(&ctx->lock); | |
1455 | insert_rq: | |
1456 | __blk_mq_insert_request(hctx, rq, false); | |
1457 | spin_unlock(&ctx->lock); | |
1458 | return false; | |
1459 | } else { | |
1460 | struct request_queue *q = hctx->queue; | |
1461 | ||
1462 | spin_lock(&ctx->lock); | |
1463 | if (!blk_mq_attempt_merge(q, ctx, bio)) { | |
1464 | blk_mq_bio_to_request(rq, bio); | |
1465 | goto insert_rq; | |
1466 | } | |
1467 | ||
1468 | spin_unlock(&ctx->lock); | |
1469 | __blk_mq_finish_request(hctx, ctx, rq); | |
1470 | return true; | |
1471 | } | |
1472 | } | |
1473 | ||
1474 | static blk_qc_t request_to_qc_t(struct blk_mq_hw_ctx *hctx, struct request *rq) | |
1475 | { | |
1476 | if (rq->tag != -1) | |
1477 | return blk_tag_to_qc_t(rq->tag, hctx->queue_num, false); | |
1478 | ||
1479 | return blk_tag_to_qc_t(rq->internal_tag, hctx->queue_num, true); | |
1480 | } | |
1481 | ||
1482 | static void __blk_mq_try_issue_directly(struct request *rq, blk_qc_t *cookie, | |
1483 | bool may_sleep) | |
1484 | { | |
1485 | struct request_queue *q = rq->q; | |
1486 | struct blk_mq_queue_data bd = { | |
1487 | .rq = rq, | |
1488 | .last = true, | |
1489 | }; | |
1490 | struct blk_mq_hw_ctx *hctx; | |
1491 | blk_qc_t new_cookie; | |
1492 | int ret; | |
1493 | ||
1494 | if (q->elevator) | |
1495 | goto insert; | |
1496 | ||
1497 | if (!blk_mq_get_driver_tag(rq, &hctx, false)) | |
1498 | goto insert; | |
1499 | ||
1500 | new_cookie = request_to_qc_t(hctx, rq); | |
1501 | ||
1502 | /* | |
1503 | * For OK queue, we are done. For error, kill it. Any other | |
1504 | * error (busy), just add it to our list as we previously | |
1505 | * would have done | |
1506 | */ | |
1507 | ret = q->mq_ops->queue_rq(hctx, &bd); | |
1508 | if (ret == BLK_MQ_RQ_QUEUE_OK) { | |
1509 | *cookie = new_cookie; | |
1510 | return; | |
1511 | } | |
1512 | ||
1513 | if (ret == BLK_MQ_RQ_QUEUE_ERROR) { | |
1514 | *cookie = BLK_QC_T_NONE; | |
1515 | blk_mq_end_request(rq, -EIO); | |
1516 | return; | |
1517 | } | |
1518 | ||
1519 | __blk_mq_requeue_request(rq); | |
1520 | insert: | |
1521 | blk_mq_sched_insert_request(rq, false, true, false, may_sleep); | |
1522 | } | |
1523 | ||
1524 | static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, | |
1525 | struct request *rq, blk_qc_t *cookie) | |
1526 | { | |
1527 | if (!(hctx->flags & BLK_MQ_F_BLOCKING)) { | |
1528 | rcu_read_lock(); | |
1529 | __blk_mq_try_issue_directly(rq, cookie, false); | |
1530 | rcu_read_unlock(); | |
1531 | } else { | |
1532 | unsigned int srcu_idx; | |
1533 | ||
1534 | might_sleep(); | |
1535 | ||
1536 | srcu_idx = srcu_read_lock(&hctx->queue_rq_srcu); | |
1537 | __blk_mq_try_issue_directly(rq, cookie, true); | |
1538 | srcu_read_unlock(&hctx->queue_rq_srcu, srcu_idx); | |
1539 | } | |
1540 | } | |
1541 | ||
1542 | static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio) | |
1543 | { | |
1544 | const int is_sync = op_is_sync(bio->bi_opf); | |
1545 | const int is_flush_fua = op_is_flush(bio->bi_opf); | |
1546 | struct blk_mq_alloc_data data = { .flags = 0 }; | |
1547 | struct request *rq; | |
1548 | unsigned int request_count = 0; | |
1549 | struct blk_plug *plug; | |
1550 | struct request *same_queue_rq = NULL; | |
1551 | blk_qc_t cookie; | |
1552 | unsigned int wb_acct; | |
1553 | ||
1554 | blk_queue_bounce(q, &bio); | |
1555 | ||
1556 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { | |
1557 | bio_io_error(bio); | |
1558 | return BLK_QC_T_NONE; | |
1559 | } | |
1560 | ||
1561 | blk_queue_split(q, &bio, q->bio_split); | |
1562 | ||
1563 | if (!is_flush_fua && !blk_queue_nomerges(q) && | |
1564 | blk_attempt_plug_merge(q, bio, &request_count, &same_queue_rq)) | |
1565 | return BLK_QC_T_NONE; | |
1566 | ||
1567 | if (blk_mq_sched_bio_merge(q, bio)) | |
1568 | return BLK_QC_T_NONE; | |
1569 | ||
1570 | wb_acct = wbt_wait(q->rq_wb, bio, NULL); | |
1571 | ||
1572 | trace_block_getrq(q, bio, bio->bi_opf); | |
1573 | ||
1574 | rq = blk_mq_sched_get_request(q, bio, bio->bi_opf, &data); | |
1575 | if (unlikely(!rq)) { | |
1576 | __wbt_done(q->rq_wb, wb_acct); | |
1577 | return BLK_QC_T_NONE; | |
1578 | } | |
1579 | ||
1580 | wbt_track(&rq->issue_stat, wb_acct); | |
1581 | ||
1582 | cookie = request_to_qc_t(data.hctx, rq); | |
1583 | ||
1584 | plug = current->plug; | |
1585 | if (unlikely(is_flush_fua)) { | |
1586 | blk_mq_put_ctx(data.ctx); | |
1587 | blk_mq_bio_to_request(rq, bio); | |
1588 | if (q->elevator) { | |
1589 | blk_mq_sched_insert_request(rq, false, true, true, | |
1590 | true); | |
1591 | } else { | |
1592 | blk_insert_flush(rq); | |
1593 | blk_mq_run_hw_queue(data.hctx, true); | |
1594 | } | |
1595 | } else if (plug && q->nr_hw_queues == 1) { | |
1596 | struct request *last = NULL; | |
1597 | ||
1598 | blk_mq_put_ctx(data.ctx); | |
1599 | blk_mq_bio_to_request(rq, bio); | |
1600 | ||
1601 | /* | |
1602 | * @request_count may become stale because of schedule | |
1603 | * out, so check the list again. | |
1604 | */ | |
1605 | if (list_empty(&plug->mq_list)) | |
1606 | request_count = 0; | |
1607 | else if (blk_queue_nomerges(q)) | |
1608 | request_count = blk_plug_queued_count(q); | |
1609 | ||
1610 | if (!request_count) | |
1611 | trace_block_plug(q); | |
1612 | else | |
1613 | last = list_entry_rq(plug->mq_list.prev); | |
1614 | ||
1615 | if (request_count >= BLK_MAX_REQUEST_COUNT || (last && | |
1616 | blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) { | |
1617 | blk_flush_plug_list(plug, false); | |
1618 | trace_block_plug(q); | |
1619 | } | |
1620 | ||
1621 | list_add_tail(&rq->queuelist, &plug->mq_list); | |
1622 | } else if (plug && !blk_queue_nomerges(q)) { | |
1623 | blk_mq_bio_to_request(rq, bio); | |
1624 | ||
1625 | /* | |
1626 | * We do limited plugging. If the bio can be merged, do that. | |
1627 | * Otherwise the existing request in the plug list will be | |
1628 | * issued. So the plug list will have one request at most | |
1629 | * The plug list might get flushed before this. If that happens, | |
1630 | * the plug list is empty, and same_queue_rq is invalid. | |
1631 | */ | |
1632 | if (list_empty(&plug->mq_list)) | |
1633 | same_queue_rq = NULL; | |
1634 | if (same_queue_rq) | |
1635 | list_del_init(&same_queue_rq->queuelist); | |
1636 | list_add_tail(&rq->queuelist, &plug->mq_list); | |
1637 | ||
1638 | blk_mq_put_ctx(data.ctx); | |
1639 | ||
1640 | if (same_queue_rq) | |
1641 | blk_mq_try_issue_directly(data.hctx, same_queue_rq, | |
1642 | &cookie); | |
1643 | } else if (q->nr_hw_queues > 1 && is_sync) { | |
1644 | blk_mq_put_ctx(data.ctx); | |
1645 | blk_mq_bio_to_request(rq, bio); | |
1646 | blk_mq_try_issue_directly(data.hctx, rq, &cookie); | |
1647 | } else if (q->elevator) { | |
1648 | blk_mq_put_ctx(data.ctx); | |
1649 | blk_mq_bio_to_request(rq, bio); | |
1650 | blk_mq_sched_insert_request(rq, false, true, true, true); | |
1651 | } else if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) { | |
1652 | blk_mq_put_ctx(data.ctx); | |
1653 | blk_mq_run_hw_queue(data.hctx, true); | |
1654 | } else | |
1655 | blk_mq_put_ctx(data.ctx); | |
1656 | ||
1657 | return cookie; | |
1658 | } | |
1659 | ||
1660 | void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags, | |
1661 | unsigned int hctx_idx) | |
1662 | { | |
1663 | struct page *page; | |
1664 | ||
1665 | if (tags->rqs && set->ops->exit_request) { | |
1666 | int i; | |
1667 | ||
1668 | for (i = 0; i < tags->nr_tags; i++) { | |
1669 | struct request *rq = tags->static_rqs[i]; | |
1670 | ||
1671 | if (!rq) | |
1672 | continue; | |
1673 | set->ops->exit_request(set, rq, hctx_idx); | |
1674 | tags->static_rqs[i] = NULL; | |
1675 | } | |
1676 | } | |
1677 | ||
1678 | while (!list_empty(&tags->page_list)) { | |
1679 | page = list_first_entry(&tags->page_list, struct page, lru); | |
1680 | list_del_init(&page->lru); | |
1681 | /* | |
1682 | * Remove kmemleak object previously allocated in | |
1683 | * blk_mq_init_rq_map(). | |
1684 | */ | |
1685 | kmemleak_free(page_address(page)); | |
1686 | __free_pages(page, page->private); | |
1687 | } | |
1688 | } | |
1689 | ||
1690 | void blk_mq_free_rq_map(struct blk_mq_tags *tags) | |
1691 | { | |
1692 | kfree(tags->rqs); | |
1693 | tags->rqs = NULL; | |
1694 | kfree(tags->static_rqs); | |
1695 | tags->static_rqs = NULL; | |
1696 | ||
1697 | blk_mq_free_tags(tags); | |
1698 | } | |
1699 | ||
1700 | struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, | |
1701 | unsigned int hctx_idx, | |
1702 | unsigned int nr_tags, | |
1703 | unsigned int reserved_tags) | |
1704 | { | |
1705 | struct blk_mq_tags *tags; | |
1706 | int node; | |
1707 | ||
1708 | node = blk_mq_hw_queue_to_node(set->mq_map, hctx_idx); | |
1709 | if (node == NUMA_NO_NODE) | |
1710 | node = set->numa_node; | |
1711 | ||
1712 | tags = blk_mq_init_tags(nr_tags, reserved_tags, node, | |
1713 | BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags)); | |
1714 | if (!tags) | |
1715 | return NULL; | |
1716 | ||
1717 | tags->rqs = kzalloc_node(nr_tags * sizeof(struct request *), | |
1718 | GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, | |
1719 | node); | |
1720 | if (!tags->rqs) { | |
1721 | blk_mq_free_tags(tags); | |
1722 | return NULL; | |
1723 | } | |
1724 | ||
1725 | tags->static_rqs = kzalloc_node(nr_tags * sizeof(struct request *), | |
1726 | GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, | |
1727 | node); | |
1728 | if (!tags->static_rqs) { | |
1729 | kfree(tags->rqs); | |
1730 | blk_mq_free_tags(tags); | |
1731 | return NULL; | |
1732 | } | |
1733 | ||
1734 | return tags; | |
1735 | } | |
1736 | ||
1737 | static size_t order_to_size(unsigned int order) | |
1738 | { | |
1739 | return (size_t)PAGE_SIZE << order; | |
1740 | } | |
1741 | ||
1742 | int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags, | |
1743 | unsigned int hctx_idx, unsigned int depth) | |
1744 | { | |
1745 | unsigned int i, j, entries_per_page, max_order = 4; | |
1746 | size_t rq_size, left; | |
1747 | int node; | |
1748 | ||
1749 | node = blk_mq_hw_queue_to_node(set->mq_map, hctx_idx); | |
1750 | if (node == NUMA_NO_NODE) | |
1751 | node = set->numa_node; | |
1752 | ||
1753 | INIT_LIST_HEAD(&tags->page_list); | |
1754 | ||
1755 | /* | |
1756 | * rq_size is the size of the request plus driver payload, rounded | |
1757 | * to the cacheline size | |
1758 | */ | |
1759 | rq_size = round_up(sizeof(struct request) + set->cmd_size, | |
1760 | cache_line_size()); | |
1761 | left = rq_size * depth; | |
1762 | ||
1763 | for (i = 0; i < depth; ) { | |
1764 | int this_order = max_order; | |
1765 | struct page *page; | |
1766 | int to_do; | |
1767 | void *p; | |
1768 | ||
1769 | while (this_order && left < order_to_size(this_order - 1)) | |
1770 | this_order--; | |
1771 | ||
1772 | do { | |
1773 | page = alloc_pages_node(node, | |
1774 | GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO, | |
1775 | this_order); | |
1776 | if (page) | |
1777 | break; | |
1778 | if (!this_order--) | |
1779 | break; | |
1780 | if (order_to_size(this_order) < rq_size) | |
1781 | break; | |
1782 | } while (1); | |
1783 | ||
1784 | if (!page) | |
1785 | goto fail; | |
1786 | ||
1787 | page->private = this_order; | |
1788 | list_add_tail(&page->lru, &tags->page_list); | |
1789 | ||
1790 | p = page_address(page); | |
1791 | /* | |
1792 | * Allow kmemleak to scan these pages as they contain pointers | |
1793 | * to additional allocations like via ops->init_request(). | |
1794 | */ | |
1795 | kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO); | |
1796 | entries_per_page = order_to_size(this_order) / rq_size; | |
1797 | to_do = min(entries_per_page, depth - i); | |
1798 | left -= to_do * rq_size; | |
1799 | for (j = 0; j < to_do; j++) { | |
1800 | struct request *rq = p; | |
1801 | ||
1802 | tags->static_rqs[i] = rq; | |
1803 | if (set->ops->init_request) { | |
1804 | if (set->ops->init_request(set, rq, hctx_idx, | |
1805 | node)) { | |
1806 | tags->static_rqs[i] = NULL; | |
1807 | goto fail; | |
1808 | } | |
1809 | } | |
1810 | ||
1811 | p += rq_size; | |
1812 | i++; | |
1813 | } | |
1814 | } | |
1815 | return 0; | |
1816 | ||
1817 | fail: | |
1818 | blk_mq_free_rqs(set, tags, hctx_idx); | |
1819 | return -ENOMEM; | |
1820 | } | |
1821 | ||
1822 | /* | |
1823 | * 'cpu' is going away. splice any existing rq_list entries from this | |
1824 | * software queue to the hw queue dispatch list, and ensure that it | |
1825 | * gets run. | |
1826 | */ | |
1827 | static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node) | |
1828 | { | |
1829 | struct blk_mq_hw_ctx *hctx; | |
1830 | struct blk_mq_ctx *ctx; | |
1831 | LIST_HEAD(tmp); | |
1832 | ||
1833 | hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead); | |
1834 | ctx = __blk_mq_get_ctx(hctx->queue, cpu); | |
1835 | ||
1836 | spin_lock(&ctx->lock); | |
1837 | if (!list_empty(&ctx->rq_list)) { | |
1838 | list_splice_init(&ctx->rq_list, &tmp); | |
1839 | blk_mq_hctx_clear_pending(hctx, ctx); | |
1840 | } | |
1841 | spin_unlock(&ctx->lock); | |
1842 | ||
1843 | if (list_empty(&tmp)) | |
1844 | return 0; | |
1845 | ||
1846 | spin_lock(&hctx->lock); | |
1847 | list_splice_tail_init(&tmp, &hctx->dispatch); | |
1848 | spin_unlock(&hctx->lock); | |
1849 | ||
1850 | blk_mq_run_hw_queue(hctx, true); | |
1851 | return 0; | |
1852 | } | |
1853 | ||
1854 | static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx) | |
1855 | { | |
1856 | cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD, | |
1857 | &hctx->cpuhp_dead); | |
1858 | } | |
1859 | ||
1860 | /* hctx->ctxs will be freed in queue's release handler */ | |
1861 | static void blk_mq_exit_hctx(struct request_queue *q, | |
1862 | struct blk_mq_tag_set *set, | |
1863 | struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx) | |
1864 | { | |
1865 | blk_mq_tag_idle(hctx); | |
1866 | ||
1867 | if (set->ops->exit_request) | |
1868 | set->ops->exit_request(set, hctx->fq->flush_rq, hctx_idx); | |
1869 | ||
1870 | blk_mq_sched_exit_hctx(q, hctx, hctx_idx); | |
1871 | ||
1872 | if (set->ops->exit_hctx) | |
1873 | set->ops->exit_hctx(hctx, hctx_idx); | |
1874 | ||
1875 | if (hctx->flags & BLK_MQ_F_BLOCKING) | |
1876 | cleanup_srcu_struct(&hctx->queue_rq_srcu); | |
1877 | ||
1878 | blk_mq_remove_cpuhp(hctx); | |
1879 | blk_free_flush_queue(hctx->fq); | |
1880 | sbitmap_free(&hctx->ctx_map); | |
1881 | } | |
1882 | ||
1883 | static void blk_mq_exit_hw_queues(struct request_queue *q, | |
1884 | struct blk_mq_tag_set *set, int nr_queue) | |
1885 | { | |
1886 | struct blk_mq_hw_ctx *hctx; | |
1887 | unsigned int i; | |
1888 | ||
1889 | queue_for_each_hw_ctx(q, hctx, i) { | |
1890 | if (i == nr_queue) | |
1891 | break; | |
1892 | blk_mq_exit_hctx(q, set, hctx, i); | |
1893 | } | |
1894 | } | |
1895 | ||
1896 | static int blk_mq_init_hctx(struct request_queue *q, | |
1897 | struct blk_mq_tag_set *set, | |
1898 | struct blk_mq_hw_ctx *hctx, unsigned hctx_idx) | |
1899 | { | |
1900 | int node; | |
1901 | ||
1902 | node = hctx->numa_node; | |
1903 | if (node == NUMA_NO_NODE) | |
1904 | node = hctx->numa_node = set->numa_node; | |
1905 | ||
1906 | INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn); | |
1907 | spin_lock_init(&hctx->lock); | |
1908 | INIT_LIST_HEAD(&hctx->dispatch); | |
1909 | hctx->queue = q; | |
1910 | hctx->queue_num = hctx_idx; | |
1911 | hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED; | |
1912 | ||
1913 | cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead); | |
1914 | ||
1915 | hctx->tags = set->tags[hctx_idx]; | |
1916 | ||
1917 | /* | |
1918 | * Allocate space for all possible cpus to avoid allocation at | |
1919 | * runtime | |
1920 | */ | |
1921 | hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *), | |
1922 | GFP_KERNEL, node); | |
1923 | if (!hctx->ctxs) | |
1924 | goto unregister_cpu_notifier; | |
1925 | ||
1926 | if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8), GFP_KERNEL, | |
1927 | node)) | |
1928 | goto free_ctxs; | |
1929 | ||
1930 | hctx->nr_ctx = 0; | |
1931 | ||
1932 | if (set->ops->init_hctx && | |
1933 | set->ops->init_hctx(hctx, set->driver_data, hctx_idx)) | |
1934 | goto free_bitmap; | |
1935 | ||
1936 | if (blk_mq_sched_init_hctx(q, hctx, hctx_idx)) | |
1937 | goto exit_hctx; | |
1938 | ||
1939 | hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size); | |
1940 | if (!hctx->fq) | |
1941 | goto sched_exit_hctx; | |
1942 | ||
1943 | if (set->ops->init_request && | |
1944 | set->ops->init_request(set, hctx->fq->flush_rq, hctx_idx, | |
1945 | node)) | |
1946 | goto free_fq; | |
1947 | ||
1948 | if (hctx->flags & BLK_MQ_F_BLOCKING) | |
1949 | init_srcu_struct(&hctx->queue_rq_srcu); | |
1950 | ||
1951 | return 0; | |
1952 | ||
1953 | free_fq: | |
1954 | kfree(hctx->fq); | |
1955 | sched_exit_hctx: | |
1956 | blk_mq_sched_exit_hctx(q, hctx, hctx_idx); | |
1957 | exit_hctx: | |
1958 | if (set->ops->exit_hctx) | |
1959 | set->ops->exit_hctx(hctx, hctx_idx); | |
1960 | free_bitmap: | |
1961 | sbitmap_free(&hctx->ctx_map); | |
1962 | free_ctxs: | |
1963 | kfree(hctx->ctxs); | |
1964 | unregister_cpu_notifier: | |
1965 | blk_mq_remove_cpuhp(hctx); | |
1966 | return -1; | |
1967 | } | |
1968 | ||
1969 | static void blk_mq_init_cpu_queues(struct request_queue *q, | |
1970 | unsigned int nr_hw_queues) | |
1971 | { | |
1972 | unsigned int i; | |
1973 | ||
1974 | for_each_possible_cpu(i) { | |
1975 | struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i); | |
1976 | struct blk_mq_hw_ctx *hctx; | |
1977 | ||
1978 | __ctx->cpu = i; | |
1979 | spin_lock_init(&__ctx->lock); | |
1980 | INIT_LIST_HEAD(&__ctx->rq_list); | |
1981 | __ctx->queue = q; | |
1982 | ||
1983 | /* If the cpu isn't online, the cpu is mapped to first hctx */ | |
1984 | if (!cpu_online(i)) | |
1985 | continue; | |
1986 | ||
1987 | hctx = blk_mq_map_queue(q, i); | |
1988 | ||
1989 | /* | |
1990 | * Set local node, IFF we have more than one hw queue. If | |
1991 | * not, we remain on the home node of the device | |
1992 | */ | |
1993 | if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE) | |
1994 | hctx->numa_node = local_memory_node(cpu_to_node(i)); | |
1995 | } | |
1996 | } | |
1997 | ||
1998 | static bool __blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, int hctx_idx) | |
1999 | { | |
2000 | int ret = 0; | |
2001 | ||
2002 | set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx, | |
2003 | set->queue_depth, set->reserved_tags); | |
2004 | if (!set->tags[hctx_idx]) | |
2005 | return false; | |
2006 | ||
2007 | ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx, | |
2008 | set->queue_depth); | |
2009 | if (!ret) | |
2010 | return true; | |
2011 | ||
2012 | blk_mq_free_rq_map(set->tags[hctx_idx]); | |
2013 | set->tags[hctx_idx] = NULL; | |
2014 | return false; | |
2015 | } | |
2016 | ||
2017 | static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set, | |
2018 | unsigned int hctx_idx) | |
2019 | { | |
2020 | if (set->tags[hctx_idx]) { | |
2021 | blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx); | |
2022 | blk_mq_free_rq_map(set->tags[hctx_idx]); | |
2023 | set->tags[hctx_idx] = NULL; | |
2024 | } | |
2025 | } | |
2026 | ||
2027 | static void blk_mq_map_swqueue(struct request_queue *q, | |
2028 | const struct cpumask *online_mask) | |
2029 | { | |
2030 | unsigned int i, hctx_idx; | |
2031 | struct blk_mq_hw_ctx *hctx; | |
2032 | struct blk_mq_ctx *ctx; | |
2033 | struct blk_mq_tag_set *set = q->tag_set; | |
2034 | ||
2035 | /* | |
2036 | * Avoid others reading imcomplete hctx->cpumask through sysfs | |
2037 | */ | |
2038 | mutex_lock(&q->sysfs_lock); | |
2039 | ||
2040 | queue_for_each_hw_ctx(q, hctx, i) { | |
2041 | cpumask_clear(hctx->cpumask); | |
2042 | hctx->nr_ctx = 0; | |
2043 | } | |
2044 | ||
2045 | /* | |
2046 | * Map software to hardware queues | |
2047 | */ | |
2048 | for_each_possible_cpu(i) { | |
2049 | /* If the cpu isn't online, the cpu is mapped to first hctx */ | |
2050 | if (!cpumask_test_cpu(i, online_mask)) | |
2051 | continue; | |
2052 | ||
2053 | hctx_idx = q->mq_map[i]; | |
2054 | /* unmapped hw queue can be remapped after CPU topo changed */ | |
2055 | if (!set->tags[hctx_idx] && | |
2056 | !__blk_mq_alloc_rq_map(set, hctx_idx)) { | |
2057 | /* | |
2058 | * If tags initialization fail for some hctx, | |
2059 | * that hctx won't be brought online. In this | |
2060 | * case, remap the current ctx to hctx[0] which | |
2061 | * is guaranteed to always have tags allocated | |
2062 | */ | |
2063 | q->mq_map[i] = 0; | |
2064 | } | |
2065 | ||
2066 | ctx = per_cpu_ptr(q->queue_ctx, i); | |
2067 | hctx = blk_mq_map_queue(q, i); | |
2068 | ||
2069 | cpumask_set_cpu(i, hctx->cpumask); | |
2070 | ctx->index_hw = hctx->nr_ctx; | |
2071 | hctx->ctxs[hctx->nr_ctx++] = ctx; | |
2072 | } | |
2073 | ||
2074 | mutex_unlock(&q->sysfs_lock); | |
2075 | ||
2076 | queue_for_each_hw_ctx(q, hctx, i) { | |
2077 | /* | |
2078 | * If no software queues are mapped to this hardware queue, | |
2079 | * disable it and free the request entries. | |
2080 | */ | |
2081 | if (!hctx->nr_ctx) { | |
2082 | /* Never unmap queue 0. We need it as a | |
2083 | * fallback in case of a new remap fails | |
2084 | * allocation | |
2085 | */ | |
2086 | if (i && set->tags[i]) | |
2087 | blk_mq_free_map_and_requests(set, i); | |
2088 | ||
2089 | hctx->tags = NULL; | |
2090 | continue; | |
2091 | } | |
2092 | ||
2093 | hctx->tags = set->tags[i]; | |
2094 | WARN_ON(!hctx->tags); | |
2095 | ||
2096 | /* | |
2097 | * Set the map size to the number of mapped software queues. | |
2098 | * This is more accurate and more efficient than looping | |
2099 | * over all possibly mapped software queues. | |
2100 | */ | |
2101 | sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx); | |
2102 | ||
2103 | /* | |
2104 | * Initialize batch roundrobin counts | |
2105 | */ | |
2106 | hctx->next_cpu = cpumask_first(hctx->cpumask); | |
2107 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; | |
2108 | } | |
2109 | } | |
2110 | ||
2111 | static void queue_set_hctx_shared(struct request_queue *q, bool shared) | |
2112 | { | |
2113 | struct blk_mq_hw_ctx *hctx; | |
2114 | int i; | |
2115 | ||
2116 | queue_for_each_hw_ctx(q, hctx, i) { | |
2117 | if (shared) | |
2118 | hctx->flags |= BLK_MQ_F_TAG_SHARED; | |
2119 | else | |
2120 | hctx->flags &= ~BLK_MQ_F_TAG_SHARED; | |
2121 | } | |
2122 | } | |
2123 | ||
2124 | static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set, bool shared) | |
2125 | { | |
2126 | struct request_queue *q; | |
2127 | ||
2128 | lockdep_assert_held(&set->tag_list_lock); | |
2129 | ||
2130 | list_for_each_entry(q, &set->tag_list, tag_set_list) { | |
2131 | blk_mq_freeze_queue(q); | |
2132 | queue_set_hctx_shared(q, shared); | |
2133 | blk_mq_unfreeze_queue(q); | |
2134 | } | |
2135 | } | |
2136 | ||
2137 | static void blk_mq_del_queue_tag_set(struct request_queue *q) | |
2138 | { | |
2139 | struct blk_mq_tag_set *set = q->tag_set; | |
2140 | ||
2141 | mutex_lock(&set->tag_list_lock); | |
2142 | list_del_rcu(&q->tag_set_list); | |
2143 | INIT_LIST_HEAD(&q->tag_set_list); | |
2144 | if (list_is_singular(&set->tag_list)) { | |
2145 | /* just transitioned to unshared */ | |
2146 | set->flags &= ~BLK_MQ_F_TAG_SHARED; | |
2147 | /* update existing queue */ | |
2148 | blk_mq_update_tag_set_depth(set, false); | |
2149 | } | |
2150 | mutex_unlock(&set->tag_list_lock); | |
2151 | ||
2152 | synchronize_rcu(); | |
2153 | } | |
2154 | ||
2155 | static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set, | |
2156 | struct request_queue *q) | |
2157 | { | |
2158 | q->tag_set = set; | |
2159 | ||
2160 | mutex_lock(&set->tag_list_lock); | |
2161 | ||
2162 | /* Check to see if we're transitioning to shared (from 1 to 2 queues). */ | |
2163 | if (!list_empty(&set->tag_list) && !(set->flags & BLK_MQ_F_TAG_SHARED)) { | |
2164 | set->flags |= BLK_MQ_F_TAG_SHARED; | |
2165 | /* update existing queue */ | |
2166 | blk_mq_update_tag_set_depth(set, true); | |
2167 | } | |
2168 | if (set->flags & BLK_MQ_F_TAG_SHARED) | |
2169 | queue_set_hctx_shared(q, true); | |
2170 | list_add_tail_rcu(&q->tag_set_list, &set->tag_list); | |
2171 | ||
2172 | mutex_unlock(&set->tag_list_lock); | |
2173 | } | |
2174 | ||
2175 | /* | |
2176 | * It is the actual release handler for mq, but we do it from | |
2177 | * request queue's release handler for avoiding use-after-free | |
2178 | * and headache because q->mq_kobj shouldn't have been introduced, | |
2179 | * but we can't group ctx/kctx kobj without it. | |
2180 | */ | |
2181 | void blk_mq_release(struct request_queue *q) | |
2182 | { | |
2183 | struct blk_mq_hw_ctx *hctx; | |
2184 | unsigned int i; | |
2185 | ||
2186 | /* hctx kobj stays in hctx */ | |
2187 | queue_for_each_hw_ctx(q, hctx, i) { | |
2188 | if (!hctx) | |
2189 | continue; | |
2190 | kobject_put(&hctx->kobj); | |
2191 | } | |
2192 | ||
2193 | q->mq_map = NULL; | |
2194 | ||
2195 | kfree(q->queue_hw_ctx); | |
2196 | ||
2197 | /* | |
2198 | * release .mq_kobj and sw queue's kobject now because | |
2199 | * both share lifetime with request queue. | |
2200 | */ | |
2201 | blk_mq_sysfs_deinit(q); | |
2202 | ||
2203 | free_percpu(q->queue_ctx); | |
2204 | } | |
2205 | ||
2206 | struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set) | |
2207 | { | |
2208 | struct request_queue *uninit_q, *q; | |
2209 | ||
2210 | uninit_q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node); | |
2211 | if (!uninit_q) | |
2212 | return ERR_PTR(-ENOMEM); | |
2213 | ||
2214 | q = blk_mq_init_allocated_queue(set, uninit_q); | |
2215 | if (IS_ERR(q)) | |
2216 | blk_cleanup_queue(uninit_q); | |
2217 | ||
2218 | return q; | |
2219 | } | |
2220 | EXPORT_SYMBOL(blk_mq_init_queue); | |
2221 | ||
2222 | static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set, | |
2223 | struct request_queue *q) | |
2224 | { | |
2225 | int i, j; | |
2226 | struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx; | |
2227 | ||
2228 | blk_mq_sysfs_unregister(q); | |
2229 | for (i = 0; i < set->nr_hw_queues; i++) { | |
2230 | int node; | |
2231 | ||
2232 | if (hctxs[i]) | |
2233 | continue; | |
2234 | ||
2235 | node = blk_mq_hw_queue_to_node(q->mq_map, i); | |
2236 | hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx), | |
2237 | GFP_KERNEL, node); | |
2238 | if (!hctxs[i]) | |
2239 | break; | |
2240 | ||
2241 | if (!zalloc_cpumask_var_node(&hctxs[i]->cpumask, GFP_KERNEL, | |
2242 | node)) { | |
2243 | kfree(hctxs[i]); | |
2244 | hctxs[i] = NULL; | |
2245 | break; | |
2246 | } | |
2247 | ||
2248 | atomic_set(&hctxs[i]->nr_active, 0); | |
2249 | hctxs[i]->numa_node = node; | |
2250 | hctxs[i]->queue_num = i; | |
2251 | ||
2252 | if (blk_mq_init_hctx(q, set, hctxs[i], i)) { | |
2253 | free_cpumask_var(hctxs[i]->cpumask); | |
2254 | kfree(hctxs[i]); | |
2255 | hctxs[i] = NULL; | |
2256 | break; | |
2257 | } | |
2258 | blk_mq_hctx_kobj_init(hctxs[i]); | |
2259 | } | |
2260 | for (j = i; j < q->nr_hw_queues; j++) { | |
2261 | struct blk_mq_hw_ctx *hctx = hctxs[j]; | |
2262 | ||
2263 | if (hctx) { | |
2264 | if (hctx->tags) | |
2265 | blk_mq_free_map_and_requests(set, j); | |
2266 | blk_mq_exit_hctx(q, set, hctx, j); | |
2267 | kobject_put(&hctx->kobj); | |
2268 | hctxs[j] = NULL; | |
2269 | ||
2270 | } | |
2271 | } | |
2272 | q->nr_hw_queues = i; | |
2273 | blk_mq_sysfs_register(q); | |
2274 | } | |
2275 | ||
2276 | struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set, | |
2277 | struct request_queue *q) | |
2278 | { | |
2279 | /* mark the queue as mq asap */ | |
2280 | q->mq_ops = set->ops; | |
2281 | ||
2282 | q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn, | |
2283 | blk_mq_poll_stats_bkt, | |
2284 | BLK_MQ_POLL_STATS_BKTS, q); | |
2285 | if (!q->poll_cb) | |
2286 | goto err_exit; | |
2287 | ||
2288 | q->queue_ctx = alloc_percpu(struct blk_mq_ctx); | |
2289 | if (!q->queue_ctx) | |
2290 | goto err_exit; | |
2291 | ||
2292 | /* init q->mq_kobj and sw queues' kobjects */ | |
2293 | blk_mq_sysfs_init(q); | |
2294 | ||
2295 | q->queue_hw_ctx = kzalloc_node(nr_cpu_ids * sizeof(*(q->queue_hw_ctx)), | |
2296 | GFP_KERNEL, set->numa_node); | |
2297 | if (!q->queue_hw_ctx) | |
2298 | goto err_percpu; | |
2299 | ||
2300 | q->mq_map = set->mq_map; | |
2301 | ||
2302 | blk_mq_realloc_hw_ctxs(set, q); | |
2303 | if (!q->nr_hw_queues) | |
2304 | goto err_hctxs; | |
2305 | ||
2306 | INIT_WORK(&q->timeout_work, blk_mq_timeout_work); | |
2307 | blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ); | |
2308 | ||
2309 | q->nr_queues = nr_cpu_ids; | |
2310 | ||
2311 | q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT; | |
2312 | ||
2313 | if (!(set->flags & BLK_MQ_F_SG_MERGE)) | |
2314 | q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE; | |
2315 | ||
2316 | q->sg_reserved_size = INT_MAX; | |
2317 | ||
2318 | INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work); | |
2319 | INIT_LIST_HEAD(&q->requeue_list); | |
2320 | spin_lock_init(&q->requeue_lock); | |
2321 | ||
2322 | blk_queue_make_request(q, blk_mq_make_request); | |
2323 | ||
2324 | /* | |
2325 | * Do this after blk_queue_make_request() overrides it... | |
2326 | */ | |
2327 | q->nr_requests = set->queue_depth; | |
2328 | ||
2329 | /* | |
2330 | * Default to classic polling | |
2331 | */ | |
2332 | q->poll_nsec = -1; | |
2333 | ||
2334 | if (set->ops->complete) | |
2335 | blk_queue_softirq_done(q, set->ops->complete); | |
2336 | ||
2337 | blk_mq_init_cpu_queues(q, set->nr_hw_queues); | |
2338 | ||
2339 | mutex_lock(&all_q_mutex); | |
2340 | get_online_cpus(); | |
2341 | ||
2342 | list_add_tail(&q->all_q_node, &all_q_list); | |
2343 | blk_mq_add_queue_tag_set(set, q); | |
2344 | blk_mq_map_swqueue(q, cpu_online_mask); | |
2345 | ||
2346 | put_online_cpus(); | |
2347 | mutex_unlock(&all_q_mutex); | |
2348 | ||
2349 | if (!(set->flags & BLK_MQ_F_NO_SCHED)) { | |
2350 | int ret; | |
2351 | ||
2352 | ret = blk_mq_sched_init(q); | |
2353 | if (ret) | |
2354 | return ERR_PTR(ret); | |
2355 | } | |
2356 | ||
2357 | return q; | |
2358 | ||
2359 | err_hctxs: | |
2360 | kfree(q->queue_hw_ctx); | |
2361 | err_percpu: | |
2362 | free_percpu(q->queue_ctx); | |
2363 | err_exit: | |
2364 | q->mq_ops = NULL; | |
2365 | return ERR_PTR(-ENOMEM); | |
2366 | } | |
2367 | EXPORT_SYMBOL(blk_mq_init_allocated_queue); | |
2368 | ||
2369 | void blk_mq_free_queue(struct request_queue *q) | |
2370 | { | |
2371 | struct blk_mq_tag_set *set = q->tag_set; | |
2372 | ||
2373 | mutex_lock(&all_q_mutex); | |
2374 | list_del_init(&q->all_q_node); | |
2375 | mutex_unlock(&all_q_mutex); | |
2376 | ||
2377 | blk_mq_del_queue_tag_set(q); | |
2378 | ||
2379 | blk_mq_exit_hw_queues(q, set, set->nr_hw_queues); | |
2380 | } | |
2381 | ||
2382 | /* Basically redo blk_mq_init_queue with queue frozen */ | |
2383 | static void blk_mq_queue_reinit(struct request_queue *q, | |
2384 | const struct cpumask *online_mask) | |
2385 | { | |
2386 | WARN_ON_ONCE(!atomic_read(&q->mq_freeze_depth)); | |
2387 | ||
2388 | blk_mq_sysfs_unregister(q); | |
2389 | ||
2390 | /* | |
2391 | * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe | |
2392 | * we should change hctx numa_node according to new topology (this | |
2393 | * involves free and re-allocate memory, worthy doing?) | |
2394 | */ | |
2395 | ||
2396 | blk_mq_map_swqueue(q, online_mask); | |
2397 | ||
2398 | blk_mq_sysfs_register(q); | |
2399 | } | |
2400 | ||
2401 | /* | |
2402 | * New online cpumask which is going to be set in this hotplug event. | |
2403 | * Declare this cpumasks as global as cpu-hotplug operation is invoked | |
2404 | * one-by-one and dynamically allocating this could result in a failure. | |
2405 | */ | |
2406 | static struct cpumask cpuhp_online_new; | |
2407 | ||
2408 | static void blk_mq_queue_reinit_work(void) | |
2409 | { | |
2410 | struct request_queue *q; | |
2411 | ||
2412 | mutex_lock(&all_q_mutex); | |
2413 | /* | |
2414 | * We need to freeze and reinit all existing queues. Freezing | |
2415 | * involves synchronous wait for an RCU grace period and doing it | |
2416 | * one by one may take a long time. Start freezing all queues in | |
2417 | * one swoop and then wait for the completions so that freezing can | |
2418 | * take place in parallel. | |
2419 | */ | |
2420 | list_for_each_entry(q, &all_q_list, all_q_node) | |
2421 | blk_freeze_queue_start(q); | |
2422 | list_for_each_entry(q, &all_q_list, all_q_node) | |
2423 | blk_mq_freeze_queue_wait(q); | |
2424 | ||
2425 | list_for_each_entry(q, &all_q_list, all_q_node) | |
2426 | blk_mq_queue_reinit(q, &cpuhp_online_new); | |
2427 | ||
2428 | list_for_each_entry(q, &all_q_list, all_q_node) | |
2429 | blk_mq_unfreeze_queue(q); | |
2430 | ||
2431 | mutex_unlock(&all_q_mutex); | |
2432 | } | |
2433 | ||
2434 | static int blk_mq_queue_reinit_dead(unsigned int cpu) | |
2435 | { | |
2436 | cpumask_copy(&cpuhp_online_new, cpu_online_mask); | |
2437 | blk_mq_queue_reinit_work(); | |
2438 | return 0; | |
2439 | } | |
2440 | ||
2441 | /* | |
2442 | * Before hotadded cpu starts handling requests, new mappings must be | |
2443 | * established. Otherwise, these requests in hw queue might never be | |
2444 | * dispatched. | |
2445 | * | |
2446 | * For example, there is a single hw queue (hctx) and two CPU queues (ctx0 | |
2447 | * for CPU0, and ctx1 for CPU1). | |
2448 | * | |
2449 | * Now CPU1 is just onlined and a request is inserted into ctx1->rq_list | |
2450 | * and set bit0 in pending bitmap as ctx1->index_hw is still zero. | |
2451 | * | |
2452 | * And then while running hw queue, blk_mq_flush_busy_ctxs() finds bit0 is set | |
2453 | * in pending bitmap and tries to retrieve requests in hctx->ctxs[0]->rq_list. | |
2454 | * But htx->ctxs[0] is a pointer to ctx0, so the request in ctx1->rq_list is | |
2455 | * ignored. | |
2456 | */ | |
2457 | static int blk_mq_queue_reinit_prepare(unsigned int cpu) | |
2458 | { | |
2459 | cpumask_copy(&cpuhp_online_new, cpu_online_mask); | |
2460 | cpumask_set_cpu(cpu, &cpuhp_online_new); | |
2461 | blk_mq_queue_reinit_work(); | |
2462 | return 0; | |
2463 | } | |
2464 | ||
2465 | static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set) | |
2466 | { | |
2467 | int i; | |
2468 | ||
2469 | for (i = 0; i < set->nr_hw_queues; i++) | |
2470 | if (!__blk_mq_alloc_rq_map(set, i)) | |
2471 | goto out_unwind; | |
2472 | ||
2473 | return 0; | |
2474 | ||
2475 | out_unwind: | |
2476 | while (--i >= 0) | |
2477 | blk_mq_free_rq_map(set->tags[i]); | |
2478 | ||
2479 | return -ENOMEM; | |
2480 | } | |
2481 | ||
2482 | /* | |
2483 | * Allocate the request maps associated with this tag_set. Note that this | |
2484 | * may reduce the depth asked for, if memory is tight. set->queue_depth | |
2485 | * will be updated to reflect the allocated depth. | |
2486 | */ | |
2487 | static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set) | |
2488 | { | |
2489 | unsigned int depth; | |
2490 | int err; | |
2491 | ||
2492 | depth = set->queue_depth; | |
2493 | do { | |
2494 | err = __blk_mq_alloc_rq_maps(set); | |
2495 | if (!err) | |
2496 | break; | |
2497 | ||
2498 | set->queue_depth >>= 1; | |
2499 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) { | |
2500 | err = -ENOMEM; | |
2501 | break; | |
2502 | } | |
2503 | } while (set->queue_depth); | |
2504 | ||
2505 | if (!set->queue_depth || err) { | |
2506 | pr_err("blk-mq: failed to allocate request map\n"); | |
2507 | return -ENOMEM; | |
2508 | } | |
2509 | ||
2510 | if (depth != set->queue_depth) | |
2511 | pr_info("blk-mq: reduced tag depth (%u -> %u)\n", | |
2512 | depth, set->queue_depth); | |
2513 | ||
2514 | return 0; | |
2515 | } | |
2516 | ||
2517 | static int blk_mq_update_queue_map(struct blk_mq_tag_set *set) | |
2518 | { | |
2519 | if (set->ops->map_queues) | |
2520 | return set->ops->map_queues(set); | |
2521 | else | |
2522 | return blk_mq_map_queues(set); | |
2523 | } | |
2524 | ||
2525 | /* | |
2526 | * Alloc a tag set to be associated with one or more request queues. | |
2527 | * May fail with EINVAL for various error conditions. May adjust the | |
2528 | * requested depth down, if if it too large. In that case, the set | |
2529 | * value will be stored in set->queue_depth. | |
2530 | */ | |
2531 | int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) | |
2532 | { | |
2533 | int ret; | |
2534 | ||
2535 | BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS); | |
2536 | ||
2537 | if (!set->nr_hw_queues) | |
2538 | return -EINVAL; | |
2539 | if (!set->queue_depth) | |
2540 | return -EINVAL; | |
2541 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) | |
2542 | return -EINVAL; | |
2543 | ||
2544 | if (!set->ops->queue_rq) | |
2545 | return -EINVAL; | |
2546 | ||
2547 | if (set->queue_depth > BLK_MQ_MAX_DEPTH) { | |
2548 | pr_info("blk-mq: reduced tag depth to %u\n", | |
2549 | BLK_MQ_MAX_DEPTH); | |
2550 | set->queue_depth = BLK_MQ_MAX_DEPTH; | |
2551 | } | |
2552 | ||
2553 | /* | |
2554 | * If a crashdump is active, then we are potentially in a very | |
2555 | * memory constrained environment. Limit us to 1 queue and | |
2556 | * 64 tags to prevent using too much memory. | |
2557 | */ | |
2558 | if (is_kdump_kernel()) { | |
2559 | set->nr_hw_queues = 1; | |
2560 | set->queue_depth = min(64U, set->queue_depth); | |
2561 | } | |
2562 | /* | |
2563 | * There is no use for more h/w queues than cpus. | |
2564 | */ | |
2565 | if (set->nr_hw_queues > nr_cpu_ids) | |
2566 | set->nr_hw_queues = nr_cpu_ids; | |
2567 | ||
2568 | set->tags = kzalloc_node(nr_cpu_ids * sizeof(struct blk_mq_tags *), | |
2569 | GFP_KERNEL, set->numa_node); | |
2570 | if (!set->tags) | |
2571 | return -ENOMEM; | |
2572 | ||
2573 | ret = -ENOMEM; | |
2574 | set->mq_map = kzalloc_node(sizeof(*set->mq_map) * nr_cpu_ids, | |
2575 | GFP_KERNEL, set->numa_node); | |
2576 | if (!set->mq_map) | |
2577 | goto out_free_tags; | |
2578 | ||
2579 | ret = blk_mq_update_queue_map(set); | |
2580 | if (ret) | |
2581 | goto out_free_mq_map; | |
2582 | ||
2583 | ret = blk_mq_alloc_rq_maps(set); | |
2584 | if (ret) | |
2585 | goto out_free_mq_map; | |
2586 | ||
2587 | mutex_init(&set->tag_list_lock); | |
2588 | INIT_LIST_HEAD(&set->tag_list); | |
2589 | ||
2590 | return 0; | |
2591 | ||
2592 | out_free_mq_map: | |
2593 | kfree(set->mq_map); | |
2594 | set->mq_map = NULL; | |
2595 | out_free_tags: | |
2596 | kfree(set->tags); | |
2597 | set->tags = NULL; | |
2598 | return ret; | |
2599 | } | |
2600 | EXPORT_SYMBOL(blk_mq_alloc_tag_set); | |
2601 | ||
2602 | void blk_mq_free_tag_set(struct blk_mq_tag_set *set) | |
2603 | { | |
2604 | int i; | |
2605 | ||
2606 | for (i = 0; i < nr_cpu_ids; i++) | |
2607 | blk_mq_free_map_and_requests(set, i); | |
2608 | ||
2609 | kfree(set->mq_map); | |
2610 | set->mq_map = NULL; | |
2611 | ||
2612 | kfree(set->tags); | |
2613 | set->tags = NULL; | |
2614 | } | |
2615 | EXPORT_SYMBOL(blk_mq_free_tag_set); | |
2616 | ||
2617 | int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) | |
2618 | { | |
2619 | struct blk_mq_tag_set *set = q->tag_set; | |
2620 | struct blk_mq_hw_ctx *hctx; | |
2621 | int i, ret; | |
2622 | ||
2623 | if (!set) | |
2624 | return -EINVAL; | |
2625 | ||
2626 | blk_mq_freeze_queue(q); | |
2627 | ||
2628 | ret = 0; | |
2629 | queue_for_each_hw_ctx(q, hctx, i) { | |
2630 | if (!hctx->tags) | |
2631 | continue; | |
2632 | /* | |
2633 | * If we're using an MQ scheduler, just update the scheduler | |
2634 | * queue depth. This is similar to what the old code would do. | |
2635 | */ | |
2636 | if (!hctx->sched_tags) { | |
2637 | ret = blk_mq_tag_update_depth(hctx, &hctx->tags, | |
2638 | min(nr, set->queue_depth), | |
2639 | false); | |
2640 | } else { | |
2641 | ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags, | |
2642 | nr, true); | |
2643 | } | |
2644 | if (ret) | |
2645 | break; | |
2646 | } | |
2647 | ||
2648 | if (!ret) | |
2649 | q->nr_requests = nr; | |
2650 | ||
2651 | blk_mq_unfreeze_queue(q); | |
2652 | ||
2653 | return ret; | |
2654 | } | |
2655 | ||
2656 | void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues) | |
2657 | { | |
2658 | struct request_queue *q; | |
2659 | ||
2660 | lockdep_assert_held(&set->tag_list_lock); | |
2661 | ||
2662 | if (nr_hw_queues > nr_cpu_ids) | |
2663 | nr_hw_queues = nr_cpu_ids; | |
2664 | if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues) | |
2665 | return; | |
2666 | ||
2667 | list_for_each_entry(q, &set->tag_list, tag_set_list) | |
2668 | blk_mq_freeze_queue(q); | |
2669 | ||
2670 | set->nr_hw_queues = nr_hw_queues; | |
2671 | blk_mq_update_queue_map(set); | |
2672 | list_for_each_entry(q, &set->tag_list, tag_set_list) { | |
2673 | blk_mq_realloc_hw_ctxs(set, q); | |
2674 | blk_mq_queue_reinit(q, cpu_online_mask); | |
2675 | } | |
2676 | ||
2677 | list_for_each_entry(q, &set->tag_list, tag_set_list) | |
2678 | blk_mq_unfreeze_queue(q); | |
2679 | } | |
2680 | EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues); | |
2681 | ||
2682 | /* Enable polling stats and return whether they were already enabled. */ | |
2683 | static bool blk_poll_stats_enable(struct request_queue *q) | |
2684 | { | |
2685 | if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) || | |
2686 | test_and_set_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags)) | |
2687 | return true; | |
2688 | blk_stat_add_callback(q, q->poll_cb); | |
2689 | return false; | |
2690 | } | |
2691 | ||
2692 | static void blk_mq_poll_stats_start(struct request_queue *q) | |
2693 | { | |
2694 | /* | |
2695 | * We don't arm the callback if polling stats are not enabled or the | |
2696 | * callback is already active. | |
2697 | */ | |
2698 | if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) || | |
2699 | blk_stat_is_active(q->poll_cb)) | |
2700 | return; | |
2701 | ||
2702 | blk_stat_activate_msecs(q->poll_cb, 100); | |
2703 | } | |
2704 | ||
2705 | static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb) | |
2706 | { | |
2707 | struct request_queue *q = cb->data; | |
2708 | int bucket; | |
2709 | ||
2710 | for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) { | |
2711 | if (cb->stat[bucket].nr_samples) | |
2712 | q->poll_stat[bucket] = cb->stat[bucket]; | |
2713 | } | |
2714 | } | |
2715 | ||
2716 | static unsigned long blk_mq_poll_nsecs(struct request_queue *q, | |
2717 | struct blk_mq_hw_ctx *hctx, | |
2718 | struct request *rq) | |
2719 | { | |
2720 | unsigned long ret = 0; | |
2721 | int bucket; | |
2722 | ||
2723 | /* | |
2724 | * If stats collection isn't on, don't sleep but turn it on for | |
2725 | * future users | |
2726 | */ | |
2727 | if (!blk_poll_stats_enable(q)) | |
2728 | return 0; | |
2729 | ||
2730 | /* | |
2731 | * As an optimistic guess, use half of the mean service time | |
2732 | * for this type of request. We can (and should) make this smarter. | |
2733 | * For instance, if the completion latencies are tight, we can | |
2734 | * get closer than just half the mean. This is especially | |
2735 | * important on devices where the completion latencies are longer | |
2736 | * than ~10 usec. We do use the stats for the relevant IO size | |
2737 | * if available which does lead to better estimates. | |
2738 | */ | |
2739 | bucket = blk_mq_poll_stats_bkt(rq); | |
2740 | if (bucket < 0) | |
2741 | return ret; | |
2742 | ||
2743 | if (q->poll_stat[bucket].nr_samples) | |
2744 | ret = (q->poll_stat[bucket].mean + 1) / 2; | |
2745 | ||
2746 | return ret; | |
2747 | } | |
2748 | ||
2749 | static bool blk_mq_poll_hybrid_sleep(struct request_queue *q, | |
2750 | struct blk_mq_hw_ctx *hctx, | |
2751 | struct request *rq) | |
2752 | { | |
2753 | struct hrtimer_sleeper hs; | |
2754 | enum hrtimer_mode mode; | |
2755 | unsigned int nsecs; | |
2756 | ktime_t kt; | |
2757 | ||
2758 | if (test_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags)) | |
2759 | return false; | |
2760 | ||
2761 | /* | |
2762 | * poll_nsec can be: | |
2763 | * | |
2764 | * -1: don't ever hybrid sleep | |
2765 | * 0: use half of prev avg | |
2766 | * >0: use this specific value | |
2767 | */ | |
2768 | if (q->poll_nsec == -1) | |
2769 | return false; | |
2770 | else if (q->poll_nsec > 0) | |
2771 | nsecs = q->poll_nsec; | |
2772 | else | |
2773 | nsecs = blk_mq_poll_nsecs(q, hctx, rq); | |
2774 | ||
2775 | if (!nsecs) | |
2776 | return false; | |
2777 | ||
2778 | set_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags); | |
2779 | ||
2780 | /* | |
2781 | * This will be replaced with the stats tracking code, using | |
2782 | * 'avg_completion_time / 2' as the pre-sleep target. | |
2783 | */ | |
2784 | kt = nsecs; | |
2785 | ||
2786 | mode = HRTIMER_MODE_REL; | |
2787 | hrtimer_init_on_stack(&hs.timer, CLOCK_MONOTONIC, mode); | |
2788 | hrtimer_set_expires(&hs.timer, kt); | |
2789 | ||
2790 | hrtimer_init_sleeper(&hs, current); | |
2791 | do { | |
2792 | if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags)) | |
2793 | break; | |
2794 | set_current_state(TASK_UNINTERRUPTIBLE); | |
2795 | hrtimer_start_expires(&hs.timer, mode); | |
2796 | if (hs.task) | |
2797 | io_schedule(); | |
2798 | hrtimer_cancel(&hs.timer); | |
2799 | mode = HRTIMER_MODE_ABS; | |
2800 | } while (hs.task && !signal_pending(current)); | |
2801 | ||
2802 | __set_current_state(TASK_RUNNING); | |
2803 | destroy_hrtimer_on_stack(&hs.timer); | |
2804 | return true; | |
2805 | } | |
2806 | ||
2807 | static bool __blk_mq_poll(struct blk_mq_hw_ctx *hctx, struct request *rq) | |
2808 | { | |
2809 | struct request_queue *q = hctx->queue; | |
2810 | long state; | |
2811 | ||
2812 | /* | |
2813 | * If we sleep, have the caller restart the poll loop to reset | |
2814 | * the state. Like for the other success return cases, the | |
2815 | * caller is responsible for checking if the IO completed. If | |
2816 | * the IO isn't complete, we'll get called again and will go | |
2817 | * straight to the busy poll loop. | |
2818 | */ | |
2819 | if (blk_mq_poll_hybrid_sleep(q, hctx, rq)) | |
2820 | return true; | |
2821 | ||
2822 | hctx->poll_considered++; | |
2823 | ||
2824 | state = current->state; | |
2825 | while (!need_resched()) { | |
2826 | int ret; | |
2827 | ||
2828 | hctx->poll_invoked++; | |
2829 | ||
2830 | ret = q->mq_ops->poll(hctx, rq->tag); | |
2831 | if (ret > 0) { | |
2832 | hctx->poll_success++; | |
2833 | set_current_state(TASK_RUNNING); | |
2834 | return true; | |
2835 | } | |
2836 | ||
2837 | if (signal_pending_state(state, current)) | |
2838 | set_current_state(TASK_RUNNING); | |
2839 | ||
2840 | if (current->state == TASK_RUNNING) | |
2841 | return true; | |
2842 | if (ret < 0) | |
2843 | break; | |
2844 | cpu_relax(); | |
2845 | } | |
2846 | ||
2847 | return false; | |
2848 | } | |
2849 | ||
2850 | bool blk_mq_poll(struct request_queue *q, blk_qc_t cookie) | |
2851 | { | |
2852 | struct blk_mq_hw_ctx *hctx; | |
2853 | struct blk_plug *plug; | |
2854 | struct request *rq; | |
2855 | ||
2856 | if (!q->mq_ops || !q->mq_ops->poll || !blk_qc_t_valid(cookie) || | |
2857 | !test_bit(QUEUE_FLAG_POLL, &q->queue_flags)) | |
2858 | return false; | |
2859 | ||
2860 | plug = current->plug; | |
2861 | if (plug) | |
2862 | blk_flush_plug_list(plug, false); | |
2863 | ||
2864 | hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)]; | |
2865 | if (!blk_qc_t_is_internal(cookie)) | |
2866 | rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie)); | |
2867 | else { | |
2868 | rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie)); | |
2869 | /* | |
2870 | * With scheduling, if the request has completed, we'll | |
2871 | * get a NULL return here, as we clear the sched tag when | |
2872 | * that happens. The request still remains valid, like always, | |
2873 | * so we should be safe with just the NULL check. | |
2874 | */ | |
2875 | if (!rq) | |
2876 | return false; | |
2877 | } | |
2878 | ||
2879 | return __blk_mq_poll(hctx, rq); | |
2880 | } | |
2881 | EXPORT_SYMBOL_GPL(blk_mq_poll); | |
2882 | ||
2883 | void blk_mq_disable_hotplug(void) | |
2884 | { | |
2885 | mutex_lock(&all_q_mutex); | |
2886 | } | |
2887 | ||
2888 | void blk_mq_enable_hotplug(void) | |
2889 | { | |
2890 | mutex_unlock(&all_q_mutex); | |
2891 | } | |
2892 | ||
2893 | static int __init blk_mq_init(void) | |
2894 | { | |
2895 | cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL, | |
2896 | blk_mq_hctx_notify_dead); | |
2897 | ||
2898 | cpuhp_setup_state_nocalls(CPUHP_BLK_MQ_PREPARE, "block/mq:prepare", | |
2899 | blk_mq_queue_reinit_prepare, | |
2900 | blk_mq_queue_reinit_dead); | |
2901 | return 0; | |
2902 | } | |
2903 | subsys_initcall(blk_mq_init); |