From nobody Thu Oct 9 13:18:00 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B60752E2658; Tue, 17 Jun 2025 13:43:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750167812; cv=none; b=Em7DKxsTsYxIyVt4COZ6JBCItuEE7qQgzv18z9pzhbl7G6ggQL1cKrZ72/Wz07STs6HfrIj6/JWcDAp7xSHEvqtFGcPpchwc7w1T8feJFlnujTgpe0B1iB/zilFRQKfVyQy12a8ZgvEgW7kJcuE5+WU/AvslU9/+mLbGJ9xopRU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750167812; c=relaxed/simple; bh=y28xAfylXh2rdRLTqKxx7aDOa1vFkQYxy1i35oempSA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=MoesUqbSVcnHqHaigrU1qZo9DfBcG3jMx4imCVxWvFZ2DPgCxPuHy3Nd0cpjr7eI0woLzIrjAJY0bpwuE/VRCsFDC/9/pfeu+VWTrx5TnKaLnGtIwDQyE8jqrC0OBItunifFNLUOST9NLDLsALOjWrp8f1nnWR+deqEsdwOmTuo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pssgh3Al; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="pssgh3Al" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DFE0DC4CEF1; Tue, 17 Jun 2025 13:43:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750167812; bh=y28xAfylXh2rdRLTqKxx7aDOa1vFkQYxy1i35oempSA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=pssgh3Al5vy6j/4p9KScq/o/Ti3hh1aeNJWZm4FJ/snp05kXRmmrz4S1i6c16IstP UrDa16/QS9nRYwLeHhWrEmCAbjxDbVTvxQ23QE3362vjm4LNPhpP1waumRGYKWwvkV LaLPAQsjWy2lWBTvofNeUOlqbjQDAs7jBEmEs6golgP+ahVxEHQV8ZbdDF5+oEKBC1 zs79+xsqtgL69aenOLInKqyQL7UGhAPfZlp/iopBsGbMI5hx+yHB2jVqXsSpRCwq7m RPNKULk4BKNw/6DANWA8GaRAlDVHfnaUqg68nQcNRUWdRfFtA8QdRRYUteKxAkn33U IIzgBXfnYnlog== From: Daniel Wagner Date: Tue, 17 Jun 2025 15:43:23 +0200 Subject: [PATCH 1/5] lib/group_cpus: Let group_cpu_evenly() return the number of initialized masks Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250617-isolcpus-queue-counters-v1-1-13923686b54b@kernel.org> References: <20250617-isolcpus-queue-counters-v1-0-13923686b54b@kernel.org> In-Reply-To: <20250617-isolcpus-queue-counters-v1-0-13923686b54b@kernel.org> To: Jens Axboe , Christoph Hellwig Cc: Keith Busch , Sagi Grimberg , "Michael S. Tsirkin" , "Martin K. Petersen" , Thomas Gleixner , Costa Shulyupin , Juri Lelli , Valentin Schneider , Waiman Long , Ming Lei , Frederic Weisbecker , Hannes Reinecke , linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-nvme@lists.infradead.org, megaraidlinux.pdl@broadcom.com, linux-scsi@vger.kernel.org, storagedev@microchip.com, virtualization@lists.linux.dev, GR-QLogic-Storage-Upstream@marvell.com, Daniel Wagner X-Mailer: b4 0.14.2 group_cpu_evenly() might have allocated less groups then requested: group_cpu_evenly() __group_cpus_evenly() alloc_nodes_groups() # allocated total groups may be less than numgrps when # active total CPU number is less then numgrps In this case, the caller will do an out of bound access because the caller assumes the masks returned has numgrps. Return the number of groups created so the caller can limit the access range accordingly. Acked-by: Thomas Gleixner Reviewed-by: Hannes Reinecke Reviewed-by: Ming Lei Signed-off-by: Daniel Wagner Reviewed-by: Chaitanya Kulkarni Reviewed-by: Christoph Hellwig --- block/blk-mq-cpumap.c | 6 +++--- drivers/virtio/virtio_vdpa.c | 9 +++++---- fs/fuse/virtio_fs.c | 6 +++--- include/linux/group_cpus.h | 2 +- kernel/irq/affinity.c | 11 +++++------ lib/group_cpus.c | 16 ++++++++-------- 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/block/blk-mq-cpumap.c b/block/blk-mq-cpumap.c index 444798c5374f48088b661b519f2638bda8556cf2..269161252add756897fce1b65ca= e5b2e6aebd647 100644 --- a/block/blk-mq-cpumap.c +++ b/block/blk-mq-cpumap.c @@ -19,9 +19,9 @@ void blk_mq_map_queues(struct blk_mq_queue_map *qmap) { const struct cpumask *masks; - unsigned int queue, cpu; + unsigned int queue, cpu, nr_masks; =20 - masks =3D group_cpus_evenly(qmap->nr_queues); + masks =3D group_cpus_evenly(qmap->nr_queues, &nr_masks); if (!masks) { for_each_possible_cpu(cpu) qmap->mq_map[cpu] =3D qmap->queue_offset; @@ -29,7 +29,7 @@ void blk_mq_map_queues(struct blk_mq_queue_map *qmap) } =20 for (queue =3D 0; queue < qmap->nr_queues; queue++) { - for_each_cpu(cpu, &masks[queue]) + for_each_cpu(cpu, &masks[queue % nr_masks]) qmap->mq_map[cpu] =3D qmap->queue_offset + queue; } kfree(masks); diff --git a/drivers/virtio/virtio_vdpa.c b/drivers/virtio/virtio_vdpa.c index 1f60c9d5cb1810a6f208c24bb2ac640d537391a0..a7b297dae4890c9d6002744b90f= c133bbedb7b44 100644 --- a/drivers/virtio/virtio_vdpa.c +++ b/drivers/virtio/virtio_vdpa.c @@ -329,20 +329,21 @@ create_affinity_masks(unsigned int nvecs, struct irq_= affinity *affd) =20 for (i =3D 0, usedvecs =3D 0; i < affd->nr_sets; i++) { unsigned int this_vecs =3D affd->set_size[i]; + unsigned int nr_masks; int j; - struct cpumask *result =3D group_cpus_evenly(this_vecs); + struct cpumask *result =3D group_cpus_evenly(this_vecs, &nr_masks); =20 if (!result) { kfree(masks); return NULL; } =20 - for (j =3D 0; j < this_vecs; j++) + for (j =3D 0; j < nr_masks; j++) cpumask_copy(&masks[curvec + j], &result[j]); kfree(result); =20 - curvec +=3D this_vecs; - usedvecs +=3D this_vecs; + curvec +=3D nr_masks; + usedvecs +=3D nr_masks; } =20 /* Fill out vectors at the end that don't need affinity */ diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c index 53c2626e90e723ad88f1aee69d7507b4f197ab13..3fbfb1a2942b753643015a45fa0= c5d89ff72aa2f 100644 --- a/fs/fuse/virtio_fs.c +++ b/fs/fuse/virtio_fs.c @@ -862,7 +862,7 @@ static void virtio_fs_requests_done_work(struct work_st= ruct *work) static void virtio_fs_map_queues(struct virtio_device *vdev, struct virtio= _fs *fs) { const struct cpumask *mask, *masks; - unsigned int q, cpu; + unsigned int q, cpu, nr_masks; =20 /* First attempt to map using existing transport layer affinities * e.g. PCIe MSI-X @@ -882,7 +882,7 @@ static void virtio_fs_map_queues(struct virtio_device *= vdev, struct virtio_fs *f return; fallback: /* Attempt to map evenly in groups over the CPUs */ - masks =3D group_cpus_evenly(fs->num_request_queues); + masks =3D group_cpus_evenly(fs->num_request_queues, &nr_masks); /* If even this fails we default to all CPUs use first request queue */ if (!masks) { for_each_possible_cpu(cpu) @@ -891,7 +891,7 @@ static void virtio_fs_map_queues(struct virtio_device *= vdev, struct virtio_fs *f } =20 for (q =3D 0; q < fs->num_request_queues; q++) { - for_each_cpu(cpu, &masks[q]) + for_each_cpu(cpu, &masks[q % nr_masks]) fs->mq_map[cpu] =3D q + VQ_REQUEST; } kfree(masks); diff --git a/include/linux/group_cpus.h b/include/linux/group_cpus.h index e42807ec61f6e8cf3787af7daa0d8686edfef0a3..9d4e5ab6c314b31c09fda82c3f6= ac18f77e9de36 100644 --- a/include/linux/group_cpus.h +++ b/include/linux/group_cpus.h @@ -9,6 +9,6 @@ #include #include =20 -struct cpumask *group_cpus_evenly(unsigned int numgrps); +struct cpumask *group_cpus_evenly(unsigned int numgrps, unsigned int *numm= asks); =20 #endif diff --git a/kernel/irq/affinity.c b/kernel/irq/affinity.c index 44a4eba80315cc098ecfa366ca1d88483641b12a..4013e6ad2b2f1cb91de12bb428b= 3281105f7d23b 100644 --- a/kernel/irq/affinity.c +++ b/kernel/irq/affinity.c @@ -69,21 +69,20 @@ irq_create_affinity_masks(unsigned int nvecs, struct ir= q_affinity *affd) * have multiple sets, build each sets affinity mask separately. */ for (i =3D 0, usedvecs =3D 0; i < affd->nr_sets; i++) { - unsigned int this_vecs =3D affd->set_size[i]; - int j; - struct cpumask *result =3D group_cpus_evenly(this_vecs); + unsigned int nr_masks, this_vecs =3D affd->set_size[i]; + struct cpumask *result =3D group_cpus_evenly(this_vecs, &nr_masks); =20 if (!result) { kfree(masks); return NULL; } =20 - for (j =3D 0; j < this_vecs; j++) + for (int j =3D 0; j < nr_masks; j++) cpumask_copy(&masks[curvec + j].mask, &result[j]); kfree(result); =20 - curvec +=3D this_vecs; - usedvecs +=3D this_vecs; + curvec +=3D nr_masks; + usedvecs +=3D nr_masks; } =20 /* Fill out vectors at the end that don't need affinity */ diff --git a/lib/group_cpus.c b/lib/group_cpus.c index ee272c4cefcc13907ce9f211f479615d2e3c9154..a075959ccb212ece84334e4859c= 884f4217d30b6 100644 --- a/lib/group_cpus.c +++ b/lib/group_cpus.c @@ -332,9 +332,11 @@ static int __group_cpus_evenly(unsigned int startgrp, = unsigned int numgrps, /** * group_cpus_evenly - Group all CPUs evenly per NUMA/CPU locality * @numgrps: number of groups + * @nummasks: number of initialized cpumasks * * Return: cpumask array if successful, NULL otherwise. And each element - * includes CPUs assigned to this group + * includes CPUs assigned to this group. nummasks contains the number + * of initialized masks which can be less than numgrps. * * Try to put close CPUs from viewpoint of CPU and NUMA locality into * same group, and run two-stage grouping: @@ -344,7 +346,7 @@ static int __group_cpus_evenly(unsigned int startgrp, u= nsigned int numgrps, * We guarantee in the resulted grouping that all CPUs are covered, and * no same CPU is assigned to multiple groups */ -struct cpumask *group_cpus_evenly(unsigned int numgrps) +struct cpumask *group_cpus_evenly(unsigned int numgrps, unsigned int *numm= asks) { unsigned int curgrp =3D 0, nr_present =3D 0, nr_others =3D 0; cpumask_var_t *node_to_cpumask; @@ -386,7 +388,7 @@ struct cpumask *group_cpus_evenly(unsigned int numgrps) ret =3D __group_cpus_evenly(curgrp, numgrps, node_to_cpumask, npresmsk, nmsk, masks); if (ret < 0) - goto fail_build_affinity; + goto fail_node_to_cpumask; nr_present =3D ret; =20 /* @@ -405,10 +407,6 @@ struct cpumask *group_cpus_evenly(unsigned int numgrps) if (ret >=3D 0) nr_others =3D ret; =20 - fail_build_affinity: - if (ret >=3D 0) - WARN_ON(nr_present + nr_others < numgrps); - fail_node_to_cpumask: free_node_to_cpumask(node_to_cpumask); =20 @@ -421,10 +419,11 @@ struct cpumask *group_cpus_evenly(unsigned int numgrp= s) kfree(masks); return NULL; } + *nummasks =3D min(nr_present + nr_others, numgrps); return masks; } #else /* CONFIG_SMP */ -struct cpumask *group_cpus_evenly(unsigned int numgrps) +struct cpumask *group_cpus_evenly(unsigned int numgrps, unsigned int *numm= asks) { struct cpumask *masks =3D kcalloc(numgrps, sizeof(*masks), GFP_KERNEL); =20 @@ -433,6 +432,7 @@ struct cpumask *group_cpus_evenly(unsigned int numgrps) =20 /* assign all CPUs(cpu 0) to the 1st group only */ cpumask_copy(&masks[0], cpu_possible_mask); + *nummasks =3D 1; return masks; } #endif /* CONFIG_SMP */ --=20 2.49.0 From nobody Thu Oct 9 13:18:00 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E99A52D0289; Tue, 17 Jun 2025 13:43:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750167816; cv=none; b=Drm6Y+eqsbv2lLlHBxuLdEytCILZJ7uYnqiJuumntnM89RWqfPLJaR4d3Jok+eQawEfp+naqq0z9nqOV/iviSmlqk5p7g7HbFRowWL3/DTMX/j2C0RyqxGsfli0WJD4aGxu0+GyumfWtUvuBeVr59e7MZRWwwZCuLxKGILzhnHI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750167816; c=relaxed/simple; bh=vkcmDs7WGjxfLxosZoIsjGpUt+4BhL+w9Kzg93Ijs90=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WSf3NSCyQVfz5k487igaPYzqBSkEQfhsFT0rCtle4Jz6D2xb65eqtCuQHO4clo6Je/YsG2nGbhWW46w0Rk1IXPtRgqlIUzCuC/LVE73Tj1YTwkJSVkZk383RbWu3ZTD9qq3XobOPKXROgTMZs2dcK6R7ZxaukoTP9XFdmNpGVm0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dEiCsE+e; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="dEiCsE+e" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0125DC4CEEE; Tue, 17 Jun 2025 13:43:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750167815; bh=vkcmDs7WGjxfLxosZoIsjGpUt+4BhL+w9Kzg93Ijs90=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=dEiCsE+erRluo9qPNFrTv/hsTg3Ka9jGx0gzfDA91T6m/I484aVd9RuVekzieGHSl kNCn3K6/Q5bdiSj/NEzsdUTbAo/jdBVT+DsCD3doGvZtT9/P0Aq+0eEPMNQg1t28Wy 3d5p7yCaEaBGI2BVoqWgycxMufw3OYGfkubreNMfG/9i7aCB/+D8b1sdEWnpieAwMa Cn3q1iCqf95J5AFFesQgOUIUdnykk8fP1xmZucH3+Lx3HYPZ8uJcOkgKxi9BbIy3jM U3unwgDCdzL8P11zC7+wC4tS1wfdNwQBTGTOnlO+ZIe+w8SMJVBhhaYlPqoHY/IRMb WJTyBAKzdKTSw== From: Daniel Wagner Date: Tue, 17 Jun 2025 15:43:24 +0200 Subject: [PATCH 2/5] blk-mq: add number of queue calc helper Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250617-isolcpus-queue-counters-v1-2-13923686b54b@kernel.org> References: <20250617-isolcpus-queue-counters-v1-0-13923686b54b@kernel.org> In-Reply-To: <20250617-isolcpus-queue-counters-v1-0-13923686b54b@kernel.org> To: Jens Axboe , Christoph Hellwig Cc: Keith Busch , Sagi Grimberg , "Michael S. Tsirkin" , "Martin K. Petersen" , Thomas Gleixner , Costa Shulyupin , Juri Lelli , Valentin Schneider , Waiman Long , Ming Lei , Frederic Weisbecker , Hannes Reinecke , linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-nvme@lists.infradead.org, megaraidlinux.pdl@broadcom.com, linux-scsi@vger.kernel.org, storagedev@microchip.com, virtualization@lists.linux.dev, GR-QLogic-Storage-Upstream@marvell.com, Daniel Wagner X-Mailer: b4 0.14.2 Add two variants of helper functions that calculate the correct number of queues to use. Two variants are needed because some drivers base their maximum number of queues on the possible CPU mask, while others use the online CPU mask. Reviewed-by: Christoph Hellwig Reviewed-by: Hannes Reinecke Reviewed-by: Ming Lei Signed-off-by: Daniel Wagner Reviewed-by: Chaitanya Kulkarni --- block/blk-mq-cpumap.c | 40 ++++++++++++++++++++++++++++++++++++++++ include/linux/blk-mq.h | 2 ++ 2 files changed, 42 insertions(+) diff --git a/block/blk-mq-cpumap.c b/block/blk-mq-cpumap.c index 269161252add756897fce1b65cae5b2e6aebd647..705da074ad6c7e88042296f21b7= 39c6d686a72b6 100644 --- a/block/blk-mq-cpumap.c +++ b/block/blk-mq-cpumap.c @@ -12,10 +12,50 @@ #include #include #include +#include =20 #include "blk.h" #include "blk-mq.h" =20 +static unsigned int blk_mq_num_queues(const struct cpumask *mask, + unsigned int max_queues) +{ + unsigned int num; + + num =3D cpumask_weight(mask); + return min_not_zero(num, max_queues); +} + +/** + * blk_mq_num_possible_queues - Calc nr of queues for multiqueue devices + * @max_queues: The maximum number of queues the hardware/driver + * supports. If max_queues is 0, the argument is + * ignored. + * + * Calculates the number of queues to be used for a multiqueue + * device based on the number of possible CPUs. + */ +unsigned int blk_mq_num_possible_queues(unsigned int max_queues) +{ + return blk_mq_num_queues(cpu_possible_mask, max_queues); +} +EXPORT_SYMBOL_GPL(blk_mq_num_possible_queues); + +/** + * blk_mq_num_online_queues - Calc nr of queues for multiqueue devices + * @max_queues: The maximum number of queues the hardware/driver + * supports. If max_queues is 0, the argument is + * ignored. + * + * Calculates the number of queues to be used for a multiqueue + * device based on the number of online CPUs. + */ +unsigned int blk_mq_num_online_queues(unsigned int max_queues) +{ + return blk_mq_num_queues(cpu_online_mask, max_queues); +} +EXPORT_SYMBOL_GPL(blk_mq_num_online_queues); + void blk_mq_map_queues(struct blk_mq_queue_map *qmap) { const struct cpumask *masks; diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h index de8c85a03bb7f40501f449ae98919a5352f55db8..2a5a828f19a0ba6ff0812daf40e= ed67f0e12ada1 100644 --- a/include/linux/blk-mq.h +++ b/include/linux/blk-mq.h @@ -947,6 +947,8 @@ int blk_mq_freeze_queue_wait_timeout(struct request_que= ue *q, void blk_mq_unfreeze_queue_non_owner(struct request_queue *q); void blk_freeze_queue_start_non_owner(struct request_queue *q); =20 +unsigned int blk_mq_num_possible_queues(unsigned int max_queues); +unsigned int blk_mq_num_online_queues(unsigned int max_queues); void blk_mq_map_queues(struct blk_mq_queue_map *qmap); void blk_mq_map_hw_queues(struct blk_mq_queue_map *qmap, struct device *dev, unsigned int offset); --=20 2.49.0 From nobody Thu Oct 9 13:18:00 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C451E2E3AE4; Tue, 17 Jun 2025 13:43:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750167818; cv=none; b=IazezUMDINt8lMjeJn3fEO11JY1C/zPe+4ckJFiQcIv+RiDgpvhOiEiFnWagiadWBgOgqGq3W7kmtWkGbPuiqfDZrUzTtIZ2b6BQcDDal1m+o0YVF822C6EeYvwzT/xqP+huUo4iUFP4VmgbAaUM2K+AEG4m6rlF0w6UAE79AfY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750167818; c=relaxed/simple; bh=Wb0VVks1in8/wgvZy69kc6dxGdTcBSbAW1IQk4SRnXw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=qaL0JY+oS3NuLjDodLH7bPoaPyoYBTnPwGfgVYAVqAWtrgCzbb5LPfFoZhrmpDv3o95OvQWjAyi3uDWZyIE7exdr1RZH3VurHRb37dWdqfwgyHL5FqnSYhHt+HgzDyDV7Ntd2tyc3uEJD4tXWQ0bqAh5roNAmmB1mVJPNqkFBPA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hBzVWER+; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hBzVWER+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1B5C4C4CEF1; Tue, 17 Jun 2025 13:43:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750167818; bh=Wb0VVks1in8/wgvZy69kc6dxGdTcBSbAW1IQk4SRnXw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=hBzVWER+QS2jJkuriCQIa4LyuNFDsINLSqZCoogErBxBKw4JN1mmn4iu72E0jirch PPnW/Y1vgakKthwwc4M7/IgTA11CG1K9WXqrxvI7UBiPUmpUZeXZasTZMVjKqF9rZN wB9wN+xmKxNTUjLXYBrKzNSqtwe+LyJgmSEErCtTTaIy73h77pqb03mf1b47bI6KRl rFpYCVyw0NRmytgwI+oB2GyenYLXZclZ13elJZVH4WEwZmfT7sAjBBWpB1fXvCfSK1 UjCnqQNpzzbSbhTlnroe9JaSpHIxhBaQ58/M9I8M1VS/vXpNAW0LIXqPWHhPXueZtq N41rxh/7TPJQQ== From: Daniel Wagner Date: Tue, 17 Jun 2025 15:43:25 +0200 Subject: [PATCH 3/5] nvme-pci: use block layer helpers to calculate num of queues Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250617-isolcpus-queue-counters-v1-3-13923686b54b@kernel.org> References: <20250617-isolcpus-queue-counters-v1-0-13923686b54b@kernel.org> In-Reply-To: <20250617-isolcpus-queue-counters-v1-0-13923686b54b@kernel.org> To: Jens Axboe , Christoph Hellwig Cc: Keith Busch , Sagi Grimberg , "Michael S. Tsirkin" , "Martin K. Petersen" , Thomas Gleixner , Costa Shulyupin , Juri Lelli , Valentin Schneider , Waiman Long , Ming Lei , Frederic Weisbecker , Hannes Reinecke , linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-nvme@lists.infradead.org, megaraidlinux.pdl@broadcom.com, linux-scsi@vger.kernel.org, storagedev@microchip.com, virtualization@lists.linux.dev, GR-QLogic-Storage-Upstream@marvell.com, Daniel Wagner X-Mailer: b4 0.14.2 The calculation of the upper limit for queues does not depend solely on the number of possible CPUs; for example, the isolcpus kernel command-line option must also be considered. To account for this, the block layer provides a helper function to retrieve the maximum number of queues. Use it to set an appropriate upper queue number limit. Reviewed-by: Christoph Hellwig Reviewed-by: Hannes Reinecke Signed-off-by: Daniel Wagner Reviewed-by: Chaitanya Kulkarni --- drivers/nvme/host/pci.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c index 8ff12e415cb5d1529d760b33f3e0cf3b8d1555f1..f134bf4f41b2581e4809e618250= de7985b5c9701 100644 --- a/drivers/nvme/host/pci.c +++ b/drivers/nvme/host/pci.c @@ -97,7 +97,7 @@ static int io_queue_count_set(const char *val, const stru= ct kernel_param *kp) int ret; =20 ret =3D kstrtouint(val, 10, &n); - if (ret !=3D 0 || n > num_possible_cpus()) + if (ret !=3D 0 || n > blk_mq_num_possible_queues(0)) return -EINVAL; return param_set_uint(val, kp); } @@ -2520,7 +2520,8 @@ static unsigned int nvme_max_io_queues(struct nvme_de= v *dev) */ if (dev->ctrl.quirks & NVME_QUIRK_SHARED_TAGS) return 1; - return num_possible_cpus() + dev->nr_write_queues + dev->nr_poll_queues; + return blk_mq_num_possible_queues(0) + dev->nr_write_queues + + dev->nr_poll_queues; } =20 static int nvme_setup_io_queues(struct nvme_dev *dev) --=20 2.49.0 From nobody Thu Oct 9 13:18:00 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7FB032E2650; Tue, 17 Jun 2025 13:43:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750167825; cv=none; b=I65F52d8EbfkK2FmaCThZ3Jc+9hpZQD38zeXu0P9Ofw3SPLdhoqJlUB4jy8AcT1il81XrouuMSKqvHi/gvJ+p8NWdHb6qawxlSHZC963ua+8fTuOHM06Sfx2QahUqeadH5jeGLqm5UaqPq6YcTpF+PVXl4mqumCR1mvr6yUsRo4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750167825; c=relaxed/simple; bh=Jn0Twj4tKAN0mdT5Cb3ACM11+GkGPhGeiq1Yq1DEbb0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VXagw9lgIOWCV73FbWFkSjCeRSBnYJDclKp7tjVlT/StHG3WTvqGI7ikqiAdKd5UmIN1A99Y2nvM2bkleGAVNDCnHTa48Ws6NQcp2eANh8BLVcyFwXwf7iR+jODr4jqCY9h6OXVUsMNaDGyC8rRlyW+DfzN/sVVW7tvBHXmkkCQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=L5J32dvF; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="L5J32dvF" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4DC66C4CEE3; Tue, 17 Jun 2025 13:43:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750167825; bh=Jn0Twj4tKAN0mdT5Cb3ACM11+GkGPhGeiq1Yq1DEbb0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=L5J32dvFnKy+dXBZJdxV3EsEqTXgLluCyDH3EWNaLXOXYwQ/tziWHmQwB7I79uPAn CLRIpxD4Gmp/lh55zXOBmmwxnMgLnEmJqgEPtdd+LUbkur58rgLYsA/hwlJohsrxBF c0ukZlAHISJHonKyMLdz20dZ9p7sH22pi2hNwNVWdcrTmPGtsZw/c+hqsv/PUYjXO2 mpZxblXSSvQrjsOh+XD1nUbtcJul7Xif8WZRMVdAJKqNorvPtyQwfjZxxTcAGNdb8O ibUi/jLY2O11H4MQA5Qo/iAwChJgXDGdK7FiRFGz3N8MxrXcZgM3t6h//G0h7yzzfs 7IdIuPFh8P5BQ== From: Daniel Wagner Date: Tue, 17 Jun 2025 15:43:26 +0200 Subject: [PATCH 4/5] scsi: use block layer helpers to calculate num of queues Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250617-isolcpus-queue-counters-v1-4-13923686b54b@kernel.org> References: <20250617-isolcpus-queue-counters-v1-0-13923686b54b@kernel.org> In-Reply-To: <20250617-isolcpus-queue-counters-v1-0-13923686b54b@kernel.org> To: Jens Axboe , Christoph Hellwig Cc: Keith Busch , Sagi Grimberg , "Michael S. Tsirkin" , "Martin K. Petersen" , Thomas Gleixner , Costa Shulyupin , Juri Lelli , Valentin Schneider , Waiman Long , Ming Lei , Frederic Weisbecker , Hannes Reinecke , linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-nvme@lists.infradead.org, megaraidlinux.pdl@broadcom.com, linux-scsi@vger.kernel.org, storagedev@microchip.com, virtualization@lists.linux.dev, GR-QLogic-Storage-Upstream@marvell.com, Daniel Wagner X-Mailer: b4 0.14.2 The calculation of the upper limit for queues does not depend solely on the number of online CPUs; for example, the isolcpus kernel command-line option must also be considered. To account for this, the block layer provides a helper function to retrieve the maximum number of queues. Use it to set an appropriate upper queue number limit. Reviewed-by: Martin K. Petersen Reviewed-by: Hannes Reinecke Reviewed-by: Ming Lei Signed-off-by: Daniel Wagner Reviewed-by: Chaitanya Kulkarni --- drivers/scsi/megaraid/megaraid_sas_base.c | 15 +++++++++------ drivers/scsi/qla2xxx/qla_isr.c | 10 +++++----- drivers/scsi/smartpqi/smartpqi_init.c | 5 ++--- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/drivers/scsi/megaraid/megaraid_sas_base.c b/drivers/scsi/megar= aid/megaraid_sas_base.c index 3aac0e17cb00612ed7b6fb4a2e8745c7120fc506..0224eb97092bd938250f108522d= aaf9f033c1a4d 100644 --- a/drivers/scsi/megaraid/megaraid_sas_base.c +++ b/drivers/scsi/megaraid/megaraid_sas_base.c @@ -5967,7 +5967,8 @@ megasas_alloc_irq_vectors(struct megasas_instance *in= stance) else instance->iopoll_q_count =3D 0; =20 - num_msix_req =3D num_online_cpus() + instance->low_latency_index_start; + num_msix_req =3D blk_mq_num_online_queues(0) + + instance->low_latency_index_start; instance->msix_vectors =3D min(num_msix_req, instance->msix_vectors); =20 @@ -5983,7 +5984,8 @@ megasas_alloc_irq_vectors(struct megasas_instance *in= stance) /* Disable Balanced IOPS mode and try realloc vectors */ instance->perf_mode =3D MR_LATENCY_PERF_MODE; instance->low_latency_index_start =3D 1; - num_msix_req =3D num_online_cpus() + instance->low_latency_index_start; + num_msix_req =3D blk_mq_num_online_queues(0) + + instance->low_latency_index_start; =20 instance->msix_vectors =3D min(num_msix_req, instance->msix_vectors); @@ -6239,7 +6241,7 @@ static int megasas_init_fw(struct megasas_instance *i= nstance) intr_coalescing =3D (scratch_pad_1 & MR_INTR_COALESCING_SUPPORT_OFFSET) ? true : false; if (intr_coalescing && - (num_online_cpus() >=3D MR_HIGH_IOPS_QUEUE_COUNT) && + (blk_mq_num_online_queues(0) >=3D MR_HIGH_IOPS_QUEUE_COUNT) && (instance->msix_vectors =3D=3D MEGASAS_MAX_MSIX_QUEUES)) instance->perf_mode =3D MR_BALANCED_PERF_MODE; else @@ -6283,7 +6285,8 @@ static int megasas_init_fw(struct megasas_instance *i= nstance) else instance->low_latency_index_start =3D 1; =20 - num_msix_req =3D num_online_cpus() + instance->low_latency_index_start; + num_msix_req =3D blk_mq_num_online_queues(0) + + instance->low_latency_index_start; =20 instance->msix_vectors =3D min(num_msix_req, instance->msix_vectors); @@ -6315,8 +6318,8 @@ static int megasas_init_fw(struct megasas_instance *i= nstance) megasas_setup_reply_map(instance); =20 dev_info(&instance->pdev->dev, - "current msix/online cpus\t: (%d/%d)\n", - instance->msix_vectors, (unsigned int)num_online_cpus()); + "current msix/max num queues\t: (%d/%u)\n", + instance->msix_vectors, blk_mq_num_online_queues(0)); dev_info(&instance->pdev->dev, "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled"); =20 diff --git a/drivers/scsi/qla2xxx/qla_isr.c b/drivers/scsi/qla2xxx/qla_isr.c index fe98c76e9be32ff03a1960f366f0d700d1168383..c4c6b5c6658c0734f7ff68bcc31= b33dde87296dd 100644 --- a/drivers/scsi/qla2xxx/qla_isr.c +++ b/drivers/scsi/qla2xxx/qla_isr.c @@ -4533,13 +4533,13 @@ qla24xx_enable_msix(struct qla_hw_data *ha, struct = rsp_que *rsp) if (USER_CTRL_IRQ(ha) || !ha->mqiobase) { /* user wants to control IRQ setting for target mode */ ret =3D pci_alloc_irq_vectors(ha->pdev, min_vecs, - min((u16)ha->msix_count, (u16)(num_online_cpus() + min_vecs)), - PCI_IRQ_MSIX); + blk_mq_num_online_queues(ha->msix_count) + min_vecs, + PCI_IRQ_MSIX); } else ret =3D pci_alloc_irq_vectors_affinity(ha->pdev, min_vecs, - min((u16)ha->msix_count, (u16)(num_online_cpus() + min_vecs)), - PCI_IRQ_MSIX | PCI_IRQ_AFFINITY, - &desc); + blk_mq_num_online_queues(ha->msix_count) + min_vecs, + PCI_IRQ_MSIX | PCI_IRQ_AFFINITY, + &desc); =20 if (ret < 0) { ql_log(ql_log_fatal, vha, 0x00c7, diff --git a/drivers/scsi/smartpqi/smartpqi_init.c b/drivers/scsi/smartpqi/= smartpqi_init.c index 3d40a63e378d792ffc005c51cb2fdbb04e1acc5f..125944941601e683e9aa9d4fc6a= 346230bef904b 100644 --- a/drivers/scsi/smartpqi/smartpqi_init.c +++ b/drivers/scsi/smartpqi/smartpqi_init.c @@ -5294,15 +5294,14 @@ static void pqi_calculate_queue_resources(struct pq= i_ctrl_info *ctrl_info) if (is_kdump_kernel()) { num_queue_groups =3D 1; } else { - int num_cpus; int max_queue_groups; =20 max_queue_groups =3D min(ctrl_info->max_inbound_queues / 2, ctrl_info->max_outbound_queues - 1); max_queue_groups =3D min(max_queue_groups, PQI_MAX_QUEUE_GROUPS); =20 - num_cpus =3D num_online_cpus(); - num_queue_groups =3D min(num_cpus, ctrl_info->max_msix_vectors); + num_queue_groups =3D + blk_mq_num_online_queues(ctrl_info->max_msix_vectors); num_queue_groups =3D min(num_queue_groups, max_queue_groups); } =20 --=20 2.49.0 From nobody Thu Oct 9 13:18:00 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A49892E888F; Tue, 17 Jun 2025 13:43:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750167828; cv=none; b=qq8EQCiMa/G/g9xS3Llqyp8DMK7BIikOIUnMxusp330BIgJxwhYQpxvjY5j/m6GaJidvQZ4nxlrKDl+xIygtGp/tyl7jDCRJEuW9c+Xl+LuCkK1ImN4P0/PzjtSUFXCdkcztFwgvByStixFtOQOEGglDmG1qKlciIVvewxe/2bM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750167828; c=relaxed/simple; bh=3edJmKoyUSjMTYlt2oNOEj/NOQQ/uX8bqeMbrf4iMm8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ebvZ06CTT6mtJZqiplYvAuyrf+yl1eYnmdqkzRpppMtsO//6YjIJ/xbCM2zBjaxB4BV3ipkU92Bwpd8a8uteJK4qLY5LKSWegRpLd+uCV6qRsxJUZ4P34H6rcKO6bJqDjEkUOiuiRJvyDxdAAuD/iZrWD3189Wcr13UikpBBMfU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MNYoPSYB; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MNYoPSYB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9D687C4CEE3; Tue, 17 Jun 2025 13:43:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750167828; bh=3edJmKoyUSjMTYlt2oNOEj/NOQQ/uX8bqeMbrf4iMm8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=MNYoPSYBKQ2E60FbE8av7UdKTeFZsKp3S1eSfPUOuJJhZWJ4nSpsIQXDbIrEEGshZ iHANFRqB4+ogYxSrGyADEDNwXR5u9jn2P6HBuTrlP2slsKz9o9Y39tMu8j3JUETGla 83P1lNhc+taMFmbuSnKF2vIa4IKya0LP157KDLSkK0r3Q2gUfbztIrS8O/MKw3fRIX gybwsEBPDiv3IDfOsLObyxSmGNQUIywBltt2JdllGdeuTEH4GMk/v1HYtlRutH1kQM gpX41cp5n6zrWZjqstS3pKlN0WuFir/IqypxXI9urvSnxaGOh2xDOa1rhL2FCsp6t9 tYOyyzhTcmPsw== From: Daniel Wagner Date: Tue, 17 Jun 2025 15:43:27 +0200 Subject: [PATCH 5/5] virtio: blk/scsi: use block layer helpers to calculate num of queues Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250617-isolcpus-queue-counters-v1-5-13923686b54b@kernel.org> References: <20250617-isolcpus-queue-counters-v1-0-13923686b54b@kernel.org> In-Reply-To: <20250617-isolcpus-queue-counters-v1-0-13923686b54b@kernel.org> To: Jens Axboe , Christoph Hellwig Cc: Keith Busch , Sagi Grimberg , "Michael S. Tsirkin" , "Martin K. Petersen" , Thomas Gleixner , Costa Shulyupin , Juri Lelli , Valentin Schneider , Waiman Long , Ming Lei , Frederic Weisbecker , Hannes Reinecke , linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-nvme@lists.infradead.org, megaraidlinux.pdl@broadcom.com, linux-scsi@vger.kernel.org, storagedev@microchip.com, virtualization@lists.linux.dev, GR-QLogic-Storage-Upstream@marvell.com, Daniel Wagner X-Mailer: b4 0.14.2 The calculation of the upper limit for queues does not depend solely on the number of possible CPUs; for example, the isolcpus kernel command-line option must also be considered. To account for this, the block layer provides a helper function to retrieve the maximum number of queues. Use it to set an appropriate upper queue number limit. Reviewed-by: Christoph Hellwig Acked-by: Michael S. Tsirkin Reviewed-by: Hannes Reinecke Reviewed-by: Ming Lei Signed-off-by: Daniel Wagner Reviewed-by: Chaitanya Kulkarni --- drivers/block/virtio_blk.c | 5 ++--- drivers/scsi/virtio_scsi.c | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c index 30bca8cb7106040d3bbb11ba9e0b546510534324..e649fa67bac16b4f0c6e8e8f0e6= bec111897c355 100644 --- a/drivers/block/virtio_blk.c +++ b/drivers/block/virtio_blk.c @@ -976,9 +976,8 @@ static int init_vq(struct virtio_blk *vblk) return -EINVAL; } =20 - num_vqs =3D min_t(unsigned int, - min_not_zero(num_request_queues, nr_cpu_ids), - num_vqs); + num_vqs =3D blk_mq_num_possible_queues( + min_not_zero(num_request_queues, num_vqs)); =20 num_poll_vqs =3D min_t(unsigned int, poll_queues, num_vqs - 1); =20 diff --git a/drivers/scsi/virtio_scsi.c b/drivers/scsi/virtio_scsi.c index 21ce3e9401929cd273fde08b0944e8b47e1e66cc..96a69edddbe5555574fc8fed1ba= 7c82a99df4472 100644 --- a/drivers/scsi/virtio_scsi.c +++ b/drivers/scsi/virtio_scsi.c @@ -919,6 +919,7 @@ static int virtscsi_probe(struct virtio_device *vdev) /* We need to know how many queues before we allocate. */ num_queues =3D virtscsi_config_get(vdev, num_queues) ? : 1; num_queues =3D min_t(unsigned int, nr_cpu_ids, num_queues); + num_queues =3D blk_mq_num_possible_queues(num_queues); =20 num_targets =3D virtscsi_config_get(vdev, max_target) + 1; =20 --=20 2.49.0