From: Sumit Saxena <sumit.saxena@broadcom.com>
To: "Martin K . Petersen" <martin.petersen@oracle.com>,
Jens Axboe <axboe@kernel.dk>
Cc: "James E . J . Bottomley" <James.Bottomley@HansenPartnership.com>,
linux-scsi@vger.kernel.org, linux-block@vger.kernel.org,
Adam Radford <aradford@gmail.com>,
Khalid Aziz <khalid@gonehiking.org>,
Adaptec OEM Raid Solutions <aacraid@microsemi.com>,
Matthew Wilcox <willy@infradead.org>,
Hannes Reinecke <hare@suse.com>,
"Juergen E . Fischer" <fischer@norbit.de>,
Russell King <linux@armlinux.org.uk>,
linux-arm-kernel@lists.infradead.org,
Finn Thain <fthain@linux-m68k.org>,
Michael Schmitz <schmitzmic@gmail.com>,
Anil Gurumurthy <anil.gurumurthy@qlogic.com>,
Sudarsana Kalluru <sudarsana.kalluru@qlogic.com>,
Oliver Neukum <oliver@neukum.org>, Ali Akcaagac <aliakc@web.de>,
Jamie Lenehan <lenehan@twibble.org>,
Ram Vegesna <ram.vegesna@broadcom.com>,
target-devel@vger.kernel.org,
Bradley Grove <linuxdrivers@attotech.com>,
Satish Kharat <satishkh@cisco.com>,
Sesidhar Baddela <sebaddel@cisco.com>,
Karan Tilak Kumar <kartilak@cisco.com>,
Yihang Li <liyihang9@h-partners.com>,
Don Brace <don.brace@microchip.com>,
storagedev@microchip.com,
HighPoint Linux Team <linux@highpoint-tech.com>,
Tyrel Datwyler <tyreld@linux.ibm.com>,
Madhavan Srinivasan <maddy@linux.ibm.com>,
Michael Ellerman <mpe@ellerman.id.au>,
Nicholas Piggin <npiggin@gmail.com>,
Christophe Leroy <chleroy@kernel.org>,
linuxppc-dev@lists.ozlabs.org, Brian King <brking@us.ibm.com>,
Lee Duncan <lduncan@suse.com>, Chris Leech <cleech@redhat.com>,
Mike Christie <michael.christie@oracle.com>,
open-iscsi@googlegroups.com, Justin Tee <justin.tee@broadcom.com>,
Paul Ely <paul.ely@broadcom.com>,
Kashyap Desai <kashyap.desai@broadcom.com>,
Shivasharan S <shivasharan.srikanteshwara@broadcom.com>,
Chandrakanth Patil <chandrakanth.patil@broadcom.com>,
megaraidlinux.pdl@broadcom.com,
Sathya Prakash Veerichetty <sathya.prakash@broadcom.com>,
Sreekanth Reddy <sreekanth.reddy@broadcom.com>,
mpi3mr-linuxdrv.pdl@broadcom.com,
Suganath Prabu Subramani <suganath-prabu.subramani@broadcom.com>,
Ranjan Kumar <ranjan.kumar@broadcom.com>,
MPT-FusionLinux.pdl@broadcom.com,
Daniel Palmer <daniel@thingy.jp>,
GOTO Masanori <gotom@debian.or.jp>,
YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>,
Jack Wang <jinpu.wang@cloud.ionos.com>,
Geoff Levand <geoff@infradead.org>, Michael Reed <mdr@sgi.com>,
Nilesh Javali <njavali@marvell.com>,
GR-QLogic-Storage-Upstream@marvell.com,
Narsimhulu Musini <nmusini@cisco.com>,
"K . Y . Srinivasan" <kys@microsoft.com>,
Haiyang Zhang <haiyangz@microsoft.com>,
Wei Liu <wei.liu@kernel.org>, Dexuan Cui <decui@microsoft.com>,
Long Li <longli@microsoft.com>,
linux-hyperv@vger.kernel.org,
"Michael S . Tsirkin" <mst@redhat.com>,
Jason Wang <jasowang@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Stefan Hajnoczi <stefanha@redhat.com>,
Eugenio Perez <eperezma@redhat.com>,
virtualization@lists.linux.dev,
Vishal Bhakta <vishal.bhakta@broadcom.com>,
bcm-kernel-feedback-list@broadcom.com,
Juergen Gross <jgross@suse.com>,
Stefano Stabellini <sstabellini@kernel.org>,
Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>,
xen-devel@lists.xenproject.org,
Sumit Saxena <sumit.saxena@broadcom.com>,
John Garry <john.g.garry@oracle.com>
Subject: [PATCH v3 4/4] scsi: use percpu counters for iostat counters in struct scsi_device
Date: Tue, 9 Jun 2026 17:48:03 +0530 [thread overview]
Message-ID: <20260609121806.2121755-5-sumit.saxena@broadcom.com> (raw)
In-Reply-To: <20260609121806.2121755-1-sumit.saxena@broadcom.com>
iorequest_cnt and iodone_cnt are updated on every command dispatch and
completion, often from different CPUs on high queue depth workloads.
Using adjacent atomic_t fields causes cache line contention between the
submission and completion paths.
Extend the same treatment to ioerr_cnt and iotmo_cnt so all four iostat
counters in struct scsi_device use struct percpu_counter.
Suggested-by: John Garry <john.g.garry@oracle.com>
Signed-off-by: Sumit Saxena <sumit.saxena@broadcom.com>
---
drivers/scsi/scsi_error.c | 4 ++--
drivers/scsi/scsi_lib.c | 10 +++++-----
drivers/scsi/scsi_scan.c | 8 ++++++++
drivers/scsi/scsi_sysfs.c | 23 ++++++++++++++---------
drivers/scsi/sd.c | 2 +-
include/scsi/scsi_device.h | 9 +++++----
6 files changed, 35 insertions(+), 21 deletions(-)
diff --git a/drivers/scsi/scsi_error.c b/drivers/scsi/scsi_error.c
index 147127fb4db9..b1aa7da2ba7c 100644
--- a/drivers/scsi/scsi_error.c
+++ b/drivers/scsi/scsi_error.c
@@ -349,7 +349,7 @@ enum blk_eh_timer_return scsi_timeout(struct request *req)
trace_scsi_dispatch_cmd_timeout(scmd);
scsi_log_completion(scmd, TIMEOUT_ERROR);
- atomic_inc(&scmd->device->iotmo_cnt);
+ percpu_counter_inc(&scmd->device->iotmo_cnt);
if (host->eh_deadline != -1 && !host->last_reset)
host->last_reset = jiffies;
@@ -370,7 +370,7 @@ enum blk_eh_timer_return scsi_timeout(struct request *req)
*/
if (test_and_set_bit(SCMD_STATE_COMPLETE, &scmd->state))
return BLK_EH_DONE;
- atomic_inc(&scmd->device->iodone_cnt);
+ percpu_counter_inc(&scmd->device->iodone_cnt);
if (scsi_abort_command(scmd) != SUCCESS) {
set_host_byte(scmd, DID_TIME_OUT);
scsi_eh_scmd_add(scmd);
diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c
index 6e8c7a42603e..979fdace33ac 100644
--- a/drivers/scsi/scsi_lib.c
+++ b/drivers/scsi/scsi_lib.c
@@ -1554,9 +1554,9 @@ static void scsi_complete(struct request *rq)
INIT_LIST_HEAD(&cmd->eh_entry);
- atomic_inc(&cmd->device->iodone_cnt);
+ percpu_counter_inc(&cmd->device->iodone_cnt);
if (cmd->result)
- atomic_inc(&cmd->device->ioerr_cnt);
+ percpu_counter_inc(&cmd->device->ioerr_cnt);
disposition = scsi_decide_disposition(cmd);
if (disposition != SUCCESS && scsi_cmd_runtime_exceeced(cmd))
@@ -1592,7 +1592,7 @@ static enum scsi_qc_status scsi_dispatch_cmd(struct scsi_cmnd *cmd)
struct Scsi_Host *host = cmd->device->host;
int rtn = 0;
- atomic_inc(&cmd->device->iorequest_cnt);
+ percpu_counter_inc(&cmd->device->iorequest_cnt);
/* check if the device is still usable */
if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
@@ -1614,7 +1614,7 @@ static enum scsi_qc_status scsi_dispatch_cmd(struct scsi_cmnd *cmd)
*/
SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
"queuecommand : device blocked\n"));
- atomic_dec(&cmd->device->iorequest_cnt);
+ percpu_counter_dec(&cmd->device->iorequest_cnt);
return SCSI_MLQUEUE_DEVICE_BUSY;
}
@@ -1647,7 +1647,7 @@ static enum scsi_qc_status scsi_dispatch_cmd(struct scsi_cmnd *cmd)
trace_scsi_dispatch_cmd_start(cmd);
rtn = host->hostt->queuecommand(host, cmd);
if (rtn) {
- atomic_dec(&cmd->device->iorequest_cnt);
+ percpu_counter_dec(&cmd->device->iorequest_cnt);
trace_scsi_dispatch_cmd_error(cmd, rtn);
if (rtn != SCSI_MLQUEUE_DEVICE_BUSY &&
rtn != SCSI_MLQUEUE_TARGET_BUSY)
diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c
index 121a14d5fdb8..bc885c72f01e 100644
--- a/drivers/scsi/scsi_scan.c
+++ b/drivers/scsi/scsi_scan.c
@@ -350,6 +350,14 @@ static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
scsi_sysfs_device_initialize(sdev);
+ if (percpu_counter_init(&sdev->iorequest_cnt, 0, GFP_KERNEL) ||
+ percpu_counter_init(&sdev->iodone_cnt, 0, GFP_KERNEL) ||
+ percpu_counter_init(&sdev->ioerr_cnt, 0, GFP_KERNEL) ||
+ percpu_counter_init(&sdev->iotmo_cnt, 0, GFP_KERNEL)) {
+ ret = -ENOMEM;
+ goto out_device_destroy;
+ }
+
if (scsi_device_is_pseudo_dev(sdev))
return sdev;
diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c
index dfc3559e7e04..f652edd16497 100644
--- a/drivers/scsi/scsi_sysfs.c
+++ b/drivers/scsi/scsi_sysfs.c
@@ -516,6 +516,10 @@ static void scsi_device_dev_release(struct device *dev)
if (vpd_pgb7)
kfree_rcu(vpd_pgb7, rcu);
kfree(sdev->inquiry);
+ percpu_counter_destroy(&sdev->iotmo_cnt);
+ percpu_counter_destroy(&sdev->ioerr_cnt);
+ percpu_counter_destroy(&sdev->iodone_cnt);
+ percpu_counter_destroy(&sdev->iorequest_cnt);
kfree(sdev);
if (parent)
@@ -936,26 +940,27 @@ static ssize_t
show_iostat_counterbits(struct device *dev, struct device_attribute *attr,
char *buf)
{
- return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
+ /* iostat counters are per-CPU sums (s64). Report width for tools. */
+ return sysfs_emit(buf, "%zu\n", sizeof(s64) * 8);
}
static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
-#define show_sdev_iostat(field) \
+#define show_sdev_iostat_percpu(field) \
static ssize_t \
show_iostat_##field(struct device *dev, struct device_attribute *attr, \
char *buf) \
{ \
struct scsi_device *sdev = to_scsi_device(dev); \
- unsigned long long count = atomic_read(&sdev->field); \
- return snprintf(buf, 20, "0x%llx\n", count); \
+ unsigned long long count = percpu_counter_sum(&sdev->field); \
+ return sysfs_emit(buf, "0x%llx\n", count); \
} \
-static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
+static DEVICE_ATTR(field, 0444, show_iostat_##field, NULL)
-show_sdev_iostat(iorequest_cnt);
-show_sdev_iostat(iodone_cnt);
-show_sdev_iostat(ioerr_cnt);
-show_sdev_iostat(iotmo_cnt);
+show_sdev_iostat_percpu(iorequest_cnt);
+show_sdev_iostat_percpu(iodone_cnt);
+show_sdev_iostat_percpu(ioerr_cnt);
+show_sdev_iostat_percpu(iotmo_cnt);
static ssize_t
sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index adc3fa55ca2c..b7ce01de17b3 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -4043,7 +4043,7 @@ static int sd_probe(struct scsi_device *sdp)
sdkp->index = index;
sdkp->max_retries = SD_MAX_RETRIES;
atomic_set(&sdkp->openers, 0);
- atomic_set(&sdkp->device->ioerr_cnt, 0);
+ percpu_counter_set(&sdkp->device->ioerr_cnt, 0);
if (!sdp->request_queue->rq_timeout) {
if (sdp->type != TYPE_MOD)
diff --git a/include/scsi/scsi_device.h b/include/scsi/scsi_device.h
index 029f5115b2ea..4be36bf2a475 100644
--- a/include/scsi/scsi_device.h
+++ b/include/scsi/scsi_device.h
@@ -9,6 +9,7 @@
#include <scsi/scsi.h>
#include <scsi/scsi_common.h>
#include <linux/atomic.h>
+#include <linux/percpu_counter.h>
#include <linux/sbitmap.h>
struct bsg_device;
@@ -272,10 +273,10 @@ struct scsi_device {
unsigned int max_device_blocked; /* what device_blocked counts down from */
#define SCSI_DEFAULT_DEVICE_BLOCKED 3
- atomic_t iorequest_cnt;
- atomic_t iodone_cnt;
- atomic_t ioerr_cnt;
- atomic_t iotmo_cnt;
+ struct percpu_counter iorequest_cnt;
+ struct percpu_counter iodone_cnt;
+ struct percpu_counter ioerr_cnt;
+ struct percpu_counter iotmo_cnt;
struct device sdev_gendev,
sdev_dev;
--
2.43.7
next prev parent reply other threads:[~2026-06-10 4:40 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-09 12:17 [PATCH v3 0/4] scsi/block: NUMA-local scan allocations, shared-tag path cleanup, and SCSI I/O counters Sumit Saxena
2026-06-09 12:18 ` [PATCH v3 1/4] scsi: scan: allocate sdev and starget on the NUMA node of the host adapter Sumit Saxena
2026-06-10 6:00 ` Hannes Reinecke
2026-06-09 12:18 ` [PATCH v3 2/4] scsi: host: allocate struct Scsi_Host " Sumit Saxena
2026-06-09 13:03 ` John Garry
2026-06-10 5:59 ` Hannes Reinecke
2026-06-09 12:18 ` [PATCH v3 3/4] block: drop shared-tag fairness throttling Sumit Saxena
2026-06-10 6:14 ` Christoph Hellwig
[not found] ` <CAL2rwxr1uGshb1o=jvP2OnBffNz2cKXj8tHuAUCN5HFuy2vB_g@mail.gmail.com>
2026-06-10 16:35 ` Keith Busch
2026-06-10 6:18 ` Hannes Reinecke
2026-06-09 12:18 ` Sumit Saxena [this message]
2026-06-10 6:21 ` [PATCH v3 4/4] scsi: use percpu counters for iostat counters in struct scsi_device Hannes Reinecke
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=20260609121806.2121755-5-sumit.saxena@broadcom.com \
--to=sumit.saxena@broadcom.com \
--cc=GR-QLogic-Storage-Upstream@marvell.com \
--cc=James.Bottomley@HansenPartnership.com \
--cc=MPT-FusionLinux.pdl@broadcom.com \
--cc=aacraid@microsemi.com \
--cc=aliakc@web.de \
--cc=anil.gurumurthy@qlogic.com \
--cc=aradford@gmail.com \
--cc=axboe@kernel.dk \
--cc=bcm-kernel-feedback-list@broadcom.com \
--cc=brking@us.ibm.com \
--cc=chandrakanth.patil@broadcom.com \
--cc=chleroy@kernel.org \
--cc=cleech@redhat.com \
--cc=daniel@thingy.jp \
--cc=decui@microsoft.com \
--cc=don.brace@microchip.com \
--cc=eperezma@redhat.com \
--cc=fischer@norbit.de \
--cc=fthain@linux-m68k.org \
--cc=geoff@infradead.org \
--cc=gotom@debian.or.jp \
--cc=haiyangz@microsoft.com \
--cc=hare@suse.com \
--cc=jasowang@redhat.com \
--cc=jgross@suse.com \
--cc=jinpu.wang@cloud.ionos.com \
--cc=john.g.garry@oracle.com \
--cc=justin.tee@broadcom.com \
--cc=kartilak@cisco.com \
--cc=kashyap.desai@broadcom.com \
--cc=khalid@gonehiking.org \
--cc=kys@microsoft.com \
--cc=lduncan@suse.com \
--cc=lenehan@twibble.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-hyperv@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=linux@highpoint-tech.com \
--cc=linuxdrivers@attotech.com \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=liyihang9@h-partners.com \
--cc=longli@microsoft.com \
--cc=maddy@linux.ibm.com \
--cc=martin.petersen@oracle.com \
--cc=mdr@sgi.com \
--cc=megaraidlinux.pdl@broadcom.com \
--cc=michael.christie@oracle.com \
--cc=mpe@ellerman.id.au \
--cc=mpi3mr-linuxdrv.pdl@broadcom.com \
--cc=mst@redhat.com \
--cc=njavali@marvell.com \
--cc=nmusini@cisco.com \
--cc=npiggin@gmail.com \
--cc=oleksandr_tyshchenko@epam.com \
--cc=oliver@neukum.org \
--cc=open-iscsi@googlegroups.com \
--cc=paul.ely@broadcom.com \
--cc=pbonzini@redhat.com \
--cc=ram.vegesna@broadcom.com \
--cc=ranjan.kumar@broadcom.com \
--cc=sathya.prakash@broadcom.com \
--cc=satishkh@cisco.com \
--cc=schmitzmic@gmail.com \
--cc=sebaddel@cisco.com \
--cc=shivasharan.srikanteshwara@broadcom.com \
--cc=sreekanth.reddy@broadcom.com \
--cc=sstabellini@kernel.org \
--cc=stefanha@redhat.com \
--cc=storagedev@microchip.com \
--cc=sudarsana.kalluru@qlogic.com \
--cc=suganath-prabu.subramani@broadcom.com \
--cc=target-devel@vger.kernel.org \
--cc=tyreld@linux.ibm.com \
--cc=virtualization@lists.linux.dev \
--cc=vishal.bhakta@broadcom.com \
--cc=wei.liu@kernel.org \
--cc=willy@infradead.org \
--cc=xen-devel@lists.xenproject.org \
--cc=yokota@netlab.is.tsukuba.ac.jp \
/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