Blame view

block/blk-tag.c 10 KB
8324aa91d   Jens Axboe   block: split tag ...
1
2
3
4
5
6
7
  /*
   * Functions related to tagged command queuing
   */
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/bio.h>
  #include <linux/blkdev.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
8
  #include <linux/slab.h>
8324aa91d   Jens Axboe   block: split tag ...
9

278caf012   Adrian Bunk   block/blk-tag.c s...
10
  #include "blk.h"
8324aa91d   Jens Axboe   block: split tag ...
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  /**
   * blk_queue_find_tag - find a request by its tag and queue
   * @q:	 The request queue for the device
   * @tag: The tag of the request
   *
   * Notes:
   *    Should be used when a device returns a tag and you want to match
   *    it with a request.
   *
   *    no locks need be held.
   **/
  struct request *blk_queue_find_tag(struct request_queue *q, int tag)
  {
  	return blk_map_queue_find_tag(q->queue_tags, tag);
  }
8324aa91d   Jens Axboe   block: split tag ...
26
27
28
  EXPORT_SYMBOL(blk_queue_find_tag);
  
  /**
d45b3279a   Christoph Hellwig   block: don't assu...
29
   * blk_free_tags - release a given set of tag maintenance info
8324aa91d   Jens Axboe   block: split tag ...
30
31
   * @bqt:	the tag map to free
   *
d45b3279a   Christoph Hellwig   block: don't assu...
32
33
   * Drop the reference count on @bqt and frees it when the last reference
   * is dropped.
8324aa91d   Jens Axboe   block: split tag ...
34
   */
d45b3279a   Christoph Hellwig   block: don't assu...
35
  void blk_free_tags(struct blk_queue_tag *bqt)
8324aa91d   Jens Axboe   block: split tag ...
36
  {
d45b3279a   Christoph Hellwig   block: don't assu...
37
  	if (atomic_dec_and_test(&bqt->refcnt)) {
0e3eb45e4   Matthew Wilcox   block: remove unu...
38
39
  		BUG_ON(find_first_bit(bqt->tag_map, bqt->max_depth) <
  							bqt->max_depth);
8324aa91d   Jens Axboe   block: split tag ...
40
41
42
43
44
45
46
47
48
  
  		kfree(bqt->tag_index);
  		bqt->tag_index = NULL;
  
  		kfree(bqt->tag_map);
  		bqt->tag_map = NULL;
  
  		kfree(bqt);
  	}
8324aa91d   Jens Axboe   block: split tag ...
49
  }
d45b3279a   Christoph Hellwig   block: don't assu...
50
  EXPORT_SYMBOL(blk_free_tags);
8324aa91d   Jens Axboe   block: split tag ...
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  
  /**
   * __blk_queue_free_tags - release tag maintenance info
   * @q:  the request queue for the device
   *
   *  Notes:
   *    blk_cleanup_queue() will take care of calling this function, if tagging
   *    has been used. So there's no need to call this directly.
   **/
  void __blk_queue_free_tags(struct request_queue *q)
  {
  	struct blk_queue_tag *bqt = q->queue_tags;
  
  	if (!bqt)
  		return;
d45b3279a   Christoph Hellwig   block: don't assu...
66
  	blk_free_tags(bqt);
8324aa91d   Jens Axboe   block: split tag ...
67
68
  
  	q->queue_tags = NULL;
aa94b5371   Jens Axboe   block: adjust tag...
69
  	queue_flag_clear_unlocked(QUEUE_FLAG_QUEUED, q);
8324aa91d   Jens Axboe   block: split tag ...
70
71
72
  }
  
  /**
8324aa91d   Jens Axboe   block: split tag ...
73
74
75
76
   * blk_queue_free_tags - release tag maintenance info
   * @q:  the request queue for the device
   *
   *  Notes:
710027a48   Randy Dunlap   Add some block/ s...
77
   *	This is used to disable tagged queuing to a device, yet leave
8324aa91d   Jens Axboe   block: split tag ...
78
79
80
81
   *	queue in function.
   **/
  void blk_queue_free_tags(struct request_queue *q)
  {
aa94b5371   Jens Axboe   block: adjust tag...
82
  	queue_flag_clear_unlocked(QUEUE_FLAG_QUEUED, q);
8324aa91d   Jens Axboe   block: split tag ...
83
  }
