Skip to contentSkip to conversation

scsi/block: NUMA-local scan allocations, shared-tag path cleanup, and SCSI I/O counters

PATCH Superseded Sumit Saxena 3 participants 12 messages Apr 20 – Apr 28

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover scsi/block: NUMA-local scan allocations, shared-tag path cleanup, and SCSI I/O counters

This series contains three performance improvements targeting the SCSI and block layers on multi-socket NUMA and heavily loaded SMP systems.

On multi-socket NUMA systems we observed extreme I/O throughput variance of 50-60% between runs. This series identifies and fixes two root causes:
cross-node memory accesses due to NUMA-unaware allocations in the scan path, and false sharing between hot atomic counters in struct request_queue and struct scsi_device.

Performance notes:

Tested on a dual-socket NUMA system (2x 32-core, 256 GB/socket) with an mpi3mr HBA, running fio (random read, 4K, QD 64, 16 jobs, 60 s, direct I/O). IOPS figures are in KIOPS (thousands of IOPS):

  Configuration                    Avg KIOPS   Range (KIOPS)   Spread
  Baseline                         6,255       4,200 - 6,700   ~37%
  Baseline + all patches           7,350       7,000 - 7,700    ~10%

Key findings:

These patches combinedly reduces the observed 50-60% run-to-run variance to under 10%, significantly improving workload predictability and improves IOPs by 16-18%.

No functional regressions observed.

This patch series is based on Martin's for-next tree.

Changes in v2
--------------

Patch 1 — Same functional goal as v1 patch 1: NUMA-local scsi_device / scsi_target allocations in the scan path so steady-state I/O does not habitually touch remote memory when the host has a fixed DMA/NUMA affinity.

Patch 2 — Replaces v1’s ____cacheline_aligned_in_smp on nr_active_requests_shared_tags with shared tags removal work done by Bart Van Assche [1], rebased for the current tree; it removes the atomic counter- nr_active_request_shared_tags that motivated the v1 false-sharing workaround and, in our testing, improves IOPS on the order of roughly 16–18% for the shared-tag workload exercised. This patch touches include/linux/blkdev.h, so needs review from [email protected]; an Acked-by from the block maintainer is requested before merging via the SCSI tree.

Patch 3 — Replaces v1’s cache-line padding of iodone_cnt with percpu_counter for both iorequest_cnt and iodone_cnt, so submission and completion paths mostly update CPU-local state instead of bouncing a single cache line, without inflating struct scsi_device for SMP alignment.

[1]: https://lore.kernel.org/linux-block/[email protected]/

James Rizzo (1):
scsi: scan: allocate sdev and starget on the NUMA node of the host adapter

Bart Van Assche (1):
block: drop shared-tag fairness throttling

Sumit Saxena (1):
scsi: use percpu counters for iorequest_cnt and iodone_cnt

 12 files changed, 68 insertions(+), 145 deletions(-)

---
2.43.7

#1 lore raw

PATCH v2 1/3 scsi: scan: allocate sdev and starget on the NUMA node of the host adapter

From: James Rizzo <[email protected]>

When a host adapter is attached to a specific NUMA node, allocating scsi_device and scsi_target via kzalloc() may place them on a remote node. All hot-path I/O accesses to these structures then cross the NUMA interconnect, adding latency and consuming inter-node bandwidth.

Use kzalloc_node() with dev_to_node(shost->dma_dev) so allocations land on the same node as the HBA, reducing cross-node traffic and improving I/O performance on NUMA systems.

---
 drivers/scsi/scsi_scan.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/scsi/scsi_scan.c +6 -3
index ef22a4228b85..9749a8dbe964 100644
@@ -34,6 +34,7 @@
 #include <linux/kthread.h>
 #include <linux/spinlock.h>
 #include <linux/async.h>
+#include <linux/topology.h>
 #include <linux/slab.h>
 #include <linux/unaligned.h>
 
@@ -286,9 +287,10 @@ static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
 	int display_failure_msg = 1, ret;
 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
 	struct queue_limits lim;
+	int node = dev_to_node(shost->dma_dev);
 
-	sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
-		       GFP_KERNEL);
+	sdev = kzalloc_node(sizeof(*sdev) + shost->transportt->device_size,
+		       GFP_KERNEL, node);
 	if (!sdev)
 		goto out;
 
