blob: fbc153aef166d7faa27b65f41d0870ca844d9aad [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jens Axboe8324aa92008-01-29 14:51:59 +01002/*
3 * Functions related to tagged command queuing
4 */
5#include <linux/kernel.h>
6#include <linux/module.h>
7#include <linux/bio.h>
8#include <linux/blkdev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Jens Axboe8324aa92008-01-29 14:51:59 +010010
Adrian Bunk278caf02008-03-04 11:23:44 +010011#include "blk.h"
12
Jens Axboe8324aa92008-01-29 14:51:59 +010013/**
14 * blk_queue_find_tag - find a request by its tag and queue
15 * @q: The request queue for the device
16 * @tag: The tag of the request
17 *
18 * Notes:
19 * Should be used when a device returns a tag and you want to match
20 * it with a request.
21 *
22 * no locks need be held.
23 **/
24struct request *blk_queue_find_tag(struct request_queue *q, int tag)
25{
26 return blk_map_queue_find_tag(q->queue_tags, tag);
27}
Jens Axboe8324aa92008-01-29 14:51:59 +010028EXPORT_SYMBOL(blk_queue_find_tag);
29
30/**
Christoph Hellwigd45b3272014-07-08 12:25:28 +020031 * blk_free_tags - release a given set of tag maintenance info
Jens Axboe8324aa92008-01-29 14:51:59 +010032 * @bqt: the tag map to free
33 *
Christoph Hellwigd45b3272014-07-08 12:25:28 +020034 * Drop the reference count on @bqt and frees it when the last reference
35 * is dropped.
Jens Axboe8324aa92008-01-29 14:51:59 +010036 */
Christoph Hellwigd45b3272014-07-08 12:25:28 +020037void blk_free_tags(struct blk_queue_tag *bqt)
Jens Axboe8324aa92008-01-29 14:51:59 +010038{
Christoph Hellwigd45b3272014-07-08 12:25:28 +020039 if (atomic_dec_and_test(&bqt->refcnt)) {
Matthew Wilcox0e3eb452008-08-26 09:02:28 +020040 BUG_ON(find_first_bit(bqt->tag_map, bqt->max_depth) <
41 bqt->max_depth);
Jens Axboe8324aa92008-01-29 14:51:59 +010042
43 kfree(bqt->tag_index);
44 bqt->tag_index = NULL;
45
46 kfree(bqt->tag_map);
47 bqt->tag_map = NULL;
48
49 kfree(bqt);
50 }
Jens Axboe8324aa92008-01-29 14:51:59 +010051}
Christoph Hellwigd45b3272014-07-08 12:25:28 +020052EXPORT_SYMBOL(blk_free_tags);
Jens Axboe8324aa92008-01-29 14:51:59 +010053
54/**
55 * __blk_queue_free_tags - release tag maintenance info
56 * @q: the request queue for the device
57 *
58 * Notes:
59 * blk_cleanup_queue() will take care of calling this function, if tagging
60 * has been used. So there's no need to call this directly.
61 **/
62void __blk_queue_free_tags(struct request_queue *q)
63{
64 struct blk_queue_tag *bqt = q->queue_tags;
65
66 if (!bqt)
67 return;
68
Christoph Hellwigd45b3272014-07-08 12:25:28 +020069 blk_free_tags(bqt);
Jens Axboe8324aa92008-01-29 14:51:59 +010070
71 q->queue_tags = NULL;
Jens Axboeaa94b532008-05-07 09:27:43 +020072 queue_flag_clear_unlocked(QUEUE_FLAG_QUEUED, q);
Jens Axboe8324aa92008-01-29 14:51:59 +010073}
74
75/**
Jens Axboe8324aa92008-01-29 14:51:59 +010076 * blk_queue_free_tags - release tag maintenance info
77 * @q: the request queue for the device
78 *
79 * Notes:
Randy Dunlap710027a2008-08-19 20:13:11 +020080 * This is used to disable tagged queuing to a device, yet leave
Jens Axboe8324aa92008-01-29 14:51:59 +010081 * queue in function.
82 **/
83void blk_queue_free_tags(struct request_queue *q)
84{
Jens Axboeaa94b532008-05-07 09:27:43 +020085 queue_flag_clear_unlocked(QUEUE_FLAG_QUEUED, q);
Jens Axboe8324aa92008-01-29 14:51:59 +010086}
Jens Axboe8324aa92008-01-29 14:51:59 +010087EXPORT_SYMBOL(blk_queue_free_tags);
88
89static int
90init_tag_map(struct request_queue *q, struct blk_queue_tag *tags, int depth)
91{
92 struct request **tag_index;
93 unsigned long *tag_map;
94 int nr_ulongs;
95
96 if (q && depth > q->nr_requests * 2) {
97 depth = q->nr_requests * 2;
98 printk(KERN_ERR "%s: adjusted depth to %d\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -070099 __func__, depth);
Jens Axboe8324aa92008-01-29 14:51:59 +0100100 }
101
Kees Cook6396bb22018-06-12 14:03:40 -0700102 tag_index = kcalloc(depth, sizeof(struct request *), GFP_ATOMIC);
Jens Axboe8324aa92008-01-29 14:51:59 +0100103 if (!tag_index)
104 goto fail;
105
106 nr_ulongs = ALIGN(depth, BITS_PER_LONG) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -0700107 tag_map = kcalloc(nr_ulongs, sizeof(unsigned long), GFP_ATOMIC);
Jens Axboe8324aa92008-01-29 14:51:59 +0100108 if (!tag_map)
109 goto fail;
110
111 tags->real_max_depth = depth;
112 tags->max_depth = depth;
113 tags->tag_index = tag_index;
114 tags->tag_map = tag_map;
115
116 return 0;
117fail:
118 kfree(tag_index);
119 return -ENOMEM;
120}
121
122static struct blk_queue_tag *__blk_queue_init_tags(struct request_queue *q,
Shaohua Liee1b6f72015-01-15 17:32:25 -0800123 int depth, int alloc_policy)
Jens Axboe8324aa92008-01-29 14:51:59 +0100124{
125 struct blk_queue_tag *tags;
126
127 tags = kmalloc(sizeof(struct blk_queue_tag), GFP_ATOMIC);
128 if (!tags)
129 goto fail;
130
131 if (init_tag_map(q, tags, depth))
132 goto fail;
133
Jens Axboe8324aa92008-01-29 14:51:59 +0100134 atomic_set(&tags->refcnt, 1);
Shaohua Liee1b6f72015-01-15 17:32:25 -0800135 tags->alloc_policy = alloc_policy;
136 tags->next_tag = 0;
Jens Axboe8324aa92008-01-29 14:51:59 +0100137 return tags;
138fail:
139 kfree(tags);
140 return NULL;
141}
142
143/**
144 * blk_init_tags - initialize the tag info for an external tag map
145 * @depth: the maximum queue depth supported
Shaohua Liee1b6f72015-01-15 17:32:25 -0800146 * @alloc_policy: tag allocation policy
Jens Axboe8324aa92008-01-29 14:51:59 +0100147 **/
Shaohua Liee1b6f72015-01-15 17:32:25 -0800148struct blk_queue_tag *blk_init_tags(int depth, int alloc_policy)
Jens Axboe8324aa92008-01-29 14:51:59 +0100149{
Shaohua Liee1b6f72015-01-15 17:32:25 -0800150 return __blk_queue_init_tags(NULL, depth, alloc_policy);
Jens Axboe8324aa92008-01-29 14:51:59 +0100151}
152EXPORT_SYMBOL(blk_init_tags);
153
154/**
155 * blk_queue_init_tags - initialize the queue tag info
156 * @q: the request queue for the device
157 * @depth: the maximum queue depth supported
158 * @tags: the tag to use
Shaohua Liee1b6f72015-01-15 17:32:25 -0800159 * @alloc_policy: tag allocation policy
Jens Axboeaa94b532008-05-07 09:27:43 +0200160 *
161 * Queue lock must be held here if the function is called to resize an
162 * existing map.
Jens Axboe8324aa92008-01-29 14:51:59 +0100163 **/
164int blk_queue_init_tags(struct request_queue *q, int depth,
Shaohua Liee1b6f72015-01-15 17:32:25 -0800165 struct blk_queue_tag *tags, int alloc_policy)
Jens Axboe8324aa92008-01-29 14:51:59 +0100166{
167 int rc;
168
169 BUG_ON(tags && q->queue_tags && tags != q->queue_tags);
170
171 if (!tags && !q->queue_tags) {
Shaohua Liee1b6f72015-01-15 17:32:25 -0800172 tags = __blk_queue_init_tags(q, depth, alloc_policy);
Jens Axboe8324aa92008-01-29 14:51:59 +0100173
174 if (!tags)
Peter Senna Tschudind41570b2012-09-12 17:06:40 +0200175 return -ENOMEM;
176
Jens Axboe8324aa92008-01-29 14:51:59 +0100177 } else if (q->queue_tags) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100178 rc = blk_queue_resize_tags(q, depth);
179 if (rc)
Jens Axboe8324aa92008-01-29 14:51:59 +0100180 return rc;
Nick Piggin75ad23b2008-04-29 14:48:33 +0200181 queue_flag_set(QUEUE_FLAG_QUEUED, q);
Jens Axboe8324aa92008-01-29 14:51:59 +0100182 return 0;
183 } else
184 atomic_inc(&tags->refcnt);
185
186 /*
187 * assign it, all done
188 */
189 q->queue_tags = tags;
Jens Axboeaa94b532008-05-07 09:27:43 +0200190 queue_flag_set_unlocked(QUEUE_FLAG_QUEUED, q);
Jens Axboe8324aa92008-01-29 14:51:59 +0100191 return 0;
Jens Axboe8324aa92008-01-29 14:51:59 +0100192}
Jens Axboe8324aa92008-01-29 14:51:59 +0100193EXPORT_SYMBOL(blk_queue_init_tags);
194
195/**
196 * blk_queue_resize_tags - change the queueing depth
197 * @q: the request queue for the device
198 * @new_depth: the new max command queueing depth
199 *
200 * Notes:
201 * Must be called with the queue lock held.
202 **/
203int blk_queue_resize_tags(struct request_queue *q, int new_depth)
204{
205 struct blk_queue_tag *bqt = q->queue_tags;
206 struct request **tag_index;
207 unsigned long *tag_map;
208 int max_depth, nr_ulongs;
209
210 if (!bqt)
211 return -ENXIO;
212
213 /*
214 * if we already have large enough real_max_depth. just
215 * adjust max_depth. *NOTE* as requests with tag value
216 * between new_depth and real_max_depth can be in-flight, tag
217 * map can not be shrunk blindly here.
218 */
219 if (new_depth <= bqt->real_max_depth) {
220 bqt->max_depth = new_depth;
221 return 0;
222 }
223
224 /*
225 * Currently cannot replace a shared tag map with a new
226 * one, so error out if this is the case
227 */
228 if (atomic_read(&bqt->refcnt) != 1)
229 return -EBUSY;
230
231 /*
232 * save the old state info, so we can copy it back
233 */
234 tag_index = bqt->tag_index;
235 tag_map = bqt->tag_map;
236 max_depth = bqt->real_max_depth;
237
238 if (init_tag_map(q, bqt, new_depth))
239 return -ENOMEM;
240
241 memcpy(bqt->tag_index, tag_index, max_depth * sizeof(struct request *));
242 nr_ulongs = ALIGN(max_depth, BITS_PER_LONG) / BITS_PER_LONG;
243 memcpy(bqt->tag_map, tag_map, nr_ulongs * sizeof(unsigned long));
244
245 kfree(tag_index);
246 kfree(tag_map);
247 return 0;
248}
Jens Axboe8324aa92008-01-29 14:51:59 +0100249EXPORT_SYMBOL(blk_queue_resize_tags);
250
251/**
252 * blk_queue_end_tag - end tag operations for a request
253 * @q: the request queue for the device
254 * @rq: the request that has completed
255 *
256 * Description:
Randy Dunlap710027a2008-08-19 20:13:11 +0200257 * Typically called when end_that_request_first() returns %0, meaning
Jens Axboe8324aa92008-01-29 14:51:59 +0100258 * all transfers have been done for a request. It's important to call
259 * this function before end_that_request_last(), as that will put the
260 * request back on the free list thus corrupting the internal tag list.
Jens Axboe8324aa92008-01-29 14:51:59 +0100261 **/
262void blk_queue_end_tag(struct request_queue *q, struct request *rq)
263{
264 struct blk_queue_tag *bqt = q->queue_tags;
Dan Williamsf2b20d42011-12-29 09:16:28 +0100265 unsigned tag = rq->tag; /* negative tags invalid */
Jens Axboe8324aa92008-01-29 14:51:59 +0100266
Bart Van Assche2fff8a92017-06-20 11:15:45 -0700267 lockdep_assert_held(q->queue_lock);
268
Dan Williamsf2b20d42011-12-29 09:16:28 +0100269 BUG_ON(tag >= bqt->real_max_depth);
Jens Axboe8324aa92008-01-29 14:51:59 +0100270
271 list_del_init(&rq->queuelist);
Christoph Hellwige8064022016-10-20 15:12:13 +0200272 rq->rq_flags &= ~RQF_QUEUED;
Jens Axboe8324aa92008-01-29 14:51:59 +0100273 rq->tag = -1;
Jens Axboebd166ef2017-01-17 06:03:22 -0700274 rq->internal_tag = -1;
Jens Axboe8324aa92008-01-29 14:51:59 +0100275
276 if (unlikely(bqt->tag_index[tag] == NULL))
277 printk(KERN_ERR "%s: tag %d is missing\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -0700278 __func__, tag);
Jens Axboe8324aa92008-01-29 14:51:59 +0100279
280 bqt->tag_index[tag] = NULL;
281
282 if (unlikely(!test_bit(tag, bqt->tag_map))) {
283 printk(KERN_ERR "%s: attempt to clear non-busy tag (%d)\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -0700284 __func__, tag);
Jens Axboe8324aa92008-01-29 14:51:59 +0100285 return;
286 }
287 /*
288 * The tag_map bit acts as a lock for tag_index[bit], so we need
289 * unlock memory barrier semantics.
290 */
291 clear_bit_unlock(tag, bqt->tag_map);
Jens Axboe8324aa92008-01-29 14:51:59 +0100292}
Jens Axboe8324aa92008-01-29 14:51:59 +0100293
294/**
295 * blk_queue_start_tag - find a free tag and assign it
296 * @q: the request queue for the device
297 * @rq: the block request that needs tagging
298 *
299 * Description:
300 * This can either be used as a stand-alone helper, or possibly be
301 * assigned as the queue &prep_rq_fn (in which case &struct request
302 * automagically gets a tag assigned). Note that this function
303 * assumes that any type of request can be queued! if this is not
304 * true for your device, you must check the request type before
305 * calling this function. The request will also be removed from
306 * the request queue, so it's the drivers responsibility to readd
307 * it if it should need to be restarted for some reason.
Jens Axboe8324aa92008-01-29 14:51:59 +0100308 **/
309int blk_queue_start_tag(struct request_queue *q, struct request *rq)
310{
311 struct blk_queue_tag *bqt = q->queue_tags;
Jens Axboe0a7ae2f2009-05-20 08:54:31 +0200312 unsigned max_depth;
Jens Axboe8324aa92008-01-29 14:51:59 +0100313 int tag;
314
Bart Van Assche2fff8a92017-06-20 11:15:45 -0700315 lockdep_assert_held(q->queue_lock);
316
Christoph Hellwige8064022016-10-20 15:12:13 +0200317 if (unlikely((rq->rq_flags & RQF_QUEUED))) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100318 printk(KERN_ERR
Jens Axboe8324aa92008-01-29 14:51:59 +0100319 "%s: request %p for device [%s] already tagged %d",
Harvey Harrison24c03d42008-05-01 04:35:17 -0700320 __func__, rq,
Jens Axboe8324aa92008-01-29 14:51:59 +0100321 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->tag);
322 BUG();
323 }
324
325 /*
326 * Protect against shared tag maps, as we may not have exclusive
327 * access to the tag map.
Jens Axboee3ba9ae2008-09-25 11:42:41 +0200328 *
329 * We reserve a few tags just for sync IO, since we don't want
330 * to starve sync IO on behalf of flooding async IO.
Jens Axboe8324aa92008-01-29 14:51:59 +0100331 */
Jens Axboee3ba9ae2008-09-25 11:42:41 +0200332 max_depth = bqt->max_depth;
Jens Axboe0a7ae2f2009-05-20 08:54:31 +0200333 if (!rq_is_sync(rq) && max_depth > 1) {
Jan Karaa6b3f762013-06-28 21:32:27 +0200334 switch (max_depth) {
335 case 2:
Jens Axboe0a7ae2f2009-05-20 08:54:31 +0200336 max_depth = 1;
Jan Karaa6b3f762013-06-28 21:32:27 +0200337 break;
338 case 3:
339 max_depth = 2;
340 break;
341 default:
342 max_depth -= 2;
343 }
Jens Axboe1b59dd52009-10-06 20:19:02 +0200344 if (q->in_flight[BLK_RW_ASYNC] > max_depth)
Jens Axboe0a7ae2f2009-05-20 08:54:31 +0200345 return 1;
346 }
Jens Axboee3ba9ae2008-09-25 11:42:41 +0200347
Jens Axboe8324aa92008-01-29 14:51:59 +0100348 do {
Shaohua Liee1b6f72015-01-15 17:32:25 -0800349 if (bqt->alloc_policy == BLK_TAG_ALLOC_FIFO) {
350 tag = find_first_zero_bit(bqt->tag_map, max_depth);
351 if (tag >= max_depth)
352 return 1;
353 } else {
354 int start = bqt->next_tag;
355 int size = min_t(int, bqt->max_depth, max_depth + start);
356 tag = find_next_zero_bit(bqt->tag_map, size, start);
357 if (tag >= size && start + size > bqt->max_depth) {
358 size = start + size - bqt->max_depth;
359 tag = find_first_zero_bit(bqt->tag_map, size);
360 }
361 if (tag >= size)
362 return 1;
363 }
Jens Axboe8324aa92008-01-29 14:51:59 +0100364
365 } while (test_and_set_bit_lock(tag, bqt->tag_map));
366 /*
367 * We need lock ordering semantics given by test_and_set_bit_lock.
368 * See blk_queue_end_tag for details.
369 */
370
Shaohua Liee1b6f72015-01-15 17:32:25 -0800371 bqt->next_tag = (tag + 1) % bqt->max_depth;
Christoph Hellwige8064022016-10-20 15:12:13 +0200372 rq->rq_flags |= RQF_QUEUED;
Jens Axboe8324aa92008-01-29 14:51:59 +0100373 rq->tag = tag;
374 bqt->tag_index[tag] = rq;
Tejun Heo9934c8c2009-05-08 11:54:16 +0900375 blk_start_request(rq);
Jens Axboe8324aa92008-01-29 14:51:59 +0100376 return 0;
377}
Jens Axboe8324aa92008-01-29 14:51:59 +0100378EXPORT_SYMBOL(blk_queue_start_tag);