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