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