dm-rq.c 26 KB

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