@@ -501,8 +503,9 @@ static struct scsi_target *scsi_alloc_target(struct device *parent,
 	struct scsi_target *starget;
 	struct scsi_target *found_target;
 	int error, ref_got;
+	int node = dev_to_node(shost->dma_dev);
 
-	starget = kzalloc(size, GFP_KERNEL);
+	starget = kzalloc_node(size, GFP_KERNEL, node);
 	if (!starget) {
 		printk(KERN_ERR "%s: allocation failure\n", __func__);
 		return NULL;
Signature · 1 line
2.43.7
#2 lore raw

Re: [PATCH v2 1/3] scsi: scan: allocate sdev and starget on the NUMA node of the host adapter

Quoted 10 lines · Sumit Saxena

From: James Rizzo <[email protected]>

When a host adapter is attached to a specific NUMA node, allocating scsi_device and scsi_target via kzalloc() may place them on a remote node. All hot-path I/O accesses to these structures then cross the NUMA interconnect, adding latency and consuming inter-node bandwidth.

Use kzalloc_node() with dev_to_node(shost->dma_dev) so allocations land on the same node as the HBA, reducing cross-node traffic and improving I/O performance on NUMA systems.

I suppose that this makes sense. We already do this sort of thing in scsi_mq_setup_tags() (in setting numa node) and scsi_realloc_sdev_budget_map() -> sbitmap_init_node(sdev->request_queue->numa_node)

For the actual shost allocation, we still use kzalloc() in scsi_host_alloc(). However, shost associated device is often a pci device, and we probe pci devices in the same NUMA node it exists, and we try NUMA local allocations by default, so nothing is needed to change for the shost allocation - is this right?

Quoted 20 lines
---
  drivers/scsi/scsi_scan.c | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)
drivers/scsi/scsi_scan.c +1 -0
index ef22a4228b85..9749a8dbe964 100644
@@ -34,6 +34,7 @@
  #include <linux/kthread.h>
  #include <linux/spinlock.h>
  #include <linux/async.h>
+#include <linux/topology.h>f
  #include <linux/slab.h>
  #include <linux/unaligned.h>
 

drivers/scsi/scsi_scan.c static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget, · line 290

Quoted 4 lines
drivers/scsi/scsi_scan.c +1 -0
@@ -286,9 +287,10 @@ static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
  	int display_failure_msg = 1, ret;
  	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  	struct queue_limits lim;
+	int node = dev_to_node(shost->dma_dev);

this variable is only used once, so we can use dev_to_node(shost->dma_dev) directly

drivers/scsi/scsi_scan.c static struct scsi_target *scsi_alloc_target(struct device *parent, · line 506

Quoted 13 lines
drivers/scsi/scsi_scan.c +3 -2
-	sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
-		       GFP_KERNEL);
+	sdev = kzalloc_node(sizeof(*sdev) + shost->transportt->device_size,
+		       GFP_KERNEL, node);
  	if (!sdev)
  		goto out;
 
@@ -501,8 +503,9 @@ static struct scsi_target *scsi_alloc_target(struct device *parent,
  	struct scsi_target *starget;
  	struct scsi_target *found_target;
  	int error, ref_got;
+	int node = dev_to_node(shost->dma_dev);

same as above

Quoted 6 lines
drivers/scsi/scsi_scan.c +1 -1
-	starget = kzalloc(size, GFP_KERNEL);
+	starget = kzalloc_node(size, GFP_KERNEL, node);
  	if (!starget) {
  		printk(KERN_ERR "%s: allocation failure\n", __func__);
  		return NULL;
#3 lore raw

Re: [PATCH v2 1/3] scsi: scan: allocate sdev and starget on the NUMA node of the host adapter

Quoted 18 lines · John Garry
Quoted 10 lines · Sumit Saxena

From: James Rizzo <[email protected]>

When a host adapter is attached to a specific NUMA node, allocating scsi_device and scsi_target via kzalloc() may place them on a remote node. All hot-path I/O accesses to these structures then cross the NUMA interconnect, adding latency and consuming inter-node bandwidth.

Use kzalloc_node() with dev_to_node(shost->dma_dev) so allocations land on the same node as the HBA, reducing cross-node traffic and improving I/O performance on NUMA systems.

I suppose that this makes sense. We already do this sort of thing in scsi_mq_setup_tags() (in setting numa node) and scsi_realloc_sdev_budget_map() -> sbitmap_init_node(sdev->request_queue->numa_node)

Quoted 5 lines

For the actual shost allocation, we still use kzalloc() in scsi_host_alloc(). However, shost associated device is often a pci device, and we probe pci devices in the same NUMA node it exists, and we try NUMA local allocations by default, so nothing is needed to change for the shost allocation - is this right?

shost allocation is not an issue, I will drop changes related to it. The tests indicate that at times sdev and starget are allocated to remote NUMA node.
So, I will limit these changes to sdev and starget only.

Quoted 20 lines
Quoted 20 lines
---
  drivers/scsi/scsi_scan.c | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)
drivers/scsi/scsi_scan.c +1 -0
index ef22a4228b85..9749a8dbe964 100644
@@ -34,6 +34,7 @@
  #include <linux/kthread.h>
  #include <linux/spinlock.h>
  #include <linux/async.h>
+#include <linux/topology.h>f
  #include <linux/slab.h>
  #include <linux/unaligned.h>
 
Quoted 8 lines

drivers/scsi/scsi_scan.c static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget, · line 290

Quoted 5 lines
drivers/scsi/scsi_scan.c +1 -0
@@ -286,9 +287,10 @@ static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
      int display_failure_msg = 1, ret;
      struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
      struct queue_limits lim;
+     int node = dev_to_node(shost->dma_dev);

this variable is only used once, so we can use
dev_to_node(shost->dma_dev) directly

Ack

Quoted 9 lines
Quoted 9 lines
drivers/scsi/scsi_scan.c +2 -2
-     sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
-                    GFP_KERNEL);
+     sdev = kzalloc_node(sizeof(*sdev) + shost->transportt->device_size,
+                    GFP_KERNEL, node);
      if (!sdev)
              goto out;
 
Quoted 7 lines

drivers/scsi/scsi_scan.c static struct scsi_target *scsi_alloc_target(struct device *parent, · line 506

Quoted 5 lines
drivers/scsi/scsi_scan.c +1 -0
@@ -501,8 +503,9 @@ static struct scsi_target *scsi_alloc_target(struct device *parent,
      struct scsi_target *starget;
      struct scsi_target *found_target;
      int error, ref_got;
+     int node = dev_to_node(shost->dma_dev);

same as above

Ack

Quoted 8 lines
Quoted 7 lines
drivers/scsi/scsi_scan.c +1 -1
-     starget = kzalloc(size, GFP_KERNEL);
+     starget = kzalloc_node(size, GFP_KERNEL, node);
      if (!starget) {
              printk(KERN_ERR "%s: allocation failure\n", __func__);
              return NULL;
#4 lore raw

Re: [PATCH v2 1/3] scsi: scan: allocate sdev and starget on the NUMA node of the host adapter

Quoted 6 lines · Sumit Saxena
Quoted 5 lines

For the actual shost allocation, we still use kzalloc() in scsi_host_alloc(). However, shost associated device is often a pci device, and we probe pci devices in the same NUMA node it exists, and we try NUMA local allocations by default, so nothing is needed to change for the shost allocation - is this right?

shost allocation is not an issue, I will drop changes related to it.

But you have not made any changes related shost allocation and I am questioning why.

Further to what I was saying, even though most shosts are based on PCI device, not all are and I think that it is worth making this same change for shost allocation.

Quoted 3 lines

The tests indicate that at times sdev and starget are allocated to remote NUMA node.
So, I will limit these changes to sdev and starget only.

#5 lore raw

Re: [PATCH v2 1/3] scsi: scan: allocate sdev and starget on the NUMA node of the host adapter

Quoted 11 lines · John Garry
Quoted 6 lines · Sumit Saxena
Quoted 5 lines

For the actual shost allocation, we still use kzalloc() in scsi_host_alloc(). However, shost associated device is often a pci device, and we probe pci devices in the same NUMA node it exists, and we try NUMA local allocations by default, so nothing is needed to change for the shost allocation - is this right?

shost allocation is not an issue, I will drop changes related to it.

But you have not made any changes related shost allocation and I am questioning why.

Yes, sorry for the confusion.

Quoted 4 lines

Further to what I was saying, even though most shosts are based on PCI device, not all are and I think that it is worth making this same change for shost allocation.

Ok, I will add similar changes for shost allocation in v3.

Quoted 5 lines
Quoted 4 lines

The tests indicate that at times sdev and starget are allocated to remote NUMA node.
So, I will limit these changes to sdev and starget only.

#6 lore raw

PATCH v2 2/3 block: drop shared-tag fairness throttling

In reply to Sumit Saxena

From: Bart Van Assche <[email protected]>

Original patch [1] by Bart Van Assche; this version is rebased onto the current tree. In testing it improves IOPS by roughly 16-18% by removing the fair-sharing throttle on shared tag queues.

This patch removes the following code and structure members:
- The function hctx_may_queue().
- blk_mq_hw_ctx.nr_active and request_queue.nr_active_requests_shared_tags and also all the code that modifies these two member variables.

[1]: https://lore.kernel.org/linux-block/[email protected]/

---
 block/blk-core.c       |   2 -
 block/blk-mq-debugfs.c |  22 ++++++++-
 block/blk-mq-tag.c     |   4 --
 block/blk-mq.c         |  17 +------
 block/blk-mq.h         | 100 -----------------------------------------
 include/linux/blk-mq.h |   6 ---
 include/linux/blkdev.h |   2 -
 7 files changed, 22 insertions(+), 131 deletions(-)
block/blk-core.c +0 -2
index 474700ffaa1c..430907b26fc4 100644
@@ -421,8 +421,6 @@ struct request_queue *blk_alloc_queue(struct queue_limits *lim, int node_id)
 
 	q->node = node_id;
 
-	atomic_set(&q->nr_active_requests_shared_tags, 0);
-
 	timer_setup(&q->timeout, blk_rq_timed_out_timer, 0);
 	INIT_WORK(&q->timeout_work, blk_timeout_work);
 	INIT_LIST_HEAD(&q->icq_list);
block/blk-mq-debugfs.c +21 -1
index 28167c9baa55..6ef922d7abc1 100644
@@ -467,11 +467,31 @@ static int hctx_sched_tags_bitmap_show(void *data, struct seq_file *m)
 	return 0;
 }
 
+struct count_active_params {
+	struct blk_mq_hw_ctx	*hctx;
+	int			*active;
+};
+
+static bool hctx_count_active(struct request *rq, void *data)
+{
+	const struct count_active_params *params = data;
+
+	if (rq->mq_hctx == params->hctx)
+		(*params->active)++;
+
+	return true;
+}
+
 static int hctx_active_show(void *data, struct seq_file *m)
 {
 	struct blk_mq_hw_ctx *hctx = data;
+	int active = 0;
+	struct count_active_params params = { .hctx = hctx, .active = &active };
+
+	blk_mq_all_tag_iter(hctx->sched_tags ?: hctx->tags, hctx_count_active,
+			    &params);
 
-	seq_printf(m, "%d\n", __blk_mq_active_requests(hctx));
+	seq_printf(m, "%d\n", active);
 	return 0;
 }
 
block/blk-mq-tag.c +0 -4
index 33946cdb5716..bfd27cc6249b 100644
@@ -109,10 +109,6 @@ void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
 static int __blk_mq_get_tag(struct blk_mq_alloc_data *data,
 			    struct sbitmap_queue *bt)
 {
-	if (!data->q->elevator && !(data->flags & BLK_MQ_REQ_RESERVED) &&
-			!hctx_may_queue(data->hctx, bt))
-		return BLK_MQ_NO_TAG;
-
 	if (data->shallow_depth)
 		return sbitmap_queue_get_shallow(bt, data->shallow_depth);
 	else
block/blk-mq.c +1 -16
index 9af8c3dec3f6..3c54000bc554 100644
@@ -489,8 +489,6 @@ __blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data)
 		}
 	} while (data->nr_tags > nr);
 
-	if (!(data->rq_flags & RQF_SCHED_TAGS))
-		blk_mq_add_active_requests(data->hctx, nr);
 	/* caller already holds a reference, add for remainder */
 	percpu_ref_get_many(&data->q->q_usage_counter, nr - 1);
 	data->nr_tags -= nr;
@@ -587,8 +585,6 @@ static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data)
 		goto retry;
 	}
 