8324aa91d   Jens Axboe   block: split tag ...
84
85
86
87
88
89
90
91
92
93
94
95
96
  EXPORT_SYMBOL(blk_queue_free_tags);
  
  static int
  init_tag_map(struct request_queue *q, struct blk_queue_tag *tags, int depth)
  {
  	struct request **tag_index;
  	unsigned long *tag_map;
  	int nr_ulongs;
  
  	if (q && depth > q->nr_requests * 2) {
  		depth = q->nr_requests * 2;
  		printk(KERN_ERR "%s: adjusted depth to %d
  ",
24c03d47d   Harvey Harrison   block: remove rem...
97
  		       __func__, depth);
8324aa91d   Jens Axboe   block: split tag ...
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  	}
  
  	tag_index = kzalloc(depth * sizeof(struct request *), GFP_ATOMIC);
  	if (!tag_index)
  		goto fail;
  
  	nr_ulongs = ALIGN(depth, BITS_PER_LONG) / BITS_PER_LONG;
  	tag_map = kzalloc(nr_ulongs * sizeof(unsigned long), GFP_ATOMIC);
  	if (!tag_map)
  		goto fail;
  
  	tags->real_max_depth = depth;
  	tags->max_depth = depth;
  	tags->tag_index = tag_index;
  	tags->tag_map = tag_map;
  
  	return 0;
  fail:
  	kfree(tag_index);
  	return -ENOMEM;
  }
  
  static struct blk_queue_tag *__blk_queue_init_tags(struct request_queue *q,
ee1b6f7af   Shaohua Li   block: support di...
121
  						int depth, int alloc_policy)
8324aa91d   Jens Axboe   block: split tag ...
122
123
124
125
126
127
128
129
130
  {
  	struct blk_queue_tag *tags;
  
  	tags = kmalloc(sizeof(struct blk_queue_tag), GFP_ATOMIC);
  	if (!tags)
  		goto fail;
  
  	if (init_tag_map(q, tags, depth))
  		goto fail;
8324aa91d   Jens Axboe   block: split tag ...
131
  	atomic_set(&tags->refcnt, 1);
ee1b6f7af   Shaohua Li   block: support di...
132
133
  	tags->alloc_policy = alloc_policy;
  	tags->next_tag = 0;
8324aa91d   Jens Axboe   block: split tag ...
134
135
136
137
138
139
140
141
142
  	return tags;
  fail:
  	kfree(tags);
  	return NULL;
  }
  
  /**
   * blk_init_tags - initialize the tag info for an external tag map
   * @depth:	the maximum queue depth supported
ee1b6f7af   Shaohua Li   block: support di...
143
   * @alloc_policy: tag allocation policy
8324aa91d   Jens Axboe   block: split tag ...
144
   **/
ee1b6f7af   Shaohua Li   block: support di...
145
  struct blk_queue_tag *blk_init_tags(int depth, int alloc_policy)
8324aa91d   Jens Axboe   block: split tag ...
146
  {
ee1b6f7af   Shaohua Li   block: support di...
147
  	return __blk_queue_init_tags(NULL, depth, alloc_policy);
8324aa91d   Jens Axboe   block: split tag ...
148
149
150
151
152
153
154
155
  }
  EXPORT_SYMBOL(blk_init_tags);
  
  /**
   * blk_queue_init_tags - initialize the queue tag info
   * @q:  the request queue for the device
   * @depth:  the maximum queue depth supported
   * @tags: the tag to use
ee1b6f7af   Shaohua Li   block: support di...
156
   * @alloc_policy: tag allocation policy
aa94b5371   Jens Axboe   block: adjust tag...
157
158
159
   *
   * Queue lock must be held here if the function is called to resize an
   * existing map.
8324aa91d   Jens Axboe   block: split tag ...
160
161
   **/
  int blk_queue_init_tags(struct request_queue *q, int depth,
ee1b6f7af   Shaohua Li   block: support di...
162
  			struct blk_queue_tag *tags, int alloc_policy)
