dm-rq.c 21 KB

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