-	if (!(data->rq_flags & RQF_SCHED_TAGS))
-		blk_mq_inc_active_requests(data->hctx);
 	rq = blk_mq_rq_ctx_init(data, blk_mq_tags_from_data(data), tag);
 	blk_mq_rq_time_init(rq, alloc_time_ns);
 	return rq;
@@ -763,8 +759,6 @@ struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
 	tag = blk_mq_get_tag(&data);
 	if (tag == BLK_MQ_NO_TAG)
 		goto out_queue_exit;
-	if (!(data.rq_flags & RQF_SCHED_TAGS))
-		blk_mq_inc_active_requests(data.hctx);
 	rq = blk_mq_rq_ctx_init(&data, blk_mq_tags_from_data(&data), tag);
 	blk_mq_rq_time_init(rq, alloc_time_ns);
 	rq->__data_len = 0;
@@ -807,10 +801,8 @@ static void __blk_mq_free_request(struct request *rq)
 	blk_pm_mark_last_busy(rq);
 	rq->mq_hctx = NULL;
 
-	if (rq->tag != BLK_MQ_NO_TAG) {
-		blk_mq_dec_active_requests(hctx);
+	if (rq->tag != BLK_MQ_NO_TAG)
 		blk_mq_put_tag(hctx->tags, ctx, rq->tag);
-	}
 	if (sched_tag != BLK_MQ_NO_TAG)
 		blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag);
 	blk_mq_sched_restart(hctx);
