dm-rq.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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/elevator.h> /* for rq_end_sector() */
  9. #include <linux/blk-mq.h>
  10. #define DM_MSG_PREFIX "core-rq"
  11. #define DM_MQ_NR_HW_QUEUES 1
  12. #define DM_MQ_QUEUE_DEPTH 2048
  13. static unsigned dm_mq_nr_hw_queues = DM_MQ_NR_HW_QUEUES;
  14. static unsigned dm_mq_queue_depth = DM_MQ_QUEUE_DEPTH;
  15. /*
  16. * Request-based DM's mempools' reserved IOs set by the user.
  17. */
  18. #define RESERVED_REQUEST_BASED_IOS 256
  19. static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
  20. static bool use_blk_mq = IS_ENABLED(CONFIG_DM_MQ_DEFAULT);
  21. bool dm_use_blk_mq_default(void)
  22. {
  23. return use_blk_mq;
  24. }
  25. bool dm_use_blk_mq(struct mapped_device *md)
  26. {
  27. return md->use_blk_mq;
  28. }
  29. EXPORT_SYMBOL_GPL(dm_use_blk_mq);
  30. unsigned dm_get_reserved_rq_based_ios(void)
  31. {
  32. return __dm_get_module_param(&reserved_rq_based_ios,
  33. RESERVED_REQUEST_BASED_IOS, DM_RESERVED_MAX_IOS);
  34. }
  35. EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
  36. static unsigned dm_get_blk_mq_nr_hw_queues(void)
  37. {
  38. return __dm_get_module_param(&dm_mq_nr_hw_queues, 1, 32);
  39. }
  40. static unsigned dm_get_blk_mq_queue_depth(void)
  41. {
  42. return __dm_get_module_param(&dm_mq_queue_depth,
  43. DM_MQ_QUEUE_DEPTH, BLK_MQ_MAX_DEPTH);
  44. }
  45. int dm_request_based(struct mapped_device *md)
  46. {
  47. return queue_is_rq_based(md->queue);
  48. }
  49. static void dm_old_start_queue(struct request_queue *q)
  50. {
  51. unsigned long flags;
  52. spin_lock_irqsave(q->queue_lock, flags);
  53. if (blk_queue_stopped(q))
  54. blk_start_queue(q);
  55. spin_unlock_irqrestore(q->queue_lock, flags);
  56. }
  57. static void dm_mq_start_queue(struct request_queue *q)
  58. {
  59. blk_mq_unquiesce_queue(q);
  60. blk_mq_kick_requeue_list(q);
  61. }
  62. void dm_start_queue(struct request_queue *q)
  63. {
  64. if (!q->mq_ops)
  65. dm_old_start_queue(q);
  66. else
  67. dm_mq_start_queue(q);
  68. }
  69. static void dm_old_stop_queue(struct request_queue *q)
  70. {
  71. unsigned long flags;
  72. spin_lock_irqsave(q->queue_lock, flags);
  73. if (!blk_queue_stopped(q))
  74. blk_stop_queue(q);
  75. spin_unlock_irqrestore(q->queue_lock, flags);
  76. }
  77. static void dm_mq_stop_queue(struct request_queue *q)
  78. {
  79. blk_mq_quiesce_queue(q);
  80. }
  81. void dm_stop_queue(struct request_queue *q)
  82. {
  83. if (!q->mq_ops)
  84. dm_old_stop_queue(q);
  85. else
  86. dm_mq_stop_queue(q);
  87. }
  88. /*
  89. * Partial completion handling for request-based dm
  90. */
  91. static void end_clone_bio(struct bio *clone)
  92. {
  93. struct dm_rq_clone_bio_info *info =
  94. container_of(clone, struct dm_rq_clone_bio_info, clone);
  95. struct dm_rq_target_io *tio = info->tio;
  96. unsigned int nr_bytes = info->orig->bi_iter.bi_size;
  97. blk_status_t error = clone->bi_status;
  98. bool is_last = !clone->bi_next;
  99. bio_put(clone);
  100. if (tio->error)
  101. /*
  102. * An error has already been detected on the request.
  103. * Once error occurred, just let clone->end_io() handle
  104. * the remainder.
  105. */
  106. return;
  107. else if (error) {
  108. /*
  109. * Don't notice the error to the upper layer yet.
  110. * The error handling decision is made by the target driver,
  111. * when the request is completed.
  112. */
  113. tio->error = error;
  114. goto exit;
  115. }
  116. /*
  117. * I/O for the bio successfully completed.
  118. * Notice the data completion to the upper layer.
  119. */
  120. tio->completed += nr_bytes;
  121. /*
  122. * Update the original request.
  123. * Do not use blk_end_request() here, because it may complete
  124. * the original request before the clone, and break the ordering.
  125. */
  126. if (is_last)
  127. exit:
  128. blk_update_request(tio->orig, BLK_STS_OK, tio->completed);
  129. }
  130. static struct dm_rq_target_io *tio_from_request(struct request *rq)
  131. {
  132. return blk_mq_rq_to_pdu(rq);
  133. }
  134. static void rq_end_stats(struct mapped_device *md, struct request *orig)
  135. {
  136. if (unlikely(dm_stats_used(&md->stats))) {
  137. struct dm_rq_target_io *tio = tio_from_request(orig);
  138. tio->duration_jiffies = jiffies - tio->duration_jiffies;
  139. dm_stats_account_io(&md->stats, rq_data_dir(orig),
  140. blk_rq_pos(orig), tio->n_sectors, true,
  141. tio->duration_jiffies, &tio->stats_aux);
  142. }
  143. }
  144. /*
  145. * Don't touch any member of the md after calling this function because
  146. * the md may be freed in dm_put() at the end of this function.
  147. * Or do dm_get() before calling this function and dm_put() later.
  148. */
  149. static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
  150. {
  151. struct request_queue *q = md->queue;
  152. unsigned long flags;
  153. atomic_dec(&md->pending[rw]);
  154. /* nudge anyone waiting on suspend queue */
  155. if (!md_in_flight(md))
  156. wake_up(&md->wait);
  157. /*
  158. * Run this off this callpath, as drivers could invoke end_io while
  159. * inside their request_fn (and holding the queue lock). Calling
  160. * back into ->request_fn() could deadlock attempting to grab the
  161. * queue lock again.
  162. */
  163. if (!q->mq_ops && run_queue) {
  164. spin_lock_irqsave(q->queue_lock, flags);
  165. blk_run_queue_async(q);
  166. spin_unlock_irqrestore(q->queue_lock, flags);
  167. }
  168. /*
  169. * dm_put() must be at the end of this function. See the comment above
  170. */
  171. dm_put(md);
  172. }
  173. /*
  174. * Complete the clone and the original request.
  175. * Must be called without clone's queue lock held,
  176. * see end_clone_request() for more details.
  177. */
  178. static void dm_end_request(struct request *clone, blk_status_t error)
  179. {
  180. int rw = rq_data_dir(clone);
  181. struct dm_rq_target_io *tio = clone->end_io_data;
  182. struct mapped_device *md = tio->md;
  183. struct request *rq = tio->orig;
  184. blk_rq_unprep_clone(clone);
  185. tio->ti->type->release_clone_rq(clone, NULL);
  186. rq_end_stats(md, rq);
  187. if (!rq->q->mq_ops)
  188. blk_end_request_all(rq, error);
  189. else
  190. blk_mq_end_request(rq, error);
  191. rq_completed(md, rw, true);
  192. }
  193. /*
  194. * Requeue the original request of a clone.
  195. */
  196. static void dm_old_requeue_request(struct request *rq, unsigned long delay_ms)
  197. {
  198. struct request_queue *q = rq->q;
  199. unsigned long flags;
  200. spin_lock_irqsave(q->queue_lock, flags);
  201. blk_requeue_request(q, rq);
  202. blk_delay_queue(q, delay_ms);
  203. spin_unlock_irqrestore(q->queue_lock, flags);
  204. }
  205. static void __dm_mq_kick_requeue_list(struct request_queue *q, unsigned long msecs)
  206. {
  207. blk_mq_delay_kick_requeue_list(q, msecs);
  208. }
  209. void dm_mq_kick_requeue_list(struct mapped_device *md)
  210. {
  211. __dm_mq_kick_requeue_list(dm_get_md_queue(md), 0);
  212. }
  213. EXPORT_SYMBOL(dm_mq_kick_requeue_list);
  214. static void dm_mq_delay_requeue_request(struct request *rq, unsigned long msecs)
  215. {
  216. blk_mq_requeue_request(rq, false);
  217. __dm_mq_kick_requeue_list(rq->q, msecs);
  218. }
  219. static void dm_requeue_original_request(struct dm_rq_target_io *tio, bool delay_requeue)
  220. {
  221. struct mapped_device *md = tio->md;
  222. struct request *rq = tio->orig;
  223. int rw = rq_data_dir(rq);
  224. unsigned long delay_ms = delay_requeue ? 100 : 0;
  225. rq_end_stats(md, rq);
  226. if (tio->clone) {
  227. blk_rq_unprep_clone(tio->clone);
  228. tio->ti->type->release_clone_rq(tio->clone, NULL);
  229. }
  230. if (!rq->q->mq_ops)
  231. dm_old_requeue_request(rq, delay_ms);
  232. else
  233. dm_mq_delay_requeue_request(rq, delay_ms);
  234. rq_completed(md, rw, false);
  235. }
  236. static void dm_done(struct request *clone, blk_status_t error, bool mapped)
  237. {
  238. int r = DM_ENDIO_DONE;
  239. struct dm_rq_target_io *tio = clone->end_io_data;
  240. dm_request_endio_fn rq_end_io = NULL;
  241. if (tio->ti) {
  242. rq_end_io = tio->ti->type->rq_end_io;
  243. if (mapped && rq_end_io)
  244. r = rq_end_io(tio->ti, clone, error, &tio->info);
  245. }
  246. if (unlikely(error == BLK_STS_TARGET)) {
  247. if (req_op(clone) == REQ_OP_DISCARD &&
  248. !clone->q->limits.max_discard_sectors)
  249. disable_discard(tio->md);
  250. else if (req_op(clone) == REQ_OP_WRITE_SAME &&
  251. !clone->q->limits.max_write_same_sectors)
  252. disable_write_same(tio->md);
  253. else if (req_op(clone) == REQ_OP_WRITE_ZEROES &&
  254. !clone->q->limits.max_write_zeroes_sectors)
  255. disable_write_zeroes(tio->md);
  256. }
  257. switch (r) {
  258. case DM_ENDIO_DONE:
  259. /* The target wants to complete the I/O */
  260. dm_end_request(clone, error);
  261. break;
  262. case DM_ENDIO_INCOMPLETE:
  263. /* The target will handle the I/O */
  264. return;
  265. case DM_ENDIO_REQUEUE:
  266. /* The target wants to requeue the I/O */
  267. dm_requeue_original_request(tio, false);
  268. break;
  269. case DM_ENDIO_DELAY_REQUEUE:
  270. /* The target wants to requeue the I/O after a delay */
  271. dm_requeue_original_request(tio, true);
  272. break;
  273. default:
  274. DMWARN("unimplemented target endio return value: %d", r);
  275. BUG();
  276. }
  277. }
  278. /*
  279. * Request completion handler for request-based dm
  280. */
  281. static void dm_softirq_done(struct request *rq)
  282. {
  283. bool mapped = true;
  284. struct dm_rq_target_io *tio = tio_from_request(rq);
  285. struct request *clone = tio->clone;
  286. int rw;
  287. if (!clone) {
  288. struct mapped_device *md = tio->md;
  289. rq_end_stats(md, rq);
  290. rw = rq_data_dir(rq);
  291. if (!rq->q->mq_ops)
  292. blk_end_request_all(rq, tio->error);
  293. else
  294. blk_mq_end_request(rq, tio->error);
  295. rq_completed(md, rw, false);
  296. return;
  297. }
  298. if (rq->rq_flags & RQF_FAILED)
  299. mapped = false;
  300. dm_done(clone, tio->error, mapped);
  301. }
  302. /*
  303. * Complete the clone and the original request with the error status
  304. * through softirq context.
  305. */
  306. static void dm_complete_request(struct request *rq, blk_status_t error)
  307. {
  308. struct dm_rq_target_io *tio = tio_from_request(rq);
  309. tio->error = error;
  310. if (!rq->q->mq_ops)
  311. blk_complete_request(rq);
  312. else
  313. blk_mq_complete_request(rq);
  314. }
  315. /*
  316. * Complete the not-mapped clone and the original request with the error status
  317. * through softirq context.
  318. * Target's rq_end_io() function isn't called.
  319. * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
  320. */
  321. static void dm_kill_unmapped_request(struct request *rq, blk_status_t error)
  322. {
  323. rq->rq_flags |= RQF_FAILED;
  324. dm_complete_request(rq, error);
  325. }
  326. /*
  327. * Called with the clone's queue lock held (in the case of .request_fn)
  328. */
  329. static void end_clone_request(struct request *clone, blk_status_t error)
  330. {
  331. struct dm_rq_target_io *tio = clone->end_io_data;
  332. /*
  333. * Actual request completion is done in a softirq context which doesn't
  334. * hold the clone's queue lock. Otherwise, deadlock could occur because:
  335. * - another request may be submitted by the upper level driver
  336. * of the stacking during the completion
  337. * - the submission which requires queue lock may be done
  338. * against this clone's queue
  339. */
  340. dm_complete_request(tio->orig, error);
  341. }
  342. static blk_status_t dm_dispatch_clone_request(struct request *clone, struct request *rq)
  343. {
  344. blk_status_t r;
  345. if (blk_queue_io_stat(clone->q))
  346. clone->rq_flags |= RQF_IO_STAT;
  347. clone->start_time_ns = ktime_get_ns();
  348. r = blk_insert_cloned_request(clone->q, clone);
  349. if (r != BLK_STS_OK && r != BLK_STS_RESOURCE && r != BLK_STS_DEV_RESOURCE)
  350. /* must complete clone in terms of original request */
  351. dm_complete_request(rq, r);
  352. return r;
  353. }
  354. static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
  355. void *data)
  356. {
  357. struct dm_rq_target_io *tio = data;
  358. struct dm_rq_clone_bio_info *info =
  359. container_of(bio, struct dm_rq_clone_bio_info, clone);
  360. info->orig = bio_orig;
  361. info->tio = tio;
  362. bio->bi_end_io = end_clone_bio;
  363. return 0;
  364. }
  365. static int setup_clone(struct request *clone, struct request *rq,
  366. struct dm_rq_target_io *tio, gfp_t gfp_mask)
  367. {
  368. int r;
  369. r = blk_rq_prep_clone(clone, rq, &tio->md->bs, gfp_mask,
  370. dm_rq_bio_constructor, tio);
  371. if (r)
  372. return r;
  373. clone->end_io = end_clone_request;
  374. clone->end_io_data = tio;
  375. tio->clone = clone;
  376. return 0;
  377. }
  378. static void map_tio_request(struct kthread_work *work);
  379. static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
  380. struct mapped_device *md)
  381. {
  382. tio->md = md;
  383. tio->ti = NULL;
  384. tio->clone = NULL;
  385. tio->orig = rq;
  386. tio->error = 0;
  387. tio->completed = 0;
  388. /*
  389. * Avoid initializing info for blk-mq; it passes
  390. * target-specific data through info.ptr
  391. * (see: dm_mq_init_request)
  392. */
  393. if (!md->init_tio_pdu)
  394. memset(&tio->info, 0, sizeof(tio->info));
  395. if (md->kworker_task)
  396. kthread_init_work(&tio->work, map_tio_request);
  397. }
  398. /*
  399. * Returns:
  400. * DM_MAPIO_* : the request has been processed as indicated
  401. * DM_MAPIO_REQUEUE : the original request needs to be immediately requeued
  402. * < 0 : the request was completed due to failure
  403. */
  404. static int map_request(struct dm_rq_target_io *tio)
  405. {
  406. int r;
  407. struct dm_target *ti = tio->ti;
  408. struct mapped_device *md = tio->md;
  409. struct request *rq = tio->orig;
  410. struct request *clone = NULL;
  411. blk_status_t ret;
  412. r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
  413. check_again:
  414. switch (r) {
  415. case DM_MAPIO_SUBMITTED:
  416. /* The target has taken the I/O to submit by itself later */
  417. break;
  418. case DM_MAPIO_REMAPPED:
  419. if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
  420. /* -ENOMEM */
  421. ti->type->release_clone_rq(clone, &tio->info);
  422. return DM_MAPIO_REQUEUE;
  423. }
  424. /* The target has remapped the I/O so dispatch it */
  425. trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
  426. blk_rq_pos(rq));
  427. ret = dm_dispatch_clone_request(clone, rq);
  428. if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE) {
  429. blk_rq_unprep_clone(clone);
  430. blk_mq_cleanup_rq(clone);
  431. tio->ti->type->release_clone_rq(clone, &tio->info);
  432. tio->clone = NULL;
  433. if (!rq->q->mq_ops)
  434. r = DM_MAPIO_DELAY_REQUEUE;
  435. else
  436. r = DM_MAPIO_REQUEUE;
  437. goto check_again;
  438. }
  439. break;
  440. case DM_MAPIO_REQUEUE:
  441. /* The target wants to requeue the I/O */
  442. break;
  443. case DM_MAPIO_DELAY_REQUEUE:
  444. /* The target wants to requeue the I/O after a delay */
  445. dm_requeue_original_request(tio, true);
  446. break;
  447. case DM_MAPIO_KILL:
  448. /* The target wants to complete the I/O */
  449. dm_kill_unmapped_request(rq, BLK_STS_IOERR);
  450. break;
  451. default:
  452. DMWARN("unimplemented target map return value: %d", r);
  453. BUG();
  454. }
  455. return r;
  456. }
  457. static void dm_start_request(struct mapped_device *md, struct request *orig)
  458. {
  459. if (!orig->q->mq_ops)
  460. blk_start_request(orig);
  461. else
  462. blk_mq_start_request(orig);
  463. atomic_inc(&md->pending[rq_data_dir(orig)]);
  464. if (md->seq_rq_merge_deadline_usecs) {
  465. md->last_rq_pos = rq_end_sector(orig);
  466. md->last_rq_rw = rq_data_dir(orig);
  467. md->last_rq_start_time = ktime_get();
  468. }
  469. if (unlikely(dm_stats_used(&md->stats))) {
  470. struct dm_rq_target_io *tio = tio_from_request(orig);
  471. tio->duration_jiffies = jiffies;
  472. tio->n_sectors = blk_rq_sectors(orig);
  473. dm_stats_account_io(&md->stats, rq_data_dir(orig),
  474. blk_rq_pos(orig), tio->n_sectors, false, 0,
  475. &tio->stats_aux);
  476. }
  477. /*
  478. * Hold the md reference here for the in-flight I/O.
  479. * We can't rely on the reference count by device opener,
  480. * because the device may be closed during the request completion
  481. * when all bios are completed.
  482. * See the comment in rq_completed() too.
  483. */
  484. dm_get(md);
  485. }
  486. static int __dm_rq_init_rq(struct mapped_device *md, struct request *rq)
  487. {
  488. struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
  489. /*
  490. * Must initialize md member of tio, otherwise it won't
  491. * be available in dm_mq_queue_rq.
  492. */
  493. tio->md = md;
  494. if (md->init_tio_pdu) {
  495. /* target-specific per-io data is immediately after the tio */
  496. tio->info.ptr = tio + 1;
  497. }
  498. return 0;
  499. }
  500. static int dm_rq_init_rq(struct request_queue *q, struct request *rq, gfp_t gfp)
  501. {
  502. return __dm_rq_init_rq(q->rq_alloc_data, rq);
  503. }
  504. static void map_tio_request(struct kthread_work *work)
  505. {
  506. struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
  507. if (map_request(tio) == DM_MAPIO_REQUEUE)
  508. dm_requeue_original_request(tio, false);
  509. }
  510. ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
  511. {
  512. return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs);
  513. }
  514. #define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
  515. ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
  516. const char *buf, size_t count)
  517. {
  518. unsigned deadline;
  519. if (dm_get_md_type(md) != DM_TYPE_REQUEST_BASED)
  520. return count;
  521. if (kstrtouint(buf, 10, &deadline))
  522. return -EINVAL;
  523. if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS)
  524. deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS;
  525. md->seq_rq_merge_deadline_usecs = deadline;
  526. return count;
  527. }
  528. static bool dm_old_request_peeked_before_merge_deadline(struct mapped_device *md)
  529. {
  530. ktime_t kt_deadline;
  531. if (!md->seq_rq_merge_deadline_usecs)
  532. return false;
  533. kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC);
  534. kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline);
  535. return !ktime_after(ktime_get(), kt_deadline);
  536. }
  537. /*
  538. * q->request_fn for old request-based dm.
  539. * Called with the queue lock held.
  540. */
  541. static void dm_old_request_fn(struct request_queue *q)
  542. {
  543. struct mapped_device *md = q->queuedata;
  544. struct dm_target *ti = md->immutable_target;
  545. struct request *rq;
  546. struct dm_rq_target_io *tio;
  547. sector_t pos = 0;
  548. if (unlikely(!ti)) {
  549. int srcu_idx;
  550. struct dm_table *map = dm_get_live_table(md, &srcu_idx);
  551. if (unlikely(!map)) {
  552. dm_put_live_table(md, srcu_idx);
  553. return;
  554. }
  555. ti = dm_table_find_target(map, pos);
  556. dm_put_live_table(md, srcu_idx);
  557. }
  558. /*
  559. * For suspend, check blk_queue_stopped() and increment
  560. * ->pending within a single queue_lock not to increment the
  561. * number of in-flight I/Os after the queue is stopped in
  562. * dm_suspend().
  563. */
  564. while (!blk_queue_stopped(q)) {
  565. rq = blk_peek_request(q);
  566. if (!rq)
  567. return;
  568. /* always use block 0 to find the target for flushes for now */
  569. pos = 0;
  570. if (req_op(rq) != REQ_OP_FLUSH)
  571. pos = blk_rq_pos(rq);
  572. if ((dm_old_request_peeked_before_merge_deadline(md) &&
  573. md_in_flight(md) && rq->bio && !bio_multiple_segments(rq->bio) &&
  574. md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq)) ||
  575. (ti->type->busy && ti->type->busy(ti))) {
  576. blk_delay_queue(q, 10);
  577. return;
  578. }
  579. dm_start_request(md, rq);
  580. tio = tio_from_request(rq);
  581. init_tio(tio, rq, md);
  582. /* Establish tio->ti before queuing work (map_tio_request) */
  583. tio->ti = ti;
  584. kthread_queue_work(&md->kworker, &tio->work);
  585. BUG_ON(!irqs_disabled());
  586. }
  587. }
  588. /*
  589. * Fully initialize a .request_fn request-based queue.
  590. */
  591. int dm_old_init_request_queue(struct mapped_device *md, struct dm_table *t)
  592. {
  593. struct dm_target *immutable_tgt;
  594. /* Fully initialize the queue */
  595. md->queue->cmd_size = sizeof(struct dm_rq_target_io);
  596. md->queue->rq_alloc_data = md;
  597. md->queue->request_fn = dm_old_request_fn;
  598. md->queue->init_rq_fn = dm_rq_init_rq;
  599. immutable_tgt = dm_table_get_immutable_target(t);
  600. if (immutable_tgt && immutable_tgt->per_io_data_size) {
  601. /* any target-specific per-io data is immediately after the tio */
  602. md->queue->cmd_size += immutable_tgt->per_io_data_size;
  603. md->init_tio_pdu = true;
  604. }
  605. if (blk_init_allocated_queue(md->queue) < 0)
  606. return -EINVAL;
  607. /* disable dm_old_request_fn's merge heuristic by default */
  608. md->seq_rq_merge_deadline_usecs = 0;
  609. blk_queue_softirq_done(md->queue, dm_softirq_done);
  610. /* Initialize the request-based DM worker thread */
  611. kthread_init_worker(&md->kworker);
  612. md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
  613. "kdmwork-%s", dm_device_name(md));
  614. if (IS_ERR(md->kworker_task)) {
  615. int error = PTR_ERR(md->kworker_task);
  616. md->kworker_task = NULL;
  617. return error;
  618. }
  619. return 0;
  620. }
  621. static int dm_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
  622. unsigned int hctx_idx, unsigned int numa_node)
  623. {
  624. return __dm_rq_init_rq(set->driver_data, rq);
  625. }
  626. static blk_status_t dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
  627. const struct blk_mq_queue_data *bd)
  628. {
  629. struct request *rq = bd->rq;
  630. struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
  631. struct mapped_device *md = tio->md;
  632. struct dm_target *ti = md->immutable_target;
  633. if (unlikely(!ti)) {
  634. int srcu_idx;
  635. struct dm_table *map = dm_get_live_table(md, &srcu_idx);
  636. ti = dm_table_find_target(map, 0);
  637. dm_put_live_table(md, srcu_idx);
  638. }
  639. if (ti->type->busy && ti->type->busy(ti))
  640. return BLK_STS_RESOURCE;
  641. dm_start_request(md, rq);
  642. /* Init tio using md established in .init_request */
  643. init_tio(tio, rq, md);
  644. /*
  645. * Establish tio->ti before calling map_request().
  646. */
  647. tio->ti = ti;
  648. /* Direct call is fine since .queue_rq allows allocations */
  649. if (map_request(tio) == DM_MAPIO_REQUEUE) {
  650. /* Undo dm_start_request() before requeuing */
  651. rq_end_stats(md, rq);
  652. rq_completed(md, rq_data_dir(rq), false);
  653. return BLK_STS_RESOURCE;
  654. }
  655. return BLK_STS_OK;
  656. }
  657. static const struct blk_mq_ops dm_mq_ops = {
  658. .queue_rq = dm_mq_queue_rq,
  659. .complete = dm_softirq_done,
  660. .init_request = dm_mq_init_request,
  661. };
  662. int dm_mq_init_request_queue(struct mapped_device *md, struct dm_table *t)
  663. {
  664. struct request_queue *q;
  665. struct dm_target *immutable_tgt;
  666. int err;
  667. if (!dm_table_all_blk_mq_devices(t)) {
  668. DMERR("request-based dm-mq may only be stacked on blk-mq device(s)");
  669. return -EINVAL;
  670. }
  671. md->tag_set = kzalloc_node(sizeof(struct blk_mq_tag_set), GFP_KERNEL, md->numa_node_id);
  672. if (!md->tag_set)
  673. return -ENOMEM;
  674. md->tag_set->ops = &dm_mq_ops;
  675. md->tag_set->queue_depth = dm_get_blk_mq_queue_depth();
  676. md->tag_set->numa_node = md->numa_node_id;
  677. md->tag_set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
  678. md->tag_set->nr_hw_queues = dm_get_blk_mq_nr_hw_queues();
  679. md->tag_set->driver_data = md;
  680. md->tag_set->cmd_size = sizeof(struct dm_rq_target_io);
  681. immutable_tgt = dm_table_get_immutable_target(t);
  682. if (immutable_tgt && immutable_tgt->per_io_data_size) {
  683. /* any target-specific per-io data is immediately after the tio */
  684. md->tag_set->cmd_size += immutable_tgt->per_io_data_size;
  685. md->init_tio_pdu = true;
  686. }
  687. err = blk_mq_alloc_tag_set(md->tag_set);
  688. if (err)
  689. goto out_kfree_tag_set;
  690. q = blk_mq_init_allocated_queue(md->tag_set, md->queue);
  691. if (IS_ERR(q)) {
  692. err = PTR_ERR(q);
  693. goto out_tag_set;
  694. }
  695. return 0;
  696. out_tag_set:
  697. blk_mq_free_tag_set(md->tag_set);
  698. out_kfree_tag_set:
  699. kfree(md->tag_set);
  700. md->tag_set = NULL;
  701. return err;
  702. }
  703. void dm_mq_cleanup_mapped_device(struct mapped_device *md)
  704. {
  705. if (md->tag_set) {
  706. blk_mq_free_tag_set(md->tag_set);
  707. kfree(md->tag_set);
  708. md->tag_set = NULL;
  709. }
  710. }
  711. module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
  712. MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
  713. module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
  714. MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
  715. module_param(dm_mq_nr_hw_queues, uint, S_IRUGO | S_IWUSR);
  716. MODULE_PARM_DESC(dm_mq_nr_hw_queues, "Number of hardware queues for request-based dm-mq devices");
  717. module_param(dm_mq_queue_depth, uint, S_IRUGO | S_IWUSR);
  718. MODULE_PARM_DESC(dm_mq_queue_depth, "Queue depth for request-based dm-mq devices");