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