@@ -1188,8 +1180,6 @@ static inline void blk_mq_flush_tag_batch(struct blk_mq_hw_ctx *hctx,
 {
 	struct request_queue *q = hctx->queue;
 
-	blk_mq_sub_active_requests(hctx, nr_tags);
-
 	blk_mq_put_tags(hctx->tags, tag_array, nr_tags);
 	percpu_ref_put_many(&q->q_usage_counter, nr_tags);
 }
@@ -1875,9 +1865,6 @@ bool __blk_mq_alloc_driver_tag(struct request *rq)
 	if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag)) {
 		bt = &rq->mq_hctx->tags->breserved_tags;
 		tag_offset = 0;
-	} else {
-		if (!hctx_may_queue(rq->mq_hctx, bt))
-			return false;
 	}
 
 	tag = __sbitmap_queue_get(bt);
@@ -1885,7 +1872,6 @@ bool __blk_mq_alloc_driver_tag(struct request *rq)
 		return false;
 
 	rq->tag = tag + tag_offset;
-	blk_mq_inc_active_requests(rq->mq_hctx);
 	return true;
 }
 
@@ -4037,7 +4023,6 @@ blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set,
 	if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node))
 		goto free_hctx;
 
-	atomic_set(&hctx->nr_active, 0);
 	if (node == NUMA_NO_NODE)
 		node = set->numa_node;
 	hctx->numa_node = node;
