blk-tag.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Functions related to tagged command queuing
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/bio.h>
  7. #include <linux/blkdev.h>
  8. #include <linux/slab.h>
  9. #include "blk.h"
  10. /**
  11. * blk_queue_find_tag - find a request by its tag and queue
  12. * @q: The request queue for the device
  13. * @tag: The tag of the request
  14. *
  15. * Notes:
  16. * Should be used when a device returns a tag and you want to match
  17. * it with a request.
  18. *
  19. * no locks need be held.
  20. **/
  21. struct request *blk_queue_find_tag(struct request_queue *q, int tag)
  22. {
  23. return blk_map_queue_find_tag(q->queue_tags, tag);
  24. }
  25. EXPORT_SYMBOL(blk_queue_find_tag);
  26. /**
  27. * __blk_free_tags - release a given set of tag maintenance info
  28. * @bqt: the tag map to free
  29. *
  30. * Tries to free the specified @bqt. Returns true if it was
  31. * actually freed and false if there are still references using it
  32. */
  33. static int __blk_free_tags(struct blk_queue_tag *bqt)
  34. {
  35. int retval;
  36. retval = atomic_dec_and_test(&bqt->refcnt);
  37. if (retval) {
  38. BUG_ON(find_first_bit(bqt->tag_map, bqt->max_depth) <
  39. bqt->max_depth);
  40. kfree(bqt->tag_index);
  41. bqt->tag_index = NULL;
  42. kfree(bqt->tag_map);
  43. bqt->tag_map = NULL;
  44. kfree(bqt);
  45. }
  46. return retval;
  47. }
  48. /**
  49. * __blk_queue_free_tags - release tag maintenance info
  50. * @q: the request queue for the device
  51. *
  52. * Notes:
  53. * blk_cleanup_queue() will take care of calling this function, if tagging
  54. * has been used. So there's no need to call this directly.
  55. **/
  56. void __blk_queue_free_tags(struct request_queue *q)
  57. {
  58. struct blk_queue_tag *bqt = q->queue_tags;
  59. if (!bqt)
  60. return;
  61. __blk_free_tags(bqt);
  62. q->queue_tags = NULL;
  63. queue_flag_clear_unlocked(QUEUE_FLAG_QUEUED, q);
  64. }
  65. /**
  66. * blk_free_tags - release a given set of tag maintenance info
  67. * @bqt: the tag map to free
  68. *
  69. * For externally managed @bqt frees the map. Callers of this
  70. * function must guarantee to have released all the queues that
  71. * might have been using this tag map.
  72. */
  73. void blk_free_tags(struct blk_queue_tag *bqt)
  74. {
  75. if (unlikely(!__blk_free_tags(bqt)))
  76. BUG();
  77. }
  78. EXPORT_SYMBOL(blk_free_tags);
  79. /**
  80. * blk_queue_free_tags - release tag maintenance info
  81. * @q: the request queue for the device
  82. *
  83. * Notes:
  84. * This is used to disable tagged queuing to a device, yet leave
  85. * queue in function.
  86. **/
  87. void blk_queue_free_tags(struct request_queue *q)
  88. {
  89. queue_flag_clear_unlocked(QUEUE_FLAG_QUEUED, q);
  90. }
  91. EXPORT_SYMBOL(blk_queue_free_tags);
  92. static int
  93. init_tag_map(struct request_queue *q, struct blk_queue_tag *tags, int depth)
  94. {
  95. struct request **tag_index;
  96. unsigned long *tag_map;
  97. int nr_ulongs;
  98. if (q && depth > q->nr_requests * 2) {
  99. depth = q->nr_requests * 2;
  100. printk(KERN_ERR "%s: adjusted depth to %d\n",
  101. __func__, depth);
  102. }
  103. tag_index = kzalloc(depth * sizeof(struct request *), GFP_ATOMIC);
  104. if (!tag_index)
  105. goto fail;
  106. nr_ulongs = ALIGN(depth, BITS_PER_LONG) / BITS_PER_LONG;
  107. tag_map = kzalloc(nr_ulongs * sizeof(unsigned long), GFP_ATOMIC);
  108. if (!tag_map)
  109. goto fail;
  110. tags->real_max_depth = depth;
  111. tags->max_depth = depth;
  112. tags->tag_index = tag_index;
  113. tags->tag_map = tag_map;
  114. return 0;
  115. fail:
  116. kfree(tag_index);
  117. return -ENOMEM;
  118. }
  119. static struct blk_queue_tag *__blk_queue_init_tags(struct request_queue *q,
  120. int depth)
  121. {
  122. struct blk_queue_tag *tags;
  123. tags = kmalloc(sizeof(struct blk_queue_tag), GFP_ATOMIC);
  124. if (!tags)
  125. goto fail;
  126. if (init_tag_map(q, tags, depth))
  127. goto fail;
  128. atomic_set(&tags->refcnt, 1);
  129. return tags;
  130. fail:
  131. kfree(tags);
  132. return NULL;
  133. }
  134. /**
  135. * blk_init_tags - initialize the tag info for an external tag map
  136. * @depth: the maximum queue depth supported
  137. **/
  138. struct blk_queue_tag *blk_init_tags(int depth)
  139. {
  140. return __blk_queue_init_tags(NULL, depth);
  141. }
  142. EXPORT_SYMBOL(blk_init_tags);
  143. /**
  144. * blk_queue_init_tags - initialize the queue tag info
  145. * @q: the request queue for the device
  146. * @depth: the maximum queue depth supported
  147. * @tags: the tag to use
  148. *
  149. * Queue lock must be held here if the function is called to resize an
  150. * existing map.
  151. **/
  152. int blk_queue_init_tags(struct request_queue *q, int depth,
  153. struct blk_queue_tag *tags)
  154. {
  155. int rc;
  156. BUG_ON(tags && q->queue_tags && tags != q->queue_tags);
  157. if (!tags && !q->queue_tags) {
  158. tags = __blk_queue_init_tags(q, depth);
  159. if (!tags)
  160. goto fail;
  161. } else if (q->queue_tags) {
  162. rc = blk_queue_resize_tags(q, depth);
  163. if (rc)
  164. return rc;
  165. queue_flag_set(QUEUE_FLAG_QUEUED, q);
  166. return 0;
  167. } else
  168. atomic_inc(&tags->refcnt);
  169. /*
  170. * assign it, all done
  171. */
  172. q->queue_tags = tags;
  173. queue_flag_set_unlocked(QUEUE_FLAG_QUEUED, q);
  174. INIT_LIST_HEAD(&q->tag_busy_list);
  175. return 0;
  176. fail:
  177. kfree(tags);
  178. return -ENOMEM;
  179. }
  180. EXPORT_SYMBOL(blk_queue_init_tags);
  181. /**
  182. * blk_queue_resize_tags - change the queueing depth
  183. * @q: the request queue for the device
  184. * @new_depth: the new max command queueing depth
  185. *
  186. * Notes:
  187. * Must be called with the queue lock held.
  188. **/
  189. int blk_queue_resize_tags(struct request_queue *q, int new_depth)
  190. {
  191. struct blk_queue_tag *bqt = q->queue_tags;
  192. struct request **tag_index;
  193. unsigned long *tag_map;
  194. int max_depth, nr_ulongs;
  195. if (!bqt)
  196. return -ENXIO;
  197. /*
  198. * if we already have large enough real_max_depth. just
  199. * adjust max_depth. *NOTE* as requests with tag value
  200. * between new_depth and real_max_depth can be in-flight, tag
  201. * map can not be shrunk blindly here.
  202. */
  203. if (new_depth <= bqt->real_max_depth) {
  204. bqt->max_depth = new_depth;
  205. return 0;
  206. }
  207. /*
  208. * Currently cannot replace a shared tag map with a new
  209. * one, so error out if this is the case
  210. */
  211. if (atomic_read(&bqt->refcnt) != 1)
  212. return -EBUSY;
  213. /*
  214. * save the old state info, so we can copy it back
  215. */
  216. tag_index = bqt->tag_index;
  217. tag_map = bqt->tag_map;
  218. max_depth = bqt->real_max_depth;
  219. if (init_tag_map(q, bqt, new_depth))
  220. return -ENOMEM;
  221. memcpy(bqt->tag_index, tag_index, max_depth * sizeof(struct request *));
  222. nr_ulongs = ALIGN(max_depth, BITS_PER_LONG) / BITS_PER_LONG;
  223. memcpy(bqt->tag_map, tag_map, nr_ulongs * sizeof(unsigned long));
  224. kfree(tag_index);
  225. kfree(tag_map);
  226. return 0;
  227. }
  228. EXPORT_SYMBOL(blk_queue_resize_tags);
  229. /**
  230. * blk_queue_end_tag - end tag operations for a request
  231. * @q: the request queue for the device
  232. * @rq: the request that has completed
  233. *
  234. * Description:
  235. * Typically called when end_that_request_first() returns %0, meaning
  236. * all transfers have been done for a request. It's important to call
  237. * this function before end_that_request_last(), as that will put the
  238. * request back on the free list thus corrupting the internal tag list.
  239. *
  240. * Notes:
  241. * queue lock must be held.
  242. **/
  243. void blk_queue_end_tag(struct request_queue *q, struct request *rq)
  244. {
  245. struct blk_queue_tag *bqt = q->queue_tags;
  246. int tag = rq->tag;
  247. BUG_ON(tag == -1);
  248. if (unlikely(tag >= bqt->real_max_depth))
  249. /*
  250. * This can happen after tag depth has been reduced.
  251. * FIXME: how about a warning or info message here?
  252. */
  253. return;
  254. list_del_init(&rq->queuelist);
  255. rq->cmd_flags &= ~REQ_QUEUED;
  256. rq->tag = -1;
  257. if (unlikely(bqt->tag_index[tag] == NULL))
  258. printk(KERN_ERR "%s: tag %d is missing\n",
  259. __func__, tag);
  260. bqt->tag_index[tag] = NULL;
  261. if (unlikely(!test_bit(tag, bqt->tag_map))) {
  262. printk(KERN_ERR "%s: attempt to clear non-busy tag (%d)\n",
  263. __func__, tag);
  264. return;
  265. }
  266. /*
  267. * The tag_map bit acts as a lock for tag_index[bit], so we need
  268. * unlock memory barrier semantics.
  269. */
  270. clear_bit_unlock(tag, bqt->tag_map);
  271. }
  272. EXPORT_SYMBOL(blk_queue_end_tag);
  273. /**
  274. * blk_queue_start_tag - find a free tag and assign it
  275. * @q: the request queue for the device
  276. * @rq: the block request that needs tagging
  277. *
  278. * Description:
  279. * This can either be used as a stand-alone helper, or possibly be
  280. * assigned as the queue &prep_rq_fn (in which case &struct request
  281. * automagically gets a tag assigned). Note that this function
  282. * assumes that any type of request can be queued! if this is not
  283. * true for your device, you must check the request type before
  284. * calling this function. The request will also be removed from
  285. * the request queue, so it's the drivers responsibility to readd
  286. * it if it should need to be restarted for some reason.
  287. *
  288. * Notes:
  289. * queue lock must be held.
  290. **/
  291. int blk_queue_start_tag(struct request_queue *q, struct request *rq)
  292. {
  293. struct blk_queue_tag *bqt = q->queue_tags;
  294. unsigned max_depth;
  295. int tag;
  296. if (unlikely((rq->cmd_flags & REQ_QUEUED))) {
  297. printk(KERN_ERR
  298. "%s: request %p for device [%s] already tagged %d",
  299. __func__, rq,
  300. rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->tag);
  301. BUG();
  302. }
  303. /*
  304. * Protect against shared tag maps, as we may not have exclusive
  305. * access to the tag map.
  306. *
  307. * We reserve a few tags just for sync IO, since we don't want
  308. * to starve sync IO on behalf of flooding async IO.
  309. */
  310. max_depth = bqt->max_depth;
  311. if (!rq_is_sync(rq) && max_depth > 1) {
  312. max_depth -= 2;
  313. if (!max_depth)
  314. max_depth = 1;
  315. if (q->in_flight[BLK_RW_ASYNC] > max_depth)
  316. return 1;
  317. }
  318. do {
  319. tag = find_first_zero_bit(bqt->tag_map, max_depth);
  320. if (tag >= max_depth)
  321. return 1;
  322. } while (test_and_set_bit_lock(tag, bqt->tag_map));
  323. /*
  324. * We need lock ordering semantics given by test_and_set_bit_lock.
  325. * See blk_queue_end_tag for details.
  326. */
  327. rq->cmd_flags |= REQ_QUEUED;
  328. rq->tag = tag;
  329. bqt->tag_index[tag] = rq;
  330. blk_start_request(rq);
  331. list_add(&rq->queuelist, &q->tag_busy_list);
  332. return 0;
  333. }
  334. EXPORT_SYMBOL(blk_queue_start_tag);
  335. /**
  336. * blk_queue_invalidate_tags - invalidate all pending tags
  337. * @q: the request queue for the device
  338. *
  339. * Description:
  340. * Hardware conditions may dictate a need to stop all pending requests.
  341. * In this case, we will safely clear the block side of the tag queue and
  342. * readd all requests to the request queue in the right order.
  343. *
  344. * Notes:
  345. * queue lock must be held.
  346. **/
  347. void blk_queue_invalidate_tags(struct request_queue *q)
  348. {
  349. struct list_head *tmp, *n;
  350. list_for_each_safe(tmp, n, &q->tag_busy_list)
  351. blk_requeue_request(q, list_entry_rq(tmp));
  352. }
  353. EXPORT_SYMBOL(blk_queue_invalidate_tags);