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