blob: cc57e2dd9a0bb3cf0f6443a29800f6a76120bc5e [file] [log] [blame]
Christoph Hellwig3dcf60bc2019-04-30 14:42:43 -04001// SPDX-License-Identifier: GPL-2.0
Jens Axboe75bb4622014-05-28 10:15:41 -06002/*
Omar Sandoval88459642016-09-17 08:38:44 -06003 * Tag allocation using scalable bitmaps. Uses active queue tracking to support
4 * fairer distribution of tags between multiple submitters when a shared tag map
5 * is used.
Jens Axboe75bb4622014-05-28 10:15:41 -06006 *
7 * Copyright (C) 2013-2014 Jens Axboe
8 */
Jens Axboe320ae512013-10-24 09:20:05 +01009#include <linux/kernel.h>
10#include <linux/module.h>
Jens Axboe320ae512013-10-24 09:20:05 +010011
Ming Leif9934a82019-07-24 11:48:40 +080012#include <linux/delay.h>
Jens Axboe320ae512013-10-24 09:20:05 +010013#include "blk.h"
14#include "blk-mq.h"
John Garryd97e5942021-05-13 20:00:58 +080015#include "blk-mq-sched.h"
Jens Axboe320ae512013-10-24 09:20:05 +010016
Jens Axboe0d2602c2014-05-13 15:10:52 -060017/*
Laibin Qiu180dccb2022-01-13 10:55:36 +080018 * Recalculate wakeup batch when tag is shared by hctx.
19 */
20static void blk_mq_update_wake_batch(struct blk_mq_tags *tags,
21 unsigned int users)
22{
23 if (!users)
24 return;
25
26 sbitmap_queue_recalculate_wake_batch(&tags->bitmap_tags,
27 users);
28 sbitmap_queue_recalculate_wake_batch(&tags->breserved_tags,
29 users);
30}
31
32/*
Jens Axboe0d2602c2014-05-13 15:10:52 -060033 * If a previously inactive queue goes active, bump the active user count.
Jianchao Wangd263ed92018-08-09 08:34:17 -060034 * We need to do this before try to allocate driver tag, then even if fail
35 * to get tag when first time, the other shared-tag users could reserve
36 * budget for it.
Jens Axboe0d2602c2014-05-13 15:10:52 -060037 */
Liu Songee78ec12022-06-25 23:15:21 +080038void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
Jens Axboe0d2602c2014-05-13 15:10:52 -060039{
Laibin Qiu180dccb2022-01-13 10:55:36 +080040 unsigned int users;
Yu Kuai4f1731df2023-06-10 10:30:43 +080041 struct blk_mq_tags *tags = hctx->tags;
Laibin Qiu180dccb2022-01-13 10:55:36 +080042
Tian Lan3e94d542023-05-22 17:05:55 -040043 /*
44 * calling test_bit() prior to test_and_set_bit() is intentional,
45 * it avoids dirtying the cacheline if the queue is already active.
46 */
John Garry079a2e32021-10-05 18:23:39 +080047 if (blk_mq_is_shared_tags(hctx->flags)) {
John Garryf1b49fd2020-08-19 23:20:27 +080048 struct request_queue *q = hctx->queue;
John Garryf1b49fd2020-08-19 23:20:27 +080049
Tian Lan3e94d542023-05-22 17:05:55 -040050 if (test_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags) ||
51 test_and_set_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags))
Liu Songee78ec12022-06-25 23:15:21 +080052 return;
John Garryf1b49fd2020-08-19 23:20:27 +080053 } else {
Tian Lan3e94d542023-05-22 17:05:55 -040054 if (test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state) ||
55 test_and_set_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
Liu Songee78ec12022-06-25 23:15:21 +080056 return;
John Garryf1b49fd2020-08-19 23:20:27 +080057 }
Jens Axboe0d2602c2014-05-13 15:10:52 -060058
Yu Kuai4f1731df2023-06-10 10:30:43 +080059 spin_lock_irq(&tags->lock);
60 users = tags->active_queues + 1;
61 WRITE_ONCE(tags->active_queues, users);
62 blk_mq_update_wake_batch(tags, users);
63 spin_unlock_irq(&tags->lock);
Jens Axboe0d2602c2014-05-13 15:10:52 -060064}
65
66/*
Jens Axboeaed3ea92014-12-22 14:04:42 -070067 * Wakeup all potentially sleeping on tags
Jens Axboe0d2602c2014-05-13 15:10:52 -060068 */
Jens Axboeaed3ea92014-12-22 14:04:42 -070069void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool include_reserve)
Jens Axboe0d2602c2014-05-13 15:10:52 -060070{
John Garryae0f1a72021-10-05 18:23:38 +080071 sbitmap_queue_wake_all(&tags->bitmap_tags);
Omar Sandoval88459642016-09-17 08:38:44 -060072 if (include_reserve)
John Garryae0f1a72021-10-05 18:23:38 +080073 sbitmap_queue_wake_all(&tags->breserved_tags);
Jens Axboe0d2602c2014-05-13 15:10:52 -060074}
75
76/*
Jens Axboee3a2b3f2014-05-20 11:49:02 -060077 * If a previously busy queue goes inactive, potential waiters could now
78 * be allowed to queue. Wake them up and check.
79 */
80void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
81{
82 struct blk_mq_tags *tags = hctx->tags;
Laibin Qiu180dccb2022-01-13 10:55:36 +080083 unsigned int users;
Jens Axboee3a2b3f2014-05-20 11:49:02 -060084
John Garry079a2e32021-10-05 18:23:39 +080085 if (blk_mq_is_shared_tags(hctx->flags)) {
John Garrye155b0c2021-10-05 18:23:37 +080086 struct request_queue *q = hctx->queue;
87
John Garryf1b49fd2020-08-19 23:20:27 +080088 if (!test_and_clear_bit(QUEUE_FLAG_HCTX_ACTIVE,
89 &q->queue_flags))
90 return;
John Garryf1b49fd2020-08-19 23:20:27 +080091 } else {
92 if (!test_and_clear_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
93 return;
John Garryf1b49fd2020-08-19 23:20:27 +080094 }
Jens Axboee3a2b3f2014-05-20 11:49:02 -060095
Yu Kuai4f1731df2023-06-10 10:30:43 +080096 spin_lock_irq(&tags->lock);
97 users = tags->active_queues - 1;
98 WRITE_ONCE(tags->active_queues, users);
Laibin Qiu180dccb2022-01-13 10:55:36 +080099 blk_mq_update_wake_batch(tags, users);
Yu Kuai4f1731df2023-06-10 10:30:43 +0800100 spin_unlock_irq(&tags->lock);
John Garry079a2e32021-10-05 18:23:39 +0800101
Jens Axboeaed3ea92014-12-22 14:04:42 -0700102 blk_mq_tag_wakeup_all(tags, false);
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600103}
104
Jens Axboe200e86b2017-01-25 08:11:38 -0700105static int __blk_mq_get_tag(struct blk_mq_alloc_data *data,
106 struct sbitmap_queue *bt)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600107{
Ming Lei28500852020-09-11 18:41:14 +0800108 if (!data->q->elevator && !(data->flags & BLK_MQ_REQ_RESERVED) &&
109 !hctx_may_queue(data->hctx, bt))
Christoph Hellwig766473682020-05-29 15:53:12 +0200110 return BLK_MQ_NO_TAG;
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200111
Omar Sandoval229a92872017-04-14 00:59:59 -0700112 if (data->shallow_depth)
John Garry3f607292022-02-08 20:07:04 +0800113 return sbitmap_queue_get_shallow(bt, data->shallow_depth);
Omar Sandoval229a92872017-04-14 00:59:59 -0700114 else
115 return __sbitmap_queue_get(bt);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600116}
117
Jens Axboe349302d2021-10-09 13:10:39 -0600118unsigned long blk_mq_get_tags(struct blk_mq_alloc_data *data, int nr_tags,
119 unsigned int *offset)
120{
121 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
122 struct sbitmap_queue *bt = &tags->bitmap_tags;
123 unsigned long ret;
124
125 if (data->shallow_depth ||data->flags & BLK_MQ_REQ_RESERVED ||
126 data->hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
127 return 0;
128 ret = __sbitmap_queue_get_batch(bt, nr_tags, offset);
129 *offset += tags->nr_reserved_tags;
130 return ret;
131}
132
Jens Axboe49411152017-01-13 08:09:05 -0700133unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600134{
Jens Axboe49411152017-01-13 08:09:05 -0700135 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
136 struct sbitmap_queue *bt;
Omar Sandoval88459642016-09-17 08:38:44 -0600137 struct sbq_wait_state *ws;
Jens Axboe5d2ee712018-11-29 17:36:41 -0700138 DEFINE_SBQ_WAIT(wait);
Jens Axboe49411152017-01-13 08:09:05 -0700139 unsigned int tag_offset;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600140 int tag;
141
Jens Axboe49411152017-01-13 08:09:05 -0700142 if (data->flags & BLK_MQ_REQ_RESERVED) {
143 if (unlikely(!tags->nr_reserved_tags)) {
144 WARN_ON_ONCE(1);
Christoph Hellwig419c3d52020-05-29 15:53:11 +0200145 return BLK_MQ_NO_TAG;
Jens Axboe49411152017-01-13 08:09:05 -0700146 }
John Garryae0f1a72021-10-05 18:23:38 +0800147 bt = &tags->breserved_tags;
Jens Axboe49411152017-01-13 08:09:05 -0700148 tag_offset = 0;
149 } else {
John Garryae0f1a72021-10-05 18:23:38 +0800150 bt = &tags->bitmap_tags;
Jens Axboe49411152017-01-13 08:09:05 -0700151 tag_offset = tags->nr_reserved_tags;
152 }
153
Jens Axboe200e86b2017-01-25 08:11:38 -0700154 tag = __blk_mq_get_tag(data, bt);
Christoph Hellwig766473682020-05-29 15:53:12 +0200155 if (tag != BLK_MQ_NO_TAG)
Jens Axboe49411152017-01-13 08:09:05 -0700156 goto found_tag;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600157
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100158 if (data->flags & BLK_MQ_REQ_NOWAIT)
Christoph Hellwig419c3d52020-05-29 15:53:11 +0200159 return BLK_MQ_NO_TAG;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600160
Jens Axboe49411152017-01-13 08:09:05 -0700161 ws = bt_wait_ptr(bt, data->hctx);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600162 do {
Ming Leie6fc4642018-05-24 11:00:39 -0600163 struct sbitmap_queue *bt_prev;
164
Bart Van Asscheb3223202014-12-08 08:46:34 -0700165 /*
166 * We're out of tags on this hardware queue, kick any
167 * pending IO submits before going to sleep waiting for
Jens Axboe8cecb072017-01-19 07:39:17 -0700168 * some to complete.
Bart Van Asscheb3223202014-12-08 08:46:34 -0700169 */
Jens Axboe8cecb072017-01-19 07:39:17 -0700170 blk_mq_run_hw_queue(data->hctx, false);
Bart Van Asscheb3223202014-12-08 08:46:34 -0700171
Jens Axboe080ff352014-12-08 08:49:06 -0700172 /*
173 * Retry tag allocation after running the hardware queue,
174 * as running the queue may also have found completions.
175 */
Jens Axboe200e86b2017-01-25 08:11:38 -0700176 tag = __blk_mq_get_tag(data, bt);
Christoph Hellwig766473682020-05-29 15:53:12 +0200177 if (tag != BLK_MQ_NO_TAG)
Jens Axboe080ff352014-12-08 08:49:06 -0700178 break;
179
Jens Axboe5d2ee712018-11-29 17:36:41 -0700180 sbitmap_prepare_to_wait(bt, ws, &wait, TASK_UNINTERRUPTIBLE);
Jens Axboe4e5dff42017-11-14 10:24:58 -0700181
182 tag = __blk_mq_get_tag(data, bt);
Christoph Hellwig766473682020-05-29 15:53:12 +0200183 if (tag != BLK_MQ_NO_TAG)
Jens Axboe4e5dff42017-11-14 10:24:58 -0700184 break;
185
Ming Leie6fc4642018-05-24 11:00:39 -0600186 bt_prev = bt;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600187 io_schedule();
Ming Leicb96a42c2014-06-01 00:43:37 +0800188
Jens Axboe5d2ee712018-11-29 17:36:41 -0700189 sbitmap_finish_wait(bt, ws, &wait);
190
Ming Leicb96a42c2014-06-01 00:43:37 +0800191 data->ctx = blk_mq_get_ctx(data->q);
Jens Axboef9afca42018-10-29 13:11:38 -0600192 data->hctx = blk_mq_map_queue(data->q, data->cmd_flags,
Jianchao Wang8ccdf4a2019-01-24 18:25:32 +0800193 data->ctx);
Jens Axboe49411152017-01-13 08:09:05 -0700194 tags = blk_mq_tags_from_data(data);
195 if (data->flags & BLK_MQ_REQ_RESERVED)
John Garryae0f1a72021-10-05 18:23:38 +0800196 bt = &tags->breserved_tags;
Jens Axboe49411152017-01-13 08:09:05 -0700197 else
John Garryae0f1a72021-10-05 18:23:38 +0800198 bt = &tags->bitmap_tags;
Jens Axboe49411152017-01-13 08:09:05 -0700199
Ming Leie6fc4642018-05-24 11:00:39 -0600200 /*
201 * If destination hw queue is changed, fake wake up on
202 * previous queue for compensating the wake up miss, so
203 * other allocations on previous queue won't be starved.
204 */
205 if (bt != bt_prev)
Keith Busch4acb8342022-09-09 11:40:22 -0700206 sbitmap_queue_wake_up(bt_prev, 1);
Ming Leie6fc4642018-05-24 11:00:39 -0600207
Jens Axboe49411152017-01-13 08:09:05 -0700208 ws = bt_wait_ptr(bt, data->hctx);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600209 } while (1);
210
Jens Axboe5d2ee712018-11-29 17:36:41 -0700211 sbitmap_finish_wait(bt, ws, &wait);
Jens Axboe49411152017-01-13 08:09:05 -0700212
213found_tag:
Ming Leibf0beec2020-05-29 15:53:15 +0200214 /*
215 * Give up this allocation if the hctx is inactive. The caller will
216 * retry on an active hctx.
217 */
218 if (unlikely(test_bit(BLK_MQ_S_INACTIVE, &data->hctx->state))) {
219 blk_mq_put_tag(tags, data->ctx, tag + tag_offset);
220 return BLK_MQ_NO_TAG;
221 }
Jens Axboe49411152017-01-13 08:09:05 -0700222 return tag + tag_offset;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600223}
224
John Garrycae740a2020-02-26 20:10:15 +0800225void blk_mq_put_tag(struct blk_mq_tags *tags, struct blk_mq_ctx *ctx,
226 unsigned int tag)
Jens Axboe320ae512013-10-24 09:20:05 +0100227{
Sagi Grimberg415b8062017-02-27 10:04:39 -0700228 if (!blk_mq_tag_is_reserved(tags, tag)) {
Jens Axboe4bb659b2014-05-09 09:36:49 -0600229 const int real_tag = tag - tags->nr_reserved_tags;
230
Jens Axboe70114c32014-11-24 15:52:30 -0700231 BUG_ON(real_tag >= tags->nr_tags);
John Garryae0f1a72021-10-05 18:23:38 +0800232 sbitmap_queue_clear(&tags->bitmap_tags, real_tag, ctx->cpu);
Jens Axboe70114c32014-11-24 15:52:30 -0700233 } else {
John Garryae0f1a72021-10-05 18:23:38 +0800234 sbitmap_queue_clear(&tags->breserved_tags, tag, ctx->cpu);
Jens Axboe70114c32014-11-24 15:52:30 -0700235 }
Jens Axboe320ae512013-10-24 09:20:05 +0100236}
237
Jens Axboef794f332021-10-08 05:50:46 -0600238void blk_mq_put_tags(struct blk_mq_tags *tags, int *tag_array, int nr_tags)
239{
240 sbitmap_queue_clear_batch(&tags->bitmap_tags, tags->nr_reserved_tags,
241 tag_array, nr_tags);
242}
243
Omar Sandoval88459642016-09-17 08:38:44 -0600244struct bt_iter_data {
245 struct blk_mq_hw_ctx *hctx;
John Garryfea9f922021-12-06 20:49:50 +0800246 struct request_queue *q;
John Garryfc39f8d2021-12-06 20:49:49 +0800247 busy_tag_iter_fn *fn;
Omar Sandoval88459642016-09-17 08:38:44 -0600248 void *data;
249 bool reserved;
250};
251
Ming Lei2e315dc2021-05-11 23:22:34 +0800252static struct request *blk_mq_find_and_get_req(struct blk_mq_tags *tags,
253 unsigned int bitnr)
254{
Ming Leibd631412021-05-11 23:22:35 +0800255 struct request *rq;
256 unsigned long flags;
Ming Lei2e315dc2021-05-11 23:22:34 +0800257
Ming Leibd631412021-05-11 23:22:35 +0800258 spin_lock_irqsave(&tags->lock, flags);
259 rq = tags->rqs[bitnr];
Jens Axboe0a467d02021-10-14 14:39:59 -0600260 if (!rq || rq->tag != bitnr || !req_ref_inc_not_zero(rq))
Ming Leibd631412021-05-11 23:22:35 +0800261 rq = NULL;
262 spin_unlock_irqrestore(&tags->lock, flags);
Ming Lei2e315dc2021-05-11 23:22:34 +0800263 return rq;
264}
265
Omar Sandoval88459642016-09-17 08:38:44 -0600266static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100267{
Omar Sandoval88459642016-09-17 08:38:44 -0600268 struct bt_iter_data *iter_data = data;
269 struct blk_mq_hw_ctx *hctx = iter_data->hctx;
John Garryfea9f922021-12-06 20:49:50 +0800270 struct request_queue *q = iter_data->q;
271 struct blk_mq_tag_set *set = q->tag_set;
John Garryfea9f922021-12-06 20:49:50 +0800272 struct blk_mq_tags *tags;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700273 struct request *rq;
Ming Lei2e315dc2021-05-11 23:22:34 +0800274 bool ret = true;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600275
John Garryfea9f922021-12-06 20:49:50 +0800276 if (blk_mq_is_shared_tags(set->flags))
277 tags = set->shared_tags;
278 else
279 tags = hctx->tags;
280
John Garry4cf6e6c2022-07-06 20:03:54 +0800281 if (!iter_data->reserved)
Omar Sandoval88459642016-09-17 08:38:44 -0600282 bitnr += tags->nr_reserved_tags;
Jens Axboe7f5562d2017-08-04 13:37:03 -0600283 /*
284 * We can hit rq == NULL here, because the tagging functions
Bart Van Asschec7b1bf52018-09-21 13:34:46 -0700285 * test and set the bit before assigning ->rqs[].
Jens Axboe7f5562d2017-08-04 13:37:03 -0600286 */
Ming Lei2e315dc2021-05-11 23:22:34 +0800287 rq = blk_mq_find_and_get_req(tags, bitnr);
288 if (!rq)
289 return true;
290
John Garryfea9f922021-12-06 20:49:50 +0800291 if (rq->q == q && (!hctx || rq->mq_hctx == hctx))
John Garry2dd65322022-07-06 20:03:53 +0800292 ret = iter_data->fn(rq, iter_data->data);
Ming Lei2e315dc2021-05-11 23:22:34 +0800293 blk_mq_put_rq_ref(rq);
294 return ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100295}
296
Bart Van Asschec7b1bf52018-09-21 13:34:46 -0700297/**
298 * bt_for_each - iterate over the requests associated with a hardware queue
299 * @hctx: Hardware queue to examine.
John Garryfea9f922021-12-06 20:49:50 +0800300 * @q: Request queue to examine.
Bart Van Asschec7b1bf52018-09-21 13:34:46 -0700301 * @bt: sbitmap to examine. This is either the breserved_tags member
302 * or the bitmap_tags member of struct blk_mq_tags.
303 * @fn: Pointer to the function that will be called for each request
304 * associated with @hctx that has been assigned a driver tag.
305 * @fn will be called as follows: @fn(@hctx, rq, @data, @reserved)
Jens Axboeab11fe52018-11-08 11:09:50 -0700306 * where rq is a pointer to a request. Return true to continue
307 * iterating tags, false to stop.
Bart Van Asschec7b1bf52018-09-21 13:34:46 -0700308 * @data: Will be passed as third argument to @fn.
309 * @reserved: Indicates whether @bt is the breserved_tags member or the
310 * bitmap_tags member of struct blk_mq_tags.
311 */
John Garryfea9f922021-12-06 20:49:50 +0800312static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct request_queue *q,
313 struct sbitmap_queue *bt, busy_tag_iter_fn *fn,
314 void *data, bool reserved)
Keith Buschf26cdc82015-06-01 09:29:53 -0600315{
Omar Sandoval88459642016-09-17 08:38:44 -0600316 struct bt_iter_data iter_data = {
317 .hctx = hctx,
318 .fn = fn,
319 .data = data,
320 .reserved = reserved,
John Garryfea9f922021-12-06 20:49:50 +0800321 .q = q,
Omar Sandoval88459642016-09-17 08:38:44 -0600322 };
323
324 sbitmap_for_each_set(&bt->sb, bt_iter, &iter_data);
325}
326
327struct bt_tags_iter_data {
328 struct blk_mq_tags *tags;
329 busy_tag_iter_fn *fn;
330 void *data;
Ming Lei602380d2020-05-29 15:53:14 +0200331 unsigned int flags;
Omar Sandoval88459642016-09-17 08:38:44 -0600332};
333
Ming Lei602380d2020-05-29 15:53:14 +0200334#define BT_TAG_ITER_RESERVED (1 << 0)
335#define BT_TAG_ITER_STARTED (1 << 1)
Ming Lei22f614b2020-06-05 19:44:10 +0800336#define BT_TAG_ITER_STATIC_RQS (1 << 2)
Ming Lei602380d2020-05-29 15:53:14 +0200337
Omar Sandoval88459642016-09-17 08:38:44 -0600338static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
339{
340 struct bt_tags_iter_data *iter_data = data;
341 struct blk_mq_tags *tags = iter_data->tags;
Keith Buschf26cdc82015-06-01 09:29:53 -0600342 struct request *rq;
Ming Lei2e315dc2021-05-11 23:22:34 +0800343 bool ret = true;
344 bool iter_static_rqs = !!(iter_data->flags & BT_TAG_ITER_STATIC_RQS);
Keith Buschf26cdc82015-06-01 09:29:53 -0600345
John Garry4cf6e6c2022-07-06 20:03:54 +0800346 if (!(iter_data->flags & BT_TAG_ITER_RESERVED))
Omar Sandoval88459642016-09-17 08:38:44 -0600347 bitnr += tags->nr_reserved_tags;
Keith Buschf26cdc82015-06-01 09:29:53 -0600348
Jens Axboe7f5562d2017-08-04 13:37:03 -0600349 /*
350 * We can hit rq == NULL here, because the tagging functions
Ming Lei22f614b2020-06-05 19:44:10 +0800351 * test and set the bit before assigning ->rqs[].
Jens Axboe7f5562d2017-08-04 13:37:03 -0600352 */
Ming Lei2e315dc2021-05-11 23:22:34 +0800353 if (iter_static_rqs)
Ming Lei22f614b2020-06-05 19:44:10 +0800354 rq = tags->static_rqs[bitnr];
355 else
Ming Lei2e315dc2021-05-11 23:22:34 +0800356 rq = blk_mq_find_and_get_req(tags, bitnr);
Ming Lei602380d2020-05-29 15:53:14 +0200357 if (!rq)
358 return true;
Ming Lei2e315dc2021-05-11 23:22:34 +0800359
360 if (!(iter_data->flags & BT_TAG_ITER_STARTED) ||
361 blk_mq_request_started(rq))
John Garry2dd65322022-07-06 20:03:53 +0800362 ret = iter_data->fn(rq, iter_data->data);
Ming Lei2e315dc2021-05-11 23:22:34 +0800363 if (!iter_static_rqs)
364 blk_mq_put_rq_ref(rq);
365 return ret;
Omar Sandoval88459642016-09-17 08:38:44 -0600366}
Keith Buschf26cdc82015-06-01 09:29:53 -0600367
Bart Van Asschec7b1bf52018-09-21 13:34:46 -0700368/**
369 * bt_tags_for_each - iterate over the requests in a tag map
370 * @tags: Tag map to iterate over.
371 * @bt: sbitmap to examine. This is either the breserved_tags member
372 * or the bitmap_tags member of struct blk_mq_tags.
373 * @fn: Pointer to the function that will be called for each started
374 * request. @fn will be called as follows: @fn(rq, @data,
Jens Axboeab11fe52018-11-08 11:09:50 -0700375 * @reserved) where rq is a pointer to a request. Return true
376 * to continue iterating tags, false to stop.
Bart Van Asschec7b1bf52018-09-21 13:34:46 -0700377 * @data: Will be passed as second argument to @fn.
Ming Lei602380d2020-05-29 15:53:14 +0200378 * @flags: BT_TAG_ITER_*
Bart Van Asschec7b1bf52018-09-21 13:34:46 -0700379 */
Omar Sandoval88459642016-09-17 08:38:44 -0600380static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt,
Ming Lei602380d2020-05-29 15:53:14 +0200381 busy_tag_iter_fn *fn, void *data, unsigned int flags)
Omar Sandoval88459642016-09-17 08:38:44 -0600382{
383 struct bt_tags_iter_data iter_data = {
384 .tags = tags,
385 .fn = fn,
386 .data = data,
Ming Lei602380d2020-05-29 15:53:14 +0200387 .flags = flags,
Omar Sandoval88459642016-09-17 08:38:44 -0600388 };
389
390 if (tags->rqs)
391 sbitmap_for_each_set(&bt->sb, bt_tags_iter, &iter_data);
Keith Buschf26cdc82015-06-01 09:29:53 -0600392}
393
Ming Lei602380d2020-05-29 15:53:14 +0200394static void __blk_mq_all_tag_iter(struct blk_mq_tags *tags,
395 busy_tag_iter_fn *fn, void *priv, unsigned int flags)
396{
397 WARN_ON_ONCE(flags & BT_TAG_ITER_RESERVED);
398
399 if (tags->nr_reserved_tags)
John Garryae0f1a72021-10-05 18:23:38 +0800400 bt_tags_for_each(tags, &tags->breserved_tags, fn, priv,
Ming Lei602380d2020-05-29 15:53:14 +0200401 flags | BT_TAG_ITER_RESERVED);
John Garryae0f1a72021-10-05 18:23:38 +0800402 bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, flags);
Ming Lei602380d2020-05-29 15:53:14 +0200403}
404
Bart Van Asschec7b1bf52018-09-21 13:34:46 -0700405/**
Ming Lei602380d2020-05-29 15:53:14 +0200406 * blk_mq_all_tag_iter - iterate over all requests in a tag map
Bart Van Asschec7b1bf52018-09-21 13:34:46 -0700407 * @tags: Tag map to iterate over.
Ming Lei602380d2020-05-29 15:53:14 +0200408 * @fn: Pointer to the function that will be called for each
Bart Van Asschec7b1bf52018-09-21 13:34:46 -0700409 * request. @fn will be called as follows: @fn(rq, @priv,
410 * reserved) where rq is a pointer to a request. 'reserved'
Jens Axboeab11fe52018-11-08 11:09:50 -0700411 * indicates whether or not @rq is a reserved request. Return
412 * true to continue iterating tags, false to stop.
Bart Van Asschec7b1bf52018-09-21 13:34:46 -0700413 * @priv: Will be passed as second argument to @fn.
Ming Lei22f614b2020-06-05 19:44:10 +0800414 *
415 * Caller has to pass the tag map from which requests are allocated.
Bart Van Asschec7b1bf52018-09-21 13:34:46 -0700416 */
Ming Lei602380d2020-05-29 15:53:14 +0200417void blk_mq_all_tag_iter(struct blk_mq_tags *tags, busy_tag_iter_fn *fn,
418 void *priv)
Keith Buschf26cdc82015-06-01 09:29:53 -0600419{
Baolin Wanga8a5e382020-06-15 17:12:23 +0800420 __blk_mq_all_tag_iter(tags, fn, priv, BT_TAG_ITER_STATIC_RQS);
Keith Buschf26cdc82015-06-01 09:29:53 -0600421}
Keith Buschf26cdc82015-06-01 09:29:53 -0600422
Bart Van Asschec7b1bf52018-09-21 13:34:46 -0700423/**
424 * blk_mq_tagset_busy_iter - iterate over all started requests in a tag set
425 * @tagset: Tag set to iterate over.
426 * @fn: Pointer to the function that will be called for each started
427 * request. @fn will be called as follows: @fn(rq, @priv,
428 * reserved) where rq is a pointer to a request. 'reserved'
Jens Axboeab11fe52018-11-08 11:09:50 -0700429 * indicates whether or not @rq is a reserved request. Return
430 * true to continue iterating tags, false to stop.
Bart Van Asschec7b1bf52018-09-21 13:34:46 -0700431 * @priv: Will be passed as second argument to @fn.
Ming Lei2e315dc2021-05-11 23:22:34 +0800432 *
433 * We grab one request reference before calling @fn and release it after
434 * @fn returns.
Bart Van Asschec7b1bf52018-09-21 13:34:46 -0700435 */
Sagi Grimberge0489482016-03-10 13:58:46 +0200436void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
437 busy_tag_iter_fn *fn, void *priv)
438{
John Garry0994c642021-10-18 17:41:23 +0800439 unsigned int flags = tagset->flags;
440 int i, nr_tags;
Sagi Grimberge0489482016-03-10 13:58:46 +0200441
John Garry0994c642021-10-18 17:41:23 +0800442 nr_tags = blk_mq_is_shared_tags(flags) ? 1 : tagset->nr_hw_queues;
443
444 for (i = 0; i < nr_tags; i++) {
Sagi Grimberge0489482016-03-10 13:58:46 +0200445 if (tagset->tags && tagset->tags[i])
Ming Lei602380d2020-05-29 15:53:14 +0200446 __blk_mq_all_tag_iter(tagset->tags[i], fn, priv,
447 BT_TAG_ITER_STARTED);
Sagi Grimberge0489482016-03-10 13:58:46 +0200448 }
449}
450EXPORT_SYMBOL(blk_mq_tagset_busy_iter);
451
John Garry2dd65322022-07-06 20:03:53 +0800452static bool blk_mq_tagset_count_completed_rqs(struct request *rq, void *data)
Ming Leif9934a82019-07-24 11:48:40 +0800453{
454 unsigned *count = data;
455
456 if (blk_mq_request_completed(rq))
457 (*count)++;
458 return true;
459}
460
461/**
Bhaskar Chowdhury9cf1adc2021-03-20 04:22:22 +0530462 * blk_mq_tagset_wait_completed_request - Wait until all scheduled request
463 * completions have finished.
Ming Leif9934a82019-07-24 11:48:40 +0800464 * @tagset: Tag set to drain completed request
465 *
466 * Note: This function has to be run after all IO queues are shutdown
467 */
468void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset)
469{
470 while (true) {
471 unsigned count = 0;
472
473 blk_mq_tagset_busy_iter(tagset,
474 blk_mq_tagset_count_completed_rqs, &count);
475 if (!count)
476 break;
477 msleep(5);
478 }
479}
480EXPORT_SYMBOL(blk_mq_tagset_wait_completed_request);
481
Bart Van Asschec7b1bf52018-09-21 13:34:46 -0700482/**
483 * blk_mq_queue_tag_busy_iter - iterate over all requests with a driver tag
484 * @q: Request queue to examine.
485 * @fn: Pointer to the function that will be called for each request
486 * on @q. @fn will be called as follows: @fn(hctx, rq, @priv,
487 * reserved) where rq is a pointer to a request and hctx points
488 * to the hardware queue associated with the request. 'reserved'
489 * indicates whether or not @rq is a reserved request.
490 * @priv: Will be passed as third argument to @fn.
491 *
492 * Note: if @q->tag_set is shared with other request queues then @fn will be
493 * called for all requests on all queues that share that tag set and not only
494 * for requests associated with @q.
495 */
John Garryfc39f8d2021-12-06 20:49:49 +0800496void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_tag_iter_fn *fn,
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700497 void *priv)
Jens Axboe320ae512013-10-24 09:20:05 +0100498{
Jianchao Wangf5bbbbe2018-08-21 15:15:04 +0800499 /*
Ming Lei4e5cc992022-03-08 15:32:19 +0800500 * __blk_mq_update_nr_hw_queues() updates nr_hw_queues and hctx_table
Bart Van Asschec7b1bf52018-09-21 13:34:46 -0700501 * while the queue is frozen. So we can use q_usage_counter to avoid
yangerkun76cffcc2020-09-19 11:54:25 +0800502 * racing with it.
Jianchao Wangf5bbbbe2018-08-21 15:15:04 +0800503 */
Keith Busch530ca2c2018-09-25 10:36:20 -0600504 if (!percpu_ref_tryget(&q->q_usage_counter))
Jianchao Wangf5bbbbe2018-08-21 15:15:04 +0800505 return;
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200506
John Garryfea9f922021-12-06 20:49:50 +0800507 if (blk_mq_is_shared_tags(q->tag_set->flags)) {
508 struct blk_mq_tags *tags = q->tag_set->shared_tags;
509 struct sbitmap_queue *bresv = &tags->breserved_tags;
510 struct sbitmap_queue *btags = &tags->bitmap_tags;
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200511
512 if (tags->nr_reserved_tags)
John Garryfea9f922021-12-06 20:49:50 +0800513 bt_for_each(NULL, q, bresv, fn, priv, true);
514 bt_for_each(NULL, q, btags, fn, priv, false);
515 } else {
516 struct blk_mq_hw_ctx *hctx;
Ming Lei4f481202022-03-08 15:32:18 +0800517 unsigned long i;
John Garryfea9f922021-12-06 20:49:50 +0800518
519 queue_for_each_hw_ctx(q, hctx, i) {
520 struct blk_mq_tags *tags = hctx->tags;
521 struct sbitmap_queue *bresv = &tags->breserved_tags;
522 struct sbitmap_queue *btags = &tags->bitmap_tags;
523
524 /*
525 * If no software queues are currently mapped to this
526 * hardware queue, there's nothing to check
527 */
528 if (!blk_mq_hw_queue_mapped(hctx))
529 continue;
530
531 if (tags->nr_reserved_tags)
532 bt_for_each(hctx, q, bresv, fn, priv, true);
533 bt_for_each(hctx, q, btags, fn, priv, false);
534 }
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200535 }
Keith Busch530ca2c2018-09-25 10:36:20 -0600536 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100537}
538
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700539static int bt_alloc(struct sbitmap_queue *bt, unsigned int depth,
540 bool round_robin, int node)
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600541{
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700542 return sbitmap_queue_init_node(bt, depth, -1, round_robin, GFP_KERNEL,
543 node);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600544}
545
John Garry56b68082021-05-13 20:00:57 +0800546int blk_mq_init_bitmaps(struct sbitmap_queue *bitmap_tags,
547 struct sbitmap_queue *breserved_tags,
548 unsigned int queue_depth, unsigned int reserved,
549 int node, int alloc_policy)
550{
551 unsigned int depth = queue_depth - reserved;
552 bool round_robin = alloc_policy == BLK_TAG_ALLOC_RR;
553
554 if (bt_alloc(bitmap_tags, depth, round_robin, node))
555 return -ENOMEM;
556 if (bt_alloc(breserved_tags, reserved, round_robin, node))
557 goto free_bitmap_tags;
558
559 return 0;
560
561free_bitmap_tags:
562 sbitmap_queue_free(bitmap_tags);
563 return -ENOMEM;
564}
565
Jens Axboe320ae512013-10-24 09:20:05 +0100566struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
Shaohua Li24391c02015-01-23 14:18:00 -0700567 unsigned int reserved_tags,
John Garrye155b0c2021-10-05 18:23:37 +0800568 int node, int alloc_policy)
Jens Axboe320ae512013-10-24 09:20:05 +0100569{
Jens Axboe320ae512013-10-24 09:20:05 +0100570 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +0100571
572 if (total_tags > BLK_MQ_TAG_MAX) {
573 pr_err("blk-mq: tag depth too large\n");
574 return NULL;
575 }
576
577 tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
578 if (!tags)
579 return NULL;
580
Jens Axboe320ae512013-10-24 09:20:05 +0100581 tags->nr_tags = total_tags;
582 tags->nr_reserved_tags = reserved_tags;
Ming Leibd631412021-05-11 23:22:35 +0800583 spin_lock_init(&tags->lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100584
John Garryae0f1a72021-10-05 18:23:38 +0800585 if (blk_mq_init_bitmaps(&tags->bitmap_tags, &tags->breserved_tags,
586 total_tags, reserved_tags, node,
587 alloc_policy) < 0) {
Hannes Reinecke4d063232020-08-19 23:20:21 +0800588 kfree(tags);
589 return NULL;
590 }
591 return tags;
Jens Axboe320ae512013-10-24 09:20:05 +0100592}
593
John Garrye155b0c2021-10-05 18:23:37 +0800594void blk_mq_free_tags(struct blk_mq_tags *tags)
Jens Axboe320ae512013-10-24 09:20:05 +0100595{
John Garryae0f1a72021-10-05 18:23:38 +0800596 sbitmap_queue_free(&tags->bitmap_tags);
597 sbitmap_queue_free(&tags->breserved_tags);
Jens Axboe320ae512013-10-24 09:20:05 +0100598 kfree(tags);
599}
600
Jens Axboe70f36b62017-01-19 10:59:07 -0700601int blk_mq_tag_update_depth(struct blk_mq_hw_ctx *hctx,
602 struct blk_mq_tags **tagsptr, unsigned int tdepth,
603 bool can_grow)
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600604{
Jens Axboe70f36b62017-01-19 10:59:07 -0700605 struct blk_mq_tags *tags = *tagsptr;
606
607 if (tdepth <= tags->nr_reserved_tags)
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600608 return -EINVAL;
609
Jens Axboe70f36b62017-01-19 10:59:07 -0700610 /*
611 * If we are allowed to grow beyond the original size, allocate
612 * a new set of tags before freeing the old one.
613 */
614 if (tdepth > tags->nr_tags) {
615 struct blk_mq_tag_set *set = hctx->queue->tag_set;
616 struct blk_mq_tags *new;
Jens Axboe70f36b62017-01-19 10:59:07 -0700617
618 if (!can_grow)
619 return -EINVAL;
620
621 /*
622 * We need some sort of upper limit, set it high enough that
623 * no valid use cases should require more.
624 */
John Garryd97e5942021-05-13 20:00:58 +0800625 if (tdepth > MAX_SCHED_RQ)
Jens Axboe70f36b62017-01-19 10:59:07 -0700626 return -EINVAL;
627
John Garrye155b0c2021-10-05 18:23:37 +0800628 /*
629 * Only the sbitmap needs resizing since we allocated the max
630 * initially.
631 */
John Garry079a2e32021-10-05 18:23:39 +0800632 if (blk_mq_is_shared_tags(set->flags))
John Garrye155b0c2021-10-05 18:23:37 +0800633 return 0;
634
John Garry63064be2021-10-05 18:23:35 +0800635 new = blk_mq_alloc_map_and_rqs(set, hctx->queue_num, tdepth);
Jens Axboe70f36b62017-01-19 10:59:07 -0700636 if (!new)
637 return -ENOMEM;
Jens Axboe70f36b62017-01-19 10:59:07 -0700638
John Garry645db342021-10-05 18:23:36 +0800639 blk_mq_free_map_and_rqs(set, *tagsptr, hctx->queue_num);
Jens Axboe70f36b62017-01-19 10:59:07 -0700640 *tagsptr = new;
641 } else {
642 /*
643 * Don't need (or can't) update reserved tags here, they
644 * remain static and should never need resizing.
645 */
John Garryae0f1a72021-10-05 18:23:38 +0800646 sbitmap_queue_resize(&tags->bitmap_tags,
Ming Lei75d6e172018-08-02 18:23:26 +0800647 tdepth - tags->nr_reserved_tags);
Jens Axboe70f36b62017-01-19 10:59:07 -0700648 }
649
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600650 return 0;
651}
652
John Garry079a2e32021-10-05 18:23:39 +0800653void blk_mq_tag_resize_shared_tags(struct blk_mq_tag_set *set, unsigned int size)
John Garry32bc15a2020-08-19 23:20:24 +0800654{
John Garry079a2e32021-10-05 18:23:39 +0800655 struct blk_mq_tags *tags = set->shared_tags;
John Garrye155b0c2021-10-05 18:23:37 +0800656
John Garryae0f1a72021-10-05 18:23:38 +0800657 sbitmap_queue_resize(&tags->bitmap_tags, size - set->reserved_tags);
John Garry32bc15a2020-08-19 23:20:24 +0800658}
659
John Garry079a2e32021-10-05 18:23:39 +0800660void blk_mq_tag_update_sched_shared_tags(struct request_queue *q)
John Garrya7e73882021-10-05 18:23:34 +0800661{
John Garry079a2e32021-10-05 18:23:39 +0800662 sbitmap_queue_resize(&q->sched_shared_tags->bitmap_tags,
John Garrya7e73882021-10-05 18:23:34 +0800663 q->nr_requests - q->tag_set->reserved_tags);
664}
665
Bart Van Assche205fb5f52014-10-30 14:45:11 +0100666/**
667 * blk_mq_unique_tag() - return a tag that is unique queue-wide
668 * @rq: request for which to compute a unique tag
669 *
670 * The tag field in struct request is unique per hardware queue but not over
671 * all hardware queues. Hence this function that returns a tag with the
672 * hardware context index in the upper bits and the per hardware queue tag in
673 * the lower bits.
674 *
675 * Note: When called for a request that is queued on a non-multiqueue request
676 * queue, the hardware context index is set to zero.
677 */
678u32 blk_mq_unique_tag(struct request *rq)
679{
Jens Axboeea4f9952018-10-29 15:06:13 -0600680 return (rq->mq_hctx->queue_num << BLK_MQ_UNIQUE_TAG_BITS) |
Bart Van Assche205fb5f52014-10-30 14:45:11 +0100681 (rq->tag & BLK_MQ_UNIQUE_TAG_MASK);
682}
683EXPORT_SYMBOL(blk_mq_unique_tag);