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> | |
fe45e630 | 13 | #include <linux/blk-integrity.h> |
f75782e4 | 14 | #include <linux/kmemleak.h> |
320ae51f JA |
15 | #include <linux/mm.h> |
16 | #include <linux/init.h> | |
17 | #include <linux/slab.h> | |
18 | #include <linux/workqueue.h> | |
19 | #include <linux/smp.h> | |
e41d12f5 | 20 | #include <linux/interrupt.h> |
320ae51f | 21 | #include <linux/llist.h> |
320ae51f JA |
22 | #include <linux/cpu.h> |
23 | #include <linux/cache.h> | |
24 | #include <linux/sched/sysctl.h> | |
105ab3d8 | 25 | #include <linux/sched/topology.h> |
174cd4b1 | 26 | #include <linux/sched/signal.h> |
320ae51f | 27 | #include <linux/delay.h> |
aedcd72f | 28 | #include <linux/crash_dump.h> |
88c7b2b7 | 29 | #include <linux/prefetch.h> |
a892c8d5 | 30 | #include <linux/blk-crypto.h> |
82d981d4 | 31 | #include <linux/part_stat.h> |
320ae51f JA |
32 | |
33 | #include <trace/events/block.h> | |
34 | ||
35 | #include <linux/blk-mq.h> | |
54d4e6ab | 36 | #include <linux/t10-pi.h> |
320ae51f JA |
37 | #include "blk.h" |
38 | #include "blk-mq.h" | |
9c1051aa | 39 | #include "blk-mq-debugfs.h" |
320ae51f | 40 | #include "blk-mq-tag.h" |
986d413b | 41 | #include "blk-pm.h" |
cf43e6be | 42 | #include "blk-stat.h" |
bd166ef1 | 43 | #include "blk-mq-sched.h" |
c1c80384 | 44 | #include "blk-rq-qos.h" |
320ae51f | 45 | |
f9ab4918 | 46 | static DEFINE_PER_CPU(struct llist_head, blk_cpu_done); |
c3077b5d | 47 | |
34dbad5d OS |
48 | static void blk_mq_poll_stats_start(struct request_queue *q); |
49 | static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb); | |
50 | ||
720b8ccc SB |
51 | static int blk_mq_poll_stats_bkt(const struct request *rq) |
52 | { | |
3d244306 | 53 | int ddir, sectors, bucket; |
720b8ccc | 54 | |
99c749a4 | 55 | ddir = rq_data_dir(rq); |
3d244306 | 56 | sectors = blk_rq_stats_sectors(rq); |
720b8ccc | 57 | |
3d244306 | 58 | bucket = ddir + 2 * ilog2(sectors); |
720b8ccc SB |
59 | |
60 | if (bucket < 0) | |
61 | return -1; | |
62 | else if (bucket >= BLK_MQ_POLL_STATS_BKTS) | |
63 | return ddir + BLK_MQ_POLL_STATS_BKTS - 2; | |
64 | ||
65 | return bucket; | |
66 | } | |
67 | ||
3e08773c CH |
68 | #define BLK_QC_T_SHIFT 16 |
69 | #define BLK_QC_T_INTERNAL (1U << 31) | |
70 | ||
f70299f0 CH |
71 | static inline struct blk_mq_hw_ctx *blk_qc_to_hctx(struct request_queue *q, |
72 | blk_qc_t qc) | |
73 | { | |
74 | return q->queue_hw_ctx[(qc & ~BLK_QC_T_INTERNAL) >> BLK_QC_T_SHIFT]; | |
75 | } | |
76 | ||
c6699d6f CH |
77 | static inline struct request *blk_qc_to_rq(struct blk_mq_hw_ctx *hctx, |
78 | blk_qc_t qc) | |
79 | { | |
efbabbe1 CH |
80 | unsigned int tag = qc & ((1U << BLK_QC_T_SHIFT) - 1); |
81 | ||
82 | if (qc & BLK_QC_T_INTERNAL) | |
83 | return blk_mq_tag_to_rq(hctx->sched_tags, tag); | |
84 | return blk_mq_tag_to_rq(hctx->tags, tag); | |
c6699d6f CH |
85 | } |
86 | ||
3e08773c CH |
87 | static inline blk_qc_t blk_rq_to_qc(struct request *rq) |
88 | { | |
89 | return (rq->mq_hctx->queue_num << BLK_QC_T_SHIFT) | | |
90 | (rq->tag != -1 ? | |
91 | rq->tag : (rq->internal_tag | BLK_QC_T_INTERNAL)); | |
92 | } | |
93 | ||
320ae51f | 94 | /* |
85fae294 YY |
95 | * Check if any of the ctx, dispatch list or elevator |
96 | * have pending work in this hardware queue. | |
320ae51f | 97 | */ |
79f720a7 | 98 | static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx) |
320ae51f | 99 | { |
79f720a7 JA |
100 | return !list_empty_careful(&hctx->dispatch) || |
101 | sbitmap_any_bit_set(&hctx->ctx_map) || | |
bd166ef1 | 102 | blk_mq_sched_has_work(hctx); |
1429d7c9 JA |
103 | } |
104 | ||
320ae51f JA |
105 | /* |
106 | * Mark this ctx as having pending work in this hardware queue | |
107 | */ | |
108 | static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx, | |
109 | struct blk_mq_ctx *ctx) | |
110 | { | |
f31967f0 JA |
111 | const int bit = ctx->index_hw[hctx->type]; |
112 | ||
113 | if (!sbitmap_test_bit(&hctx->ctx_map, bit)) | |
114 | sbitmap_set_bit(&hctx->ctx_map, bit); | |
1429d7c9 JA |
115 | } |
116 | ||
117 | static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx, | |
118 | struct blk_mq_ctx *ctx) | |
119 | { | |
f31967f0 JA |
120 | const int bit = ctx->index_hw[hctx->type]; |
121 | ||
122 | sbitmap_clear_bit(&hctx->ctx_map, bit); | |
320ae51f JA |
123 | } |
124 | ||
f299b7c7 | 125 | struct mq_inflight { |
8446fe92 | 126 | struct block_device *part; |
a2e80f6f | 127 | unsigned int inflight[2]; |
f299b7c7 JA |
128 | }; |
129 | ||
7baa8572 | 130 | static bool blk_mq_check_inflight(struct blk_mq_hw_ctx *hctx, |
f299b7c7 JA |
131 | struct request *rq, void *priv, |
132 | bool reserved) | |
133 | { | |
134 | struct mq_inflight *mi = priv; | |
135 | ||
b0d97557 JX |
136 | if ((!mi->part->bd_partno || rq->part == mi->part) && |
137 | blk_mq_rq_state(rq) == MQ_RQ_IN_FLIGHT) | |
bb4e6b14 | 138 | mi->inflight[rq_data_dir(rq)]++; |
7baa8572 JA |
139 | |
140 | return true; | |
f299b7c7 JA |
141 | } |
142 | ||
8446fe92 CH |
143 | unsigned int blk_mq_in_flight(struct request_queue *q, |
144 | struct block_device *part) | |
f299b7c7 | 145 | { |
a2e80f6f | 146 | struct mq_inflight mi = { .part = part }; |
f299b7c7 | 147 | |
f299b7c7 | 148 | blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi); |
e016b782 | 149 | |
a2e80f6f | 150 | return mi.inflight[0] + mi.inflight[1]; |
bf0ddaba OS |
151 | } |
152 | ||
8446fe92 CH |
153 | void blk_mq_in_flight_rw(struct request_queue *q, struct block_device *part, |
154 | unsigned int inflight[2]) | |
bf0ddaba | 155 | { |
a2e80f6f | 156 | struct mq_inflight mi = { .part = part }; |
bf0ddaba | 157 | |
bb4e6b14 | 158 | blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi); |
a2e80f6f PB |
159 | inflight[0] = mi.inflight[0]; |
160 | inflight[1] = mi.inflight[1]; | |
bf0ddaba OS |
161 | } |
162 | ||
1671d522 | 163 | void blk_freeze_queue_start(struct request_queue *q) |
43a5e4e2 | 164 | { |
7996a8b5 BL |
165 | mutex_lock(&q->mq_freeze_lock); |
166 | if (++q->mq_freeze_depth == 1) { | |
3ef28e83 | 167 | percpu_ref_kill(&q->q_usage_counter); |
7996a8b5 | 168 | mutex_unlock(&q->mq_freeze_lock); |
344e9ffc | 169 | if (queue_is_mq(q)) |
055f6e18 | 170 | blk_mq_run_hw_queues(q, false); |
7996a8b5 BL |
171 | } else { |
172 | mutex_unlock(&q->mq_freeze_lock); | |
cddd5d17 | 173 | } |
f3af020b | 174 | } |
1671d522 | 175 | EXPORT_SYMBOL_GPL(blk_freeze_queue_start); |
f3af020b | 176 | |
6bae363e | 177 | void blk_mq_freeze_queue_wait(struct request_queue *q) |
f3af020b | 178 | { |
3ef28e83 | 179 | wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter)); |
43a5e4e2 | 180 | } |
6bae363e | 181 | EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait); |
43a5e4e2 | 182 | |
f91328c4 KB |
183 | int blk_mq_freeze_queue_wait_timeout(struct request_queue *q, |
184 | unsigned long timeout) | |
185 | { | |
186 | return wait_event_timeout(q->mq_freeze_wq, | |
187 | percpu_ref_is_zero(&q->q_usage_counter), | |
188 | timeout); | |
189 | } | |
190 | EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout); | |
43a5e4e2 | 191 | |
f3af020b TH |
192 | /* |
193 | * Guarantee no request is in use, so we can change any data structure of | |
194 | * the queue afterward. | |
195 | */ | |
3ef28e83 | 196 | void blk_freeze_queue(struct request_queue *q) |
f3af020b | 197 | { |
3ef28e83 DW |
198 | /* |
199 | * In the !blk_mq case we are only calling this to kill the | |
200 | * q_usage_counter, otherwise this increases the freeze depth | |
201 | * and waits for it to return to zero. For this reason there is | |
202 | * no blk_unfreeze_queue(), and blk_freeze_queue() is not | |
203 | * exported to drivers as the only user for unfreeze is blk_mq. | |
204 | */ | |
1671d522 | 205 | blk_freeze_queue_start(q); |
f3af020b TH |
206 | blk_mq_freeze_queue_wait(q); |
207 | } | |
3ef28e83 DW |
208 | |
209 | void blk_mq_freeze_queue(struct request_queue *q) | |
210 | { | |
211 | /* | |
212 | * ...just an alias to keep freeze and unfreeze actions balanced | |
213 | * in the blk_mq_* namespace | |
214 | */ | |
215 | blk_freeze_queue(q); | |
216 | } | |
c761d96b | 217 | EXPORT_SYMBOL_GPL(blk_mq_freeze_queue); |
f3af020b | 218 | |
aec89dc5 | 219 | void __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic) |
320ae51f | 220 | { |
7996a8b5 | 221 | mutex_lock(&q->mq_freeze_lock); |
aec89dc5 CH |
222 | if (force_atomic) |
223 | q->q_usage_counter.data->force_atomic = true; | |
7996a8b5 BL |
224 | q->mq_freeze_depth--; |
225 | WARN_ON_ONCE(q->mq_freeze_depth < 0); | |
226 | if (!q->mq_freeze_depth) { | |
bdd63160 | 227 | percpu_ref_resurrect(&q->q_usage_counter); |
320ae51f | 228 | wake_up_all(&q->mq_freeze_wq); |
add703fd | 229 | } |
7996a8b5 | 230 | mutex_unlock(&q->mq_freeze_lock); |
320ae51f | 231 | } |
aec89dc5 CH |
232 | |
233 | void blk_mq_unfreeze_queue(struct request_queue *q) | |
234 | { | |
235 | __blk_mq_unfreeze_queue(q, false); | |
236 | } | |
b4c6a028 | 237 | EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue); |
320ae51f | 238 | |
852ec809 BVA |
239 | /* |
240 | * FIXME: replace the scsi_internal_device_*block_nowait() calls in the | |
241 | * mpt3sas driver such that this function can be removed. | |
242 | */ | |
243 | void blk_mq_quiesce_queue_nowait(struct request_queue *q) | |
244 | { | |
e70feb8b ML |
245 | unsigned long flags; |
246 | ||
247 | spin_lock_irqsave(&q->queue_lock, flags); | |
248 | if (!q->quiesce_depth++) | |
249 | blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q); | |
250 | spin_unlock_irqrestore(&q->queue_lock, flags); | |
852ec809 BVA |
251 | } |
252 | EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait); | |
253 | ||
6a83e74d | 254 | /** |
9ef4d020 | 255 | * blk_mq_wait_quiesce_done() - wait until in-progress quiesce is done |
6a83e74d BVA |
256 | * @q: request queue. |
257 | * | |
9ef4d020 ML |
258 | * Note: it is driver's responsibility for making sure that quiesce has |
259 | * been started. | |
6a83e74d | 260 | */ |
9ef4d020 | 261 | void blk_mq_wait_quiesce_done(struct request_queue *q) |
6a83e74d | 262 | { |
704b914f ML |
263 | if (blk_queue_has_srcu(q)) |
264 | synchronize_srcu(q->srcu); | |
265 | else | |
6a83e74d BVA |
266 | synchronize_rcu(); |
267 | } | |
9ef4d020 ML |
268 | EXPORT_SYMBOL_GPL(blk_mq_wait_quiesce_done); |
269 | ||
270 | /** | |
271 | * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished | |
272 | * @q: request queue. | |
273 | * | |
274 | * Note: this function does not prevent that the struct request end_io() | |
275 | * callback function is invoked. Once this function is returned, we make | |
276 | * sure no dispatch can happen until the queue is unquiesced via | |
277 | * blk_mq_unquiesce_queue(). | |
278 | */ | |
279 | void blk_mq_quiesce_queue(struct request_queue *q) | |
280 | { | |
281 | blk_mq_quiesce_queue_nowait(q); | |
282 | blk_mq_wait_quiesce_done(q); | |
283 | } | |
6a83e74d BVA |
284 | EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue); |
285 | ||
e4e73913 ML |
286 | /* |
287 | * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue() | |
288 | * @q: request queue. | |
289 | * | |
290 | * This function recovers queue into the state before quiescing | |
291 | * which is done by blk_mq_quiesce_queue. | |
292 | */ | |
293 | void blk_mq_unquiesce_queue(struct request_queue *q) | |
294 | { | |
e70feb8b ML |
295 | unsigned long flags; |
296 | bool run_queue = false; | |
297 | ||
298 | spin_lock_irqsave(&q->queue_lock, flags); | |
299 | if (WARN_ON_ONCE(q->quiesce_depth <= 0)) { | |
300 | ; | |
301 | } else if (!--q->quiesce_depth) { | |
302 | blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q); | |
303 | run_queue = true; | |
304 | } | |
305 | spin_unlock_irqrestore(&q->queue_lock, flags); | |
f4560ffe | 306 | |
1d9e9bc6 | 307 | /* dispatch requests which are inserted during quiescing */ |
e70feb8b ML |
308 | if (run_queue) |
309 | blk_mq_run_hw_queues(q, true); | |
e4e73913 ML |
310 | } |
311 | EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue); | |
312 | ||
aed3ea94 JA |
313 | void blk_mq_wake_waiters(struct request_queue *q) |
314 | { | |
315 | struct blk_mq_hw_ctx *hctx; | |
316 | unsigned int i; | |
317 | ||
318 | queue_for_each_hw_ctx(q, hctx, i) | |
319 | if (blk_mq_hw_queue_mapped(hctx)) | |
320 | blk_mq_tag_wakeup_all(hctx->tags, true); | |
321 | } | |
322 | ||
52fdbbcc CH |
323 | void blk_rq_init(struct request_queue *q, struct request *rq) |
324 | { | |
325 | memset(rq, 0, sizeof(*rq)); | |
326 | ||
327 | INIT_LIST_HEAD(&rq->queuelist); | |
328 | rq->q = q; | |
329 | rq->__sector = (sector_t) -1; | |
330 | INIT_HLIST_NODE(&rq->hash); | |
331 | RB_CLEAR_NODE(&rq->rb_node); | |
332 | rq->tag = BLK_MQ_NO_TAG; | |
333 | rq->internal_tag = BLK_MQ_NO_TAG; | |
334 | rq->start_time_ns = ktime_get_ns(); | |
335 | rq->part = NULL; | |
336 | blk_crypto_rq_set_defaults(rq); | |
337 | } | |
338 | EXPORT_SYMBOL(blk_rq_init); | |
339 | ||
e4cdf1a1 | 340 | static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data, |
fe6134f6 | 341 | struct blk_mq_tags *tags, unsigned int tag, u64 alloc_time_ns) |
320ae51f | 342 | { |
605f784e PB |
343 | struct blk_mq_ctx *ctx = data->ctx; |
344 | struct blk_mq_hw_ctx *hctx = data->hctx; | |
345 | struct request_queue *q = data->q; | |
e4cdf1a1 | 346 | struct request *rq = tags->static_rqs[tag]; |
c3a148d2 | 347 | |
c7b84d42 JA |
348 | rq->q = q; |
349 | rq->mq_ctx = ctx; | |
350 | rq->mq_hctx = hctx; | |
351 | rq->cmd_flags = data->cmd_flags; | |
352 | ||
353 | if (data->flags & BLK_MQ_REQ_PM) | |
354 | data->rq_flags |= RQF_PM; | |
355 | if (blk_queue_io_stat(q)) | |
356 | data->rq_flags |= RQF_IO_STAT; | |
357 | rq->rq_flags = data->rq_flags; | |
358 | ||
56f8da64 | 359 | if (!(data->rq_flags & RQF_ELV)) { |
e4cdf1a1 | 360 | rq->tag = tag; |
76647368 | 361 | rq->internal_tag = BLK_MQ_NO_TAG; |
56f8da64 JA |
362 | } else { |
363 | rq->tag = BLK_MQ_NO_TAG; | |
364 | rq->internal_tag = tag; | |
e4cdf1a1 | 365 | } |
c7b84d42 | 366 | rq->timeout = 0; |
e4cdf1a1 | 367 | |
4f266f2b PB |
368 | if (blk_mq_need_time_stamp(rq)) |
369 | rq->start_time_ns = ktime_get_ns(); | |
370 | else | |
371 | rq->start_time_ns = 0; | |
af76e555 | 372 | rq->part = NULL; |
6f816b4b TH |
373 | #ifdef CONFIG_BLK_RQ_ALLOC_TIME |
374 | rq->alloc_time_ns = alloc_time_ns; | |
375 | #endif | |
544ccc8d | 376 | rq->io_start_time_ns = 0; |
3d244306 | 377 | rq->stats_sectors = 0; |
af76e555 CH |
378 | rq->nr_phys_segments = 0; |
379 | #if defined(CONFIG_BLK_DEV_INTEGRITY) | |
380 | rq->nr_integrity_segments = 0; | |
381 | #endif | |
af76e555 CH |
382 | rq->end_io = NULL; |
383 | rq->end_io_data = NULL; | |
af76e555 | 384 | |
4f266f2b PB |
385 | blk_crypto_rq_set_defaults(rq); |
386 | INIT_LIST_HEAD(&rq->queuelist); | |
387 | /* tag was already set */ | |
388 | WRITE_ONCE(rq->deadline, 0); | |
0a467d0f | 389 | req_ref_set(rq, 1); |
7ea4d8a4 | 390 | |
4f266f2b | 391 | if (rq->rq_flags & RQF_ELV) { |
7ea4d8a4 CH |
392 | struct elevator_queue *e = data->q->elevator; |
393 | ||
4f266f2b PB |
394 | INIT_HLIST_NODE(&rq->hash); |
395 | RB_CLEAR_NODE(&rq->rb_node); | |
396 | ||
397 | if (!op_is_flush(data->cmd_flags) && | |
398 | e->type->ops.prepare_request) { | |
7ea4d8a4 CH |
399 | e->type->ops.prepare_request(rq); |
400 | rq->rq_flags |= RQF_ELVPRIV; | |
401 | } | |
402 | } | |
403 | ||
e4cdf1a1 | 404 | return rq; |
5dee8577 CH |
405 | } |
406 | ||
349302da JA |
407 | static inline struct request * |
408 | __blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data, | |
409 | u64 alloc_time_ns) | |
410 | { | |
411 | unsigned int tag, tag_offset; | |
fe6134f6 | 412 | struct blk_mq_tags *tags; |
349302da | 413 | struct request *rq; |
fe6134f6 | 414 | unsigned long tag_mask; |
349302da JA |
415 | int i, nr = 0; |
416 | ||
fe6134f6 JA |
417 | tag_mask = blk_mq_get_tags(data, data->nr_tags, &tag_offset); |
418 | if (unlikely(!tag_mask)) | |
349302da JA |
419 | return NULL; |
420 | ||
fe6134f6 JA |
421 | tags = blk_mq_tags_from_data(data); |
422 | for (i = 0; tag_mask; i++) { | |
423 | if (!(tag_mask & (1UL << i))) | |
349302da JA |
424 | continue; |
425 | tag = tag_offset + i; | |
a22c00be | 426 | prefetch(tags->static_rqs[tag]); |
fe6134f6 JA |
427 | tag_mask &= ~(1UL << i); |
428 | rq = blk_mq_rq_ctx_init(data, tags, tag, alloc_time_ns); | |
013a7f95 | 429 | rq_list_add(data->cached_rq, rq); |
c5fc7b93 | 430 | nr++; |
349302da | 431 | } |
c5fc7b93 JA |
432 | /* caller already holds a reference, add for remainder */ |
433 | percpu_ref_get_many(&data->q->q_usage_counter, nr - 1); | |
349302da JA |
434 | data->nr_tags -= nr; |
435 | ||
013a7f95 | 436 | return rq_list_pop(data->cached_rq); |
349302da JA |
437 | } |
438 | ||
b90cfaed | 439 | static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data) |
d2c0d383 | 440 | { |
e6e7abff | 441 | struct request_queue *q = data->q; |
6f816b4b | 442 | u64 alloc_time_ns = 0; |
47c122e3 | 443 | struct request *rq; |
600c3b0c | 444 | unsigned int tag; |
d2c0d383 | 445 | |
6f816b4b TH |
446 | /* alloc_time includes depth and tag waits */ |
447 | if (blk_queue_rq_alloc_time(q)) | |
448 | alloc_time_ns = ktime_get_ns(); | |
449 | ||
f9afca4d | 450 | if (data->cmd_flags & REQ_NOWAIT) |
03a07c92 | 451 | data->flags |= BLK_MQ_REQ_NOWAIT; |
d2c0d383 | 452 | |
781dd830 JA |
453 | if (q->elevator) { |
454 | struct elevator_queue *e = q->elevator; | |
455 | ||
456 | data->rq_flags |= RQF_ELV; | |
457 | ||
d2c0d383 | 458 | /* |
8d663f34 | 459 | * Flush/passthrough requests are special and go directly to the |
17a51199 JA |
460 | * dispatch list. Don't include reserved tags in the |
461 | * limiting, as it isn't useful. | |
d2c0d383 | 462 | */ |
f9afca4d | 463 | if (!op_is_flush(data->cmd_flags) && |
8d663f34 | 464 | !blk_op_is_passthrough(data->cmd_flags) && |
f9afca4d | 465 | e->type->ops.limit_depth && |
17a51199 | 466 | !(data->flags & BLK_MQ_REQ_RESERVED)) |
f9afca4d | 467 | e->type->ops.limit_depth(data->cmd_flags, data); |
d2c0d383 CH |
468 | } |
469 | ||
bf0beec0 | 470 | retry: |
600c3b0c CH |
471 | data->ctx = blk_mq_get_ctx(q); |
472 | data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx); | |
781dd830 | 473 | if (!(data->rq_flags & RQF_ELV)) |
600c3b0c CH |
474 | blk_mq_tag_busy(data->hctx); |
475 | ||
349302da JA |
476 | /* |
477 | * Try batched alloc if we want more than 1 tag. | |
478 | */ | |
479 | if (data->nr_tags > 1) { | |
480 | rq = __blk_mq_alloc_requests_batch(data, alloc_time_ns); | |
481 | if (rq) | |
482 | return rq; | |
483 | data->nr_tags = 1; | |
484 | } | |
485 | ||
bf0beec0 ML |
486 | /* |
487 | * Waiting allocations only fail because of an inactive hctx. In that | |
488 | * case just retry the hctx assignment and tag allocation as CPU hotplug | |
489 | * should have migrated us to an online CPU by now. | |
490 | */ | |
e4cdf1a1 | 491 | tag = blk_mq_get_tag(data); |
bf0beec0 ML |
492 | if (tag == BLK_MQ_NO_TAG) { |
493 | if (data->flags & BLK_MQ_REQ_NOWAIT) | |
494 | return NULL; | |
bf0beec0 | 495 | /* |
349302da JA |
496 | * Give up the CPU and sleep for a random short time to |
497 | * ensure that thread using a realtime scheduling class | |
498 | * are migrated off the CPU, and thus off the hctx that | |
499 | * is going away. | |
bf0beec0 ML |
500 | */ |
501 | msleep(3); | |
502 | goto retry; | |
503 | } | |
47c122e3 | 504 | |
fe6134f6 JA |
505 | return blk_mq_rq_ctx_init(data, blk_mq_tags_from_data(data), tag, |
506 | alloc_time_ns); | |
d2c0d383 CH |
507 | } |
508 | ||
cd6ce148 | 509 | struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op, |
9a95e4ef | 510 | blk_mq_req_flags_t flags) |
320ae51f | 511 | { |
e6e7abff CH |
512 | struct blk_mq_alloc_data data = { |
513 | .q = q, | |
514 | .flags = flags, | |
515 | .cmd_flags = op, | |
47c122e3 | 516 | .nr_tags = 1, |
e6e7abff | 517 | }; |
bd166ef1 | 518 | struct request *rq; |
a492f075 | 519 | int ret; |
320ae51f | 520 | |
3a0a5299 | 521 | ret = blk_queue_enter(q, flags); |
a492f075 JL |
522 | if (ret) |
523 | return ERR_PTR(ret); | |
320ae51f | 524 | |
b90cfaed | 525 | rq = __blk_mq_alloc_requests(&data); |
bd166ef1 | 526 | if (!rq) |
a5ea5811 | 527 | goto out_queue_exit; |
0c4de0f3 CH |
528 | rq->__data_len = 0; |
529 | rq->__sector = (sector_t) -1; | |
530 | rq->bio = rq->biotail = NULL; | |
320ae51f | 531 | return rq; |
a5ea5811 CH |
532 | out_queue_exit: |
533 | blk_queue_exit(q); | |
534 | return ERR_PTR(-EWOULDBLOCK); | |
320ae51f | 535 | } |
4bb659b1 | 536 | EXPORT_SYMBOL(blk_mq_alloc_request); |
320ae51f | 537 | |
cd6ce148 | 538 | struct request *blk_mq_alloc_request_hctx(struct request_queue *q, |
9a95e4ef | 539 | unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx) |
1f5bd336 | 540 | { |
e6e7abff CH |
541 | struct blk_mq_alloc_data data = { |
542 | .q = q, | |
543 | .flags = flags, | |
544 | .cmd_flags = op, | |
47c122e3 | 545 | .nr_tags = 1, |
e6e7abff | 546 | }; |
600c3b0c | 547 | u64 alloc_time_ns = 0; |
6d2809d5 | 548 | unsigned int cpu; |
600c3b0c | 549 | unsigned int tag; |
1f5bd336 ML |
550 | int ret; |
551 | ||
600c3b0c CH |
552 | /* alloc_time includes depth and tag waits */ |
553 | if (blk_queue_rq_alloc_time(q)) | |
554 | alloc_time_ns = ktime_get_ns(); | |
555 | ||
1f5bd336 ML |
556 | /* |
557 | * If the tag allocator sleeps we could get an allocation for a | |
558 | * different hardware context. No need to complicate the low level | |
559 | * allocator for this for the rare use case of a command tied to | |
560 | * a specific queue. | |
561 | */ | |
600c3b0c | 562 | if (WARN_ON_ONCE(!(flags & (BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED)))) |
1f5bd336 ML |
563 | return ERR_PTR(-EINVAL); |
564 | ||
565 | if (hctx_idx >= q->nr_hw_queues) | |
566 | return ERR_PTR(-EIO); | |
567 | ||
3a0a5299 | 568 | ret = blk_queue_enter(q, flags); |
1f5bd336 ML |
569 | if (ret) |
570 | return ERR_PTR(ret); | |
571 | ||
c8712c6a CH |
572 | /* |
573 | * Check if the hardware context is actually mapped to anything. | |
574 | * If not tell the caller that it should skip this queue. | |
575 | */ | |
a5ea5811 | 576 | ret = -EXDEV; |
e6e7abff CH |
577 | data.hctx = q->queue_hw_ctx[hctx_idx]; |
578 | if (!blk_mq_hw_queue_mapped(data.hctx)) | |
a5ea5811 | 579 | goto out_queue_exit; |
e6e7abff CH |
580 | cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask); |
581 | data.ctx = __blk_mq_get_ctx(q, cpu); | |
1f5bd336 | 582 | |
42fdc5e4 | 583 | if (!q->elevator) |
600c3b0c | 584 | blk_mq_tag_busy(data.hctx); |
781dd830 JA |
585 | else |
586 | data.rq_flags |= RQF_ELV; | |
600c3b0c | 587 | |
a5ea5811 | 588 | ret = -EWOULDBLOCK; |
600c3b0c CH |
589 | tag = blk_mq_get_tag(&data); |
590 | if (tag == BLK_MQ_NO_TAG) | |
a5ea5811 | 591 | goto out_queue_exit; |
fe6134f6 JA |
592 | return blk_mq_rq_ctx_init(&data, blk_mq_tags_from_data(&data), tag, |
593 | alloc_time_ns); | |
600c3b0c | 594 | |
a5ea5811 CH |
595 | out_queue_exit: |
596 | blk_queue_exit(q); | |
597 | return ERR_PTR(ret); | |
1f5bd336 ML |
598 | } |
599 | EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx); | |
600 | ||
12f5b931 KB |
601 | static void __blk_mq_free_request(struct request *rq) |
602 | { | |
603 | struct request_queue *q = rq->q; | |
604 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
ea4f995e | 605 | struct blk_mq_hw_ctx *hctx = rq->mq_hctx; |
12f5b931 KB |
606 | const int sched_tag = rq->internal_tag; |
607 | ||
a892c8d5 | 608 | blk_crypto_free_request(rq); |
986d413b | 609 | blk_pm_mark_last_busy(rq); |
ea4f995e | 610 | rq->mq_hctx = NULL; |
76647368 | 611 | if (rq->tag != BLK_MQ_NO_TAG) |
cae740a0 | 612 | blk_mq_put_tag(hctx->tags, ctx, rq->tag); |
76647368 | 613 | if (sched_tag != BLK_MQ_NO_TAG) |
cae740a0 | 614 | blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag); |
12f5b931 KB |
615 | blk_mq_sched_restart(hctx); |
616 | blk_queue_exit(q); | |
617 | } | |
618 | ||
6af54051 | 619 | void blk_mq_free_request(struct request *rq) |
320ae51f | 620 | { |
320ae51f | 621 | struct request_queue *q = rq->q; |
ea4f995e | 622 | struct blk_mq_hw_ctx *hctx = rq->mq_hctx; |
6af54051 | 623 | |
222ee581 CH |
624 | if ((rq->rq_flags & RQF_ELVPRIV) && |
625 | q->elevator->type->ops.finish_request) | |
626 | q->elevator->type->ops.finish_request(rq); | |
320ae51f | 627 | |
e8064021 | 628 | if (rq->rq_flags & RQF_MQ_INFLIGHT) |
bccf5e26 | 629 | __blk_mq_dec_active_requests(hctx); |
87760e5e | 630 | |
7beb2f84 | 631 | if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq))) |
d152c682 | 632 | laptop_io_completion(q->disk->bdi); |
7beb2f84 | 633 | |
a7905043 | 634 | rq_qos_done(q, rq); |
0d2602ca | 635 | |
12f5b931 | 636 | WRITE_ONCE(rq->state, MQ_RQ_IDLE); |
0a467d0f | 637 | if (req_ref_put_and_test(rq)) |
12f5b931 | 638 | __blk_mq_free_request(rq); |
320ae51f | 639 | } |
1a3b595a | 640 | EXPORT_SYMBOL_GPL(blk_mq_free_request); |
320ae51f | 641 | |
47c122e3 | 642 | void blk_mq_free_plug_rqs(struct blk_plug *plug) |
320ae51f | 643 | { |
013a7f95 | 644 | struct request *rq; |
fe1f4526 | 645 | |
c5fc7b93 | 646 | while ((rq = rq_list_pop(&plug->cached_rq)) != NULL) |
47c122e3 | 647 | blk_mq_free_request(rq); |
47c122e3 | 648 | } |
522a7775 | 649 | |
22350ad7 CH |
650 | void blk_dump_rq_flags(struct request *rq, char *msg) |
651 | { | |
652 | printk(KERN_INFO "%s: dev %s: flags=%llx\n", msg, | |
f3fa33ac | 653 | rq->q->disk ? rq->q->disk->disk_name : "?", |
22350ad7 CH |
654 | (unsigned long long) rq->cmd_flags); |
655 | ||
656 | printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n", | |
657 | (unsigned long long)blk_rq_pos(rq), | |
658 | blk_rq_sectors(rq), blk_rq_cur_sectors(rq)); | |
659 | printk(KERN_INFO " bio %p, biotail %p, len %u\n", | |
660 | rq->bio, rq->biotail, blk_rq_bytes(rq)); | |
661 | } | |
662 | EXPORT_SYMBOL(blk_dump_rq_flags); | |
663 | ||
9be3e06f JA |
664 | static void req_bio_endio(struct request *rq, struct bio *bio, |
665 | unsigned int nbytes, blk_status_t error) | |
666 | { | |
478eb72b | 667 | if (unlikely(error)) { |
9be3e06f | 668 | bio->bi_status = error; |
478eb72b | 669 | } else if (req_op(rq) == REQ_OP_ZONE_APPEND) { |
9be3e06f JA |
670 | /* |
671 | * Partial zone append completions cannot be supported as the | |
672 | * BIO fragments may end up not being written sequentially. | |
673 | */ | |
297db731 | 674 | if (bio->bi_iter.bi_size != nbytes) |
9be3e06f JA |
675 | bio->bi_status = BLK_STS_IOERR; |
676 | else | |
677 | bio->bi_iter.bi_sector = rq->__sector; | |
678 | } | |
679 | ||
478eb72b PB |
680 | bio_advance(bio, nbytes); |
681 | ||
682 | if (unlikely(rq->rq_flags & RQF_QUIET)) | |
683 | bio_set_flag(bio, BIO_QUIET); | |
9be3e06f JA |
684 | /* don't actually finish bio if it's part of flush sequence */ |
685 | if (bio->bi_iter.bi_size == 0 && !(rq->rq_flags & RQF_FLUSH_SEQ)) | |
686 | bio_endio(bio); | |
687 | } | |
688 | ||
689 | static void blk_account_io_completion(struct request *req, unsigned int bytes) | |
690 | { | |
691 | if (req->part && blk_do_io_stat(req)) { | |
692 | const int sgrp = op_stat_group(req_op(req)); | |
693 | ||
694 | part_stat_lock(); | |
695 | part_stat_add(req->part, sectors[sgrp], bytes >> 9); | |
696 | part_stat_unlock(); | |
697 | } | |
698 | } | |
699 | ||
0d7a29a2 CH |
700 | static void blk_print_req_error(struct request *req, blk_status_t status) |
701 | { | |
702 | printk_ratelimited(KERN_ERR | |
703 | "%s error, dev %s, sector %llu op 0x%x:(%s) flags 0x%x " | |
704 | "phys_seg %u prio class %u\n", | |
705 | blk_status_to_str(status), | |
f3fa33ac | 706 | req->q->disk ? req->q->disk->disk_name : "?", |
0d7a29a2 CH |
707 | blk_rq_pos(req), req_op(req), blk_op_str(req_op(req)), |
708 | req->cmd_flags & ~REQ_OP_MASK, | |
709 | req->nr_phys_segments, | |
710 | IOPRIO_PRIO_CLASS(req->ioprio)); | |
711 | } | |
712 | ||
9be3e06f JA |
713 | /** |
714 | * blk_update_request - Complete multiple bytes without completing the request | |
715 | * @req: the request being processed | |
716 | * @error: block status code | |
717 | * @nr_bytes: number of bytes to complete for @req | |
718 | * | |
719 | * Description: | |
720 | * Ends I/O on a number of bytes attached to @req, but doesn't complete | |
721 | * the request structure even if @req doesn't have leftover. | |
722 | * If @req has leftover, sets it up for the next range of segments. | |
723 | * | |
724 | * Passing the result of blk_rq_bytes() as @nr_bytes guarantees | |
725 | * %false return from this function. | |
726 | * | |
727 | * Note: | |
728 | * The RQF_SPECIAL_PAYLOAD flag is ignored on purpose in this function | |
729 | * except in the consistency check at the end of this function. | |
730 | * | |
731 | * Return: | |
732 | * %false - this request doesn't have any more data | |
733 | * %true - this request has more data | |
734 | **/ | |
735 | bool blk_update_request(struct request *req, blk_status_t error, | |
736 | unsigned int nr_bytes) | |
737 | { | |
738 | int total_bytes; | |
739 | ||
8a7d267b | 740 | trace_block_rq_complete(req, error, nr_bytes); |
9be3e06f JA |
741 | |
742 | if (!req->bio) | |
743 | return false; | |
744 | ||
745 | #ifdef CONFIG_BLK_DEV_INTEGRITY | |
746 | if (blk_integrity_rq(req) && req_op(req) == REQ_OP_READ && | |
747 | error == BLK_STS_OK) | |
748 | req->q->integrity.profile->complete_fn(req, nr_bytes); | |
749 | #endif | |
750 | ||
751 | if (unlikely(error && !blk_rq_is_passthrough(req) && | |
752 | !(req->rq_flags & RQF_QUIET))) | |
753 | blk_print_req_error(req, error); | |
754 | ||
755 | blk_account_io_completion(req, nr_bytes); | |
756 | ||
757 | total_bytes = 0; | |
758 | while (req->bio) { | |
759 | struct bio *bio = req->bio; | |
760 | unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes); | |
761 | ||
762 | if (bio_bytes == bio->bi_iter.bi_size) | |
763 | req->bio = bio->bi_next; | |
764 | ||
765 | /* Completion has already been traced */ | |
766 | bio_clear_flag(bio, BIO_TRACE_COMPLETION); | |
767 | req_bio_endio(req, bio, bio_bytes, error); | |
768 | ||
769 | total_bytes += bio_bytes; | |
770 | nr_bytes -= bio_bytes; | |
771 | ||
772 | if (!nr_bytes) | |
773 | break; | |
774 | } | |
775 | ||
776 | /* | |
777 | * completely done | |
778 | */ | |
779 | if (!req->bio) { | |
780 | /* | |
781 | * Reset counters so that the request stacking driver | |
782 | * can find how many bytes remain in the request | |
783 | * later. | |
784 | */ | |
785 | req->__data_len = 0; | |
786 | return false; | |
787 | } | |
788 | ||
789 | req->__data_len -= total_bytes; | |
790 | ||
791 | /* update sector only for requests with clear definition of sector */ | |
792 | if (!blk_rq_is_passthrough(req)) | |
793 | req->__sector += total_bytes >> 9; | |
794 | ||
795 | /* mixed attributes always follow the first bio */ | |
796 | if (req->rq_flags & RQF_MIXED_MERGE) { | |
797 | req->cmd_flags &= ~REQ_FAILFAST_MASK; | |
798 | req->cmd_flags |= req->bio->bi_opf & REQ_FAILFAST_MASK; | |
799 | } | |
800 | ||
801 | if (!(req->rq_flags & RQF_SPECIAL_PAYLOAD)) { | |
802 | /* | |
803 | * If total number of sectors is less than the first segment | |
804 | * size, something has gone terribly wrong. | |
805 | */ | |
806 | if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) { | |
807 | blk_dump_rq_flags(req, "request botched"); | |
808 | req->__data_len = blk_rq_cur_bytes(req); | |
809 | } | |
810 | ||
811 | /* recalculate the number of segments */ | |
812 | req->nr_phys_segments = blk_recalc_rq_segments(req); | |
813 | } | |
814 | ||
815 | return true; | |
816 | } | |
817 | EXPORT_SYMBOL_GPL(blk_update_request); | |
818 | ||
450b7879 CH |
819 | static void __blk_account_io_done(struct request *req, u64 now) |
820 | { | |
821 | const int sgrp = op_stat_group(req_op(req)); | |
822 | ||
823 | part_stat_lock(); | |
824 | update_io_ticks(req->part, jiffies, true); | |
825 | part_stat_inc(req->part, ios[sgrp]); | |
826 | part_stat_add(req->part, nsecs[sgrp], now - req->start_time_ns); | |
827 | part_stat_unlock(); | |
828 | } | |
829 | ||
830 | static inline void blk_account_io_done(struct request *req, u64 now) | |
831 | { | |
832 | /* | |
833 | * Account IO completion. flush_rq isn't accounted as a | |
834 | * normal IO on queueing nor completion. Accounting the | |
835 | * containing request is enough. | |
836 | */ | |
837 | if (blk_do_io_stat(req) && req->part && | |
838 | !(req->rq_flags & RQF_FLUSH_SEQ)) | |
839 | __blk_account_io_done(req, now); | |
840 | } | |
841 | ||
842 | static void __blk_account_io_start(struct request *rq) | |
843 | { | |
844 | /* passthrough requests can hold bios that do not have ->bi_bdev set */ | |
845 | if (rq->bio && rq->bio->bi_bdev) | |
846 | rq->part = rq->bio->bi_bdev; | |
f3fa33ac CH |
847 | else if (rq->q->disk) |
848 | rq->part = rq->q->disk->part0; | |
450b7879 CH |
849 | |
850 | part_stat_lock(); | |
851 | update_io_ticks(rq->part, jiffies, false); | |
852 | part_stat_unlock(); | |
853 | } | |
854 | ||
855 | static inline void blk_account_io_start(struct request *req) | |
856 | { | |
857 | if (blk_do_io_stat(req)) | |
858 | __blk_account_io_start(req); | |
859 | } | |
860 | ||
f794f335 | 861 | static inline void __blk_mq_end_request_acct(struct request *rq, u64 now) |
320ae51f | 862 | { |
4bc6339a OS |
863 | if (rq->rq_flags & RQF_STATS) { |
864 | blk_mq_poll_stats_start(rq->q); | |
522a7775 | 865 | blk_stat_add(rq, now); |
4bc6339a OS |
866 | } |
867 | ||
87890092 | 868 | blk_mq_sched_completed_request(rq, now); |
522a7775 | 869 | blk_account_io_done(rq, now); |
f794f335 | 870 | } |
522a7775 | 871 | |
f794f335 JA |
872 | inline void __blk_mq_end_request(struct request *rq, blk_status_t error) |
873 | { | |
874 | if (blk_mq_need_time_stamp(rq)) | |
875 | __blk_mq_end_request_acct(rq, ktime_get_ns()); | |
0d11e6ac | 876 | |
91b63639 | 877 | if (rq->end_io) { |
a7905043 | 878 | rq_qos_done(rq->q, rq); |
320ae51f | 879 | rq->end_io(rq, error); |
91b63639 | 880 | } else { |
320ae51f | 881 | blk_mq_free_request(rq); |
91b63639 | 882 | } |
320ae51f | 883 | } |
c8a446ad | 884 | EXPORT_SYMBOL(__blk_mq_end_request); |
63151a44 | 885 | |
2a842aca | 886 | void blk_mq_end_request(struct request *rq, blk_status_t error) |
63151a44 CH |
887 | { |
888 | if (blk_update_request(rq, error, blk_rq_bytes(rq))) | |
889 | BUG(); | |
c8a446ad | 890 | __blk_mq_end_request(rq, error); |
63151a44 | 891 | } |
c8a446ad | 892 | EXPORT_SYMBOL(blk_mq_end_request); |
320ae51f | 893 | |
f794f335 JA |
894 | #define TAG_COMP_BATCH 32 |
895 | ||
896 | static inline void blk_mq_flush_tag_batch(struct blk_mq_hw_ctx *hctx, | |
897 | int *tag_array, int nr_tags) | |
898 | { | |
899 | struct request_queue *q = hctx->queue; | |
900 | ||
3b87c6ea ML |
901 | /* |
902 | * All requests should have been marked as RQF_MQ_INFLIGHT, so | |
903 | * update hctx->nr_active in batch | |
904 | */ | |
905 | if (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) | |
906 | __blk_mq_sub_active_requests(hctx, nr_tags); | |
907 | ||
f794f335 JA |
908 | blk_mq_put_tags(hctx->tags, tag_array, nr_tags); |
909 | percpu_ref_put_many(&q->q_usage_counter, nr_tags); | |
910 | } | |
911 | ||
912 | void blk_mq_end_request_batch(struct io_comp_batch *iob) | |
913 | { | |
914 | int tags[TAG_COMP_BATCH], nr_tags = 0; | |
02f7eab0 | 915 | struct blk_mq_hw_ctx *cur_hctx = NULL; |
f794f335 JA |
916 | struct request *rq; |
917 | u64 now = 0; | |
918 | ||
919 | if (iob->need_ts) | |
920 | now = ktime_get_ns(); | |
921 | ||
922 | while ((rq = rq_list_pop(&iob->req_list)) != NULL) { | |
923 | prefetch(rq->bio); | |
924 | prefetch(rq->rq_next); | |
925 | ||
926 | blk_update_request(rq, BLK_STS_OK, blk_rq_bytes(rq)); | |
927 | if (iob->need_ts) | |
928 | __blk_mq_end_request_acct(rq, now); | |
929 | ||
98b26a0e JA |
930 | rq_qos_done(rq->q, rq); |
931 | ||
f794f335 | 932 | WRITE_ONCE(rq->state, MQ_RQ_IDLE); |
0a467d0f | 933 | if (!req_ref_put_and_test(rq)) |
f794f335 JA |
934 | continue; |
935 | ||
936 | blk_crypto_free_request(rq); | |
937 | blk_pm_mark_last_busy(rq); | |
f794f335 | 938 | |
02f7eab0 JA |
939 | if (nr_tags == TAG_COMP_BATCH || cur_hctx != rq->mq_hctx) { |
940 | if (cur_hctx) | |
941 | blk_mq_flush_tag_batch(cur_hctx, tags, nr_tags); | |
f794f335 | 942 | nr_tags = 0; |
02f7eab0 | 943 | cur_hctx = rq->mq_hctx; |
f794f335 JA |
944 | } |
945 | tags[nr_tags++] = rq->tag; | |
f794f335 JA |
946 | } |
947 | ||
948 | if (nr_tags) | |
02f7eab0 | 949 | blk_mq_flush_tag_batch(cur_hctx, tags, nr_tags); |
f794f335 JA |
950 | } |
951 | EXPORT_SYMBOL_GPL(blk_mq_end_request_batch); | |
952 | ||
f9ab4918 | 953 | static void blk_complete_reqs(struct llist_head *list) |
320ae51f | 954 | { |
f9ab4918 SAS |
955 | struct llist_node *entry = llist_reverse_order(llist_del_all(list)); |
956 | struct request *rq, *next; | |
c3077b5d | 957 | |
f9ab4918 | 958 | llist_for_each_entry_safe(rq, next, entry, ipi_list) |
c3077b5d | 959 | rq->q->mq_ops->complete(rq); |
320ae51f | 960 | } |
320ae51f | 961 | |
f9ab4918 | 962 | static __latent_entropy void blk_done_softirq(struct softirq_action *h) |
320ae51f | 963 | { |
f9ab4918 | 964 | blk_complete_reqs(this_cpu_ptr(&blk_cpu_done)); |
115243f5 CH |
965 | } |
966 | ||
c3077b5d CH |
967 | static int blk_softirq_cpu_dead(unsigned int cpu) |
968 | { | |
f9ab4918 | 969 | blk_complete_reqs(&per_cpu(blk_cpu_done, cpu)); |
c3077b5d CH |
970 | return 0; |
971 | } | |
972 | ||
40d09b53 | 973 | static void __blk_mq_complete_request_remote(void *data) |
c3077b5d | 974 | { |
f9ab4918 | 975 | __raise_softirq_irqoff(BLOCK_SOFTIRQ); |
c3077b5d CH |
976 | } |
977 | ||
96339526 CH |
978 | static inline bool blk_mq_complete_need_ipi(struct request *rq) |
979 | { | |
980 | int cpu = raw_smp_processor_id(); | |
981 | ||
982 | if (!IS_ENABLED(CONFIG_SMP) || | |
983 | !test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) | |
984 | return false; | |
71425189 SAS |
985 | /* |
986 | * With force threaded interrupts enabled, raising softirq from an SMP | |
987 | * function call will always result in waking the ksoftirqd thread. | |
988 | * This is probably worse than completing the request on a different | |
989 | * cache domain. | |
990 | */ | |
91cc470e | 991 | if (force_irqthreads()) |
71425189 | 992 | return false; |
96339526 CH |
993 | |
994 | /* same CPU or cache domain? Complete locally */ | |
995 | if (cpu == rq->mq_ctx->cpu || | |
996 | (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags) && | |
997 | cpus_share_cache(cpu, rq->mq_ctx->cpu))) | |
998 | return false; | |
999 | ||
1000 | /* don't try to IPI to an offline CPU */ | |
1001 | return cpu_online(rq->mq_ctx->cpu); | |
1002 | } | |
1003 | ||
f9ab4918 SAS |
1004 | static void blk_mq_complete_send_ipi(struct request *rq) |
1005 | { | |
1006 | struct llist_head *list; | |
1007 | unsigned int cpu; | |
1008 | ||
1009 | cpu = rq->mq_ctx->cpu; | |
1010 | list = &per_cpu(blk_cpu_done, cpu); | |
1011 | if (llist_add(&rq->ipi_list, list)) { | |
1012 | INIT_CSD(&rq->csd, __blk_mq_complete_request_remote, rq); | |
1013 | smp_call_function_single_async(cpu, &rq->csd); | |
1014 | } | |
1015 | } | |
1016 | ||
1017 | static void blk_mq_raise_softirq(struct request *rq) | |
1018 | { | |
1019 | struct llist_head *list; | |
1020 | ||
1021 | preempt_disable(); | |
1022 | list = this_cpu_ptr(&blk_cpu_done); | |
1023 | if (llist_add(&rq->ipi_list, list)) | |
1024 | raise_softirq(BLOCK_SOFTIRQ); | |
1025 | preempt_enable(); | |
1026 | } | |
1027 | ||
40d09b53 | 1028 | bool blk_mq_complete_request_remote(struct request *rq) |
320ae51f | 1029 | { |
af78ff7c | 1030 | WRITE_ONCE(rq->state, MQ_RQ_COMPLETE); |
36e76539 | 1031 | |
4ab32bf3 JA |
1032 | /* |
1033 | * For a polled request, always complete locallly, it's pointless | |
1034 | * to redirect the completion. | |
1035 | */ | |
6ce913fe | 1036 | if (rq->cmd_flags & REQ_POLLED) |
40d09b53 | 1037 | return false; |
38535201 | 1038 | |
96339526 | 1039 | if (blk_mq_complete_need_ipi(rq)) { |
f9ab4918 SAS |
1040 | blk_mq_complete_send_ipi(rq); |
1041 | return true; | |
3d6efbf6 | 1042 | } |
40d09b53 | 1043 | |
f9ab4918 SAS |
1044 | if (rq->q->nr_hw_queues == 1) { |
1045 | blk_mq_raise_softirq(rq); | |
1046 | return true; | |
1047 | } | |
1048 | return false; | |
40d09b53 CH |
1049 | } |
1050 | EXPORT_SYMBOL_GPL(blk_mq_complete_request_remote); | |
1051 | ||
1052 | /** | |
1053 | * blk_mq_complete_request - end I/O on a request | |
1054 | * @rq: the request being processed | |
1055 | * | |
1056 | * Description: | |
1057 | * Complete a request by scheduling the ->complete_rq operation. | |
1058 | **/ | |
1059 | void blk_mq_complete_request(struct request *rq) | |
1060 | { | |
1061 | if (!blk_mq_complete_request_remote(rq)) | |
1062 | rq->q->mq_ops->complete(rq); | |
320ae51f | 1063 | } |
15f73f5b | 1064 | EXPORT_SYMBOL(blk_mq_complete_request); |
30a91cb4 | 1065 | |
105663f7 AA |
1066 | /** |
1067 | * blk_mq_start_request - Start processing a request | |
1068 | * @rq: Pointer to request to be started | |
1069 | * | |
1070 | * Function used by device drivers to notify the block layer that a request | |
1071 | * is going to be processed now, so blk layer can do proper initializations | |
1072 | * such as starting the timeout timer. | |
1073 | */ | |
e2490073 | 1074 | void blk_mq_start_request(struct request *rq) |
320ae51f JA |
1075 | { |
1076 | struct request_queue *q = rq->q; | |
1077 | ||
a54895fa | 1078 | trace_block_rq_issue(rq); |
320ae51f | 1079 | |
cf43e6be | 1080 | if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) { |
00067077 JA |
1081 | u64 start_time; |
1082 | #ifdef CONFIG_BLK_CGROUP | |
1083 | if (rq->bio) | |
1084 | start_time = bio_issue_time(&rq->bio->bi_issue); | |
1085 | else | |
1086 | #endif | |
1087 | start_time = ktime_get_ns(); | |
1088 | rq->io_start_time_ns = start_time; | |
3d244306 | 1089 | rq->stats_sectors = blk_rq_sectors(rq); |
cf43e6be | 1090 | rq->rq_flags |= RQF_STATS; |
a7905043 | 1091 | rq_qos_issue(q, rq); |
cf43e6be JA |
1092 | } |
1093 | ||
1d9bd516 | 1094 | WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE); |
538b7534 | 1095 | |
1d9bd516 | 1096 | blk_add_timer(rq); |
12f5b931 | 1097 | WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT); |
49f5baa5 | 1098 | |
54d4e6ab MG |
1099 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
1100 | if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE) | |
1101 | q->integrity.profile->prepare_fn(rq); | |
1102 | #endif | |
3e08773c CH |
1103 | if (rq->bio && rq->bio->bi_opf & REQ_POLLED) |
1104 | WRITE_ONCE(rq->bio->bi_cookie, blk_rq_to_qc(rq)); | |
320ae51f | 1105 | } |
e2490073 | 1106 | EXPORT_SYMBOL(blk_mq_start_request); |
320ae51f | 1107 | |
4054cff9 CH |
1108 | /** |
1109 | * blk_end_sync_rq - executes a completion event on a request | |
1110 | * @rq: request to complete | |
1111 | * @error: end I/O status of the request | |
1112 | */ | |
1113 | static void blk_end_sync_rq(struct request *rq, blk_status_t error) | |
1114 | { | |
1115 | struct completion *waiting = rq->end_io_data; | |
1116 | ||
1117 | rq->end_io_data = (void *)(uintptr_t)error; | |
1118 | ||
1119 | /* | |
1120 | * complete last, if this is a stack request the process (and thus | |
1121 | * the rq pointer) could be invalid right after this complete() | |
1122 | */ | |
1123 | complete(waiting); | |
1124 | } | |
1125 | ||
1126 | /** | |
1127 | * blk_execute_rq_nowait - insert a request to I/O scheduler for execution | |
4054cff9 CH |
1128 | * @rq: request to insert |
1129 | * @at_head: insert request at head or tail of queue | |
1130 | * @done: I/O completion handler | |
1131 | * | |
1132 | * Description: | |
1133 | * Insert a fully prepared request at the back of the I/O scheduler queue | |
1134 | * for execution. Don't wait for completion. | |
1135 | * | |
1136 | * Note: | |
1137 | * This function will invoke @done directly if the queue is dead. | |
1138 | */ | |
b84ba30b | 1139 | void blk_execute_rq_nowait(struct request *rq, bool at_head, rq_end_io_fn *done) |
4054cff9 CH |
1140 | { |
1141 | WARN_ON(irqs_disabled()); | |
1142 | WARN_ON(!blk_rq_is_passthrough(rq)); | |
1143 | ||
4054cff9 CH |
1144 | rq->end_io = done; |
1145 | ||
1146 | blk_account_io_start(rq); | |
1147 | ||
1148 | /* | |
1149 | * don't check dying flag for MQ because the request won't | |
1150 | * be reused after dying flag is set | |
1151 | */ | |
1152 | blk_mq_sched_insert_request(rq, at_head, true, false); | |
1153 | } | |
1154 | EXPORT_SYMBOL_GPL(blk_execute_rq_nowait); | |
1155 | ||
1156 | static bool blk_rq_is_poll(struct request *rq) | |
1157 | { | |
1158 | if (!rq->mq_hctx) | |
1159 | return false; | |
1160 | if (rq->mq_hctx->type != HCTX_TYPE_POLL) | |
1161 | return false; | |
1162 | if (WARN_ON_ONCE(!rq->bio)) | |
1163 | return false; | |
1164 | return true; | |
1165 | } | |
1166 | ||
1167 | static void blk_rq_poll_completion(struct request *rq, struct completion *wait) | |
1168 | { | |
1169 | do { | |
1170 | bio_poll(rq->bio, NULL, 0); | |
1171 | cond_resched(); | |
1172 | } while (!completion_done(wait)); | |
1173 | } | |
1174 | ||
1175 | /** | |
1176 | * blk_execute_rq - insert a request into queue for execution | |
4054cff9 CH |
1177 | * @rq: request to insert |
1178 | * @at_head: insert request at head or tail of queue | |
1179 | * | |
1180 | * Description: | |
1181 | * Insert a fully prepared request at the back of the I/O scheduler queue | |
1182 | * for execution and wait for completion. | |
1183 | * Return: The blk_status_t result provided to blk_mq_end_request(). | |
1184 | */ | |
b84ba30b | 1185 | blk_status_t blk_execute_rq(struct request *rq, bool at_head) |
4054cff9 CH |
1186 | { |
1187 | DECLARE_COMPLETION_ONSTACK(wait); | |
1188 | unsigned long hang_check; | |
1189 | ||
1190 | rq->end_io_data = &wait; | |
b84ba30b | 1191 | blk_execute_rq_nowait(rq, at_head, blk_end_sync_rq); |
4054cff9 CH |
1192 | |
1193 | /* Prevent hang_check timer from firing at us during very long I/O */ | |
1194 | hang_check = sysctl_hung_task_timeout_secs; | |
1195 | ||
1196 | if (blk_rq_is_poll(rq)) | |
1197 | blk_rq_poll_completion(rq, &wait); | |
1198 | else if (hang_check) | |
1199 | while (!wait_for_completion_io_timeout(&wait, | |
1200 | hang_check * (HZ/2))) | |
1201 | ; | |
1202 | else | |
1203 | wait_for_completion_io(&wait); | |
1204 | ||
1205 | return (blk_status_t)(uintptr_t)rq->end_io_data; | |
1206 | } | |
1207 | EXPORT_SYMBOL(blk_execute_rq); | |
1208 | ||
ed0791b2 | 1209 | static void __blk_mq_requeue_request(struct request *rq) |
320ae51f JA |
1210 | { |
1211 | struct request_queue *q = rq->q; | |
1212 | ||
923218f6 ML |
1213 | blk_mq_put_driver_tag(rq); |
1214 | ||
a54895fa | 1215 | trace_block_rq_requeue(rq); |
a7905043 | 1216 | rq_qos_requeue(q, rq); |
49f5baa5 | 1217 | |
12f5b931 KB |
1218 | if (blk_mq_request_started(rq)) { |
1219 | WRITE_ONCE(rq->state, MQ_RQ_IDLE); | |
da661267 | 1220 | rq->rq_flags &= ~RQF_TIMED_OUT; |
e2490073 | 1221 | } |
320ae51f JA |
1222 | } |
1223 | ||
2b053aca | 1224 | void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list) |
ed0791b2 | 1225 | { |
ed0791b2 | 1226 | __blk_mq_requeue_request(rq); |
ed0791b2 | 1227 | |
105976f5 ML |
1228 | /* this request will be re-inserted to io scheduler queue */ |
1229 | blk_mq_sched_requeue_request(rq); | |
1230 | ||
2b053aca | 1231 | blk_mq_add_to_requeue_list(rq, true, kick_requeue_list); |
ed0791b2 CH |
1232 | } |
1233 | EXPORT_SYMBOL(blk_mq_requeue_request); | |
1234 | ||
6fca6a61 CH |
1235 | static void blk_mq_requeue_work(struct work_struct *work) |
1236 | { | |
1237 | struct request_queue *q = | |
2849450a | 1238 | container_of(work, struct request_queue, requeue_work.work); |
6fca6a61 CH |
1239 | LIST_HEAD(rq_list); |
1240 | struct request *rq, *next; | |
6fca6a61 | 1241 | |
18e9781d | 1242 | spin_lock_irq(&q->requeue_lock); |
6fca6a61 | 1243 | list_splice_init(&q->requeue_list, &rq_list); |
18e9781d | 1244 | spin_unlock_irq(&q->requeue_lock); |
6fca6a61 CH |
1245 | |
1246 | list_for_each_entry_safe(rq, next, &rq_list, queuelist) { | |
aef1897c | 1247 | if (!(rq->rq_flags & (RQF_SOFTBARRIER | RQF_DONTPREP))) |
6fca6a61 CH |
1248 | continue; |
1249 | ||
e8064021 | 1250 | rq->rq_flags &= ~RQF_SOFTBARRIER; |
6fca6a61 | 1251 | list_del_init(&rq->queuelist); |
aef1897c JW |
1252 | /* |
1253 | * If RQF_DONTPREP, rq has contained some driver specific | |
1254 | * data, so insert it to hctx dispatch list to avoid any | |
1255 | * merge. | |
1256 | */ | |
1257 | if (rq->rq_flags & RQF_DONTPREP) | |
01e99aec | 1258 | blk_mq_request_bypass_insert(rq, false, false); |
aef1897c JW |
1259 | else |
1260 | blk_mq_sched_insert_request(rq, true, false, false); | |
6fca6a61 CH |
1261 | } |
1262 | ||
1263 | while (!list_empty(&rq_list)) { | |
1264 | rq = list_entry(rq_list.next, struct request, queuelist); | |
1265 | list_del_init(&rq->queuelist); | |
9e97d295 | 1266 | blk_mq_sched_insert_request(rq, false, false, false); |
6fca6a61 CH |
1267 | } |
1268 | ||
52d7f1b5 | 1269 | blk_mq_run_hw_queues(q, false); |
6fca6a61 CH |
1270 | } |
1271 | ||
2b053aca BVA |
1272 | void blk_mq_add_to_requeue_list(struct request *rq, bool at_head, |
1273 | bool kick_requeue_list) | |
6fca6a61 CH |
1274 | { |
1275 | struct request_queue *q = rq->q; | |
1276 | unsigned long flags; | |
1277 | ||
1278 | /* | |
1279 | * We abuse this flag that is otherwise used by the I/O scheduler to | |
ff821d27 | 1280 | * request head insertion from the workqueue. |
6fca6a61 | 1281 | */ |
e8064021 | 1282 | BUG_ON(rq->rq_flags & RQF_SOFTBARRIER); |
6fca6a61 CH |
1283 | |
1284 | spin_lock_irqsave(&q->requeue_lock, flags); | |
1285 | if (at_head) { | |
e8064021 | 1286 | rq->rq_flags |= RQF_SOFTBARRIER; |
6fca6a61 CH |
1287 | list_add(&rq->queuelist, &q->requeue_list); |
1288 | } else { | |
1289 | list_add_tail(&rq->queuelist, &q->requeue_list); | |
1290 | } | |
1291 | spin_unlock_irqrestore(&q->requeue_lock, flags); | |
2b053aca BVA |
1292 | |
1293 | if (kick_requeue_list) | |
1294 | blk_mq_kick_requeue_list(q); | |
6fca6a61 | 1295 | } |
6fca6a61 CH |
1296 | |
1297 | void blk_mq_kick_requeue_list(struct request_queue *q) | |
1298 | { | |
ae943d20 | 1299 | kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0); |
6fca6a61 CH |
1300 | } |
1301 | EXPORT_SYMBOL(blk_mq_kick_requeue_list); | |
1302 | ||
2849450a MS |
1303 | void blk_mq_delay_kick_requeue_list(struct request_queue *q, |
1304 | unsigned long msecs) | |
1305 | { | |
d4acf365 BVA |
1306 | kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, |
1307 | msecs_to_jiffies(msecs)); | |
2849450a MS |
1308 | } |
1309 | EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list); | |
1310 | ||
3c94d83c JA |
1311 | static bool blk_mq_rq_inflight(struct blk_mq_hw_ctx *hctx, struct request *rq, |
1312 | void *priv, bool reserved) | |
ae879912 JA |
1313 | { |
1314 | /* | |
05a4fed6 | 1315 | * If we find a request that isn't idle and the queue matches, |
3c94d83c | 1316 | * we know the queue is busy. Return false to stop the iteration. |
ae879912 | 1317 | */ |
05a4fed6 | 1318 | if (blk_mq_request_started(rq) && rq->q == hctx->queue) { |
ae879912 JA |
1319 | bool *busy = priv; |
1320 | ||
1321 | *busy = true; | |
1322 | return false; | |
1323 | } | |
1324 | ||
1325 | return true; | |
1326 | } | |
1327 | ||
3c94d83c | 1328 | bool blk_mq_queue_inflight(struct request_queue *q) |
ae879912 JA |
1329 | { |
1330 | bool busy = false; | |
1331 | ||
3c94d83c | 1332 | blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy); |
ae879912 JA |
1333 | return busy; |
1334 | } | |
3c94d83c | 1335 | EXPORT_SYMBOL_GPL(blk_mq_queue_inflight); |
ae879912 | 1336 | |
358f70da | 1337 | static void blk_mq_rq_timed_out(struct request *req, bool reserved) |
320ae51f | 1338 | { |
da661267 | 1339 | req->rq_flags |= RQF_TIMED_OUT; |
d1210d5a CH |
1340 | if (req->q->mq_ops->timeout) { |
1341 | enum blk_eh_timer_return ret; | |
1342 | ||
1343 | ret = req->q->mq_ops->timeout(req, reserved); | |
1344 | if (ret == BLK_EH_DONE) | |
1345 | return; | |
1346 | WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER); | |
46f92d42 | 1347 | } |
d1210d5a CH |
1348 | |
1349 | blk_add_timer(req); | |
87ee7b11 | 1350 | } |
5b3f25fc | 1351 | |
12f5b931 | 1352 | static bool blk_mq_req_expired(struct request *rq, unsigned long *next) |
81481eb4 | 1353 | { |
12f5b931 | 1354 | unsigned long deadline; |
87ee7b11 | 1355 | |
12f5b931 KB |
1356 | if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT) |
1357 | return false; | |
da661267 CH |
1358 | if (rq->rq_flags & RQF_TIMED_OUT) |
1359 | return false; | |
a7af0af3 | 1360 | |
079076b3 | 1361 | deadline = READ_ONCE(rq->deadline); |
12f5b931 KB |
1362 | if (time_after_eq(jiffies, deadline)) |
1363 | return true; | |
a7af0af3 | 1364 | |
12f5b931 KB |
1365 | if (*next == 0) |
1366 | *next = deadline; | |
1367 | else if (time_after(*next, deadline)) | |
1368 | *next = deadline; | |
1369 | return false; | |
87ee7b11 JA |
1370 | } |
1371 | ||
2e315dc0 ML |
1372 | void blk_mq_put_rq_ref(struct request *rq) |
1373 | { | |
a9ed27a7 | 1374 | if (is_flush_rq(rq)) |
2e315dc0 | 1375 | rq->end_io(rq, 0); |
0a467d0f | 1376 | else if (req_ref_put_and_test(rq)) |
2e315dc0 ML |
1377 | __blk_mq_free_request(rq); |
1378 | } | |
1379 | ||
7baa8572 | 1380 | static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx, |
1d9bd516 TH |
1381 | struct request *rq, void *priv, bool reserved) |
1382 | { | |
12f5b931 KB |
1383 | unsigned long *next = priv; |
1384 | ||
1385 | /* | |
c797b40c ML |
1386 | * blk_mq_queue_tag_busy_iter() has locked the request, so it cannot |
1387 | * be reallocated underneath the timeout handler's processing, then | |
1388 | * the expire check is reliable. If the request is not expired, then | |
1389 | * it was completed and reallocated as a new request after returning | |
1390 | * from blk_mq_check_expired(). | |
1d9bd516 | 1391 | */ |
12f5b931 | 1392 | if (blk_mq_req_expired(rq, next)) |
1d9bd516 | 1393 | blk_mq_rq_timed_out(rq, reserved); |
7baa8572 | 1394 | return true; |
1d9bd516 TH |
1395 | } |
1396 | ||
287922eb | 1397 | static void blk_mq_timeout_work(struct work_struct *work) |
320ae51f | 1398 | { |
287922eb CH |
1399 | struct request_queue *q = |
1400 | container_of(work, struct request_queue, timeout_work); | |
12f5b931 | 1401 | unsigned long next = 0; |
1d9bd516 | 1402 | struct blk_mq_hw_ctx *hctx; |
81481eb4 | 1403 | int i; |
320ae51f | 1404 | |
71f79fb3 GKB |
1405 | /* A deadlock might occur if a request is stuck requiring a |
1406 | * timeout at the same time a queue freeze is waiting | |
1407 | * completion, since the timeout code would not be able to | |
1408 | * acquire the queue reference here. | |
1409 | * | |
1410 | * That's why we don't use blk_queue_enter here; instead, we use | |
1411 | * percpu_ref_tryget directly, because we need to be able to | |
1412 | * obtain a reference even in the short window between the queue | |
1413 | * starting to freeze, by dropping the first reference in | |
1671d522 | 1414 | * blk_freeze_queue_start, and the moment the last request is |
71f79fb3 GKB |
1415 | * consumed, marked by the instant q_usage_counter reaches |
1416 | * zero. | |
1417 | */ | |
1418 | if (!percpu_ref_tryget(&q->q_usage_counter)) | |
287922eb CH |
1419 | return; |
1420 | ||
12f5b931 | 1421 | blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next); |
320ae51f | 1422 | |
12f5b931 KB |
1423 | if (next != 0) { |
1424 | mod_timer(&q->timeout, next); | |
0d2602ca | 1425 | } else { |
fcd36c36 BVA |
1426 | /* |
1427 | * Request timeouts are handled as a forward rolling timer. If | |
1428 | * we end up here it means that no requests are pending and | |
1429 | * also that no request has been pending for a while. Mark | |
1430 | * each hctx as idle. | |
1431 | */ | |
f054b56c ML |
1432 | queue_for_each_hw_ctx(q, hctx, i) { |
1433 | /* the hctx may be unmapped, so check it here */ | |
1434 | if (blk_mq_hw_queue_mapped(hctx)) | |
1435 | blk_mq_tag_idle(hctx); | |
1436 | } | |
0d2602ca | 1437 | } |
287922eb | 1438 | blk_queue_exit(q); |
320ae51f JA |
1439 | } |
1440 | ||
88459642 OS |
1441 | struct flush_busy_ctx_data { |
1442 | struct blk_mq_hw_ctx *hctx; | |
1443 | struct list_head *list; | |
1444 | }; | |
1445 | ||
1446 | static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data) | |
1447 | { | |
1448 | struct flush_busy_ctx_data *flush_data = data; | |
1449 | struct blk_mq_hw_ctx *hctx = flush_data->hctx; | |
1450 | struct blk_mq_ctx *ctx = hctx->ctxs[bitnr]; | |
c16d6b5a | 1451 | enum hctx_type type = hctx->type; |
88459642 | 1452 | |
88459642 | 1453 | spin_lock(&ctx->lock); |
c16d6b5a | 1454 | list_splice_tail_init(&ctx->rq_lists[type], flush_data->list); |
e9a99a63 | 1455 | sbitmap_clear_bit(sb, bitnr); |
88459642 OS |
1456 | spin_unlock(&ctx->lock); |
1457 | return true; | |
1458 | } | |
1459 | ||
1429d7c9 JA |
1460 | /* |
1461 | * Process software queues that have been marked busy, splicing them | |
1462 | * to the for-dispatch | |
1463 | */ | |
2c3ad667 | 1464 | void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list) |
1429d7c9 | 1465 | { |
88459642 OS |
1466 | struct flush_busy_ctx_data data = { |
1467 | .hctx = hctx, | |
1468 | .list = list, | |
1469 | }; | |
1429d7c9 | 1470 | |
88459642 | 1471 | sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data); |
1429d7c9 | 1472 | } |
2c3ad667 | 1473 | EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs); |
1429d7c9 | 1474 | |
b347689f ML |
1475 | struct dispatch_rq_data { |
1476 | struct blk_mq_hw_ctx *hctx; | |
1477 | struct request *rq; | |
1478 | }; | |
1479 | ||
1480 | static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr, | |
1481 | void *data) | |
1482 | { | |
1483 | struct dispatch_rq_data *dispatch_data = data; | |
1484 | struct blk_mq_hw_ctx *hctx = dispatch_data->hctx; | |
1485 | struct blk_mq_ctx *ctx = hctx->ctxs[bitnr]; | |
c16d6b5a | 1486 | enum hctx_type type = hctx->type; |
b347689f ML |
1487 | |
1488 | spin_lock(&ctx->lock); | |
c16d6b5a ML |
1489 | if (!list_empty(&ctx->rq_lists[type])) { |
1490 | dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next); | |
b347689f | 1491 | list_del_init(&dispatch_data->rq->queuelist); |
c16d6b5a | 1492 | if (list_empty(&ctx->rq_lists[type])) |
b347689f ML |
1493 | sbitmap_clear_bit(sb, bitnr); |
1494 | } | |
1495 | spin_unlock(&ctx->lock); | |
1496 | ||
1497 | return !dispatch_data->rq; | |
1498 | } | |
1499 | ||
1500 | struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx, | |
1501 | struct blk_mq_ctx *start) | |
1502 | { | |
f31967f0 | 1503 | unsigned off = start ? start->index_hw[hctx->type] : 0; |
b347689f ML |
1504 | struct dispatch_rq_data data = { |
1505 | .hctx = hctx, | |
1506 | .rq = NULL, | |
1507 | }; | |
1508 | ||
1509 | __sbitmap_for_each_set(&hctx->ctx_map, off, | |
1510 | dispatch_rq_from_ctx, &data); | |
1511 | ||
1512 | return data.rq; | |
1513 | } | |
1514 | ||
a808a9d5 | 1515 | static bool __blk_mq_alloc_driver_tag(struct request *rq) |
570e9b73 | 1516 | { |
ae0f1a73 | 1517 | struct sbitmap_queue *bt = &rq->mq_hctx->tags->bitmap_tags; |
570e9b73 | 1518 | unsigned int tag_offset = rq->mq_hctx->tags->nr_reserved_tags; |
570e9b73 ML |
1519 | int tag; |
1520 | ||
568f2700 ML |
1521 | blk_mq_tag_busy(rq->mq_hctx); |
1522 | ||
570e9b73 | 1523 | if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag)) { |
ae0f1a73 | 1524 | bt = &rq->mq_hctx->tags->breserved_tags; |
570e9b73 | 1525 | tag_offset = 0; |
28500850 ML |
1526 | } else { |
1527 | if (!hctx_may_queue(rq->mq_hctx, bt)) | |
1528 | return false; | |
570e9b73 ML |
1529 | } |
1530 | ||
570e9b73 ML |
1531 | tag = __sbitmap_queue_get(bt); |
1532 | if (tag == BLK_MQ_NO_TAG) | |
1533 | return false; | |
1534 | ||
1535 | rq->tag = tag + tag_offset; | |
570e9b73 ML |
1536 | return true; |
1537 | } | |
1538 | ||
a808a9d5 | 1539 | bool __blk_mq_get_driver_tag(struct blk_mq_hw_ctx *hctx, struct request *rq) |
570e9b73 | 1540 | { |
a808a9d5 | 1541 | if (rq->tag == BLK_MQ_NO_TAG && !__blk_mq_alloc_driver_tag(rq)) |
568f2700 ML |
1542 | return false; |
1543 | ||
51db1c37 | 1544 | if ((hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) && |
568f2700 ML |
1545 | !(rq->rq_flags & RQF_MQ_INFLIGHT)) { |
1546 | rq->rq_flags |= RQF_MQ_INFLIGHT; | |
bccf5e26 | 1547 | __blk_mq_inc_active_requests(hctx); |
568f2700 ML |
1548 | } |
1549 | hctx->tags->rqs[rq->tag] = rq; | |
1550 | return true; | |
570e9b73 ML |
1551 | } |
1552 | ||
eb619fdb JA |
1553 | static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode, |
1554 | int flags, void *key) | |
da55f2cc OS |
1555 | { |
1556 | struct blk_mq_hw_ctx *hctx; | |
1557 | ||
1558 | hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait); | |
1559 | ||
5815839b | 1560 | spin_lock(&hctx->dispatch_wait_lock); |
e8618575 JA |
1561 | if (!list_empty(&wait->entry)) { |
1562 | struct sbitmap_queue *sbq; | |
1563 | ||
1564 | list_del_init(&wait->entry); | |
ae0f1a73 | 1565 | sbq = &hctx->tags->bitmap_tags; |
e8618575 JA |
1566 | atomic_dec(&sbq->ws_active); |
1567 | } | |
5815839b ML |
1568 | spin_unlock(&hctx->dispatch_wait_lock); |
1569 | ||
da55f2cc OS |
1570 | blk_mq_run_hw_queue(hctx, true); |
1571 | return 1; | |
1572 | } | |
1573 | ||
f906a6a0 JA |
1574 | /* |
1575 | * Mark us waiting for a tag. For shared tags, this involves hooking us into | |
ee3e4de5 BVA |
1576 | * the tag wakeups. For non-shared tags, we can simply mark us needing a |
1577 | * restart. For both cases, take care to check the condition again after | |
f906a6a0 JA |
1578 | * marking us as waiting. |
1579 | */ | |
2278d69f | 1580 | static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx, |
f906a6a0 | 1581 | struct request *rq) |
da55f2cc | 1582 | { |
ae0f1a73 | 1583 | struct sbitmap_queue *sbq = &hctx->tags->bitmap_tags; |
5815839b | 1584 | struct wait_queue_head *wq; |
f906a6a0 JA |
1585 | wait_queue_entry_t *wait; |
1586 | bool ret; | |
da55f2cc | 1587 | |
51db1c37 | 1588 | if (!(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) { |
684b7324 | 1589 | blk_mq_sched_mark_restart_hctx(hctx); |
f906a6a0 | 1590 | |
c27d53fb BVA |
1591 | /* |
1592 | * It's possible that a tag was freed in the window between the | |
1593 | * allocation failure and adding the hardware queue to the wait | |
1594 | * queue. | |
1595 | * | |
1596 | * Don't clear RESTART here, someone else could have set it. | |
1597 | * At most this will cost an extra queue run. | |
1598 | */ | |
8ab6bb9e | 1599 | return blk_mq_get_driver_tag(rq); |
eb619fdb | 1600 | } |
eb619fdb | 1601 | |
2278d69f | 1602 | wait = &hctx->dispatch_wait; |
c27d53fb BVA |
1603 | if (!list_empty_careful(&wait->entry)) |
1604 | return false; | |
1605 | ||
e8618575 | 1606 | wq = &bt_wait_ptr(sbq, hctx)->wait; |
5815839b ML |
1607 | |
1608 | spin_lock_irq(&wq->lock); | |
1609 | spin_lock(&hctx->dispatch_wait_lock); | |
c27d53fb | 1610 | if (!list_empty(&wait->entry)) { |
5815839b ML |
1611 | spin_unlock(&hctx->dispatch_wait_lock); |
1612 | spin_unlock_irq(&wq->lock); | |
c27d53fb | 1613 | return false; |
eb619fdb JA |
1614 | } |
1615 | ||
e8618575 | 1616 | atomic_inc(&sbq->ws_active); |
5815839b ML |
1617 | wait->flags &= ~WQ_FLAG_EXCLUSIVE; |
1618 | __add_wait_queue(wq, wait); | |
c27d53fb | 1619 | |
da55f2cc | 1620 | /* |
eb619fdb JA |
1621 | * It's possible that a tag was freed in the window between the |
1622 | * allocation failure and adding the hardware queue to the wait | |
1623 | * queue. | |
da55f2cc | 1624 | */ |
8ab6bb9e | 1625 | ret = blk_mq_get_driver_tag(rq); |
c27d53fb | 1626 | if (!ret) { |
5815839b ML |
1627 | spin_unlock(&hctx->dispatch_wait_lock); |
1628 | spin_unlock_irq(&wq->lock); | |
c27d53fb | 1629 | return false; |
eb619fdb | 1630 | } |
c27d53fb BVA |
1631 | |
1632 | /* | |
1633 | * We got a tag, remove ourselves from the wait queue to ensure | |
1634 | * someone else gets the wakeup. | |
1635 | */ | |
c27d53fb | 1636 | list_del_init(&wait->entry); |
e8618575 | 1637 | atomic_dec(&sbq->ws_active); |
5815839b ML |
1638 | spin_unlock(&hctx->dispatch_wait_lock); |
1639 | spin_unlock_irq(&wq->lock); | |
c27d53fb BVA |
1640 | |
1641 | return true; | |
da55f2cc OS |
1642 | } |
1643 | ||
6e768717 ML |
1644 | #define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8 |
1645 | #define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4 | |
1646 | /* | |
1647 | * Update dispatch busy with the Exponential Weighted Moving Average(EWMA): | |
1648 | * - EWMA is one simple way to compute running average value | |
1649 | * - weight(7/8 and 1/8) is applied so that it can decrease exponentially | |
1650 | * - take 4 as factor for avoiding to get too small(0) result, and this | |
1651 | * factor doesn't matter because EWMA decreases exponentially | |
1652 | */ | |
1653 | static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy) | |
1654 | { | |
1655 | unsigned int ewma; | |
1656 | ||
6e768717 ML |
1657 | ewma = hctx->dispatch_busy; |
1658 | ||
1659 | if (!ewma && !busy) | |
1660 | return; | |
1661 | ||
1662 | ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1; | |
1663 | if (busy) | |
1664 | ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR; | |
1665 | ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT; | |
1666 | ||
1667 | hctx->dispatch_busy = ewma; | |
1668 | } | |
1669 | ||
86ff7c2a ML |
1670 | #define BLK_MQ_RESOURCE_DELAY 3 /* ms units */ |
1671 | ||
c92a4103 JT |
1672 | static void blk_mq_handle_dev_resource(struct request *rq, |
1673 | struct list_head *list) | |
1674 | { | |
1675 | struct request *next = | |
1676 | list_first_entry_or_null(list, struct request, queuelist); | |
1677 | ||
1678 | /* | |
1679 | * If an I/O scheduler has been configured and we got a driver tag for | |
1680 | * the next request already, free it. | |
1681 | */ | |
1682 | if (next) | |
1683 | blk_mq_put_driver_tag(next); | |
1684 | ||
1685 | list_add(&rq->queuelist, list); | |
1686 | __blk_mq_requeue_request(rq); | |
1687 | } | |
1688 | ||
0512a75b KB |
1689 | static void blk_mq_handle_zone_resource(struct request *rq, |
1690 | struct list_head *zone_list) | |
1691 | { | |
1692 | /* | |
1693 | * If we end up here it is because we cannot dispatch a request to a | |
1694 | * specific zone due to LLD level zone-write locking or other zone | |
1695 | * related resource not being available. In this case, set the request | |
1696 | * aside in zone_list for retrying it later. | |
1697 | */ | |
1698 | list_add(&rq->queuelist, zone_list); | |
1699 | __blk_mq_requeue_request(rq); | |
1700 | } | |
1701 | ||
75383524 ML |
1702 | enum prep_dispatch { |
1703 | PREP_DISPATCH_OK, | |
1704 | PREP_DISPATCH_NO_TAG, | |
1705 | PREP_DISPATCH_NO_BUDGET, | |
1706 | }; | |
1707 | ||
1708 | static enum prep_dispatch blk_mq_prep_dispatch_rq(struct request *rq, | |
1709 | bool need_budget) | |
1710 | { | |
1711 | struct blk_mq_hw_ctx *hctx = rq->mq_hctx; | |
2a5a24aa | 1712 | int budget_token = -1; |
75383524 | 1713 | |
2a5a24aa ML |
1714 | if (need_budget) { |
1715 | budget_token = blk_mq_get_dispatch_budget(rq->q); | |
1716 | if (budget_token < 0) { | |
1717 | blk_mq_put_driver_tag(rq); | |
1718 | return PREP_DISPATCH_NO_BUDGET; | |
1719 | } | |
1720 | blk_mq_set_rq_budget_token(rq, budget_token); | |
75383524 ML |
1721 | } |
1722 | ||
1723 | if (!blk_mq_get_driver_tag(rq)) { | |
1724 | /* | |
1725 | * The initial allocation attempt failed, so we need to | |
1726 | * rerun the hardware queue when a tag is freed. The | |
1727 | * waitqueue takes care of that. If the queue is run | |
1728 | * before we add this entry back on the dispatch list, | |
1729 | * we'll re-run it below. | |
1730 | */ | |
1731 | if (!blk_mq_mark_tag_wait(hctx, rq)) { | |
1fd40b5e ML |
1732 | /* |
1733 | * All budgets not got from this function will be put | |
1734 | * together during handling partial dispatch | |
1735 | */ | |
1736 | if (need_budget) | |
2a5a24aa | 1737 | blk_mq_put_dispatch_budget(rq->q, budget_token); |
75383524 ML |
1738 | return PREP_DISPATCH_NO_TAG; |
1739 | } | |
1740 | } | |
1741 | ||
1742 | return PREP_DISPATCH_OK; | |
1743 | } | |
1744 | ||
1fd40b5e ML |
1745 | /* release all allocated budgets before calling to blk_mq_dispatch_rq_list */ |
1746 | static void blk_mq_release_budgets(struct request_queue *q, | |
2a5a24aa | 1747 | struct list_head *list) |
1fd40b5e | 1748 | { |
2a5a24aa | 1749 | struct request *rq; |
1fd40b5e | 1750 | |
2a5a24aa ML |
1751 | list_for_each_entry(rq, list, queuelist) { |
1752 | int budget_token = blk_mq_get_rq_budget_token(rq); | |
1fd40b5e | 1753 | |
2a5a24aa ML |
1754 | if (budget_token >= 0) |
1755 | blk_mq_put_dispatch_budget(q, budget_token); | |
1756 | } | |
1fd40b5e ML |
1757 | } |
1758 | ||
1f57f8d4 JA |
1759 | /* |
1760 | * Returns true if we did some work AND can potentially do more. | |
1761 | */ | |
445874e8 | 1762 | bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list, |
1fd40b5e | 1763 | unsigned int nr_budgets) |
320ae51f | 1764 | { |
75383524 | 1765 | enum prep_dispatch prep; |
445874e8 | 1766 | struct request_queue *q = hctx->queue; |
6d6f167c | 1767 | struct request *rq, *nxt; |
fc17b653 | 1768 | int errors, queued; |
86ff7c2a | 1769 | blk_status_t ret = BLK_STS_OK; |
0512a75b | 1770 | LIST_HEAD(zone_list); |
9586e67b | 1771 | bool needs_resource = false; |
320ae51f | 1772 | |
81380ca1 OS |
1773 | if (list_empty(list)) |
1774 | return false; | |
1775 | ||
320ae51f JA |
1776 | /* |
1777 | * Now process all the entries, sending them to the driver. | |
1778 | */ | |
93efe981 | 1779 | errors = queued = 0; |
81380ca1 | 1780 | do { |
74c45052 | 1781 | struct blk_mq_queue_data bd; |
320ae51f | 1782 | |
f04c3df3 | 1783 | rq = list_first_entry(list, struct request, queuelist); |
0bca799b | 1784 | |
445874e8 | 1785 | WARN_ON_ONCE(hctx != rq->mq_hctx); |
1fd40b5e | 1786 | prep = blk_mq_prep_dispatch_rq(rq, !nr_budgets); |
75383524 | 1787 | if (prep != PREP_DISPATCH_OK) |
0bca799b | 1788 | break; |
de148297 | 1789 | |
320ae51f | 1790 | list_del_init(&rq->queuelist); |
320ae51f | 1791 | |
74c45052 | 1792 | bd.rq = rq; |
113285b4 JA |
1793 | |
1794 | /* | |
1795 | * Flag last if we have no more requests, or if we have more | |
1796 | * but can't assign a driver tag to it. | |
1797 | */ | |
1798 | if (list_empty(list)) | |
1799 | bd.last = true; | |
1800 | else { | |
113285b4 | 1801 | nxt = list_first_entry(list, struct request, queuelist); |
8ab6bb9e | 1802 | bd.last = !blk_mq_get_driver_tag(nxt); |
113285b4 | 1803 | } |
74c45052 | 1804 | |
1fd40b5e ML |
1805 | /* |
1806 | * once the request is queued to lld, no need to cover the | |
1807 | * budget any more | |
1808 | */ | |
1809 | if (nr_budgets) | |
1810 | nr_budgets--; | |
74c45052 | 1811 | ret = q->mq_ops->queue_rq(hctx, &bd); |
7bf13729 ML |
1812 | switch (ret) { |
1813 | case BLK_STS_OK: | |
1814 | queued++; | |
320ae51f | 1815 | break; |
7bf13729 | 1816 | case BLK_STS_RESOURCE: |
9586e67b NA |
1817 | needs_resource = true; |
1818 | fallthrough; | |
7bf13729 ML |
1819 | case BLK_STS_DEV_RESOURCE: |
1820 | blk_mq_handle_dev_resource(rq, list); | |
1821 | goto out; | |
1822 | case BLK_STS_ZONE_RESOURCE: | |
0512a75b KB |
1823 | /* |
1824 | * Move the request to zone_list and keep going through | |
1825 | * the dispatch list to find more requests the drive can | |
1826 | * accept. | |
1827 | */ | |
1828 | blk_mq_handle_zone_resource(rq, &zone_list); | |
9586e67b | 1829 | needs_resource = true; |
7bf13729 ML |
1830 | break; |
1831 | default: | |
93efe981 | 1832 | errors++; |
e21ee5a6 | 1833 | blk_mq_end_request(rq, ret); |
320ae51f | 1834 | } |
81380ca1 | 1835 | } while (!list_empty(list)); |
7bf13729 | 1836 | out: |
0512a75b KB |
1837 | if (!list_empty(&zone_list)) |
1838 | list_splice_tail_init(&zone_list, list); | |
1839 | ||
632bfb63 | 1840 | /* If we didn't flush the entire list, we could have told the driver |
1841 | * there was more coming, but that turned out to be a lie. | |
1842 | */ | |
1843 | if ((!list_empty(list) || errors) && q->mq_ops->commit_rqs && queued) | |
1844 | q->mq_ops->commit_rqs(hctx); | |
320ae51f JA |
1845 | /* |
1846 | * Any items that need requeuing? Stuff them into hctx->dispatch, | |
1847 | * that is where we will continue on next queue run. | |
1848 | */ | |
f04c3df3 | 1849 | if (!list_empty(list)) { |
86ff7c2a | 1850 | bool needs_restart; |
75383524 ML |
1851 | /* For non-shared tags, the RESTART check will suffice */ |
1852 | bool no_tag = prep == PREP_DISPATCH_NO_TAG && | |
51db1c37 | 1853 | (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED); |
86ff7c2a | 1854 | |
2a5a24aa ML |
1855 | if (nr_budgets) |
1856 | blk_mq_release_budgets(q, list); | |
86ff7c2a | 1857 | |
320ae51f | 1858 | spin_lock(&hctx->lock); |
01e99aec | 1859 | list_splice_tail_init(list, &hctx->dispatch); |
320ae51f | 1860 | spin_unlock(&hctx->lock); |
f04c3df3 | 1861 | |
d7d8535f ML |
1862 | /* |
1863 | * Order adding requests to hctx->dispatch and checking | |
1864 | * SCHED_RESTART flag. The pair of this smp_mb() is the one | |
1865 | * in blk_mq_sched_restart(). Avoid restart code path to | |
1866 | * miss the new added requests to hctx->dispatch, meantime | |
1867 | * SCHED_RESTART is observed here. | |
1868 | */ | |
1869 | smp_mb(); | |
1870 | ||
9ba52e58 | 1871 | /* |
710c785f BVA |
1872 | * If SCHED_RESTART was set by the caller of this function and |
1873 | * it is no longer set that means that it was cleared by another | |
1874 | * thread and hence that a queue rerun is needed. | |
9ba52e58 | 1875 | * |
eb619fdb JA |
1876 | * If 'no_tag' is set, that means that we failed getting |
1877 | * a driver tag with an I/O scheduler attached. If our dispatch | |
1878 | * waitqueue is no longer active, ensure that we run the queue | |
1879 | * AFTER adding our entries back to the list. | |
bd166ef1 | 1880 | * |
710c785f BVA |
1881 | * If no I/O scheduler has been configured it is possible that |
1882 | * the hardware queue got stopped and restarted before requests | |
1883 | * were pushed back onto the dispatch list. Rerun the queue to | |
1884 | * avoid starvation. Notes: | |
1885 | * - blk_mq_run_hw_queue() checks whether or not a queue has | |
1886 | * been stopped before rerunning a queue. | |
1887 | * - Some but not all block drivers stop a queue before | |
fc17b653 | 1888 | * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq |
710c785f | 1889 | * and dm-rq. |
86ff7c2a ML |
1890 | * |
1891 | * If driver returns BLK_STS_RESOURCE and SCHED_RESTART | |
1892 | * bit is set, run queue after a delay to avoid IO stalls | |
ab3cee37 | 1893 | * that could otherwise occur if the queue is idle. We'll do |
9586e67b NA |
1894 | * similar if we couldn't get budget or couldn't lock a zone |
1895 | * and SCHED_RESTART is set. | |
bd166ef1 | 1896 | */ |
86ff7c2a | 1897 | needs_restart = blk_mq_sched_needs_restart(hctx); |
9586e67b NA |
1898 | if (prep == PREP_DISPATCH_NO_BUDGET) |
1899 | needs_resource = true; | |
86ff7c2a | 1900 | if (!needs_restart || |
eb619fdb | 1901 | (no_tag && list_empty_careful(&hctx->dispatch_wait.entry))) |
bd166ef1 | 1902 | blk_mq_run_hw_queue(hctx, true); |
9586e67b | 1903 | else if (needs_restart && needs_resource) |
86ff7c2a | 1904 | blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY); |
1f57f8d4 | 1905 | |
6e768717 | 1906 | blk_mq_update_dispatch_busy(hctx, true); |
1f57f8d4 | 1907 | return false; |
6e768717 ML |
1908 | } else |
1909 | blk_mq_update_dispatch_busy(hctx, false); | |
f04c3df3 | 1910 | |
93efe981 | 1911 | return (queued + errors) != 0; |
f04c3df3 JA |
1912 | } |
1913 | ||
105663f7 AA |
1914 | /** |
1915 | * __blk_mq_run_hw_queue - Run a hardware queue. | |
1916 | * @hctx: Pointer to the hardware queue to run. | |
1917 | * | |
1918 | * Send pending requests to the hardware. | |
1919 | */ | |
6a83e74d BVA |
1920 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) |
1921 | { | |
b7a71e66 JA |
1922 | /* |
1923 | * We can't run the queue inline with ints disabled. Ensure that | |
1924 | * we catch bad users of this early. | |
1925 | */ | |
1926 | WARN_ON_ONCE(in_interrupt()); | |
1927 | ||
bcc330f4 ML |
1928 | blk_mq_run_dispatch_ops(hctx->queue, |
1929 | blk_mq_sched_dispatch_requests(hctx)); | |
6a83e74d BVA |
1930 | } |
1931 | ||
f82ddf19 ML |
1932 | static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx) |
1933 | { | |
1934 | int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask); | |
1935 | ||
1936 | if (cpu >= nr_cpu_ids) | |
1937 | cpu = cpumask_first(hctx->cpumask); | |
1938 | return cpu; | |
1939 | } | |
1940 | ||
506e931f JA |
1941 | /* |
1942 | * It'd be great if the workqueue API had a way to pass | |
1943 | * in a mask and had some smarts for more clever placement. | |
1944 | * For now we just round-robin here, switching for every | |
1945 | * BLK_MQ_CPU_WORK_BATCH queued items. | |
1946 | */ | |
1947 | static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) | |
1948 | { | |
7bed4595 | 1949 | bool tried = false; |
476f8c98 | 1950 | int next_cpu = hctx->next_cpu; |
7bed4595 | 1951 | |
b657d7e6 CH |
1952 | if (hctx->queue->nr_hw_queues == 1) |
1953 | return WORK_CPU_UNBOUND; | |
506e931f JA |
1954 | |
1955 | if (--hctx->next_cpu_batch <= 0) { | |
7bed4595 | 1956 | select_cpu: |
476f8c98 | 1957 | next_cpu = cpumask_next_and(next_cpu, hctx->cpumask, |
20e4d813 | 1958 | cpu_online_mask); |
506e931f | 1959 | if (next_cpu >= nr_cpu_ids) |
f82ddf19 | 1960 | next_cpu = blk_mq_first_mapped_cpu(hctx); |
506e931f JA |
1961 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; |
1962 | } | |
1963 | ||
7bed4595 ML |
1964 | /* |
1965 | * Do unbound schedule if we can't find a online CPU for this hctx, | |
1966 | * and it should only happen in the path of handling CPU DEAD. | |
1967 | */ | |
476f8c98 | 1968 | if (!cpu_online(next_cpu)) { |
7bed4595 ML |
1969 | if (!tried) { |
1970 | tried = true; | |
1971 | goto select_cpu; | |
1972 | } | |
1973 | ||
1974 | /* | |
1975 | * Make sure to re-select CPU next time once after CPUs | |
1976 | * in hctx->cpumask become online again. | |
1977 | */ | |
476f8c98 | 1978 | hctx->next_cpu = next_cpu; |
7bed4595 ML |
1979 | hctx->next_cpu_batch = 1; |
1980 | return WORK_CPU_UNBOUND; | |
1981 | } | |
476f8c98 ML |
1982 | |
1983 | hctx->next_cpu = next_cpu; | |
1984 | return next_cpu; | |
506e931f JA |
1985 | } |
1986 | ||
105663f7 AA |
1987 | /** |
1988 | * __blk_mq_delay_run_hw_queue - Run (or schedule to run) a hardware queue. | |
1989 | * @hctx: Pointer to the hardware queue to run. | |
1990 | * @async: If we want to run the queue asynchronously. | |
fa94ba8a | 1991 | * @msecs: Milliseconds of delay to wait before running the queue. |
105663f7 AA |
1992 | * |
1993 | * If !@async, try to run the queue now. Else, run the queue asynchronously and | |
1994 | * with a delay of @msecs. | |
1995 | */ | |
7587a5ae BVA |
1996 | static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async, |
1997 | unsigned long msecs) | |
320ae51f | 1998 | { |
5435c023 | 1999 | if (unlikely(blk_mq_hctx_stopped(hctx))) |
320ae51f JA |
2000 | return; |
2001 | ||
1b792f2f | 2002 | if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) { |
2a90d4aa PB |
2003 | int cpu = get_cpu(); |
2004 | if (cpumask_test_cpu(cpu, hctx->cpumask)) { | |
398205b8 | 2005 | __blk_mq_run_hw_queue(hctx); |
2a90d4aa | 2006 | put_cpu(); |
398205b8 PB |
2007 | return; |
2008 | } | |
e4043dcf | 2009 | |
2a90d4aa | 2010 | put_cpu(); |
e4043dcf | 2011 | } |
398205b8 | 2012 | |
ae943d20 BVA |
2013 | kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work, |
2014 | msecs_to_jiffies(msecs)); | |
7587a5ae BVA |
2015 | } |
2016 | ||
105663f7 AA |
2017 | /** |
2018 | * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously. | |
2019 | * @hctx: Pointer to the hardware queue to run. | |
fa94ba8a | 2020 | * @msecs: Milliseconds of delay to wait before running the queue. |
105663f7 AA |
2021 | * |
2022 | * Run a hardware queue asynchronously with a delay of @msecs. | |
2023 | */ | |
7587a5ae BVA |
2024 | void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs) |
2025 | { | |
2026 | __blk_mq_delay_run_hw_queue(hctx, true, msecs); | |
2027 | } | |
2028 | EXPORT_SYMBOL(blk_mq_delay_run_hw_queue); | |
2029 | ||
105663f7 AA |
2030 | /** |
2031 | * blk_mq_run_hw_queue - Start to run a hardware queue. | |
2032 | * @hctx: Pointer to the hardware queue to run. | |
2033 | * @async: If we want to run the queue asynchronously. | |
2034 | * | |
2035 | * Check if the request queue is not in a quiesced state and if there are | |
2036 | * pending requests to be sent. If this is true, run the queue to send requests | |
2037 | * to hardware. | |
2038 | */ | |
626fb735 | 2039 | void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) |
7587a5ae | 2040 | { |
24f5a90f ML |
2041 | bool need_run; |
2042 | ||
2043 | /* | |
2044 | * When queue is quiesced, we may be switching io scheduler, or | |
2045 | * updating nr_hw_queues, or other things, and we can't run queue | |
2046 | * any more, even __blk_mq_hctx_has_pending() can't be called safely. | |
2047 | * | |
2048 | * And queue will be rerun in blk_mq_unquiesce_queue() if it is | |
2049 | * quiesced. | |
2050 | */ | |
41adf531 | 2051 | __blk_mq_run_dispatch_ops(hctx->queue, false, |
2a904d00 ML |
2052 | need_run = !blk_queue_quiesced(hctx->queue) && |
2053 | blk_mq_hctx_has_pending(hctx)); | |
24f5a90f | 2054 | |
626fb735 | 2055 | if (need_run) |
79f720a7 | 2056 | __blk_mq_delay_run_hw_queue(hctx, async, 0); |
320ae51f | 2057 | } |
5b727272 | 2058 | EXPORT_SYMBOL(blk_mq_run_hw_queue); |
320ae51f | 2059 | |
b6e68ee8 JK |
2060 | /* |
2061 | * Is the request queue handled by an IO scheduler that does not respect | |
2062 | * hardware queues when dispatching? | |
2063 | */ | |
2064 | static bool blk_mq_has_sqsched(struct request_queue *q) | |
2065 | { | |
2066 | struct elevator_queue *e = q->elevator; | |
2067 | ||
2068 | if (e && e->type->ops.dispatch_request && | |
2069 | !(e->type->elevator_features & ELEVATOR_F_MQ_AWARE)) | |
2070 | return true; | |
2071 | return false; | |
2072 | } | |
2073 | ||
2074 | /* | |
2075 | * Return prefered queue to dispatch from (if any) for non-mq aware IO | |
2076 | * scheduler. | |
2077 | */ | |
2078 | static struct blk_mq_hw_ctx *blk_mq_get_sq_hctx(struct request_queue *q) | |
2079 | { | |
2080 | struct blk_mq_hw_ctx *hctx; | |
2081 | ||
2082 | /* | |
2083 | * If the IO scheduler does not respect hardware queues when | |
2084 | * dispatching, we just don't bother with multiple HW queues and | |
2085 | * dispatch from hctx for the current CPU since running multiple queues | |
2086 | * just causes lock contention inside the scheduler and pointless cache | |
2087 | * bouncing. | |
2088 | */ | |
2089 | hctx = blk_mq_map_queue_type(q, HCTX_TYPE_DEFAULT, | |
2090 | raw_smp_processor_id()); | |
2091 | if (!blk_mq_hctx_stopped(hctx)) | |
2092 | return hctx; | |
2093 | return NULL; | |
2094 | } | |
2095 | ||
105663f7 | 2096 | /** |
24f7bb88 | 2097 | * blk_mq_run_hw_queues - Run all hardware queues in a request queue. |
105663f7 AA |
2098 | * @q: Pointer to the request queue to run. |
2099 | * @async: If we want to run the queue asynchronously. | |
2100 | */ | |
b94ec296 | 2101 | void blk_mq_run_hw_queues(struct request_queue *q, bool async) |
320ae51f | 2102 | { |
b6e68ee8 | 2103 | struct blk_mq_hw_ctx *hctx, *sq_hctx; |
320ae51f JA |
2104 | int i; |
2105 | ||
b6e68ee8 JK |
2106 | sq_hctx = NULL; |
2107 | if (blk_mq_has_sqsched(q)) | |
2108 | sq_hctx = blk_mq_get_sq_hctx(q); | |
320ae51f | 2109 | queue_for_each_hw_ctx(q, hctx, i) { |
79f720a7 | 2110 | if (blk_mq_hctx_stopped(hctx)) |
320ae51f | 2111 | continue; |
b6e68ee8 JK |
2112 | /* |
2113 | * Dispatch from this hctx either if there's no hctx preferred | |
2114 | * by IO scheduler or if it has requests that bypass the | |
2115 | * scheduler. | |
2116 | */ | |
2117 | if (!sq_hctx || sq_hctx == hctx || | |
2118 | !list_empty_careful(&hctx->dispatch)) | |
2119 | blk_mq_run_hw_queue(hctx, async); | |
320ae51f JA |
2120 | } |
2121 | } | |
b94ec296 | 2122 | EXPORT_SYMBOL(blk_mq_run_hw_queues); |
320ae51f | 2123 | |
b9151e7b DA |
2124 | /** |
2125 | * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously. | |
2126 | * @q: Pointer to the request queue to run. | |
fa94ba8a | 2127 | * @msecs: Milliseconds of delay to wait before running the queues. |
b9151e7b DA |
2128 | */ |
2129 | void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs) | |
2130 | { | |
b6e68ee8 | 2131 | struct blk_mq_hw_ctx *hctx, *sq_hctx; |
b9151e7b DA |
2132 | int i; |
2133 | ||
b6e68ee8 JK |
2134 | sq_hctx = NULL; |
2135 | if (blk_mq_has_sqsched(q)) | |
2136 | sq_hctx = blk_mq_get_sq_hctx(q); | |
b9151e7b DA |
2137 | queue_for_each_hw_ctx(q, hctx, i) { |
2138 | if (blk_mq_hctx_stopped(hctx)) | |
2139 | continue; | |
b6e68ee8 JK |
2140 | /* |
2141 | * Dispatch from this hctx either if there's no hctx preferred | |
2142 | * by IO scheduler or if it has requests that bypass the | |
2143 | * scheduler. | |
2144 | */ | |
2145 | if (!sq_hctx || sq_hctx == hctx || | |
2146 | !list_empty_careful(&hctx->dispatch)) | |
2147 | blk_mq_delay_run_hw_queue(hctx, msecs); | |
b9151e7b DA |
2148 | } |
2149 | } | |
2150 | EXPORT_SYMBOL(blk_mq_delay_run_hw_queues); | |
2151 | ||
fd001443 BVA |
2152 | /** |
2153 | * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped | |
2154 | * @q: request queue. | |
2155 | * | |
2156 | * The caller is responsible for serializing this function against | |
2157 | * blk_mq_{start,stop}_hw_queue(). | |
2158 | */ | |
2159 | bool blk_mq_queue_stopped(struct request_queue *q) | |
2160 | { | |
2161 | struct blk_mq_hw_ctx *hctx; | |
2162 | int i; | |
2163 | ||
2164 | queue_for_each_hw_ctx(q, hctx, i) | |
2165 | if (blk_mq_hctx_stopped(hctx)) | |
2166 | return true; | |
2167 | ||
2168 | return false; | |
2169 | } | |
2170 | EXPORT_SYMBOL(blk_mq_queue_stopped); | |
2171 | ||
39a70c76 ML |
2172 | /* |
2173 | * This function is often used for pausing .queue_rq() by driver when | |
2174 | * there isn't enough resource or some conditions aren't satisfied, and | |
4d606219 | 2175 | * BLK_STS_RESOURCE is usually returned. |
39a70c76 ML |
2176 | * |
2177 | * We do not guarantee that dispatch can be drained or blocked | |
2178 | * after blk_mq_stop_hw_queue() returns. Please use | |
2179 | * blk_mq_quiesce_queue() for that requirement. | |
2180 | */ | |
2719aa21 JA |
2181 | void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx) |
2182 | { | |
641a9ed6 | 2183 | cancel_delayed_work(&hctx->run_work); |
280d45f6 | 2184 | |
641a9ed6 | 2185 | set_bit(BLK_MQ_S_STOPPED, &hctx->state); |
2719aa21 | 2186 | } |
641a9ed6 | 2187 | EXPORT_SYMBOL(blk_mq_stop_hw_queue); |
2719aa21 | 2188 | |
39a70c76 ML |
2189 | /* |
2190 | * This function is often used for pausing .queue_rq() by driver when | |
2191 | * there isn't enough resource or some conditions aren't satisfied, and | |
4d606219 | 2192 | * BLK_STS_RESOURCE is usually returned. |
39a70c76 ML |
2193 | * |
2194 | * We do not guarantee that dispatch can be drained or blocked | |
2195 | * after blk_mq_stop_hw_queues() returns. Please use | |
2196 | * blk_mq_quiesce_queue() for that requirement. | |
2197 | */ | |
2719aa21 JA |
2198 | void blk_mq_stop_hw_queues(struct request_queue *q) |
2199 | { | |
641a9ed6 ML |
2200 | struct blk_mq_hw_ctx *hctx; |
2201 | int i; | |
2202 | ||
2203 | queue_for_each_hw_ctx(q, hctx, i) | |
2204 | blk_mq_stop_hw_queue(hctx); | |
280d45f6 CH |
2205 | } |
2206 | EXPORT_SYMBOL(blk_mq_stop_hw_queues); | |
2207 | ||
320ae51f JA |
2208 | void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx) |
2209 | { | |
2210 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
e4043dcf | 2211 | |
0ffbce80 | 2212 | blk_mq_run_hw_queue(hctx, false); |
320ae51f JA |
2213 | } |
2214 | EXPORT_SYMBOL(blk_mq_start_hw_queue); | |
2215 | ||
2f268556 CH |
2216 | void blk_mq_start_hw_queues(struct request_queue *q) |
2217 | { | |
2218 | struct blk_mq_hw_ctx *hctx; | |
2219 | int i; | |
2220 | ||
2221 | queue_for_each_hw_ctx(q, hctx, i) | |
2222 | blk_mq_start_hw_queue(hctx); | |
2223 | } | |
2224 | EXPORT_SYMBOL(blk_mq_start_hw_queues); | |
2225 | ||
ae911c5e JA |
2226 | void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) |
2227 | { | |
2228 | if (!blk_mq_hctx_stopped(hctx)) | |
2229 | return; | |
2230 | ||
2231 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); | |
2232 | blk_mq_run_hw_queue(hctx, async); | |
2233 | } | |
2234 | EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue); | |
2235 | ||
1b4a3258 | 2236 | void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async) |
320ae51f JA |
2237 | { |
2238 | struct blk_mq_hw_ctx *hctx; | |
2239 | int i; | |
2240 | ||
ae911c5e JA |
2241 | queue_for_each_hw_ctx(q, hctx, i) |
2242 | blk_mq_start_stopped_hw_queue(hctx, async); | |
320ae51f JA |
2243 | } |
2244 | EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues); | |
2245 | ||
70f4db63 | 2246 | static void blk_mq_run_work_fn(struct work_struct *work) |
320ae51f JA |
2247 | { |
2248 | struct blk_mq_hw_ctx *hctx; | |
2249 | ||
9f993737 | 2250 | hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work); |
320ae51f | 2251 | |
21c6e939 | 2252 | /* |
15fe8a90 | 2253 | * If we are stopped, don't run the queue. |
21c6e939 | 2254 | */ |
0841031a | 2255 | if (blk_mq_hctx_stopped(hctx)) |
0196d6b4 | 2256 | return; |
7587a5ae BVA |
2257 | |
2258 | __blk_mq_run_hw_queue(hctx); | |
2259 | } | |
2260 | ||
cfd0c552 | 2261 | static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx, |
cfd0c552 ML |
2262 | struct request *rq, |
2263 | bool at_head) | |
320ae51f | 2264 | { |
e57690fe | 2265 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
c16d6b5a | 2266 | enum hctx_type type = hctx->type; |
e57690fe | 2267 | |
7b607814 BVA |
2268 | lockdep_assert_held(&ctx->lock); |
2269 | ||
a54895fa | 2270 | trace_block_rq_insert(rq); |
01b983c9 | 2271 | |
72a0a36e | 2272 | if (at_head) |
c16d6b5a | 2273 | list_add(&rq->queuelist, &ctx->rq_lists[type]); |
72a0a36e | 2274 | else |
c16d6b5a | 2275 | list_add_tail(&rq->queuelist, &ctx->rq_lists[type]); |
cfd0c552 | 2276 | } |
4bb659b1 | 2277 | |
2c3ad667 JA |
2278 | void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq, |
2279 | bool at_head) | |
cfd0c552 ML |
2280 | { |
2281 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
2282 | ||
7b607814 BVA |
2283 | lockdep_assert_held(&ctx->lock); |
2284 | ||
e57690fe | 2285 | __blk_mq_insert_req_list(hctx, rq, at_head); |
320ae51f | 2286 | blk_mq_hctx_mark_pending(hctx, ctx); |
320ae51f JA |
2287 | } |
2288 | ||
105663f7 AA |
2289 | /** |
2290 | * blk_mq_request_bypass_insert - Insert a request at dispatch list. | |
2291 | * @rq: Pointer to request to be inserted. | |
26bfeb26 | 2292 | * @at_head: true if the request should be inserted at the head of the list. |
105663f7 AA |
2293 | * @run_queue: If we should run the hardware queue after inserting the request. |
2294 | * | |
157f377b JA |
2295 | * Should only be used carefully, when the caller knows we want to |
2296 | * bypass a potential IO scheduler on the target device. | |
2297 | */ | |
01e99aec ML |
2298 | void blk_mq_request_bypass_insert(struct request *rq, bool at_head, |
2299 | bool run_queue) | |
157f377b | 2300 | { |
ea4f995e | 2301 | struct blk_mq_hw_ctx *hctx = rq->mq_hctx; |
157f377b JA |
2302 | |
2303 | spin_lock(&hctx->lock); | |
01e99aec ML |
2304 | if (at_head) |
2305 | list_add(&rq->queuelist, &hctx->dispatch); | |
2306 | else | |
2307 | list_add_tail(&rq->queuelist, &hctx->dispatch); | |
157f377b JA |
2308 | spin_unlock(&hctx->lock); |
2309 | ||
b0850297 ML |
2310 | if (run_queue) |
2311 | blk_mq_run_hw_queue(hctx, false); | |
157f377b JA |
2312 | } |
2313 | ||
bd166ef1 JA |
2314 | void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx, |
2315 | struct list_head *list) | |
320ae51f JA |
2316 | |
2317 | { | |
3f0cedc7 | 2318 | struct request *rq; |
c16d6b5a | 2319 | enum hctx_type type = hctx->type; |
3f0cedc7 | 2320 | |
320ae51f JA |
2321 | /* |
2322 | * preemption doesn't flush plug list, so it's possible ctx->cpu is | |
2323 | * offline now | |
2324 | */ | |
3f0cedc7 | 2325 | list_for_each_entry(rq, list, queuelist) { |
e57690fe | 2326 | BUG_ON(rq->mq_ctx != ctx); |
a54895fa | 2327 | trace_block_rq_insert(rq); |
320ae51f | 2328 | } |
3f0cedc7 ML |
2329 | |
2330 | spin_lock(&ctx->lock); | |
c16d6b5a | 2331 | list_splice_tail_init(list, &ctx->rq_lists[type]); |
cfd0c552 | 2332 | blk_mq_hctx_mark_pending(hctx, ctx); |
320ae51f | 2333 | spin_unlock(&ctx->lock); |
320ae51f JA |
2334 | } |
2335 | ||
dc5fc361 JA |
2336 | static void blk_mq_commit_rqs(struct blk_mq_hw_ctx *hctx, int *queued, |
2337 | bool from_schedule) | |
320ae51f | 2338 | { |
dc5fc361 JA |
2339 | if (hctx->queue->mq_ops->commit_rqs) { |
2340 | trace_block_unplug(hctx->queue, *queued, !from_schedule); | |
2341 | hctx->queue->mq_ops->commit_rqs(hctx); | |
2342 | } | |
2343 | *queued = 0; | |
2344 | } | |
320ae51f | 2345 | |
14ccb66b CH |
2346 | static void blk_mq_bio_to_request(struct request *rq, struct bio *bio, |
2347 | unsigned int nr_segs) | |
320ae51f | 2348 | { |
93f221ae EB |
2349 | int err; |
2350 | ||
f924cdde CH |
2351 | if (bio->bi_opf & REQ_RAHEAD) |
2352 | rq->cmd_flags |= REQ_FAILFAST_MASK; | |
2353 | ||
2354 | rq->__sector = bio->bi_iter.bi_sector; | |
2355 | rq->write_hint = bio->bi_write_hint; | |
14ccb66b | 2356 | blk_rq_bio_prep(rq, bio, nr_segs); |
93f221ae EB |
2357 | |
2358 | /* This can't fail, since GFP_NOIO includes __GFP_DIRECT_RECLAIM. */ | |
2359 | err = blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO); | |
2360 | WARN_ON_ONCE(err); | |
4b570521 | 2361 | |
b5af37ab | 2362 | blk_account_io_start(rq); |
320ae51f JA |
2363 | } |
2364 | ||
0f95549c | 2365 | static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx, |
3e08773c | 2366 | struct request *rq, bool last) |
f984df1f | 2367 | { |
f984df1f | 2368 | struct request_queue *q = rq->q; |
f984df1f SL |
2369 | struct blk_mq_queue_data bd = { |
2370 | .rq = rq, | |
be94f058 | 2371 | .last = last, |
f984df1f | 2372 | }; |
f06345ad | 2373 | blk_status_t ret; |
0f95549c | 2374 | |
0f95549c MS |
2375 | /* |
2376 | * For OK queue, we are done. For error, caller may kill it. | |
2377 | * Any other error (busy), just add it to our list as we | |
2378 | * previously would have done. | |
2379 | */ | |
2380 | ret = q->mq_ops->queue_rq(hctx, &bd); | |
2381 | switch (ret) { | |
2382 | case BLK_STS_OK: | |
6ce3dd6e | 2383 | blk_mq_update_dispatch_busy(hctx, false); |
0f95549c MS |
2384 | break; |
2385 | case BLK_STS_RESOURCE: | |
86ff7c2a | 2386 | case BLK_STS_DEV_RESOURCE: |
6ce3dd6e | 2387 | blk_mq_update_dispatch_busy(hctx, true); |
0f95549c MS |
2388 | __blk_mq_requeue_request(rq); |
2389 | break; | |
2390 | default: | |
6ce3dd6e | 2391 | blk_mq_update_dispatch_busy(hctx, false); |
0f95549c MS |
2392 | break; |
2393 | } | |
2394 | ||
2395 | return ret; | |
2396 | } | |
2397 | ||
fd9c40f6 | 2398 | static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, |
0f95549c | 2399 | struct request *rq, |
fd9c40f6 | 2400 | bool bypass_insert, bool last) |
0f95549c MS |
2401 | { |
2402 | struct request_queue *q = rq->q; | |
d964f04a | 2403 | bool run_queue = true; |
2a5a24aa | 2404 | int budget_token; |
d964f04a | 2405 | |
23d4ee19 | 2406 | /* |
fd9c40f6 | 2407 | * RCU or SRCU read lock is needed before checking quiesced flag. |
23d4ee19 | 2408 | * |
fd9c40f6 BVA |
2409 | * When queue is stopped or quiesced, ignore 'bypass_insert' from |
2410 | * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller, | |
2411 | * and avoid driver to try to dispatch again. | |
23d4ee19 | 2412 | */ |
fd9c40f6 | 2413 | if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) { |
d964f04a | 2414 | run_queue = false; |
fd9c40f6 BVA |
2415 | bypass_insert = false; |
2416 | goto insert; | |
d964f04a | 2417 | } |
f984df1f | 2418 | |
2ff0682d | 2419 | if ((rq->rq_flags & RQF_ELV) && !bypass_insert) |
fd9c40f6 | 2420 | goto insert; |
2253efc8 | 2421 | |
2a5a24aa ML |
2422 | budget_token = blk_mq_get_dispatch_budget(q); |
2423 | if (budget_token < 0) | |
fd9c40f6 | 2424 | goto insert; |
bd166ef1 | 2425 | |
2a5a24aa ML |
2426 | blk_mq_set_rq_budget_token(rq, budget_token); |
2427 | ||
8ab6bb9e | 2428 | if (!blk_mq_get_driver_tag(rq)) { |
2a5a24aa | 2429 | blk_mq_put_dispatch_budget(q, budget_token); |
fd9c40f6 | 2430 | goto insert; |
88022d72 | 2431 | } |
de148297 | 2432 | |
3e08773c | 2433 | return __blk_mq_issue_directly(hctx, rq, last); |
fd9c40f6 BVA |
2434 | insert: |
2435 | if (bypass_insert) | |
2436 | return BLK_STS_RESOURCE; | |
2437 | ||
db03f88f ML |
2438 | blk_mq_sched_insert_request(rq, false, run_queue, false); |
2439 | ||
fd9c40f6 BVA |
2440 | return BLK_STS_OK; |
2441 | } | |
2442 | ||
105663f7 AA |
2443 | /** |
2444 | * blk_mq_try_issue_directly - Try to send a request directly to device driver. | |
2445 | * @hctx: Pointer of the associated hardware queue. | |
2446 | * @rq: Pointer to request to be sent. | |
105663f7 AA |
2447 | * |
2448 | * If the device has enough resources to accept a new request now, send the | |
2449 | * request directly to device driver. Else, insert at hctx->dispatch queue, so | |
2450 | * we can try send it another time in the future. Requests inserted at this | |
2451 | * queue have higher priority. | |
2452 | */ | |
fd9c40f6 | 2453 | static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, |
3e08773c | 2454 | struct request *rq) |
fd9c40f6 | 2455 | { |
2a904d00 ML |
2456 | blk_status_t ret = |
2457 | __blk_mq_try_issue_directly(hctx, rq, false, true); | |
fd9c40f6 | 2458 | |
fd9c40f6 | 2459 | if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE) |
01e99aec | 2460 | blk_mq_request_bypass_insert(rq, false, true); |
fd9c40f6 BVA |
2461 | else if (ret != BLK_STS_OK) |
2462 | blk_mq_end_request(rq, ret); | |
fd9c40f6 BVA |
2463 | } |
2464 | ||
06c8c691 | 2465 | static blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last) |
fd9c40f6 | 2466 | { |
4cafe86c | 2467 | return __blk_mq_try_issue_directly(rq->mq_hctx, rq, true, last); |
5eb6126e CH |
2468 | } |
2469 | ||
b84c5b50 CH |
2470 | static void blk_mq_plug_issue_direct(struct blk_plug *plug, bool from_schedule) |
2471 | { | |
2472 | struct blk_mq_hw_ctx *hctx = NULL; | |
2473 | struct request *rq; | |
2474 | int queued = 0; | |
2475 | int errors = 0; | |
2476 | ||
2477 | while ((rq = rq_list_pop(&plug->mq_list))) { | |
2478 | bool last = rq_list_empty(plug->mq_list); | |
2479 | blk_status_t ret; | |
2480 | ||
2481 | if (hctx != rq->mq_hctx) { | |
2482 | if (hctx) | |
2483 | blk_mq_commit_rqs(hctx, &queued, from_schedule); | |
2484 | hctx = rq->mq_hctx; | |
2485 | } | |
2486 | ||
2487 | ret = blk_mq_request_issue_directly(rq, last); | |
2488 | switch (ret) { | |
2489 | case BLK_STS_OK: | |
2490 | queued++; | |
2491 | break; | |
2492 | case BLK_STS_RESOURCE: | |
2493 | case BLK_STS_DEV_RESOURCE: | |
2494 | blk_mq_request_bypass_insert(rq, false, last); | |
2495 | blk_mq_commit_rqs(hctx, &queued, from_schedule); | |
2496 | return; | |
2497 | default: | |
2498 | blk_mq_end_request(rq, ret); | |
2499 | errors++; | |
2500 | break; | |
2501 | } | |
2502 | } | |
2503 | ||
2504 | /* | |
2505 | * If we didn't flush the entire list, we could have told the driver | |
2506 | * there was more coming, but that turned out to be a lie. | |
2507 | */ | |
2508 | if (errors) | |
2509 | blk_mq_commit_rqs(hctx, &queued, from_schedule); | |
2510 | } | |
2511 | ||
2512 | void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) | |
2513 | { | |
2514 | struct blk_mq_hw_ctx *this_hctx; | |
2515 | struct blk_mq_ctx *this_ctx; | |
2516 | unsigned int depth; | |
2517 | LIST_HEAD(list); | |
2518 | ||
2519 | if (rq_list_empty(plug->mq_list)) | |
2520 | return; | |
2521 | plug->rq_count = 0; | |
2522 | ||
2523 | if (!plug->multiple_queues && !plug->has_elevator && !from_schedule) { | |
73f3760e ML |
2524 | struct request_queue *q = rq_list_peek(&plug->mq_list)->q; |
2525 | ||
2526 | blk_mq_run_dispatch_ops(q, | |
4cafe86c | 2527 | blk_mq_plug_issue_direct(plug, false)); |
b84c5b50 CH |
2528 | if (rq_list_empty(plug->mq_list)) |
2529 | return; | |
2530 | } | |
2531 | ||
2532 | this_hctx = NULL; | |
2533 | this_ctx = NULL; | |
2534 | depth = 0; | |
2535 | do { | |
2536 | struct request *rq; | |
2537 | ||
2538 | rq = rq_list_pop(&plug->mq_list); | |
2539 | ||
2540 | if (!this_hctx) { | |
2541 | this_hctx = rq->mq_hctx; | |
2542 | this_ctx = rq->mq_ctx; | |
2543 | } else if (this_hctx != rq->mq_hctx || this_ctx != rq->mq_ctx) { | |
2544 | trace_block_unplug(this_hctx->queue, depth, | |
2545 | !from_schedule); | |
2546 | blk_mq_sched_insert_requests(this_hctx, this_ctx, | |
2547 | &list, from_schedule); | |
2548 | depth = 0; | |
2549 | this_hctx = rq->mq_hctx; | |
2550 | this_ctx = rq->mq_ctx; | |
2551 | ||
2552 | } | |
2553 | ||
2554 | list_add(&rq->queuelist, &list); | |
2555 | depth++; | |
2556 | } while (!rq_list_empty(plug->mq_list)); | |
2557 | ||
2558 | if (!list_empty(&list)) { | |
2559 | trace_block_unplug(this_hctx->queue, depth, !from_schedule); | |
2560 | blk_mq_sched_insert_requests(this_hctx, this_ctx, &list, | |
2561 | from_schedule); | |
2562 | } | |
2563 | } | |
2564 | ||
6ce3dd6e ML |
2565 | void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx, |
2566 | struct list_head *list) | |
2567 | { | |
536167d4 | 2568 | int queued = 0; |
632bfb63 | 2569 | int errors = 0; |
536167d4 | 2570 | |
6ce3dd6e | 2571 | while (!list_empty(list)) { |
fd9c40f6 | 2572 | blk_status_t ret; |
6ce3dd6e ML |
2573 | struct request *rq = list_first_entry(list, struct request, |
2574 | queuelist); | |
2575 | ||
2576 | list_del_init(&rq->queuelist); | |
fd9c40f6 BVA |
2577 | ret = blk_mq_request_issue_directly(rq, list_empty(list)); |
2578 | if (ret != BLK_STS_OK) { | |
2579 | if (ret == BLK_STS_RESOURCE || | |
2580 | ret == BLK_STS_DEV_RESOURCE) { | |
01e99aec | 2581 | blk_mq_request_bypass_insert(rq, false, |
c616cbee | 2582 | list_empty(list)); |
fd9c40f6 BVA |
2583 | break; |
2584 | } | |
2585 | blk_mq_end_request(rq, ret); | |
632bfb63 | 2586 | errors++; |
536167d4 KB |
2587 | } else |
2588 | queued++; | |
6ce3dd6e | 2589 | } |
d666ba98 JA |
2590 | |
2591 | /* | |
2592 | * If we didn't flush the entire list, we could have told | |
2593 | * the driver there was more coming, but that turned out to | |
2594 | * be a lie. | |
2595 | */ | |
632bfb63 | 2596 | if ((!list_empty(list) || errors) && |
2597 | hctx->queue->mq_ops->commit_rqs && queued) | |
d666ba98 | 2598 | hctx->queue->mq_ops->commit_rqs(hctx); |
6ce3dd6e ML |
2599 | } |
2600 | ||
7f2a6a69 | 2601 | /* |
ba0ffdd8 | 2602 | * Allow 2x BLK_MAX_REQUEST_COUNT requests on plug queue for multiple |
7f2a6a69 SL |
2603 | * queues. This is important for md arrays to benefit from merging |
2604 | * requests. | |
2605 | */ | |
2606 | static inline unsigned short blk_plug_max_rq_count(struct blk_plug *plug) | |
2607 | { | |
2608 | if (plug->multiple_queues) | |
ba0ffdd8 | 2609 | return BLK_MAX_REQUEST_COUNT * 2; |
7f2a6a69 SL |
2610 | return BLK_MAX_REQUEST_COUNT; |
2611 | } | |
2612 | ||
1e9c2303 CH |
2613 | static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq) |
2614 | { | |
2615 | struct request *last = rq_list_peek(&plug->mq_list); | |
2616 | ||
2617 | if (!plug->rq_count) { | |
2618 | trace_block_plug(rq->q); | |
2619 | } else if (plug->rq_count >= blk_plug_max_rq_count(plug) || | |
2620 | (!blk_queue_nomerges(rq->q) && | |
2621 | blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) { | |
2622 | blk_mq_flush_plug_list(plug, false); | |
2623 | trace_block_plug(rq->q); | |
2624 | } | |
2625 | ||
2626 | if (!plug->multiple_queues && last && last->q != rq->q) | |
2627 | plug->multiple_queues = true; | |
2628 | if (!plug->has_elevator && (rq->rq_flags & RQF_ELV)) | |
2629 | plug->has_elevator = true; | |
2630 | rq->rq_next = NULL; | |
2631 | rq_list_add(&plug->mq_list, rq); | |
2632 | plug->rq_count++; | |
2633 | } | |
2634 | ||
b131f201 | 2635 | static bool blk_mq_attempt_bio_merge(struct request_queue *q, |
0c5bcc92 | 2636 | struct bio *bio, unsigned int nr_segs) |
900e0807 JA |
2637 | { |
2638 | if (!blk_queue_nomerges(q) && bio_mergeable(bio)) { | |
0c5bcc92 | 2639 | if (blk_attempt_plug_merge(q, bio, nr_segs)) |
900e0807 JA |
2640 | return true; |
2641 | if (blk_mq_sched_bio_merge(q, bio, nr_segs)) | |
2642 | return true; | |
2643 | } | |
2644 | return false; | |
2645 | } | |
2646 | ||
71539717 JA |
2647 | static struct request *blk_mq_get_new_requests(struct request_queue *q, |
2648 | struct blk_plug *plug, | |
900e0807 | 2649 | struct bio *bio, |
0c5bcc92 | 2650 | unsigned int nsegs) |
71539717 JA |
2651 | { |
2652 | struct blk_mq_alloc_data data = { | |
2653 | .q = q, | |
2654 | .nr_tags = 1, | |
71539717 JA |
2655 | }; |
2656 | struct request *rq; | |
2657 | ||
5b13bc8a | 2658 | if (unlikely(bio_queue_enter(bio))) |
b637108a | 2659 | return NULL; |
5b13bc8a CH |
2660 | if (unlikely(!submit_bio_checks(bio))) |
2661 | goto queue_exit; | |
2662 | if (blk_mq_attempt_bio_merge(q, bio, nsegs)) | |
2663 | goto queue_exit; | |
900e0807 JA |
2664 | |
2665 | rq_qos_throttle(q, bio); | |
2666 | ||
5f480b1a ML |
2667 | /* ->bi_opf is finalized after submit_bio_checks() returns */ |
2668 | data.cmd_flags = bio->bi_opf; | |
71539717 JA |
2669 | if (plug) { |
2670 | data.nr_tags = plug->nr_ios; | |
2671 | plug->nr_ios = 1; | |
2672 | data.cached_rq = &plug->cached_rq; | |
2673 | } | |
2674 | ||
2675 | rq = __blk_mq_alloc_requests(&data); | |
373b5416 JA |
2676 | if (rq) |
2677 | return rq; | |
71539717 JA |
2678 | rq_qos_cleanup(q, bio); |
2679 | if (bio->bi_opf & REQ_NOWAIT) | |
2680 | bio_wouldblock_error(bio); | |
5b13bc8a CH |
2681 | queue_exit: |
2682 | blk_queue_exit(q); | |
71539717 JA |
2683 | return NULL; |
2684 | } | |
2685 | ||
5b13bc8a | 2686 | static inline struct request *blk_mq_get_cached_request(struct request_queue *q, |
a08ed9aa | 2687 | struct blk_plug *plug, struct bio **bio, unsigned int nsegs) |
71539717 | 2688 | { |
b637108a | 2689 | struct request *rq; |
b637108a | 2690 | |
5b13bc8a CH |
2691 | if (!plug) |
2692 | return NULL; | |
2693 | rq = rq_list_peek(&plug->cached_rq); | |
2694 | if (!rq || rq->q != q) | |
2695 | return NULL; | |
71539717 | 2696 | |
a08ed9aa | 2697 | if (unlikely(!submit_bio_checks(*bio))) |
b637108a | 2698 | return NULL; |
a08ed9aa JA |
2699 | if (blk_mq_attempt_bio_merge(q, *bio, nsegs)) { |
2700 | *bio = NULL; | |
5b13bc8a | 2701 | return NULL; |
a08ed9aa JA |
2702 | } |
2703 | if (blk_mq_get_hctx_type((*bio)->bi_opf) != rq->mq_hctx->type) | |
5b13bc8a | 2704 | return NULL; |
a08ed9aa | 2705 | if (op_is_flush(rq->cmd_flags) != op_is_flush((*bio)->bi_opf)) |
5b13bc8a CH |
2706 | return NULL; |
2707 | ||
a08ed9aa | 2708 | rq->cmd_flags = (*bio)->bi_opf; |
5b13bc8a CH |
2709 | plug->cached_rq = rq_list_next(rq); |
2710 | INIT_LIST_HEAD(&rq->queuelist); | |
a08ed9aa | 2711 | rq_qos_throttle(q, *bio); |
5b13bc8a | 2712 | return rq; |
71539717 JA |
2713 | } |
2714 | ||
105663f7 | 2715 | /** |
c62b37d9 | 2716 | * blk_mq_submit_bio - Create and send a request to block device. |
105663f7 AA |
2717 | * @bio: Bio pointer. |
2718 | * | |
2719 | * Builds up a request structure from @q and @bio and send to the device. The | |
2720 | * request may not be queued directly to hardware if: | |
2721 | * * This request can be merged with another one | |
2722 | * * We want to place request at plug queue for possible future merging | |
2723 | * * There is an IO scheduler active at this queue | |
2724 | * | |
2725 | * It will not queue the request if there is an error with the bio, or at the | |
2726 | * request creation. | |
105663f7 | 2727 | */ |
3e08773c | 2728 | void blk_mq_submit_bio(struct bio *bio) |
07068d5b | 2729 | { |
ed6cddef | 2730 | struct request_queue *q = bdev_get_queue(bio->bi_bdev); |
5b13bc8a | 2731 | struct blk_plug *plug = blk_mq_plug(q, bio); |
ef295ecf | 2732 | const int is_sync = op_is_sync(bio->bi_opf); |
07068d5b | 2733 | struct request *rq; |
abd45c15 | 2734 | unsigned int nr_segs = 1; |
a892c8d5 | 2735 | blk_status_t ret; |
07068d5b | 2736 | |
900e0807 JA |
2737 | if (unlikely(!blk_crypto_bio_prep(&bio))) |
2738 | return; | |
2739 | ||
07068d5b | 2740 | blk_queue_bounce(q, &bio); |
abd45c15 JA |
2741 | if (blk_may_split(q, bio)) |
2742 | __blk_queue_split(q, &bio, &nr_segs); | |
f36ea50c | 2743 | |
e23947bd | 2744 | if (!bio_integrity_prep(bio)) |
900e0807 | 2745 | return; |
87760e5e | 2746 | |
a08ed9aa | 2747 | rq = blk_mq_get_cached_request(q, plug, &bio, nr_segs); |
5b13bc8a | 2748 | if (!rq) { |
a08ed9aa JA |
2749 | if (!bio) |
2750 | return; | |
5b13bc8a CH |
2751 | rq = blk_mq_get_new_requests(q, plug, bio, nr_segs); |
2752 | if (unlikely(!rq)) | |
2753 | return; | |
2754 | } | |
87760e5e | 2755 | |
e8a676d6 | 2756 | trace_block_getrq(bio); |
d6f1dda2 | 2757 | |
c1c80384 | 2758 | rq_qos_track(q, rq, bio); |
07068d5b | 2759 | |
970d168d BVA |
2760 | blk_mq_bio_to_request(rq, bio, nr_segs); |
2761 | ||
a892c8d5 ST |
2762 | ret = blk_crypto_init_request(rq); |
2763 | if (ret != BLK_STS_OK) { | |
2764 | bio->bi_status = ret; | |
2765 | bio_endio(bio); | |
2766 | blk_mq_free_request(rq); | |
3e08773c | 2767 | return; |
a892c8d5 ST |
2768 | } |
2769 | ||
2b504bd4 ML |
2770 | if (op_is_flush(bio->bi_opf)) { |
2771 | blk_insert_flush(rq); | |
d92ca9d8 | 2772 | return; |
2b504bd4 | 2773 | } |
d92ca9d8 | 2774 | |
1e9c2303 | 2775 | if (plug) |
ce5b009c | 2776 | blk_add_rq_to_plug(plug, rq); |
1e9c2303 CH |
2777 | else if ((rq->rq_flags & RQF_ELV) || |
2778 | (rq->mq_hctx->dispatch_busy && | |
2779 | (q->nr_hw_queues == 1 || !is_sync))) | |
a12de1d4 | 2780 | blk_mq_sched_insert_request(rq, false, true, true); |
1e9c2303 | 2781 | else |
bcc330f4 | 2782 | blk_mq_run_dispatch_ops(rq->q, |
2a904d00 | 2783 | blk_mq_try_issue_directly(rq->mq_hctx, rq)); |
320ae51f JA |
2784 | } |
2785 | ||
06c8c691 CH |
2786 | /** |
2787 | * blk_cloned_rq_check_limits - Helper function to check a cloned request | |
2788 | * for the new queue limits | |
2789 | * @q: the queue | |
2790 | * @rq: the request being checked | |
2791 | * | |
2792 | * Description: | |
2793 | * @rq may have been made based on weaker limitations of upper-level queues | |
2794 | * in request stacking drivers, and it may violate the limitation of @q. | |
2795 | * Since the block layer and the underlying device driver trust @rq | |
2796 | * after it is inserted to @q, it should be checked against @q before | |
2797 | * the insertion using this generic function. | |
2798 | * | |
2799 | * Request stacking drivers like request-based dm may change the queue | |
2800 | * limits when retrying requests on other queues. Those requests need | |
2801 | * to be checked against the new queue limits again during dispatch. | |
2802 | */ | |
2803 | static blk_status_t blk_cloned_rq_check_limits(struct request_queue *q, | |
2804 | struct request *rq) | |
2805 | { | |
2806 | unsigned int max_sectors = blk_queue_get_max_sectors(q, req_op(rq)); | |
2807 | ||
2808 | if (blk_rq_sectors(rq) > max_sectors) { | |
2809 | /* | |
2810 | * SCSI device does not have a good way to return if | |
2811 | * Write Same/Zero is actually supported. If a device rejects | |
2812 | * a non-read/write command (discard, write same,etc.) the | |
2813 | * low-level device driver will set the relevant queue limit to | |
2814 | * 0 to prevent blk-lib from issuing more of the offending | |
2815 | * operations. Commands queued prior to the queue limit being | |
2816 | * reset need to be completed with BLK_STS_NOTSUPP to avoid I/O | |
2817 | * errors being propagated to upper layers. | |
2818 | */ | |
2819 | if (max_sectors == 0) | |
2820 | return BLK_STS_NOTSUPP; | |
2821 | ||
2822 | printk(KERN_ERR "%s: over max size limit. (%u > %u)\n", | |
2823 | __func__, blk_rq_sectors(rq), max_sectors); | |
2824 | return BLK_STS_IOERR; | |
2825 | } | |
2826 | ||
2827 | /* | |
2828 | * The queue settings related to segment counting may differ from the | |
2829 | * original queue. | |
2830 | */ | |
2831 | rq->nr_phys_segments = blk_recalc_rq_segments(rq); | |
2832 | if (rq->nr_phys_segments > queue_max_segments(q)) { | |
2833 | printk(KERN_ERR "%s: over max segments limit. (%hu > %hu)\n", | |
2834 | __func__, rq->nr_phys_segments, queue_max_segments(q)); | |
2835 | return BLK_STS_IOERR; | |
2836 | } | |
2837 | ||
2838 | return BLK_STS_OK; | |
2839 | } | |
2840 | ||
2841 | /** | |
2842 | * blk_insert_cloned_request - Helper for stacking drivers to submit a request | |
2843 | * @q: the queue to submit the request | |
2844 | * @rq: the request being queued | |
2845 | */ | |
2846 | blk_status_t blk_insert_cloned_request(struct request_queue *q, struct request *rq) | |
2847 | { | |
2848 | blk_status_t ret; | |
2849 | ||
2850 | ret = blk_cloned_rq_check_limits(q, rq); | |
2851 | if (ret != BLK_STS_OK) | |
2852 | return ret; | |
2853 | ||
f3fa33ac CH |
2854 | if (rq->q->disk && |
2855 | should_fail_request(rq->q->disk->part0, blk_rq_bytes(rq))) | |
06c8c691 CH |
2856 | return BLK_STS_IOERR; |
2857 | ||
2858 | if (blk_crypto_insert_cloned_request(rq)) | |
2859 | return BLK_STS_IOERR; | |
2860 | ||
2861 | blk_account_io_start(rq); | |
2862 | ||
2863 | /* | |
2864 | * Since we have a scheduler attached on the top device, | |
2865 | * bypass a potential scheduler on the bottom device for | |
2866 | * insert. | |
2867 | */ | |
4cafe86c ML |
2868 | blk_mq_run_dispatch_ops(rq->q, |
2869 | ret = blk_mq_request_issue_directly(rq, true)); | |
2870 | return ret; | |
06c8c691 CH |
2871 | } |
2872 | EXPORT_SYMBOL_GPL(blk_insert_cloned_request); | |
2873 | ||
2874 | /** | |
2875 | * blk_rq_unprep_clone - Helper function to free all bios in a cloned request | |
2876 | * @rq: the clone request to be cleaned up | |
2877 | * | |
2878 | * Description: | |
2879 | * Free all bios in @rq for a cloned request. | |
2880 | */ | |
2881 | void blk_rq_unprep_clone(struct request *rq) | |
2882 | { | |
2883 | struct bio *bio; | |
2884 | ||
2885 | while ((bio = rq->bio) != NULL) { | |
2886 | rq->bio = bio->bi_next; | |
2887 | ||
2888 | bio_put(bio); | |
2889 | } | |
2890 | } | |
2891 | EXPORT_SYMBOL_GPL(blk_rq_unprep_clone); | |
2892 | ||
2893 | /** | |
2894 | * blk_rq_prep_clone - Helper function to setup clone request | |
2895 | * @rq: the request to be setup | |
2896 | * @rq_src: original request to be cloned | |
2897 | * @bs: bio_set that bios for clone are allocated from | |
2898 | * @gfp_mask: memory allocation mask for bio | |
2899 | * @bio_ctr: setup function to be called for each clone bio. | |
2900 | * Returns %0 for success, non %0 for failure. | |
2901 | * @data: private data to be passed to @bio_ctr | |
2902 | * | |
2903 | * Description: | |
2904 | * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq. | |
2905 | * Also, pages which the original bios are pointing to are not copied | |
2906 | * and the cloned bios just point same pages. | |
2907 | * So cloned bios must be completed before original bios, which means | |
2908 | * the caller must complete @rq before @rq_src. | |
2909 | */ | |
2910 | int blk_rq_prep_clone(struct request *rq, struct request *rq_src, | |
2911 | struct bio_set *bs, gfp_t gfp_mask, | |
2912 | int (*bio_ctr)(struct bio *, struct bio *, void *), | |
2913 | void *data) | |
2914 | { | |
2915 | struct bio *bio, *bio_src; | |
2916 | ||
2917 | if (!bs) | |
2918 | bs = &fs_bio_set; | |
2919 | ||
2920 | __rq_for_each_bio(bio_src, rq_src) { | |
2921 | bio = bio_clone_fast(bio_src, gfp_mask, bs); | |
2922 | if (!bio) | |
2923 | goto free_and_out; | |
2924 | ||
2925 | if (bio_ctr && bio_ctr(bio, bio_src, data)) | |
2926 | goto free_and_out; | |
2927 | ||
2928 | if (rq->bio) { | |
2929 | rq->biotail->bi_next = bio; | |
2930 | rq->biotail = bio; | |
2931 | } else { | |
2932 | rq->bio = rq->biotail = bio; | |
2933 | } | |
2934 | bio = NULL; | |
2935 | } | |
2936 | ||
2937 | /* Copy attributes of the original request to the clone request. */ | |
2938 | rq->__sector = blk_rq_pos(rq_src); | |
2939 | rq->__data_len = blk_rq_bytes(rq_src); | |
2940 | if (rq_src->rq_flags & RQF_SPECIAL_PAYLOAD) { | |
2941 | rq->rq_flags |= RQF_SPECIAL_PAYLOAD; | |
2942 | rq->special_vec = rq_src->special_vec; | |
2943 | } | |
2944 | rq->nr_phys_segments = rq_src->nr_phys_segments; | |
2945 | rq->ioprio = rq_src->ioprio; | |
2946 | ||
2947 | if (rq->bio && blk_crypto_rq_bio_prep(rq, rq->bio, gfp_mask) < 0) | |
2948 | goto free_and_out; | |
2949 | ||
2950 | return 0; | |
2951 | ||
2952 | free_and_out: | |
2953 | if (bio) | |
2954 | bio_put(bio); | |
2955 | blk_rq_unprep_clone(rq); | |
2956 | ||
2957 | return -ENOMEM; | |
2958 | } | |
2959 | EXPORT_SYMBOL_GPL(blk_rq_prep_clone); | |
2960 | ||
f2b8f3ce CH |
2961 | /* |
2962 | * Steal bios from a request and add them to a bio list. | |
2963 | * The request must not have been partially completed before. | |
2964 | */ | |
2965 | void blk_steal_bios(struct bio_list *list, struct request *rq) | |
2966 | { | |
2967 | if (rq->bio) { | |
2968 | if (list->tail) | |
2969 | list->tail->bi_next = rq->bio; | |
2970 | else | |
2971 | list->head = rq->bio; | |
2972 | list->tail = rq->biotail; | |
2973 | ||
2974 | rq->bio = NULL; | |
2975 | rq->biotail = NULL; | |
2976 | } | |
2977 | ||
2978 | rq->__data_len = 0; | |
2979 | } | |
2980 | EXPORT_SYMBOL_GPL(blk_steal_bios); | |
2981 | ||
bd63141d ML |
2982 | static size_t order_to_size(unsigned int order) |
2983 | { | |
2984 | return (size_t)PAGE_SIZE << order; | |
2985 | } | |
2986 | ||
2987 | /* called before freeing request pool in @tags */ | |
f32e4eaf JG |
2988 | static void blk_mq_clear_rq_mapping(struct blk_mq_tags *drv_tags, |
2989 | struct blk_mq_tags *tags) | |
bd63141d | 2990 | { |
bd63141d ML |
2991 | struct page *page; |
2992 | unsigned long flags; | |
2993 | ||
4f245d5b JG |
2994 | /* There is no need to clear a driver tags own mapping */ |
2995 | if (drv_tags == tags) | |
2996 | return; | |
2997 | ||
bd63141d ML |
2998 | list_for_each_entry(page, &tags->page_list, lru) { |
2999 | unsigned long start = (unsigned long)page_address(page); | |
3000 | unsigned long end = start + order_to_size(page->private); | |
3001 | int i; | |
3002 | ||
f32e4eaf | 3003 | for (i = 0; i < drv_tags->nr_tags; i++) { |
bd63141d ML |
3004 | struct request *rq = drv_tags->rqs[i]; |
3005 | unsigned long rq_addr = (unsigned long)rq; | |
3006 | ||
3007 | if (rq_addr >= start && rq_addr < end) { | |
0a467d0f | 3008 | WARN_ON_ONCE(req_ref_read(rq) != 0); |
bd63141d ML |
3009 | cmpxchg(&drv_tags->rqs[i], rq, NULL); |
3010 | } | |
3011 | } | |
3012 | } | |
3013 | ||
3014 | /* | |
3015 | * Wait until all pending iteration is done. | |
3016 | * | |
3017 | * Request reference is cleared and it is guaranteed to be observed | |
3018 | * after the ->lock is released. | |
3019 | */ | |
3020 | spin_lock_irqsave(&drv_tags->lock, flags); | |
3021 | spin_unlock_irqrestore(&drv_tags->lock, flags); | |
3022 | } | |
3023 | ||
cc71a6f4 JA |
3024 | void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags, |
3025 | unsigned int hctx_idx) | |
95363efd | 3026 | { |
f32e4eaf | 3027 | struct blk_mq_tags *drv_tags; |
e9b267d9 | 3028 | struct page *page; |
320ae51f | 3029 | |
079a2e3e JG |
3030 | if (blk_mq_is_shared_tags(set->flags)) |
3031 | drv_tags = set->shared_tags; | |
e155b0c2 JG |
3032 | else |
3033 | drv_tags = set->tags[hctx_idx]; | |
f32e4eaf | 3034 | |
65de57bb | 3035 | if (tags->static_rqs && set->ops->exit_request) { |
e9b267d9 | 3036 | int i; |
320ae51f | 3037 | |
24d2f903 | 3038 | for (i = 0; i < tags->nr_tags; i++) { |
2af8cbe3 JA |
3039 | struct request *rq = tags->static_rqs[i]; |
3040 | ||
3041 | if (!rq) | |
e9b267d9 | 3042 | continue; |
d6296d39 | 3043 | set->ops->exit_request(set, rq, hctx_idx); |
2af8cbe3 | 3044 | tags->static_rqs[i] = NULL; |
e9b267d9 | 3045 | } |
320ae51f | 3046 | } |
320ae51f | 3047 | |
f32e4eaf | 3048 | blk_mq_clear_rq_mapping(drv_tags, tags); |
bd63141d | 3049 | |
24d2f903 CH |
3050 | while (!list_empty(&tags->page_list)) { |
3051 | page = list_first_entry(&tags->page_list, struct page, lru); | |
6753471c | 3052 | list_del_init(&page->lru); |
f75782e4 CM |
3053 | /* |
3054 | * Remove kmemleak object previously allocated in | |
273938bf | 3055 | * blk_mq_alloc_rqs(). |
f75782e4 CM |
3056 | */ |
3057 | kmemleak_free(page_address(page)); | |
320ae51f JA |
3058 | __free_pages(page, page->private); |
3059 | } | |
cc71a6f4 | 3060 | } |
320ae51f | 3061 | |
e155b0c2 | 3062 | void blk_mq_free_rq_map(struct blk_mq_tags *tags) |
cc71a6f4 | 3063 | { |
24d2f903 | 3064 | kfree(tags->rqs); |
cc71a6f4 | 3065 | tags->rqs = NULL; |
2af8cbe3 JA |
3066 | kfree(tags->static_rqs); |
3067 | tags->static_rqs = NULL; | |
320ae51f | 3068 | |
e155b0c2 | 3069 | blk_mq_free_tags(tags); |
320ae51f JA |
3070 | } |
3071 | ||
63064be1 JG |
3072 | static struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, |
3073 | unsigned int hctx_idx, | |
3074 | unsigned int nr_tags, | |
e155b0c2 | 3075 | unsigned int reserved_tags) |
320ae51f | 3076 | { |
24d2f903 | 3077 | struct blk_mq_tags *tags; |
59f082e4 | 3078 | int node; |
320ae51f | 3079 | |
7d76f856 | 3080 | node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx); |
59f082e4 SL |
3081 | if (node == NUMA_NO_NODE) |
3082 | node = set->numa_node; | |
3083 | ||
e155b0c2 JG |
3084 | tags = blk_mq_init_tags(nr_tags, reserved_tags, node, |
3085 | BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags)); | |
24d2f903 CH |
3086 | if (!tags) |
3087 | return NULL; | |
320ae51f | 3088 | |
590b5b7d | 3089 | tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *), |
36e1f3d1 | 3090 | GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, |
59f082e4 | 3091 | node); |
24d2f903 | 3092 | if (!tags->rqs) { |
e155b0c2 | 3093 | blk_mq_free_tags(tags); |
24d2f903 CH |
3094 | return NULL; |
3095 | } | |
320ae51f | 3096 | |
590b5b7d KC |
3097 | tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *), |
3098 | GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, | |
3099 | node); | |
2af8cbe3 JA |
3100 | if (!tags->static_rqs) { |
3101 | kfree(tags->rqs); | |
e155b0c2 | 3102 | blk_mq_free_tags(tags); |
2af8cbe3 JA |
3103 | return NULL; |
3104 | } | |
3105 | ||
cc71a6f4 JA |
3106 | return tags; |
3107 | } | |
3108 | ||
1d9bd516 TH |
3109 | static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq, |
3110 | unsigned int hctx_idx, int node) | |
3111 | { | |
3112 | int ret; | |
3113 | ||
3114 | if (set->ops->init_request) { | |
3115 | ret = set->ops->init_request(set, rq, hctx_idx, node); | |
3116 | if (ret) | |
3117 | return ret; | |
3118 | } | |
3119 | ||
12f5b931 | 3120 | WRITE_ONCE(rq->state, MQ_RQ_IDLE); |
1d9bd516 TH |
3121 | return 0; |
3122 | } | |
3123 | ||
63064be1 JG |
3124 | static int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, |
3125 | struct blk_mq_tags *tags, | |
3126 | unsigned int hctx_idx, unsigned int depth) | |
cc71a6f4 JA |
3127 | { |
3128 | unsigned int i, j, entries_per_page, max_order = 4; | |
3129 | size_t rq_size, left; | |
59f082e4 SL |
3130 | int node; |
3131 | ||
7d76f856 | 3132 | node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx); |
59f082e4 SL |
3133 | if (node == NUMA_NO_NODE) |
3134 | node = set->numa_node; | |
cc71a6f4 JA |
3135 | |
3136 | INIT_LIST_HEAD(&tags->page_list); | |
3137 | ||
320ae51f JA |
3138 | /* |
3139 | * rq_size is the size of the request plus driver payload, rounded | |
3140 | * to the cacheline size | |
3141 | */ | |
24d2f903 | 3142 | rq_size = round_up(sizeof(struct request) + set->cmd_size, |
320ae51f | 3143 | cache_line_size()); |
cc71a6f4 | 3144 | left = rq_size * depth; |
320ae51f | 3145 | |
cc71a6f4 | 3146 | for (i = 0; i < depth; ) { |
320ae51f JA |
3147 | int this_order = max_order; |
3148 | struct page *page; | |
3149 | int to_do; | |
3150 | void *p; | |
3151 | ||
b3a834b1 | 3152 | while (this_order && left < order_to_size(this_order - 1)) |
320ae51f JA |
3153 | this_order--; |
3154 | ||
3155 | do { | |
59f082e4 | 3156 | page = alloc_pages_node(node, |
36e1f3d1 | 3157 | GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO, |
a5164405 | 3158 | this_order); |
320ae51f JA |
3159 | if (page) |
3160 | break; | |
3161 | if (!this_order--) | |
3162 | break; | |
3163 | if (order_to_size(this_order) < rq_size) | |
3164 | break; | |
3165 | } while (1); | |
3166 | ||
3167 | if (!page) | |
24d2f903 | 3168 | goto fail; |
320ae51f JA |
3169 | |
3170 | page->private = this_order; | |
24d2f903 | 3171 | list_add_tail(&page->lru, &tags->page_list); |
320ae51f JA |
3172 | |
3173 | p = page_address(page); | |
f75782e4 CM |
3174 | /* |
3175 | * Allow kmemleak to scan these pages as they contain pointers | |
3176 | * to additional allocations like via ops->init_request(). | |
3177 | */ | |
36e1f3d1 | 3178 | kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO); |
320ae51f | 3179 | entries_per_page = order_to_size(this_order) / rq_size; |
cc71a6f4 | 3180 | to_do = min(entries_per_page, depth - i); |
320ae51f JA |
3181 | left -= to_do * rq_size; |
3182 | for (j = 0; j < to_do; j++) { | |
2af8cbe3 JA |
3183 | struct request *rq = p; |
3184 | ||
3185 | tags->static_rqs[i] = rq; | |
1d9bd516 TH |
3186 | if (blk_mq_init_request(set, rq, hctx_idx, node)) { |
3187 | tags->static_rqs[i] = NULL; | |
3188 | goto fail; | |
e9b267d9 CH |
3189 | } |
3190 | ||
320ae51f JA |
3191 | p += rq_size; |
3192 | i++; | |
3193 | } | |
3194 | } | |
cc71a6f4 | 3195 | return 0; |
320ae51f | 3196 | |
24d2f903 | 3197 | fail: |
cc71a6f4 JA |
3198 | blk_mq_free_rqs(set, tags, hctx_idx); |
3199 | return -ENOMEM; | |
320ae51f JA |
3200 | } |
3201 | ||
bf0beec0 ML |
3202 | struct rq_iter_data { |
3203 | struct blk_mq_hw_ctx *hctx; | |
3204 | bool has_rq; | |
3205 | }; | |
3206 | ||
3207 | static bool blk_mq_has_request(struct request *rq, void *data, bool reserved) | |
3208 | { | |
3209 | struct rq_iter_data *iter_data = data; | |
3210 | ||
3211 | if (rq->mq_hctx != iter_data->hctx) | |
3212 | return true; | |
3213 | iter_data->has_rq = true; | |
3214 | return false; | |
3215 | } | |
3216 | ||
3217 | static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx) | |
3218 | { | |
3219 | struct blk_mq_tags *tags = hctx->sched_tags ? | |
3220 | hctx->sched_tags : hctx->tags; | |
3221 | struct rq_iter_data data = { | |
3222 | .hctx = hctx, | |
3223 | }; | |
3224 | ||
3225 | blk_mq_all_tag_iter(tags, blk_mq_has_request, &data); | |
3226 | return data.has_rq; | |
3227 | } | |
3228 | ||
3229 | static inline bool blk_mq_last_cpu_in_hctx(unsigned int cpu, | |
3230 | struct blk_mq_hw_ctx *hctx) | |
3231 | { | |
3232 | if (cpumask_next_and(-1, hctx->cpumask, cpu_online_mask) != cpu) | |
3233 | return false; | |
3234 | if (cpumask_next_and(cpu, hctx->cpumask, cpu_online_mask) < nr_cpu_ids) | |
3235 | return false; | |
3236 | return true; | |
3237 | } | |
3238 | ||
3239 | static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node) | |
3240 | { | |
3241 | struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node, | |
3242 | struct blk_mq_hw_ctx, cpuhp_online); | |
3243 | ||
3244 | if (!cpumask_test_cpu(cpu, hctx->cpumask) || | |
3245 | !blk_mq_last_cpu_in_hctx(cpu, hctx)) | |
3246 | return 0; | |
3247 | ||
3248 | /* | |
3249 | * Prevent new request from being allocated on the current hctx. | |
3250 | * | |
3251 | * The smp_mb__after_atomic() Pairs with the implied barrier in | |
3252 | * test_and_set_bit_lock in sbitmap_get(). Ensures the inactive flag is | |
3253 | * seen once we return from the tag allocator. | |
3254 | */ | |
3255 | set_bit(BLK_MQ_S_INACTIVE, &hctx->state); | |
3256 | smp_mb__after_atomic(); | |
3257 | ||
3258 | /* | |
3259 | * Try to grab a reference to the queue and wait for any outstanding | |
3260 | * requests. If we could not grab a reference the queue has been | |
3261 | * frozen and there are no requests. | |
3262 | */ | |
3263 | if (percpu_ref_tryget(&hctx->queue->q_usage_counter)) { | |
3264 | while (blk_mq_hctx_has_requests(hctx)) | |
3265 | msleep(5); | |
3266 | percpu_ref_put(&hctx->queue->q_usage_counter); | |
3267 | } | |
3268 | ||
3269 | return 0; | |
3270 | } | |
3271 | ||
3272 | static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node) | |
3273 | { | |
3274 | struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node, | |
3275 | struct blk_mq_hw_ctx, cpuhp_online); | |
3276 | ||
3277 | if (cpumask_test_cpu(cpu, hctx->cpumask)) | |
3278 | clear_bit(BLK_MQ_S_INACTIVE, &hctx->state); | |
3279 | return 0; | |
3280 | } | |
3281 | ||
e57690fe JA |
3282 | /* |
3283 | * 'cpu' is going away. splice any existing rq_list entries from this | |
3284 | * software queue to the hw queue dispatch list, and ensure that it | |
3285 | * gets run. | |
3286 | */ | |
9467f859 | 3287 | static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node) |
484b4061 | 3288 | { |
9467f859 | 3289 | struct blk_mq_hw_ctx *hctx; |
484b4061 JA |
3290 | struct blk_mq_ctx *ctx; |
3291 | LIST_HEAD(tmp); | |
c16d6b5a | 3292 | enum hctx_type type; |
484b4061 | 3293 | |
9467f859 | 3294 | hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead); |
bf0beec0 ML |
3295 | if (!cpumask_test_cpu(cpu, hctx->cpumask)) |
3296 | return 0; | |
3297 | ||
e57690fe | 3298 | ctx = __blk_mq_get_ctx(hctx->queue, cpu); |
c16d6b5a | 3299 | type = hctx->type; |
484b4061 JA |
3300 | |
3301 | spin_lock(&ctx->lock); | |
c16d6b5a ML |
3302 | if (!list_empty(&ctx->rq_lists[type])) { |
3303 | list_splice_init(&ctx->rq_lists[type], &tmp); | |
484b4061 JA |
3304 | blk_mq_hctx_clear_pending(hctx, ctx); |
3305 | } | |
3306 | spin_unlock(&ctx->lock); | |
3307 | ||
3308 | if (list_empty(&tmp)) | |
9467f859 | 3309 | return 0; |
484b4061 | 3310 | |
e57690fe JA |
3311 | spin_lock(&hctx->lock); |
3312 | list_splice_tail_init(&tmp, &hctx->dispatch); | |
3313 | spin_unlock(&hctx->lock); | |
484b4061 JA |
3314 | |
3315 | blk_mq_run_hw_queue(hctx, true); | |
9467f859 | 3316 | return 0; |
484b4061 JA |
3317 | } |
3318 | ||
9467f859 | 3319 | static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx) |
484b4061 | 3320 | { |
bf0beec0 ML |
3321 | if (!(hctx->flags & BLK_MQ_F_STACKING)) |
3322 | cpuhp_state_remove_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE, | |
3323 | &hctx->cpuhp_online); | |
9467f859 TG |
3324 | cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD, |
3325 | &hctx->cpuhp_dead); | |
484b4061 JA |
3326 | } |
3327 | ||
364b6181 ML |
3328 | /* |
3329 | * Before freeing hw queue, clearing the flush request reference in | |
3330 | * tags->rqs[] for avoiding potential UAF. | |
3331 | */ | |
3332 | static void blk_mq_clear_flush_rq_mapping(struct blk_mq_tags *tags, | |
3333 | unsigned int queue_depth, struct request *flush_rq) | |
3334 | { | |
3335 | int i; | |
3336 | unsigned long flags; | |
3337 | ||
3338 | /* The hw queue may not be mapped yet */ | |
3339 | if (!tags) | |
3340 | return; | |
3341 | ||
0a467d0f | 3342 | WARN_ON_ONCE(req_ref_read(flush_rq) != 0); |
364b6181 ML |
3343 | |
3344 | for (i = 0; i < queue_depth; i++) | |
3345 | cmpxchg(&tags->rqs[i], flush_rq, NULL); | |
3346 | ||
3347 | /* | |
3348 | * Wait until all pending iteration is done. | |
3349 | * | |
3350 | * Request reference is cleared and it is guaranteed to be observed | |
3351 | * after the ->lock is released. | |
3352 | */ | |
3353 | spin_lock_irqsave(&tags->lock, flags); | |
3354 | spin_unlock_irqrestore(&tags->lock, flags); | |
3355 | } | |
3356 | ||
c3b4afca | 3357 | /* hctx->ctxs will be freed in queue's release handler */ |
08e98fc6 ML |
3358 | static void blk_mq_exit_hctx(struct request_queue *q, |
3359 | struct blk_mq_tag_set *set, | |
3360 | struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx) | |
3361 | { | |
364b6181 ML |
3362 | struct request *flush_rq = hctx->fq->flush_rq; |
3363 | ||
8ab0b7dc ML |
3364 | if (blk_mq_hw_queue_mapped(hctx)) |
3365 | blk_mq_tag_idle(hctx); | |
08e98fc6 | 3366 | |
364b6181 ML |
3367 | blk_mq_clear_flush_rq_mapping(set->tags[hctx_idx], |
3368 | set->queue_depth, flush_rq); | |
f70ced09 | 3369 | if (set->ops->exit_request) |
364b6181 | 3370 | set->ops->exit_request(set, flush_rq, hctx_idx); |
f70ced09 | 3371 | |
08e98fc6 ML |
3372 | if (set->ops->exit_hctx) |
3373 | set->ops->exit_hctx(hctx, hctx_idx); | |
3374 | ||
9467f859 | 3375 | blk_mq_remove_cpuhp(hctx); |
2f8f1336 ML |
3376 | |
3377 | spin_lock(&q->unused_hctx_lock); | |
3378 | list_add(&hctx->hctx_list, &q->unused_hctx_list); | |
3379 | spin_unlock(&q->unused_hctx_lock); | |
08e98fc6 ML |
3380 | } |
3381 | ||
624dbe47 ML |
3382 | static void blk_mq_exit_hw_queues(struct request_queue *q, |
3383 | struct blk_mq_tag_set *set, int nr_queue) | |
3384 | { | |
3385 | struct blk_mq_hw_ctx *hctx; | |
3386 | unsigned int i; | |
3387 | ||
3388 | queue_for_each_hw_ctx(q, hctx, i) { | |
3389 | if (i == nr_queue) | |
3390 | break; | |
477e19de | 3391 | blk_mq_debugfs_unregister_hctx(hctx); |
08e98fc6 | 3392 | blk_mq_exit_hctx(q, set, hctx, i); |
624dbe47 | 3393 | } |
624dbe47 ML |
3394 | } |
3395 | ||
08e98fc6 ML |
3396 | static int blk_mq_init_hctx(struct request_queue *q, |
3397 | struct blk_mq_tag_set *set, | |
3398 | struct blk_mq_hw_ctx *hctx, unsigned hctx_idx) | |
320ae51f | 3399 | { |
7c6c5b7c ML |
3400 | hctx->queue_num = hctx_idx; |
3401 | ||
bf0beec0 ML |
3402 | if (!(hctx->flags & BLK_MQ_F_STACKING)) |
3403 | cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE, | |
3404 | &hctx->cpuhp_online); | |
7c6c5b7c ML |
3405 | cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead); |
3406 | ||
3407 | hctx->tags = set->tags[hctx_idx]; | |
3408 | ||
3409 | if (set->ops->init_hctx && | |
3410 | set->ops->init_hctx(hctx, set->driver_data, hctx_idx)) | |
3411 | goto unregister_cpu_notifier; | |
08e98fc6 | 3412 | |
7c6c5b7c ML |
3413 | if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx, |
3414 | hctx->numa_node)) | |
3415 | goto exit_hctx; | |
3416 | return 0; | |
3417 | ||
3418 | exit_hctx: | |
3419 | if (set->ops->exit_hctx) | |
3420 | set->ops->exit_hctx(hctx, hctx_idx); | |
3421 | unregister_cpu_notifier: | |
3422 | blk_mq_remove_cpuhp(hctx); | |
3423 | return -1; | |
3424 | } | |
3425 | ||
3426 | static struct blk_mq_hw_ctx * | |
3427 | blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set, | |
3428 | int node) | |
3429 | { | |
3430 | struct blk_mq_hw_ctx *hctx; | |
3431 | gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY; | |
3432 | ||
704b914f | 3433 | hctx = kzalloc_node(sizeof(struct blk_mq_hw_ctx), gfp, node); |
7c6c5b7c ML |
3434 | if (!hctx) |
3435 | goto fail_alloc_hctx; | |
3436 | ||
3437 | if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node)) | |
3438 | goto free_hctx; | |
3439 | ||
3440 | atomic_set(&hctx->nr_active, 0); | |
08e98fc6 | 3441 | if (node == NUMA_NO_NODE) |
7c6c5b7c ML |
3442 | node = set->numa_node; |
3443 | hctx->numa_node = node; | |
08e98fc6 | 3444 | |
9f993737 | 3445 | INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn); |
08e98fc6 ML |
3446 | spin_lock_init(&hctx->lock); |
3447 | INIT_LIST_HEAD(&hctx->dispatch); | |
3448 | hctx->queue = q; | |
51db1c37 | 3449 | hctx->flags = set->flags & ~BLK_MQ_F_TAG_QUEUE_SHARED; |
08e98fc6 | 3450 | |
2f8f1336 ML |
3451 | INIT_LIST_HEAD(&hctx->hctx_list); |
3452 | ||
320ae51f | 3453 | /* |
08e98fc6 ML |
3454 | * Allocate space for all possible cpus to avoid allocation at |
3455 | * runtime | |
320ae51f | 3456 | */ |
d904bfa7 | 3457 | hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *), |
7c6c5b7c | 3458 | gfp, node); |
08e98fc6 | 3459 | if (!hctx->ctxs) |
7c6c5b7c | 3460 | goto free_cpumask; |
320ae51f | 3461 | |
5b202853 | 3462 | if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8), |
c548e62b | 3463 | gfp, node, false, false)) |
08e98fc6 | 3464 | goto free_ctxs; |
08e98fc6 | 3465 | hctx->nr_ctx = 0; |
320ae51f | 3466 | |
5815839b | 3467 | spin_lock_init(&hctx->dispatch_wait_lock); |
eb619fdb JA |
3468 | init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake); |
3469 | INIT_LIST_HEAD(&hctx->dispatch_wait.entry); | |
3470 | ||
754a1572 | 3471 | hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp); |
f70ced09 | 3472 | if (!hctx->fq) |
7c6c5b7c | 3473 | goto free_bitmap; |
320ae51f | 3474 | |
7c6c5b7c | 3475 | blk_mq_hctx_kobj_init(hctx); |
6a83e74d | 3476 | |
7c6c5b7c | 3477 | return hctx; |
320ae51f | 3478 | |
08e98fc6 | 3479 | free_bitmap: |
88459642 | 3480 | sbitmap_free(&hctx->ctx_map); |
08e98fc6 ML |
3481 | free_ctxs: |
3482 | kfree(hctx->ctxs); | |
7c6c5b7c ML |
3483 | free_cpumask: |
3484 | free_cpumask_var(hctx->cpumask); | |
3485 | free_hctx: | |
3486 | kfree(hctx); | |
3487 | fail_alloc_hctx: | |
3488 | return NULL; | |
08e98fc6 | 3489 | } |
320ae51f | 3490 | |
320ae51f JA |
3491 | static void blk_mq_init_cpu_queues(struct request_queue *q, |
3492 | unsigned int nr_hw_queues) | |
3493 | { | |
b3c661b1 JA |
3494 | struct blk_mq_tag_set *set = q->tag_set; |
3495 | unsigned int i, j; | |
320ae51f JA |
3496 | |
3497 | for_each_possible_cpu(i) { | |
3498 | struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i); | |
3499 | struct blk_mq_hw_ctx *hctx; | |
c16d6b5a | 3500 | int k; |
320ae51f | 3501 | |
320ae51f JA |
3502 | __ctx->cpu = i; |
3503 | spin_lock_init(&__ctx->lock); | |
c16d6b5a ML |
3504 | for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++) |
3505 | INIT_LIST_HEAD(&__ctx->rq_lists[k]); | |
3506 | ||
320ae51f JA |
3507 | __ctx->queue = q; |
3508 | ||
320ae51f JA |
3509 | /* |
3510 | * Set local node, IFF we have more than one hw queue. If | |
3511 | * not, we remain on the home node of the device | |
3512 | */ | |
b3c661b1 JA |
3513 | for (j = 0; j < set->nr_maps; j++) { |
3514 | hctx = blk_mq_map_queue_type(q, j, i); | |
3515 | if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE) | |
576e85c5 | 3516 | hctx->numa_node = cpu_to_node(i); |
b3c661b1 | 3517 | } |
320ae51f JA |
3518 | } |
3519 | } | |
3520 | ||
63064be1 JG |
3521 | struct blk_mq_tags *blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set, |
3522 | unsigned int hctx_idx, | |
3523 | unsigned int depth) | |
cc71a6f4 | 3524 | { |
63064be1 JG |
3525 | struct blk_mq_tags *tags; |
3526 | int ret; | |
cc71a6f4 | 3527 | |
e155b0c2 | 3528 | tags = blk_mq_alloc_rq_map(set, hctx_idx, depth, set->reserved_tags); |
63064be1 JG |
3529 | if (!tags) |
3530 | return NULL; | |
cc71a6f4 | 3531 | |
63064be1 JG |
3532 | ret = blk_mq_alloc_rqs(set, tags, hctx_idx, depth); |
3533 | if (ret) { | |
e155b0c2 | 3534 | blk_mq_free_rq_map(tags); |
63064be1 JG |
3535 | return NULL; |
3536 | } | |
cc71a6f4 | 3537 | |
63064be1 | 3538 | return tags; |
cc71a6f4 JA |
3539 | } |
3540 | ||
63064be1 JG |
3541 | static bool __blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set, |
3542 | int hctx_idx) | |
cc71a6f4 | 3543 | { |
079a2e3e JG |
3544 | if (blk_mq_is_shared_tags(set->flags)) { |
3545 | set->tags[hctx_idx] = set->shared_tags; | |
1c0706a7 | 3546 | |
e155b0c2 | 3547 | return true; |
bd166ef1 | 3548 | } |
e155b0c2 | 3549 | |
63064be1 JG |
3550 | set->tags[hctx_idx] = blk_mq_alloc_map_and_rqs(set, hctx_idx, |
3551 | set->queue_depth); | |
3552 | ||
3553 | return set->tags[hctx_idx]; | |
cc71a6f4 JA |
3554 | } |
3555 | ||
645db34e JG |
3556 | void blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set, |
3557 | struct blk_mq_tags *tags, | |
3558 | unsigned int hctx_idx) | |
cc71a6f4 | 3559 | { |
645db34e JG |
3560 | if (tags) { |
3561 | blk_mq_free_rqs(set, tags, hctx_idx); | |
e155b0c2 | 3562 | blk_mq_free_rq_map(tags); |
bd166ef1 | 3563 | } |
cc71a6f4 JA |
3564 | } |
3565 | ||
e155b0c2 JG |
3566 | static void __blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set, |
3567 | unsigned int hctx_idx) | |
3568 | { | |
079a2e3e | 3569 | if (!blk_mq_is_shared_tags(set->flags)) |
e155b0c2 JG |
3570 | blk_mq_free_map_and_rqs(set, set->tags[hctx_idx], hctx_idx); |
3571 | ||
3572 | set->tags[hctx_idx] = NULL; | |
cc71a6f4 JA |
3573 | } |
3574 | ||
4b855ad3 | 3575 | static void blk_mq_map_swqueue(struct request_queue *q) |
320ae51f | 3576 | { |
b3c661b1 | 3577 | unsigned int i, j, hctx_idx; |
320ae51f JA |
3578 | struct blk_mq_hw_ctx *hctx; |
3579 | struct blk_mq_ctx *ctx; | |
2a34c087 | 3580 | struct blk_mq_tag_set *set = q->tag_set; |
320ae51f JA |
3581 | |
3582 | queue_for_each_hw_ctx(q, hctx, i) { | |
e4043dcf | 3583 | cpumask_clear(hctx->cpumask); |
320ae51f | 3584 | hctx->nr_ctx = 0; |
d416c92c | 3585 | hctx->dispatch_from = NULL; |
320ae51f JA |
3586 | } |
3587 | ||
3588 | /* | |
4b855ad3 | 3589 | * Map software to hardware queues. |
4412efec ML |
3590 | * |
3591 | * If the cpu isn't present, the cpu is mapped to first hctx. | |
320ae51f | 3592 | */ |
20e4d813 | 3593 | for_each_possible_cpu(i) { |
4412efec | 3594 | |
897bb0c7 | 3595 | ctx = per_cpu_ptr(q->queue_ctx, i); |
b3c661b1 | 3596 | for (j = 0; j < set->nr_maps; j++) { |
bb94aea1 JW |
3597 | if (!set->map[j].nr_queues) { |
3598 | ctx->hctxs[j] = blk_mq_map_queue_type(q, | |
3599 | HCTX_TYPE_DEFAULT, i); | |
e5edd5f2 | 3600 | continue; |
bb94aea1 | 3601 | } |
fd689871 ML |
3602 | hctx_idx = set->map[j].mq_map[i]; |
3603 | /* unmapped hw queue can be remapped after CPU topo changed */ | |
3604 | if (!set->tags[hctx_idx] && | |
63064be1 | 3605 | !__blk_mq_alloc_map_and_rqs(set, hctx_idx)) { |
fd689871 ML |
3606 | /* |
3607 | * If tags initialization fail for some hctx, | |
3608 | * that hctx won't be brought online. In this | |
3609 | * case, remap the current ctx to hctx[0] which | |
3610 | * is guaranteed to always have tags allocated | |
3611 | */ | |
3612 | set->map[j].mq_map[i] = 0; | |
3613 | } | |
e5edd5f2 | 3614 | |
b3c661b1 | 3615 | hctx = blk_mq_map_queue_type(q, j, i); |
8ccdf4a3 | 3616 | ctx->hctxs[j] = hctx; |
b3c661b1 JA |
3617 | /* |
3618 | * If the CPU is already set in the mask, then we've | |
3619 | * mapped this one already. This can happen if | |
3620 | * devices share queues across queue maps. | |
3621 | */ | |
3622 | if (cpumask_test_cpu(i, hctx->cpumask)) | |
3623 | continue; | |
3624 | ||
3625 | cpumask_set_cpu(i, hctx->cpumask); | |
3626 | hctx->type = j; | |
3627 | ctx->index_hw[hctx->type] = hctx->nr_ctx; | |
3628 | hctx->ctxs[hctx->nr_ctx++] = ctx; | |
3629 | ||
3630 | /* | |
3631 | * If the nr_ctx type overflows, we have exceeded the | |
3632 | * amount of sw queues we can support. | |
3633 | */ | |
3634 | BUG_ON(!hctx->nr_ctx); | |
3635 | } | |
bb94aea1 JW |
3636 | |
3637 | for (; j < HCTX_MAX_TYPES; j++) | |
3638 | ctx->hctxs[j] = blk_mq_map_queue_type(q, | |
3639 | HCTX_TYPE_DEFAULT, i); | |
320ae51f | 3640 | } |
506e931f JA |
3641 | |
3642 | queue_for_each_hw_ctx(q, hctx, i) { | |
4412efec ML |
3643 | /* |
3644 | * If no software queues are mapped to this hardware queue, | |
3645 | * disable it and free the request entries. | |
3646 | */ | |
3647 | if (!hctx->nr_ctx) { | |
3648 | /* Never unmap queue 0. We need it as a | |
3649 | * fallback in case of a new remap fails | |
3650 | * allocation | |
3651 | */ | |
e155b0c2 JG |
3652 | if (i) |
3653 | __blk_mq_free_map_and_rqs(set, i); | |
4412efec ML |
3654 | |
3655 | hctx->tags = NULL; | |
3656 | continue; | |
3657 | } | |
484b4061 | 3658 | |
2a34c087 ML |
3659 | hctx->tags = set->tags[i]; |
3660 | WARN_ON(!hctx->tags); | |
3661 | ||
889fa31f CY |
3662 | /* |
3663 | * Set the map size to the number of mapped software queues. | |
3664 | * This is more accurate and more efficient than looping | |
3665 | * over all possibly mapped software queues. | |
3666 | */ | |
88459642 | 3667 | sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx); |
889fa31f | 3668 | |
484b4061 JA |
3669 | /* |
3670 | * Initialize batch roundrobin counts | |
3671 | */ | |
f82ddf19 | 3672 | hctx->next_cpu = blk_mq_first_mapped_cpu(hctx); |
506e931f JA |
3673 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; |
3674 | } | |
320ae51f JA |
3675 | } |
3676 | ||
8e8320c9 JA |
3677 | /* |
3678 | * Caller needs to ensure that we're either frozen/quiesced, or that | |
3679 | * the queue isn't live yet. | |
3680 | */ | |
2404e607 | 3681 | static void queue_set_hctx_shared(struct request_queue *q, bool shared) |
0d2602ca JA |
3682 | { |
3683 | struct blk_mq_hw_ctx *hctx; | |
0d2602ca JA |
3684 | int i; |
3685 | ||
2404e607 | 3686 | queue_for_each_hw_ctx(q, hctx, i) { |
454bb677 | 3687 | if (shared) { |
51db1c37 | 3688 | hctx->flags |= BLK_MQ_F_TAG_QUEUE_SHARED; |
454bb677 YK |
3689 | } else { |
3690 | blk_mq_tag_idle(hctx); | |
51db1c37 | 3691 | hctx->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED; |
454bb677 | 3692 | } |
2404e607 JM |
3693 | } |
3694 | } | |
3695 | ||
655ac300 HR |
3696 | static void blk_mq_update_tag_set_shared(struct blk_mq_tag_set *set, |
3697 | bool shared) | |
2404e607 JM |
3698 | { |
3699 | struct request_queue *q; | |
0d2602ca | 3700 | |
705cda97 BVA |
3701 | lockdep_assert_held(&set->tag_list_lock); |
3702 | ||
0d2602ca JA |
3703 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
3704 | blk_mq_freeze_queue(q); | |
2404e607 | 3705 | queue_set_hctx_shared(q, shared); |
0d2602ca JA |
3706 | blk_mq_unfreeze_queue(q); |
3707 | } | |
3708 | } | |
3709 | ||
3710 | static void blk_mq_del_queue_tag_set(struct request_queue *q) | |
3711 | { | |
3712 | struct blk_mq_tag_set *set = q->tag_set; | |
3713 | ||
0d2602ca | 3714 | mutex_lock(&set->tag_list_lock); |
08c875cb | 3715 | list_del(&q->tag_set_list); |
2404e607 JM |
3716 | if (list_is_singular(&set->tag_list)) { |
3717 | /* just transitioned to unshared */ | |
51db1c37 | 3718 | set->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED; |
2404e607 | 3719 | /* update existing queue */ |
655ac300 | 3720 | blk_mq_update_tag_set_shared(set, false); |
2404e607 | 3721 | } |
0d2602ca | 3722 | mutex_unlock(&set->tag_list_lock); |
a347c7ad | 3723 | INIT_LIST_HEAD(&q->tag_set_list); |
0d2602ca JA |
3724 | } |
3725 | ||
3726 | static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set, | |
3727 | struct request_queue *q) | |
3728 | { | |
0d2602ca | 3729 | mutex_lock(&set->tag_list_lock); |
2404e607 | 3730 | |
ff821d27 JA |
3731 | /* |
3732 | * Check to see if we're transitioning to shared (from 1 to 2 queues). | |
3733 | */ | |
3734 | if (!list_empty(&set->tag_list) && | |
51db1c37 ML |
3735 | !(set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) { |
3736 | set->flags |= BLK_MQ_F_TAG_QUEUE_SHARED; | |
2404e607 | 3737 | /* update existing queue */ |
655ac300 | 3738 | blk_mq_update_tag_set_shared(set, true); |
2404e607 | 3739 | } |
51db1c37 | 3740 | if (set->flags & BLK_MQ_F_TAG_QUEUE_SHARED) |
2404e607 | 3741 | queue_set_hctx_shared(q, true); |
08c875cb | 3742 | list_add_tail(&q->tag_set_list, &set->tag_list); |
2404e607 | 3743 | |
0d2602ca JA |
3744 | mutex_unlock(&set->tag_list_lock); |
3745 | } | |
3746 | ||
1db4909e ML |
3747 | /* All allocations will be freed in release handler of q->mq_kobj */ |
3748 | static int blk_mq_alloc_ctxs(struct request_queue *q) | |
3749 | { | |
3750 | struct blk_mq_ctxs *ctxs; | |
3751 | int cpu; | |
3752 | ||
3753 | ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL); | |
3754 | if (!ctxs) | |
3755 | return -ENOMEM; | |
3756 | ||
3757 | ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx); | |
3758 | if (!ctxs->queue_ctx) | |
3759 | goto fail; | |
3760 | ||
3761 | for_each_possible_cpu(cpu) { | |
3762 | struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu); | |
3763 | ctx->ctxs = ctxs; | |
3764 | } | |
3765 | ||
3766 | q->mq_kobj = &ctxs->kobj; | |
3767 | q->queue_ctx = ctxs->queue_ctx; | |
3768 | ||
3769 | return 0; | |
3770 | fail: | |
3771 | kfree(ctxs); | |
3772 | return -ENOMEM; | |
3773 | } | |
3774 | ||
e09aae7e ML |
3775 | /* |
3776 | * It is the actual release handler for mq, but we do it from | |
3777 | * request queue's release handler for avoiding use-after-free | |
3778 | * and headache because q->mq_kobj shouldn't have been introduced, | |
3779 | * but we can't group ctx/kctx kobj without it. | |
3780 | */ | |
3781 | void blk_mq_release(struct request_queue *q) | |
3782 | { | |
2f8f1336 ML |
3783 | struct blk_mq_hw_ctx *hctx, *next; |
3784 | int i; | |
e09aae7e | 3785 | |
2f8f1336 ML |
3786 | queue_for_each_hw_ctx(q, hctx, i) |
3787 | WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list)); | |
3788 | ||
3789 | /* all hctx are in .unused_hctx_list now */ | |
3790 | list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) { | |
3791 | list_del_init(&hctx->hctx_list); | |
6c8b232e | 3792 | kobject_put(&hctx->kobj); |
c3b4afca | 3793 | } |
e09aae7e ML |
3794 | |
3795 | kfree(q->queue_hw_ctx); | |
3796 | ||
7ea5fe31 ML |
3797 | /* |
3798 | * release .mq_kobj and sw queue's kobject now because | |
3799 | * both share lifetime with request queue. | |
3800 | */ | |
3801 | blk_mq_sysfs_deinit(q); | |
e09aae7e ML |
3802 | } |
3803 | ||
5ec780a6 | 3804 | static struct request_queue *blk_mq_init_queue_data(struct blk_mq_tag_set *set, |
2f227bb9 | 3805 | void *queuedata) |
b62c21b7 | 3806 | { |
26a9750a CH |
3807 | struct request_queue *q; |
3808 | int ret; | |
b62c21b7 | 3809 | |
704b914f | 3810 | q = blk_alloc_queue(set->numa_node, set->flags & BLK_MQ_F_BLOCKING); |
26a9750a | 3811 | if (!q) |
b62c21b7 | 3812 | return ERR_PTR(-ENOMEM); |
26a9750a CH |
3813 | q->queuedata = queuedata; |
3814 | ret = blk_mq_init_allocated_queue(set, q); | |
3815 | if (ret) { | |
3816 | blk_cleanup_queue(q); | |
3817 | return ERR_PTR(ret); | |
3818 | } | |
b62c21b7 MS |
3819 | return q; |
3820 | } | |
2f227bb9 CH |
3821 | |
3822 | struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set) | |
3823 | { | |
3824 | return blk_mq_init_queue_data(set, NULL); | |
3825 | } | |
b62c21b7 MS |
3826 | EXPORT_SYMBOL(blk_mq_init_queue); |
3827 | ||
4dcc4874 CH |
3828 | struct gendisk *__blk_mq_alloc_disk(struct blk_mq_tag_set *set, void *queuedata, |
3829 | struct lock_class_key *lkclass) | |
9316a9ed JA |
3830 | { |
3831 | struct request_queue *q; | |
b461dfc4 | 3832 | struct gendisk *disk; |
9316a9ed | 3833 | |
b461dfc4 CH |
3834 | q = blk_mq_init_queue_data(set, queuedata); |
3835 | if (IS_ERR(q)) | |
3836 | return ERR_CAST(q); | |
9316a9ed | 3837 | |
4a1fa41d | 3838 | disk = __alloc_disk_node(q, set->numa_node, lkclass); |
b461dfc4 CH |
3839 | if (!disk) { |
3840 | blk_cleanup_queue(q); | |
3841 | return ERR_PTR(-ENOMEM); | |
9316a9ed | 3842 | } |
b461dfc4 | 3843 | return disk; |
9316a9ed | 3844 | } |
b461dfc4 | 3845 | EXPORT_SYMBOL(__blk_mq_alloc_disk); |
9316a9ed | 3846 | |
34d11ffa JW |
3847 | static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx( |
3848 | struct blk_mq_tag_set *set, struct request_queue *q, | |
3849 | int hctx_idx, int node) | |
3850 | { | |
2f8f1336 | 3851 | struct blk_mq_hw_ctx *hctx = NULL, *tmp; |
34d11ffa | 3852 | |
2f8f1336 ML |
3853 | /* reuse dead hctx first */ |
3854 | spin_lock(&q->unused_hctx_lock); | |
3855 | list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) { | |
3856 | if (tmp->numa_node == node) { | |
3857 | hctx = tmp; | |
3858 | break; | |
3859 | } | |
3860 | } | |
3861 | if (hctx) | |
3862 | list_del_init(&hctx->hctx_list); | |
3863 | spin_unlock(&q->unused_hctx_lock); | |
3864 | ||
3865 | if (!hctx) | |
3866 | hctx = blk_mq_alloc_hctx(q, set, node); | |
34d11ffa | 3867 | if (!hctx) |
7c6c5b7c | 3868 | goto fail; |
34d11ffa | 3869 | |
7c6c5b7c ML |
3870 | if (blk_mq_init_hctx(q, set, hctx, hctx_idx)) |
3871 | goto free_hctx; | |
34d11ffa JW |
3872 | |
3873 | return hctx; | |
7c6c5b7c ML |
3874 | |
3875 | free_hctx: | |
3876 | kobject_put(&hctx->kobj); | |
3877 | fail: | |
3878 | return NULL; | |
34d11ffa JW |
3879 | } |
3880 | ||
868f2f0b KB |
3881 | static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set, |
3882 | struct request_queue *q) | |
320ae51f | 3883 | { |
e01ad46d | 3884 | int i, j, end; |
868f2f0b | 3885 | struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx; |
f14bbe77 | 3886 | |
ac0d6b92 BVA |
3887 | if (q->nr_hw_queues < set->nr_hw_queues) { |
3888 | struct blk_mq_hw_ctx **new_hctxs; | |
3889 | ||
3890 | new_hctxs = kcalloc_node(set->nr_hw_queues, | |
3891 | sizeof(*new_hctxs), GFP_KERNEL, | |
3892 | set->numa_node); | |
3893 | if (!new_hctxs) | |
3894 | return; | |
3895 | if (hctxs) | |
3896 | memcpy(new_hctxs, hctxs, q->nr_hw_queues * | |
3897 | sizeof(*hctxs)); | |
3898 | q->queue_hw_ctx = new_hctxs; | |
ac0d6b92 BVA |
3899 | kfree(hctxs); |
3900 | hctxs = new_hctxs; | |
3901 | } | |
3902 | ||
fb350e0a ML |
3903 | /* protect against switching io scheduler */ |
3904 | mutex_lock(&q->sysfs_lock); | |
24d2f903 | 3905 | for (i = 0; i < set->nr_hw_queues; i++) { |
868f2f0b | 3906 | int node; |
34d11ffa | 3907 | struct blk_mq_hw_ctx *hctx; |
868f2f0b | 3908 | |
7d76f856 | 3909 | node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], i); |
34d11ffa JW |
3910 | /* |
3911 | * If the hw queue has been mapped to another numa node, | |
3912 | * we need to realloc the hctx. If allocation fails, fallback | |
3913 | * to use the previous one. | |
3914 | */ | |
3915 | if (hctxs[i] && (hctxs[i]->numa_node == node)) | |
3916 | continue; | |
868f2f0b | 3917 | |
34d11ffa JW |
3918 | hctx = blk_mq_alloc_and_init_hctx(set, q, i, node); |
3919 | if (hctx) { | |
2f8f1336 | 3920 | if (hctxs[i]) |
34d11ffa | 3921 | blk_mq_exit_hctx(q, set, hctxs[i], i); |
34d11ffa JW |
3922 | hctxs[i] = hctx; |
3923 | } else { | |
3924 | if (hctxs[i]) | |
3925 | pr_warn("Allocate new hctx on node %d fails,\ | |
3926 | fallback to previous one on node %d\n", | |
3927 | node, hctxs[i]->numa_node); | |
3928 | else | |
3929 | break; | |
868f2f0b | 3930 | } |
320ae51f | 3931 | } |
e01ad46d JW |
3932 | /* |
3933 | * Increasing nr_hw_queues fails. Free the newly allocated | |
3934 | * hctxs and keep the previous q->nr_hw_queues. | |
3935 | */ | |
3936 | if (i != set->nr_hw_queues) { | |
3937 | j = q->nr_hw_queues; | |
3938 | end = i; | |
3939 | } else { | |
3940 | j = i; | |
3941 | end = q->nr_hw_queues; | |
3942 | q->nr_hw_queues = set->nr_hw_queues; | |
3943 | } | |
34d11ffa | 3944 | |
e01ad46d | 3945 | for (; j < end; j++) { |
868f2f0b KB |
3946 | struct blk_mq_hw_ctx *hctx = hctxs[j]; |
3947 | ||
3948 | if (hctx) { | |
868f2f0b | 3949 | blk_mq_exit_hctx(q, set, hctx, j); |
868f2f0b | 3950 | hctxs[j] = NULL; |
868f2f0b KB |
3951 | } |
3952 | } | |
fb350e0a | 3953 | mutex_unlock(&q->sysfs_lock); |
868f2f0b KB |
3954 | } |
3955 | ||
26a9750a CH |
3956 | int blk_mq_init_allocated_queue(struct blk_mq_tag_set *set, |
3957 | struct request_queue *q) | |
868f2f0b | 3958 | { |
704b914f ML |
3959 | WARN_ON_ONCE(blk_queue_has_srcu(q) != |
3960 | !!(set->flags & BLK_MQ_F_BLOCKING)); | |
3961 | ||
66841672 ML |
3962 | /* mark the queue as mq asap */ |
3963 | q->mq_ops = set->ops; | |
3964 | ||
34dbad5d | 3965 | q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn, |
720b8ccc SB |
3966 | blk_mq_poll_stats_bkt, |
3967 | BLK_MQ_POLL_STATS_BKTS, q); | |
34dbad5d OS |
3968 | if (!q->poll_cb) |
3969 | goto err_exit; | |
3970 | ||
1db4909e | 3971 | if (blk_mq_alloc_ctxs(q)) |
41de54c6 | 3972 | goto err_poll; |
868f2f0b | 3973 | |
737f98cf ML |
3974 | /* init q->mq_kobj and sw queues' kobjects */ |
3975 | blk_mq_sysfs_init(q); | |
3976 | ||
2f8f1336 ML |
3977 | INIT_LIST_HEAD(&q->unused_hctx_list); |
3978 | spin_lock_init(&q->unused_hctx_lock); | |
3979 | ||
868f2f0b KB |
3980 | blk_mq_realloc_hw_ctxs(set, q); |
3981 | if (!q->nr_hw_queues) | |
3982 | goto err_hctxs; | |
320ae51f | 3983 | |
287922eb | 3984 | INIT_WORK(&q->timeout_work, blk_mq_timeout_work); |
e56f698b | 3985 | blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ); |
320ae51f | 3986 | |
a8908939 | 3987 | q->tag_set = set; |
320ae51f | 3988 | |
94eddfbe | 3989 | q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT; |
cd19181b ML |
3990 | if (set->nr_maps > HCTX_TYPE_POLL && |
3991 | set->map[HCTX_TYPE_POLL].nr_queues) | |
6544d229 | 3992 | blk_queue_flag_set(QUEUE_FLAG_POLL, q); |
320ae51f | 3993 | |
2849450a | 3994 | INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work); |
6fca6a61 CH |
3995 | INIT_LIST_HEAD(&q->requeue_list); |
3996 | spin_lock_init(&q->requeue_lock); | |
3997 | ||
eba71768 JA |
3998 | q->nr_requests = set->queue_depth; |
3999 | ||
64f1c21e JA |
4000 | /* |
4001 | * Default to classic polling | |
4002 | */ | |
29ece8b4 | 4003 | q->poll_nsec = BLK_MQ_POLL_CLASSIC; |
64f1c21e | 4004 | |
24d2f903 | 4005 | blk_mq_init_cpu_queues(q, set->nr_hw_queues); |
0d2602ca | 4006 | blk_mq_add_queue_tag_set(set, q); |
4b855ad3 | 4007 | blk_mq_map_swqueue(q); |
26a9750a | 4008 | return 0; |
18741986 | 4009 | |
320ae51f | 4010 | err_hctxs: |
868f2f0b | 4011 | kfree(q->queue_hw_ctx); |
73d9c8d4 | 4012 | q->nr_hw_queues = 0; |
1db4909e | 4013 | blk_mq_sysfs_deinit(q); |
41de54c6 JS |
4014 | err_poll: |
4015 | blk_stat_free_callback(q->poll_cb); | |
4016 | q->poll_cb = NULL; | |
c7de5726 ML |
4017 | err_exit: |
4018 | q->mq_ops = NULL; | |
26a9750a | 4019 | return -ENOMEM; |
320ae51f | 4020 | } |
b62c21b7 | 4021 | EXPORT_SYMBOL(blk_mq_init_allocated_queue); |
320ae51f | 4022 | |
c7e2d94b ML |
4023 | /* tags can _not_ be used after returning from blk_mq_exit_queue */ |
4024 | void blk_mq_exit_queue(struct request_queue *q) | |
320ae51f | 4025 | { |
630ef623 | 4026 | struct blk_mq_tag_set *set = q->tag_set; |
320ae51f | 4027 | |
630ef623 | 4028 | /* Checks hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED. */ |
624dbe47 | 4029 | blk_mq_exit_hw_queues(q, set, set->nr_hw_queues); |
630ef623 BVA |
4030 | /* May clear BLK_MQ_F_TAG_QUEUE_SHARED in hctx->flags. */ |
4031 | blk_mq_del_queue_tag_set(q); | |
320ae51f | 4032 | } |
320ae51f | 4033 | |
a5164405 JA |
4034 | static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set) |
4035 | { | |
4036 | int i; | |
4037 | ||
079a2e3e JG |
4038 | if (blk_mq_is_shared_tags(set->flags)) { |
4039 | set->shared_tags = blk_mq_alloc_map_and_rqs(set, | |
e155b0c2 JG |
4040 | BLK_MQ_NO_HCTX_IDX, |
4041 | set->queue_depth); | |
079a2e3e | 4042 | if (!set->shared_tags) |
e155b0c2 JG |
4043 | return -ENOMEM; |
4044 | } | |
4045 | ||
8229cca8 | 4046 | for (i = 0; i < set->nr_hw_queues; i++) { |
63064be1 | 4047 | if (!__blk_mq_alloc_map_and_rqs(set, i)) |
a5164405 | 4048 | goto out_unwind; |
8229cca8 XT |
4049 | cond_resched(); |
4050 | } | |
a5164405 JA |
4051 | |
4052 | return 0; | |
4053 | ||
4054 | out_unwind: | |
4055 | while (--i >= 0) | |
e155b0c2 JG |
4056 | __blk_mq_free_map_and_rqs(set, i); |
4057 | ||
079a2e3e JG |
4058 | if (blk_mq_is_shared_tags(set->flags)) { |
4059 | blk_mq_free_map_and_rqs(set, set->shared_tags, | |
e155b0c2 | 4060 | BLK_MQ_NO_HCTX_IDX); |
645db34e | 4061 | } |
a5164405 | 4062 | |
a5164405 JA |
4063 | return -ENOMEM; |
4064 | } | |
4065 | ||
4066 | /* | |
4067 | * Allocate the request maps associated with this tag_set. Note that this | |
4068 | * may reduce the depth asked for, if memory is tight. set->queue_depth | |
4069 | * will be updated to reflect the allocated depth. | |
4070 | */ | |
63064be1 | 4071 | static int blk_mq_alloc_set_map_and_rqs(struct blk_mq_tag_set *set) |
a5164405 JA |
4072 | { |
4073 | unsigned int depth; | |
4074 | int err; | |
4075 | ||
4076 | depth = set->queue_depth; | |
4077 | do { | |
4078 | err = __blk_mq_alloc_rq_maps(set); | |
4079 | if (!err) | |
4080 | break; | |
4081 | ||
4082 | set->queue_depth >>= 1; | |
4083 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) { | |
4084 | err = -ENOMEM; | |
4085 | break; | |
4086 | } | |
4087 | } while (set->queue_depth); | |
4088 | ||
4089 | if (!set->queue_depth || err) { | |
4090 | pr_err("blk-mq: failed to allocate request map\n"); | |
4091 | return -ENOMEM; | |
4092 | } | |
4093 | ||
4094 | if (depth != set->queue_depth) | |
4095 | pr_info("blk-mq: reduced tag depth (%u -> %u)\n", | |
4096 | depth, set->queue_depth); | |
4097 | ||
4098 | return 0; | |
4099 | } | |
4100 | ||
ebe8bddb OS |
4101 | static int blk_mq_update_queue_map(struct blk_mq_tag_set *set) |
4102 | { | |
6e66b493 BVA |
4103 | /* |
4104 | * blk_mq_map_queues() and multiple .map_queues() implementations | |
4105 | * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the | |
4106 | * number of hardware queues. | |
4107 | */ | |
4108 | if (set->nr_maps == 1) | |
4109 | set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues; | |
4110 | ||
59388702 | 4111 | if (set->ops->map_queues && !is_kdump_kernel()) { |
b3c661b1 JA |
4112 | int i; |
4113 | ||
7d4901a9 ML |
4114 | /* |
4115 | * transport .map_queues is usually done in the following | |
4116 | * way: | |
4117 | * | |
4118 | * for (queue = 0; queue < set->nr_hw_queues; queue++) { | |
4119 | * mask = get_cpu_mask(queue) | |
4120 | * for_each_cpu(cpu, mask) | |
b3c661b1 | 4121 | * set->map[x].mq_map[cpu] = queue; |
7d4901a9 ML |
4122 | * } |
4123 | * | |
4124 | * When we need to remap, the table has to be cleared for | |
4125 | * killing stale mapping since one CPU may not be mapped | |
4126 | * to any hw queue. | |
4127 | */ | |
b3c661b1 JA |
4128 | for (i = 0; i < set->nr_maps; i++) |
4129 | blk_mq_clear_mq_map(&set->map[i]); | |
7d4901a9 | 4130 | |
ebe8bddb | 4131 | return set->ops->map_queues(set); |
b3c661b1 JA |
4132 | } else { |
4133 | BUG_ON(set->nr_maps > 1); | |
7d76f856 | 4134 | return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]); |
b3c661b1 | 4135 | } |
ebe8bddb OS |
4136 | } |
4137 | ||
f7e76dbc BVA |
4138 | static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set, |
4139 | int cur_nr_hw_queues, int new_nr_hw_queues) | |
4140 | { | |
4141 | struct blk_mq_tags **new_tags; | |
4142 | ||
4143 | if (cur_nr_hw_queues >= new_nr_hw_queues) | |
4144 | return 0; | |
4145 | ||
4146 | new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *), | |
4147 | GFP_KERNEL, set->numa_node); | |
4148 | if (!new_tags) | |
4149 | return -ENOMEM; | |
4150 | ||
4151 | if (set->tags) | |
4152 | memcpy(new_tags, set->tags, cur_nr_hw_queues * | |
4153 | sizeof(*set->tags)); | |
4154 | kfree(set->tags); | |
4155 | set->tags = new_tags; | |
4156 | set->nr_hw_queues = new_nr_hw_queues; | |
4157 | ||
4158 | return 0; | |
4159 | } | |
4160 | ||
91cdf265 MI |
4161 | static int blk_mq_alloc_tag_set_tags(struct blk_mq_tag_set *set, |
4162 | int new_nr_hw_queues) | |
4163 | { | |
4164 | return blk_mq_realloc_tag_set_tags(set, 0, new_nr_hw_queues); | |
4165 | } | |
4166 | ||
a4391c64 JA |
4167 | /* |
4168 | * Alloc a tag set to be associated with one or more request queues. | |
4169 | * May fail with EINVAL for various error conditions. May adjust the | |
c018c84f | 4170 | * requested depth down, if it's too large. In that case, the set |
a4391c64 JA |
4171 | * value will be stored in set->queue_depth. |
4172 | */ | |
24d2f903 CH |
4173 | int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) |
4174 | { | |
b3c661b1 | 4175 | int i, ret; |
da695ba2 | 4176 | |
205fb5f5 BVA |
4177 | BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS); |
4178 | ||
24d2f903 CH |
4179 | if (!set->nr_hw_queues) |
4180 | return -EINVAL; | |
a4391c64 | 4181 | if (!set->queue_depth) |
24d2f903 CH |
4182 | return -EINVAL; |
4183 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) | |
4184 | return -EINVAL; | |
4185 | ||
7d7e0f90 | 4186 | if (!set->ops->queue_rq) |
24d2f903 CH |
4187 | return -EINVAL; |
4188 | ||
de148297 ML |
4189 | if (!set->ops->get_budget ^ !set->ops->put_budget) |
4190 | return -EINVAL; | |
4191 | ||
a4391c64 JA |
4192 | if (set->queue_depth > BLK_MQ_MAX_DEPTH) { |
4193 | pr_info("blk-mq: reduced tag depth to %u\n", | |
4194 | BLK_MQ_MAX_DEPTH); | |
4195 | set->queue_depth = BLK_MQ_MAX_DEPTH; | |
4196 | } | |
24d2f903 | 4197 | |
b3c661b1 JA |
4198 | if (!set->nr_maps) |
4199 | set->nr_maps = 1; | |
4200 | else if (set->nr_maps > HCTX_MAX_TYPES) | |
4201 | return -EINVAL; | |
4202 | ||
6637fadf SL |
4203 | /* |
4204 | * If a crashdump is active, then we are potentially in a very | |
4205 | * memory constrained environment. Limit us to 1 queue and | |
4206 | * 64 tags to prevent using too much memory. | |
4207 | */ | |
4208 | if (is_kdump_kernel()) { | |
4209 | set->nr_hw_queues = 1; | |
59388702 | 4210 | set->nr_maps = 1; |
6637fadf SL |
4211 | set->queue_depth = min(64U, set->queue_depth); |
4212 | } | |
868f2f0b | 4213 | /* |
392546ae JA |
4214 | * There is no use for more h/w queues than cpus if we just have |
4215 | * a single map | |
868f2f0b | 4216 | */ |
392546ae | 4217 | if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids) |
868f2f0b | 4218 | set->nr_hw_queues = nr_cpu_ids; |
6637fadf | 4219 | |
91cdf265 | 4220 | if (blk_mq_alloc_tag_set_tags(set, set->nr_hw_queues) < 0) |
a5164405 | 4221 | return -ENOMEM; |
24d2f903 | 4222 | |
da695ba2 | 4223 | ret = -ENOMEM; |
b3c661b1 JA |
4224 | for (i = 0; i < set->nr_maps; i++) { |
4225 | set->map[i].mq_map = kcalloc_node(nr_cpu_ids, | |
07b35eb5 | 4226 | sizeof(set->map[i].mq_map[0]), |
b3c661b1 JA |
4227 | GFP_KERNEL, set->numa_node); |
4228 | if (!set->map[i].mq_map) | |
4229 | goto out_free_mq_map; | |
59388702 | 4230 | set->map[i].nr_queues = is_kdump_kernel() ? 1 : set->nr_hw_queues; |
b3c661b1 | 4231 | } |
bdd17e75 | 4232 | |
ebe8bddb | 4233 | ret = blk_mq_update_queue_map(set); |
da695ba2 CH |
4234 | if (ret) |
4235 | goto out_free_mq_map; | |
4236 | ||
63064be1 | 4237 | ret = blk_mq_alloc_set_map_and_rqs(set); |
da695ba2 | 4238 | if (ret) |
bdd17e75 | 4239 | goto out_free_mq_map; |
24d2f903 | 4240 | |
0d2602ca JA |
4241 | mutex_init(&set->tag_list_lock); |
4242 | INIT_LIST_HEAD(&set->tag_list); | |
4243 | ||
24d2f903 | 4244 | return 0; |
bdd17e75 CH |
4245 | |
4246 | out_free_mq_map: | |
b3c661b1 JA |
4247 | for (i = 0; i < set->nr_maps; i++) { |
4248 | kfree(set->map[i].mq_map); | |
4249 | set->map[i].mq_map = NULL; | |
4250 | } | |
5676e7b6 RE |
4251 | kfree(set->tags); |
4252 | set->tags = NULL; | |
da695ba2 | 4253 | return ret; |
24d2f903 CH |
4254 | } |
4255 | EXPORT_SYMBOL(blk_mq_alloc_tag_set); | |
4256 | ||
cdb14e0f CH |
4257 | /* allocate and initialize a tagset for a simple single-queue device */ |
4258 | int blk_mq_alloc_sq_tag_set(struct blk_mq_tag_set *set, | |
4259 | const struct blk_mq_ops *ops, unsigned int queue_depth, | |
4260 | unsigned int set_flags) | |
4261 | { | |
4262 | memset(set, 0, sizeof(*set)); | |
4263 | set->ops = ops; | |
4264 | set->nr_hw_queues = 1; | |
4265 | set->nr_maps = 1; | |
4266 | set->queue_depth = queue_depth; | |
4267 | set->numa_node = NUMA_NO_NODE; | |
4268 | set->flags = set_flags; | |
4269 | return blk_mq_alloc_tag_set(set); | |
4270 | } | |
4271 | EXPORT_SYMBOL_GPL(blk_mq_alloc_sq_tag_set); | |
4272 | ||
24d2f903 CH |
4273 | void blk_mq_free_tag_set(struct blk_mq_tag_set *set) |
4274 | { | |
b3c661b1 | 4275 | int i, j; |
24d2f903 | 4276 | |
f7e76dbc | 4277 | for (i = 0; i < set->nr_hw_queues; i++) |
e155b0c2 | 4278 | __blk_mq_free_map_and_rqs(set, i); |
484b4061 | 4279 | |
079a2e3e JG |
4280 | if (blk_mq_is_shared_tags(set->flags)) { |
4281 | blk_mq_free_map_and_rqs(set, set->shared_tags, | |
e155b0c2 JG |
4282 | BLK_MQ_NO_HCTX_IDX); |
4283 | } | |
32bc15af | 4284 | |
b3c661b1 JA |
4285 | for (j = 0; j < set->nr_maps; j++) { |
4286 | kfree(set->map[j].mq_map); | |
4287 | set->map[j].mq_map = NULL; | |
4288 | } | |
bdd17e75 | 4289 | |
981bd189 | 4290 | kfree(set->tags); |
5676e7b6 | 4291 | set->tags = NULL; |
24d2f903 CH |
4292 | } |
4293 | EXPORT_SYMBOL(blk_mq_free_tag_set); | |
4294 | ||
e3a2b3f9 JA |
4295 | int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) |
4296 | { | |
4297 | struct blk_mq_tag_set *set = q->tag_set; | |
4298 | struct blk_mq_hw_ctx *hctx; | |
4299 | int i, ret; | |
4300 | ||
bd166ef1 | 4301 | if (!set) |
e3a2b3f9 JA |
4302 | return -EINVAL; |
4303 | ||
e5fa8140 AZ |
4304 | if (q->nr_requests == nr) |
4305 | return 0; | |
4306 | ||
70f36b60 | 4307 | blk_mq_freeze_queue(q); |
24f5a90f | 4308 | blk_mq_quiesce_queue(q); |
70f36b60 | 4309 | |
e3a2b3f9 JA |
4310 | ret = 0; |
4311 | queue_for_each_hw_ctx(q, hctx, i) { | |
e9137d4b KB |
4312 | if (!hctx->tags) |
4313 | continue; | |
bd166ef1 JA |
4314 | /* |
4315 | * If we're using an MQ scheduler, just update the scheduler | |
4316 | * queue depth. This is similar to what the old code would do. | |
4317 | */ | |
f6adcef5 | 4318 | if (hctx->sched_tags) { |
70f36b60 | 4319 | ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags, |
f6adcef5 | 4320 | nr, true); |
f6adcef5 JG |
4321 | } else { |
4322 | ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr, | |
4323 | false); | |
70f36b60 | 4324 | } |
e3a2b3f9 JA |
4325 | if (ret) |
4326 | break; | |
77f1e0a5 JA |
4327 | if (q->elevator && q->elevator->type->ops.depth_updated) |
4328 | q->elevator->type->ops.depth_updated(hctx); | |
e3a2b3f9 | 4329 | } |
d97e594c | 4330 | if (!ret) { |
e3a2b3f9 | 4331 | q->nr_requests = nr; |
079a2e3e | 4332 | if (blk_mq_is_shared_tags(set->flags)) { |
8fa04464 | 4333 | if (q->elevator) |
079a2e3e | 4334 | blk_mq_tag_update_sched_shared_tags(q); |
8fa04464 | 4335 | else |
079a2e3e | 4336 | blk_mq_tag_resize_shared_tags(set, nr); |
8fa04464 | 4337 | } |
d97e594c | 4338 | } |
e3a2b3f9 | 4339 | |
24f5a90f | 4340 | blk_mq_unquiesce_queue(q); |
70f36b60 | 4341 | blk_mq_unfreeze_queue(q); |
70f36b60 | 4342 | |
e3a2b3f9 JA |
4343 | return ret; |
4344 | } | |
4345 | ||
d48ece20 JW |
4346 | /* |
4347 | * request_queue and elevator_type pair. | |
4348 | * It is just used by __blk_mq_update_nr_hw_queues to cache | |
4349 | * the elevator_type associated with a request_queue. | |
4350 | */ | |
4351 | struct blk_mq_qe_pair { | |
4352 | struct list_head node; | |
4353 | struct request_queue *q; | |
4354 | struct elevator_type *type; | |
4355 | }; | |
4356 | ||
4357 | /* | |
4358 | * Cache the elevator_type in qe pair list and switch the | |
4359 | * io scheduler to 'none' | |
4360 | */ | |
4361 | static bool blk_mq_elv_switch_none(struct list_head *head, | |
4362 | struct request_queue *q) | |
4363 | { | |
4364 | struct blk_mq_qe_pair *qe; | |
4365 | ||
4366 | if (!q->elevator) | |
4367 | return true; | |
4368 | ||
4369 | qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY); | |
4370 | if (!qe) | |
4371 | return false; | |
4372 | ||
4373 | INIT_LIST_HEAD(&qe->node); | |
4374 | qe->q = q; | |
4375 | qe->type = q->elevator->type; | |
4376 | list_add(&qe->node, head); | |
4377 | ||
4378 | mutex_lock(&q->sysfs_lock); | |
4379 | /* | |
4380 | * After elevator_switch_mq, the previous elevator_queue will be | |
4381 | * released by elevator_release. The reference of the io scheduler | |
4382 | * module get by elevator_get will also be put. So we need to get | |
4383 | * a reference of the io scheduler module here to prevent it to be | |
4384 | * removed. | |
4385 | */ | |
4386 | __module_get(qe->type->elevator_owner); | |
4387 | elevator_switch_mq(q, NULL); | |
4388 | mutex_unlock(&q->sysfs_lock); | |
4389 | ||
4390 | return true; | |
4391 | } | |
4392 | ||
4393 | static void blk_mq_elv_switch_back(struct list_head *head, | |
4394 | struct request_queue *q) | |
4395 | { | |
4396 | struct blk_mq_qe_pair *qe; | |
4397 | struct elevator_type *t = NULL; | |
4398 | ||
4399 | list_for_each_entry(qe, head, node) | |
4400 | if (qe->q == q) { | |
4401 | t = qe->type; | |
4402 | break; | |
4403 | } | |
4404 | ||
4405 | if (!t) | |
4406 | return; | |
4407 | ||
4408 | list_del(&qe->node); | |
4409 | kfree(qe); | |
4410 | ||
4411 | mutex_lock(&q->sysfs_lock); | |
4412 | elevator_switch_mq(q, t); | |
4413 | mutex_unlock(&q->sysfs_lock); | |
4414 | } | |
4415 | ||
e4dc2b32 KB |
4416 | static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, |
4417 | int nr_hw_queues) | |
868f2f0b KB |
4418 | { |
4419 | struct request_queue *q; | |
d48ece20 | 4420 | LIST_HEAD(head); |
e01ad46d | 4421 | int prev_nr_hw_queues; |
868f2f0b | 4422 | |
705cda97 BVA |
4423 | lockdep_assert_held(&set->tag_list_lock); |
4424 | ||
392546ae | 4425 | if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids) |
868f2f0b | 4426 | nr_hw_queues = nr_cpu_ids; |
fe35ec58 WZ |
4427 | if (nr_hw_queues < 1) |
4428 | return; | |
4429 | if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues) | |
868f2f0b KB |
4430 | return; |
4431 | ||
4432 | list_for_each_entry(q, &set->tag_list, tag_set_list) | |
4433 | blk_mq_freeze_queue(q); | |
d48ece20 JW |
4434 | /* |
4435 | * Switch IO scheduler to 'none', cleaning up the data associated | |
4436 | * with the previous scheduler. We will switch back once we are done | |
4437 | * updating the new sw to hw queue mappings. | |
4438 | */ | |
4439 | list_for_each_entry(q, &set->tag_list, tag_set_list) | |
4440 | if (!blk_mq_elv_switch_none(&head, q)) | |
4441 | goto switch_back; | |
868f2f0b | 4442 | |
477e19de JW |
4443 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
4444 | blk_mq_debugfs_unregister_hctxs(q); | |
4445 | blk_mq_sysfs_unregister(q); | |
4446 | } | |
4447 | ||
a2584e43 | 4448 | prev_nr_hw_queues = set->nr_hw_queues; |
f7e76dbc BVA |
4449 | if (blk_mq_realloc_tag_set_tags(set, set->nr_hw_queues, nr_hw_queues) < |
4450 | 0) | |
4451 | goto reregister; | |
4452 | ||
868f2f0b | 4453 | set->nr_hw_queues = nr_hw_queues; |
e01ad46d | 4454 | fallback: |
aa880ad6 | 4455 | blk_mq_update_queue_map(set); |
868f2f0b KB |
4456 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
4457 | blk_mq_realloc_hw_ctxs(set, q); | |
e01ad46d | 4458 | if (q->nr_hw_queues != set->nr_hw_queues) { |
a846a8e6 YB |
4459 | int i = prev_nr_hw_queues; |
4460 | ||
e01ad46d JW |
4461 | pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n", |
4462 | nr_hw_queues, prev_nr_hw_queues); | |
a846a8e6 YB |
4463 | for (; i < set->nr_hw_queues; i++) |
4464 | __blk_mq_free_map_and_rqs(set, i); | |
4465 | ||
e01ad46d | 4466 | set->nr_hw_queues = prev_nr_hw_queues; |
7d76f856 | 4467 | blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]); |
e01ad46d JW |
4468 | goto fallback; |
4469 | } | |
477e19de JW |
4470 | blk_mq_map_swqueue(q); |
4471 | } | |
4472 | ||
f7e76dbc | 4473 | reregister: |
477e19de JW |
4474 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
4475 | blk_mq_sysfs_register(q); | |
4476 | blk_mq_debugfs_register_hctxs(q); | |
868f2f0b KB |
4477 | } |
4478 | ||
d48ece20 JW |
4479 | switch_back: |
4480 | list_for_each_entry(q, &set->tag_list, tag_set_list) | |
4481 | blk_mq_elv_switch_back(&head, q); | |
4482 | ||
868f2f0b KB |
4483 | list_for_each_entry(q, &set->tag_list, tag_set_list) |
4484 | blk_mq_unfreeze_queue(q); | |
4485 | } | |
e4dc2b32 KB |
4486 | |
4487 | void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues) | |
4488 | { | |
4489 | mutex_lock(&set->tag_list_lock); | |
4490 | __blk_mq_update_nr_hw_queues(set, nr_hw_queues); | |
4491 | mutex_unlock(&set->tag_list_lock); | |
4492 | } | |
868f2f0b KB |
4493 | EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues); |
4494 | ||
34dbad5d OS |
4495 | /* Enable polling stats and return whether they were already enabled. */ |
4496 | static bool blk_poll_stats_enable(struct request_queue *q) | |
4497 | { | |
48b5c1fb | 4498 | if (q->poll_stat) |
34dbad5d | 4499 | return true; |
48b5c1fb JA |
4500 | |
4501 | return blk_stats_alloc_enable(q); | |
34dbad5d OS |
4502 | } |
4503 | ||
4504 | static void blk_mq_poll_stats_start(struct request_queue *q) | |
4505 | { | |
4506 | /* | |
4507 | * We don't arm the callback if polling stats are not enabled or the | |
4508 | * callback is already active. | |
4509 | */ | |
48b5c1fb | 4510 | if (!q->poll_stat || blk_stat_is_active(q->poll_cb)) |
34dbad5d OS |
4511 | return; |
4512 | ||
4513 | blk_stat_activate_msecs(q->poll_cb, 100); | |
4514 | } | |
4515 | ||
4516 | static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb) | |
4517 | { | |
4518 | struct request_queue *q = cb->data; | |
720b8ccc | 4519 | int bucket; |
34dbad5d | 4520 | |
720b8ccc SB |
4521 | for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) { |
4522 | if (cb->stat[bucket].nr_samples) | |
4523 | q->poll_stat[bucket] = cb->stat[bucket]; | |
4524 | } | |
34dbad5d OS |
4525 | } |
4526 | ||
64f1c21e | 4527 | static unsigned long blk_mq_poll_nsecs(struct request_queue *q, |
64f1c21e JA |
4528 | struct request *rq) |
4529 | { | |
64f1c21e | 4530 | unsigned long ret = 0; |
720b8ccc | 4531 | int bucket; |
64f1c21e JA |
4532 | |
4533 | /* | |
4534 | * If stats collection isn't on, don't sleep but turn it on for | |
4535 | * future users | |
4536 | */ | |
34dbad5d | 4537 | if (!blk_poll_stats_enable(q)) |
64f1c21e JA |
4538 | return 0; |
4539 | ||
64f1c21e JA |
4540 | /* |
4541 | * As an optimistic guess, use half of the mean service time | |
4542 | * for this type of request. We can (and should) make this smarter. | |
4543 | * For instance, if the completion latencies are tight, we can | |
4544 | * get closer than just half the mean. This is especially | |
4545 | * important on devices where the completion latencies are longer | |
720b8ccc SB |
4546 | * than ~10 usec. We do use the stats for the relevant IO size |
4547 | * if available which does lead to better estimates. | |
64f1c21e | 4548 | */ |
720b8ccc SB |
4549 | bucket = blk_mq_poll_stats_bkt(rq); |
4550 | if (bucket < 0) | |
4551 | return ret; | |
4552 | ||
4553 | if (q->poll_stat[bucket].nr_samples) | |
4554 | ret = (q->poll_stat[bucket].mean + 1) / 2; | |
64f1c21e JA |
4555 | |
4556 | return ret; | |
4557 | } | |
4558 | ||
c6699d6f | 4559 | static bool blk_mq_poll_hybrid(struct request_queue *q, blk_qc_t qc) |
06426adf | 4560 | { |
c6699d6f CH |
4561 | struct blk_mq_hw_ctx *hctx = blk_qc_to_hctx(q, qc); |
4562 | struct request *rq = blk_qc_to_rq(hctx, qc); | |
06426adf JA |
4563 | struct hrtimer_sleeper hs; |
4564 | enum hrtimer_mode mode; | |
64f1c21e | 4565 | unsigned int nsecs; |
06426adf JA |
4566 | ktime_t kt; |
4567 | ||
c6699d6f CH |
4568 | /* |
4569 | * If a request has completed on queue that uses an I/O scheduler, we | |
4570 | * won't get back a request from blk_qc_to_rq. | |
4571 | */ | |
4572 | if (!rq || (rq->rq_flags & RQF_MQ_POLL_SLEPT)) | |
64f1c21e JA |
4573 | return false; |
4574 | ||
4575 | /* | |
1052b8ac | 4576 | * If we get here, hybrid polling is enabled. Hence poll_nsec can be: |
64f1c21e | 4577 | * |
64f1c21e JA |
4578 | * 0: use half of prev avg |
4579 | * >0: use this specific value | |
4580 | */ | |
1052b8ac | 4581 | if (q->poll_nsec > 0) |
64f1c21e JA |
4582 | nsecs = q->poll_nsec; |
4583 | else | |
cae740a0 | 4584 | nsecs = blk_mq_poll_nsecs(q, rq); |
64f1c21e JA |
4585 | |
4586 | if (!nsecs) | |
06426adf JA |
4587 | return false; |
4588 | ||
76a86f9d | 4589 | rq->rq_flags |= RQF_MQ_POLL_SLEPT; |
06426adf JA |
4590 | |
4591 | /* | |
4592 | * This will be replaced with the stats tracking code, using | |
4593 | * 'avg_completion_time / 2' as the pre-sleep target. | |
4594 | */ | |
8b0e1953 | 4595 | kt = nsecs; |
06426adf JA |
4596 | |
4597 | mode = HRTIMER_MODE_REL; | |
dbc1625f | 4598 | hrtimer_init_sleeper_on_stack(&hs, CLOCK_MONOTONIC, mode); |
06426adf JA |
4599 | hrtimer_set_expires(&hs.timer, kt); |
4600 | ||
06426adf | 4601 | do { |
5a61c363 | 4602 | if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE) |
06426adf JA |
4603 | break; |
4604 | set_current_state(TASK_UNINTERRUPTIBLE); | |
9dd8813e | 4605 | hrtimer_sleeper_start_expires(&hs, mode); |
06426adf JA |
4606 | if (hs.task) |
4607 | io_schedule(); | |
4608 | hrtimer_cancel(&hs.timer); | |
4609 | mode = HRTIMER_MODE_ABS; | |
4610 | } while (hs.task && !signal_pending(current)); | |
4611 | ||
4612 | __set_current_state(TASK_RUNNING); | |
4613 | destroy_hrtimer_on_stack(&hs.timer); | |
1052b8ac | 4614 | |
06426adf | 4615 | /* |
c6699d6f CH |
4616 | * If we sleep, have the caller restart the poll loop to reset the |
4617 | * state. Like for the other success return cases, the caller is | |
4618 | * responsible for checking if the IO completed. If the IO isn't | |
4619 | * complete, we'll get called again and will go straight to the busy | |
4620 | * poll loop. | |
06426adf | 4621 | */ |
06426adf JA |
4622 | return true; |
4623 | } | |
06426adf | 4624 | |
c6699d6f | 4625 | static int blk_mq_poll_classic(struct request_queue *q, blk_qc_t cookie, |
5a72e899 | 4626 | struct io_comp_batch *iob, unsigned int flags) |
bbd7bb70 | 4627 | { |
c6699d6f CH |
4628 | struct blk_mq_hw_ctx *hctx = blk_qc_to_hctx(q, cookie); |
4629 | long state = get_current_state(); | |
4630 | int ret; | |
bbd7bb70 | 4631 | |
aa61bec3 | 4632 | do { |
5a72e899 | 4633 | ret = q->mq_ops->poll(hctx, iob); |
bbd7bb70 | 4634 | if (ret > 0) { |
849a3700 | 4635 | __set_current_state(TASK_RUNNING); |
85f4d4b6 | 4636 | return ret; |
bbd7bb70 JA |
4637 | } |
4638 | ||
4639 | if (signal_pending_state(state, current)) | |
849a3700 | 4640 | __set_current_state(TASK_RUNNING); |
b03fbd4f | 4641 | if (task_is_running(current)) |
85f4d4b6 | 4642 | return 1; |
c6699d6f | 4643 | |
ef99b2d3 | 4644 | if (ret < 0 || (flags & BLK_POLL_ONESHOT)) |
bbd7bb70 JA |
4645 | break; |
4646 | cpu_relax(); | |
aa61bec3 | 4647 | } while (!need_resched()); |
bbd7bb70 | 4648 | |
67b4110f | 4649 | __set_current_state(TASK_RUNNING); |
85f4d4b6 | 4650 | return 0; |
bbd7bb70 | 4651 | } |
1052b8ac | 4652 | |
5a72e899 JA |
4653 | int blk_mq_poll(struct request_queue *q, blk_qc_t cookie, struct io_comp_batch *iob, |
4654 | unsigned int flags) | |
1052b8ac | 4655 | { |
d729cf9a CH |
4656 | if (!(flags & BLK_POLL_NOSLEEP) && |
4657 | q->poll_nsec != BLK_MQ_POLL_CLASSIC) { | |
c6699d6f | 4658 | if (blk_mq_poll_hybrid(q, cookie)) |
85f4d4b6 | 4659 | return 1; |
c6699d6f | 4660 | } |
5a72e899 | 4661 | return blk_mq_poll_classic(q, cookie, iob, flags); |
bbd7bb70 JA |
4662 | } |
4663 | ||
9cf2bab6 JA |
4664 | unsigned int blk_mq_rq_cpu(struct request *rq) |
4665 | { | |
4666 | return rq->mq_ctx->cpu; | |
4667 | } | |
4668 | EXPORT_SYMBOL(blk_mq_rq_cpu); | |
4669 | ||
2a19b28f ML |
4670 | void blk_mq_cancel_work_sync(struct request_queue *q) |
4671 | { | |
4672 | if (queue_is_mq(q)) { | |
4673 | struct blk_mq_hw_ctx *hctx; | |
4674 | int i; | |
4675 | ||
4676 | cancel_delayed_work_sync(&q->requeue_work); | |
4677 | ||
4678 | queue_for_each_hw_ctx(q, hctx, i) | |
4679 | cancel_delayed_work_sync(&hctx->run_work); | |
4680 | } | |
4681 | } | |
4682 | ||
320ae51f JA |
4683 | static int __init blk_mq_init(void) |
4684 | { | |
c3077b5d CH |
4685 | int i; |
4686 | ||
4687 | for_each_possible_cpu(i) | |
f9ab4918 | 4688 | init_llist_head(&per_cpu(blk_cpu_done, i)); |
c3077b5d CH |
4689 | open_softirq(BLOCK_SOFTIRQ, blk_done_softirq); |
4690 | ||
4691 | cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD, | |
4692 | "block/softirq:dead", NULL, | |
4693 | blk_softirq_cpu_dead); | |
9467f859 TG |
4694 | cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL, |
4695 | blk_mq_hctx_notify_dead); | |
bf0beec0 ML |
4696 | cpuhp_setup_state_multi(CPUHP_AP_BLK_MQ_ONLINE, "block/mq:online", |
4697 | blk_mq_hctx_notify_online, | |
4698 | blk_mq_hctx_notify_offline); | |
320ae51f JA |
4699 | return 0; |
4700 | } | |
4701 | subsys_initcall(blk_mq_init); |