8324aa91d   Jens Axboe   block: split tag ...
163
164
165
166
167
168
  {
  	int rc;
  
  	BUG_ON(tags && q->queue_tags && tags != q->queue_tags);
  
  	if (!tags && !q->queue_tags) {
ee1b6f7af   Shaohua Li   block: support di...
169
  		tags = __blk_queue_init_tags(q, depth, alloc_policy);
8324aa91d   Jens Axboe   block: split tag ...
170
171
  
  		if (!tags)
d41570b74   Peter Senna Tschudin   block/blk-tag.c: ...
172
  			return -ENOMEM;
8324aa91d   Jens Axboe   block: split tag ...
173
  	} else if (q->queue_tags) {
6728cb0e6   Jens Axboe   block: make core ...
174
175
  		rc = blk_queue_resize_tags(q, depth);
  		if (rc)
8324aa91d   Jens Axboe   block: split tag ...
176
  			return rc;
75ad23bc0   Nick Piggin   block: make queue...
177
  		queue_flag_set(QUEUE_FLAG_QUEUED, q);
8324aa91d   Jens Axboe   block: split tag ...
178
179
180
181
182
183
184
185
  		return 0;
  	} else
  		atomic_inc(&tags->refcnt);
  
  	/*
  	 * assign it, all done
  	 */
  	q->queue_tags = tags;
aa94b5371   Jens Axboe   block: adjust tag...
186
  	queue_flag_set_unlocked(QUEUE_FLAG_QUEUED, q);
8324aa91d   Jens Axboe   block: split tag ...
187
188
  	INIT_LIST_HEAD(&q->tag_busy_list);
  	return 0;
8324aa91d   Jens Axboe   block: split tag ...
189
  }
8324aa91d   Jens Axboe   block: split tag ...
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
  EXPORT_SYMBOL(blk_queue_init_tags);
  
  /**
   * blk_queue_resize_tags - change the queueing depth
   * @q:  the request queue for the device
   * @new_depth: the new max command queueing depth
   *
   *  Notes:
   *    Must be called with the queue lock held.
   **/
  int blk_queue_resize_tags(struct request_queue *q, int new_depth)
  {
  	struct blk_queue_tag *bqt = q->queue_tags;
  	struct request **tag_index;
  	unsigned long *tag_map;
  	int max_depth, nr_ulongs;
  
  	if (!bqt)
  		return -ENXIO;
  
  	/*
  	 * if we already have large enough real_max_depth.  just
  	 * adjust max_depth.  *NOTE* as requests with tag value
  	 * between new_depth and real_max_depth can be in-flight, tag
  	 * map can not be shrunk blindly here.
  	 */
  	if (new_depth <= bqt->real_max_depth) {
  		bqt->max_depth = new_depth;
  		return 0;
  	}
  
  	/*
  	 * Currently cannot replace a shared tag map with a new
  	 * one, so error out if this is the case
  	 */
  	if (atomic_read(&bqt->refcnt) != 1)
  		return -EBUSY;
  
  	/*
  	 * save the old state info, so we can copy it back
  	 */
  	tag_index = bqt->tag_index;
  	tag_map = bqt->tag_map;
  	max_depth = bqt->real_max_depth;
  
  	if (init_tag_map(q, bqt, new_depth))
  		return -ENOMEM;
  
  	memcpy(bqt->tag_index, tag_index, max_depth * sizeof(struct request *));
  	nr_ulongs = ALIGN(max_depth, BITS_PER_LONG) / BITS_PER_LONG;
  	memcpy(bqt->tag_map, tag_map, nr_ulongs * sizeof(unsigned long));
  
  	kfree(tag_index);
  	kfree(tag_map);
  	return 0;
  }
