dm-rq.c 24 KB

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