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