8324aa91d   Jens Axboe   block: split tag ...
246
247
248
249
250
251
252
253
  EXPORT_SYMBOL(blk_queue_resize_tags);
  
  /**
   * blk_queue_end_tag - end tag operations for a request
   * @q:  the request queue for the device
   * @rq: the request that has completed
   *
   *  Description:
710027a48   Randy Dunlap   Add some block/ s...
254
   *    Typically called when end_that_request_first() returns %0, meaning
8324aa91d   Jens Axboe   block: split tag ...
255
256
257
258
259
260
261
262
263
264
   *    all transfers have been done for a request. It's important to call
   *    this function before end_that_request_last(), as that will put the
   *    request back on the free list thus corrupting the internal tag list.
   *
   *  Notes:
   *   queue lock must be held.
   **/
  void blk_queue_end_tag(struct request_queue *q, struct request *rq)
  {
  	struct blk_queue_tag *bqt = q->queue_tags;
f2b20d436   Dan Williams   block: fix blk_qu...
265
  	unsigned tag = rq->tag; /* negative tags invalid */
8324aa91d   Jens Axboe   block: split tag ...
266

f2b20d436   Dan Williams   block: fix blk_qu...
267
  	BUG_ON(tag >= bqt->real_max_depth);
8324aa91d   Jens Axboe   block: split tag ...
268
269
270
271
272
273
274
275
  
  	list_del_init(&rq->queuelist);
  	rq->cmd_flags &= ~REQ_QUEUED;
  	rq->tag = -1;
  
  	if (unlikely(bqt->tag_index[tag] == NULL))
  		printk(KERN_ERR "%s: tag %d is missing
  ",
24c03d47d   Harvey Harrison   block: remove rem...
276
  		       __func__, tag);
8324aa91d   Jens Axboe   block: split tag ...
277
278
279
280
281
282
  
  	bqt->tag_index[tag] = NULL;
  
  	if (unlikely(!test_bit(tag, bqt->tag_map))) {
  		printk(KERN_ERR "%s: attempt to clear non-busy tag (%d)
  ",
24c03d47d   Harvey Harrison   block: remove rem...
283
  		       __func__, tag);
8324aa91d   Jens Axboe   block: split tag ...
284
285
286
287
288
289
290
  		return;
  	}
  	/*
  	 * The tag_map bit acts as a lock for tag_index[bit], so we need
  	 * unlock memory barrier semantics.
  	 */
  	clear_bit_unlock(tag, bqt->tag_map);
8324aa91d   Jens Axboe   block: split tag ...
291
  }
