scsi: implement .cleanup_rq callback
authorMing Lei <ming.lei@redhat.com>
Thu, 25 Jul 2019 02:05:00 +0000 (10:05 +0800)
committerJens Axboe <axboe@kernel.dk>
Mon, 5 Aug 2019 03:42:08 +0000 (21:42 -0600)
Implement .cleanup_rq() callback for freeing driver private part
of the request. Then we can avoid to leak this part if the request isn't
completed by SCSI, and freed by blk-mq or upper layer(such as dm-rq) finally.

Cc: Ewan D. Milne <emilne@redhat.com>
Cc: Bart Van Assche <bvanassche@acm.org>
Cc: Hannes Reinecke <hare@suse.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Mike Snitzer <snitzer@redhat.com>
Cc: dm-devel@redhat.com
Cc: <stable@vger.kernel.org>
Fixes: 396eaf21ee17 ("blk-mq: improve DM's blk-mq IO merging via blk_insert_cloned_request feedback")
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
drivers/scsi/scsi_lib.c

index 11e64b50497f81949476014f2b31d40d024346ed..4e88d7e9cf9a31a38713457645c5ea77b78d5783 100644 (file)
@@ -1089,6 +1089,18 @@ static void scsi_initialize_rq(struct request *rq)
        cmd->retries = 0;
 }
 
+/*
+ * Only called when the request isn't completed by SCSI, and not freed by
+ * SCSI
+ */
+static void scsi_cleanup_rq(struct request *rq)
+{
+       if (rq->rq_flags & RQF_DONTPREP) {
+               scsi_mq_uninit_cmd(blk_mq_rq_to_pdu(rq));
+               rq->rq_flags &= ~RQF_DONTPREP;
+       }
+}
+
 /* Add a command to the list used by the aacraid and dpt_i2o drivers */
 void scsi_add_cmd_to_list(struct scsi_cmnd *cmd)
 {
@@ -1821,6 +1833,7 @@ static const struct blk_mq_ops scsi_mq_ops = {
        .init_request   = scsi_mq_init_request,
        .exit_request   = scsi_mq_exit_request,
        .initialize_rq_fn = scsi_initialize_rq,
+       .cleanup_rq     = scsi_cleanup_rq,
        .busy           = scsi_mq_lld_busy,
        .map_queues     = scsi_map_queues,
 };