From: John Garry <[email protected]>

Add scsi_mpath_{start,end}_request() to handle updating private multipath
request data, like nvme_mpath_{start,end}_request().

New member Scsi_Host.mpath_nr_active is added. This is required for queue
depth multipath iopolicy.

For NVMe, this count is per controller. The reason is that many NSes may
be connected to a controller, so congestion should be judged at
controller level.

SCSI has no definition of a controller, but SCSI host is a comparable
concept.

Indeed, many SCSI disks may be connected to the same SCSI host, so it
makes sense to count number of active requests at this point. However,
for a transport like iSCSI Initiator over TCP/IP, we have a separate SCSI
host per SCSI device (so there the count would be same at SCSI device
level).

Signed-off-by: John Garry <[email protected]>
---
 drivers/scsi/scsi_lib.c       |  8 +++++-
 drivers/scsi/scsi_multipath.c | 53 +++++++++++++++++++++++++++++++++++
 include/scsi/scsi_cmnd.h      |  6 ++++
 include/scsi/scsi_host.h      |  4 +++
 include/scsi/scsi_multipath.h | 11 ++++++++
 5 files changed, 81 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c
index 1e13aefdb6d64..3dda25a05f78c 100644
--- a/drivers/scsi/scsi_lib.c
+++ b/drivers/scsi/scsi_lib.c
@@ -669,7 +669,10 @@ static bool scsi_end_request(struct request *req, 
blk_status_t error,
        struct scsi_device *sdev = cmd->device;
        struct request_queue *q = sdev->request_queue;
 
-       if (blk_update_request(req, error, bytes))
+       if (is_mpath_request(req)) {
+               if (scsi_mpath_end_request(req, error, bytes))
+                       return true;
+       } else if (blk_update_request(req, error, bytes))
                return true;
 
        if (q->limits.features & BLK_FEAT_ADD_RANDOM)
@@ -1917,6 +1920,9 @@ static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx 
*hctx,
        memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
        cmd->submitter = SUBMITTED_BY_BLOCK_LAYER;
 
+       if (is_mpath_request(req))
+               scsi_mpath_start_request(req);
+
        blk_mq_start_request(req);
        if (blk_mq_is_reserved_rq(req)) {
                reason = shost->hostt->queue_reserved_command(shost, cmd);
diff --git a/drivers/scsi/scsi_multipath.c b/drivers/scsi/scsi_multipath.c
index e566d47199fcc..6c50fdde70d65 100644
--- a/drivers/scsi/scsi_multipath.c
+++ b/drivers/scsi/scsi_multipath.c
@@ -555,6 +555,59 @@ void scsi_mpath_put_head(struct scsi_mpath_head 
*scsi_mpath_head)
 }
 EXPORT_SYMBOL_GPL(scsi_mpath_put_head);
 
+void scsi_mpath_start_request(struct request *req)
+{
+       struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
+       struct scsi_device *sdev = scmd->device;
+       struct scsi_mpath_device *scsi_mpath_dev = sdev->scsi_mpath_dev;
+       struct scsi_mpath_head *scsi_mpath_head =
+                               scsi_mpath_dev->scsi_mpath_head;
+       struct bio *clone = req->bio;
+       struct bio *master = clone->bi_private;
+
+       if (mpath_qd_iopolicy(&scsi_mpath_head->iopolicy) &&
+           !(scmd->flags & SCMD_MPATH_CNT_ACTIVE)) {
+               struct Scsi_Host *shost = sdev->host;
+
+               atomic_inc(&shost->mpath_nr_active);
+               scmd->flags |= SCMD_MPATH_CNT_ACTIVE;
+       }
+
+       if (!blk_queue_io_stat(master->bi_bdev->bd_queue) || 
blk_rq_is_passthrough(req) ||
+           (scmd->flags & SCMD_MPATH_IO_STATS))
+               return;
+
+       scmd->flags |= SCMD_MPATH_IO_STATS;
+       scmd->start_time = bdev_start_io_acct(master->bi_bdev, req_op(req),
+                               jiffies);
+       scmd->start_bytes = blk_rq_bytes(req);
+}
+
+bool scsi_mpath_end_request(struct request *req, blk_status_t error,
+                               unsigned int nr_bytes)
+{
+       struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
+       struct scsi_device *sdev = scmd->device;
+       struct bio *clone = req->bio, *master = clone->bi_private;
+
+       if (blk_update_request(req, error, nr_bytes))
+               return true;
+
+       if (scmd->flags & SCMD_MPATH_CNT_ACTIVE) {
+               struct Scsi_Host *shost = sdev->host;
+
+               atomic_dec_if_positive(&shost->mpath_nr_active);
+       }
+
+       if (!(scmd->flags & SCMD_MPATH_IO_STATS))
+               return false;
+       bdev_end_io_acct(master->bi_bdev, req_op(req),
+                        scmd->start_bytes >> SECTOR_SHIFT,
+                        scmd->start_time);
+
+       return false;
+}
+
 int __init scsi_multipath_init(void)
 {
        return class_register(&scsi_mpath_device_class);
diff --git a/include/scsi/scsi_cmnd.h b/include/scsi/scsi_cmnd.h
index 8ecfb94049db5..3ff425c5f1e56 100644
--- a/include/scsi/scsi_cmnd.h
+++ b/include/scsi/scsi_cmnd.h
@@ -60,6 +60,8 @@ struct scsi_pointer {
 #define SCMD_FAIL_IF_RECOVERING        (1 << 4)
 /* flags preserved across unprep / reprep */
 #define SCMD_PRESERVED_FLAGS   (SCMD_INITIALIZED | SCMD_FAIL_IF_RECOVERING)
+#define SCMD_MPATH_IO_STATS    (1 << 5)
+#define SCMD_MPATH_CNT_ACTIVE  (1 << 6)
 
 /* for scmd->state */
 #define SCMD_STATE_COMPLETE    0
@@ -139,6 +141,10 @@ struct scsi_cmnd {
                                         * to release this memory.  (The memory
                                         * obtained by scsi_malloc is guaranteed
                                         * to be at an address < 16Mb). */
+       #ifdef CONFIG_SCSI_MULTIPATH
+       unsigned long           start_time;
+       unsigned int            start_bytes;
+       #endif
 
        int result;             /* Status code from lower level driver */
 };
diff --git a/include/scsi/scsi_host.h b/include/scsi/scsi_host.h
index 7e2011830ba4b..cf4fff3ea1a8e 100644
--- a/include/scsi/scsi_host.h
+++ b/include/scsi/scsi_host.h
@@ -753,6 +753,10 @@ struct Scsi_Host {
        /* Delay for runtime autosuspend */
        int rpm_autosuspend_delay;
 
+       #ifdef CONFIG_SCSI_MULTIPATH
+       atomic_t mpath_nr_active;
+       #endif
+
        /*
         * We should ensure that this is aligned, both for better performance
         * and also because some compilers (m68k) don't automatically force
diff --git a/include/scsi/scsi_multipath.h b/include/scsi/scsi_multipath.h
index cc77e63c43da3..c3398e9f03f16 100644
--- a/include/scsi/scsi_multipath.h
+++ b/include/scsi/scsi_multipath.h
@@ -56,6 +56,9 @@ int scsi_mpath_get_head(struct scsi_mpath_head 
*scsi_mpath_head);
 void scsi_mpath_put_head(struct scsi_mpath_head *scsi_mpath_head);
 void scsi_mpath_dev_clear_path(struct scsi_mpath_device *scsi_mpath_dev);
 void scsi_mpath_revalidate_paths(struct scsi_mpath_device *scsi_mpath_dev);
+void scsi_mpath_start_request(struct request *req);
+bool scsi_mpath_end_request(struct request *req, blk_status_t error,
+                              unsigned int nr_bytes);
 #else /* CONFIG_SCSI_MULTIPATH */
 
 struct scsi_mpath_head {
@@ -98,6 +101,14 @@ static inline
 void scsi_mpath_revalidate_paths(struct scsi_mpath_device *scsi_mpath_dev)
 {
 }
+static inline void scsi_mpath_start_request(struct request *req)
+{
+}
+static inline bool scsi_mpath_end_request(struct request *req, blk_status_t 
error,
+                              unsigned int nr_bytes)
+{
+       return false;
+}
 static inline void scsi_mpath_add_sysfs_link(struct scsi_device *sdev)
 {
 }
-- 
2.43.7


Reply via email to