blk-tag.c 10 KB

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