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