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