blk-mq: remove blk-mq-tag.h
[linux-block.git] / block / blk-mq-tag.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * 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.
6  *
7  * Copyright (C) 2013-2014 Jens Axboe
8  */
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11
12 #include <linux/blk-mq.h>
13 #include <linux/delay.h>
14 #include "blk.h"
15 #include "blk-mq.h"
16 #include "blk-mq-sched.h"
17
18 /*
19  * Recalculate wakeup batch when tag is shared by hctx.
20  */
21 static void blk_mq_update_wake_batch(struct blk_mq_tags *tags,
22                 unsigned int users)
23 {
24         if (!users)
25                 return;
26
27         sbitmap_queue_recalculate_wake_batch(&tags->bitmap_tags,
28                         users);
29         sbitmap_queue_recalculate_wake_batch(&tags->breserved_tags,
30                         users);
31 }
32
33 /*
34  * If a previously inactive queue goes active, bump the active user count.
35  * We need to do this before try to allocate driver tag, then even if fail
36  * to get tag when first time, the other shared-tag users could reserve
37  * budget for it.
38  */
39 void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
40 {
41         unsigned int users;
42
43         if (blk_mq_is_shared_tags(hctx->flags)) {
44                 struct request_queue *q = hctx->queue;
45
46                 if (test_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags))
47                         return;
48                 set_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags);
49         } else {
50                 if (test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
51                         return;
52                 set_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state);
53         }
54
55         users = atomic_inc_return(&hctx->tags->active_queues);
56
57         blk_mq_update_wake_batch(hctx->tags, users);
58 }
59
60 /*
61  * Wakeup all potentially sleeping on tags
62  */
63 void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool include_reserve)
64 {
65         sbitmap_queue_wake_all(&tags->bitmap_tags);
66         if (include_reserve)
67                 sbitmap_queue_wake_all(&tags->breserved_tags);
68 }
69
70 /*
71  * If a previously busy queue goes inactive, potential waiters could now
72  * be allowed to queue. Wake them up and check.
73  */
74 void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
75 {
76         struct blk_mq_tags *tags = hctx->tags;
77         unsigned int users;
78
79         if (blk_mq_is_shared_tags(hctx->flags)) {
80                 struct request_queue *q = hctx->queue;
81
82                 if (!test_and_clear_bit(QUEUE_FLAG_HCTX_ACTIVE,
83                                         &q->queue_flags))
84                         return;
85         } else {
86                 if (!test_and_clear_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
87                         return;
88         }
89
90         users = atomic_dec_return(&tags->active_queues);
91
92         blk_mq_update_wake_batch(tags, users);
93
94         blk_mq_tag_wakeup_all(tags, false);
95 }
96
97 static int __blk_mq_get_tag(struct blk_mq_alloc_data *data,
98                             struct sbitmap_queue *bt)
99 {
100         if (!data->q->elevator && !(data->flags & BLK_MQ_REQ_RESERVED) &&
101                         !hctx_may_queue(data->hctx, bt))
102                 return BLK_MQ_NO_TAG;
103
104         if (data->shallow_depth)
105                 return sbitmap_queue_get_shallow(bt, data->shallow_depth);
106         else
107                 return __sbitmap_queue_get(bt);
108 }
109
110 unsigned long blk_mq_get_tags(struct blk_mq_alloc_data *data, int nr_tags,
111                               unsigned int *offset)
112 {
113         struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
114         struct sbitmap_queue *bt = &tags->bitmap_tags;
115         unsigned long ret;
116
117         if (data->shallow_depth ||data->flags & BLK_MQ_REQ_RESERVED ||
118             data->hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
119                 return 0;
120         ret = __sbitmap_queue_get_batch(bt, nr_tags, offset);
121         *offset += tags->nr_reserved_tags;
122         return ret;
123 }
124
125 unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
126 {
127         struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
128         struct sbitmap_queue *bt;
129         struct sbq_wait_state *ws;
130         DEFINE_SBQ_WAIT(wait);
131         unsigned int tag_offset;
132         int tag;
133
134         if (data->flags & BLK_MQ_REQ_RESERVED) {
135                 if (unlikely(!tags->nr_reserved_tags)) {
136                         WARN_ON_ONCE(1);
137                         return BLK_MQ_NO_TAG;
138                 }
139                 bt = &tags->breserved_tags;
140                 tag_offset = 0;
141         } else {
142                 bt = &tags->bitmap_tags;
143                 tag_offset = tags->nr_reserved_tags;
144         }
145
146         tag = __blk_mq_get_tag(data, bt);
147         if (tag != BLK_MQ_NO_TAG)
148                 goto found_tag;
149
150         if (data->flags & BLK_MQ_REQ_NOWAIT)
151                 return BLK_MQ_NO_TAG;
152
153         ws = bt_wait_ptr(bt, data->hctx);
154         do {
155                 struct sbitmap_queue *bt_prev;
156
157                 /*
158                  * We're out of tags on this hardware queue, kick any
159                  * pending IO submits before going to sleep waiting for
160                  * some to complete.
161                  */
162                 blk_mq_run_hw_queue(data->hctx, false);
163
164                 /*
165                  * Retry tag allocation after running the hardware queue,
166                  * as running the queue may also have found completions.
167                  */
168                 tag = __blk_mq_get_tag(data, bt);
169                 if (tag != BLK_MQ_NO_TAG)
170                         break;
171
172                 sbitmap_prepare_to_wait(bt, ws, &wait, TASK_UNINTERRUPTIBLE);
173
174                 tag = __blk_mq_get_tag(data, bt);
175                 if (tag != BLK_MQ_NO_TAG)
176                         break;
177
178                 bt_prev = bt;
179                 io_schedule();
180
181                 sbitmap_finish_wait(bt, ws, &wait);
182
183                 data->ctx = blk_mq_get_ctx(data->q);
184                 data->hctx = blk_mq_map_queue(data->q, data->cmd_flags,
185                                                 data->ctx);
186                 tags = blk_mq_tags_from_data(data);
187                 if (data->flags & BLK_MQ_REQ_RESERVED)
188                         bt = &tags->breserved_tags;
189                 else
190                         bt = &tags->bitmap_tags;
191
192                 /*
193                  * If destination hw queue is changed, fake wake up on
194                  * previous queue for compensating the wake up miss, so
195                  * other allocations on previous queue won't be starved.
196                  */
197                 if (bt != bt_prev)
198                         sbitmap_queue_wake_up(bt_prev, 1);
199
200                 ws = bt_wait_ptr(bt, data->hctx);
201         } while (1);
202
203         sbitmap_finish_wait(bt, ws, &wait);
204
205 found_tag:
206         /*
207          * Give up this allocation if the hctx is inactive.  The caller will
208          * retry on an active hctx.
209          */
210         if (unlikely(test_bit(BLK_MQ_S_INACTIVE, &data->hctx->state))) {
211                 blk_mq_put_tag(tags, data->ctx, tag + tag_offset);
212                 return BLK_MQ_NO_TAG;
213         }
214         return tag + tag_offset;
215 }
216
217 void blk_mq_put_tag(struct blk_mq_tags *tags, struct blk_mq_ctx *ctx,
218                     unsigned int tag)
219 {
220         if (!blk_mq_tag_is_reserved(tags, tag)) {
221                 const int real_tag = tag - tags->nr_reserved_tags;
222
223                 BUG_ON(real_tag >= tags->nr_tags);
224                 sbitmap_queue_clear(&tags->bitmap_tags, real_tag, ctx->cpu);
225         } else {
226                 sbitmap_queue_clear(&tags->breserved_tags, tag, ctx->cpu);
227         }
228 }
229
230 void blk_mq_put_tags(struct blk_mq_tags *tags, int *tag_array, int nr_tags)
231 {
232         sbitmap_queue_clear_batch(&tags->bitmap_tags, tags->nr_reserved_tags,
233                                         tag_array, nr_tags);
234 }
235
236 struct bt_iter_data {
237         struct blk_mq_hw_ctx *hctx;
238         struct request_queue *q;
239         busy_tag_iter_fn *fn;
240         void *data;
241         bool reserved;
242 };
243
244 static struct request *blk_mq_find_and_get_req(struct blk_mq_tags *tags,
245                 unsigned int bitnr)
246 {
247         struct request *rq;
248         unsigned long flags;
249
250         spin_lock_irqsave(&tags->lock, flags);
251         rq = tags->rqs[bitnr];
252         if (!rq || rq->tag != bitnr || !req_ref_inc_not_zero(rq))
253                 rq = NULL;
254         spin_unlock_irqrestore(&tags->lock, flags);
255         return rq;
256 }
257
258 static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
259 {
260         struct bt_iter_data *iter_data = data;
261         struct blk_mq_hw_ctx *hctx = iter_data->hctx;
262         struct request_queue *q = iter_data->q;
263         struct blk_mq_tag_set *set = q->tag_set;
264         struct blk_mq_tags *tags;
265         struct request *rq;
266         bool ret = true;
267
268         if (blk_mq_is_shared_tags(set->flags))
269                 tags = set->shared_tags;
270         else
271                 tags = hctx->tags;
272
273         if (!iter_data->reserved)
274                 bitnr += tags->nr_reserved_tags;
275         /*
276          * We can hit rq == NULL here, because the tagging functions
277          * test and set the bit before assigning ->rqs[].
278          */
279         rq = blk_mq_find_and_get_req(tags, bitnr);
280         if (!rq)
281                 return true;
282
283         if (rq->q == q && (!hctx || rq->mq_hctx == hctx))
284                 ret = iter_data->fn(rq, iter_data->data);
285         blk_mq_put_rq_ref(rq);
286         return ret;
287 }
288
289 /**
290  * bt_for_each - iterate over the requests associated with a hardware queue
291  * @hctx:       Hardware queue to examine.
292  * @q:          Request queue to examine.
293  * @bt:         sbitmap to examine. This is either the breserved_tags member
294  *              or the bitmap_tags member of struct blk_mq_tags.
295  * @fn:         Pointer to the function that will be called for each request
296  *              associated with @hctx that has been assigned a driver tag.
297  *              @fn will be called as follows: @fn(@hctx, rq, @data, @reserved)
298  *              where rq is a pointer to a request. Return true to continue
299  *              iterating tags, false to stop.
300  * @data:       Will be passed as third argument to @fn.
301  * @reserved:   Indicates whether @bt is the breserved_tags member or the
302  *              bitmap_tags member of struct blk_mq_tags.
303  */
304 static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct request_queue *q,
305                         struct sbitmap_queue *bt, busy_tag_iter_fn *fn,
306                         void *data, bool reserved)
307 {
308         struct bt_iter_data iter_data = {
309                 .hctx = hctx,
310                 .fn = fn,
311                 .data = data,
312                 .reserved = reserved,
313                 .q = q,
314         };
315
316         sbitmap_for_each_set(&bt->sb, bt_iter, &iter_data);
317 }
318
319 struct bt_tags_iter_data {
320         struct blk_mq_tags *tags;
321         busy_tag_iter_fn *fn;
322         void *data;
323         unsigned int flags;
324 };
325
326 #define BT_TAG_ITER_RESERVED            (1 << 0)
327 #define BT_TAG_ITER_STARTED             (1 << 1)
328 #define BT_TAG_ITER_STATIC_RQS          (1 << 2)
329
330 static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
331 {
332         struct bt_tags_iter_data *iter_data = data;
333         struct blk_mq_tags *tags = iter_data->tags;
334         struct request *rq;
335         bool ret = true;
336         bool iter_static_rqs = !!(iter_data->flags & BT_TAG_ITER_STATIC_RQS);
337
338         if (!(iter_data->flags & BT_TAG_ITER_RESERVED))
339                 bitnr += tags->nr_reserved_tags;
340
341         /*
342          * We can hit rq == NULL here, because the tagging functions
343          * test and set the bit before assigning ->rqs[].
344          */
345         if (iter_static_rqs)
346                 rq = tags->static_rqs[bitnr];
347         else
348                 rq = blk_mq_find_and_get_req(tags, bitnr);
349         if (!rq)
350                 return true;
351
352         if (!(iter_data->flags & BT_TAG_ITER_STARTED) ||
353             blk_mq_request_started(rq))
354                 ret = iter_data->fn(rq, iter_data->data);
355         if (!iter_static_rqs)
356                 blk_mq_put_rq_ref(rq);
357         return ret;
358 }
359
360 /**
361  * bt_tags_for_each - iterate over the requests in a tag map
362  * @tags:       Tag map to iterate over.
363  * @bt:         sbitmap to examine. This is either the breserved_tags member
364  *              or the bitmap_tags member of struct blk_mq_tags.
365  * @fn:         Pointer to the function that will be called for each started
366  *              request. @fn will be called as follows: @fn(rq, @data,
367  *              @reserved) where rq is a pointer to a request. Return true
368  *              to continue iterating tags, false to stop.
369  * @data:       Will be passed as second argument to @fn.
370  * @flags:      BT_TAG_ITER_*
371  */
372 static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt,
373                              busy_tag_iter_fn *fn, void *data, unsigned int flags)
374 {
375         struct bt_tags_iter_data iter_data = {
376                 .tags = tags,
377                 .fn = fn,
378                 .data = data,
379                 .flags = flags,
380         };
381
382         if (tags->rqs)
383                 sbitmap_for_each_set(&bt->sb, bt_tags_iter, &iter_data);
384 }
385
386 static void __blk_mq_all_tag_iter(struct blk_mq_tags *tags,
387                 busy_tag_iter_fn *fn, void *priv, unsigned int flags)
388 {
389         WARN_ON_ONCE(flags & BT_TAG_ITER_RESERVED);
390
391         if (tags->nr_reserved_tags)
392                 bt_tags_for_each(tags, &tags->breserved_tags, fn, priv,
393                                  flags | BT_TAG_ITER_RESERVED);
394         bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, flags);
395 }
396
397 /**
398  * blk_mq_all_tag_iter - iterate over all requests in a tag map
399  * @tags:       Tag map to iterate over.
400  * @fn:         Pointer to the function that will be called for each
401  *              request. @fn will be called as follows: @fn(rq, @priv,
402  *              reserved) where rq is a pointer to a request. 'reserved'
403  *              indicates whether or not @rq is a reserved request. Return
404  *              true to continue iterating tags, false to stop.
405  * @priv:       Will be passed as second argument to @fn.
406  *
407  * Caller has to pass the tag map from which requests are allocated.
408  */
409 void blk_mq_all_tag_iter(struct blk_mq_tags *tags, busy_tag_iter_fn *fn,
410                 void *priv)
411 {
412         __blk_mq_all_tag_iter(tags, fn, priv, BT_TAG_ITER_STATIC_RQS);
413 }
414
415 /**
416  * blk_mq_tagset_busy_iter - iterate over all started requests in a tag set
417  * @tagset:     Tag set to iterate over.
418  * @fn:         Pointer to the function that will be called for each started
419  *              request. @fn will be called as follows: @fn(rq, @priv,
420  *              reserved) where rq is a pointer to a request. 'reserved'
421  *              indicates whether or not @rq is a reserved request. Return
422  *              true to continue iterating tags, false to stop.
423  * @priv:       Will be passed as second argument to @fn.
424  *
425  * We grab one request reference before calling @fn and release it after
426  * @fn returns.
427  */
428 void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
429                 busy_tag_iter_fn *fn, void *priv)
430 {
431         unsigned int flags = tagset->flags;
432         int i, nr_tags;
433
434         nr_tags = blk_mq_is_shared_tags(flags) ? 1 : tagset->nr_hw_queues;
435
436         for (i = 0; i < nr_tags; i++) {
437                 if (tagset->tags && tagset->tags[i])
438                         __blk_mq_all_tag_iter(tagset->tags[i], fn, priv,
439                                               BT_TAG_ITER_STARTED);
440         }
441 }
442 EXPORT_SYMBOL(blk_mq_tagset_busy_iter);
443
444 static bool blk_mq_tagset_count_completed_rqs(struct request *rq, void *data)
445 {
446         unsigned *count = data;
447
448         if (blk_mq_request_completed(rq))
449                 (*count)++;
450         return true;
451 }
452
453 /**
454  * blk_mq_tagset_wait_completed_request - Wait until all scheduled request
455  * completions have finished.
456  * @tagset:     Tag set to drain completed request
457  *
458  * Note: This function has to be run after all IO queues are shutdown
459  */
460 void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset)
461 {
462         while (true) {
463                 unsigned count = 0;
464
465                 blk_mq_tagset_busy_iter(tagset,
466                                 blk_mq_tagset_count_completed_rqs, &count);
467                 if (!count)
468                         break;
469                 msleep(5);
470         }
471 }
472 EXPORT_SYMBOL(blk_mq_tagset_wait_completed_request);
473
474 /**
475  * blk_mq_queue_tag_busy_iter - iterate over all requests with a driver tag
476  * @q:          Request queue to examine.
477  * @fn:         Pointer to the function that will be called for each request
478  *              on @q. @fn will be called as follows: @fn(hctx, rq, @priv,
479  *              reserved) where rq is a pointer to a request and hctx points
480  *              to the hardware queue associated with the request. 'reserved'
481  *              indicates whether or not @rq is a reserved request.
482  * @priv:       Will be passed as third argument to @fn.
483  *
484  * Note: if @q->tag_set is shared with other request queues then @fn will be
485  * called for all requests on all queues that share that tag set and not only
486  * for requests associated with @q.
487  */
488 void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_tag_iter_fn *fn,
489                 void *priv)
490 {
491         /*
492          * __blk_mq_update_nr_hw_queues() updates nr_hw_queues and hctx_table
493          * while the queue is frozen. So we can use q_usage_counter to avoid
494          * racing with it.
495          */
496         if (!percpu_ref_tryget(&q->q_usage_counter))
497                 return;
498
499         if (blk_mq_is_shared_tags(q->tag_set->flags)) {
500                 struct blk_mq_tags *tags = q->tag_set->shared_tags;
501                 struct sbitmap_queue *bresv = &tags->breserved_tags;
502                 struct sbitmap_queue *btags = &tags->bitmap_tags;
503
504                 if (tags->nr_reserved_tags)
505                         bt_for_each(NULL, q, bresv, fn, priv, true);
506                 bt_for_each(NULL, q, btags, fn, priv, false);
507         } else {
508                 struct blk_mq_hw_ctx *hctx;
509                 unsigned long i;
510
511                 queue_for_each_hw_ctx(q, hctx, i) {
512                         struct blk_mq_tags *tags = hctx->tags;
513                         struct sbitmap_queue *bresv = &tags->breserved_tags;
514                         struct sbitmap_queue *btags = &tags->bitmap_tags;
515
516                         /*
517                          * If no software queues are currently mapped to this
518                          * hardware queue, there's nothing to check
519                          */
520                         if (!blk_mq_hw_queue_mapped(hctx))
521                                 continue;
522
523                         if (tags->nr_reserved_tags)
524                                 bt_for_each(hctx, q, bresv, fn, priv, true);
525                         bt_for_each(hctx, q, btags, fn, priv, false);
526                 }
527         }
528         blk_queue_exit(q);
529 }
530
531 static int bt_alloc(struct sbitmap_queue *bt, unsigned int depth,
532                     bool round_robin, int node)
533 {
534         return sbitmap_queue_init_node(bt, depth, -1, round_robin, GFP_KERNEL,
535                                        node);
536 }
537
538 int blk_mq_init_bitmaps(struct sbitmap_queue *bitmap_tags,
539                         struct sbitmap_queue *breserved_tags,
540                         unsigned int queue_depth, unsigned int reserved,
541                         int node, int alloc_policy)
542 {
543         unsigned int depth = queue_depth - reserved;
544         bool round_robin = alloc_policy == BLK_TAG_ALLOC_RR;
545
546         if (bt_alloc(bitmap_tags, depth, round_robin, node))
547                 return -ENOMEM;
548         if (bt_alloc(breserved_tags, reserved, round_robin, node))
549                 goto free_bitmap_tags;
550
551         return 0;
552
553 free_bitmap_tags:
554         sbitmap_queue_free(bitmap_tags);
555         return -ENOMEM;
556 }
557
558 struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
559                                      unsigned int reserved_tags,
560                                      int node, int alloc_policy)
561 {
562         struct blk_mq_tags *tags;
563
564         if (total_tags > BLK_MQ_TAG_MAX) {
565                 pr_err("blk-mq: tag depth too large\n");
566                 return NULL;
567         }
568
569         tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
570         if (!tags)
571                 return NULL;
572
573         tags->nr_tags = total_tags;
574         tags->nr_reserved_tags = reserved_tags;
575         spin_lock_init(&tags->lock);
576
577         if (blk_mq_init_bitmaps(&tags->bitmap_tags, &tags->breserved_tags,
578                                 total_tags, reserved_tags, node,
579                                 alloc_policy) < 0) {
580                 kfree(tags);
581                 return NULL;
582         }
583         return tags;
584 }
585
586 void blk_mq_free_tags(struct blk_mq_tags *tags)
587 {
588         sbitmap_queue_free(&tags->bitmap_tags);
589         sbitmap_queue_free(&tags->breserved_tags);
590         kfree(tags);
591 }
592
593 int blk_mq_tag_update_depth(struct blk_mq_hw_ctx *hctx,
594                             struct blk_mq_tags **tagsptr, unsigned int tdepth,
595                             bool can_grow)
596 {
597         struct blk_mq_tags *tags = *tagsptr;
598
599         if (tdepth <= tags->nr_reserved_tags)
600                 return -EINVAL;
601
602         /*
603          * If we are allowed to grow beyond the original size, allocate
604          * a new set of tags before freeing the old one.
605          */
606         if (tdepth > tags->nr_tags) {
607                 struct blk_mq_tag_set *set = hctx->queue->tag_set;
608                 struct blk_mq_tags *new;
609
610                 if (!can_grow)
611                         return -EINVAL;
612
613                 /*
614                  * We need some sort of upper limit, set it high enough that
615                  * no valid use cases should require more.
616                  */
617                 if (tdepth > MAX_SCHED_RQ)
618                         return -EINVAL;
619
620                 /*
621                  * Only the sbitmap needs resizing since we allocated the max
622                  * initially.
623                  */
624                 if (blk_mq_is_shared_tags(set->flags))
625                         return 0;
626
627                 new = blk_mq_alloc_map_and_rqs(set, hctx->queue_num, tdepth);
628                 if (!new)
629                         return -ENOMEM;
630
631                 blk_mq_free_map_and_rqs(set, *tagsptr, hctx->queue_num);
632                 *tagsptr = new;
633         } else {
634                 /*
635                  * Don't need (or can't) update reserved tags here, they
636                  * remain static and should never need resizing.
637                  */
638                 sbitmap_queue_resize(&tags->bitmap_tags,
639                                 tdepth - tags->nr_reserved_tags);
640         }
641
642         return 0;
643 }
644
645 void blk_mq_tag_resize_shared_tags(struct blk_mq_tag_set *set, unsigned int size)
646 {
647         struct blk_mq_tags *tags = set->shared_tags;
648
649         sbitmap_queue_resize(&tags->bitmap_tags, size - set->reserved_tags);
650 }
651
652 void blk_mq_tag_update_sched_shared_tags(struct request_queue *q)
653 {
654         sbitmap_queue_resize(&q->sched_shared_tags->bitmap_tags,
655                              q->nr_requests - q->tag_set->reserved_tags);
656 }
657
658 /**
659  * blk_mq_unique_tag() - return a tag that is unique queue-wide
660  * @rq: request for which to compute a unique tag
661  *
662  * The tag field in struct request is unique per hardware queue but not over
663  * all hardware queues. Hence this function that returns a tag with the
664  * hardware context index in the upper bits and the per hardware queue tag in
665  * the lower bits.
666  *
667  * Note: When called for a request that is queued on a non-multiqueue request
668  * queue, the hardware context index is set to zero.
669  */
670 u32 blk_mq_unique_tag(struct request *rq)
671 {
672         return (rq->mq_hctx->queue_num << BLK_MQ_UNIQUE_TAG_BITS) |
673                 (rq->tag & BLK_MQ_UNIQUE_TAG_MASK);
674 }
675 EXPORT_SYMBOL(blk_mq_unique_tag);