Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Tag allocation using scalable bitmaps. Uses active queue tracking to support
0004  * fairer distribution of tags between multiple submitters when a shared tag map
0005  * is used.
0006  *
0007  * Copyright (C) 2013-2014 Jens Axboe
0008  */
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 
0012 #include <linux/blk-mq.h>
0013 #include <linux/delay.h>
0014 #include "blk.h"
0015 #include "blk-mq.h"
0016 #include "blk-mq-sched.h"
0017 #include "blk-mq-tag.h"
0018 
0019 /*
0020  * Recalculate wakeup batch when tag is shared by hctx.
0021  */
0022 static void blk_mq_update_wake_batch(struct blk_mq_tags *tags,
0023         unsigned int users)
0024 {
0025     if (!users)
0026         return;
0027 
0028     sbitmap_queue_recalculate_wake_batch(&tags->bitmap_tags,
0029             users);
0030     sbitmap_queue_recalculate_wake_batch(&tags->breserved_tags,
0031             users);
0032 }
0033 
0034 /*
0035  * If a previously inactive queue goes active, bump the active user count.
0036  * We need to do this before try to allocate driver tag, then even if fail
0037  * to get tag when first time, the other shared-tag users could reserve
0038  * budget for it.
0039  */
0040 void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
0041 {
0042     unsigned int users;
0043 
0044     if (blk_mq_is_shared_tags(hctx->flags)) {
0045         struct request_queue *q = hctx->queue;
0046 
0047         if (test_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags))
0048             return;
0049         set_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags);
0050     } else {
0051         if (test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
0052             return;
0053         set_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state);
0054     }
0055 
0056     users = atomic_inc_return(&hctx->tags->active_queues);
0057 
0058     blk_mq_update_wake_batch(hctx->tags, users);
0059 }
0060 
0061 /*
0062  * Wakeup all potentially sleeping on tags
0063  */
0064 void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool include_reserve)
0065 {
0066     sbitmap_queue_wake_all(&tags->bitmap_tags);
0067     if (include_reserve)
0068         sbitmap_queue_wake_all(&tags->breserved_tags);
0069 }
0070 
0071 /*
0072  * If a previously busy queue goes inactive, potential waiters could now
0073  * be allowed to queue. Wake them up and check.
0074  */
0075 void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
0076 {
0077     struct blk_mq_tags *tags = hctx->tags;
0078     unsigned int users;
0079 
0080     if (blk_mq_is_shared_tags(hctx->flags)) {
0081         struct request_queue *q = hctx->queue;
0082 
0083         if (!test_and_clear_bit(QUEUE_FLAG_HCTX_ACTIVE,
0084                     &q->queue_flags))
0085             return;
0086     } else {
0087         if (!test_and_clear_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
0088             return;
0089     }
0090 
0091     users = atomic_dec_return(&tags->active_queues);
0092 
0093     blk_mq_update_wake_batch(tags, users);
0094 
0095     blk_mq_tag_wakeup_all(tags, false);
0096 }
0097 
0098 static int __blk_mq_get_tag(struct blk_mq_alloc_data *data,
0099                 struct sbitmap_queue *bt)
0100 {
0101     if (!data->q->elevator && !(data->flags & BLK_MQ_REQ_RESERVED) &&
0102             !hctx_may_queue(data->hctx, bt))
0103         return BLK_MQ_NO_TAG;
0104 
0105     if (data->shallow_depth)
0106         return sbitmap_queue_get_shallow(bt, data->shallow_depth);
0107     else
0108         return __sbitmap_queue_get(bt);
0109 }
0110 
0111 unsigned long blk_mq_get_tags(struct blk_mq_alloc_data *data, int nr_tags,
0112                   unsigned int *offset)
0113 {
0114     struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
0115     struct sbitmap_queue *bt = &tags->bitmap_tags;
0116     unsigned long ret;
0117 
0118     if (data->shallow_depth ||data->flags & BLK_MQ_REQ_RESERVED ||
0119         data->hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
0120         return 0;
0121     ret = __sbitmap_queue_get_batch(bt, nr_tags, offset);
0122     *offset += tags->nr_reserved_tags;
0123     return ret;
0124 }
0125 
0126 unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
0127 {
0128     struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
0129     struct sbitmap_queue *bt;
0130     struct sbq_wait_state *ws;
0131     DEFINE_SBQ_WAIT(wait);
0132     unsigned int tag_offset;
0133     int tag;
0134 
0135     if (data->flags & BLK_MQ_REQ_RESERVED) {
0136         if (unlikely(!tags->nr_reserved_tags)) {
0137             WARN_ON_ONCE(1);
0138             return BLK_MQ_NO_TAG;
0139         }
0140         bt = &tags->breserved_tags;
0141         tag_offset = 0;
0142     } else {
0143         bt = &tags->bitmap_tags;
0144         tag_offset = tags->nr_reserved_tags;
0145     }
0146 
0147     tag = __blk_mq_get_tag(data, bt);
0148     if (tag != BLK_MQ_NO_TAG)
0149         goto found_tag;
0150 
0151     if (data->flags & BLK_MQ_REQ_NOWAIT)
0152         return BLK_MQ_NO_TAG;
0153 
0154     ws = bt_wait_ptr(bt, data->hctx);
0155     do {
0156         struct sbitmap_queue *bt_prev;
0157 
0158         /*
0159          * We're out of tags on this hardware queue, kick any
0160          * pending IO submits before going to sleep waiting for
0161          * some to complete.
0162          */
0163         blk_mq_run_hw_queue(data->hctx, false);
0164 
0165         /*
0166          * Retry tag allocation after running the hardware queue,
0167          * as running the queue may also have found completions.
0168          */
0169         tag = __blk_mq_get_tag(data, bt);
0170         if (tag != BLK_MQ_NO_TAG)
0171             break;
0172 
0173         sbitmap_prepare_to_wait(bt, ws, &wait, TASK_UNINTERRUPTIBLE);
0174 
0175         tag = __blk_mq_get_tag(data, bt);
0176         if (tag != BLK_MQ_NO_TAG)
0177             break;
0178 
0179         bt_prev = bt;
0180         io_schedule();
0181 
0182         sbitmap_finish_wait(bt, ws, &wait);
0183 
0184         data->ctx = blk_mq_get_ctx(data->q);
0185         data->hctx = blk_mq_map_queue(data->q, data->cmd_flags,
0186                         data->ctx);
0187         tags = blk_mq_tags_from_data(data);
0188         if (data->flags & BLK_MQ_REQ_RESERVED)
0189             bt = &tags->breserved_tags;
0190         else
0191             bt = &tags->bitmap_tags;
0192 
0193         /*
0194          * If destination hw queue is changed, fake wake up on
0195          * previous queue for compensating the wake up miss, so
0196          * other allocations on previous queue won't be starved.
0197          */
0198         if (bt != bt_prev)
0199             sbitmap_queue_wake_up(bt_prev);
0200 
0201         ws = bt_wait_ptr(bt, data->hctx);
0202     } while (1);
0203 
0204     sbitmap_finish_wait(bt, ws, &wait);
0205 
0206 found_tag:
0207     /*
0208      * Give up this allocation if the hctx is inactive.  The caller will
0209      * retry on an active hctx.
0210      */
0211     if (unlikely(test_bit(BLK_MQ_S_INACTIVE, &data->hctx->state))) {
0212         blk_mq_put_tag(tags, data->ctx, tag + tag_offset);
0213         return BLK_MQ_NO_TAG;
0214     }
0215     return tag + tag_offset;
0216 }
0217 
0218 void blk_mq_put_tag(struct blk_mq_tags *tags, struct blk_mq_ctx *ctx,
0219             unsigned int tag)
0220 {
0221     if (!blk_mq_tag_is_reserved(tags, tag)) {
0222         const int real_tag = tag - tags->nr_reserved_tags;
0223 
0224         BUG_ON(real_tag >= tags->nr_tags);
0225         sbitmap_queue_clear(&tags->bitmap_tags, real_tag, ctx->cpu);
0226     } else {
0227         sbitmap_queue_clear(&tags->breserved_tags, tag, ctx->cpu);
0228     }
0229 }
0230 
0231 void blk_mq_put_tags(struct blk_mq_tags *tags, int *tag_array, int nr_tags)
0232 {
0233     sbitmap_queue_clear_batch(&tags->bitmap_tags, tags->nr_reserved_tags,
0234                     tag_array, nr_tags);
0235 }
0236 
0237 struct bt_iter_data {
0238     struct blk_mq_hw_ctx *hctx;
0239     struct request_queue *q;
0240     busy_tag_iter_fn *fn;
0241     void *data;
0242     bool reserved;
0243 };
0244 
0245 static struct request *blk_mq_find_and_get_req(struct blk_mq_tags *tags,
0246         unsigned int bitnr)
0247 {
0248     struct request *rq;
0249     unsigned long flags;
0250 
0251     spin_lock_irqsave(&tags->lock, flags);
0252     rq = tags->rqs[bitnr];
0253     if (!rq || rq->tag != bitnr || !req_ref_inc_not_zero(rq))
0254         rq = NULL;
0255     spin_unlock_irqrestore(&tags->lock, flags);
0256     return rq;
0257 }
0258 
0259 static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
0260 {
0261     struct bt_iter_data *iter_data = data;
0262     struct blk_mq_hw_ctx *hctx = iter_data->hctx;
0263     struct request_queue *q = iter_data->q;
0264     struct blk_mq_tag_set *set = q->tag_set;
0265     struct blk_mq_tags *tags;
0266     struct request *rq;
0267     bool ret = true;
0268 
0269     if (blk_mq_is_shared_tags(set->flags))
0270         tags = set->shared_tags;
0271     else
0272         tags = hctx->tags;
0273 
0274     if (!iter_data->reserved)
0275         bitnr += tags->nr_reserved_tags;
0276     /*
0277      * We can hit rq == NULL here, because the tagging functions
0278      * test and set the bit before assigning ->rqs[].
0279      */
0280     rq = blk_mq_find_and_get_req(tags, bitnr);
0281     if (!rq)
0282         return true;
0283 
0284     if (rq->q == q && (!hctx || rq->mq_hctx == hctx))
0285         ret = iter_data->fn(rq, iter_data->data);
0286     blk_mq_put_rq_ref(rq);
0287     return ret;
0288 }
0289 
0290 /**
0291  * bt_for_each - iterate over the requests associated with a hardware queue
0292  * @hctx:   Hardware queue to examine.
0293  * @q:      Request queue to examine.
0294  * @bt:     sbitmap to examine. This is either the breserved_tags member
0295  *      or the bitmap_tags member of struct blk_mq_tags.
0296  * @fn:     Pointer to the function that will be called for each request
0297  *      associated with @hctx that has been assigned a driver tag.
0298  *      @fn will be called as follows: @fn(@hctx, rq, @data, @reserved)
0299  *      where rq is a pointer to a request. Return true to continue
0300  *      iterating tags, false to stop.
0301  * @data:   Will be passed as third argument to @fn.
0302  * @reserved:   Indicates whether @bt is the breserved_tags member or the
0303  *      bitmap_tags member of struct blk_mq_tags.
0304  */
0305 static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct request_queue *q,
0306             struct sbitmap_queue *bt, busy_tag_iter_fn *fn,
0307             void *data, bool reserved)
0308 {
0309     struct bt_iter_data iter_data = {
0310         .hctx = hctx,
0311         .fn = fn,
0312         .data = data,
0313         .reserved = reserved,
0314         .q = q,
0315     };
0316 
0317     sbitmap_for_each_set(&bt->sb, bt_iter, &iter_data);
0318 }
0319 
0320 struct bt_tags_iter_data {
0321     struct blk_mq_tags *tags;
0322     busy_tag_iter_fn *fn;
0323     void *data;
0324     unsigned int flags;
0325 };
0326 
0327 #define BT_TAG_ITER_RESERVED        (1 << 0)
0328 #define BT_TAG_ITER_STARTED     (1 << 1)
0329 #define BT_TAG_ITER_STATIC_RQS      (1 << 2)
0330 
0331 static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
0332 {
0333     struct bt_tags_iter_data *iter_data = data;
0334     struct blk_mq_tags *tags = iter_data->tags;
0335     struct request *rq;
0336     bool ret = true;
0337     bool iter_static_rqs = !!(iter_data->flags & BT_TAG_ITER_STATIC_RQS);
0338 
0339     if (!(iter_data->flags & BT_TAG_ITER_RESERVED))
0340         bitnr += tags->nr_reserved_tags;
0341 
0342     /*
0343      * We can hit rq == NULL here, because the tagging functions
0344      * test and set the bit before assigning ->rqs[].
0345      */
0346     if (iter_static_rqs)
0347         rq = tags->static_rqs[bitnr];
0348     else
0349         rq = blk_mq_find_and_get_req(tags, bitnr);
0350     if (!rq)
0351         return true;
0352 
0353     if (!(iter_data->flags & BT_TAG_ITER_STARTED) ||
0354         blk_mq_request_started(rq))
0355         ret = iter_data->fn(rq, iter_data->data);
0356     if (!iter_static_rqs)
0357         blk_mq_put_rq_ref(rq);
0358     return ret;
0359 }
0360 
0361 /**
0362  * bt_tags_for_each - iterate over the requests in a tag map
0363  * @tags:   Tag map to iterate over.
0364  * @bt:     sbitmap to examine. This is either the breserved_tags member
0365  *      or the bitmap_tags member of struct blk_mq_tags.
0366  * @fn:     Pointer to the function that will be called for each started
0367  *      request. @fn will be called as follows: @fn(rq, @data,
0368  *      @reserved) where rq is a pointer to a request. Return true
0369  *      to continue iterating tags, false to stop.
0370  * @data:   Will be passed as second argument to @fn.
0371  * @flags:  BT_TAG_ITER_*
0372  */
0373 static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt,
0374                  busy_tag_iter_fn *fn, void *data, unsigned int flags)
0375 {
0376     struct bt_tags_iter_data iter_data = {
0377         .tags = tags,
0378         .fn = fn,
0379         .data = data,
0380         .flags = flags,
0381     };
0382 
0383     if (tags->rqs)
0384         sbitmap_for_each_set(&bt->sb, bt_tags_iter, &iter_data);
0385 }
0386 
0387 static void __blk_mq_all_tag_iter(struct blk_mq_tags *tags,
0388         busy_tag_iter_fn *fn, void *priv, unsigned int flags)
0389 {
0390     WARN_ON_ONCE(flags & BT_TAG_ITER_RESERVED);
0391 
0392     if (tags->nr_reserved_tags)
0393         bt_tags_for_each(tags, &tags->breserved_tags, fn, priv,
0394                  flags | BT_TAG_ITER_RESERVED);
0395     bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, flags);
0396 }
0397 
0398 /**
0399  * blk_mq_all_tag_iter - iterate over all requests in a tag map
0400  * @tags:   Tag map to iterate over.
0401  * @fn:     Pointer to the function that will be called for each
0402  *      request. @fn will be called as follows: @fn(rq, @priv,
0403  *      reserved) where rq is a pointer to a request. 'reserved'
0404  *      indicates whether or not @rq is a reserved request. Return
0405  *      true to continue iterating tags, false to stop.
0406  * @priv:   Will be passed as second argument to @fn.
0407  *
0408  * Caller has to pass the tag map from which requests are allocated.
0409  */
0410 void blk_mq_all_tag_iter(struct blk_mq_tags *tags, busy_tag_iter_fn *fn,
0411         void *priv)
0412 {
0413     __blk_mq_all_tag_iter(tags, fn, priv, BT_TAG_ITER_STATIC_RQS);
0414 }
0415 
0416 /**
0417  * blk_mq_tagset_busy_iter - iterate over all started requests in a tag set
0418  * @tagset: Tag set to iterate over.
0419  * @fn:     Pointer to the function that will be called for each started
0420  *      request. @fn will be called as follows: @fn(rq, @priv,
0421  *      reserved) where rq is a pointer to a request. 'reserved'
0422  *      indicates whether or not @rq is a reserved request. Return
0423  *      true to continue iterating tags, false to stop.
0424  * @priv:   Will be passed as second argument to @fn.
0425  *
0426  * We grab one request reference before calling @fn and release it after
0427  * @fn returns.
0428  */
0429 void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
0430         busy_tag_iter_fn *fn, void *priv)
0431 {
0432     unsigned int flags = tagset->flags;
0433     int i, nr_tags;
0434 
0435     nr_tags = blk_mq_is_shared_tags(flags) ? 1 : tagset->nr_hw_queues;
0436 
0437     for (i = 0; i < nr_tags; i++) {
0438         if (tagset->tags && tagset->tags[i])
0439             __blk_mq_all_tag_iter(tagset->tags[i], fn, priv,
0440                           BT_TAG_ITER_STARTED);
0441     }
0442 }
0443 EXPORT_SYMBOL(blk_mq_tagset_busy_iter);
0444 
0445 static bool blk_mq_tagset_count_completed_rqs(struct request *rq, void *data)
0446 {
0447     unsigned *count = data;
0448 
0449     if (blk_mq_request_completed(rq))
0450         (*count)++;
0451     return true;
0452 }
0453 
0454 /**
0455  * blk_mq_tagset_wait_completed_request - Wait until all scheduled request
0456  * completions have finished.
0457  * @tagset: Tag set to drain completed request
0458  *
0459  * Note: This function has to be run after all IO queues are shutdown
0460  */
0461 void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset)
0462 {
0463     while (true) {
0464         unsigned count = 0;
0465 
0466         blk_mq_tagset_busy_iter(tagset,
0467                 blk_mq_tagset_count_completed_rqs, &count);
0468         if (!count)
0469             break;
0470         msleep(5);
0471     }
0472 }
0473 EXPORT_SYMBOL(blk_mq_tagset_wait_completed_request);
0474 
0475 /**
0476  * blk_mq_queue_tag_busy_iter - iterate over all requests with a driver tag
0477  * @q:      Request queue to examine.
0478  * @fn:     Pointer to the function that will be called for each request
0479  *      on @q. @fn will be called as follows: @fn(hctx, rq, @priv,
0480  *      reserved) where rq is a pointer to a request and hctx points
0481  *      to the hardware queue associated with the request. 'reserved'
0482  *      indicates whether or not @rq is a reserved request.
0483  * @priv:   Will be passed as third argument to @fn.
0484  *
0485  * Note: if @q->tag_set is shared with other request queues then @fn will be
0486  * called for all requests on all queues that share that tag set and not only
0487  * for requests associated with @q.
0488  */
0489 void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_tag_iter_fn *fn,
0490         void *priv)
0491 {
0492     /*
0493      * __blk_mq_update_nr_hw_queues() updates nr_hw_queues and hctx_table
0494      * while the queue is frozen. So we can use q_usage_counter to avoid
0495      * racing with it.
0496      */
0497     if (!percpu_ref_tryget(&q->q_usage_counter))
0498         return;
0499 
0500     if (blk_mq_is_shared_tags(q->tag_set->flags)) {
0501         struct blk_mq_tags *tags = q->tag_set->shared_tags;
0502         struct sbitmap_queue *bresv = &tags->breserved_tags;
0503         struct sbitmap_queue *btags = &tags->bitmap_tags;
0504 
0505         if (tags->nr_reserved_tags)
0506             bt_for_each(NULL, q, bresv, fn, priv, true);
0507         bt_for_each(NULL, q, btags, fn, priv, false);
0508     } else {
0509         struct blk_mq_hw_ctx *hctx;
0510         unsigned long i;
0511 
0512         queue_for_each_hw_ctx(q, hctx, i) {
0513             struct blk_mq_tags *tags = hctx->tags;
0514             struct sbitmap_queue *bresv = &tags->breserved_tags;
0515             struct sbitmap_queue *btags = &tags->bitmap_tags;
0516 
0517             /*
0518              * If no software queues are currently mapped to this
0519              * hardware queue, there's nothing to check
0520              */
0521             if (!blk_mq_hw_queue_mapped(hctx))
0522                 continue;
0523 
0524             if (tags->nr_reserved_tags)
0525                 bt_for_each(hctx, q, bresv, fn, priv, true);
0526             bt_for_each(hctx, q, btags, fn, priv, false);
0527         }
0528     }
0529     blk_queue_exit(q);
0530 }
0531 
0532 static int bt_alloc(struct sbitmap_queue *bt, unsigned int depth,
0533             bool round_robin, int node)
0534 {
0535     return sbitmap_queue_init_node(bt, depth, -1, round_robin, GFP_KERNEL,
0536                        node);
0537 }
0538 
0539 int blk_mq_init_bitmaps(struct sbitmap_queue *bitmap_tags,
0540             struct sbitmap_queue *breserved_tags,
0541             unsigned int queue_depth, unsigned int reserved,
0542             int node, int alloc_policy)
0543 {
0544     unsigned int depth = queue_depth - reserved;
0545     bool round_robin = alloc_policy == BLK_TAG_ALLOC_RR;
0546 
0547     if (bt_alloc(bitmap_tags, depth, round_robin, node))
0548         return -ENOMEM;
0549     if (bt_alloc(breserved_tags, reserved, round_robin, node))
0550         goto free_bitmap_tags;
0551 
0552     return 0;
0553 
0554 free_bitmap_tags:
0555     sbitmap_queue_free(bitmap_tags);
0556     return -ENOMEM;
0557 }
0558 
0559 struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
0560                      unsigned int reserved_tags,
0561                      int node, int alloc_policy)
0562 {
0563     struct blk_mq_tags *tags;
0564 
0565     if (total_tags > BLK_MQ_TAG_MAX) {
0566         pr_err("blk-mq: tag depth too large\n");
0567         return NULL;
0568     }
0569 
0570     tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
0571     if (!tags)
0572         return NULL;
0573 
0574     tags->nr_tags = total_tags;
0575     tags->nr_reserved_tags = reserved_tags;
0576     spin_lock_init(&tags->lock);
0577 
0578     if (blk_mq_init_bitmaps(&tags->bitmap_tags, &tags->breserved_tags,
0579                 total_tags, reserved_tags, node,
0580                 alloc_policy) < 0) {
0581         kfree(tags);
0582         return NULL;
0583     }
0584     return tags;
0585 }
0586 
0587 void blk_mq_free_tags(struct blk_mq_tags *tags)
0588 {
0589     sbitmap_queue_free(&tags->bitmap_tags);
0590     sbitmap_queue_free(&tags->breserved_tags);
0591     kfree(tags);
0592 }
0593 
0594 int blk_mq_tag_update_depth(struct blk_mq_hw_ctx *hctx,
0595                 struct blk_mq_tags **tagsptr, unsigned int tdepth,
0596                 bool can_grow)
0597 {
0598     struct blk_mq_tags *tags = *tagsptr;
0599 
0600     if (tdepth <= tags->nr_reserved_tags)
0601         return -EINVAL;
0602 
0603     /*
0604      * If we are allowed to grow beyond the original size, allocate
0605      * a new set of tags before freeing the old one.
0606      */
0607     if (tdepth > tags->nr_tags) {
0608         struct blk_mq_tag_set *set = hctx->queue->tag_set;
0609         struct blk_mq_tags *new;
0610 
0611         if (!can_grow)
0612             return -EINVAL;
0613 
0614         /*
0615          * We need some sort of upper limit, set it high enough that
0616          * no valid use cases should require more.
0617          */
0618         if (tdepth > MAX_SCHED_RQ)
0619             return -EINVAL;
0620 
0621         /*
0622          * Only the sbitmap needs resizing since we allocated the max
0623          * initially.
0624          */
0625         if (blk_mq_is_shared_tags(set->flags))
0626             return 0;
0627 
0628         new = blk_mq_alloc_map_and_rqs(set, hctx->queue_num, tdepth);
0629         if (!new)
0630             return -ENOMEM;
0631 
0632         blk_mq_free_map_and_rqs(set, *tagsptr, hctx->queue_num);
0633         *tagsptr = new;
0634     } else {
0635         /*
0636          * Don't need (or can't) update reserved tags here, they
0637          * remain static and should never need resizing.
0638          */
0639         sbitmap_queue_resize(&tags->bitmap_tags,
0640                 tdepth - tags->nr_reserved_tags);
0641     }
0642 
0643     return 0;
0644 }
0645 
0646 void blk_mq_tag_resize_shared_tags(struct blk_mq_tag_set *set, unsigned int size)
0647 {
0648     struct blk_mq_tags *tags = set->shared_tags;
0649 
0650     sbitmap_queue_resize(&tags->bitmap_tags, size - set->reserved_tags);
0651 }
0652 
0653 void blk_mq_tag_update_sched_shared_tags(struct request_queue *q)
0654 {
0655     sbitmap_queue_resize(&q->sched_shared_tags->bitmap_tags,
0656                  q->nr_requests - q->tag_set->reserved_tags);
0657 }
0658 
0659 /**
0660  * blk_mq_unique_tag() - return a tag that is unique queue-wide
0661  * @rq: request for which to compute a unique tag
0662  *
0663  * The tag field in struct request is unique per hardware queue but not over
0664  * all hardware queues. Hence this function that returns a tag with the
0665  * hardware context index in the upper bits and the per hardware queue tag in
0666  * the lower bits.
0667  *
0668  * Note: When called for a request that is queued on a non-multiqueue request
0669  * queue, the hardware context index is set to zero.
0670  */
0671 u32 blk_mq_unique_tag(struct request *rq)
0672 {
0673     return (rq->mq_hctx->queue_num << BLK_MQ_UNIQUE_TAG_BITS) |
0674         (rq->tag & BLK_MQ_UNIQUE_TAG_MASK);
0675 }
0676 EXPORT_SYMBOL(blk_mq_unique_tag);