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