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