8324aa91d   Jens Axboe   block: split tag ...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
  EXPORT_SYMBOL(blk_queue_end_tag);
  
  /**
   * blk_queue_start_tag - find a free tag and assign it
   * @q:  the request queue for the device
   * @rq:  the block request that needs tagging
   *
   *  Description:
   *    This can either be used as a stand-alone helper, or possibly be
   *    assigned as the queue &prep_rq_fn (in which case &struct request
   *    automagically gets a tag assigned). Note that this function
   *    assumes that any type of request can be queued! if this is not
   *    true for your device, you must check the request type before
   *    calling this function.  The request will also be removed from
   *    the request queue, so it's the drivers responsibility to readd
   *    it if it should need to be restarted for some reason.
   *
   *  Notes:
   *   queue lock must be held.
   **/
  int blk_queue_start_tag(struct request_queue *q, struct request *rq)
  {
  	struct blk_queue_tag *bqt = q->queue_tags;
0a7ae2ff0   Jens Axboe   block: change the...
315
  	unsigned max_depth;
8324aa91d   Jens Axboe   block: split tag ...
316
317
318
  	int tag;
  
  	if (unlikely((rq->cmd_flags & REQ_QUEUED))) {
6728cb0e6   Jens Axboe   block: make core ...
319
  		printk(KERN_ERR
8324aa91d   Jens Axboe   block: split tag ...
320
  		       "%s: request %p for device [%s] already tagged %d",
24c03d47d   Harvey Harrison   block: remove rem...
321
  		       __func__, rq,
8324aa91d   Jens Axboe   block: split tag ...
322
323
324
325
326
327
328
  		       rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->tag);
  		BUG();
  	}
  
  	/*
  	 * Protect against shared tag maps, as we may not have exclusive
  	 * access to the tag map.
e3ba9ae58   Jens Axboe   block: reserve so...
329
330
331
  	 *
  	 * We reserve a few tags just for sync IO, since we don't want
  	 * to starve sync IO on behalf of flooding async IO.
8324aa91d   Jens Axboe   block: split tag ...
332
  	 */
e3ba9ae58   Jens Axboe   block: reserve so...
333
  	max_depth = bqt->max_depth;
0a7ae2ff0   Jens Axboe   block: change the...
334
  	if (!rq_is_sync(rq) && max_depth > 1) {
a6b3f7614   Jan Kara   block: Reserve on...
335
336
  		switch (max_depth) {
  		case 2:
0a7ae2ff0   Jens Axboe   block: change the...
337
  			max_depth = 1;
a6b3f7614   Jan Kara   block: Reserve on...
338
339
340
341
342
343
344
  			break;
  		case 3:
  			max_depth = 2;
  			break;
  		default:
  			max_depth -= 2;
  		}
1b59dd511   Jens Axboe   block: use proper...
345
  		if (q->in_flight[BLK_RW_ASYNC] > max_depth)
0a7ae2ff0   Jens Axboe   block: change the...
346
347
  			return 1;
  	}
e3ba9ae58   Jens Axboe   block: reserve so...
348

8324aa91d   Jens Axboe   block: split tag ...
349
  	do {
ee1b6f7af   Shaohua Li   block: support di...
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
  		if (bqt->alloc_policy == BLK_TAG_ALLOC_FIFO) {
  			tag = find_first_zero_bit(bqt->tag_map, max_depth);
  			if (tag >= max_depth)
  				return 1;
  		} else {
  			int start = bqt->next_tag;
  			int size = min_t(int, bqt->max_depth, max_depth + start);
  			tag = find_next_zero_bit(bqt->tag_map, size, start);
  			if (tag >= size && start + size > bqt->max_depth) {
  				size = start + size - bqt->max_depth;
  				tag = find_first_zero_bit(bqt->tag_map, size);
  			}
  			if (tag >= size)
  				return 1;
  		}
8324aa91d   Jens Axboe   block: split tag ...
365
366
367
368
369
370
  
  	} while (test_and_set_bit_lock(tag, bqt->tag_map));
  	/*
  	 * We need lock ordering semantics given by test_and_set_bit_lock.
  	 * See blk_queue_end_tag for details.
  	 */
ee1b6f7af   Shaohua Li   block: support di...
371
  	bqt->next_tag = (tag + 1) % bqt->max_depth;
8324aa91d   Jens Axboe   block: split tag ...
372
373
374
  	rq->cmd_flags |= REQ_QUEUED;
  	rq->tag = tag;
  	bqt->tag_index[tag] = rq;
9934c8c04   Tejun Heo   block: implement ...
375
  	blk_start_request(rq);
8324aa91d   Jens Axboe   block: split tag ...
376
  	list_add(&rq->queuelist, &q->tag_busy_list);
8324aa91d   Jens Axboe   block: split tag ...
377
378
  	return 0;
  }
8324aa91d   Jens Axboe   block: split tag ...
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
  EXPORT_SYMBOL(blk_queue_start_tag);
  
  /**
   * blk_queue_invalidate_tags - invalidate all pending tags
   * @q:  the request queue for the device
   *
   *  Description:
   *   Hardware conditions may dictate a need to stop all pending requests.
   *   In this case, we will safely clear the block side of the tag queue and
   *   readd all requests to the request queue in the right order.
   *
   *  Notes:
   *   queue lock must be held.
   **/
  void blk_queue_invalidate_tags(struct request_queue *q)
  {
  	struct list_head *tmp, *n;
  
  	list_for_each_safe(tmp, n, &q->tag_busy_list)
  		blk_requeue_request(q, list_entry_rq(tmp));
  }
8324aa91d   Jens Axboe   block: split tag ...
400
  EXPORT_SYMBOL(blk_queue_invalidate_tags);