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