block/blk-mq.h +0 -100

115 lines not on this page · raw message

include/linux/blk-mq.h +0 -6
index 18a2388ba581..ccbb07559402 100644
@@ -432,12 +432,6 @@ struct blk_mq_hw_ctx {
 	/** @queue_num: Index of this hardware queue. */
 	unsigned int		queue_num;
 
-	/**
-	 * @nr_active: Number of active requests. Only used when a tag set is
-	 * shared across request queues.
-	 */
-	atomic_t		nr_active;
-
 	/** @cpuhp_online: List to store request if CPU is going to die */
 	struct hlist_node	cpuhp_online;
 	/** @cpuhp_dead: List to store request if some CPU die. */
include/linux/blkdev.h +0 -2
index d463b9b5a0a5..0dd2a32068ec 100644
@@ -561,8 +561,6 @@ struct request_queue {
 	struct timer_list	timeout;
 	struct work_struct	timeout_work;
 
-	atomic_t		nr_active_requests_shared_tags;
-
 	struct blk_mq_tags	*sched_shared_tags;
 
 	struct list_head	icq_list;
Signature · 1 line
2.43.7
#7 lore raw

PATCH v2 3/3 scsi: use percpu counters for iorequest_cnt and iodone_cnt

In reply to Sumit Saxena

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 caused cache line contention between the submission and completion paths.

Represent these statistics with struct percpu_counter so increments are mostly local to each CPU, avoiding false sharing without growing struct scsi_device further for cache-line padding.

---
 drivers/scsi/scsi_error.c  |  2 +-
 drivers/scsi/scsi_lib.c    |  8 ++++----
 drivers/scsi/scsi_scan.c   |  9 +++++++++
 drivers/scsi/scsi_sysfs.c  | 27 +++++++++++++++++++++++----
 include/scsi/scsi_device.h |  5 +++--
 5 files changed, 40 insertions(+), 11 deletions(-)
drivers/scsi/scsi_error.c +1 -1
index 147127fb4db9..c7424ce92f3e 100644
@@ -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);
drivers/scsi/scsi_lib.c +4 -4
index 6e8c7a42603e..0b05cb63f630 100644
@@ -1554,7 +1554,7 @@ 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);
 
@@ -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)
drivers/scsi/scsi_scan.c +9 -0
index 9749a8dbe964..0b4fa89149af 100644
@@ -351,6 +351,15 @@ static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
 
 	scsi_sysfs_device_initialize(sdev);
 
+	ret = percpu_counter_init(&sdev->iorequest_cnt, 0, GFP_KERNEL);
+	if (ret)
+		goto out_device_destroy;
+	ret = percpu_counter_init(&sdev->iodone_cnt, 0, GFP_KERNEL);
+	if (ret) {
+		percpu_counter_destroy(&sdev->iorequest_cnt);
+		goto out_device_destroy;
+	}
+
 	if (scsi_device_is_pseudo_dev(sdev))
 		return sdev;
 
drivers/scsi/scsi_sysfs.c +23 -4
index dfc3559e7e04..1f5b2dc156a8 100644
@@ -516,6 +516,10 @@ static void scsi_device_dev_release(struct device *dev)
 	if (vpd_pgb7)
 		kfree_rcu(vpd_pgb7, rcu);
 	kfree(sdev->inquiry);
+	if (percpu_counter_initialized(&sdev->iodone_cnt))
+		percpu_counter_destroy(&sdev->iodone_cnt);
+	if (percpu_counter_initialized(&sdev->iorequest_cnt))
+		percpu_counter_destroy(&sdev->iorequest_cnt);
 	kfree(sdev);
 
 	if (parent)
@@ -936,11 +940,26 @@ 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);
+	/*
+	 * iorequest_cnt and iodone_cnt are per-CPU sums (s64); ioerr_cnt and
+	 * iotmo_cnt remain atomic_t.  Report the widest counter for tools.
+	 */
+	return snprintf(buf, 20, "%zu\n", sizeof(s64) * 8);
 }
 
 static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
 
