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