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