Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (C) 2016 Red Hat, Inc. All rights reserved.
0003  *
0004  * This file is released under the GPL.
0005  */
0006 
0007 #include "dm-core.h"
0008 #include "dm-rq.h"
0009 
0010 #include <linux/blk-mq.h>
0011 
0012 #define DM_MSG_PREFIX "core-rq"
0013 
0014 /*
0015  * One of these is allocated per request.
0016  */
0017 struct dm_rq_target_io {
0018     struct mapped_device *md;
0019     struct dm_target *ti;
0020     struct request *orig, *clone;
0021     struct kthread_work work;
0022     blk_status_t error;
0023     union map_info info;
0024     struct dm_stats_aux stats_aux;
0025     unsigned long duration_jiffies;
0026     unsigned n_sectors;
0027     unsigned completed;
0028 };
0029 
0030 #define DM_MQ_NR_HW_QUEUES 1
0031 #define DM_MQ_QUEUE_DEPTH 2048
0032 static unsigned dm_mq_nr_hw_queues = DM_MQ_NR_HW_QUEUES;
0033 static unsigned dm_mq_queue_depth = DM_MQ_QUEUE_DEPTH;
0034 
0035 /*
0036  * Request-based DM's mempools' reserved IOs set by the user.
0037  */
0038 #define RESERVED_REQUEST_BASED_IOS  256
0039 static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
0040 
0041 unsigned dm_get_reserved_rq_based_ios(void)
0042 {
0043     return __dm_get_module_param(&reserved_rq_based_ios,
0044                      RESERVED_REQUEST_BASED_IOS, DM_RESERVED_MAX_IOS);
0045 }
0046 
0047 static unsigned dm_get_blk_mq_nr_hw_queues(void)
0048 {
0049     return __dm_get_module_param(&dm_mq_nr_hw_queues, 1, 32);
0050 }
0051 
0052 static unsigned dm_get_blk_mq_queue_depth(void)
0053 {
0054     return __dm_get_module_param(&dm_mq_queue_depth,
0055                      DM_MQ_QUEUE_DEPTH, BLK_MQ_MAX_DEPTH);
0056 }
0057 
0058 int dm_request_based(struct mapped_device *md)
0059 {
0060     return queue_is_mq(md->queue);
0061 }
0062 
0063 void dm_start_queue(struct request_queue *q)
0064 {
0065     blk_mq_unquiesce_queue(q);
0066     blk_mq_kick_requeue_list(q);
0067 }
0068 
0069 void dm_stop_queue(struct request_queue *q)
0070 {
0071     blk_mq_quiesce_queue(q);
0072 }
0073 
0074 /*
0075  * Partial completion handling for request-based dm
0076  */
0077 static void end_clone_bio(struct bio *clone)
0078 {
0079     struct dm_rq_clone_bio_info *info =
0080         container_of(clone, struct dm_rq_clone_bio_info, clone);
0081     struct dm_rq_target_io *tio = info->tio;
0082     unsigned int nr_bytes = info->orig->bi_iter.bi_size;
0083     blk_status_t error = clone->bi_status;
0084     bool is_last = !clone->bi_next;
0085 
0086     bio_put(clone);
0087 
0088     if (tio->error)
0089         /*
0090          * An error has already been detected on the request.
0091          * Once error occurred, just let clone->end_io() handle
0092          * the remainder.
0093          */
0094         return;
0095     else if (error) {
0096         /*
0097          * Don't notice the error to the upper layer yet.
0098          * The error handling decision is made by the target driver,
0099          * when the request is completed.
0100          */
0101         tio->error = error;
0102         goto exit;
0103     }
0104 
0105     /*
0106      * I/O for the bio successfully completed.
0107      * Notice the data completion to the upper layer.
0108      */
0109     tio->completed += nr_bytes;
0110 
0111     /*
0112      * Update the original request.
0113      * Do not use blk_mq_end_request() here, because it may complete
0114      * the original request before the clone, and break the ordering.
0115      */
0116     if (is_last)
0117  exit:
0118         blk_update_request(tio->orig, BLK_STS_OK, tio->completed);
0119 }
0120 
0121 static struct dm_rq_target_io *tio_from_request(struct request *rq)
0122 {
0123     return blk_mq_rq_to_pdu(rq);
0124 }
0125 
0126 static void rq_end_stats(struct mapped_device *md, struct request *orig)
0127 {
0128     if (unlikely(dm_stats_used(&md->stats))) {
0129         struct dm_rq_target_io *tio = tio_from_request(orig);
0130         tio->duration_jiffies = jiffies - tio->duration_jiffies;
0131         dm_stats_account_io(&md->stats, rq_data_dir(orig),
0132                     blk_rq_pos(orig), tio->n_sectors, true,
0133                     tio->duration_jiffies, &tio->stats_aux);
0134     }
0135 }
0136 
0137 /*
0138  * Don't touch any member of the md after calling this function because
0139  * the md may be freed in dm_put() at the end of this function.
0140  * Or do dm_get() before calling this function and dm_put() later.
0141  */
0142 static void rq_completed(struct mapped_device *md)
0143 {
0144     /*
0145      * dm_put() must be at the end of this function. See the comment above
0146      */
0147     dm_put(md);
0148 }
0149 
0150 /*
0151  * Complete the clone and the original request.
0152  * Must be called without clone's queue lock held,
0153  * see end_clone_request() for more details.
0154  */
0155 static void dm_end_request(struct request *clone, blk_status_t error)
0156 {
0157     struct dm_rq_target_io *tio = clone->end_io_data;
0158     struct mapped_device *md = tio->md;
0159     struct request *rq = tio->orig;
0160 
0161     blk_rq_unprep_clone(clone);
0162     tio->ti->type->release_clone_rq(clone, NULL);
0163 
0164     rq_end_stats(md, rq);
0165     blk_mq_end_request(rq, error);
0166     rq_completed(md);
0167 }
0168 
0169 static void __dm_mq_kick_requeue_list(struct request_queue *q, unsigned long msecs)
0170 {
0171     blk_mq_delay_kick_requeue_list(q, msecs);
0172 }
0173 
0174 void dm_mq_kick_requeue_list(struct mapped_device *md)
0175 {
0176     __dm_mq_kick_requeue_list(md->queue, 0);
0177 }
0178 EXPORT_SYMBOL(dm_mq_kick_requeue_list);
0179 
0180 static void dm_mq_delay_requeue_request(struct request *rq, unsigned long msecs)
0181 {
0182     blk_mq_requeue_request(rq, false);
0183     __dm_mq_kick_requeue_list(rq->q, msecs);
0184 }
0185 
0186 static void dm_requeue_original_request(struct dm_rq_target_io *tio, bool delay_requeue)
0187 {
0188     struct mapped_device *md = tio->md;
0189     struct request *rq = tio->orig;
0190     unsigned long delay_ms = delay_requeue ? 100 : 0;
0191 
0192     rq_end_stats(md, rq);
0193     if (tio->clone) {
0194         blk_rq_unprep_clone(tio->clone);
0195         tio->ti->type->release_clone_rq(tio->clone, NULL);
0196     }
0197 
0198     dm_mq_delay_requeue_request(rq, delay_ms);
0199     rq_completed(md);
0200 }
0201 
0202 static void dm_done(struct request *clone, blk_status_t error, bool mapped)
0203 {
0204     int r = DM_ENDIO_DONE;
0205     struct dm_rq_target_io *tio = clone->end_io_data;
0206     dm_request_endio_fn rq_end_io = NULL;
0207 
0208     if (tio->ti) {
0209         rq_end_io = tio->ti->type->rq_end_io;
0210 
0211         if (mapped && rq_end_io)
0212             r = rq_end_io(tio->ti, clone, error, &tio->info);
0213     }
0214 
0215     if (unlikely(error == BLK_STS_TARGET)) {
0216         if (req_op(clone) == REQ_OP_DISCARD &&
0217             !clone->q->limits.max_discard_sectors)
0218             disable_discard(tio->md);
0219         else if (req_op(clone) == REQ_OP_WRITE_ZEROES &&
0220              !clone->q->limits.max_write_zeroes_sectors)
0221             disable_write_zeroes(tio->md);
0222     }
0223 
0224     switch (r) {
0225     case DM_ENDIO_DONE:
0226         /* The target wants to complete the I/O */
0227         dm_end_request(clone, error);
0228         break;
0229     case DM_ENDIO_INCOMPLETE:
0230         /* The target will handle the I/O */
0231         return;
0232     case DM_ENDIO_REQUEUE:
0233         /* The target wants to requeue the I/O */
0234         dm_requeue_original_request(tio, false);
0235         break;
0236     case DM_ENDIO_DELAY_REQUEUE:
0237         /* The target wants to requeue the I/O after a delay */
0238         dm_requeue_original_request(tio, true);
0239         break;
0240     default:
0241         DMWARN("unimplemented target endio return value: %d", r);
0242         BUG();
0243     }
0244 }
0245 
0246 /*
0247  * Request completion handler for request-based dm
0248  */
0249 static void dm_softirq_done(struct request *rq)
0250 {
0251     bool mapped = true;
0252     struct dm_rq_target_io *tio = tio_from_request(rq);
0253     struct request *clone = tio->clone;
0254 
0255     if (!clone) {
0256         struct mapped_device *md = tio->md;
0257 
0258         rq_end_stats(md, rq);
0259         blk_mq_end_request(rq, tio->error);
0260         rq_completed(md);
0261         return;
0262     }
0263 
0264     if (rq->rq_flags & RQF_FAILED)
0265         mapped = false;
0266 
0267     dm_done(clone, tio->error, mapped);
0268 }
0269 
0270 /*
0271  * Complete the clone and the original request with the error status
0272  * through softirq context.
0273  */
0274 static void dm_complete_request(struct request *rq, blk_status_t error)
0275 {
0276     struct dm_rq_target_io *tio = tio_from_request(rq);
0277 
0278     tio->error = error;
0279     if (likely(!blk_should_fake_timeout(rq->q)))
0280         blk_mq_complete_request(rq);
0281 }
0282 
0283 /*
0284  * Complete the not-mapped clone and the original request with the error status
0285  * through softirq context.
0286  * Target's rq_end_io() function isn't called.
0287  * This may be used when the target's clone_and_map_rq() function fails.
0288  */
0289 static void dm_kill_unmapped_request(struct request *rq, blk_status_t error)
0290 {
0291     rq->rq_flags |= RQF_FAILED;
0292     dm_complete_request(rq, error);
0293 }
0294 
0295 static void end_clone_request(struct request *clone, blk_status_t error)
0296 {
0297     struct dm_rq_target_io *tio = clone->end_io_data;
0298 
0299     dm_complete_request(tio->orig, error);
0300 }
0301 
0302 static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
0303                  void *data)
0304 {
0305     struct dm_rq_target_io *tio = data;
0306     struct dm_rq_clone_bio_info *info =
0307         container_of(bio, struct dm_rq_clone_bio_info, clone);
0308 
0309     info->orig = bio_orig;
0310     info->tio = tio;
0311     bio->bi_end_io = end_clone_bio;
0312 
0313     return 0;
0314 }
0315 
0316 static int setup_clone(struct request *clone, struct request *rq,
0317                struct dm_rq_target_io *tio, gfp_t gfp_mask)
0318 {
0319     int r;
0320 
0321     r = blk_rq_prep_clone(clone, rq, &tio->md->mempools->bs, gfp_mask,
0322                   dm_rq_bio_constructor, tio);
0323     if (r)
0324         return r;
0325 
0326     clone->end_io = end_clone_request;
0327     clone->end_io_data = tio;
0328 
0329     tio->clone = clone;
0330 
0331     return 0;
0332 }
0333 
0334 static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
0335              struct mapped_device *md)
0336 {
0337     tio->md = md;
0338     tio->ti = NULL;
0339     tio->clone = NULL;
0340     tio->orig = rq;
0341     tio->error = 0;
0342     tio->completed = 0;
0343     /*
0344      * Avoid initializing info for blk-mq; it passes
0345      * target-specific data through info.ptr
0346      * (see: dm_mq_init_request)
0347      */
0348     if (!md->init_tio_pdu)
0349         memset(&tio->info, 0, sizeof(tio->info));
0350 }
0351 
0352 /*
0353  * Returns:
0354  * DM_MAPIO_*       : the request has been processed as indicated
0355  * DM_MAPIO_REQUEUE : the original request needs to be immediately requeued
0356  * < 0              : the request was completed due to failure
0357  */
0358 static int map_request(struct dm_rq_target_io *tio)
0359 {
0360     int r;
0361     struct dm_target *ti = tio->ti;
0362     struct mapped_device *md = tio->md;
0363     struct request *rq = tio->orig;
0364     struct request *clone = NULL;
0365     blk_status_t ret;
0366 
0367     r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
0368     switch (r) {
0369     case DM_MAPIO_SUBMITTED:
0370         /* The target has taken the I/O to submit by itself later */
0371         break;
0372     case DM_MAPIO_REMAPPED:
0373         if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
0374             /* -ENOMEM */
0375             ti->type->release_clone_rq(clone, &tio->info);
0376             return DM_MAPIO_REQUEUE;
0377         }
0378 
0379         /* The target has remapped the I/O so dispatch it */
0380         trace_block_rq_remap(clone, disk_devt(dm_disk(md)),
0381                      blk_rq_pos(rq));
0382         ret = blk_insert_cloned_request(clone);
0383         switch (ret) {
0384         case BLK_STS_OK:
0385             break;
0386         case BLK_STS_RESOURCE:
0387         case BLK_STS_DEV_RESOURCE:
0388             blk_rq_unprep_clone(clone);
0389             blk_mq_cleanup_rq(clone);
0390             tio->ti->type->release_clone_rq(clone, &tio->info);
0391             tio->clone = NULL;
0392             return DM_MAPIO_REQUEUE;
0393         default:
0394             /* must complete clone in terms of original request */
0395             dm_complete_request(rq, ret);
0396         }
0397         break;
0398     case DM_MAPIO_REQUEUE:
0399         /* The target wants to requeue the I/O */
0400         break;
0401     case DM_MAPIO_DELAY_REQUEUE:
0402         /* The target wants to requeue the I/O after a delay */
0403         dm_requeue_original_request(tio, true);
0404         break;
0405     case DM_MAPIO_KILL:
0406         /* The target wants to complete the I/O */
0407         dm_kill_unmapped_request(rq, BLK_STS_IOERR);
0408         break;
0409     default:
0410         DMWARN("unimplemented target map return value: %d", r);
0411         BUG();
0412     }
0413 
0414     return r;
0415 }
0416 
0417 /* DEPRECATED: previously used for request-based merge heuristic in dm_request_fn() */
0418 ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
0419 {
0420     return sprintf(buf, "%u\n", 0);
0421 }
0422 
0423 ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
0424                              const char *buf, size_t count)
0425 {
0426     return count;
0427 }
0428 
0429 static void dm_start_request(struct mapped_device *md, struct request *orig)
0430 {
0431     blk_mq_start_request(orig);
0432 
0433     if (unlikely(dm_stats_used(&md->stats))) {
0434         struct dm_rq_target_io *tio = tio_from_request(orig);
0435         tio->duration_jiffies = jiffies;
0436         tio->n_sectors = blk_rq_sectors(orig);
0437         dm_stats_account_io(&md->stats, rq_data_dir(orig),
0438                     blk_rq_pos(orig), tio->n_sectors, false, 0,
0439                     &tio->stats_aux);
0440     }
0441 
0442     /*
0443      * Hold the md reference here for the in-flight I/O.
0444      * We can't rely on the reference count by device opener,
0445      * because the device may be closed during the request completion
0446      * when all bios are completed.
0447      * See the comment in rq_completed() too.
0448      */
0449     dm_get(md);
0450 }
0451 
0452 static int dm_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
0453                   unsigned int hctx_idx, unsigned int numa_node)
0454 {
0455     struct mapped_device *md = set->driver_data;
0456     struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
0457 
0458     /*
0459      * Must initialize md member of tio, otherwise it won't
0460      * be available in dm_mq_queue_rq.
0461      */
0462     tio->md = md;
0463 
0464     if (md->init_tio_pdu) {
0465         /* target-specific per-io data is immediately after the tio */
0466         tio->info.ptr = tio + 1;
0467     }
0468 
0469     return 0;
0470 }
0471 
0472 static blk_status_t dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
0473               const struct blk_mq_queue_data *bd)
0474 {
0475     struct request *rq = bd->rq;
0476     struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
0477     struct mapped_device *md = tio->md;
0478     struct dm_target *ti = md->immutable_target;
0479 
0480     /*
0481      * blk-mq's unquiesce may come from outside events, such as
0482      * elevator switch, updating nr_requests or others, and request may
0483      * come during suspend, so simply ask for blk-mq to requeue it.
0484      */
0485     if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)))
0486         return BLK_STS_RESOURCE;
0487 
0488     if (unlikely(!ti)) {
0489         int srcu_idx;
0490         struct dm_table *map;
0491 
0492         map = dm_get_live_table(md, &srcu_idx);
0493         if (unlikely(!map)) {
0494             dm_put_live_table(md, srcu_idx);
0495             return BLK_STS_RESOURCE;
0496         }
0497         ti = dm_table_find_target(map, 0);
0498         dm_put_live_table(md, srcu_idx);
0499     }
0500 
0501     if (ti->type->busy && ti->type->busy(ti))
0502         return BLK_STS_RESOURCE;
0503 
0504     dm_start_request(md, rq);
0505 
0506     /* Init tio using md established in .init_request */
0507     init_tio(tio, rq, md);
0508 
0509     /*
0510      * Establish tio->ti before calling map_request().
0511      */
0512     tio->ti = ti;
0513 
0514     /* Direct call is fine since .queue_rq allows allocations */
0515     if (map_request(tio) == DM_MAPIO_REQUEUE) {
0516         /* Undo dm_start_request() before requeuing */
0517         rq_end_stats(md, rq);
0518         rq_completed(md);
0519         return BLK_STS_RESOURCE;
0520     }
0521 
0522     return BLK_STS_OK;
0523 }
0524 
0525 static const struct blk_mq_ops dm_mq_ops = {
0526     .queue_rq = dm_mq_queue_rq,
0527     .complete = dm_softirq_done,
0528     .init_request = dm_mq_init_request,
0529 };
0530 
0531 int dm_mq_init_request_queue(struct mapped_device *md, struct dm_table *t)
0532 {
0533     struct dm_target *immutable_tgt;
0534     int err;
0535 
0536     md->tag_set = kzalloc_node(sizeof(struct blk_mq_tag_set), GFP_KERNEL, md->numa_node_id);
0537     if (!md->tag_set)
0538         return -ENOMEM;
0539 
0540     md->tag_set->ops = &dm_mq_ops;
0541     md->tag_set->queue_depth = dm_get_blk_mq_queue_depth();
0542     md->tag_set->numa_node = md->numa_node_id;
0543     md->tag_set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_STACKING;
0544     md->tag_set->nr_hw_queues = dm_get_blk_mq_nr_hw_queues();
0545     md->tag_set->driver_data = md;
0546 
0547     md->tag_set->cmd_size = sizeof(struct dm_rq_target_io);
0548     immutable_tgt = dm_table_get_immutable_target(t);
0549     if (immutable_tgt && immutable_tgt->per_io_data_size) {
0550         /* any target-specific per-io data is immediately after the tio */
0551         md->tag_set->cmd_size += immutable_tgt->per_io_data_size;
0552         md->init_tio_pdu = true;
0553     }
0554 
0555     err = blk_mq_alloc_tag_set(md->tag_set);
0556     if (err)
0557         goto out_kfree_tag_set;
0558 
0559     err = blk_mq_init_allocated_queue(md->tag_set, md->queue);
0560     if (err)
0561         goto out_tag_set;
0562     return 0;
0563 
0564 out_tag_set:
0565     blk_mq_free_tag_set(md->tag_set);
0566 out_kfree_tag_set:
0567     kfree(md->tag_set);
0568     md->tag_set = NULL;
0569 
0570     return err;
0571 }
0572 
0573 void dm_mq_cleanup_mapped_device(struct mapped_device *md)
0574 {
0575     if (md->tag_set) {
0576         blk_mq_free_tag_set(md->tag_set);
0577         kfree(md->tag_set);
0578         md->tag_set = NULL;
0579     }
0580 }
0581 
0582 module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
0583 MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
0584 
0585 /* Unused, but preserved for userspace compatibility */
0586 static bool use_blk_mq = true;
0587 module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
0588 MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
0589 
0590 module_param(dm_mq_nr_hw_queues, uint, S_IRUGO | S_IWUSR);
0591 MODULE_PARM_DESC(dm_mq_nr_hw_queues, "Number of hardware queues for request-based dm-mq devices");
0592 
0593 module_param(dm_mq_queue_depth, uint, S_IRUGO | S_IWUSR);
0594 MODULE_PARM_DESC(dm_mq_queue_depth, "Queue depth for request-based dm-mq devices");