From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f225.google.com (mail-pl1-f225.google.com [209.85.214.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B4E94014A2 for ; Tue, 9 Jun 2026 11:50:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.225 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781005810; cv=none; b=dvTgBBINGpY2RfY0m5YtZupNxX9xVDjCB8yeis8PKR6iMYWTX27hcaIUlZ6tXIcF9OPzWIF4BCH8kEncrcXwq7Dzvozeygpg6BfVFDcbL/miAeJpVoftj73Exwfq/UcuJQ6wSL0Z9USBIAwa7TihwnSlxSLs7gDroundJZVLnqU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781005810; c=relaxed/simple; bh=3Kv41lKgpOl5wee6Wmx4e4vTLz9/OZ6j/+o4p0XS7OM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=L/QwfhFQ7qq6fcH5/0dYCsnYOPhYkQgWORuiiVCbxrPu5qUf/V4gxhRR5nMcR9Lmrw3zV9V1DrdAuuq5Unlvb1zRgQef4KPVIKxBvlbdeovlQWgGBNm9v0msH+7SNIWMXEsXL7l2nRcTkaE6Qj9noIST/+M9vXPd4U+uIhhohTU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=broadcom.com; spf=fail smtp.mailfrom=broadcom.com; dkim=pass (1024-bit key) header.d=broadcom.com header.i=@broadcom.com header.b=Cm51PGc2; arc=none smtp.client-ip=209.85.214.225 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=broadcom.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=broadcom.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=broadcom.com header.i=@broadcom.com header.b="Cm51PGc2" Received: by mail-pl1-f225.google.com with SMTP id d9443c01a7336-2c132ac5ec2so53122785ad.1 for ; Tue, 09 Jun 2026 04:50:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781005808; x=1781610608; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fLAZlEa/gpJjIgpIJ2e9Ooy6MTW++uhKEEc/j768mlo=; b=ZzFpP7k2OYNhgOaX2o3pc9TgS2WoA2lDOniQRxTZbZg37lU4IfoC8agzv7Qpuz0fVA 02WngcITVWnM+tspMhoCfGZPAy1uqzB5g4tWgOw4TWq+hS4+jeNNrumTPziwOh42O0y9 uRvWs0EblQzthUJ/L9IHmyA1CyeMLowjqN95eTB9MhhUOzG4xRxQFvma5ho1HVgvOP5/ jbGmko1Adl9vL1RpElnXLTvsW86FGSzO36i6KlOGlG9Bm40jhKUAvO/IW4E4s19WDKdX QxFKqiek6Br1dSM5VcKe7gwrN8FtNFfuysj0n4Aqywyy82YzEa4m92rlD4WHiUP2W76l /uyg== X-Forwarded-Encrypted: i=1; AFNElJ8O14zRvZOuML4Ex2uJ4u884VIx2IqHuEBl+6Bm/lpq9KTwhODm333UBWBJ/FWHxjby6CPDQRV93NUc7A==@vger.kernel.org X-Gm-Message-State: AOJu0YwT8Nnpwjr80CDkSuJmAR+GCOF2lB04uItqFzM699O9f0qjwXVy jVXRexN1DalcC0uqzecWcsNKQX0gvc89Zzb/+ZapzkjnWtqZUSor3n0BWhv+o66CDt/9lL6nRab 8Fey2cc/l/7DDfrjAhz7TRFQHc/ra3oXIp2yokPD0BDKWR4aFaBEZjfTedsqsifvTo3fN8d+iqx pLBx3vKLuXBnBSEDin5dgVX+65Zhu6tBwCQL5YJPJSHFb0n8XZCU/E1k+1uM6XArT2vGVmlMiaL At+bO/hc7HC9Dw3DQ== X-Gm-Gg: Acq92OHmy8n0rFnaBuFMS3yDiU4baOJ5u0Zgg5H3appQaBLEwLe41wweWVpAy63SRdb RvVJ01KOKjI3FzdSwtEzH/3Jp+BPWqQm5HJotliSgD1A4QqE1mRvf8GwN061TTEAC42eCKnm4Nl n97X8HqFATIHxY+kXke9NkMofurd5TDyXTjPHQaZscdngh1rNExCmLoUYNn/349Pi7y/+QsKlvf 3aeP9OZELDI0UBO7z/6rIkGJK3Ie0Rgjr+IBtVKlbDBimDn6wSY5bZVJivoUk70LMDsORpS33Tr rfk1kIlOwlLwp63tileeqgKuYzvJHANB8jN/f0WEyAAI4cS3yKmGB21MXAk8tqhwMwzl+ovn7FW EHteGdY7UuaPNbycIg6qmgaH4k/6rfmTgAVALLp3EiJd/it8xGkEItgAi8zgCEf3XoPKLmoOXiv snQIuZN2zRPI8ayr4= X-Received: by 2002:a17:903:248:b0:2c1:d49c:8396 with SMTP id d9443c01a7336-2c1e7b35a28mr231344475ad.1.1781005807678; Tue, 09 Jun 2026 04:50:07 -0700 (PDT) Received: from smtp-us-east1-p01-i01-si01.dlp.protect.broadcom.com ([144.49.247.127]) by smtp-relay.gmail.com with ESMTPS id d9443c01a7336-2c164fa780esm19221675ad.23.2026.06.09.04.50.07 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 09 Jun 2026 04:50:07 -0700 (PDT) X-Relaying-Domain: broadcom.com X-CFilter-Loop: Reflected Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-36bc54005a7so7347342a91.0 for ; Tue, 09 Jun 2026 04:50:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; t=1781005806; x=1781610606; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fLAZlEa/gpJjIgpIJ2e9Ooy6MTW++uhKEEc/j768mlo=; b=Cm51PGc2QdV8g1RHRzxw8t/lDGzGZYGexsMd7UFYDiUfAukSeTvOvSYr2syyiSs51w BYOA7NvlwQYGDJPgr1RGpRd5zHqNNA82aDlR6HOKlvfdIIke3BboxXGU+ODnps5xgIg7 zR8uBfo2lrgdPJKeQEslVawVI8gyMzZFO/Kwc= X-Forwarded-Encrypted: i=1; AFNElJ/pDfIOZLwV1SAnfY/Gh1O+VeWCf4o6Ra68VsE+Vithx+VoK2XIcMJlq9b0UsPz77m/NWJ8ZwJP6gIOkg==@vger.kernel.org X-Received: by 2002:a17:90b:57c4:b0:36b:75:6387 with SMTP id 98e67ed59e1d1-370eea202f0mr20609560a91.8.1781005805614; Tue, 09 Jun 2026 04:50:05 -0700 (PDT) X-Received: by 2002:a17:90b:57c4:b0:36b:75:6387 with SMTP id 98e67ed59e1d1-370eea202f0mr20609495a91.8.1781005805070; Tue, 09 Jun 2026 04:50:05 -0700 (PDT) Received: from sumit_ws.dhcp.broadcom.net ([192.19.234.250]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-36f6bf903fasm18898075a91.2.2026.06.09.04.49.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jun 2026 04:50:03 -0700 (PDT) From: Sumit Saxena To: "Martin K . Petersen" , Jens Axboe Cc: "James E . J . Bottomley" , linux-scsi@vger.kernel.org, linux-block@vger.kernel.org, Adam Radford , Khalid Aziz , Adaptec OEM Raid Solutions , Matthew Wilcox , Hannes Reinecke , "Juergen E . Fischer" , Russell King , linux-arm-kernel@lists.infradead.org, Finn Thain , Michael Schmitz , Anil Gurumurthy , Sudarsana Kalluru , Oliver Neukum , Ali Akcaagac , Jamie Lenehan , Ram Vegesna , target-devel@vger.kernel.org, Bradley Grove , Satish Kharat , Sesidhar Baddela , Karan Tilak Kumar , Yihang Li , Don Brace , storagedev@microchip.com, HighPoint Linux Team , Tyrel Datwyler , Madhavan Srinivasan , Michael Ellerman , Nicholas Piggin , Christophe Leroy , linuxppc-dev@lists.ozlabs.org, Brian King , Lee Duncan , Chris Leech , Mike Christie , open-iscsi@googlegroups.com, Justin Tee , Paul Ely , Kashyap Desai , Shivasharan S , Chandrakanth Patil , megaraidlinux.pdl@broadcom.com, Sathya Prakash Veerichetty , Sreekanth Reddy , mpi3mr-linuxdrv.pdl@broadcom.com, Suganath Prabu Subramani , Ranjan Kumar , MPT-FusionLinux.pdl@broadcom.com, Daniel Palmer , GOTO Masanori , YOKOTA Hiroshi , Jack Wang , Geoff Levand , Michael Reed , Nilesh Javali , GR-QLogic-Storage-Upstream@marvell.com, Narsimhulu Musini , "K . Y . Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Long Li , linux-hyperv@vger.kernel.org, "Michael S . Tsirkin" , Jason Wang , Paolo Bonzini , Stefan Hajnoczi , Eugenio Perez , virtualization@lists.linux.dev, Vishal Bhakta , bcm-kernel-feedback-list@broadcom.com, Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko , xen-devel@lists.xenproject.org, Sumit Saxena , John Garry 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 Message-ID: <20260609121806.2121755-5-sumit.saxena@broadcom.com> X-Mailer: git-send-email 2.43.7 In-Reply-To: <20260609121806.2121755-1-sumit.saxena@broadcom.com> References: <20260609121806.2121755-1-sumit.saxena@broadcom.com> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-DetectorID-Processed: b00c1d49-9d2e-4205-b15f-d015386d3d5e 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 Signed-off-by: Sumit Saxena --- 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 #include #include +#include #include 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