blk-tag.c 10 KB

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