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