+#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 = percpu_counter_sum(&sdev->field);	\
+	return snprintf(buf, 20, "0x%llx\n", count);			\
+}									\
+static DEVICE_ATTR(field, 0444, show_iostat_##field, NULL)
+
 #define show_sdev_iostat(field)						\
 static ssize_t								\
 show_iostat_##field(struct device *dev, struct device_attribute *attr,	\
@@ -950,10 +969,10 @@ show_iostat_##field(struct device *dev, struct device_attribute *attr,	\
 	unsigned long long count = atomic_read(&sdev->field);		\
 	return snprintf(buf, 20, "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_percpu(iorequest_cnt);
+show_sdev_iostat_percpu(iodone_cnt);
 show_sdev_iostat(ioerr_cnt);
 show_sdev_iostat(iotmo_cnt);
 
include/scsi/scsi_device.h +3 -2
index 9c2a7bbe5891..ad80b500ced9 100644
@@ -8,6 +8,7 @@
 #include <linux/blk-mq.h>
 #include <scsi/scsi.h>
 #include <linux/atomic.h>
+#include <linux/percpu_counter.h>
 #include <linux/sbitmap.h>
 
 struct bsg_device;
@@ -271,8 +272,8 @@ 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;
+	struct percpu_counter iorequest_cnt;
+	struct percpu_counter iodone_cnt;
 	atomic_t ioerr_cnt;
 	atomic_t iotmo_cnt;
 
Signature · 1 line
2.43.7
#8 lore raw

Re: [PATCH v2 3/3] scsi: use percpu counters for iorequest_cnt and iodone_cnt

drivers/scsi/scsi_sysfs.c static ssize_t · line 958

Quoted 7 lines · Sumit Saxena
drivers/scsi/scsi_sysfs.c +7 -0
+#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 = percpu_counter_sum(&sdev->field);	\

Why "unsigned long long" instead of "u64"? u64 values can be formatted with "%llx" since "u64" is a synonym for "unsigned long long".

drivers/scsi/scsi_sysfs.c static ssize_t · line 961

Quoted 3 lines
drivers/scsi/scsi_sysfs.c +3 -0
+	return snprintf(buf, 20, "0x%llx\n", count);			\
+}									\
+static DEVICE_ATTR(field, 0444, show_iostat_##field, NULL)

Why "snprintf(buf, 20, ...)"? New code should use sysfs_emit(buf, ...), isn't it?

Otherwise this patch looks good to me.

Thanks,

Bart.

#9 lore raw

Re: [PATCH v2 3/3] scsi: use percpu counters for iorequest_cnt and iodone_cnt

Quoted 2 lines

Why "snprintf(buf, 20, ...)"? New code should use sysfs_emit(buf, ...), isn't it?

Yes, I will handle it in the next revision.

Thanks,
Sumit

#10 lore raw

Re: [PATCH v2 3/3] scsi: use percpu counters for iorequest_cnt and iodone_cnt

In reply to Sumit Saxena
Quoted 78 lines · Sumit Saxena

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 caused cache line contention between the submission and completion paths.

Represent these statistics with struct percpu_counter so increments are mostly local to each CPU, avoiding false sharing without growing struct scsi_device further for cache-line padding.

---
  drivers/scsi/scsi_error.c  |  2 +-
  drivers/scsi/scsi_lib.c    |  8 ++++----
  drivers/scsi/scsi_scan.c   |  9 +++++++++
  drivers/scsi/scsi_sysfs.c  | 27 +++++++++++++++++++++++----
  include/scsi/scsi_device.h |  5 +++--
  5 files changed, 40 insertions(+), 11 deletions(-)
drivers/scsi/scsi_error.c +1 -1
index 147127fb4db9..c7424ce92f3e 100644
@@ -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);
drivers/scsi/scsi_lib.c +4 -4
index 6e8c7a42603e..0b05cb63f630 100644
@@ -1554,7 +1554,7 @@ 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);
 
@@ -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)
drivers/scsi/scsi_scan.c +0 -0
index 9749a8dbe964..0b4fa89149af 100644
@@ -351,6 +351,15 @@ static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
 

drivers/scsi/scsi_scan.c static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget, · line 361

Quoted 10 lines
drivers/scsi/scsi_scan.c +8 -0
  	scsi_sysfs_device_initialize(sdev);
 
+	ret = percpu_counter_init(&sdev->iorequest_cnt, 0, GFP_KERNEL);
+	if (ret)
+		goto out_device_destroy;
+	ret = percpu_counter_init(&sdev->iodone_cnt, 0, GFP_KERNEL);
+	if (ret) {
+		percpu_counter_destroy(&sdev->iorequest_cnt);
+		goto out_device_destroy;
+	}

it could be neater to have:

	if (percpu_counter_init(&sdev->iorequest_cnt, 0, GFP_KERNEL) ||
	    percpu_counter_init(&sdev->iodone_cnt, 0, GFP_KERNEL)) {
		err = some err;
		goto out_device_destroy;
	}	
Quoted 9 lines
drivers/scsi/scsi_scan.c +1 -0
+
  	if (scsi_device_is_pseudo_dev(sdev))
  		return sdev;
 
