dm-rq.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * Copyright (C) 2016 Red Hat, Inc. All rights reserved.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm-core.h"
  7. #include "dm-rq.h"
  8. #include <linux/blk-mq.h>
  9. #define DM_MSG_PREFIX "core-rq"
  10. /*
  11. * One of these is allocated per request.
  12. */
  13. struct dm_rq_target_io {
  14. struct mapped_device *md;
  15. struct dm_target *ti;
  16. struct request *orig, *clone;
  17. struct kthread_work work;
  18. blk_status_t error;
  19. union map_info info;
  20. struct dm_stats_aux stats_aux;
  21. unsigned long duration_jiffies;
  22. unsigned int n_sectors;
  23. unsigned int completed;
  24. };
  25. #define DM_MQ_NR_HW_QUEUES 1
  26. #define DM_MQ_QUEUE_DEPTH 2048
  27. static unsigned int dm_mq_nr_hw_queues = DM_MQ_NR_HW_QUEUES;
  28. static unsigned int dm_mq_queue_depth = DM_MQ_QUEUE_DEPTH;
  29. /*
  30. * Request-based DM's mempools' reserved IOs set by the user.
  31. */
  32. #define RESERVED_REQUEST_BASED_IOS 256
  33. static unsigned int reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
  34. unsigned int dm_get_reserved_rq_based_ios(void)
  35. {
  36. return __dm_get_module_param(&reserved_rq_based_ios,
  37. RESERVED_REQUEST_BASED_IOS, DM_RESERVED_MAX_IOS);
  38. }
  39. static unsigned int dm_get_blk_mq_nr_hw_queues(void)
  40. {
  41. return __dm_get_module_param(&dm_mq_nr_hw_queues, 1, 32);
  42. }
  43. static unsigned int dm_get_blk_mq_queue_depth(void)
  44. {
  45. return __dm_get_module_param(&dm_mq_queue_depth,
  46. DM_MQ_QUEUE_DEPTH, BLK_MQ_MAX_DEPTH);
  47. }
  48. int dm_request_based(struct mapped_device *md)
  49. {
  50. return queue_is_mq(md->queue);
  51. }
  52. void dm_start_queue(struct request_queue *q)
  53. {
  54. blk_mq_unquiesce_queue(q);
  55. blk_mq_kick_requeue_list(q);
  56. }
  57. void dm_stop_queue(struct request_queue *q)
  58. {
  59. blk_mq_quiesce_queue(q);
  60. }
  61. /*
  62. * Partial completion handling for request-based dm
  63. */
  64. static void end_clone_bio(struct bio *clone)
  65. {
  66. struct dm_rq_clone_bio_info *info =
  67. container_of(clone, struct dm_rq_clone_bio_info, clone);
  68. struct dm_rq_target_io *tio = info->tio;
  69. unsigned int nr_bytes = info->orig->bi_iter.bi_size;
  70. blk_status_t error = clone->bi_status;
  71. bool is_last = !clone->bi_next;
  72. bio_put(clone);
  73. if (tio->error)
  74. /*
  75. * An error has already been detected on the request.
  76. * Once error occurred, just let clone->end_io() handle
  77. * the remainder.
  78. */
  79. return;
  80. else if (error) {
  81. /*
  82. * Don't notice the error to the upper layer yet.
  83. * The error handling decision is made by the target driver,
  84. * when the request is completed.
  85. */
  86. tio->error = error;
  87. goto exit;
  88. }
  89. /*
  90. * I/O for the bio successfully completed.
  91. * Notice the data completion to the upper layer.
  92. */
  93. tio->completed += nr_bytes;
  94. /*
  95. * Update the original request.
  96. * Do not use blk_mq_end_request() here, because it may complete
  97. * the original request before the clone, and break the ordering.
  98. */
  99. if (is_last)
  100. exit:
  101. blk_update_request(tio->orig, BLK_STS_OK, tio->completed);
  102. }
  103. static struct dm_rq_target_io *tio_from_request(struct request *rq)
  104. {
  105. return blk_mq_rq_to_pdu(rq);
  106. }
  107. static void rq_end_stats(struct mapped_device *md, struct request *orig)
  108. {
  109. if (unlikely(dm_stats_used(&md->stats))) {
  110. struct dm_rq_target_io *tio = tio_from_request(orig);
  111. tio->duration_jiffies = jiffies - tio->duration_jiffies;
  112. dm_stats_account_io(&md->stats, rq_data_dir(orig),
  113. blk_rq_pos(orig), tio->n_sectors, true,
  114. tio->duration_jiffies, &tio->stats_aux);
  115. }
  116. }
  117. /*
  118. * Don't touch any member of the md after calling this function because
  119. * the md may be freed in dm_put() at the end of this function.
  120. * Or do dm_get() before calling this function and dm_put() later.
  121. */
  122. static void rq_completed(struct mapped_device *md)
  123. {
  124. /*
  125. * dm_put() must be at the end of this function. See the comment above
  126. */
  127. dm_put(md);
  128. }
  129. /*
  130. * Complete the clone and the original request.
  131. * Must be called without clone's queue lock held,
  132. * see end_clone_request() for more details.
  133. */
  134. static void dm_end_request(struct request *clone, blk_status_t error)
  135. {
  136. struct dm_rq_target_io *tio = clone->end_io_data;
  137. struct mapped_device *md = tio->md;
  138. struct request *rq = tio->orig;
  139. blk_rq_unprep_clone(clone);
  140. tio->ti->type->release_clone_rq(clone, NULL);
  141. rq_end_stats(md, rq);
  142. blk_mq_end_request(rq, error);
  143. rq_completed(md);
  144. }
  145. static void __dm_mq_kick_requeue_list(struct request_queue *q, unsigned long msecs)
  146. {
  147. blk_mq_delay_kick_requeue_list(q, msecs);
  148. }
  149. void dm_mq_kick_requeue_list(struct mapped_device *md)
  150. {
  151. __dm_mq_kick_requeue_list(md->queue, 0);
  152. }
  153. EXPORT_SYMBOL(dm_mq_kick_requeue_list);
  154. static void dm_mq_delay_requeue_request(struct request *rq, unsigned long msecs)
  155. {
  156. blk_mq_requeue_request(rq, false);
  157. __dm_mq_kick_requeue_list(rq->q, msecs);
  158. }
  159. static void dm_requeue_original_request(struct dm_rq_target_io *tio, bool delay_requeue)
  160. {
  161. struct mapped_device *md = tio->md;
  162. struct request *rq = tio->orig;
  163. unsigned long delay_ms = delay_requeue ? 100 : 0;
  164. rq_end_stats(md, rq);
  165. if (tio->clone) {
  166. blk_rq_unprep_clone(tio->clone);
  167. tio->ti->type->release_clone_rq(tio->clone, NULL);
  168. }
  169. dm_mq_delay_requeue_request(rq, delay_ms);
  170. rq_completed(md);
  171. }
  172. static void dm_done(struct request *clone, blk_status_t error, bool mapped)
  173. {
  174. int r = DM_ENDIO_DONE;
  175. struct dm_rq_target_io *tio = clone->end_io_data;
  176. dm_request_endio_fn rq_end_io = NULL;
  177. if (tio->ti) {
  178. rq_end_io = tio->ti->type->rq_end_io;
  179. if (mapped && rq_end_io)
  180. r = rq_end_io(tio->ti, clone, error, &tio->info);
  181. }
  182. if (unlikely(error == BLK_STS_TARGET)) {
  183. if (req_op(clone) == REQ_OP_DISCARD &&
  184. !clone->q->limits.max_discard_sectors)
  185. disable_discard(tio->md);
  186. else if (req_op(clone) == REQ_OP_WRITE_ZEROES &&
  187. !clone->q->limits.max_write_zeroes_sectors)
  188. disable_write_zeroes(tio->md);
  189. }
  190. switch (r) {
  191. case DM_ENDIO_DONE:
  192. /* The target wants to complete the I/O */
  193. dm_end_request(clone, error);
  194. break;
  195. case DM_ENDIO_INCOMPLETE:
  196. /* The target will handle the I/O */
  197. return;
  198. case DM_ENDIO_REQUEUE:
  199. /* The target wants to requeue the I/O */
  200. dm_requeue_original_request(tio, false);
  201. break;
  202. case DM_ENDIO_DELAY_REQUEUE:
  203. /* The target wants to requeue the I/O after a delay */
  204. dm_requeue_original_request(tio, true);
  205. break;
  206. default:
  207. DMCRIT("unimplemented target endio return value: %d", r);
  208. BUG();
  209. }
  210. }
  211. /*
  212. * Request completion handler for request-based dm
  213. */
  214. static void dm_softirq_done(struct request *rq)
  215. {
  216. bool mapped = true;
  217. struct dm_rq_target_io *tio = tio_from_request(rq);
  218. struct request *clone = tio->clone;
  219. if (!clone) {
  220. struct mapped_device *md = tio->md;
  221. rq_end_stats(md, rq);
  222. blk_mq_end_request(rq, tio->error);
  223. rq_completed(md);
  224. return;
  225. }
  226. if (rq->rq_flags & RQF_FAILED)
  227. mapped = false;
  228. dm_done(clone, tio->error, mapped);
  229. }
  230. /*
  231. * Complete the clone and the original request with the error status
  232. * through softirq context.
  233. */
  234. static void dm_complete_request(struct request *rq, blk_status_t error)
  235. {
  236. struct dm_rq_target_io *tio = tio_from_request(rq);
  237. tio->error = error;
  238. if (likely(!blk_should_fake_timeout(rq->q)))
  239. blk_mq_complete_request(rq);
  240. }
  241. /*
  242. * Complete the not-mapped clone and the original request with the error status
  243. * through softirq context.
  244. * Target's rq_end_io() function isn't called.
  245. * This may be used when the target's clone_and_map_rq() function fails.
  246. */
  247. static void dm_kill_unmapped_request(struct request *rq, blk_status_t error)
  248. {
  249. rq->rq_flags |= RQF_FAILED;
  250. dm_complete_request(rq, error);
  251. }
  252. static enum rq_end_io_ret end_clone_request(struct request *clone,
  253. blk_status_t error)
  254. {
  255. struct dm_rq_target_io *tio = clone->end_io_data;
  256. dm_complete_request(tio->orig, error);
  257. return RQ_END_IO_NONE;
  258. }
  259. static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
  260. void *data)
  261. {
  262. struct dm_rq_target_io *tio = data;
  263. struct dm_rq_clone_bio_info *info =
  264. container_of(bio, struct dm_rq_clone_bio_info, clone);
  265. info->orig = bio_orig;
  266. info->tio = tio;
  267. bio->bi_end_io = end_clone_bio;
  268. return 0;
  269. }
  270. static int setup_clone(struct request *clone, struct request *rq,
  271. struct dm_rq_target_io *tio, gfp_t gfp_mask)
  272. {
  273. int r;
  274. r = blk_rq_prep_clone(clone, rq, &tio->md->mempools->bs, gfp_mask,
  275. dm_rq_bio_constructor, tio);
  276. if (r)
  277. return r;
  278. clone->end_io = end_clone_request;
  279. clone->end_io_data = tio;
  280. tio->clone = clone;
  281. return 0;
  282. }
  283. static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
  284. struct mapped_device *md)
  285. {
  286. tio->md = md;
  287. tio->ti = NULL;
  288. tio->clone = NULL;
  289. tio->orig = rq;
  290. tio->error = 0;
  291. tio->completed = 0;
  292. /*
  293. * Avoid initializing info for blk-mq; it passes
  294. * target-specific data through info.ptr
  295. * (see: dm_mq_init_request)
  296. */
  297. if (!md->init_tio_pdu)
  298. memset(&tio->info, 0, sizeof(tio->info));
  299. }
  300. /*
  301. * Returns:
  302. * DM_MAPIO_* : the request has been processed as indicated
  303. * DM_MAPIO_REQUEUE : the original request needs to be immediately requeued
  304. * < 0 : the request was completed due to failure
  305. */
  306. static int map_request(struct dm_rq_target_io *tio)
  307. {
  308. int r;
  309. struct dm_target *ti = tio->ti;
  310. struct mapped_device *md = tio->md;
  311. struct request *rq = tio->orig;
  312. struct request *clone = NULL;
  313. blk_status_t ret;
  314. r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
  315. switch (r) {
  316. case DM_MAPIO_SUBMITTED:
  317. /* The target has taken the I/O to submit by itself later */
  318. break;
  319. case DM_MAPIO_REMAPPED:
  320. if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
  321. /* -ENOMEM */
  322. ti->type->release_clone_rq(clone, &tio->info);
  323. return DM_MAPIO_REQUEUE;
  324. }
  325. /* The target has remapped the I/O so dispatch it */
  326. trace_block_rq_remap(clone, disk_devt(dm_disk(md)),
  327. blk_rq_pos(rq));
  328. ret = blk_insert_cloned_request(clone);
  329. switch (ret) {
  330. case BLK_STS_OK:
  331. break;
  332. case BLK_STS_RESOURCE:
  333. case BLK_STS_DEV_RESOURCE:
  334. blk_rq_unprep_clone(clone);
  335. blk_mq_cleanup_rq(clone);
  336. tio->ti->type->release_clone_rq(clone, &tio->info);
  337. tio->clone = NULL;
  338. return DM_MAPIO_REQUEUE;
  339. default:
  340. /* must complete clone in terms of original request */
  341. dm_complete_request(rq, ret);
  342. }
  343. break;
  344. case DM_MAPIO_REQUEUE:
  345. /* The target wants to requeue the I/O */
  346. break;
  347. case DM_MAPIO_DELAY_REQUEUE:
  348. /* The target wants to requeue the I/O after a delay */
  349. dm_requeue_original_request(tio, true);
  350. break;
  351. case DM_MAPIO_KILL:
  352. /* The target wants to complete the I/O */
  353. dm_kill_unmapped_request(rq, BLK_STS_IOERR);
  354. break;
  355. default:
  356. DMCRIT("unimplemented target map return value: %d", r);
  357. BUG();
  358. }
  359. return r;
  360. }
  361. /* DEPRECATED: previously used for request-based merge heuristic in dm_request_fn() */
  362. ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
  363. {
  364. return sprintf(buf, "%u\n", 0);
  365. }
  366. ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
  367. const char *buf, size_t count)
  368. {
  369. return count;
  370. }
  371. static void dm_start_request(struct mapped_device *md, struct request *orig)
  372. {
  373. blk_mq_start_request(orig);
  374. if (unlikely(dm_stats_used(&md->stats))) {
  375. struct dm_rq_target_io *tio = tio_from_request(orig);
  376. tio->duration_jiffies = jiffies;
  377. tio->n_sectors = blk_rq_sectors(orig);
  378. dm_stats_account_io(&md->stats, rq_data_dir(orig),
  379. blk_rq_pos(orig), tio->n_sectors, false, 0,
  380. &tio->stats_aux);
  381. }
  382. /*
  383. * Hold the md reference here for the in-flight I/O.
  384. * We can't rely on the reference count by device opener,
  385. * because the device may be closed during the request completion
  386. * when all bios are completed.
  387. * See the comment in rq_completed() too.
  388. */
  389. dm_get(md);
  390. }
  391. static int dm_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
  392. unsigned int hctx_idx, unsigned int numa_node)
  393. {
  394. struct mapped_device *md = set->driver_data;
  395. struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
  396. /*
  397. * Must initialize md member of tio, otherwise it won't
  398. * be available in dm_mq_queue_rq.
  399. */
  400. tio->md = md;
  401. if (md->init_tio_pdu) {
  402. /* target-specific per-io data is immediately after the tio */
  403. tio->info.ptr = tio + 1;
  404. }
  405. return 0;
  406. }
  407. static blk_status_t dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
  408. const struct blk_mq_queue_data *bd)
  409. {
  410. struct request *rq = bd->rq;
  411. struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
  412. struct mapped_device *md = tio->md;
  413. struct dm_target *ti = md->immutable_target;
  414. /*
  415. * blk-mq's unquiesce may come from outside events, such as
  416. * elevator switch, updating nr_requests or others, and request may
  417. * come during suspend, so simply ask for blk-mq to requeue it.
  418. */
  419. if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)))
  420. return BLK_STS_RESOURCE;
  421. if (unlikely(!ti)) {
  422. int srcu_idx;
  423. struct dm_table *map;
  424. map = dm_get_live_table(md, &srcu_idx);
  425. if (unlikely(!map)) {
  426. dm_put_live_table(md, srcu_idx);
  427. return BLK_STS_RESOURCE;
  428. }
  429. ti = dm_table_find_target(map, 0);
  430. dm_put_live_table(md, srcu_idx);
  431. }
  432. if (ti->type->busy && ti->type->busy(ti))
  433. return BLK_STS_RESOURCE;
  434. dm_start_request(md, rq);
  435. /* Init tio using md established in .init_request */
  436. init_tio(tio, rq, md);
  437. /*
  438. * Establish tio->ti before calling map_request().
  439. */
  440. tio->ti = ti;
  441. /* Direct call is fine since .queue_rq allows allocations */
  442. if (map_request(tio) == DM_MAPIO_REQUEUE) {
  443. /* Undo dm_start_request() before requeuing */
  444. rq_end_stats(md, rq);
  445. rq_completed(md);
  446. return BLK_STS_RESOURCE;
  447. }
  448. return BLK_STS_OK;
  449. }
  450. static const struct blk_mq_ops dm_mq_ops = {
  451. .queue_rq = dm_mq_queue_rq,
  452. .complete = dm_softirq_done,
  453. .init_request = dm_mq_init_request,
  454. };
  455. int dm_mq_init_request_queue(struct mapped_device *md, struct dm_table *t)
  456. {
  457. struct dm_target *immutable_tgt;
  458. int err;
  459. md->tag_set = kzalloc_node(sizeof(struct blk_mq_tag_set), GFP_KERNEL, md->numa_node_id);
  460. if (!md->tag_set)
  461. return -ENOMEM;
  462. md->tag_set->ops = &dm_mq_ops;
  463. md->tag_set->queue_depth = dm_get_blk_mq_queue_depth();
  464. md->tag_set->numa_node = md->numa_node_id;
  465. md->tag_set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_STACKING;
  466. md->tag_set->nr_hw_queues = dm_get_blk_mq_nr_hw_queues();
  467. md->tag_set->driver_data = md;
  468. md->tag_set->cmd_size = sizeof(struct dm_rq_target_io);
  469. immutable_tgt = dm_table_get_immutable_target(t);
  470. if (immutable_tgt && immutable_tgt->per_io_data_size) {
  471. /* any target-specific per-io data is immediately after the tio */
  472. md->tag_set->cmd_size += immutable_tgt->per_io_data_size;
  473. md->init_tio_pdu = true;
  474. }
  475. err = blk_mq_alloc_tag_set(md->tag_set);
  476. if (err)
  477. goto out_kfree_tag_set;
  478. err = blk_mq_init_allocated_queue(md->tag_set, md->queue);
  479. if (err)
  480. goto out_tag_set;
  481. return 0;
  482. out_tag_set:
  483. blk_mq_free_tag_set(md->tag_set);
  484. out_kfree_tag_set:
  485. kfree(md->tag_set);
  486. md->tag_set = NULL;
  487. return err;
  488. }
  489. void dm_mq_cleanup_mapped_device(struct mapped_device *md)
  490. {
  491. if (md->tag_set) {
  492. blk_mq_free_tag_set(md->tag_set);
  493. kfree(md->tag_set);
  494. md->tag_set = NULL;
  495. }
  496. }
  497. module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
  498. MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
  499. /* Unused, but preserved for userspace compatibility */
  500. static bool use_blk_mq = true;
  501. module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
  502. MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
  503. module_param(dm_mq_nr_hw_queues, uint, S_IRUGO | S_IWUSR);
  504. MODULE_PARM_DESC(dm_mq_nr_hw_queues, "Number of hardware queues for request-based dm-mq devices");
  505. module_param(dm_mq_queue_depth, uint, S_IRUGO | S_IWUSR);
  506. MODULE_PARM_DESC(dm_mq_queue_depth, "Queue depth for request-based dm-mq devices");