From: Bart Van Assche <bvanassche@acm.org>
To: "Martin K . Petersen" <martin.petersen@oracle.com>
Cc: linux-scsi@vger.kernel.org, linux-block@vger.kernel.org,
John Garry <john.g.garry@oracle.com>,
Hannes Reinecke <hare@suse.de>,
Bart Van Assche <bvanassche@acm.org>,
Jens Axboe <axboe@kernel.dk>,
Christoph Hellwig <hch@infradead.org>,
Ming Lei <ming.lei@redhat.com>,
"James E.J. Bottomley" <James.Bottomley@HansenPartnership.com>
Subject: [PATCH v2 4/5] scsi: core: Generalize scsi_device_busy()
Date: Mon, 17 Nov 2025 14:52:03 -0800 [thread overview]
Message-ID: <20251117225205.2024479-5-bvanassche@acm.org> (raw)
In-Reply-To: <20251117225205.2024479-1-bvanassche@acm.org>
Instead of only handling dev->budget_map.map != NULL, also handle
dev->budget_map.map == NULL. This patch prepares for supporting logical
units without budget map (sdev->budget_map.map == NULL).
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Ming Lei <ming.lei@redhat.com>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Hannes Reinecke <hare@suse.de>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
drivers/scsi/scsi_lib.c | 38 ++++++++++++++++++++++++++++++++++++++
include/scsi/scsi_device.h | 5 +----
2 files changed, 39 insertions(+), 4 deletions(-)
diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c
index 51ad2ad07e43..ddc51472b5eb 100644
--- a/drivers/scsi/scsi_lib.c
+++ b/drivers/scsi/scsi_lib.c
@@ -446,6 +446,44 @@ static void scsi_single_lun_run(struct scsi_device *current_sdev)
spin_unlock_irqrestore(shost->host_lock, flags);
}
+struct sdev_cmds_allocated_data {
+ const struct scsi_device *sdev;
+ int count;
+};
+
+static bool scsi_device_check_allocated(struct request *rq, void *data)
+{
+ struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
+ struct sdev_cmds_allocated_data *sifd = data;
+
+ if (cmd->device == sifd->sdev)
+ sifd->count++;
+
+ return true;
+}
+
+/**
+ * scsi_device_busy() - Number of commands allocated for a SCSI device
+ * @sdev: SCSI device.
+ *
+ * Note: There is a subtle difference between this function and
+ * scsi_host_busy(). scsi_host_busy() counts the number of commands that have
+ * been started. This function counts the number of commands that have been
+ * allocated. At least the UFS driver depends on this function counting commands
+ * that have already been allocated but that have not yet been started.
+ */
+int scsi_device_busy(const struct scsi_device *sdev)
+{
+ struct sdev_cmds_allocated_data sifd = { .sdev = sdev };
+ struct blk_mq_tag_set *set = &sdev->host->tag_set;
+
+ if (sdev->budget_map.map)
+ return sbitmap_weight(&sdev->budget_map);
+ blk_mq_tagset_iter(set, scsi_device_check_allocated, &sifd);
+ return sifd.count;
+}
+EXPORT_SYMBOL(scsi_device_busy);
+
static inline bool scsi_device_is_busy(struct scsi_device *sdev)
{
if (scsi_device_busy(sdev) >= sdev->queue_depth)
diff --git a/include/scsi/scsi_device.h b/include/scsi/scsi_device.h
index d62265d12cfe..661f0a8e4de6 100644
--- a/include/scsi/scsi_device.h
+++ b/include/scsi/scsi_device.h
@@ -713,10 +713,7 @@ static inline int scsi_device_supports_vpd(struct scsi_device *sdev)
return 0;
}
-static inline int scsi_device_busy(struct scsi_device *sdev)
-{
- return sbitmap_weight(&sdev->budget_map);
-}
+int scsi_device_busy(const struct scsi_device *sdev);
/* Macros to access the UNIT ATTENTION counters */
#define scsi_get_ua_new_media_ctr(sdev) \
next prev parent reply other threads:[~2025-11-17 22:52 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-17 22:51 [PATCH v2 0/5] Increase SCSI IOPS Bart Van Assche
2025-11-17 22:52 ` [PATCH v2 1/5] block: Rename busy_tag_iter_fn into blk_mq_rq_iter_fn Bart Van Assche
2025-11-17 22:52 ` [PATCH v2 2/5] block: Introduce __blk_mq_tagset_iter() Bart Van Assche
2025-11-17 22:52 ` [PATCH v2 3/5] block: Introduce blk_mq_tagset_iter() Bart Van Assche
2025-11-17 22:52 ` Bart Van Assche [this message]
2025-11-17 22:52 ` [PATCH v2 5/5] scsi: core: Improve IOPS in case of host-wide tags Bart Van Assche
2025-11-21 21:17 ` [PATCH v2 0/5] Increase SCSI IOPS Bart Van Assche
-- strict thread matches above, loose matches on Subject: below --
2025-11-24 18:21 Bart Van Assche
2025-11-24 18:21 ` [PATCH v2 4/5] scsi: core: Generalize scsi_device_busy() Bart Van Assche
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251117225205.2024479-5-bvanassche@acm.org \
--to=bvanassche@acm.org \
--cc=James.Bottomley@HansenPartnership.com \
--cc=axboe@kernel.dk \
--cc=hare@suse.de \
--cc=hch@infradead.org \
--cc=john.g.garry@oracle.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=martin.petersen@oracle.com \
--cc=ming.lei@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).