drivers/scsi/scsi_sysfs.c +0 -0
index dfc3559e7e04..1f5b2dc156a8 100644

drivers/scsi/scsi_sysfs.c static void scsi_device_dev_release(struct device *dev) · line 522

Quoted 7 lines
drivers/scsi/scsi_sysfs.c +4 -0
@@ -516,6 +516,10 @@ static void scsi_device_dev_release(struct device *dev)
  	if (vpd_pgb7)
  		kfree_rcu(vpd_pgb7, rcu);
  	kfree(sdev->inquiry);
+	if (percpu_counter_initialized(&sdev->iodone_cnt))
+		percpu_counter_destroy(&sdev->iodone_cnt);
+	if (percpu_counter_initialized(&sdev->iorequest_cnt))
+		percpu_counter_destroy(&sdev->iorequest_cnt);

Maybe I am wrong, but doesn't percpu_counter_destroy() handle the case of the percpu counter not being initialized? In other words, do we need the percpu_counter_initialized() checks?

Quoted 58 lines
drivers/scsi/scsi_sysfs.c +19 -4
  	kfree(sdev);
 
  	if (parent)
@@ -936,11 +940,26 @@ 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);
+	/*
+	 * iorequest_cnt and iodone_cnt are per-CPU sums (s64); ioerr_cnt and
+	 * iotmo_cnt remain atomic_t.  Report the widest counter for tools.
+	 */
+	return snprintf(buf, 20, "%zu\n", sizeof(s64) * 8);
  }
 
  static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
 
+#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 = percpu_counter_sum(&sdev->field);	\
+	return snprintf(buf, 20, "0x%llx\n", count);			\
+}									\
+static DEVICE_ATTR(field, 0444, show_iostat_##field, NULL)
+
  #define show_sdev_iostat(field)						\
  static ssize_t								\
  show_iostat_##field(struct device *dev, struct device_attribute *attr,	\
@@ -950,10 +969,10 @@ show_iostat_##field(struct device *dev, struct device_attribute *attr,	\
  	unsigned long long count = atomic_read(&sdev->field);		\
  	return snprintf(buf, 20, "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_percpu(iorequest_cnt);
+show_sdev_iostat_percpu(iodone_cnt);
  show_sdev_iostat(ioerr_cnt);
  show_sdev_iostat(iotmo_cnt);
 
include/scsi/scsi_device.h +1 -0
index 9c2a7bbe5891..ad80b500ced9 100644
@@ -8,6 +8,7 @@
  #include <linux/blk-mq.h>
  #include <scsi/scsi.h>
  #include <linux/atomic.h>
+#include <linux/percpu_counter.h>
  #include <linux/sbitmap.h>
 
  struct bsg_device;

include/scsi/scsi_device.h struct scsi_device { · line 279

Quoted 10 lines
include/scsi/scsi_device.h +2 -2
@@ -271,8 +272,8 @@ 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;
+	struct percpu_counter iorequest_cnt;
+	struct percpu_counter iodone_cnt;
  	atomic_t ioerr_cnt;
  	atomic_t iotmo_cnt;
 

Would it be simpler to make ioerr_cnt and iotmo_cnt also as percpu_counter? We could then drop some sysfs code for handling atomic_t counter.

I noticed that there is a percpu_counter_init_many() - maybe we could use that, and index into the array of counters. Note that I am not fimilar with that API, so it may not be a good idea.

#11 lore raw

Re: [PATCH v2 3/3] scsi: use percpu counters for iorequest_cnt and iodone_cnt

Quoted 92 lines · John Garry
Quoted 78 lines · Sumit Saxena

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 caused cache line contention between the submission and completion paths.

Represent these statistics with struct percpu_counter so increments are mostly local to each CPU, avoiding false sharing without growing struct scsi_device further for cache-line padding.

---
  drivers/scsi/scsi_error.c  |  2 +-
  drivers/scsi/scsi_lib.c    |  8 ++++----
  drivers/scsi/scsi_scan.c   |  9 +++++++++
  drivers/scsi/scsi_sysfs.c  | 27 +++++++++++++++++++++++----
  include/scsi/scsi_device.h |  5 +++--
  5 files changed, 40 insertions(+), 11 deletions(-)
drivers/scsi/scsi_error.c +1 -1
index 147127fb4db9..c7424ce92f3e 100644
@@ -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);
drivers/scsi/scsi_lib.c +4 -4
index 6e8c7a42603e..0b05cb63f630 100644
@@ -1554,7 +1554,7 @@ 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);
 
@@ -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)
drivers/scsi/scsi_scan.c +0 -0
index 9749a8dbe964..0b4fa89149af 100644
@@ -351,6 +351,15 @@ static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
 

drivers/scsi/scsi_scan.c static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget, · line 361

Quoted 10 lines
drivers/scsi/scsi_scan.c +8 -0
      scsi_sysfs_device_initialize(sdev);
 
+     ret = percpu_counter_init(&sdev->iorequest_cnt, 0, GFP_KERNEL);
+     if (ret)
+             goto out_device_destroy;
+     ret = percpu_counter_init(&sdev->iodone_cnt, 0, GFP_KERNEL);
+     if (ret) {
+             percpu_counter_destroy(&sdev->iorequest_cnt);
+             goto out_device_destroy;
+     }

it could be neater to have:

Quoted 6 lines
        if (percpu_counter_init(&sdev->iorequest_cnt, 0, GFP_KERNEL) ||
            percpu_counter_init(&sdev->iodone_cnt, 0, GFP_KERNEL)) {
                err = some err;
                goto out_device_destroy;
        }

Ack

Quoted 4 lines
Quoted 4 lines
drivers/scsi/scsi_scan.c +1 -0
+
      if (scsi_device_is_pseudo_dev(sdev))
              return sdev;
 
Quoted 16 lines

drivers/scsi/scsi_sysfs.c static void scsi_device_dev_release(struct device *dev) · line 522

Quoted 12 lines
drivers/scsi/scsi_sysfs.c +4 -0
index dfc3559e7e04..1f5b2dc156a8 100644
@@ -516,6 +516,10 @@ static void scsi_device_dev_release(struct device *dev)
      if (vpd_pgb7)
              kfree_rcu(vpd_pgb7, rcu);
      kfree(sdev->inquiry);
+     if (percpu_counter_initialized(&sdev->iodone_cnt))
+             percpu_counter_destroy(&sdev->iodone_cnt);
+     if (percpu_counter_initialized(&sdev->iorequest_cnt))
+             percpu_counter_destroy(&sdev->iorequest_cnt);

Maybe I am wrong, but doesn't percpu_counter_destroy() handle the case of the percpu counter not being initialized? In other words, do we need the percpu_counter_initialized() checks?

percpu_counter_initialized() checks are not required; I will drop them.

Quoted 62 lines
Quoted 62 lines
drivers/scsi/scsi_sysfs.c +19 -4
      kfree(sdev);
 
      if (parent)
@@ -936,11 +940,26 @@ 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);
+     /*
+      * iorequest_cnt and iodone_cnt are per-CPU sums (s64); ioerr_cnt and
+      * iotmo_cnt remain atomic_t.  Report the widest counter for tools.
+      */
+     return snprintf(buf, 20, "%zu\n", sizeof(s64) * 8);
  }
 
  static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
 
+#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 = percpu_counter_sum(&sdev->field);    \
+     return snprintf(buf, 20, "0x%llx\n", count);                    \
+}                                                                    \
+static DEVICE_ATTR(field, 0444, show_iostat_##field, NULL)
+
  #define show_sdev_iostat(field)                                             \
  static ssize_t                                                              \
  show_iostat_##field(struct device *dev, struct device_attribute *attr,      \
@@ -950,10 +969,10 @@ show_iostat_##field(struct device *dev, struct device_attribute *attr,  \
      unsigned long long count = atomic_read(&sdev->field);           \
      return snprintf(buf, 20, "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_percpu(iorequest_cnt);
+show_sdev_iostat_percpu(iodone_cnt);
  show_sdev_iostat(ioerr_cnt);
  show_sdev_iostat(iotmo_cnt);
 
include/scsi/scsi_device.h +1 -0
index 9c2a7bbe5891..ad80b500ced9 100644
@@ -8,6 +8,7 @@
  #include <linux/blk-mq.h>
  #include <scsi/scsi.h>
  #include <linux/atomic.h>
+#include <linux/percpu_counter.h>
  #include <linux/sbitmap.h>
 
  struct bsg_device;
@@ -271,8 +272,8 @@ struct scsi_device {
      unsigned int max_device_blocked; /* what device_blocked counts down from  */
  #define SCSI_DEFAULT_DEVICE_BLOCKED 3
 
Quoted 11 lines

include/scsi/scsi_device.h struct scsi_device { · line 279

Quoted 7 lines
include/scsi/scsi_device.h +2 -2
-     atomic_t iorequest_cnt;
-     atomic_t iodone_cnt;
+     struct percpu_counter iorequest_cnt;
+     struct percpu_counter iodone_cnt;
      atomic_t ioerr_cnt;
      atomic_t iotmo_cnt;
 

Would it be simpler to make ioerr_cnt and iotmo_cnt also as percpu_counter? We could then drop some sysfs code for handling atomic_t counter.

It looks trivial, I will handle it in next revision.

Quoted 4 lines

I noticed that there is a percpu_counter_init_many() - maybe we could use that, and index into the array of counters. Note that I am not fimilar with that API, so it may not be a good idea.

I need to check this. If it works well, I will handle it in the next version.